// JavaScript Document

function validate(theForm) {
	
	var emailRE = /^.+@.+\..{2,3}$/
	
	// Card Number Modulus Check
	var cardNumber = theForm.CNUM.value;
	var ar = new Array( cardNumber.length );
	var i = 0; 
	var sum = 0;
   	
	for( i = 0; i < cardNumber.length; ++i ) { 
		ar[i] = parseInt(cardNumber.charAt(i)); 
	}
	for( i = ar.length -2; i >= 0; i-=2 ) {
   		ar[i] *= 2;
    	if( ar[i] > 9 ) { ar[i]-=9; }
	}
	for( i = 0; i < ar.length; ++i ) { 
		sum += ar[i]; 
	}
	if ( (sum%10) == 0 ) { cardNumber = 0 }
	
	// Name/Address Validation
	if (theForm.Contact.value.length < 1) {
		alert("Please enter your name");
		theForm.Contact.focus();
		return false;	
	} else if (theForm.Add1.value.length < 1) {
		alert("Please enter the first line of your address");
		theForm.Add1.focus();
		return false;
	} else if (theForm.Town.value.length < 1) {
		alert("Please enter a town");
		theForm.Town.focus();
		return false;
	} else if (theForm.Postcode.value.length < 1) {
		alert("Please enter a postcode");
		theForm.Postcode.focus();
		return false;
	} else if (theForm.Tel.value.length < 1) {
		alert("Please enter a contact telephone number");
		theForm.Tel.focus();
		return false;
	} else if (!emailRE.test(theForm.Email.value)) {
		alert("Please enter a valid email address");
		theForm.Email.focus();
		return false;
	
	// Card Validation
	} else if (cardNumber != 0) {
		alert("Please check your card number and re-enter");
		theForm.CNUM.focus();
		return false;
	} else if (theForm.CNUM.value.length < 15) {
		alert("Please check your card number and re-enter");
		theForm.CNUM.focus();
		return false;
	} else if (isNaN(theForm.CNUM.value)) {
		alert("Please enter your full card number without spaces or dashes");
		theForm.CNUM.focus();
		return false;
	//} else if (theForm.SMONTH.value == "00") {
	//	alert("Please enter your card Start Month");
	//	theForm.SMONTH.focus();
	//	return false;		
	//} else if (theForm.SYEAR.value == "0000") {
	//	alert("Please enter your card Start Year");
	//	theForm.SYEAR.focus();
	//	return false;		
	} else if (theForm.EMONTH.value == "00") {
		alert("Please enter your card Expiry Month");
		theForm.EMONTH.focus();
		return false;		
	} else if (theForm.EYEAR.value == "0000") {
		alert("Please enter your card Expiry Year");
		theForm.EYEAR.focus();
		return false;
	} else if (theForm.SECNUM.value.length < 3) {
		alert("Please enter your card security number");
		theForm.SECNUM.focus();
		return false;				
	}
}

function MM_popupMsg(msg) { //v1.0
  alert(msg);
}