// A bit of JavaScript to validate the HTML form.
// Make sure the user has entered a 12 digit number,
// excluding spaces, tabs and dashes.
//
// Rene Churchill - rene@vtwebwizard.com - June 12th, 2003

function ValidateForm(form) {
    var id = form.customer_id.value;

    // Strip out any spaces or dashes
    var regexp1 = /[\s\-]/g;
    id = id.replace(regexp1,'');

    // Make sure the ID is 12 digits and no more
    var regexp2 = /^\d{12}$/;
    if (regexp2.test(id)) {
		// Blatent hack alert - hauler 42662 does not generate
		// correct checksum values, so skip them.
		if (id.substr(0,5) == 42662 ||
			id.substr(0,5) == 58590) {
			return true;
		}

        // Finally, do a checksum on the ID number
		// Different checksum algorithim used if custID starts with 4
		// or greater.
        // Working with numbers in JavaScript is a pain in the ass.
        var tmp = id.split('');
        var sum = new Number(0);
        var checkSum = tmp[11];
		if (tmp[0] <= 3) {
	        // digit #12 is mod10 of sum of digits 1 thru 11.
	        for (i = 0; i < 11; i++) {
	            var t = new Number(tmp[i]);
	            sum += t;
	        }
		} else {
			var j = new Number(1);
			for (i = 0; i < 11; i++) {
				var t = new Number(tmp[i]);
				sum += t * j;
				j++;
				if (j > 3) { j = 1; }
			}
		}

        // There is no mod() function for numbers in JavaScript
        // but mod10 is easy enough to do by grabing the 1 digit
        // of the sum.
        var sumString = sum.toString();
        tmp = sumString.split('');
        var modSum = tmp[tmp.length-1];

        if (checkSum == modSum) {
            return true;
        } else {
            alert("Error: Invalid Payment ID number.\n Please check for typos.");
            return false;
        }
    } else {
        alert('Error: The Payment ID number is not 12 digits');
        return false;
    }
}
