/*
'  Source Code Copyright (C) 2009 Stephen D. Goff, I
'  Custom written for Sawmill Creek Resort
'  Source Code comes with ABSOLUTELY NO WARRANTY
'  Filename:  /_inc/CheckLodge.js
'  Date:      $Date: 04/15/2009 $
'  Author:    Stephen D. Goff
'  Called By: /lodge1.asp
'  Purpose:   Validation and proper formatting of form fields
*/
// Common Routine to set Upper Case on Sentences
function checkString(theField) {
  var aName = theField.value.split(/\s+/g); // split the sentence into an array of words
  if (aName != "")
  theField.value = SetName(aName); // join it back together
}

// Called by function checkString
function SetName(aName) {
  var patternName = /(\w)(.*)/; // a letter, and then one, none or more letters
  for (i = 0 ; i < aName.length ; i ++ ) {
    var partsName = aName[i].match(patternName); // just a temp variable to store the fragments in.
    var firstLetterName = partsName[1].toUpperCase();
    var restOfWordName = partsName[2].toLowerCase();
    aName[i] = firstLetterName + restOfWordName; // re-assign it back to the array and move on
  }
  return aName.join(' '); // join it back together
}

// Validates Email Address
function CheckEmail(form) {
  if (!verifyemail_check(document.TheForm.Email.value)) {
    alert("Email not valid !");
    document.TheForm.Email.focus();
    document.TheForm.Email.blur();
    document.TheForm.Email.select();
  }
}
function verifyemail_check(str) {
  if(!str.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) {
    return (false);
  } else {
    return (true);
  }
}


// Validate Credit Cards
function checkLuhn(input)
{
  var sum = 0;
  var numdigits = input.length;
  var parity = numdigits % 2;
  for(var i=0; i < numdigits; i++) {
    var digit = parseInt(input.charAt(i))
    if(i % 2 == parity) digit *= 2;
    if(digit > 9) digit -= 9;
    sum += digit;
  }
  return (sum % 10) == 0;
}

// final check after clicking submit button ------------------------------//
function CheckForm(frm) {
  if (document.TheForm.ZipCode.value.length <= 0) {
    alert ('This field is required.\nPlease enter a valid ZipCode.');
    document.TheForm.ZipCode.focus();
    document.TheForm.ZipCode.blur();
    document.TheForm.ZipCode.select();
    return (false);
  }
  if (document.TheForm.Phone.value.length <= 0) {
    alert ('This field is required.\nPlease enter a valid Phone Number.');
    document.TheForm.Phone.focus();
    document.TheForm.Phone.blur();
    document.TheForm.Phone.select();
    return (false);
  }
  if (document.TheForm.Email.value.length <= 0) {
    alert ('This field is required.\nPlease enter a valid Email Address.');
    document.TheForm.Email.focus();
    document.TheForm.Email.blur();
    document.TheForm.Email.select();
    return (false);
  }
  if (document.TheForm.CardType.selectedIndex == 0) {
    alert ('This field is required.\nPlease choose a valid Card Type.');
    document.TheForm.CardType.focus();
    document.TheForm.CardType.blur();
    document.TheForm.CardType.select();
    return (false);
  }
  if (document.TheForm.CardNumber.value.length <= 0) {
    alert ('This field is required.\nPlease enter a valid Card Number.');
    document.TheForm.CardNumber.focus();
    document.TheForm.CardNumber.blur();
    document.TheForm.CardNumber.select();
    return (false);
  }
  if (document.TheForm.ExpMon.selectedIndex == 0) {
    alert ('This field is required.\nPlease choose a valid Expiration Month.');
    document.TheForm.ExpMon.focus();
    document.TheForm.ExpMon.blur();
    document.TheForm.ExpMon.select();
    return (false);
  }
  if (document.TheForm.ExpYear.selectedIndex == 0) {
    alert ('This field is required.\nPlease choose a valid Expiration Year.');
    document.TheForm.ExpYear.focus();
    document.TheForm.ExpYear.blur();
    document.TheForm.ExpYear.select();
    return (false);
  }
  if (document.TheForm.CVV2.value.length <= 0) {
    alert ('This field is required.\nPlease enter a valid CVV2 Number.');
    document.TheForm.CVV2.focus();
    document.TheForm.CVV2.blur();
    document.TheForm.CVV2.select();
    return (false);
  }
  if (document.TheForm.CardName.value.length <= 0) {
    alert ('This field is required.\nPlease enter your name as it appears on your card.');
    document.TheForm.CardName.focus();
    document.TheForm.CardName.blur();
    document.TheForm.CardName.select();
    return (false);
  }
  return (true);
}
