var errorArray = [];
var errorText = "";
var errorHeader = "";
var errorFooter = "";
var mandatoryArray = [	{field:"recipient", alertText:"Please fill in your email address"},//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 confirm you understand the important message attached to the purchasing of this product, and that terms and conditions apply when returning this product for a refund. If this product has been installed by our engineers we will not be able to refund the cost of the installation should you wish to return the unit as per your consumer rights."},//13
						{field:"Issue", alertText:"Please enter an Issue Number"},//14
						{field:"deladdr1", alertText:"Please fill in your delivery address"},//15
						{field:"delpostcode", alertText:"Please fill in your delivery address postcode"}];//16						

function clearErrors(theForm){
	for(i=0;i<mandatoryArray.length;i++){
		//theForm[mandatoryArray[i].field].style.border = "solid 1px #999999";
	}
	errorArray = [];
	errorText = "";
	errorFooter = "";
}

function checkForm2(theForm){
				// reset previous submission errors
				clearErrors(theForm);
	
				// disable button
				theForm.submit.disabled = true;
				theForm.submit.value = "Please wait...";
				
			
			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 = "Add to Basket";
				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 selectCustomer(tab)
{
	
	switch(tab)
	{
		case 0: document.getElementById('spnReturningCustomer').style.visibility = 'hidden'; document.getElementById('spnReturningCustomer').style.display = 'none';document.getElementById('newCustomer').style.visibility = 'visible'; document.getElementById('newCustomer').style.display = ''; break;
		case 1: document.getElementById('newCustomer').style.visibility = 'hidden'; document.getElementById('newCustomer').style.display = 'none';document.getElementById('spnReturningCustomer').style.visibility = 'visible'; document.getElementById('spnReturningCustomer').style.display = ''; break;
	}
}

function showCustomer()
{
	if(document.getElementById('rbNewCustomer').checked)
	{
		selectCustomer(0);	
	}
	
	if(document.getElementById('rbReturningCustomer').checked)
	{
		selectCustomer(1);		
	}
	
}

function showCustomerType()
{
	document.getElementById('typeOfCustomer').style.visibility = 'visible'; 
	document.getElementById('typeOfCustomer').style.display = '';
}

function hideCustomerType()
{
	document.getElementById('typeOfCustomer').style.visibility = 'hidden'; 
	document.getElementById('typeOfCustomer').style.display = 'none';
}

function checkPostCodesMatch()
{
	//Take first 2 chars of billpostcode and delpostcode and make sure they are the same
		var bpc = document.checkoutForm.billpostcode.value;
		var dpc = document.checkoutForm.delpostcode.value;

//		var bpc = "Test";
//		var dpc = "test";
		
		//alert("bpc:" + bpc + "dpc:" + dpc);
//		if(	String(bpc).substring(0,2).toUpperCase() != String(dpc).substring(0,2).toUpperCase())
		if(	String(bpc).toUpperCase() != String(dpc).toUpperCase())
		{
			alert("The delivery and billing post codes are different.  Please note HUMAXDIRECT reserves the right to refuse to deliver to an alternative delivery address, and will only do so after having spoken to the cardholder on the telephone number provided (mobiles are NOT acceptable for this purpose).");	
			document.checkoutForm.diffAddCheckbox.value = "Different";		
			selectTab(1);
		}
		else 
		{
			document.checkoutForm.diffAddCheckbox.value = "Same";		
			document.checkoutForm.deladdr1.value = document.checkoutForm.billaddr1.value;
			document.checkoutForm.deladdr2.value = document.checkoutForm.billaddr2.value;
			document.checkoutForm.deladdr3.value = document.checkoutForm.billaddr3.value;
			document.checkoutForm.deladdr4.value = document.checkoutForm.billaddr4.value;			
			selectTab(1);
		}
}

function checkDeliveryPostCodesMatch()
{
		var dpc1 = document.checkoutForm.delpostcode.value;
		var dpc2 = document.checkoutForm.prevdelpostcode.value;
		
		//alert("bpc:" + bpc + "dpc:" + dpc);
		if(	String(dpc1).substring(0,2).toUpperCase() != String(dpc2).substring(0,2).toUpperCase())
//		if(	String(dpc1).toUpperCase() != String(dpc2).toUpperCase())
		{
			alert("The delivery post code entered is different to that entered to calculate delivery costs.  Your order will be taken, however, you will be contacted by a member of Customer Services to confirm delivery costs prior to the order being processed.");	
			document.checkoutForm.diffDeliveryPostCode.value = "Different";		
		}
		else 
		{
			document.checkoutForm.diffDeliveryPostCode.value = "Same";		
		}
}

//function diffDelAddress(){
//	if(document.checkoutForm.diffAddCheckbox.checked){
//		selectTab(1);
//	}else{
//		selectTab(0);
//	}
//}
