$(document).ready(function(){

	if($('a[href="http://store.esp-global.com/products/Utah_County_Emissions_Certificate_Bundle-604-0.html"]').length) {
		$('a[href="http://store.esp-global.com/products/Utah_County_Emissions_Certificate_Bundle-604-0.html"]').parent().parent().remove();
	}
	/*$(".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);
			}
		}
	);*/
	var patt = /\/catalog\/(.*)-[0-9]+.html/i;
	var currcat = top.location.pathname;
	currcat = patt.exec(currcat);
	if(currcat) {
		currcat = currcat[1];
	}
	$('select.catselect option').each(function() {
		if(top.location.pathname==$(this).val()) {
			$(this).attr('selected','selected');
		} else {
			var item = $(this).val();
			item = patt.exec(item);
			
			if (item && item[1]==currcat) {
				//$('body').append(item[1]);
				$(this).attr('selected','selected');
			}
		}
		
	});
	
	$('.catselect').change(function() {
		top.location.href = $(this).children('option:selected').val();
	});
	
	/*
	$(".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();
	});*/
	
	preload_image_object = new Image();
	preload_image_object.src = "/images/menu/logo_over.png";
	// set image url
	image_url = new Array();
	image_url[0] = "About";
	image_url[1] = "RSD";
	image_url[2] = "ESS";
	image_url[3] = "ETP";
	image_url[4] = "logo";
	
	
	var i = 0;
	for(i=0; i<=4; i++)
	{
		preload_image_object = new Image();
		preload_image_object.src = "/images/menu/"+image_url[i]+"_over.png";
	}
	if($('form[name="signupForm"]').length) {
		$('form[name="signupForm"]').find('input[name="custom_field[1]"]').parent().prev('label').append('<span style="font-size: 10px;"></span>');
		$('form[name="signupForm"]').find('input[name="custom_field[1]"]').unbind().change(function() {
			var field = $(this);
			var val = field.val();
			var label = field.parent().prev('label');
			$.getJSON('http://esp-global.com/validateCustomer.php?cust_id=' + val + '&callback=?', function(data) {
				var status = data.status;
				if(status == "Valid") {
					field.css('border-color', 'green').addClass('valid');
					label.children('span').text(' (Valid Customer Number)').css('color', 'green');
				} else if(status == "NotFnd") {
					field.css('border-color', 'red').removeClass('valid');
					label.children('span').text(' (Invalid Customer Number)').css('color', 'red');
				}
			});
		});
		
		if(getCookie('custid')!=null) {
			var custid = getCookie('custid');
			$('form[name="signupForm"]').find('input[name="custom_field[1]"]').val(custid).change();
		}
		var state = getCookie('state')
		if(state && state=='utah') {
			$('form[name="signupForm"]').submit(function() {
				var custfield = $(this).find('input[name="custom_field[1]"]');
				if(custfield.val()=='') {
					alert("Please enter a customer ID");
					return false;
				}
				if(!custfield.hasClass('valid')) {
					alert("Please enter a valid customer ID");
					return false;
				}
			});
		}
	}
	
	if(location.search!='') {
		statep = /state=([A-Za-z0-9]*)\&?/;
		custidp = /cid=([A-Za-z0-9]*)\&?/;
		quantityp = /quantity=([A-Za-z0-9]*)\&?/;
		state = statep.exec(location.search);
		quantity = quantityp.exec(location.search);
		custid = custidp.exec(location.search);
		if(state!=null)
			setCookie('state', state[1], 2);
		if(custid!=null)
			setCookie('custid', custid[1], 2);
			
		if(quantity!=null) {
			$('#quantity').val(quantity[1]);
		}
	}
	
});

function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
	  var c = ca[i];
	  while (c.charAt(0)==' ') c = c.substring(1,c.length);
	  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function deleteCookie(name) {
	setCookie(name,"",-1);
}


