$(document).ready(function(){
	/*$(".main a img").hover(
		function () {
			if($(this).parent().attr("class")!="active") {
				var url = $(this).attr("src");
				url = url.replace(/.png/, "_over.png");
				$(this).attr("src", url);
			}
		},
		function () {
			if($(this).parent().attr("class")!="active") {
				var url = $(this).attr("src");
				url = url.replace(/_over/, "");
				$(this).attr("src", url);
			}
		}
	);*/
	
	$(".lower a img").hover(
		function () {
			if($(this).parent().attr("class")!="active") {
				var url = $(this).attr("src");
				url = url.replace(/.png/, "_over.png");
				$(this).attr("src", url);
			}
		},
		function () {
			if($(this).parent().attr("class")!="active") {
				var url = $(this).attr("src");
				url = url.replace(/_over/, "");
				$(this).attr("src", url);
			}
		}
	);
	
	if($('input').length) {
		$('input, textarea').not('input:submit').focus(function() {
			if(this.defaultValue == this.value) {
				this.value = '';
			}
		});
		$('input, textarea').not('input:submit').blur(function() {
			if(this.value == '') {
				this.value = this.defaultValue;
			}
		});
	}
	
	$('form.form').submit(function() {
		$('p.error').remove();
		$('.error').removeClass();
		var fail = false;
		var reason = "";
		$('input, textarea').not('input:submit').each(function() {
			if(this.defaultValue == this.value) {
				this.value = '';
			}
		});
		if($('#requestcall').length) {
			if($('#name').val()=="")
			{
				$('#name').addClass('error');
				fail = true;
				reason += "Please enter your Name.<br />";
			}
			if($('#phone').val()=="")
			{
				$('#phone').addClass('error');
				fail = true;
				reason += "Please enter your Phone Number.<br />";
			}
			if(!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/).test($('#email').val()))
			{
				$('#email').addClass('error');
				fail = true;
				reason += "Please enter your Email Address.<br />";
			}
		}
		if($('#requestservicecall').length) {
			if($('#name').val()=="")
			{
				$('#name').addClass('error');
				fail = true;
				reason += "Please enter your Name.<br />";
			}
			if($('#phone').val()=="")
			{
				$('#phone').addClass('error');
				fail = true;
				reason += "Please enter your Phone Number.<br />";
			}
			if(!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/).test($('#email').val()))
			{
				$('#email').addClass('error');
				fail = true;
				reason += "Please enter your Email Address.<br />";
			}
		}
		if($('#creditapp').length) {
			if($('#lname').val()=="")
			{
				$('#lname').addClass('error');
				fail = true;
				reason += "Please enter your Last Name.<br />";
			}
			if($('#fname').val()=="")
			{
				$('#fname').addClass('error');
				fail = true;
				reason += "Please enter your First Name.<br />";
			}
			if(!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/).test($('#email').val()))
			{
				$('#email').addClass('error');
				fail = true;
				reason += "Please enter your Email Address.<br />";
			}
			if($('#phone').val()=="")
			{
				$('#phone').addClass('error');
				fail = true;
				reason += "Please enter your Phone Number.<br />";
			}
		}
		if($('#refurb').length) {
			if($('#name').val()=="")
			{
				$('#name').addClass('error');
				fail = true;
				reason += "Please enter your Name.<br />";
			}
			if(!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/).test($('#email').val()))
			{
				$('#email').addClass('error');
				fail = true;
				reason += "Please enter your Email Address.<br />";
			}
		}
		if(fail) {
			$('input, textarea').not('input:submit').each(function() {
				if(this.value == '') {
					this.value = this.defaultValue;
				}
			});
			$(this).prepend('<p class="error">'+reason+'</p>');
			return false;
		}
	});
	
	if($('form.form').length && location.search != '')
	{
		if(location.search=='?m=success') {
			$('form.form').before('<h2>Thank you for your information.</h2> <p>An ESP representative will contact you shortly.</p>').hide();
		}
		else if(location.search=='?m=input') {
			$('form.form').prepend('<p class="error">We had a problem validating your input. Please try again.</p>');
		}
		else if(location.search=='?m=problem') {
			$('form.form').prepend('<p class="error">There was a problem sending your message. <br />Please send us an email at <a href="mailto:espinfo@esph.com?subject=Service Call Form Error">espinfo@esph.com</a>.</p>');
		}
		else if(location.search=='?m=mailfail') {
			$('form.form').prepend('<p class="error">There was a problem sending your message. <br />Please send us an email at <a href="mailto:sales@esph.com">sales@esph.com</a>.</p>');
		}
	}
		
	var selectfix = false;
	$('.calc .selectfix').click(function() {
		$(this).next('select').trigger('click');
	});
	
	$('.calc select').click(function() {
		//$(this).css('height', 'auto');
	});
	
	function selectFixer(select) {
		if(selectfix) {
			select.css('height', '30px');
			select.children('option').css('margin-top', '-30px;');
			selectfix = false;
		}
		else
		{
			//var num = $(select).children('option').length;
			select.css('height', 'auto');
			selectfix = true;
		}
	}
	
	$('#state2Select').change(function() {
		top.location.href = this.form.state.options[this.form.state.selectedIndex].value;
		return false;
	});
	
	if($('#calc').length) {
		if($('h2.price').length) {
			$('#principal').val('$'+$('h2.price span').text());
		}
		$('#rates, #principal').change(function() {
			var payments = $('#rates').val();
			var values = payments.split(':');
			payments = Number(values[0]);
			var i = Number(values[1]);
			
			var principal = Number($('#principal').val().replace(/\$|,/ig, ''));
			if(principal>1 && payments>1) {
				i = i/12;
				var pow = 1;
				for(var j = 0; j<payments; j++)
				{
					pow = pow * (1 + i);
				}
				var money = '' + .01 * Math.round(100*(principal * pow * i) / (pow - 1));
				var dec = money.indexOf(".");
				var dollars = money.substring(0,dec);
				var cents = money.substring(dec+1,dec+3);
				cents = (cents.length < 2) ? cents + "0" : cents;
				money = dollars + "." + cents;
				$('#payment').val('$'+money);
			}
			else
			{
				$('#payment').val('Result');
			}
		});
		$('#rates').change();
	}
	
	/*
	$(".hide").toggle();
	$(".collapse").toggle();
	
	
	$(".read").click(function () {
		$(this).parent().next(".hide").slideDown("slow");
		$(this).toggle();
		$(this).parent().next(".hide").next(".collapse").toggle();
	});
	
	$(".collapse").click(function () {
		$(this).toggle();
		$(this).prev(".hide").slideUp("slow");
		$(this).prev(".hide").prev().children(".read").toggle();
	});
	*/
	//if(lang!=null)
	//{
		preload_image_object = new Image();
		preload_image_object.src = "/images/menu/logo_over.png";
		// set image url
		image_url = new Array();
		image_url[0] = "FAQ";
		image_url[1] = "History";
		image_url[2] = "AccuScan-Products";
		image_url[3] = "RSD-Services";
		image_url[4] = "How-RSD-Works";
		image_url[5] = "Customer-Service";
		image_url[6] = "Vehicle-Testing";
		image_url[7] = "Data-Management";
		image_url[8] = "Project-Management";
		image_url[9] = "Training-Certification";
		image_url[10] = "Our-Solutions";
		image_url[11] = "About";
		image_url[12] = "RSD";
		image_url[13] = "ESS";
		image_url[14] = "ETP";
		image_url[15] = "Legal";
		
		
		var i = 0;
		for(i=0; i<=15; i++)
		{
			preload_image_object = new Image();
			preload_image_object.src = "/images/menu/"+lang+"/"+image_url[i]+"_over.png";
		}
	//}
});
