var display_speed = 'fast';
$(document).ready(function()
{
	$('.clearfloat').html('');
	$('em.error').remove();
	
	$('.partner_with_us textarea').click(function(){
   		this.select();
   		return false;
		});
	
	$('#form_holder').append($('#form_temp'));
	$('#col-left-final').append($('#col-left-temp'));
	$('#promo-final').append($('#promo-temp'));
	$('.promo').append($('#truste-seal-home'));
	$('#zip').focus();
	$('#products').focus();
	
	$('#home_cont_paras').append('<div class="clearfloat"></div>');
	$('#home_cont_para_right').html('').append($('#video_div'));
	
	
	$('.nav').hover(function(){
		var src = $(this).attr('src');
		var	pos = src.lastIndexOf('_');
		var new_src = src.substring(0,pos+1);
		 		new_src += 'on.jpg';
				$(this).attr('src',new_src);
	},function(){
		var src = $(this).attr('src');
		var	pos = src.lastIndexOf('_');
		var new_src = src.substring(0,pos+1);
		 		new_src += 'off.jpg';
				$(this).attr('src',new_src);
	});
	
	$('.zip-code').keyup(function(){
	$(this).css('background-image','none');
	});
	
        //USAGE - <div id="loading"><span class="one_line">Please wait...</span></div>
		//hide loading gif (IE fix) - must be added to DOM while visible.
		$('#loading').hide();
		
		$('#submit').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('visibility', 'hidden');
            $('#theform').fadeTo('fast',0.4);
			var bottom = 141;
			var left = 410;
            $('#loading').css('bottom', bottom);
            $('#loading').css('left', left);
            $('#loading').show();
          }//if
          return false;
        });

	
  $(document).pngFix( { blankgif:'./images/blank.gif' } ); 

});//end document.ready

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);
    }
	}
	
  $('#'+el).val(sNewText);
	
}//to_digits


/*
 * 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;
};


$().ready(function() {

//Home Form
$("#home_form").validate({
  //change error element
  errorElement: "em",
  //use main error div instead
  invalidHandler: function(e, validator) {
	  var errors = validator.numberOfInvalids();
	  if (errors) {
		  var message = errors == 1 ? 'You missed 1 field. It has been highlighted below' : 'You missed ' + errors + ' fields. They have been highlighted below';
		  $("div.error span").html(message);
		  $("div.error").show();
	  }
    else
    {
		  $("div.error").hide();
	  }
  },
  rules:{
  },
  messages:{
  }
});//validate



//Main Form
$("#theform").validate({
  //change error element
  errorElement: "em",
  //use main error div instead
  invalidHandler: function(e, validator) {
	  var errors = validator.numberOfInvalids();
	  if (errors) {
		  var message = errors == 1 ? 'You missed 1 field. It has been highlighted below' : 'You missed ' + errors + ' fields. They have been highlighted below';
		  $("div.error span").html(message);
		  $("div.error").show();
	  }
    else
    {
		  $("div.error").hide();
	  }
  },
  rules:{
  },
  messages:{
  }
});//validate 


$(function(){
  $('form').disableOnSubmit();
});

});//.ready


/**
 * --------------------------------------------------------------------
 * 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: '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);

			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
			strNewHTML += 'style="position:relative;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:relative;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)
		});
		
		
		
		/* --------------------------------------------------------------
		 *	Add fix to raise elements to allow interaction
		 *	-------------------------------------------------------------
		 */
		$("input, textarea, a").css({
			'z-index' : '3000',
			'position' : 'relative'
		});
	
	}
	
	return jQuery;

};

})(jQuery);

