// //////////////////////////////////////////////////////////////////////////////

// Customer registration validation - full ColourClick membership

// //////////////////////////////////////////////////////////////////////////////


function ValidateFullColourClick(f)
	{
	var result = false;

	if 	(f.invoicepaymentmethod.selectedIndex == 0) {alert('Please choose a payment method');}
	else if (f.cardnumber.value == "") {alert('Please enter your credit card number');}
	else if (f.cardholdername.value == "") {alert('Please enter the card holder name');}
	else if (f.cardexpiry.value == "") {alert('Please enter the Expiry details');}
	else if (f.cardsecuritynumber.value == "") {alert('Please enter 3-digit security number, usually found on the back of your card');}
	else if ( (f.cardissuenumber.value == "") && (f.invoicepaymentmethod.selectedIndex== 4) ) {alert('Please enter the Switch/Maestro card issue number');}

	else if (f.invoiceaddress.value.length== 0) {alert('Please enter your billing address');}
	else if (f.invoicecity.value== "") {alert('Please enter your billing city');}
	else if (f.invoicecounty.value== "") {alert('Please enter your billing county');}
	else if (f.invoicecountry.value== 9999) {alert('Please enter your billing country');}
	
	else if (f.mailselect.value== 0) {alert('Please select Mailing address: Same as Billing or Different?');}	
	else if ( (f.mailselect.value== 2) && (f.mailaddress.value=="") ) {alert('Please enter your mailing address');}
	else if ( (f.mailselect.value== 2) && (f.mailcity.value=="") ) {alert('Please enter your mailing city');}
	else if ( (f.mailselect.value== 2) && (f.mailcounty.value=="") ) {alert('Please enter your mailing county/state');}
	else if ( (f.mailselect.value== 2) && (f.mailcountry.value== 9999) ) {alert('Please enter your mailing country');}

	else 
		result = true;

	if (result)	{result=checkCard(f.invoicepaymentmethod[f.invoicepaymentmethod.selectedIndex].value,f.cardnumber.value);}

	return result;		
	}




// //////////////////////////////////////////////////////////////////////////////

// Checkout validation

// //////////////////////////////////////////////////////////////////////////////

function checkdata(f)

{
var result;
var action;

result=false;
action=f.action.value;

//alert(action);



//	Invoice address details
if (action == 1 || action == 9)
		{
		result=true;
//		alert('Invoice address validation');
		}




// Delivery address details
if (action == 2 || action ==9)
		{
//		if (f.customerorderref.value == "")
//			alert('Please enter an Order Number');
		result=true;
//		alert('Delivery address validation');
		}
	


// Order lines
if (action == 3 || action==9)
		{
		result=true;
//		alert('Item quantities update');
		}



// Payment
if (action == 4 || action==9)
		{
		result=false;
//		alert('Payment details update');

			{
	
			if   (f.cardnumber.value == "") 
				alert('Please enter your credit card number');

			else if   (f.cardholdername.value == "") 
				alert('Please enter the card holder name');

			else if   (f.cardexpiry.value == "") 
				alert('Please enter the Expiry details');

			else if   (f.cardsecuritynumber.value == "") 
				alert('Please enter 3-digit security number, usually found on the back of your card');

			else if   (f.cardissuenumber.value == "" && f.paymentmethod.selectedIndex== 4)			// NB. Hard-coded 4 from tPaymentMethods
				alert('Please enter the Switch/Maestro card issue number');
			
			else
				result=true;
			}
	
		
		if (result)
			result=checkCard(f.paymentmethod[f.paymentmethod.selectedIndex].value,f.cardnumber.value) ;
			

		}



// Submit (==9)
if (action==9)
		{
		}



return result;

}






function checkCardNumWithMod10(cardNum) {
	var i;
	var cc = new Array(16);
	var checksum = 0;
	var validcc;

	// assign each digit of the card number to a space in the array	
	for (i = 0; i < cardNum.length; i++) {
		cc[i] = Math.floor(cardNum.substring(i, i+1));
	}

	// walk through every other digit doing our magic
	// if the card number is sixteen digits then start at the
	// first digit (position 0), otherwise start from the
	// second (position 1)
	for (i = (cardNum.length % 2); i < cardNum.length; i+=2) {
		var a = cc[i] * 2;
		if (a >= 10) {
			var aStr = a.toString();
			var b = aStr.substring(0,1);
			var c = aStr.substring(1,2);
			cc[i] = Math.floor(b) + Math.floor(c);
		} else {
			cc[i] = a;
		}
	}

	// add up all of the digits in the array
	for (i = 0; i < cardNum.length; i++) {
		checksum += Math.floor(cc[i]);
	}

	// if the checksum is evenly divisble by 10
	// then this is a valid card number
	validcc = ((checksum % 10) == 0);

	return validcc;
}

function cleanCardNum(cardNum) {
	var i;
	var ch;
	var newCard = "";


	// walk through the string character by character to build
	// a new string with numbers only
	i = 0;
	if (i == cardNum.length) {
		alert("Please enter your card number");
		return "";
		}
	
	while (i < cardNum.length) {
		// get the current character
		ch = cardNum.substring(i, i+1);
		if ((ch >= "0") && (ch <= "9")) {
			// if the current character is a digit then add it
			// to the numbers-only string we're building
			newCard += ch;
		} else {
			// not a digit, so check if its a dash or a space
			if ((ch != " ") && (ch != "-")) {
				// not a dash or a space so fail
				alert("The card number contains invalid characters");
				return "";
			}
		}
		i++;
	}

	// we got here if we didn't fail, so return what we built
	return newCard;
}

function checkCard(cardType, cardNum) {
	var validCard;
	var cardLength;
	var cardLengthOK;
	var cardStart;
	var cardStartOK;
	
	// check if the card type is valid
	// V=Visa, M=Mastercard, D=Discover
	if ((cardType !=1) && (cardType != 2) && (cardType != 3) && (cardType != 4) ) {
		alert("Please select a card type" );
		return false;
	}

	// clean up any spaces or dashes in the card number
	validCard = cleanCardNum(cardNum);
	if (validCard != "") {
		// check the first digit to see if it matches the card type
		cardStart = validCard.substring(0,1);
		cardStartOK = (    ((cardType == "1") && (cardStart == "4")) ||
				((cardType == "2") && (cardStart == "5")) ||
				((cardType == "3") && (cardStart == "3")) ||
				(cardType == "4")				);

		if (!(cardStartOK)) {
			// card number's first digit doesn't match card type
			alert("Please make sure the card number you've entered matched the card type you selected");
			return false;
		}

		// the card number is good now, so check to make sure
		// it's a the right length
		cardLength = validCard.length;		
		cardLengthOK = ( ((cardType == "1") && ((cardLength == 13) || (cardLength == 16))) ||
				 ((cardType == "2") && (cardLength == 16)) ||
				 (cardType == "4")  ||
				 ((cardType == "3") && (cardLength == 15)) );

		if (!(cardLengthOK)) {
			// not the right length
			alert("Please make sure you've entered all of the digits on your card");
			return false;
		}

		// card number seems OK so do the Mod10
		if (checkCardNumWithMod10(validCard)) {
			return true;
		} else {
			alert("Please make sure you've entered your card number correctly");
			return false;
		}
	} else {
		return false;
	}
}


