var errorArray = [];
var errorText = "";
var errorHeader = "";
var errorFooter = "";
var mandatoryArray = [	{field:"deliveryOption", alertText:"Please choose a delivery option"},//0
						{field:"recipient", alertText:"Please fill in your email address"},//1
						{field:"recipient", alertText:"Please use a valid email address"},//2
						{field:"telephone", alertText:"Please fill in your telephone number"},//3
						{field:"CardName", alertText:"Please fill in your name (as printed on Credit Card)"},//4
						{field:"CardNumber", alertText:"Please fill in your main Credit Card number (main number across front)"},//5
						{field:"CardNumber", alertText:"Please use a valid Credit Card number"},//6
						{field:"CardType", alertText:"Please select Card Type"},//7
						{field:"ExpMon", alertText:"Please select card expiry month"},//8
						{field:"ExpYear", alertText:"Please select card expiry year"},//9
						{field:"Security", alertText:"Please fill in your Credit Card Security ID number (last 3 digits on back of card)"},//10
						{field:"billaddr1", alertText:"Please fill in your billing address"},//11
						{field:"billpostcode", alertText:"Please fill in your billing address postcode"},//12
						{field:"tc", alertText:"Please check terms and conditions box"},//13
						{field:"Issue", alertText:"Please enter an Issue Number"}];//14

function clearErrors(theForm){
	for(i=0;i<mandatoryArray.length;i++){
		//theForm[mandatoryArray[i].field].style.border = "solid 1px #999999";
	}
	errorArray = [];
	errorText = "";
	errorFooter = "";
}

function checkForm(theForm){
				// reset previous submission errors
				clearErrors(theForm);
	
				// disable button
				theForm.submit.disabled = true;
				theForm.submit.value = "PLEASE WAIT...";
				
				//rewrite ProdPrice.value to remove comma
				theForm.ProdPrice.value = theForm.grandTotal.value = price*theForm.quantity.value;
				
				if(theForm.subTotal){
					theForm.subTotal.value = a+b+c+d+e+f;
					theForm.grandTotal.value = a+b+c+d+e+f-discountAmount;
				}else{
					theForm.grandTotal.value = theForm.ProdPrice.value-discountAmount;
				}
			
			emailRegEx = /^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.(([a-z][a-z])|org|mil|com|net|org|int|gov|biz|info|coop|museum|name|pro|aero)$/i;	
			
			if(theForm.deliveryOption && theForm.deliveryOption.options[theForm.deliveryOption.selectedIndex].innerHTML == "Please Select") buildErrorAlert(0);

			if (theForm.recipient.value == "") buildErrorAlert(1);

			if(!emailRegEx.exec(theForm.recipient.value)) buildErrorAlert(2);

			if(theForm.telephone.value == "") buildErrorAlert(3);

			if(theForm.CardName.value == "") buildErrorAlert(4);

			if(theForm.CardNumber.value == "") buildErrorAlert(5);

			if(theForm.CardNumber.value.length<10) buildErrorAlert(6);

			if(theForm.CardType.selectedIndex==0) buildErrorAlert(7);

			if(theForm.ExpMon.selectedIndex==0) buildErrorAlert(8);

			if(theForm.ExpYear.selectedIndex==0) buildErrorAlert(9);

			if(theForm.Security.value == "" || theForm.Security.value.length <3) buildErrorAlert(10);

			if(theForm.billaddr1.value == "" && theForm.billaddr2.value == "" && theForm.billaddr3.value == "" && theForm.billaddr3.value == "") buildErrorAlert(11);
			
			if(theForm.billpostcode.value == "") buildErrorAlert(12);
			
			if(!theForm.tc.checked) buildErrorAlert(13);
			
			//Switch card check, make sure there is an Issue Number entered
			if(theForm.CardType.value == "SWITCH" && theForm.Issue.value == "") buildErrorAlert(14);
			
			if(errorArray.length>0){
				var divider = "-----------------------------------------------------------------------------------------------------";
				errorHeader = "You have "+errorArray.length+" error";
					if(errorArray.length>1){
						errorHeader += "s";
					}
				errorHeader += " in your form:\n"+divider+"\n";
				errorFooter += divider+"\nThese fields are highlighted in red.";
				alertErrors(theForm);
				theForm.submit.value = "SUBMIT PURCHASE";
				theForm.submit.disabled = false;
				return (false);
			}else{
				theForm.submit();
				//return (true)
			}
}

function buildErrorAlert(error){
	errorArray.push(mandatoryArray[error].field);
	errorText += errorArray.length+". "+mandatoryArray[error].alertText+"\n";
}

function alertErrors(theForm){
	for(i=0;i<errorArray.length;i++){
		theForm[errorArray[i]].style.backgroundColor = "#f2bfbf";
	}
	alert(errorHeader+errorText+errorFooter);
}

function clearHighlight(field){
	field.style.backgroundColor = "#ffffff";
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function selectTab(tab)
{
	switch(tab)
	{
		case 0: document.getElementById('diffDelAdd').style.visibility = 'hidden'; document.getElementById('diffDelAdd').style.display = 'none'; break;
		case 1: document.getElementById('diffDelAdd').style.visibility = 'visible'; document.getElementById('diffDelAdd').style.display = ''; break;
	}
}

function diffDelAddress(){
	if(document.checkoutForm.diffAddCheckbox.checked){
		selectTab(1);
	}else{
		selectTab(0);
	}
}
