
var theSForm;
var theSForm;

function init() {
	theSForm = document.dataForm;
	theSForm = document.submitOrder;
//	getLevel();
}

function setPayment(thePanel)
{
	var Panel1 = document.getElementById("paymentCreditCard");
	var Panel2 = document.getElementById("paymentCheque");
	var Panel3 = document.getElementById("paymentDirectDeposit");

	Panel1.style.left = (thePanel == "1") ? "auto" : "-999em";
	Panel2.style.left = (thePanel == "2") ? "auto" : "-999em";
	Panel3.style.left = (thePanel == "3") ? "auto" : "-999em";
}
function setLevel(theLevel, theExpiryDate)
{
	prodCost = eval('theSForm.PRICE'+theLevel+'.value');

	for (i = 0; i < theSForm.level.length; i++) 
	{
		eval('theSForm.QTY'+i).value = 0; 
	}
	eval('theSForm.QTY'+theLevel).value = 1;

	theSForm.finalCharge.value = prodCost;
	document.getElementById("totalTable").innerHTML = (theExpiryDate.length == 0) ? 'Total AUD (incl. GST) $'+prodCost : 'Total AUD (incl. GST) $'+prodCost + '<br>Expiring on ' + theExpiryDate;
}

function getLevel()
{
	var theRadio	= theSForm.level;
	var isLevelSet  = false;
	var undef;
	
	if (theRadio.length == undef)
	{
		if (theRadio.checked == true)
		{
			isLevelSet = true;
			setLevel(0,eval('theSForm.expiryDate0').value);
		}
	}
	else
	{
		for (i = 0; i < theRadio.length; i++)
		{
			if (theRadio[i].checked == true)
			{
				isLevelSet = true;
				setLevel(i, eval('theSForm.expiryDate'+i).value);
			}
		}
	}
	if (!isLevelSet)
	{
		alert('Please select a Membership Period');
		return false;
	}
	return true;
}


function isEmpty(theEl)
{
	if (theEl.value.length<1) return true;
	return false;
}

function checkForm()
{
//	alert('what freaking level')
	if (isEmpty(theSForm.billingEmail)) {alert("Please enter your email address.");theSForm.billingEmail.focus();return false;}

	var email=theSForm.billingEmail.value;

	if (email.indexOf('@')==-1 || email.indexOf('.')==-1 || email.length<7 || email.charAt(0)=='@' || email.charAt(email.length-1)=='@') {alert("'"+email+"' does not seem to be a valid email\n address. Please check it.");theSForm.email.focus();return false;}

	if (theSForm.password)
	{
		if (isEmpty(theSForm.password)) {alert("Please enter a password.");theSForm.password.focus();return false;}
		var pass1=theSForm.password.value;
		if (pass1.length<4) {alert("Your password must be at least 4 characters long.");theSForm.password.focus();return false;}
		if (pass1.length>8) {alert("Your password cannot be longer than 8 characters long.");theSForm.password.focus();return false;}
		if (pass1.toLowerCase()=="password") {alert("Sorry, '"+pass1+"' is not an acceptable password.");theSForm.password.focus();return false;}

		if (isEmpty(theSForm.password2)) {alert("Please confirm your password.");theSForm.password2.focus();return false;}
		var pass2=theSForm.password2.value;
		if (pass1 != pass2) {alert("Your passwords do not match. Please check them.");theSForm.password2.focus();return false;}
	}
	
	if (isEmpty(theSForm.billingFirstName)) {alert("Please enter your first name");theSForm.billingFirstName.focus();return false;}
	if (isEmpty(theSForm.billingLastName)) {alert("Please enter your last name");theSForm.billingLastName.focus();return false;}
	if (isEmpty(theSForm.billingOrgName)) {alert("Please enter your company name");theSForm.billingOrgName.focus();return false;}
	if (isEmpty(theSForm.billingStreet)) {alert("Please enter your street address");theSForm.billingStreet.focus();return false;}
	if (isEmpty(theSForm.billingSuburb)) {alert("Please enter your city or town");theSForm.billingSuburb.focus();return false;}
	if (isEmpty(theSForm.billingPostcode)) {alert("Please enter your postcode");theSForm.billingPostcode.focus();return false;}

	if (theSForm.billingState.options[theSForm.billingState.selectedIndex].text != "International") {
		var thePostCode = theSForm.billingPostcode.value;
		if (isNaN(thePostCode) || thePostCode.length!=4) {alert("The postcode you have entered does\nnot seem to be valid. Please check it.");theSForm.billingPostcode.focus();return false;}
	}
	if (theSForm.billingCountry.options[theSForm.billingCountry.selectedIndex].value=="") {alert("Please select your country");theSForm.billingCountry.focus();return false;}

	if (isEmpty(theSForm.billingPhone)) {alert("Please enter your phone number");theSForm.billingPhone.focus();return false;}

	var fullName = theSForm.billingFirstName.value+" "+theSForm.billingLastName.value;
/*
	theSForm.billingRecipient.value 	= fullName;
	theSForm.deliveryRecipient.value 	= fullName;
*/
	if (!getLevel()) return false; //  alert('what freaking level'); 

	if (theSForm.paymentMethod[0].checked == true)	// Credit Card Checking
	{
		if (theSForm.cardNumber.value.length < 1) 	{ alert("Please enter your Credit Card Number"); return false; }
		var ccNumber	= theSForm.cardNumber.value;
		cardNumber		= cardNumber.replace(/[^0-9]/gi,"");

		if (luhnCheck(cardNumber) == false)
		{
			alert('The Credit Card number you have entered does not\nappear to be valid');
			return false;
		}

		// check cc date
		var ccDate  = makeDate(theSForm.cardExpiryYear.options[theSForm.cardExpiryYear.selectedIndex].value,theSForm.cardExpiryMonth.options[theSForm.cardExpiryMonth.selectedIndex].value-1,1);
		var today   = new Date();

		if (compDate(today,ccDate)==false)
		{
			alert("Your credit card has expired");
			return false;
		}

		if (theSForm.cardName.value.length < 1) 	{ alert("Please enter the name on your Credit Card"); return false; }
	}
	else
	{
		theSForm.cardNumber.value			=	"";
		theSForm.cardName.value				=	"";
		theSForm.ccv.value					=	"";
		theSForm.cardExpiryMonth.value		=	"";
		theSForm.cardExpiryYear.value		=	"";
	}
 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 makeDate(theYear,theMonth,theDay) {
	theYear = theYear-0; theMonth = theMonth-0; theDay = theDay-0;
	return new Date(theYear,theMonth,theDay);
}

function compDate(firstDate,lastDate) {
	if (firstDate.getTime()>=lastDate.getTime()) return false;
	return true;
}

function popupWindow(url, width, height)
{
	pWindow = window.open (url, 'pWindow', 
		'toolbar=0,location=0,directories=0=0,menubar=0,scrollbars=0,resizable=1,height='+height+',width='+width);
	pWindow.focus();
}