///////////////////////////////////////////////////////////////////////////
//
// Form validation functions
// (c) 2002 Jiri Pilny
// How to use:
// http://www.programator.cz/skripty/checkform.htm
//
///////////////////////////////////////////////////////////////////////////

function warn(message, theField) {
    alert(message);
    if (warn.arguments.length > 1) {
		theField.focus();
		if (theField.type == "text" || theField.type == "textarea" || theField.type == "password")
			theField.select();
	}
    return false;
}

function warnIf(expression, message, theField) {
	if (expression) return warn(message, theField);
	return true;
}

function isEmpty(str) {
	return ((str == null) || (str.length == 0));
}

function checkEmpty(theField, message) {
	if (isEmpty(theField.value)) return warn(message, theField);
	return true;
}

function isWhitespace(str) {
	var whitespace = " \t\n\r";
	var i;
	for (i = 0; i < str.length; i++)
        if (whitespace.indexOf(str.charAt(i)) == -1) return false;
    return true;
}

function checkEmptyOrWhitespace(theField, message) {
	if (isEmpty(theField.value) || isWhitespace(theField.value)) return warn(message, theField);
	return true;
}

function isEmail(strEmail) {
	var i = 1;
    var sLength = strEmail.length;
    while ((i < sLength) && (strEmail.charAt(i) != "@")) i++;
    if ((i >= sLength) || (strEmail.charAt(i) != "@")) return false;
    i += 2;
    while ((i < sLength) && (strEmail.charAt(i) != ".")) i++;
    if ((i >= sLength - 1) || (strEmail.charAt(i) != ".")) return false;
    return true;
}

function checkEmail(theField, message, empty) {
	var email = theField.value;
	if (checkEmail.arguments.length < 3) empty = false;
	if (isEmpty(email) && empty) return true;
	if (!isEmail(email)) return warn(message, theField);
	return true;
}

function isDefault(theField) {
	if (theField.value==theField.defaultValue) return true;
	return false;
}

function checkDefault(theField, message) {
	if (isDefault(theField)) return warn(message, theField);
	return true;
}

function isEmptyOrDefault(theField) {
	return (isEmpty(theField.value) || isDefault(theField));
}

function checkEmptyOrDefault(theField, message) {
	if (isEmptyOrDefault(theField)) return warn(message, theField);
	return true;
}

function getRadioValue(radio) {
	for (var i = 0; i < radio.length; i++)
		if (radio[i].checked) return radio[i].value;
    return null;
}

function isEmptyRadio(radio) {
	if (getRadioValue(radio) == null) return true;
	return false;
}

function checkEmptyRadio(radio, message) {
	if (isEmptyRadio(radio)) return warn(message, radio[0]);
	return true;
}

function checkLength(theField, lengthMin, lengthMax, message) {
	var theLength = theField.value.length;
	if ((theLength >= lengthMin) && (theLength <= lengthMax)) return true;
	return warn(message, theField);
}

function stripCharsInBag(str, bag) {
    var i;
    var returnString = "";
    for (i = 0; i < str.length; i++) {   
        var c = str.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isNominalCreditCard(cc) {
	switch (cc.toUpperCase()) {
		case "VISA":
		case "MASTERCARD":
		case "AMEX":
		case "AMERICANEXPRESS":
		case "DISCOVER":
		case "JCB":
		case "DINERS":
		case "CARTEBLANCHE":
		case "ENROUTE":
			return true;
		default:
			return false;
	}
}

function checkCreditCard(strType, theField, message) {
	var CCN = stripCharsInBag(theField.value, " -");
	if (!isNominalCreditCard(strType))
		if (!isAnyCard(CCN)) return warn(message, theField);
    if (!isCardMatch(strType, CCN)) return warn(message, theField);
    return true;
}

function isCreditCard(st) {
  var sum = 0;
  var mul = 1;
  var l = st.length;
  if (st.length > 19) return false;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10) * mul;
    if (tproduct >= 10) sum += (tproduct % 10) + 1;
    else sum += tproduct;
    if (mul == 1) mul = 2;
    else mul = 1;
  }
  return ((sum % 10) == 0);
}

function isCardMatch (cardType, cardNumber) {
	cardType = cardType.toUpperCase();
	if ((cardType == "VISA") && (!isVisa(cardNumber))) return false;
	if ((cardType == "MASTERCARD") && (!isMasterCard(cardNumber))) return false;
	if (((cardType == "AMERICANEXPRESS") || (cardType == "AMEX")) && (!isAmericanExpress(cardNumber))) return false;
	if ((cardType == "DISCOVER") && (!isDiscover(cardNumber))) return false;
	if ((cardType == "JCB") && (!isJCB(cardNumber))) return false;
	if (((cardType == "DINERS") || (cardType == "CARTEBLANCHE")) && (!isDinersClub(cardNumber))) return false;
	if ((cardType == "ENROUTE") && (!isEnRoute(cardNumber))) return false;
	return true;
}

function isVisa(cc) {
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}

function isMasterCard(cc) {
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;
}

function isAmericanExpress(cc) {
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;
}

function isDinersClub(cc) {
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 14) && (firstdig == 3) &&
      ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
    return isCreditCard(cc);
  return false;
}

function isDiscover(cc) {
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
  return false;
}

function isEnRoute(cc) {
  first4digs = cc.substring(0,4);
  if ((cc.length == 15) &&
      ((first4digs == "2014") ||
       (first4digs == "2149")))
    return isCreditCard(cc);
  return false;
}

function isJCB(cc) {
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) &&
      ((first4digs == "3088") ||
       (first4digs == "3096") ||
       (first4digs == "3112") ||
       (first4digs == "3158") ||
       (first4digs == "3337") ||
       (first4digs == "3528")))
    return isCreditCard(cc);
  return false;
}

function isAnyCard(cc) {
  if (!isCreditCard(cc))
    return false;
  if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc) && !isDinersClub(cc) &&
      !isDiscover(cc) && !isEnRoute(cc) && !isJCB(cc)) {
    return false;
  }
  return true;
}
