function checkrequired(idx) 
{
	var pass=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.name.indexOf("email")>-1)
				{
					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.indexOf("email")<0 && ((tempobj.type=="text"||tempobj.type=="textarea")&&
				tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
				tempobj.selectedIndex==0)) 
				{
					pass=false;
					break;
				}
			}
	    }
	}
	if (!pass) 
	{
		shortFieldName=tempobj.name.substring(9,30).toUpperCase();
		alert("Please make sure the "+shortFieldName+" field was properly completed.");
		tempobj.focus();
	}
	else {	document.forms[idx].submit();  }
}

function checkForm(idx,cc) 
{
	var pass=true; var email=true;
	if (document.images && document.forms[idx]) 
	{
		for (i=0;i<document.forms[idx].length;i++) 
		{
			var tempobj=document.forms[idx].elements[i];
			if (tempobj.name.indexOf("required_")==0) 
			{
				if  ((tempobj.value=="") || (tempobj.type.indexOf("s")>-1 && tempobj.selectedIndex==0) )
				{ 	pass=false; break;	}
			}
			else if (tempobj.name.indexOf("email")>-1)
			{
				email = emailCheck(tempobj.value);
				if (!email) 
				{
					pass=false; 
					alert("This email address appears to be invalid. Please check it and enter the correct address.");
					tempobj.focus();
					break; 
				}
			}
	    }
	}
	if (email && !pass) 
	{
		shortFieldName=tempobj.name.substring(9,30).toUpperCase();
		alert("Please make sure the "+shortFieldName+" field was properly completed.");
		tempobj.focus();
	}
	if (pass && email && cc=="false")
	{	
		document.forms[idx].submit();  
	}
	else if (pass && email && cc=="true")
	{	
		if (validateCC(idx)) document.forms[idx].submit();
	}
}



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;
}

function validateCC(idx)
{
	var temp = document.forms[idx];
	var ccpass = true; var card = false; var card_date = false;
	var paymethod =	temp.payment_type.value;
	if (!paymethod)
	{
	 	ccpass=false;
		alert("Please check a method of payment.");
	}
	if (ccpass && paymethod=="VISA" || paymethod=="MC")
	{
		if (temp.required_credit_card_number.value == "")
		{
			ccpass=false;
			alert("Please enter a credit card number.");
			temp.required_credit_card_number.focus();
		}
		else var card = cardCheck(temp.required_credit_card_number.value);
		if (ccpass && !card) 
		{
			ccpass=false;
			alert("You have entered an invalid credit card number. Please check the number and enter it again.");
			temp.required_credit_card_number.focus();
		}
		// check the card expiry date
		var yr = temp.exp_year.value;
		var month = temp.exp_month.value;
		card_date=dateCheck(yr,month);
		if (ccpass && !card_date)
		{
			ccpass=false;
			alert("Your credit card appears to have expired. Please check your card and set the correct expiry date.");
		}
	}
	if (ccpass && card && card_date) return true;
}

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 dateCheck(yeer,month)
{
	var today = new Date();
	var m = today.getMonth()+1;
	var yr = today.getFullYear();
	//var year = "20" + yeer;
	if ((yeer == yr && month >= m) || yeer > yr ) 
		return true;
	else return false;	
}

function clearForm(i)
{
	var frm = document.forms[i];
	var len = frm.elements.length;
	for (c=0; c<len; c++)
	{
		temp = frm.elements[c];
		//alert(temp.name+", "+temp.type);
		if (temp.type=="textarea") temp.innerHTML="";
		else if(temp.type=="text") temp.value="";
		else if (temp.type=="radio"||temp.type=="checkbox") temp.checked=false;
	}
}
