
function clearQuotes(theForm) {
	for (i=0;i<theForm.elements.length;i++) {
		if (theForm.elements[i].value) {
			theForm.elements[i].value = theForm.elements[i].value.replace(/'/gi,"`");
		}
	}
}

function checkEmail(theEmail) {
	if (theEmail.indexOf('.')==-1) return false;
	if (theEmail.indexOf('@')==-1) return false;
	if (theEmail.length<6) return false;
	return true;
}

function luhnCheck(cardNumber) {
	var checkOK		= "0123456789";
	var CrValid		= true;
	var checksum	= 0;
	var ddigit		= 0;
	var kdig		= 0;

	if (cardNumber.length < 13)
		{
			alert ('You have not entered enough digits in Credit Card Number.');
			return false;
		}
	for (i = cardNumber.length - 1; i >= 0; i--)
		{
			kdig = kdig + 1;
			ch = cardNumber.charAt(i);
			if ((kdig % 2) != 0)			checksum = checksum+parseInt(ch);
			else {
				ddigit = parseInt(ch) * 2;
				if (ddigit >= 10)			checksum = checksum + 1 + (ddigit - 10);
				else						checksum = checksum + ddigit;
			}
			for (j = 0; j < checkOK.length; j = j + 1)
			{
				if (ch == checkOK.charAt(j)) break;
			}
			if (j == checkOK.length)
			{
				alert("Please enter only digits. No dashes or non-numeric characters.");
				return false;
			}
		}
	if ((checksum % 10) != 0) return false;
	return true;
}



function SubmitIt()
{
	var theForm = document.dataForm;
	clearQuotes(theForm);

	if (theForm.billingFirstName.value.length<1) {alert("Please enter your first name.");	theForm.billingFirstName.focus();return false;}
	if (theForm.billingLastName.value.length<1)  {alert("Please enter your last name.");	theForm.billingLastName.focus();return false;}

	if (theForm.billingEmail.value == '' && theForm.billingPhone.value == '') 
	{
		alert ('Please enter a contact email address or phone number.');
		theForm.billingEmail.focus();
		return false;
	}

	if (theForm.billingEmail.value.length>0) {
		if (checkEmail(theForm.billingEmail.value)==false)  {alert("The email address you have entered does not appear to be valid.");theForm.billingEmail.focus();return false;}
	}

	if (theForm.message.value.length<1) {alert("Please enter a description of your payment");theForm.message.focus();return false;}
	if (theForm.deliverySpecialInfo.value.length<1) {alert("Please enter the name or names of the Delegates");theForm.deliverySpecialInfo.focus();return false;}
	var theValue = theForm.amount.value;
	theValue = theValue.replace(/[^0-9\.]*/gi,"");
	theForm.amount.value = theValue;
	if (theValue.length<1) {alert("Please enter the amount to pay");theForm.amount.focus();return false;}
	if ((theValue-0)<1) {alert("The minimum amount to pay is $1.00");theForm.amount.focus();return false;}
	if (isNaN((theValue-0)/2)) {alert("Please enter only numbers in the amount field");theForm.amount.focus();return false;}

	if (theForm.cardName.value.length<1) {alert("Please enter the name on your credit card.");	theForm.cardName.focus();return false;}

	var ccNum = theForm.cardNumber.value;
	ccNum = ccNum.replace(/[^0-9]*/gi,"");
	theForm.cardNumber.value = ccNum;
	if (ccNum.length<13) {alert("Your credit card number does not have enough digits.");	theForm.cardNumber.focus();return false;}

	if (luhnCheck(ccNum)==false)
	{
		alert("The credit card number you have entered does not appear to be valid. Please check it for errors.");
		theForm.cardNumber.focus();
		return false;
	}

	var ThisDate		= new Date();
	var currentyear		= ThisDate.getFullYear();
	var currentmonth	= ThisDate.getMonth() + 1;
	var AllExpireYear	= 2040;
	cardexpirymonth	= theForm.cardExpiryMonth.options[theForm.cardExpiryMonth.selectedIndex].value-0;
	cardexpiryyear	= theForm.cardExpiryYear.options[theForm.cardExpiryYear.selectedIndex].value-0;
	if ((currentyear > cardexpiryyear) || (currentyear == cardexpiryyear && currentmonth >= cardexpirymonth))
	{
		alert("Your credit card appears to have expired. Please check the expiry date you have entered.");
		return false;
	}

	var CCV = theForm.ccv.value;
	CCV = CCV.replace(/[^0-9]*/gi,"");
	theForm.ccv.value = CCV;
	if (CCV.length<1) {alert("Please enter your CVV number (three or four extra\ndigits usually located on the back of your card).");	theForm.ccv.focus();return false;}

	return true;
}

