// JavaScript Document
function openWin(theLoc,theWidth,theHeight){
	win=window.open(theLoc,"NewWin","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,top=200,width="+theWidth+",height="+theHeight);
	if(win.opener==null) win.opener=window;
	win.opener.name="opener";
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function openWin(theLoc,theWidth,theHeight){
	win=window.open(theLoc,"NewWin","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width="+theWidth+",height="+theHeight);
	if(win.opener==null) win.opener=window;
	win.opener.name="opener";
}

// email,digit,other fields validate
function isEmail(strEmail){
	var patrn = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	return patrn.test(strEmail);
}
function isValidDigit(s) 
{ 
	var patrn=/^[0-9]{1,20}$/; 
	if (!patrn.exec(s)) return false;
	return true;
} 
function isValidInput(s)
{
	var patrn=/^([a-zA-Z0-9]|[ .-]){1,}$/; 
	if (!patrn.exec(s)) return false;
	return true;
}

$(document).ready(function(){
	
	if($("#homeslideshow").length>0)
	{
		setTimeout(function(){
			$('#homeslideshow img:first').fadeIn(1500);
			$('#homeslideshow').cycle();
		}, 1500);
	}
	
	if($("#testimonials").length>0)
	{$("#testimonials").cycle({fx:'fade',random:1,timeout:3600000});}
	
	if($("a.popup").length>0)
	{
		$("a.popup").each(function(){
			var str;
			str=$(this).attr("href").toLowerCase();
			$(this).removeAttr("target");
			$(this).removeAttr("onclick");
			
			if (str.indexOf("pageid=")>-1)
			{
				str=str.substring(str.indexOf("pageid=")+7);
				str="javascript:openWin('/popup.asp?pageid="+str+"',720,500);";
			}
			else
			{str="javascript:openWin('/popup.asp?url="+str+"',720,500);";}
			
			$(this).attr("href",""+str+"");
		});
	}
	
	if($("#productslist").length>0)
	{
		$("#productslist .imgdv").equalHeights();
		$("#productslist .info").equalHeights();
	}
	
	if($("#zoomlink").length>0)
	{
		$("#zoomimg").jqzoom({
			zoomWidth: 435,
			zoomHeight: 418,
			xOffset:33,
			yOffset:0,
			lens: true,
			title: false,
			showPreload: true,
			preloadText: 'Loading zoom',
			preloadPosition: 'top'
		});
	}
	
	if($("#left div.listitems").length>0)
	{
		$("#left div.listitems h4").css("margin","0");
		$("#right div.listitems").each(function(index){
			var height = $(this).height();
			$("#left div.listitems").eq(index).height(height);
		});
		
		$("#right hr.tbmar20:last").remove();
	}

});


$(document).ready(function(){

	// shipping validate
	$("#shipping_usa_fm").submit(function(){
		if ($.trim($("input:checked[name='ShipMethod']").val())=="")
		{alert("Please select a shipping method.");return false;}
	});
	
	// credit card info validate
	$("#confirmorder_fm").submit(function(){

		if ($("input#FirstName").val()=="")
		{alert("Please enter a value for the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if ($("input#FirstName").val().length<2)
		{alert("Please enter at least 2 characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if ($("input#FirstName").val().length>50)
		{alert("Please enter at most 50 characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if (!isValidInput($("input#FirstName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		
		if ($("input#LastName").val()=="")
		{alert("Please enter a value for the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if ($("input#LastName").val().length<2)
		{alert("Please enter at least 2 characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if ($("input#LastName").val().length>50)
		{alert("Please enter at most 50 characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if (!isValidInput($("input#LastName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		
		if ($("select#CCType").length > 0)
		{
			if ($("select#CCType").val()=="1")// Visa	13 or 16 digit
			{
				if (!($("input#CCNumber").val().length==13 || $("input#CCNumber").val().length==16) || !isValidDigit($("input#CCNumber").val()))
				{alert("Please enter 13 or 16 digit in the \"Credit Card Number\" field.");$("input#CCNumber").focus();return false;}
			}
			else if ($("select#CCType").val()=="2")// MasterCard	16 digit
			{
				if ($("input#CCNumber").val().length!=16 || !isValidDigit($("input#CCNumber").val()))
				{alert("Please enter 16 digit in the \"Credit Card Number\" field.");$("input#CCNumber").focus();return false;}
			}
			else if ($("select#CCType").val()=="3")// AMEX	15 digit
			{
				if ($("input#CCNumber").val().length!=15 || !isValidDigit($("input#CCNumber").val()))
				{alert("Please enter 15 digit in the \"Credit Card Number\" field.");$("input#CCNumber").focus();return false;}
			}
			else
			{}
		}
		
		if ($("input#CSC").length > 0)
		{
			if ($("input#CSC").val().length<3 || !isValidDigit($("input#CSC").val()))
			{alert("Please enter at least 3 digit in the \"Security Code\" field.");$("input#CSC").focus();return false;}
		}

	});
	
});


//Contact us form -- contact.html
$(document).ready(function(){
	$('#joinus_fm').submit(function(){
		if ($("input#Joinus_Email").val()=="")
		{alert("Please enter a value for the \"Email\" field.");$("input#Joinus_Email").focus();return false;}
		if (!isEmail($("input#Joinus_Email").val()))
		{alert("Please enter a valid email address");$("input#Joinus_Email").focus();return false;}
	});
	
	$('#contact_fm').submit(function(){
		if ($("input#FirstName").val()=="")
		{alert("Please enter a value for the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if ($("input#FirstName").val().length<2)
		{alert("Please enter at least 2 characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if ($("input#FirstName").val().length>50)
		{alert("Please enter at most 50 characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if (!isValidInput($("input#FirstName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		
		if ($("input#LastName").val()=="")
		{alert("Please enter a value for the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if ($("input#LastName").val().length<2)
		{alert("Please enter at least 2 characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if ($("input#LastName").val().length>50)
		{alert("Please enter at most 50 characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if (!isValidInput($("input#LastName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}

		if ($("input#Email").val()=="")
		{alert("Please enter a value for the \"Email\" field.");$("input#Email").focus();return false;}
		if (!isEmail($("input#Email").val()))
		{alert("Please enter a valid email address");$("input#Email").focus();return false;}
		
		if ($("#Types").val()=="")
		{alert("Please select an item for the \"My Comment/Question/Concern\".");$("#Types").focus();return false;}
		
		if ($("#Message").val()=="")
		{alert("Please enter a value for the \"Message\" field.");$("#Message").focus();return false;}

		if ($("#Message").val().length<5)
		{alert("Please enter at least 5 characters in the \"Message\" field.");$("#Message").focus();return false;}
	});
	
});

//left nav png fix for IE6
EvPNG.fix("#leftnav li a img");
