function trimString(str)
{
	  while (str.charAt(0) == ' ')
	    str = str.substring(1);
	  while (str.charAt(str.length - 1) == ' ')
	    str = str.substring(0, str.length - 1);
	  return str;
}

//### Email Check ###########
function emailCheck (emailStr)
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null)
	{
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).

	for (i=0; i<user.length; i++) 
	{
		if (user.charCodeAt(i)>127) 
		{
			//alert("Ths username contains invalid characters.");
			return false;
   		}
	}
	for (i=0; i<domain.length; i++) 
	{
		if (domain.charCodeAt(i)>127) 
		{
			//alert("Ths domain name contains invalid characters.");
			return false;
   		}
	}

	if (user.match(userPat)==null) 
	{
		return false;
	}
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) 
	{

		for (var i=1;i<=4;i++) 
		{
			if (IPArray[i]>255) 
			{
				return false;
   			}
		}
		return true;
	}
 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) 
	{
		if (domArr[i].search(atomPat)==-1) 
		{
			return false;
   		}
	}
	
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) 
	{
		return false;
	}

	// Make sure there's a host name preceding the domain.

	if (len<2) 
	{
		return false;
	}

// If we've gotten this far, everything's valid!
return true;
}//end function

function chk_login(thisform)
{
	with(thisform)
	{
		if(trimString(user.value)=="")
		{
			alert("Please enter your email address/user name");
			user.focus();
			return false;	
		}
		
		if(!emailCheck(user.value))
		{
			alert("Please enter a valid email address");
			user.focus();
			return false;
		}
		
		if(pass.value=="")
		{
			alert("Please enter password");
			pass.focus();
			return false;
		}
	}
}

function check_bill_ship()
{
	with (document.form1)
	{
		if(gift_message.value.length>200)
		{
			alert("You can only enter upto 200 character for gift message.");
			gift_message.focus();
			return false;
		}
		
		if (fname.value=="") { alert("Please Enter the First Name"); fname.focus(); return false;}
		if (lname.value==""){alert ("Please Enter the Last Name"); lname.focus();return false;}
		/*
		if(aptno.value=="")
		{
			alert("Please Enter apt, suite no.");
			aptno.focus();
			return false;	
		}*/
		if (address1.value==""){alert ("Please Enter Address 1"); address1.focus();return false;}
		
		if (city.value==""){alert ("Please Enter City"); city.focus();return false;}
		if(state.value=="0"){alert("Please Select State");state.focus();return false;}
		
		if (zip.value==""){alert ("Please Enter Zipcode"); zip.focus();return false;}
		if (email.value==""){alert ("Please Enter Email Address"); email.focus();return false;}
		if (!emailCheck(email.value)) {alert ("Please enter the correct Email Address") ; email.focus(); return false ; }
		if (email.value==""){alert ("Please Enter Email Address"); remail.focus();return false;}
		if (!emailCheck(email.value)) {alert ("Please enter the correct Email Address") ; remail.focus(); return false ; }
		if (email.value!=remail.value){alert ("Email Address Do Not Match"); remail.focus();return false;}
		if (telephone.value==""){alert ("Please Enter Telephone Number");telephone.focus();return false;}
		/*
		if (isNaN(document.form1.telephone.value))
	 	{ 
	 		alert("Please enter a numeric value for telephone number");document.form1.telephone.select();document.form1.telephone.focus(); return false;
	 	}*/	
				
		
		
		
		
		if (sh_fname.value=="") { alert("Please Enter the First Name (Shipping Info)"); sh_fname.focus(); return false;}
		if (sh_lname.value==""){alert ("Please Enter the Last Name (Shipping Info)"); sh_lname.focus();return false;}
		/*
		if(sh_aptno.value=="")
		{
			alert("Please Enter apt, suite no.(Shipping Info)");
			sh_aptno.focus();
			return false;	
		}*/
		if (sh_address1.value==""){alert ("Please Enter Address 1 (Shipping Info)"); sh_address1.focus();return false;}
		if (sh_city.value==""){alert ("Please Enter City (Shipping Info)"); sh_city.focus();return false;}
		if(sh_state.value=="0"){alert("Please Select State (Shipping Info)"); sh_state.focus(); return false;}
		if (sh_zip.value==""){alert ("Please Enter Zipcode (Shipping Info)"); sh_zip.focus();return false;}
		if (sh_email.value==""){alert ("Please Enter Email Address (Shipping Info)");sh_email.focus();return false;}
		if (rsh_email.value==""){alert ("Please Enter Retype Email Address (Shipping Info)");rsh_email.focus();return false;}
		if (rsh_email.value!=sh_email.value){alert ("Email Address Do Not Match (Shipping Info)");sh_email.focus();return false;}
		if (!emailCheck(sh_email.value)) {alert ("Please enter the correct Email Address (Shipping Info)") ; sh_email.focus(); return false ; }
		
		if (stelephone.value==""){alert ("Please Enter Telephone Number (Shipping Info)");stelephone.focus();return false;}
	}
	
	
	/*
	if (isNaN(document.form1.stelephone.value))
	 { alert("Please enter a numeric value for telephone number");document.form1.stelephone.select();document.form1.stelephone.focus(); return false;}	
	 */
		
}

function billship(cntrl)
{
	
	if(cntrl.checked==true)
	{
		window.document.form1.sh_title.selectedIndex=window.document.form1.title.selectedIndex;	
		window.document.form1.sh_fname.value=window.document.form1.fname.value;
		window.document.form1.sh_lname.value=window.document.form1.lname.value;
		window.document.form1.sh_aptno.value=window.document.form1.aptno.value;
		window.document.form1.sh_address1.value=window.document.form1.address1.value;
		
		window.document.form1.sh_city.value=window.document.form1.city.value;
		
		for(i=0;i < window.document.form1.sh_state.length;i++)
		{
			if(window.document.form1.sh_state.options[i].value==window.document.form1.state.value)
			{
				window.document.form1.sh_state.options[i].selected=true;
			}
		}
		
		window.document.form1.sh_zip.value=window.document.form1.zip.value;
		
		window.document.form1.sh_email.value=window.document.form1.email.value;
		window.document.form1.rsh_email.value=window.document.form1.remail.value;
		window.document.form1.stelephone.value=window.document.form1.telephone.value;
	}
}

function chk_register(thisform)
{
	with(thisform)
	{
		if(trimString(email.value)=="")
		{
			alert("Pleaes enter your email address");
			email.focus();
			return false;
		}
		
		if(!emailCheck(email.value))
		{
			alert("Please enter a valid email address");
			email.focus();
			return false;
		}
		
		if(pass.value=="")
		{
			alert("Pleaes enter password");
			pass.focus();
			return false;
		}
		
		if(pass.length<6)
		{
			alert("Please enter a minimum of 6 characters for password");
			pass.focus();
			return false;
		}
		
		if(trimString(fname.value)=="")
		{
			alert("Please enter your firstname");	
			fname.focus();
			return false;
		}
		
		if(trimString(lname.value)=="")
		{
			alert("Please enter your lastname");	
			lname.focus();
			return false;
		}
		
		if(trimString(phone.value)!=="" && phone.value.length>12)
		{
			alert("Please enter a phone no. of up to 12 characters");
			phone.focus();
			return false;
		}
		
		if(trimString(address.value)=="")
		{
			alert("Please enter your address");	
			address.focus();
			return false;
		}
		
		if(trimString(town.value)=="")
		{
			alert("Please enter city");	
			town.focus();
			return false;
		}
		
		if(state.value==0)
		{
			alert("Please select a state");	
			state.focus();
			return false;
		}
		
	}
	
}

function chk_authorize_frm(thisform)
{
	with(thisform)
	{
		if(firstname.value=="")	
		{
			alert("Please enter first name");
			firstname.focus();
			return false		
		}
		
		if(lastname.value=="")
		{
			alert("Please enter last name");
			lastname.focus();
			return false;
		}
		
		if(address.value=="")
		{
			alert("Please enter address");
			address.focus();
			return false;
		}
		
		if(city.value=="")
		{
			alert("Please enter city");
			city.focus();
			return false;
		}
		
		if(state.value=="")
		{
			alert("Please enter state")	;
			state.focus();
			return false;
		}
		
		if(zip.value=="")
		{
			alert("Please enter zip");	
			zip.focus();
			return false;
		}
		
		if(country.value=="")
		{
			alert("Please enter country")	;
			country.focus();
			return false;
		}
		
		
		if(card_num.value=="")
		{
			alert("Please enter card no.");	
			card_num.focus();
			return false;
		}
		
		if(month.value=="0")
		{
			alert("Please select expiry month");	
			month.focus();
			return false;
		}
		
	}
	
}