var twbooth = 122;

function checkrequired(idx) 
{
	var pass=true; var email=true;
	if (document.images) 
	{
		for (i=0;i<document.forms[idx].length;i++) 
		{
			var tempobj=document.forms[idx].elements[i];
			if (tempobj.name.indexOf("billing_")==0) 
			{
				if (tempobj.value == "") { pass=false; break; }
				else if (tempobj.name == "billing_Email")
				{
					email = emailCheck(tempobj.value);
					if (!email) 
					{
						pass=false; 
						alert("Your email address appears to be invalid. Please check it and enter your correct address.");
						tempobj.focus();
						break; 
					}
				}
				else if ( tempobj.name == "billing_Postal_Zip_Code" && tempobj.value=="")
				//&& document.forms[idx].elements[i+1].checked == false) 
        		{   pass=false; break; }        
        		else if  ( (tempobj.name != "billing_Postal_Zip_Code" && tempobj.value=="")
				|| (tempobj.type.indexOf("s")>-1 && tempobj.selectedIndex==0) )
				{ 	pass=false; break;	}
			}
	    }
	}
	if (email && !pass) 
	{
		shortFieldName=tempobj.name.substring(8,30).toUpperCase();
		alert("Please make sure the "+shortFieldName+" field was properly completed.");
		tempobj.focus();
	}
	else if (pass && email)
	{	
		//saveContactCookie('contact'); 
		document.forms[idx].submit();  
	}
}


function checkshipping(idx) 
{
	var pass=true; var amount=true;
	if (document.images) 
	{
		for (i=0;i<document.forms[idx].length;i++) 
		{
			var tempobj=document.forms[idx].elements[i];
			if (tempobj.name == "amount" && tempobj.value=="")
			{
				amount=false; 
				alert("Please enter an amount for the gift certificate.");
				tempobj.focus();
				break; 
			}
			else if (tempobj.name.indexOf("shipping_")==0) 
			{
				if (tempobj.value == "") { pass=false; break; }
				
				else if ( tempobj.name == "shipping_Postal_Zip_Code" && tempobj.value=="" 
				&& document.forms[idx].elements[i+1].checked == false) 
        		{   pass=false; break; }        
        		else if  ( (tempobj.name != "shipping_Postal_Zip_Code" && tempobj.value=="")
				|| (tempobj.type.indexOf("s")>-1 && tempobj.selectedIndex==0) )
				{ 	pass=false; break;	}
			}
	    }
	}
	if (amount && !pass) 
	{
		shortFieldName=tempobj.name.substring(9,30).toUpperCase();
		alert("Please make sure the RECIPIENT "+shortFieldName+" field was properly completed.");
		tempobj.focus();
	}
	else if (pass && amount)
	{	
		checkrequired(idx);  
	}
}




function validateCC(idx)
{
	var pass=true; var x = 1; var date = true;
	if (document.images) 
	{
		for (i=0;i<document.forms[idx].length;i++) 
		{
			var tempobj=document.forms[idx].elements[i];
			if (tempobj.name.substring(0,9)=="required_")
			{
				if (tempobj.value=="") { pass=false;	break;	}
			}
	    }
	}
	// check the card expiry date
	var today = new Date();
	var m = today.getMonth()+1;
	var y = today.getFullYear();
	var year = "20" + document.forms[idx].exp_year.value;
	var month = document.forms[idx].exp_month.value;
	
	if ((year == y && month >= m) || year > y ) date = true;
	else date = false;
	// check the card number
	var card = cardCheck(document.sendorder.required_card_number.value);
	
	if (!pass) 
	{
		shortFieldName=tempobj.name.substring(9,30).toUpperCase();
		alert("Please fill in  your "+shortFieldName);
		if (tempobj.type!="radio" && tempobj.type!="hidden") tempobj.focus(); 
	}
	
	else if (!card) 
	{
		shortFieldName=tempobj.name.substring(9,30).toUpperCase();
		alert("You have entered an invalid credit card number. Please check the number and enter it again.");
		tempobj.focus();
	}
	
	else if (!date) 
	{
		shortFieldName=tempobj.name.substring(9,30).toUpperCase();
		alert("Your credit card appears to have expired. Please check your card and set the correct expiry date.");
		tempobj.focus(); 
	}

	else {	wopener('','receipt','600','575');
		document.forms[idx].submit(); 
		//document.location="thanks.html"; 
		}
}


function cardCheck(CardNumber)
{
	var no_digit = CardNumber.length;
	var oddoeven = no_digit & 1;
	var sum = 0;
	for (var count = 0; count < no_digit; count++) 
	{
		var digit = parseInt(CardNumber.charAt(count));
		if (!((count & 1) ^ oddoeven)) 
		{
			digit *= 2;  
			if (digit > 9) digit -= 9;
		}
		sum += digit;
	}
	if (sum % 10 == 0) return true;
	else
	// You can make this test live by removing the // on the next line and putting // on the one after.
	return false;
	//return true;
}	

function clearShipping()
{
	if (document.forms[0].ship_address.checked)
	{
		var startfield = "shipping_Contact_Name";
		var endfield = "send";
		var flen = document.forms[0].length;
		for (var x = 0 ; x <= flen ; x ++) 
		{
			if (document.forms[0].elements[x].name == startfield) 
			{ 	var start = x; 	}
			if (document.forms[0].elements[x].name == endfield) 
			{ 	var endit = x; break;	}
		}
		for (var i = start; i < endit; i ++)  
		{ document.forms[0].elements[i].value=""; }
	}
}

function wopener(wurl,wname,wwidth,wheight)
{
newwindow = window.open(wurl,wname,"toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width="+wwidth+",height="+wheight+"")
}

// subroutine to check for valid email address
// This script and many more are available free online at 
// The JavaScript Source!! http://javascript.internet.com 

function emailCheck (emailStr) {
	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]
	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 domainArray=domain.match(domainPat)
	if (domainArray==null) {
	    return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   return false
	}
	if (len<2) {
	   return false
	}
	return true;
}
