var display_speed = 'fast', zipInputBG;

$(document).ready(function()
{
		$('em.error').remove();
		
		$('#form_final').append($('#form_temp'));
		$('#form_final_interior').append($('#quote_form_wrapper'));
		$('#header').append($('#nav'));
		$('#content').prepend($('#temp_right_col'));
		$('#partner_logos').append($('#temp_partner_logos'));
        $('#promo').append($('#temp_promo').html());
        $('#temp_promo').remove();
		
		$('.partner_with_us textarea').click(function(){
   			this.select();
   			return false;
		});
    
		$('ul.nav li a:not(.active) img').hover(function(){
			var src = $(this).attr('src');
			var	pos = src.lastIndexOf('.gif');
			var new_src = src.substring(0,pos);
			 		new_src += '-hover.gif';
					$(this).attr('src',new_src);
		},function(){
			var src = $(this).attr('src');
			var	pos = src.lastIndexOf('-hover.gif');
			var new_src = src.substring(0,pos);
			 		new_src += '.gif';
					$(this).attr('src',new_src);
		});

       $('#home_form_a').click(function(){
         $('#theform').submit();
         return false;
       }); 
    
		//hide loading gif (IE fix) - must be added to DOM while visible.
		$('#loading').hide();
		
		$('#form_a').click(function(){
		  $('#theform').submit(); //do this first in case of errors
		  if($('input.error, select.error').length <= 0){ //no errors
		    //must load background image here because IE won't animate otherwise
		    $('#loading').css('background', 'url(../images/loading.gif) no-repeat top left');
		    $(this).css('cursor', 'default');
		    $(this).unbind('click');
		    $(this).bind('click', function(){return false;});
		    $('#theform').fadeTo('fast',0.4);
		    //get position of submit button
		    var width = $(this).width();
		    //var height = $(this).height();
		    var p = $(this).position();
		    var top = parseInt(p.top)+7;
		    var left = parseInt(p.left)+width+25;
		    $('#loading').css('top', top);
		    $('#loading').css('left', left);
		    $('#loading').show();
		  }//if
		  return false;
		}); 
		
	    //Tooltip stuff
	    var major_med_html = "<p>Select 'Yes' to view a list of major medical conditions.</p><p>Please check all that apply.</p><p>Select 'No' if your conditions are not listed</p>";
	    $('#major_med').setToolTip(major_med_html, -2, 3);
		
	    var tobacco_html = "<p>If you've smoked in the prior 12 months, please select 'Yes'.</p>";
	    $('#tob_use').setToolTip(tobacco_html, -2, 3);
		
	    var haz_html = "<p>If you have received any special training dealing with risks associated with your job or you feel you work in a hazardous environment, please select 'Yes'.</p>";
	    $('#haz_loc').setToolTip(haz_html, -2, 3);
		
	    var cov_amt_html = "<p>$100,000 is the most common choice. If you are unsure, estimate an amount and our agents will be happy to assist you.</p>";
	    $('#cov_amt').setToolTip(cov_amt_html, 21, 3);
		
	    var years_cov_html = "<p>20 years is the most common choice. If you are unsure, estimate a number and our agents will be happy to assist you.</p>";
	    $('#years_cov').setToolTip(years_cov_html, 7, 3);
		
	    var ins_typs_html = "<p>Please select all types of insurance you are interested in.</p>";
	    $('#ins_type').setToolTip(ins_typs_html, 16, 3);
	
	$('[name=medical]').click(function(){
	  if($('input[name=medical]:radio:checked').val() == "Yes")
		$('#medical_conditions').css('display','inline');
	  else
	    $('#medical_conditions').css('display','none');
	});
	
	$('[name=insurance_type[]]').click(function(){
	  var term = false;
	  $('input[name=insurance_type[]]:checkbox:checked').each(function(){
	    if($(this).val() == "Term Life")
		  term = true;
	  });
	  if(term){
	    $('#coverage_select').css('display','inline');
		$('#coverage_title').css('display','inline');
		$('#coverage_select select').addClass('std_input required ieselectfix');
	  }else{
	    $('#coverage_select').css('display','none');
		$('#coverage_title').css('display','none');
		$('#coverage_select select').removeClass('std_input required ieselectfix error');
	  }
	});
	
	//primary and secondary phone copy functions
	$('#pp').bind('keyup click blur change paste', function(){
	    $('#sp').val($(this).val());
	});
	$('#sp').focus(function(){
	  $('#pp').unbind('keyup click blur change paste');
	});
	
		zipInputBG = $('#zip').css('background-image');
		//changeInputBG($('#zip'));

		$('form:eq(0) input[type=text]:eq(0)').focus();
		$('#zip').focus(function(){ changeInputBG($(this)); })
		$('#zip').blur(function(){ changeInputBG($(this)); })
		$('#zip').keyup(function(){ changeInputBG($(this)); });
	
		$(document).pngFix();
	
});//end document.ready

//Tooltip function takes html as a tooltip message
//x and y offset (from top center of trigger anchor
jQuery.fn.setToolTip = function(html, x, y){
  $(this).after('<div class="tooltip">'+html+'</div>');
  $(this).tooltip({position: 'top center', offset: [y, x], relative: true});
}

function changeInputBG(el){
	if(el.val() != "")
	{	
		el.css('background-image','none');
	} 
	else el.css('background-image',zipInputBG);
}//changeInputBG()


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function split_mysql_date(str)
{
  var arr = new Array();
  
  arr['y'] = str.substring(0,4);
  arr['m'] = str.substring(5,7);
  arr['d'] = str.substring(8);
  
  arr['m'] = ltrim(arr['m'], '0');
  arr['d'] = ltrim(arr['d'], '0');
  
  return arr;
}

function array_to_inputs(inputs)
{
	var str = '';
	for(var key in inputs)
	{
		str += key+'='+ inputs[key] + '&';
	}		
	return str;
}//array_to_inputs

function next_el(curr_el, to_el, length)
{
  var isNum = true;
  if($('#'+curr_el).val().length == length)
  {
    num = IsNumeric($('#'+curr_el).val());
  
    if(num)
    {
      $('#'+to_el).focus();
    }
    else
    {
      $('#'+curr_el).val('');
    }
  }
  
  
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
	 
	 
function check_year(el)
{
	 if(document.getElementById(el).value.length == 4)
	 {
   // Regular expression used to check if date is in correct format
   var pattern = new RegExp("(19|20)[0-9]{2}");

		var curr_year = Date('Y');
		var today = new Date();
		var curr_year = today.getFullYear();

   if(document.getElementById(el).value.match(pattern) && document.getElementById(el).value <= curr_year)
   {
      var year = document.getElementById(el).value;
      // Attention! Javascript consider months in the range 0 - 11
     
      // This instruction will create a date object
      source_date = new Date(year);  
   }
   else
   {
    document.getElementById(el).value = '';
      return false;
   }
	 }
}//check_year



function add_leading_zero(el)
{
	var v = document.getElementById(el).value;
	 if(v.length == 1)
	 {
   	document.getElementById(el).value = '0'+v;
   }
}//add_leading_zero


function to_digits(el)
{
	var sText = $('#'+el).val();
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;
	var sNewText='';
	
  for (i = 0; i < sText.length; i++) 
  { 
  	Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) > -1) 
    {
    	sNewText += sText.charAt(i);
    }
	}
	
	if(el == 'phone')
	{
		if(sNewText.length == 10)
		{
			$('#phone').removeClass('error');
		}
	}
	
	
  $('#'+el).val(sNewText);
	
}//to_digits



/*START PNG FIX*/
/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "pngFix"
 * Version: 1.2, 09.03.2009
 * by Andreas Eberhard, andreas.eberhard@gmail.com
 *                      http://jquery.andreaseberhard.de/
 *
 * Copyright (c) 2007 Andreas Eberhard
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Changelog:
 *    09.03.2009 Version 1.2
 *    - Update for jQuery 1.3.x, removed @ from selectors
 *    11.09.2007 Version 1.1
 *    - removed noConflict
 *    - added png-support for input type=image
 *    - 01.08.2007 CSS background-image support extension added by Scott Jehl, scott@filamentgroup.com, http://www.filamentgroup.com
 *    31.05.2007 initial Version 1.0
 * --------------------------------------------------------------------
 * @example $(function(){$(document).pngFix();});
 * @desc Fixes all PNG's in the document on document.ready
 *
 * jQuery(function(){jQuery(document).pngFix();});
 * @desc Fixes all PNG's in the document on document.ready when using noConflict
 *
 * @example $(function(){$('div.examples').pngFix();});
 * @desc Fixes all PNG's within div with class examples
 *
 * @example $(function(){$('div.examples').pngFix( { blankgif:'ext.gif' } );});
 * @desc Fixes all PNG's within div with class examples, provides blank gif for input with png
 * --------------------------------------------------------------------
 */

(function($) {

jQuery.fn.pngFix = function(settings) {

	// Settings
	settings = jQuery.extend({
		blankgif: './images/blank.gif'
	}, settings);

	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	if (jQuery.browser.msie && (ie55 || ie6)) {

		//fix images with png-source
		jQuery(this).find("img[src$=.png]").each(function() {

			jQuery(this).attr('width',jQuery(this).width());
			jQuery(this).attr('height',jQuery(this).height());

			var prevStyle = '';
			var strNewHTML = '';
			var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
			var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
			var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
			var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
			var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
			var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
			if (this.style.border) {
				prevStyle += 'border:'+this.style.border+';';
				this.style.border = '';
			}
			if (this.style.padding) {
				prevStyle += 'padding:'+this.style.padding+';';
				this.style.padding = '';
			}
			if (this.style.margin) {
				prevStyle += 'margin:'+this.style.margin+';';
				this.style.margin = '';
			}
			var imgStyle = (this.style.cssText);
			
			/* --------------------------------------------------------------
		 	*	Jonathan Russell - 04/01/2010
		 	*	Add fix to allow for extra CSS to be applied from original
		 	*	-------------------------------------------------------------
		 	*/
			var imgPosition = (jQuery(this).css('position')=='absolute') ? 'absolute' : 'relative';
			imgStyle += (typeof(jQuery(this).css('margin-top'))!='undefined') ? 'margin-top:'+jQuery(this).css('margin-top')+';' : '';
			imgStyle += (typeof(jQuery(this).css('margin-right'))!='undefined') ? 'margin-right:'+jQuery(this).css('margin-right')+';' : '';
			imgStyle += (typeof(jQuery(this).css('margin-bottom'))!='undefined') ? 'margin-bottom:'+jQuery(this).css('margin-bottom')+';' : '';
			imgStyle += (typeof(jQuery(this).css('margin-left'))!='undefined') ? 'margin-left:'+jQuery(this).css('margin-left')+';' : '';
			imgStyle += (typeof(jQuery(this).css('float'))!='undefined') ? 'float:'+jQuery(this).css('float')+';' : '';
			//alert(addCSS);

			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
			strNewHTML += 'style="position:'+imgPosition+';white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
			strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
			//strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\');';
			strNewHTML += imgStyle+'"></span>';
			if (prevStyle != ''){
				strNewHTML = '<span style="position:'+imgPosition+';display:inline-block;'+prevStyle+imgHand+'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;'+'">' + strNewHTML + '</span>';
			}

			jQuery(this).hide();
			jQuery(this).after(strNewHTML);

		});

		// fix css background pngs
		jQuery(this).find("*").each(function(){
			var bgIMG = jQuery(this).css('background-image');
			if(bgIMG.indexOf(".png")!=-1){
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				jQuery(this).css('background-image', 'none');
				
				jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
				//jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "')";
			}
		});
		
		//fix input with png-source
		jQuery(this).find("input[src$=.png]").each(function() {
			var bgIMG = jQuery(this).attr('src');
			jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
   		jQuery(this).attr('src', settings.blankgif)
		});
		
		
		
		/* --------------------------------------------------------------
		 *	Jonathan Russell - 04/01/2010
		 *	Add fix to raise elements to allow interaction
		 *	-------------------------------------------------------------
		 */
		$("input, textarea, a, a img").css({
			'z-index' : '10000',
			'position' : 'relative'
		});
		$('a, a img').css('cursor', 'hand');
	
	}
	
	return jQuery;

};

})(jQuery);


/*PNG FIX END*/



/* DISABLE ON SUBMIT */
/*
 * jQuery Disable On Submit Plugin
 * http://www.evanbot.com/article/jquery-disable-on-submit-plugin/13
 *
 * Copyright (c) 2009 Evan Byrne (http://www.evanbot.com)     
 */
$.fn.disableOnSubmit = function(disableList){
	
	if(disableList == null){var $list = 'input[type=submit],input[type=button],input[type=reset],button';}
	else{var $list = disableList;}
	
	// Makes sure button is enabled at start
	$(this).find($list).removeAttr('disabled');
	
	$(this).submit(function(){$(this).find($list).attr('disabled','disabled');});
	return this;
};

/* END DISABLE ON SUBMIT */

/*
 * jQuery Tools 1.2.5 - The missing UI library for the Web
 * 
 * [tooltip, tooltip.slide, tooltip.dynamic]
 * 
 * NO COPYRIGHTS OR LICENSES. DO WHAT YOU LIKE.
 * 
 * http://flowplayer.org/tools/
 * 
 * File generated: Fri Oct 01 10:19:32 GMT 2010
 */
(function(f){function p(a,b,c){var h=c.relative?a.position().top:a.offset().top,d=c.relative?a.position().left:a.offset().left,i=c.position[0];h-=b.outerHeight()-c.offset[0];d+=a.outerWidth()+c.offset[1];if(/iPad/i.test(navigator.userAgent))h-=f(window).scrollTop();var j=b.outerHeight()+a.outerHeight();if(i=="center")h+=j/2;if(i=="bottom")h+=j;i=c.position[1];a=b.outerWidth()+a.outerWidth();if(i=="center")d-=a/2;if(i=="left")d-=a;return{top:h,left:d}}function u(a,b){var c=this,h=a.add(c),d,i=0,j=
0,m=a.attr("title"),q=a.attr("data-tooltip"),r=o[b.effect],l,s=a.is(":input"),v=s&&a.is(":checkbox, :radio, select, :button, :submit"),t=a.attr("type"),k=b.events[t]||b.events[s?v?"widget":"input":"def"];if(!r)throw'Nonexistent effect "'+b.effect+'"';k=k.split(/,\s*/);if(k.length!=2)throw"Tooltip: bad events configuration for "+t;a.bind(k[0],function(e){clearTimeout(i);if(b.predelay)j=setTimeout(function(){c.show(e)},b.predelay);else c.show(e)}).bind(k[1],function(e){clearTimeout(j);if(b.delay)i=
setTimeout(function(){c.hide(e)},b.delay);else c.hide(e)});if(m&&b.cancelDefault){a.removeAttr("title");a.data("title",m)}f.extend(c,{show:function(e){if(!d){if(q)d=f(q);else if(b.tip)d=f(b.tip).eq(0);else if(m)d=f(b.layout).addClass(b.tipClass).appendTo(document.body).hide().append(m);else{d=a.next();d.length||(d=a.parent().next())}if(!d.length)throw"Cannot find tooltip for "+a;}if(c.isShown())return c;d.stop(true,true);var g=p(a,d,b);b.tip&&d.html(a.data("title"));e=e||f.Event();e.type="onBeforeShow";
h.trigger(e,[g]);if(e.isDefaultPrevented())return c;g=p(a,d,b);d.css({position:"absolute",top:g.top,left:g.left});l=true;r[0].call(c,function(){e.type="onShow";l="full";h.trigger(e)});g=b.events.tooltip.split(/,\s*/);if(!d.data("__set")){d.bind(g[0],function(){clearTimeout(i);clearTimeout(j)});g[1]&&!a.is("input:not(:checkbox, :radio), textarea")&&d.bind(g[1],function(n){n.relatedTarget!=a[0]&&a.trigger(k[1].split(" ")[0])});d.data("__set",true)}return c},hide:function(e){if(!d||!c.isShown())return c;
e=e||f.Event();e.type="onBeforeHide";h.trigger(e);if(!e.isDefaultPrevented()){l=false;o[b.effect][1].call(c,function(){e.type="onHide";h.trigger(e)});return c}},isShown:function(e){return e?l=="full":l},getConf:function(){return b},getTip:function(){return d},getTrigger:function(){return a}});f.each("onHide,onBeforeShow,onShow,onBeforeHide".split(","),function(e,g){f.isFunction(b[g])&&f(c).bind(g,b[g]);c[g]=function(n){n&&f(c).bind(g,n);return c}})}f.tools=f.tools||{version:"1.2.5"};f.tools.tooltip=
{conf:{effect:"toggle",fadeOutSpeed:"fast",predelay:0,delay:30,opacity:1,tip:0,position:["top","center"],offset:[0,0],relative:false,cancelDefault:true,events:{def:"mouseenter,mouseleave",input:"focus,blur",widget:"focus mouseenter,blur mouseleave",tooltip:"mouseenter,mouseleave"},layout:"<div/>",tipClass:"tooltip"},addEffect:function(a,b,c){o[a]=[b,c]}};var o={toggle:[function(a){var b=this.getConf(),c=this.getTip();b=b.opacity;b<1&&c.css({opacity:b});c.show();a.call()},function(a){this.getTip().hide();
a.call()}],fade:[function(a){var b=this.getConf();this.getTip().fadeTo(b.fadeInSpeed,b.opacity,a)},function(a){this.getTip().fadeOut(this.getConf().fadeOutSpeed,a)}]};f.fn.tooltip=function(a){var b=this.data("tooltip");if(b)return b;a=f.extend(true,{},f.tools.tooltip.conf,a);if(typeof a.position=="string")a.position=a.position.split(/,?\s/);this.each(function(){b=new u(f(this),a);f(this).data("tooltip",b)});return a.api?b:this}})(jQuery);
(function(d){var i=d.tools.tooltip;d.extend(i.conf,{direction:"up",bounce:false,slideOffset:10,slideInSpeed:200,slideOutSpeed:200,slideFade:!d.browser.msie});var e={up:["-","top"],down:["+","top"],left:["-","left"],right:["+","left"]};i.addEffect("slide",function(g){var a=this.getConf(),f=this.getTip(),b=a.slideFade?{opacity:a.opacity}:{},c=e[a.direction]||e.up;b[c[1]]=c[0]+"="+a.slideOffset;a.slideFade&&f.css({opacity:0});f.show().animate(b,a.slideInSpeed,g)},function(g){var a=this.getConf(),f=a.slideOffset,
b=a.slideFade?{opacity:0}:{},c=e[a.direction]||e.up,h=""+c[0];if(a.bounce)h=h=="+"?"-":"+";b[c[1]]=h+"="+f;this.getTip().animate(b,a.slideOutSpeed,function(){d(this).hide();g.call()})})})(jQuery);
(function(g){function j(a){var c=g(window),d=c.width()+c.scrollLeft(),h=c.height()+c.scrollTop();return[a.offset().top<=c.scrollTop(),d<=a.offset().left+a.width(),h<=a.offset().top+a.height(),c.scrollLeft()>=a.offset().left]}function k(a){for(var c=a.length;c--;)if(a[c])return false;return true}var i=g.tools.tooltip;i.dynamic={conf:{classNames:"top right bottom left"}};g.fn.dynamic=function(a){if(typeof a=="number")a={speed:a};a=g.extend({},i.dynamic.conf,a);var c=a.classNames.split(/\s/),d;this.each(function(){var h=
g(this).tooltip().onBeforeShow(function(e,f){e=this.getTip();var b=this.getConf();d||(d=[b.position[0],b.position[1],b.offset[0],b.offset[1],g.extend({},b)]);g.extend(b,d[4]);b.position=[d[0],d[1]];b.offset=[d[2],d[3]];e.css({visibility:"hidden",position:"absolute",top:f.top,left:f.left}).show();f=j(e);if(!k(f)){if(f[2]){g.extend(b,a.top);b.position[0]="top";e.addClass(c[0])}if(f[3]){g.extend(b,a.right);b.position[1]="right";e.addClass(c[1])}if(f[0]){g.extend(b,a.bottom);b.position[0]="bottom";e.addClass(c[2])}if(f[1]){g.extend(b,
a.left);b.position[1]="left";e.addClass(c[3])}if(f[0]||f[2])b.offset[0]*=-1;if(f[1]||f[3])b.offset[1]*=-1}e.css({visibility:"visible"}).hide()});h.onBeforeShow(function(){var e=this.getConf();this.getTip();setTimeout(function(){e.position=[d[0],d[1]];e.offset=[d[2],d[3]]},0)});h.onHide(function(){var e=this.getTip();e.removeClass(a.classNames)});ret=h});return a.api?ret:this}})(jQuery);

