/*******************************************************************************
*   Copyright, TernionCorporation.
*
********************************************************************************
*
*   File: formFunctions.js
*
*   Abstract: Routines for validating the form.
*
********************************************************************************

/* Get today's date */

var todaysDate = new Date ();

/* Set some variables for the element names in the form for easy access */

var formMonth = "#element_7_1";
var formDay = "#element_7_2";
var formYear = "#element_7_3";
var phone = "#element_3";
var errorMessage = "";
var errorFlag = false;
var focusElement = null;

var dtCh = "/";
var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;

/******************************************************************************/
function errorDetected (elementToSet)
/******************************************************************************/
   {
   /* Sets the element to focus on and set the error flag to true when an
    * error is detected while validating the form.
      
   History:
      25 Jun 08 - Created. KDSquier
      04 Jun 09 - Modified. BKPruitt
   */
   /* An error has not been detected yet? */

   if (!errorFlag)
      {
      focusElement = elementToSet;
      errorFlag = true;
      }
   } /* errorDetected */
   

/*
 * DHTML date validation script. Courtesy of SmartWebby.com 
 * (http://www.smartwebby.com/dhtml/)
 * Modified by KDSquier on June 26, 2008
 */
/******************************************************************************/
function isInteger (s)
/******************************************************************************/
   {
   /* Checks if the characters in a given string are all digits.
      
   History:
      25 Jun 08 - Created. KDSquier
   */
   /* Loop over all characters in the string. */

   for (var i = 0; i < s.length; i++)
      {   
      var c = s.charAt (i);
      
      /* This character is number? */
   
      if (((c < "0") || (c > "9"))) 
         return false;
      }

   return (true);

   } /* isInteger */

/******************************************************************************/
function stripCharsInBag (s, bag)
/******************************************************************************/
   {
   /* Removes all characters in a given string that are in the given bag.
      
   History:
      25 Jun 08 - Created. KDSquier
   */
   var returnString = "";
  
   /* Loop over all characters in the string. */

   for (var i = 0; i < s.length; i++)
      {   
      var c = s.charAt (i);

      /* This character in bag? */

      if (bag.indexOf (c) == -1) 
         returnString += c;
      }
   
   return (returnString);

   } /*stripCharsInBag */

/******************************************************************************/
function daysInFebruary (year)
/******************************************************************************/
   {
   /* Returns either 28 or 29 for the number of days in February given the year.
      
   History:
      25 Jun 08 - Created. KDSquier
   */
   
   return (((year % 4 == 0) && ( (!(year % 100 == 0)) 
      || (year % 400 == 0))) ? 29 : 28 );

   } /* daysInFebruary */

/******************************************************************************/
function daysArray() 
/******************************************************************************/
   {
   /* Creates an array of months with their maximum number of days.
      
   History:
      25 Jun 08 - Created. KDSquier
   */
   /* Loop over all 12 months */

	for (var i = 1; i <= 12; i++) 
      {

      /* This month January, March, May, July, August, October, or December? */

      if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
		   this[i] = 31;
		
      /* This month April, June, September, or November? */

      if (i == 4 || i == 6 || i == 9 || i == 11) 
         this[i] = 30;

      /* This month February? */

		if (i==2) 
         this[i] = 29;
      } 

   return (this);

   } /* daysArray */

/******************************************************************************/
function checkDate(dtStr)
/******************************************************************************/
   {
   /* Checks if the given date string is valid and in MM/DD/YYYY format.
      
   History:
      25 Jun 08 - Created. KDSquier
   */
	var daysInMonth = daysArray ();
	var pos1 = dtStr.indexOf (dtCh);
	var pos2 = dtStr.indexOf (dtCh, pos1 + 1);
	var strMonth = dtStr.substring (0, pos1);
	var strDay = dtStr.substring (pos1 + 1, pos2);
	var strYear = dtStr.substring (pos2 + 1);
	strYr = strYear;
   var temp = "";
  
   /* Parse the month, day, and year, but leading 0's need to be removed */
   /* Month has a '0' in the front? */

   if (strMonth.charAt (0) == "0" && strMonth.length > 1) 
      temp = strMonth.substring (1);
   else
      temp = strMonth;
   month = parseInt (temp);
	
   /* Day has a '0' in the front? */
   
   if (strDay.charAt (0) == "0" && strDay.length > 1) 
      temp = strDay.substring (1);
   else
      temp = strDay;
	day = parseInt (temp);
	
   year = parseInt (strYr);

   /* The date is not valid and/or not in MM/DD/YYYY format? */

	if ( 
      (strMonth.length != 2 || month < 1 || month > 12 || !isInteger (strMonth))
      ||
      (strDay.length != 2 || day < 1 || day > 31 
         || (month == 2 && day > daysInFebruary (year)) 
         || day > daysInMonth [month] || !isInteger (strDay))
      ||
      (strYear.length != 4 || !isInteger (strYear))
      ||
	   (dtStr.indexOf (dtCh,pos2 + 1) != -1 
         || isInteger (stripCharsInBag (dtStr, dtCh)) == false)
      )
      {
      errorDetected ($(formMonth));
		errorMessage = errorMessage + 
         " - Valid date in MM/DD/YYYY format\n";
      }

   /* The entered date is earlier than today's date? */
   
   if ( (strMonth != "" && strDay != "" && strYear != "" ) &&
         (new Date ()).setFullYear (strYear, strMonth - 1, strDay) 
         < todaysDate )
      {
      errorDetected ($(formMonth));
		errorMessage = errorMessage + 
         " - Date cannot be earlier than today's date.\n";
      }

   return (true);

   } /* checkDate */

/**
 * DHTML phone number validation script. 
 * Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 * Modified by KDSquier on June 26, 2008
 */

/******************************************************************************/
function validateForm() 
/******************************************************************************/
   {
   /* Validates the HTML form.
      
   History:
      25 Jun 08 - Created. KDSquier
   */
   errorMessage = "Please enter the required information " +
      "\nbelow to proceed:\n\n";
   errorFlag = false;
   focusElement = null;

   /* First Name field not been entered? */
   
   var currentElement = $("#element_1_1");
   if (currentElement.val () == "")
      {
      errorDetected (currentElement);
		errorMessage = errorMessage + " - First Name\n";
      }

   /* Last Name field not been entered? */

   currentElement = $("#element_1_2");
   if (currentElement.val () == "")
      {
      errorDetected (currentElement);
		errorMessage = errorMessage + " - Last Name\n";
      }

   /* Company field not been entered? */
   
   currentElement = $("#element_2");
   if (currentElement.val () == "")
      {
      errorDetected (currentElement);
		errorMessage = errorMessage + " - Company\n";
      }

    /* Email field not valid? */
   
   currentElement = $("#element_4");
   if (currentElement.val () == "" 
         || !/.+@.+\.[a-z]+/i.test (currentElement.val ()))
      {
      errorDetected (currentElement);
		errorMessage = errorMessage + " - Valid Email Address\n";
      }

     /* State field not selected? */

   currentElement = $("#element_5_4");
   if (currentElement.val () == "")
      {
      errorDetected (currentElement);
		errorMessage = errorMessage + " - State / Province / Region\n";
      }

   /* Country field not been entered? */
   
   currentElement = $("#element_5_6");
   if (currentElement.val () == "")
      {
      errorDetected (currentElement);
		errorMessage = errorMessage + " - Country\n";
      }

   if (errorFlag)
      {
      focusElement.focus();
      window.scrollBy(0, -50);
      alert (errorMessage);
      return (false);
      }

   else
      return (true);

   } /* validateForm */
