/*
 * Filename: ajax_validate.js
 * Purpose: Container for all of the functions to validate forms and form values
 * Change log:
 *  02.11.2010 - Created - David Verro, Tim Burrows
 */    
 
/********************Global Variables************************/

//stores http data for the javascript to read and react to
var xmlhttp

//border style for errors on text fields
var strErrorBorder = "2px #00f inset";

//border style for fields without errors
var strRegBorder = "2px #ddd inset";
 
/********************    Functions   ************************/


/*
 * Function Name: SubmitFormValidate
 * Purpose: Checks all of the form values and notifies the user of any missing
 *          or incorrect entries prior to final submittal  
 * Change log:
 *  02.11.2010 - Created - David Verro, Tim Burrows
 */ 
function SubmitFormValidate()
{
  try
  {
    //get the html data
    xmlhttp=GetXmlHttpObject();
    
    //check for browser compatibility
    if (xmlhttp==null)
    {
      alert ("Your browser is incompatible with this online form."); 
      return;
    }
                                                          
    //store the number of errors found in the form
    var intErrorCount = 0;
    var lastErrorId = "";
    
    //array to store variable names, & clean values
    var FormArray = new Array(document.forms[0].elements.length);
    
    var intElementCount;
    for(intElementCount = 0; intElementCount<document.forms[0].elements.length; intElementCount++)
    {
      if(document.forms[0].elements[intElementCount].type == 'text')
      {
        FormArray[intElementCount] = new Array(12);
        FormArray[intElementCount]['id'] = document.forms[0].elements[intElementCount].id;  
        FormArray[intElementCount]['type'] = document.forms[0].elements[intElementCount].type;  
        FormArray[intElementCount]['value'] = document.forms[0].elements[intElementCount].value;  
        FormArray[intElementCount]['name'] = document.forms[0].elements[intElementCount].name;
        FormArray[intElementCount]['title'] = document.forms[0].elements[intElementCount].title; 
        FormArray[intElementCount]['alt'] = document.forms[0].elements[intElementCount].alt;
        FormArray[intElementCount]['required'] = true;   
        FormArray[intElementCount]['datatype'] = "";   
        FormArray[intElementCount]['max'] = document.forms[0].elements[intElementCount].maxlength; 
        FormArray[intElementCount]['min'] = "";        
        FormArray[intElementCount]['default'] = "";      
        FormArray[intElementCount]['order'] = 0; 
      }
      else if(document.forms[0].elements[intElementCount].type == 'radio' || document.forms[0].elements[intElementCount].type == 'checkbox') 
      {
        FormArray[intElementCount] = new Array(12);
        FormArray[intElementCount]['id'] = document.forms[0].elements[intElementCount].id;  
        FormArray[intElementCount]['type'] = document.forms[0].elements[intElementCount].type;
        FormArray[intElementCount]['value'] = document.forms[0].elements[intElementCount].checked;  
        FormArray[intElementCount]['name'] = document.forms[0].elements[intElementCount].name;
        FormArray[intElementCount]['title'] = document.forms[0].elements[intElementCount].title; 
        FormArray[intElementCount]['alt'] = document.forms[0].elements[intElementCount].alt;
        FormArray[intElementCount]['required'] = true;   
        FormArray[intElementCount]['datatype'] = "";   
        FormArray[intElementCount]['max'] = document.forms[0].elements[intElementCount].maxlength; 
        FormArray[intElementCount]['min'] = "";        
        FormArray[intElementCount]['default'] = "";      
        FormArray[intElementCount]['order'] = 0;
      }  
      else if(document.forms[0].elements[intElementCount].type == 'select-one') 
      {
        FormArray[intElementCount] = new Array(12);
        FormArray[intElementCount]['id'] = document.forms[0].elements[intElementCount].id;  
        FormArray[intElementCount]['type'] = document.forms[0].elements[intElementCount].type;  
        FormArray[intElementCount]['value'] = document.forms[0].elements[intElementCount].value;  
        FormArray[intElementCount]['name'] = document.forms[0].elements[intElementCount].name;
        FormArray[intElementCount]['title'] = document.forms[0].elements[intElementCount].title; 
        FormArray[intElementCount]['alt'] = document.forms[0].elements[intElementCount].alt;
        FormArray[intElementCount]['required'] = true;   
        FormArray[intElementCount]['datatype'] = "";   
        FormArray[intElementCount]['max'] = document.forms[0].elements[intElementCount].maxlength; 
        FormArray[intElementCount]['min'] = "";        
        FormArray[intElementCount]['default'] = "";      
        FormArray[intElementCount]['order'] = 0; 
      }         
      else if(document.forms[0].elements[intElementCount].type == 'hidden')
      {         
        FormArray[intElementCount] = new Array(12);
        FormArray[intElementCount]['id'] = document.forms[0].elements[intElementCount].id;  
        FormArray[intElementCount]['type'] = document.forms[0].elements[intElementCount].type;  
        FormArray[intElementCount]['value'] = document.forms[0].elements[intElementCount].value;  
        FormArray[intElementCount]['name'] = document.forms[0].elements[intElementCount].name;
        FormArray[intElementCount]['title'] = document.forms[0].elements[intElementCount].title; 
        FormArray[intElementCount]['alt'] = document.forms[0].elements[intElementCount].alt;
        FormArray[intElementCount]['required'] = true;   
        FormArray[intElementCount]['datatype'] = "";   
        FormArray[intElementCount]['max'] = document.forms[0].elements[intElementCount].maxlength; 
        FormArray[intElementCount]['min'] = "";        
        FormArray[intElementCount]['default'] = "";      
        FormArray[intElementCount]['order'] = 0; 
        //alert(document.forms[0].elements[intElementCount].id + " Value: " + 
          //document.forms[0].elements[intElementCount].value);      
      }
      else
      {
        //alert(document.forms[0].elements[intElementCount].id + " Type: " + 
          //document.forms[0].elements[intElementCount].type);      
      }
    }   
    //alert(intElementCount);
    
    
    for(j = 0; j<FormArray.length; j++)
    {
      //check to see if there is data in this array index
      if(FormArray[j])
      {
        //check to see if the title has information, if not leave defaults
        if(FormArray[j]['title'] && FormArray[j]['id'] != 'recaptcha_challenge_field')
        {
          //alert("Title split for " + FormArray[j]['id'] + " Type: " + FormArray[j]['type'])
          FormArray[j]['title'] = FormArray[j]['title'].split('&');
          if(FormArray[j]['title'][0])
            FormArray[j]['required'] =  FormArray[j]['title'][0];
          if(FormArray[j]['title'][1])
            FormArray[j]['datatype'] =  FormArray[j]['title'][1];
          if(FormArray[j]['title'][2])
            FormArray[j]['min'] =  FormArray[j]['title'][2];
          if(FormArray[j]['title'][3])
            FormArray[j]['max'] =  FormArray[j]['title'][3]; 
          if(FormArray[j]['title'][4])
            FormArray[j]['default'] =  FormArray[j]['title'][4];    
          if(FormArray[j]['title'][5])
            FormArray[j]['order'] = FormArray[j]['title'][5];; 
        }
        else
        {
          //alert("Title not split for " + FormArray[j]['id'] + " Type: " + FormArray[j]['type'])
        }
        var count = j;
      }
    }
    //alert(count);
    
    /* 
    //checking data in array  
    var strElementData;    
    for(k = 4; k<5; k++)
    {   
      //check to see if there is data in this array index
      if(FormArray[k])
      {
        strElementData = strElementData + "ID: " + FormArray[k]['id'] + ", ";
        strElementData = strElementData + "Value: " + FormArray[k]['value'] + ", ";  
        strElementData = strElementData + "Name: " + FormArray[k]['name'] + ", ";  
        strElementData = strElementData + "Title: " + FormArray[k]['title'] + ", ";  
        strElementData = strElementData + "Alt: " + FormArray[k]['alt'] + ", "; 
        strElementData = strElementData + "Required: " + FormArray[k]['required'] + ", ";
        strElementData = strElementData + "Number: " + FormArray[k]['number'] + ", ";
        strElementData = strElementData + "Date: " + FormArray[k]['date'] + ", ";
        strElementData = strElementData + "MinLength: " + FormArray[k]['minlength'] + ", ";
        strElementData = strElementData + "MaxLength: " + FormArray[k]['maxlength'] + "\n";
      }
    }        
    alert(strElementData);         */
    
    var counter = 0;
    for(row = 35; row>=0; row--)
    {     
      var intFoundError = intErrorCount;
      if(FormArray[row])
      {
        switch(FormArray[row].type)
        {
          case ("text"):
            //alert(FormArray[row]['id'] + " Req: " + FormArray[row]['required']);
            if(FormArray[row]['required'] == 'true' || FormArray[row]['value'] != "")
            {
              //alert(FormArray[row]['value']);
              counter++;
              if(FormArray[row]['datatype'])
              {
                //alert(FormArray[row]['id'] + " datatype: " + FormArray[row]['datatype']);
                switch(FormArray[row]['datatype'])
                {
                  case ('date'):
                    intErrorCount += validateDate(FormArray[row]['id'], 
                        FormArray[row]['alt'], FormArray[row]['value'], 
                        FormArray[row]['min'], FormArray[row]['max'],
                        FormArray[row]['default']);
                    break;
                  case ('numeric'):
                    intErrorCount += validateNumber(FormArray[row]['id'], 
                        FormArray[row]['alt'], FormArray[row]['value'], 
                        FormArray[row]['min'], FormArray[row]['max'],
                        FormArray[row]['default']);
                    break;
                  case ('alphanumeric'):
                    intErrorCount += validateString(FormArray[row]['id'], 
                      FormArray[row]['alt'], FormArray[row]['value'], 
                      FormArray[row]['min'], FormArray[row]['max'],
                      FormArray[row]['default']);
                    break; 
                  case ('alpha'):
                    intErrorCount += validateAlphaString(FormArray[row]['id'], 
                      FormArray[row]['alt'], FormArray[row]['value'], 
                      FormArray[row]['min'], FormArray[row]['max'],
                      FormArray[row]['default']);
                    break; 
                  case ('phone'):
                    intErrorCount += validateNumber(FormArray[row]['id'], 
                      FormArray[row]['alt'], FormArray[row]['value'], 
                      FormArray[row]['min'], FormArray[row]['max'],
                      FormArray[row]['default']);
                    break; 
                  case ('float'):
                    intErrorCount += validateFloat(FormArray[row]['id'], 
                      FormArray[row]['alt'], FormArray[row]['value'], 
                      FormArray[row]['min'], FormArray[row]['max'],
                      FormArray[row]['default']);
                    break;
                  default:
                    break;
                }
              }
            }
            else
            {      
              //remove the error message
              document.getElementById("err_" + FormArray[row]['id']).innerHTML = "";
              //set the control back to normal styles
              document.getElementById(FormArray[row]['id']).style.border = strRegBorder;
            }
            break;
            
          default: 
            break;
        };
      } 
      if(parseInt(intErrorCount) > parseInt(intFoundError))
      {
        lastErrorId = FormArray[row]['id'];  
      }
    }
    //alert(lastErrorId);
    //alert(counter);
    
    /***** Custom Requirements ****/ 
    
    //check to see if the agreement was understood
    if(!document.getElementById("understand").checked || 
        document.getElementById("understand").checked != true)
    {
      intErrorCount++;
      document.getElementById("err_understand").innerHTML = 
        "* You must read and agree to the statement.";
      document.getElementById("understand").style.border = strErrorBorder;
      //document.getElementById("understand").focus();
    } 
    
    var strPoNumber = document.getElementById("ponumber").value;
    
    //check to make sure the value is not the default
    if(strPoNumber == "" && document.getElementById("po").checked)
    {  
      intErrorCount++;
      document.getElementById("err_ponumber").innerHTML = 
        "* you must enter a PO number if you are paying by company PO";
    }
    else if(strPoNumber != "" && !document.getElementById("po").checked)
    {  
      intErrorCount++;
      document.getElementById("err_ponumber").innerHTML = 
        "* PO numbers are only required for those paying by company PO";
    }
    else
    {
      document.getElementById("err_ponumber").innerHTML = "";
      FormArray[0] = document.getElementById("ponumber").value;
    }   
    
    //Check CAPTCHA
    //alert("Ajax Verify: " + verifyRecaptcha());
    
    
    /***** End Custom Requirements ****/       
         
    if(intErrorCount == 0)
    {
      //clear error messages
      document.getElementById("topnumberoferrors").innerHTML = "";
      document.getElementById("bottomnumberoferrors").innerHTML = ""; 
    
      // Set up URL to send via the POST method
      var url= "regformprocess.php";
      
      // Build the parameter string
      var parameters = "";
      
      //sort the array by the order given in the html tags
      FormArray.sort(sortByOrder);
      
      //var order = "";
      //create the post array
      for(i=0; i<FormArray.length; i++)
      {
        /*testing
        if(FormArray[i]['order'])
          order += FormArray[i]['order'] + ' - ' + FormArray[i]['id'] + '\n';
        */  
        if(FormArray[i])
          parameters += FormArray[i]['id'] + "=" + FormArray[i]['value'] + 
                        "," + FormArray[i]['alt'] + "," + FormArray[i]['type'] + 
                        "," + FormArray[i]['name'] + "," + FormArray[i]['required'] + 
                        "," + FormArray[i]['datatype'] + "," + FormArray[i]['max'] +
                        "," + FormArray[i]['min'] + "," + FormArray[i]['default'] +
                        "," + FormArray[i]['order'] + "&";
      }

      //alert(order);    
      //alert(parameters); 
      
      
      // Set up the xmlHTTP request object    
      xmlhttp.open("POST", url, true);
      xmlhttp.onreadystatechange=stateChanged;
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.send(parameters);
    }
    else
    {
      var strErrorMessage = "";
      if(intErrorCount > 1)
        strErrorMessage = "* There are " + intErrorCount + " errors on the form.  \
          Correct the highlighted areas then press submit.";
      else
        strErrorMessage = "* There is " + intErrorCount + " error on the form.  \
          Correct the highlighted areas then press submit.";
      
      //tell the user how many errors are on the form
      document.getElementById("topnumberoferrors").innerHTML = strErrorMessage;
      document.getElementById("bottomnumberoferrors").innerHTML = "<br />" + strErrorMessage; 
    
      document.getElementById("understand").checked = false;
      if(lastErrorId != "")
      {
        //alert(lastErrorId);
        document.getElementById(lastErrorId).focus(); 
      }
    }
  }
  catch(err)
  {
    var txt="There was an error in fucntion: SubmitFormValidate.\n";
    txt+="Error description: " + err.description + "\n";  
    txt+="Error Message: " + err.message + "\n";
    txt+="Click OK to continue.\n"; 
    DisplayError(txt); 
  }
}

function sortByOrder(a, b)
{
   if(parseInt(a['order'])<parseInt(b['order']))
    return -1;
   else if(parseInt(a['order'])>parseInt(b['order']))
    return 1;
   else
    return 0;      
}

/*
 * Function Name: validateString
 * Purpose: Checks a string value to make sure it exists and is valid
 * parameters:
 *    aId - Id to retrieve the string to be validated
 *    aName - the name of the value to be displayed to users
 *    aValue (optional - gets value) - the value of the string to be validated 
 *    aMinLength (optional - 1) - the minimum length of the string to be validated
 *    aMaxLength (optional - 5000) - the maximum length of the string to be validated  
 * Returns:
 *    Number of errors found - int
 *      0 - valid value
 *      1 - at least one error found     
 * Change log:
 *  02.11.2010 - Created - David Verro
 */
function validateString(aId, aName, aValue, aMinLength, aMaxLength, aDefault)
{
  try
  { 
    //construct the id of the error span associated  
    var errId = "err_" + aId;
    
    //check to make sure an id was passed in
    if(aId == null)
    {
      //if there are any problems with the id throw an exception
      throw "ID is null.  Aborting validation."; 
    }
    else if(aId == undefined)
    {
      //if there are any problems with the id throw an exception
      throw "ID is undefined.  Aborting validation.";
    }
    else if(aId == undefined)
    {
      //if there are any problems with the id throw an exception
      throw "ID is blank.  Aborting validation.";
    }
    
    //check to make sure a name was passed in
    if(aName == null)
    {
      //if not use the id - not ideal
      aName = aId;
    }
    else if(aName == undefined)
    {               
      //if not use the id - not ideal
      aName = aId;
    }
    else if(aName == "")
    {             
      //if not use the id - not ideal
      aName = aId;
    }
    
    //check to make sure a value was passed in
    if(aValue == null)
    {
      //if not go get the value based on the id
      var aValue = document.getElementById(aId).value;
    }
    else if(aValue == undefined)
    {    
      //if not go get the value based on the id
      var aValue = document.getElementById(aId).value;
    }
    
    //check to see if there is a default value to check against    
    if(aDefault == null)
    {
      aDefault = "";
    }
    else if(aDefault == undefined)
    {
      aDefault = "";
    }
    
    //if there is a default value and it matches the value from the form
    if(aDefault != "" && aValue == aDefault)
    {  
      //blank the value to cause an error
      aValue = "";
    }
    
    //check for a min value
    if(aMinLength == null)
    {
      //set default
      var aMinValue = 0;
    }
    
    //check for a max value
    if(aMaxLength == null)
    {
      //set default
      var aMaxValue = 5000;
    }
    
    //check to see if it is an empty string
    if(aValue == "")
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " is required";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    } 
    else if(aValue.length < aMinLength)
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " does not meet the minimum length required (" + aMinLength + ")";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    else if(aValue.length > aMaxLength)
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " exceeds the maximum length (" + aMaxLength + ")";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    else
    {
      //remove the error message
      document.getElementById(errId).innerHTML = "";
      //set the control back to normal styles
      document.getElementById(aId).style.border = strRegBorder;
      return 0;
    }
  }
  catch(err)
  {
    var txt="There was an error in function: validateString, while checking " + aId + "\n";
    txt+="Error description: " + err.description + "\n";
    txt+="Error Message: " + err.message + "\n";
    txt+="Click OK to continue.\n"; 
    DisplayError(txt); 
  }
}

/*
 * Function Name: validateAlphaString
 * Purpose: Checks a string value to make sure it exists and is valid (a-zA-Z, )
 *    -no numbers or special characters
 *    -used mostly for names  
 * parameters:
 *    aId - Id to retrieve the string to be validated
 *    aName - the name of the value to be displayed to users
 *    aValue (optional - gets value) - the value of the string to be validated 
 *    aMinLength (optional - 1) - the minimum length of the string to be validated
 *    aMaxLength (optional - 5000) - the maximum length of the string to be validated  
 * Returns:
 *    Number of errors found - int
 *      0 - valid value
 *      1 - at least one error found     
 * Change log:
 *  02.11.2010 - Created - David Verro
 */
function validateAlphaString(aId, aName, aValue, aMinLength, aMaxLength, aDefault)
{
  try
  { 
    //construct the id of the error span associated  
    var errId = "err_" + aId;
    
    //check to make sure an id was passed in
    if(aId == null)
    {
      //if there are any problems with the id throw an exception
      throw "ID is null.  Aborting validation."; 
    }
    else if(aId == undefined)
    {
      //if there are any problems with the id throw an exception
      throw "ID is undefined.  Aborting validation.";
    }
    else if(aId == undefined)
    {
      //if there are any problems with the id throw an exception
      throw "ID is blank.  Aborting validation.";
    }
    
    //check to make sure a name was passed in
    if(aName == null)
    {
      //if not use the id - not ideal
      aName = aId;
    }
    else if(aName == undefined)
    {               
      //if not use the id - not ideal
      aName = aId;
    }
    else if(aName == "")
    {             
      //if not use the id - not ideal
      aName = aId;
    }
    
    //check to make sure a value was passed in
    if(aValue == null)
    {
      //if not go get the value based on the id
      var aValue = document.getElementById(aId).value;
    }
    else if(aValue == undefined)
    {    
      //if not go get the value based on the id
      var aValue = document.getElementById(aId).value;
    }
    
    //check to see if there is a default value to check against    
    if(aDefault == null)
    {
      aDefault = "";
    }
    else if(aDefault == undefined)
    {
      aDefault = "";
    }
    
    //if there is a default value and it matches the value from the form
    if(aDefault != "" && aValue == aDefault)
    {  
      //blank the value to cause an error
      aValue = "";
    }
    
    //check for a min value
    if(aMinLength == null)
    {
      //set default
      var aMinValue = 0;
    }
    
    //check for a max value
    if(aMaxLength == null)
    {
      //set default
      var aMaxValue = 5000;
    }
    
    //alert(aValue.length);
    var alpha = RegExp("[a-zA-Z, ]{" + aValue.length + "}");//RegExp("[a-z]");
    //alert(alpha);
    var bool = alpha.test(aValue);
    //alert("Reg Exp: " + bool);
    
    //check to see if it is an empty string
    if(aValue == "")
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " is required";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    else if(!alpha.test(aValue))
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " can only include (a-z ,)";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    else if(aValue.length < aMinLength)
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " does not meet the minimum length required (" + aMinLength + ")";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    else if(aValue.length > aMaxLength)
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " exceeds the maximum length (" + aMaxLength + ")";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    else
    {
      //remove the error message
      document.getElementById(errId).innerHTML = "";
      //set the control back to normal styles
      document.getElementById(aId).style.border = strRegBorder;
      return 0;
    }
  }
  catch(err)
  {
    var txt="There was an error in function: validateAlphaString, while checking " + aId + "\n";
    txt+="Error description: " + err.description + "\n";
    txt+="Error Message: " + err.message + "\n";
    txt+="Click OK to continue.\n"; 
    DisplayError(txt); 
  }
}

/*
 * Function Name: validateDate
 * Purpose: Checks a date value to make sure it exists and is valid
 * parameters:
 *    aId - Id to retrieve the string to be validated
 *    aName - the name of the value to be displayed to users
 *    aValue (optional - gets value) - the value of the string to be validated 
 *    aMin (optional - 1900) - the minimum year value to be validated
 *    aMax (optional - 5000) - the maximum year value to be validated  
 * Returns:
 *    Number of errors found - int
 *      0 - valid value
 *      1 - at least one error found     
 * Change log:
 *  02.15.2010 - Created - David Verro
 */
function validateDate(aId, aName, aValue, aMin, aMax, aDefault)
{
  try
  { 
    //construct the id of the error span associated  
    var errId = "err_" + aId;
    
    //check to make sure an id was passed in
    if(aId == null)
    {
      //if there are any problems with the id throw an exception
      throw "ID is null.  Aborting validation."; 
    }
    else if(aId == undefined)
    {
      //if there are any problems with the id throw an exception
      throw "ID is undefined.  Aborting validation.";
    }
    else if(aId == undefined)
    {
      //if there are any problems with the id throw an exception
      throw "ID is blank.  Aborting validation.";
    }
    
    //check to make sure a name was passed in
    if(aName == null)
    {
      //if not use the id - not ideal
      aName = aId;
    }
    else if(aName == undefined)
    {               
      //if not use the id - not ideal
      aName = aId;
    }
    else if(aName == "")
    {             
      //if not use the id - not ideal
      aName = aId;
    }
    
    //check to make sure a value was passed in
    if(aValue == null)
    {
      //if not go get the value based on the id
      var aValue = document.getElementById(aId).value;
    }
    else if(aValue == undefined)
    {    
      //if not go get the value based on the id
      var aValue = document.getElementById(aId).value;
    }
    
    //check to see if there is a default value to check against    
    if(aDefault == null)
    {
      aDefault = "";
    }
    else if(aDefault == undefined)
    {
      aDefault = "";
    }
    
    //if there is a default value and it matches the value from the form
    if(aDefault != "" && aValue == aDefault)
    {  
      //blank the value to cause an error
      aValue = "";
    }
    
    //check for a min value
    if(aMin == null)
    {
      //set default
      var aMin = 1900;
    }
    
    //check for a max value
    if(aMax == null)
    {
      //set default
      var aMaxValue = 5000;
    }
    
    
    //check to see if it is an empty string
    if(aValue == "")
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " is required";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    
    //check formatting before proceeding
    if(aValue.indexOf("/") == -1 || aValue.Length < 8 || (aValue.indexOf("/") == aValue.lastIndexOf("/")))
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " please use correct format MM/DD/YYYY";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    
    //parse the slashes from the date and split it into an array
    aValue = aValue.split("/"); 
    
    //check month value
    if(aValue[0].length < 1 || aValue[0] < 1 || aValue[0] > 12)
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " does not have a valid month (1-12)";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    } 
    //check day value
    else if(aValue[1].length < 1 || aValue[1] < 1 || aValue[1] > 31)
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " does not have a valid day (1-31)";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    //check year value validity and range
    else if(aValue[2].length < 4 || parseInt(aValue[2]) < aMin || parseInt(aValue[2]) > aMax)
    {
      //check to see if they put in a two digit year
      if(aValue[2].length == 2)
      {
        if(confirm("Did you mean 20" + aValue[2] + "?"))
        {
          aValue[2] = "20" + aValue[2];
          document.getElementById(aId).value = aValue[0] + "/" + aValue[1] + "/" + aValue[2];
        }
        else
        { 
          //set the appropriate error message
          document.getElementById(errId).innerHTML = "* " + aName + " does not have a valid year (" + aMin + "-" + aMax + ")";
          //highlight the control
          document.getElementById(aId).style.border = strErrorBorder;
          //set focus
          //document.getElementById(aId).focus();
          return 1;
        }
      }
      else
      {
        //set the appropriate error message
        document.getElementById(errId).innerHTML = "* " + aName + " does not have a valid year (" + aMin + "-" + aMax + ")";
        //highlight the control
        document.getElementById(aId).style.border = strErrorBorder;
        //set focus
        //document.getElementById(aId).focus();
        return 1;
      }
    }
    //check specific day values by year and month
    switch(parseInt(aValue[0]))
    {
      //february
      case (2):
        //check for leap year
        if(aValue[2] % 4 == 0 && (!(aValue[2] % 100 == 0)) || (aValue[2] % 400 == 0))
        {
          if(aValue[1] > 29)
          {
            //set the appropriate error message
            document.getElementById(errId).innerHTML = "* " + aName + " does not have a valid day (1-29)";
            //highlight the control
            document.getElementById(aId).style.border = strErrorBorder;
            //set focus
            //document.getElementById(aId).focus();
            return 1;
          }          
        }
        else if(aValue[1] > 28)
        {
          //set the appropriate error message
          document.getElementById(errId).innerHTML = "* " + aName + " does not have a valid day (1-28)";
          //highlight the control
          document.getElementById(aId).style.border = strErrorBorder;
          //set focus
          //document.getElementById(aId).focus();
          return 1;
        }
        break;
      //jan, march, may, july, august, october, december 
      //  - already checked this range
      case (1):
      case (3):   
      case (5):
      case (7):
      case (8):
      case (10):
      case (12):
        break;
      //april, june, september, november
      case (4):
      case (6):
      case (9):
      case (11):
        if(aValue[1] > 30)
        {
          //set the appropriate error message
          document.getElementById(errId).innerHTML = "* " + aName + " does not have a valid day (1-30)";
          //highlight the control
          document.getElementById(aId).style.border = strErrorBorder;
          //set focus
          //document.getElementById(aId).focus();
          return 1;
        } 
        break;
      //second catch for bad month value
      default:
        //set the appropriate error message
        document.getElementById(errId).innerHTML = "* " + aName + " does not have a valid month (1-12)";
        //highlight the control
        document.getElementById(aId).style.border = strErrorBorder;
        //set focus
        //document.getElementById(aId).focus();
        return 1;
        break;         
    };
    //alert(aValue[0] + "/" + aValue[1] + "/" + aValue[2]);
    //set the validated value in proper format
    document.getElementById(aId).value = aValue[0] + "/" + aValue[1] + "/" + aValue[2];
    
    //There have been no errors found if the function hasn't returned yet
    //remove the error message
    document.getElementById(errId).innerHTML = "";
    //set the control back to normal styles
    document.getElementById(aId).style.border = strRegBorder;
    
    return 0;
  }
  catch(err)
  {
    var txt="There was an error in function: validateDate, while checking " + aId + "\n";
    txt+="Error description: " + err.description + "\n";
    txt+="Error Message: " + err.message + "\n";
    txt+="Click OK to continue.\n"; 
    DisplayError(txt); 
  }
}

/*
 * Function Name: validateNumber
 * Purpose: Checks a number value to make sure it exists and it is a number
 * parameters:
 *    aId - Id to retrieve the number to be validated
 *    aName - the name of the value to be displayed to users
 *    aValue (optional) - the value of the number to be validated  
 *    aMinLength (optional - 1) - the minimum length of the number to be validated
 *    aMaxLength (optional - 5000) - the maximum length of the number to be validated  
 * Returns:
 *    error found - int
 *      0 - valid value
 *      1 - error found - see err_(id).innerHtml value for error message
 * Change log:
 *  02.11.2010 - Created - David Verro
 */
function validateNumber(aId, aName, aValue, aMinLength, aMaxLength, aDefault)
{
  try
  { 
    //construct the id of the error span associated  
    var errId = "err_" + aId;
    
    //check to make sure an id was passed in
    if(aId == null)
    {
      //if there are any problems with the id throw an exception
      throw "ID is null.  Aborting validation."; 
    }
    else if(aId == undefined)  
    {
      //if there are any problems with the id throw an exception
      throw "ID is undefined.  Aborting validation.";
    }
    else if(aId == undefined)
    {
      //if there are any problems with the id throw an exception
      throw "ID is blank.  Aborting validation.";
    }
    
    //check to make sure a name was passed in
    if(aName == null)
    {
      //if not use the id - not ideal
      aName = aId;
    }
    else if(aName == undefined)
    {               
      //if not use the id - not ideal
      aName = aId;
    }
    else if(aName == "")
    {             
      //if not use the id - not ideal
      aName = aId;
    }
    
    //check to make sure a value was passed in
    if(aValue == null)
    {
      //if not go get the value based on the id
      var aValue = document.getElementById(aId).value;
    }
    else if(aValue == undefined)
    {    
      //if not go get the value based on the id
      var aValue = document.getElementById(aId).value;
    }
    
    //check to see if there is a default value to check against    
    if(aDefault == null)
    {
      aDefault = "";
    }
    else if(aDefault == undefined)
    {
      aDefault = "";
    }
    
    //if there is a default value and it matches the value from the form
    if(aDefault != "" && aValue == aDefault)
    {  
      //blank the value to cause an error
      aValue = "";
    }
    
    //check for a min value
    if(aMinLength == null)
    {
      //set default
      var aMinValue = 0;
    }
    
    //check for a max value
    if(aMaxLength == null)
    {
      //set default
      var aMaxValue = 5000;
    }
    
    
    //check to see if it is an empty string
    if(aValue == "")
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " is required";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    //check to see if it is Not a Number
    else if(isNaN(aValue))
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " must be a number";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }  
    else if(aValue.length < aMinLength)
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " does not meet the minimum length required (" + aMinLength + ")";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    else if(aValue.length > aMaxLength)
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " exceeds the maximum length (" + aMaxLength + ")";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //set focus
      //document.getElementById(aId).focus();
      return 1;
    }
    else
    {
      //remove the error message
      document.getElementById(errId).innerHTML = "";
      //set the control back to normal styles
      document.getElementById(aId).style.border = strRegBorder;
      
      return 0;
    }
  }
  catch(err)
  {
    var txt="There was an error in function: validateNumber, while checking " + aId + "\n";
    txt+="Error description: " + err.description + "\n"; 
    txt+="Error Message: " + err.message + "\n";
    txt+="Click OK to continue.\n"; 
    DisplayError(txt); 
  }
}


/*
 * Function Name: validateFloat
 * Purpose: Checks a Float value to make sure it exists and it is a Float
 * parameters:
 *    aId - Id to retrieve the Float to be validated
 *    aName - the name of the value to be displayed to users
 *    aValue (optional) - the value of the Float to be validated  
 *    aMinValue (optional - 1) - the minimum Value of the Float to be validated
 *    aMaxValue (optional - 5000) - the maximum Value of the Float to be validated  
 * Returns:
 *    error found - int
 *      0 - valid value
 *      1 - error found - see err_(id).innerHtml value for error message
 * Change log:
 *  02.11.2010 - Created - David Verro
 */
function validateFloat(aId, aName, aValue, aMinValue, aMaxValue, aDefault)
{
  try
  { 
    //construct the id of the error span associated  
    var errId = "err_" + aId;
    
    //check to make sure an id was passed in
    if(aId == null)
    {
      //if there are any problems with the id throw an exception
      throw "ID is null.  Aborting validation."; 
    }
    else if(aId == undefined)
    {
      //if there are any problems with the id throw an exception
      throw "ID is undefined.  Aborting validation.";
    }
    else if(aId == undefined)
    {
      //if there are any problems with the id throw an exception
      throw "ID is blank.  Aborting validation.";
    }
    
    //check to make sure a name was passed in
    if(aName == null)
    {
      //if not use the id - not ideal
      aName = aId;
    }
    else if(aName == undefined)
    {               
      //if not use the id - not ideal
      aName = aId;
    }
    else if(aName == "")
    {             
      //if not use the id - not ideal
      aName = aId;
    }
    
    //check to make sure a value was passed in
    if(aValue == null)
    {
      //if not go get the value based on the id
      var aValue = document.getElementById(aId);
    }
    else if(aValue == undefined)
    {    
      //if not go get the value based on the id
      var aValue = document.getElementById(aId);
    }
    
    //check to see if there is a default value to check against    
    if(aDefault == null)
    {
      aDefault = "";
    }
    else if(aDefault == undefined)
    {
      aDefault = "";
    }
    
    //if there is a default value and it matches the value from the form
    if(aDefault != "" && parseFloat(aValue) == parseFloat(aDefault))
    {  
      //blank the value to cause an error
      aValue = "";
    }
    
    //check for a min value
    if(aMinValue == null)
    {
      //set default
      var aMinValue = 0;
    }
    
    //check for a max value
    if(aMaxValue == null)
    {
      //set default
      var aMaxValue = 99999.99;
    }
    
    
    //check to see if it is an empty string
    if(aValue == "")
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " is required";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //clear and set focus
      document.getElementById(aId).value = parseFloat(0).toFixed(2);
      //document.getElementById(aId).focus();
      return 1;
    }
    //check to see if it is Not a Float
    else if(isNaN(aValue))
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " must be a number";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //clear and set focus
      document.getElementById(aId).value = parseFloat(0).toFixed(2);
      //document.getElementById(aId).focus();
      return 1;
    }  
    else if(parseFloat(aValue) < parseFloat(aMinValue))
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " does not meet the minimum Value required (" + aMinValue + ")";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //clear and set focus
      document.getElementById(aId).value = parseFloat(0).toFixed(2);
      //document.getElementById(aId).focus();
      return 1;
    }
    else if(parseFloat(aValue) > parseFloat(aMaxValue))
    {
      //set the appropriate error message
      document.getElementById(errId).innerHTML = "* " + aName + " exceeds the maximum Value (" + aMaxValue + ")";
      //highlight the control
      document.getElementById(aId).style.border = strErrorBorder;
      //clear and set focus
      document.getElementById(aId).value = parseFloat(0).toFixed(2);
      //document.getElementById(aId).focus();
      return 1;
    }
    else
    {
      //remove the error message
      document.getElementById(errId).innerHTML = "";
      //set the control back to normal styles
      document.getElementById(aId).style.border = strRegBorder;
      
      return 0;
    }
  }
  catch(err)
  {
    var txt="There was an error in function: validateFloat, while checking " + aId + "\n";
    txt+="Error description: " + err.description + "\n"; 
    txt+="Error Message: " + err.message + "\n";
    txt+="Click OK to continue.\n"; 
    DisplayError(txt); 
  }
}


/*
 * Function Name: stateChanged
 * Purpose: Reacts to the state of the process changing
 * parameters:
 *    indirect parameter - readyState - state of the process
 *      0 - Object is not initialized 
 *      1 - Loading object is loading data
 *      2 - Loaded object has loaded data   
 *      3 - Data from object can be worked with
 *      4 - Object completely initialized  
 * Change log:
 *  02.11.2010 - Created - David Verro, Tim Burrows
 */
function stateChanged()
{	
  if (xmlhttp.readyState==4)
  {
    //insert new content into the page (divid)
    document.getElementById("innercontent").innerHTML=xmlhttp.responseText;
    //redirect if desired
    setTimeout("window.location = 'index.php'", 10000);
  }
}


/*
 * Function Name: GetXmlHttpObject
 * Purpose: Gets the html data from the browser  
 * Change log:
 *  02.11.2010 - Created - David Verro, Tim Burrows
 */
function GetXmlHttpObject()
{
  if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  
  if (window.ActiveXObject)
  {
  // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  return null;
}

