/* ********************************************************************************
 * generic channel 6 validation code
 * ---------------------------------
 * some code may be (c) Macromedia.com, 1997-2002
 * some code may be (c) Yaromat.com, 1998-2002 
 * some code may be (c) ch-6.co.uk, 2002-2003
 * ---------------------------------
 * Usage: <FORM onSubmit="return RunChecks()" ACTION="target.php" METHOD="POST">
 * Notes: taken from ch-ching.com site - validation for "checkout.asp"
 * ---------------------------------
 * Last modified: 2004-09-14 [validateNumber() added]
 * ********************************************************************************/

var sErrorMessage = '';

// CHECK FUNCTIONS ********************************

// validate a Credit Card number:
// (a) must have 16 characters
// (b) all 16 characters must be digits
// (c) must pass mod 10 test
function validateCCNumber(oElement) {
	var field = '';
  var valid = "0123456789"
  var bOK = true;
  var temp;
	if (oElement.value) {
		field = oElement.value;
	}
	// length check:
	if (field.length !== 16) {
		bOK = false;
	}
	// numeric check:
	if (bOK) {
		for (var i=0; i<field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") bOK = false;
		}
	}
	// mod 10 check - only do if the CC num is OK so far:
	if (bOK) {
		bOK = validateMod10(oElement);
	} else {
		sErrorMessage += "Please only enter numbers (exactly 16) into the credit card field WITHOUT SPACES.\n\n";	
	}
	// return result:	
  if (!bOK) {
    return false;
  } else {
		return true;
	}
}

// mod 10 validation (eg. for Credit Card numbers):
function validateMod10(oElement) {
	var string = oElement.value; // original card number
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++) { temp += splitstring[i]; }
	var cc = temp;
	var cclength = cc.length; // length of card number
	var s2 = ""; // second string of digits
	var s3 = 0; // sum of all digits in s2 added together
	var mr = "1"; // multiplier - this switches between 1 and 2
	var q = ""; // holds current digit being examined
	var isvalid = false;  // whether card is valid

	for (x=cclength-1; x >= 0; x--) { 
		q = cc.substring(x, x+1);
		s2 = s2 + q * mr;
		mr = ((mr==1)? 2 : 1);
	}
	for (x=s2.length-1; x >=0; x--) { s3 = s3 + eval(s2.charAt(x)); }
	isvalid = (s3 % 10); //isvalid = ((isvalid==0)? true: false);
	//return isvalid;
	if (isvalid!=0) {
		sErrorMessage += "The card number you entered does not appear to be correct.\nPlease double check to make sure you entered the correct number.\n\n";
		return false;
	} else {
		return true;
	}
}

// validate an HTML <SELECT...> - ensure
// that the value is not empty:
function validateDropDown(oElement,sErrorText) {
	if (oElement && oElement.value) {
		if (oElement.value=="") {
			sErrorMessage += sErrorText + '\n\n';
			return false;
		} else {
			return true;
		}
	} else {
		sErrorMessage += sErrorText + '\n\n';
		return false;
	}
}

// ensure entered string not too short:
function validateTextMin(oElement,minlength,sErrorText) {
	if (oElement && oElement.value && oElement.value.length) {
		if (oElement.value.length < minlength) {
			sErrorMessage += sErrorText + '\n\n';
			return false;
		} else {
			return true;	
		}
	} else {
		sErrorMessage += sErrorText + '\n\n';
		return false;
	}
}

// ensure entered string not too long:
function validateTextMax(oElement,maxlength,sErrorText) {
	if (oElement && oElement.value && oElement.value.length) {
		if (oElement.value.length > maxlength) {
			sErrorMessage += sErrorText + '\n\n';
			return false;
		} else {
			return true;
		}	
	} else {
		sErrorMessage += sErrorText + '\n\n';
		return false;
	}
}

// ensure entered string _appears_ to be a valid email address:
// NB. this _can_not_ guarentee that an email address is valid;
// what is does instead is test whether the address _appears_ to
// be valid, ie. does the address:
// (a) contain 6 or more characters?
// (b) contain a '@' and a '.'?
// (c) contain a '.' _after_ the '@'?
function validateEmail(oElement, sErrorText) {
	var bResult = true;		// assume it's OK
	var sEmail = oElement.value;
	if (sEmail.length < 6 || sEmail.indexOf('@') == -1 || sEmail.indexOf('.') == -1) {
		bResult = false;
	} else {
		var iAt = sEmail.indexOf('@');				// look for '@'
		if (iAt != -1) {
			var iDot = sEmail.indexOf('.', iAt);	// look for '.' _after_ the '@'
			if (iDot == -1) {
				bResult = false;
			}
		} else {
			bResult = false;
		}
	}
	if (!bResult) sErrorMessage += sErrorText + '\n\n';
	return bResult;
}

// ensure entered string is a valid hex colour:
// ie. the string _only_ contains digits 0..9 and letters A..F.
function validateRGB(oElement, sErrorText) {
	var sOK_Chars = "0123456789ABCDEF";
	var bResult = true;	// assume it's OK
	var sRGB = oElement.value;
	if (sRGB.length != 6) {
		bResult = false;
	} else {
		sRGB = sRGB.toUpperCase();
		for (var i=0; i<sRGB.length; i++) {
			var sChar = sRGB.charAt(i);
			if (sOK_Chars.indexOf(sChar) == -1) {
				bResult = false;
			}
		}
	}
	if (!bResult) sErrorMessage += sErrorText + '\n\n';
	return bResult;
}

// ensure entered password matches entered confirmation field:
function validatePassword(oPassword,oConfirm,iMinLength,sErrorText) {
	var bResult = true;	// assume it's OK
	var sPassword = oPassword.value;
	var sConfirm = oConfirm.value;
	if (sPassword == '') bResult = false;
	else {
		if (sConfirm == '') bResult = false;
		else {
			if (sPassword != sConfirm) bResult = false;
			else {
				if (iMinLength > 0) {
					if (sPassword.length < iMinLength) bResult = false;
				}
			}
		}
	}
	if (!bResult) sErrorMessage += sErrorText + '\n\n';
	return bResult;
}

// ensure the entered value is numeric:
function validateNumber(oElement,sErrorText) {
	var bResult = true;	// assuem it's OK
	var fValue = parseFloat(oElement.value);
	if (isNaN(fValue)) {
		bResult = false;
	}
	if (!bResult) sErrorMessage += sErrorText + '\n\n';
	return bResult;
}

