// ----------------------------------------------------------------------
// Javascript form validation routines.
// Author: Stephen Poley
//
// Simple routines to quickly pick up obvious typos.
// All validation routines return true if executed by an older browser:
// in this case validation must be left to the server.
//
// Update Jun 2005: discovered that reason IE wasn't setting focus was
// due to an IE timing bug. Added 0.1 sec delay to fix.
//
// Update Oct 2005: minor tidy-up: unused parameter removed
//
// Update Jun 2006: minor improvements to variable names and layout
// ----------------------------------------------------------------------

var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread

// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}


// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}


// --------------------------------------------
//                  msg
// Display warn/error message in HTML element.
// commonCheck routine must have previously been called
// --------------------------------------------

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;   // set the CSS class to adjust appearance of message
}

// --------------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// --------------------------------------------

var proceed = 2;  

function commonCheck    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node  

  if (emptyString.test(valfield.value)) {
    if (required) {
      msg (infofield, "error", "Required field.");  
      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}

// --------------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// --------------------------------------------

function validatePresent(valfield,   // element to be validated
                         infofield ) // id of element to receive info/error msg
{
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;

  msg (infofield, "warn", "");  
  return true;
}

// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateEmail  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    msg (infofield, "error", "Not a valid e-mail address");
    setfocus(valfield);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
  if (!email2.test(tfld)) 
    msg (infofield, "warn", "Unusual e-mail address - check if correct");
  else
    msg (infofield, "warn", "");
  return true;
}


// --------------------------------------------
//            validateTelnr
// Validate telephone number
// Returns true if so (and also if could not be executed because of old browser)
// Permits spaces, hyphens, brackets and leading +
// --------------------------------------------

function validateTelnr  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var telnr = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/  ;
  if (!telnr.test(tfld)) {
    msg (infofield, "error", "The phone number you entered appears to be invalid.");
    setfocus(valfield);
    return false;
  }
  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<7) {
    msg (infofield, "error", numdigits + " digits - too short.  Did you remember to enter your area code?");
    setfocus(valfield);
    return false;
  }
  if (numdigits>14) {
    msg (infofield, "warn", numdigits + " digits - check if correct");
  }
  else { 
    if (numdigits<10) {
      msg (infofield, "error", "Only " + numdigits + " digits. Did you remember to enter your area code?");
	} else {
      msg (infofield, "warn", "");
	}
  }
  return true;
}

// --------------------------------------------
//             validateAge
// Validate person's age
// Returns true if OK 
// --------------------------------------------

function validateAge    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);
  var ageRE = /^[0-9]{1,3}$/
  if (!ageRE.test(tfld)) {
    msg (infofield, "error", "Not a valid age");
    setfocus(valfield);
    return false;
  }

  if (tfld>=200) {
    msg (infofield, "error", "Not a valid age");
    setfocus(valfield);
    return false;
  }

  if (tfld>110) msg (infofield, "warn", "Older than 110: check correct");
  else {
    if (tfld<7) msg (infofield, "warn", "Bit young for this, aren't you?");
    else        msg (infofield, "warn", "");
  }
  return true;
}


// --------------------------------------------
//               validateInputtext
// Validate if text only
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateInputtext  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required


{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var Inputtext = /^[a-zA-Z -]+$/  ;
  if (!Inputtext.test(tfld)) {
    msg (infofield, "error", "Letters only please.")
    setfocus(valfield);
    return false;
  } else {
		 msg (infofield, "good", " ");
		 return true;
	}
	
}
// --------------------------------------------
//               validateNumber
// Validate if text only
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateNumber  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required


{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var Inputtext = /[0-9 -]+$/  ;
  if (!Inputtext.test(tfld)) {
    msg (infofield, "error", "Numbers only please.")
    setfocus(valfield);
    return false;
  } else {
		 msg (infofield, "good", " ");
		 return true;
	}
	
}
// --------------------------------------------
//               validateUsername
// Validate if text only
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateUsername  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required


{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var Inputtext = /^[a-z0-9_-]+$/  ;
  if (!Inputtext.test(tfld)) {
    msg (infofield, "error", "Username should be lowercase letters and numbers only.")
    setfocus(valfield);
    return false;
  } else {
		 msg (infofield, "good", " ");
		 return true;
	}
	
}
// --------------------------------------------
//               validatePassword
// --------------------------------------------
/*
function validatePassword  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required


{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var passwordText = /^(?=.*\d).{5,12}$/;
  if (!passwordText.test(tfld)) {
    msg (infofield, "error", "Passwords should be between 5-12 characters, be letters and numbers only, and must contain at least one numeric character.")
    setfocus(valfield);
    return false;
  } else {
         
         return true;
    }
}
*/

function validatePassword  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required


{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var passwordText = /^\w*\d+\w*$/;
  var passwordMinLen = 5;
  var passwordMaxLen = 12;
  if (!passwordText.test(tfld)) {
    msg (infofield, "error", "Password should be letters and numbers only, and must contain at least one numeric character.")
    setfocus(valfield);
    return false;
  } else if(tfld.length < passwordMinLen || tfld.length > passwordMaxLen) {
	msg (infofield, "error", "Password should be 5-12 characters.")
    setfocus(valfield);
    return false;
  } else {
         msg (infofield, "good", " ");
         return true;
    }
}


// --------------------------------------------
//               validatePIN
// --------------------------------------------

function validatePin  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required


{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var pin = /^[+]?\d*$/;
  if (!pin.test(tfld)) {
    msg (infofield, "error", "PIN should be numbers only.")
    setfocus(valfield);
    return false;
  } else if (tfld.length != 4) {
      msg (infofield, "error", "PIN should be 4 digits.")
      setfocus(valfield);
      return false;
  } else {
         msg (infofield, "good", " ");
         return true;
    }
}


// --------------------------------------------
//               validateMatchingFields
// --------------------------------------------

function validateMatch  (valfield1,   // first element to be validated
						 valfield2, // second element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required

{
  var password1 = valfield1.value;
  var password2 = valfield2.value;
	  
  if (password1 != password2) {
    msg (infofield, "error", "Passwords do not match.  Please re-enter.")
    setfocus(valfield1);
    return false;
  } else {
         msg (infofield, "good", " ");
         return true;
    }
}

// -----------------------------------------
//            commonCheck2
// Common code for checkbox validation routines to
// check for older / less-equipped browsers
// Returns true (validation passed) or
//         proceed (don't know yet)
// -----------------------------------------

var proceed = 2;  

function commonCheck2   (vfld,   // element to be validated
                         ifld)   // id of element to receive info/error msg
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(ifld);
  if (!elem.firstChild)
    return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text)
    return true;  // ifld is wrong type of node  

  msg (ifld, "warn", "");  // clear any previous error message
  return proceed;
}



// -----------------------------------------
//            validateConfirm 
// Usually one doesn't want to validate if 1 checkbox of a set has been
// checked, because in this case one would use radio buttons instead.
// But sometimes one wants a reader to check a single box to confirm that 
// he or she agrees to something. That is covered by this routine.
//
// Returns true if valid (and also if could not be executed because 
// of old browser)
// -----------------------------------------

function validateConfirm   (vfld,   // checkbox to be validated
                            ifld)   // id of element to receive info/error msg
{
  var stat = commonCheck2(vfld, ifld);
  if (stat != proceed) return stat;

  if (vfld.checked) return true;

  // if we get here then the validation has failed

  var errorMsg = 'Please read the above message and confirm you agree to it by checking the box.';

  msg (ifld, "error", errorMsg);
  return false;
}

//---------------------------
//
//        validateCreditCard
//
//---------------------------
function validateCreditCard(cardNumberField, cardTypeField, infofield)
{
  cardNumber = cardNumberField.value;
  cardType = cardTypeField[cardTypeField.selectedIndex].value;

  var isValid = false;
  var ccCheckRegExp = /[^\d ]/;  
  isValid = !ccCheckRegExp.test(cardNumber);

  if (isValid)
  {
    var cardNumbersOnly = cardNumber.replace(/ /g,"");
    var cardNumberLength = cardNumbersOnly.length;
    var lengthIsValid = false;
    var prefixIsValid = false;
    var prefixRegExp;

    switch(cardType)
    {
      case "Mastercard":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^5[1-5]/;
        break;

      case "Visa":
        lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
        prefixRegExp = /^4/;
        break;

      case "AMEX":
        lengthIsValid = (cardNumberLength == 15);
        prefixRegExp = /^3(4|7)/;
        break;

      case "Discover":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^6011/;
        break;

      default:
        prefixRegExp = /^$/;
        alert("Please select a card type.");
    }

    prefixIsValid = prefixRegExp.test(cardNumbersOnly);
    isValid = prefixIsValid && lengthIsValid;
  } else {
      msg (infofield, "error", "Not all numbers");      
  }

  if (isValid)
  {
    var numberProduct;
    var numberProductDigitIndex;
    var checkSumTotal = 0;

    for (digitCounter = cardNumberLength - 1; 
      digitCounter >= 0; 
      digitCounter--)
    {
      checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
      digitCounter--;
      numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
      for (var productDigitCounter = 0;
        productDigitCounter < numberProduct.length; 
        productDigitCounter++)
      {
        checkSumTotal += 
          parseInt(numberProduct.charAt(productDigitCounter));
      }
    }

    isValid = (checkSumTotal % 10 == 0);
    
  }

  if (!isValid) {
    msg(infofield, "error", "Invalid credit card number.  Be sure that you entered the number correctly without dashes or spaces and the card type you selected is correct.");
  } else {
    msg (infofield, "good", " ");   
  }

  return isValid;
}

//-------------------------------
//     validateCardSecurity
//-------------------------------
function validateCardSecurity  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required


{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var securityNumber = /^[0-9]+$/  ;
  if (!securityNumber.test(tfld)) {
    msg (infofield, "error", "Invalid security code. Should be numbers only.")
    setfocus(valfield);
    return false;
  } else if (tfld.length < 3 || tfld.length > 4) {
      msg (infofield, "error", "Security code should be 3 or 4 digits.")
      setfocus(valfield);
      return false;
  } else {
         msg (infofield, "good", " ");
         return true;
    }
}

//-------------------------------
//     validateCardExpiration
//-------------------------------

function validateCardExpiration  (valfield1,   // month
                        valfield2, // year
                         infofield,  // id of element to receive info/error msg 
                         required)   // true if required

{
    var cardMonth = parseInt(valfield1.value);
    var cardYear = parseInt( valfield2.value);
    var right_now = new Date();
    var right_now_month = right_now.getMonth()+1;
    var right_now_year = right_now.getFullYear();
    
    if (cardYear < right_now_year || (cardYear == right_now_year && cardMonth < right_now_month))
        {
        msg (infofield, "error", "Your card has expired.  Please enter a valid card.") 
        setfocus(valfield1);
        return false;
    } else {
        msg (infofield, "good", " ");
        return true;
    }
}

//-------------------------------
//     validateSelection
//-------------------------------

function validateSelection(valfield,   // element to be validated
                         infofield ) // id of element to receive info/error msg
{
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;

  msg (infofield, "warn", "");  
  return true;
}
