



var numbers = "1234567890";	// for checking of valid card and cvn characters
var formSubmitted = false;		// prevent multiple form submissions
var w3c=(document.getElementById)?true:false;
var ie=(document.all)?true:false;
var N=-1;



function validate() {
	if (formSubmitted) {
		alert("Please wait while payment is processing");
		return(false);
	}

	if (!checkCardType()) {
		return(false);
	}

	if (!checkCardNumber()) {
		return(false);
	}

	if (!checkCardName()) {
		return(false);
	}

	if (!checkCVV()) {
		return(false);
	}

	if (!checkLUHN()) {
		return(false);
	}


	formSubmitted = true;
	return(true);
}


// this code looks like a throwback to when we presumably had radio buttons or checkboxes for
// selection of credit card type. I can't see any other reason why it would have looked like this.
// cleaned it up anyway, and moved it into this function.
//
// --andrewh 13/7/06
function checkCardType() {
	var cctype = document.ccform.cc_type[document.ccform.cc_type.selectedIndex].value;

	if (cctype == "") {
		alert("Please choose a credit card type");
		return(false);
	}

	return(true);
}


function checkCardNumber() {
	var cctype = document.ccform.cc_type[document.ccform.cc_type.selectedIndex].value;
	var ccnum = document.ccform.cc_num.value;
	ccnum = "" + ccnum;

	if ((ccnum.length < 12) || (ccnum.length > 19)) {
		alert("Invalid length for credit card number");
		return(false);
	}	

	// does it contain only digits?
	for (i=0; i < ccnum.length; i++) {
		var c = ccnum.charAt(i);
		if(numbers.indexOf(c) == -1) {
			alert("Please enter only digits in the credit card number\n(No spaces or dashes)");
			return(false);
		}
	}

	// does the first digit correspond to the correct card type?
	if ((cctype == "VISA") && (ccnum.substring(0,1) != "4")) {
		alert("Credit Card Number does not correspond to a VISA card");
		return(false);
	}

	if ((cctype == "MC") && (!((ccnum.substring(0,1) == "5") || (ccnum.substring(0,1) == "6") || (ccnum.substring(0,1) == "3")))) {
		alert("Credit Card Number does not correspond to a MasterCard card");
		return(false);
	}

	if ((cctype == "AMEX") && (ccnum.substring(0,1) != "3")) {
		alert("Credit Card Number does not correspond to a AMEX card");
		return(false);
	}

	if ((cctype == "LASER") && (!((ccnum.substring(0,1) == "6") || (ccnum.substring(0,1) == "5")))) {
		alert("Credit Card Number does not correspond to a Laser card");
		return(false);
	}

	if ((cctype == "SWITCH") && (!((ccnum.substring(0,1) == "6") || (ccnum.substring(0,1) == "5") || (ccnum.substring(0,1) == "3") || (ccnum.substring(0,2) == "49")))) {
		alert("Credit Card Number does not correspond to a Switch card");
		return(false);
	}

	return(true);
}


function checkCardName() {
	var ccname = document.ccform.cc_name.value;

	if (ccname == "") {
		alert("Please enter cardholder's name");
		return(false);
	}

	return(true);
}


function checkCVV() {
	// careful.  sometimes these input fields aren't presented to the user, so they're
	// not always going to exist or be populated.  -andrewh 13/7/06

	// are we checking for CVN?
	var cccvcind = "0";
	if (eval(document.ccform.cc_cvs_ind)) {
		cccvcind = document.ccform.cc_cvs_ind.value;
	}

	if (cccvcind == "0") {
		// we're not checking. return success.
		return(true);
	} else if (cccvcind != "1") {
		// we received a weird value. someone's playing silly buggers.
		alert("Invalid CVV indicator set. Please only submit transaction requests through official web forms.");
		return(false);
	}

	// ok, we're checking.
	var cvnNum;
	if (eval(document.ccform.cc_cvs)) {
		cvnNum = document.ccform.cc_cvs.value;
	} else {
		cvnNum = "";
	}

	// check that it contains only digits
	for (i=0; i < cvnNum.length; i++) {
		var c = cvnNum.charAt(i);
		if(numbers.indexOf(c) == -1) {
			alert("Please enter only digits for the CVN number\n(No spaces, dashes or letters)");
			return(false);
		}
	}

	var cctype = document.ccform.cc_type[document.ccform.cc_type.selectedIndex].value;
	if (cctype == "LASER") {
		// LASER cards don't have CVV
		cvnRequiredLength = 0;
	} else if (cctype == "AMEX") {
		// CVN should be 4 digits long
		cvnRequiredLength = 4;
	} else if (cctype == "SWITCH") {
		// CVN should be 3 digits long if present; 0 if not
		// not sure if all SWITCH cards have CVN details. All Maestro cards do, but not sure about older cards.
		if (cvnNum.length == 0) {
			cvnRequiredLength = 0;
		} else {
			cvnRequiredLength = 3;
		}
	} else {
		// CVN should be 3 digits long
		cvnRequiredLength = 3;
	}

	// correct length?
	if (cvnNum.length != cvnRequiredLength) {
		alert("CVN must be " + cvnRequiredLength + " digits long for this card type.");
		return(false);
	}

	// passed all checks. well done, lad.
	return(true);
}


function checkLUHN() {
	var ccnum = document.ccform.cc_num.value;
	ccnum = "" + ccnum;
	
	var i, sum, weight;
	sum=0;
	for (i = 0; i < ccnum.length - 1; i++) {
		weight = ccnum.substr(ccnum.length - (i + 2), 1) * (2 - (i % 2));
		sum += ((weight < 10) ? weight : (weight - 9));
	}

	if (parseInt(ccnum.substr(ccnum.length-1)) == ((10 - sum % 10) % 10)) {
		return(true);
	} else {
		alert("Card Number Fails Luhn Test\n(You did not enter a valid card number. Please check that you have entered it correctly)");
		return(false);
	}

	return(true);
}

function checkLUN() {
    return checkLUHN();
}




function opentrans(i, j) {
			obj = eval("document.all."+i+".style");
			obj2 = eval("document.all."+j+".style");
			if (obj.display == 'none') {
				obj.display = '';
			} else {
				obj.display = 'none';
			}
			if (obj2.display == 'none') {
				obj2.display = '';
			} else {
				obj2.display = 'none';
			}
}


function showCVN() {
      pageurl = "https://epage.payandshop.com/merchants/cvn.htm";
      window.open(pageurl, "CVN", "resizable=1,menubar=no,location=no,directories=no,scrollbars=no,status=no,height=350,width=400");
}


