/**
  <summary>This validates a form element.</summary>
  <param name="elem" type="element">The form element to validate.</param>
  <returns type="bool">true/false for success/failure</returns>
*/
function ValidateField( elem )
{

  //1.  If there is no Id, then there is nothing to validate.

  var lsId = elem.id;

  if ( lsId == "" )
    return true;

  //2.  Pick the Type and Required flags from the Id.

  var lsType = lsId.charAt(0) + lsId.charAt(1);
  var lsRequired = lsId.charAt(2)
   
  //3.  Verify that we have a value in a required field.

  if ( lsRequired.toLowerCase() == "y" ) 
  {
    if ( elem.value.length == 0 ) 
    {
      alert( "Required field missing." );
      elem.focus();
      return false;
    }
  }

  //4.  Verify the maximum length of a TextArea.

  if ( lsType.toLowerCase() == "ta" ) 
  {
    var lsLength = lsId.substring(4);

    if ( elem.value.length > lsLength ) 
    {
      alert( "The maximum size for this field is " + lsLength + "." );
      elem.focus();
      return false;
    }
  }

  //5.  Verify Integer field type.

  if ( lsType.toLowerCase() == "in" ) 
  {
    if ( ! isInteger( elem.value ) )
    {
      elem.focus();
      return false;
    }
  }

  //6.  Verfiy Date field type.

  if ( lsType.toLowerCase() == "da" ) 
  {
    if ( ! isDate( elem.value ) )
    {
      elem.focus();
      return false;
    }
  }

  //7.  We made it through all the checks, return true.

  return true;

}


var dtCh= "/";				///The Character that delimites the date.
var minYear=1900;				///The minimum valid year.
var maxYear=2100;				///The maximum valild year.


/**
  <summary>This validates the string "s" as being an integer.</summary>
  <param name="s" type="string">The string to validate.</param>
  <returns type="bool">true/false for success/failure</returns>
*/
function isInteger(s)
{
  var i;
  for (i = 0; i < s.length; i++){   
    var c = s.charAt(i);
    if ( (c < "0") || (c > "9") )
    {
      if ( i==0 && c == "-" && s.length > 1 )
        continue;

      alert( "Please enter an Integer." );
      return false;

    }
  }
  return true;
}


/**
  <summary>This validates a string as being a date.</summary>
  <param name="dtStr" type="string">The string to validate.</param>
  <returns type="bool">true/false for success/failure</returns>
*/
function isDate(dtStr)
{

  if ( dtStr.length == 0 ) 
    return true;

  if ( dtStr.length != 10 )
  {
    alert("The date format should be : mm/dd/yyyy")
    return false;
  }

  var daysInMonth = DaysArray(12)
  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

  if (strDay.charAt(0)=="0" && strDay.length>1) 
    strDay=strDay.substring(1);
  
  if (strMonth.charAt(0)=="0" && strMonth.length>1) 
    strMonth=strMonth.substring(1);

  for (var i = 1; i <= 3; i++) 
  {
    if (strYr.charAt(0)=="0" && strYr.length>1) 
      strYr=strYr.substring(1);
  }

  month=parseInt(strMonth);
  day=parseInt(strDay);
  year=parseInt(strYr);

  if (pos1==-1 || pos2==-1)
  {
    alert("The date format should be : mm/dd/yyyy");
    return false;
  }

  if (strMonth.length<1 || month<1 || month>12)
  {
    alert("Please enter a valid month");
    return false;
  }

  if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
  {
    alert("Please enter a valid day");
    return false;
  }

  if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
  {
    alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
    return false;
  }

  if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
  {
    alert("Please enter a valid date");
    return false;
  }

  return true;

}



/**
  <summary>This array holds the number of days in each month.</summary>
  <param name="n" type="integer">The number of months to load into the array (always 12).</param>
  <returns type="array">The array that contains the number of days in each month.</returns>
*/
function DaysArray(n) {

  for (var i = 1; i <= n; i++) 
  {
    this[i] = 31;

    if (i==4 || i==6 || i==9 || i==11) 
      this[i] = 30;

    if (i==2) 
      this[i] = 29;
  } 

  return this;

}


/**
  <summary>Calculates the number of days in February.</summary>
  <param name="year" type="integer">The year to find the number of days in February for.</param>
  <returns type="integer">The number of days in February for the year passed.</returns>
*/
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}


/**
  <summary>Strips the characters in the array "bag" from the string "s".</summary>
  <param name="s" type="string">The string to strip characters from.</param>
  <param name="bag" type="array">An array of the characters to strip.</param>
  <returns type="array">The string "s" with the characters in the array "bag" stripped.</returns>
*/
function stripCharsInBag(s, bag)
{
  var returnString = "";
  var i;
  for (i = 0; i < s.length; i++)
  {   
    var c = s.charAt(i);

    if (bag.indexOf(c) == -1) 
      returnString += c;
  }
  return returnString;
}
