// Javascript Validation functions for contact information
// Author: Benjamin Woodruff
// Date: 02/11/2006

//Global Variables
  var curDate = new Date();
  var curYear = curDate.getFullYear();
  var curMonth = curDate.getMonth() + 1;
  var entYear = "";
  var errFlag = true;
  
// Validate Form fields as specified   
  function valdtFormFlds(){  
    errFlag = valdtExpYear();
    if (errFlag) {
      errFlag = valdtExpMonth();
      if (errFlag) {
        createMsg(document.forms[0]);
      }
    }  
    if (errFlag == false) {
      return false;
    }
    else {
      return true;
    }
  }  
  
// Validate Credit Card Expiration Year
  function valdtExpYear() {
    entYear = document.forms[0].expyear.value
    if (entYear == "") {
      alert ( "The Credit Card Year is Null, please enter a year" );
      return false;
    }  
    else {
      if (entYear < curYear){
        alert ( "The Credit Card Year you have entered has expired" );
        document.forms[0].expyear.value = "";
        return false;
      }  
      else {
        return true;
      }  
    }
  }
  
// Validate Credit Card Expiration Month 
  function valdtExpMonth() {
    var entMonth = document.forms[0].expmonth.value;
    entYear = "2006";
    if (entMonth == "") {
      alert ( "The Credit Card Month is Null, please select a month" );
      return false;
    }  
    if (entMonth < curMonth) {
      alert ( "The Credit Card with month/date of " + entMonth + "/" + entYear + " is expired, please enter another card number with expiration date" );
      return false;    ;
    }
    else {
      return true;
    }  
  }

// Display User Entered fields in dialog box
// Build display string
  function createMsg(form) {
    var dispmsg = "";
    dispmsg = "You entered the following information";
    dispmsg += "\nName: "+ form.fname.value + " " + form.mi.value + ". "+ form.lname.value;
    dispmsg += "\nAddress: " + form.address.value;
    dispmsg += "\nCity, State, and Zip: " + form.city.value + "," + form.state.value + " " + form.zip.value;
    dispmsg += "\nPhone Number: (" + form.code.value + ") " + form.prefix.value + "-" + form.suffix.value;
    dispmsg += "\nDate of Birth: " + form.dobd.value + "/" + form.dobm.value + "/" + form.doby.value;

    // For credit card, use for loop to determine selection.
    for (var i = 0; i < form.cc.length; i++ ) {
      if (form.cc[i].checked) {
         var creditCard = form.cc[i].value;
         dispmsg += "\nCredit Card Type: " + creditCard;  
         break;
      }
    }
    dispmsg += "\nCredit Card Number: " + form.ccnum.value;
    dispmsg += "\nExpiration Month/Year: " + form.expmonth.value + "/" + form.expyear.value;
    //dispmsg += "\n
    alert ( dispmsg );
  }
// Conform the user wants to reset form on event handler.
  function confirmReset() {
    var resetForm = window.confirm("Are you sure you want to reset this form?"); 
    if (resetForm)
      return true;
    return false;
  }