//************************Script Info**************************************************************
//Version: 1.0
//Date: 06/08/07
//Changes:
//Programmed by:  Charles McKinney
//*************************************************************************************************

//global variables
var VisualInputArray = new Array();   //array for processing visual input checks
var RequiredInputArray = new Array();  //array for checking required elements on submit
var PhoneLocArray = new Array();      //array of assigned phone locations/variables
var DataLocArray = new Array();       //array for assigned data locations
var RevealArray = new Array();       //array for set reveal options
var DestObjArray = new Array();      //array for select destination lists - check all options on submit
var ErrorHandling = "normal_alert";  //global variable to define error handling options
var PhoneAreaLength;                 //global variable to check phone text length
var color_error = false;
var cs_error_background_color = "red";  //background color to use on error inputs
var cs_error_font_color = "white";      //font color to use on error inputs
var restore_error_background_color = "white";  //background color to restore on corrected errors
var restore_error_font_color = "black";        //font color to restore on corrected errors
var undefined;

//error messages
var nummsg1 = "You must enter a value";
var nummsg2 = "You must enter a number";
var nummsg3 = "You must enter a number with _DECIMALS_ decimal places";
var nummsg4 = "You entered _VALUE_ was expecting a value greater than or equal to _MINIMUM_";
var nummsg5 =  "You entered _VALUE_ was expecting a value less than or equal to _MAXIMUM_";

var reqmsg1 = "The information in _OBJECTNAME_ is required but is blank or has not been selected.";

var datamsg1 = "You must enter a location and a length separated by a period.";
var datamsg2 = "You entered a location but did not enter a length.";
var datamsg3 = "You entered a length but did not enter a location.";
var datamsg4 = "Data Locations and lengths must have a value greater than 0.";
var datamsg5 = "Data Locations and lengths must be a number.";

var phonemsg1 = "The location and length entered for _LOCATION NAME_ now exceeds the user phone text area.  This will need to be corrected";
var phonemsg4 = "This location must be at least 51 if you have a length assigned";
var phonemsg5 = "This location and length cannot exceed _PHONEAREALENGTH_";
var phonemsg7 = "The value for _LOCATIONNAME_ is _LOCATIONVALUE_  This overlaps another phone text field _FIELDNAME_";

var timemsg1 = "Acceptable time formats are ##:##am, ##:##pm for a 12 hour clock or ##:## for a 24 hour clock";
var timemsg2 = "The time you entered was _BASETIME_ and it must be _COMPARE_ _COMPARETIME_";
var timemsg3 = "The time you entered was _COMPARETIME_  and it must be _COMPARE_ _BASETIME_"

var comnum_msg1 =  "You entered _VALUE1_ and that value must be _COMPARE_ _VALUE2_";
var comnum_msg2 =  "You entered _VALUE2_ and that value must be _COMPARE_ _VALUE1_";

var chk12msg1 = "You must enter 2 characters and only 2 characters for minutes";
var chk12msg2 = "Only 1 to 12 are valid for hour of the day when using a 12 hour clock";
var chk12msg3 = "Only 00 to 59  are valid for minutes";

var chk24msg1 = "You must enter 2 characters and only 2 characters for hour";
var chk24msg2 = "You must enter 2 characters and only 2 characters for minutes";
var chk24msg3 = "Only 0 to 23 are valid for hour of the day when using a 24 hour clock";
var chk24msg4 = "Only 00 to 59  are valid for minutes";

var add_security_type_msg1 = "You must check at least one security option";

var market_msg = "The maximum number of markets allowed with _TIMEZONES_ time zones is _MAXMARKETS_.  You need to reduce the number of timezones or the number of markets.";
var market_msg2 = "The maximum length for market names is 20.";
var market_msg3 = "The maximum length for a market name is _MARKETLEN_.  You need to shorten this name or increase the length of the Market Location."
var market_msg4 = "You need to set the data location and length before defining the Market Names."

var TZ_msg1 = "You can only have more than 24 time zones if you have Half Time Zones checked";
var TZ_msg2 = "When using half-time zones the only allowed values are A-X5 where A-X represent time zones 01-24";
var TZ_msg3 = "You can only have up to 24 timezones if you are not using half timezones.  You need to change that value in order to click this."; 
var TZ_msg4 = "You have already used that value for another timezone.  You cannot have duplicate timezones.";

var bucket_msg1 = "You must enter a bucket order if you have Use Bucket Order checked";
var bucket_msg2 = "You must use commas to separate the values and there are no commas entered.";
var bucket_msg3 = "You must enter 10 values 0-9 separated by commas.  There are values missing.";
var bucket_msg4 = "You can only enter values 0 - 9 for Bucket Order and you entered _CURRENTVALUE_.";
var bucket_msg5 = "The value - _BUCKETVALUE_ -  was entered more than once.  You can only use a bucket value once.";

var index_msg1 = "You need to define the location and length of this index."
var index_msg2 = "Since you checked allow duplicates in this index you must enter a value."
var index_msg3 = "You cannot allow duplicates in the secondary index for a webSurvent job."
var index_msg4 = "You must have a Secondary Index before creating a Tertiary Index."
var index_msg5 = "A Secondary Index off password is required for webSurvent projects."

var gennumber_msg1 = "You cannot enter anything here if you have not checked Generate numbers for webSurvent sample above";
var gennumber_msg2 = "The value for generated phone numbers must be 7 digits and only 7 digits";

var password_msg1 = "You have a password input location entered but do not have Import  passwords checked.  You need to blank the input if you want to uncheck this";
var password_msg2 = "You have said to not generate a password but you have the password length set.  You need to blank the input if you do not want this checked";
var password_msg3 = "You must enter a value if you have Generate passwords checked above.";
var password_msg4 = "You maximum length of a password is 20 characters. You are importing or generating a password with greater than 20 characters"; 
var password_msg5 = "You have said to generate a password but you do not have the password length set.  You need enter a value.";
var password_msg6 = "The minimum length of a password is 5 characters.  You need to either import or generate a password with greater than 4 characters.";

var username_msg1 = "You cannot generate a User Name and import one. If you want to enter something here you must uncheck Generate User Name."
var username_msg2 = "You cannot generate a User Name and import one. If you want to check this you need to blank the User Name Input location."
var username_msg3 = "You cannot enter anything here if you have not selected Create Custom UserName";
var username_msg4 = "You cannot check these if Generate User Name is not checked."
var username_msg5 = "You must enter a value if you have selected Create Custom UserName";

var length_msg1 = "You cannot have a length on an input that is greater than the length on the output";
var length_msg2 = "You cannot have a length on an output that is less than the length on the input";

var input_output_msg1 = "You have entered an import value without entering a corresponding output value.  You must have both if you have one."
var input_output_msg2 = "You have entered an output value without entering a corresponding import value.  You must have both if you have one."

var date_msg1 = "Your start date must be the same as or earlier than your end date";

var intvstatus_msg1 = "Since you selected Interviewer Productivity Report you must tell us how to run the report.  Please select either Cost per complete or cost per interviewer hour.";
var intvstatus_msg2 = "Since you selected Interviewer Productivity Report you must provide a value for Cost per interview";
var intvstatus_msg3 = "In order to enter a cost you must select either Cost per complete or cost per interviewer hour.";

var badbase_msg = "SELECT statement must start with a bracket ([), ampersand (&) or defined keyword (@).  Please check your response."

var noreport_msg = "You must select at least one report to run";

var createindex_msg1 = "You must select at least one of Login from link, Login from Cookie or Login from random.";
var createindex_msg2 = "You must select at least Login from link or Login from Cookie.";
var createindex_msg3 = "Since you selected Use ID you must enter a value for ID in link";
var createindex_msg4 = "Since you selected Use Password you must enter a value for Password in link and Name in link";
var createindex_msg5 = "You must enter a value.";   
var createindex_msg6 = "Since you selected Login from Link then you need to check either Use ID or Use Password";
var createindex_msg7 = "Since you selected Use ID you must enter a value for Name in index";

var AccessStudy_msg1 = "You must select a mode to access";
var AccessStudy_msg2 = "Since you selected webSurvent you need to select either Test or Live.";
var AccessStudy_msg3 = "You must select either Test or Live.";

var reformat_msg1 = "You must select either qff or db as your variable source";
var reformat_msg2 = "Since you selected an SQL output you must enter both an SQL Database name and a SQL Table name";
var reformat_msg3 = "Since you selected an delimited output you must check a type of delimiter.";
var reformat_msg4 = "Since you chose Other delimiter you nust enter the character to use as a delimiter.";
var reformat_msg5 = "You must enter both a first question and a last question if you enter one of them.";
var reformat_msg6 = "Since you checked output text questions you must enter a length.";
var reformat_msg6 = "Since you checked output text questions you must enter a length.";
var reformat_msg7 = "Since you selected qff as your variable source you must select a qff to use";
var reformat_msg8 = "Since you selected db as your variable source you must select a db to use";
var reformat_msg9 = "Since you want to use a base and are using qff as your variable source you must select a db to use for your base";
var reformat_msg10 = "Since you want to use a base you need to enter a base value to use";

var Employee_msg1 = "There is already a record with that ID.  Please enter a different ID.";
var Employee_msg2 = "You did not enter an ID or name to get.";
var Employee_msg3 = "You have made changes or added records.  Do you wish to save your changes before continuing. Click OK to continue without saving.  Click Cancel to return and save your changes";
var Employee_msg4 = "You must enter an ID.";
var Employee_msg5 = "You have this record marked for deletion.  If you want to edit it and not delete it click 'OK'.  If you want to remain marked for deletion click 'Cancel'";

var od_msg1 = "You must enter a value for the base if you have base turned on";
var od_msg2 = "You must enter a value for the weight if you have weights turned on";
var od_msg3 = "You chose to use an external banner file but did not select a file to use";
var od_msg4 = "You must choose at least one column to use";
var od_msg5 = "You must choose at least one row to use";
var od_msg7 = "Since you chose other on 'What to print for Integer Zero' then you must a character to use";
var od_msg8 = "Since you chose other on 'What to print for Floating-Point Zero' then you must a character to use";
var od_msg9 = "Since you chose other on 'What to print for Missing Values' then you must a character to use";
var od_msg10 = "Since you chose other on 'What to print for Empty Cells' then you must a character to use";
var od_msg11 = "You chose to use a column number to base ranks on but did not enter a number.  Please enter a number or choose another option";
var od_msg12 = "You can select a Def file or a Qpx file but not both";

var VList_msg1 = "You selected to use a subset of questions but you did not enter a first or a last question for the subset.";
var VList_msg2 = "You selected to use a specific question or set of questions but you did not enter a question to use.";
var VList_msg3 = "You selected to use a base but did not enter a base to use.";
var VList_msg4 = "You selected to use an additional variable  but did not enter a variable to use.";
var VList_msg5 = "You need to enter specific questions to use in sequence and there is a previous question missing.";

var modweb_msg1 = "The string _THISMESSAGE_ is required in this error message.";
var modweb_msg2 = "You chose to use Set Force Suspends but did not enter a number of seconds.  This is required.";

var copyfile_msg1 = "You can only enter a number up to 4 decimal places.  Please try again.";

//********************SetDataChecks function **************************************************
//  Purpose:  Use text to set all data checks for a single object.
//
//  Usage:    Pass id of element
//            Pass checks to do in order
//
//  Date:     05/01/07
//  Changes:
//*****************************************************************************************

//add option to only check on submit for SetRequired

function SetDataChecks(ElementID,Check1) {

    var Elements = ElementID.split(":");

    for (var a = 0; a < Elements.length;a++) {

      var ThisObject = FindObjects(Elements[a]);
      ThisObject = ThisObject[0];
      
      if (ThisObject.type == "radio") {
        var ThisObject = FindObjects(Elements[a],true);
        ThisObject = ThisObject[0];
      }
      
      //alert(ElementID + " " + ThisObject);
      for (var b = 1; b < SetDataChecks.arguments.length;b++) {

         ThisCheck = SetDataChecks.arguments[b];
         CurrentCheck = ThisCheck.split(":");
         CurrentCheckFunction = CurrentCheck[0];

         //alert(ThisCheck);
         if (CurrentCheckFunction == "REQUIRED") SetRequired(ThisObject,CurrentCheck[1],CurrentCheck[2]);
         if (CurrentCheckFunction == "NUM") SetNum(ThisObject,ThisCheck);
         if (CurrentCheckFunction == "TIME") SetTime(ThisObject);
         if (CurrentCheckFunction == "DATALOC") SetDataLocation(ThisObject);
         if (CurrentCheckFunction == "PHONETEXTLENGTH") SetPhoneTextLength(ThisObject);
         if (CurrentCheckFunction == "PHONEAREA") SetPhoneTextArea(ThisObject);
         if (CurrentCheckFunction == "COMPARENUM") SetCompareNum(ThisObject,CurrentCheck[1],CurrentCheck[2]);
         if (CurrentCheckFunction == "COMPARETIME") SetCompareTime(ThisObject,CurrentCheck[1],CurrentCheck[2]);
         if (CurrentCheckFunction == "FILLDATA") SetFillData(ThisObject,ThisCheck);
         if (CurrentCheckFunction == "CLEARDATA") SetClearData(ThisObject,ThisCheck);
      }
   }
}


//********************SetVisualInput function **********************************************
//  Purpose:  Use text/html to gather and store data in a hidden input.
//
//  Usage:    Requires a specific naming convention on the div's and input tag
//            show_inputid_value to store
//            show_Q1_Y  would store a value of Y in Q1
//            Arguments are:
//            id1 - the id of the first div required
//            id2 - the id of the second div, etc....
//            for default responses fill in the hidden field on the page with default value
//
//  Date:     06/08/07
//  Changes:
//*****************************************************************************************


function SetVisualInput(id1,id2) {

  var ShowObjectArray = new Array();

  for (var a = 0; a < SetVisualInput.arguments.length;a++) {

     ShowObject = FindObjects(SetVisualInput.arguments[a]);
     ShowObject = ShowObject[0];

     ShowObjectSplit = ShowObject.id.split("_");
     InputID = ShowObjectSplit[1];
     DataInput = FindObjects(""+InputID+"");
     DataInput = DataInput[0];

     ObjectValue = ShowObjectSplit[2];

     ShowObject.input = DataInput;
     ShowObject.value = ObjectValue;
     ShowObjectArray[ShowObjectArray.length] = ShowObject;
     ShowObject.others = ShowObjectArray;
     ShowObject.checkVI = true;
     ShowObject.onclick = function() {CheckAll(this);}

     //build input properties for onload function
     VisualInputArray[VisualInputArray.length] = DataInput;
     DataInput.ShowObj = ShowObjectArray;

    //assign class and set properties based on default response

      if (ObjectValue == DataInput.value) {
         SetClass(ShowObject.id,"in");
      } else {
         SetClass(ShowObject.id,"out");
      }
  }
    AddCheckAll(CheckVisualInput);
}

//********************CheckVisualInput function **************************************************
//  Purpose:  Change input value and class based on which div was clicked.
//
//  Date:     12/08/06
//  Changes:
//*****************************************************************************************

function CheckVisualInput(ElemObject) {

     if (ElemObject.checkVI != true) return true;
     ElemObject.input.value = ElemObject.value;
     SetClass(ElemObject.id,"in");
     for (var a = 0; a < ElemObject.others.length;a++) {

       OtherObject = ElemObject.others[a];
       if (ElemObject == OtherObject) continue;
       SetClass(OtherObject.id,"out");
     }
     return true;
}

//********************LoadVisualInput function **************************************************
//  Purpose:  Checks existing or default value and sets div classes and base value appropriately
//
//  Date:     12/08/06
//  Changes:
//*****************************************************************************************


function LoadVisualInput() {

  for (var a = 0; a < VisualInputArray.length; a++) {
   CurrentObject = VisualInputArray[a];

   for (var b = 0; b < CurrentObject.ShowObj.length;b++) {
      ThisShow = CurrentObject.ShowObj[b];

        if (CurrentObject.value.toUpperCase() == ThisShow.value.toUpperCase()) {
           SetClass(ThisShow.id,"in");
        } else {
          SetClass(ThisShow.id,"out");
        }
   }
 }
}

AddOnLoad(LoadVisualInput);

//********************set num function ***********************
//
//Purpose:  Set up check for numeric entries
//
//Arguments: ElemObject = object to check - required
//           Rules = items seprated by ":"  - required but items can be blank
//           min = minimum value
//           max = maximum value
//           numdecs = number of decimals allowed
//
//
//Date:     12/08/06
//Changes:
//
//****************************************************************


function SetNum(ElemObject,Rules) { //**start setnum function

 //qlabel,qmin,qmax,numdecs,
   NewRules = Rules.split(":");

  //alert("name= " + ElemObject.id + " min= " + NewRules[1] + " max= " + NewRules[2] + " dec=" + NewRules[3]);

   if (NewRules[1] != "") {
      ElemObject.min = NewRules[1]-0;
   } else {
      ElemObject.min;
   }

   if (NewRules[2] != "") {
      ElemObject.max = NewRules[2]-0;
   } else {
      ElemObject.max;
   }

   if (NewRules[3] != "") {
      ElemObject.dec = NewRules[3]-0;
   } else {
      ElemObject.dec = 0;
   }

  //alert("name= " + ElemObject.id + " min " + ElemObject.min + " max= " + ElemObject.max + " dec=" + ElemObject.dec);

   ElemObject.num_chk = true;
   ElemObject.last_value = ElemObject.value;
   ElemObject.onblur = function() { CheckAll(this);}
   AddCheckAll(CheckNum);

} //**end setnum function


//********************check num function ***********************
//
//Purpose:  Checks to see if a valid number is entered.
//Date:     12/08/06
//Changes:
//
//****************************************************************

function CheckNum(ElemObject) {//**start check_num function

  if (ElemObject.num_chk != true) return true;

  var GetValue = FindValues(ElemObject);
  if (GetValue === false) return true;

  //if (GetValue == false && GetValue[0] == undefined) {
  //    ElemObject.value = ElemObject.last_value;
  //    ShowErrorAlert(nummsg1,ElemObject);
  //    return false;
  //}

  ThisValue = GetValue[0];
  CheckValue = ThisValue - 0;

  if (isNaN(CheckValue)) {
      ElemObject.value = ElemObject.last_value;
      ShowErrorAlert(nummsg2,ElemObject);
      return false;
  }

  if (!CheckDecimals(ThisValue,ElemObject.dec)) {
       var errormsg = SetErrorText(nummsg3,"_DECIMALS_|"+ ElemObject.dec + "");
       
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(errormsg,ElemObject);
       return false;
  }
  ThisValue = FormatDecimals(ThisValue,ElemObject.dec);

  if (ThisValue < ElemObject.min && ElemObject.min != undefined) {
     var errormsg = SetErrorText(nummsg4,"_VALUE_|"+ElemObject.value+"|_MINIMUM_|"+ElemObject.min+"");
     ElemObject.value = ElemObject.last_value;
     ShowErrorAlert(errormsg,ElemObject);
     return false;
  }


  if (ThisValue > ElemObject.max && ElemObject.max != undefined) {
     var errormsg = SetErrorText(nummsg5,"_VALUE_|"+ElemObject.value+"|_MAXIMUM_|"+ElemObject.max+"");
     ElemObject.value = ElemObject.last_value;
     ShowErrorAlert(errormsg,ElemObject);
     return false;
  }

  ElemObject.last_value = ElemObject.value;
  if (ErrorHandling == "display") HideError(ElemObject);

  ChangeColor(ElemObject);
  ElemObject.haserror = false;
  return true;
} //**end check_num function


//*******************set required function ***********************
//
//Purpose:  Set up check for required inputs
//
//Arguments: ElemObject = object to check
//           ErrorMessage - error to display (optional)
//             if no ErrorMessage will use default
//           SubmitOnly - true or missing will only check on submit
//Date:     05/01/07
//Changes:
//
//****************************************************************


function SetRequired(ElemObject,ErrorMessage,SubmitOnly) {

   if (SubmitOnly == undefined) SubmitOnly = true;
   ElemObject.required_chk = true;
   RequiredInputArray[RequiredInputArray.length] = ElemObject;
   ElemObject.last_value = ElemObject.value;
   ElemObject.errorMsg = ErrorMessage;
   ElemObject.SubmitOnly = SubmitOnly;
   if (SubmitOnly != true) ElemObject.onblur = function() { CheckAll(this);}
 
   AddCheckAll(CheckRequired);
   AddOnSubmit(SubmitRequired);
}

//*******************check required function ***********************
//
//Purpose:  Check for required inputs
//
//Arguments: ElemObject = object to check
//
//Date:     05/01/07
//Changes:
//
//****************************************************************


function CheckRequired(ElemObject) {

      if (ElemObject.required_chk != true || ElemObject.SubmitOnly == true) return true;

   if (ElemObject.value.length < 1) {
     if (ElemObject.errorMsg != undefined) {
        errormsg = ElemObject.errorMsg;
     } else {
        errormsg = SetErrorText(reqmsg1,"_OBJECTNAME_|" + ElemObject.name + "");
     }
     ElemObject.value = ElemObject.last_value;
     ShowErrorAlert(errormsg,ElemObject);
     return false;
   }
   if (ErrorHandling == "display") HideError(ElemObject);
   
   ChangeColor(ElemObject);
   ElemObject.haserror = false;
   return true;
}


//*******************submit required function ***********************
//
//Purpose:  Check for required inputs on submission of page
//
//Date:     05/01/07
//Changes:
//
//****************************************************************


function SubmitRequired() {

   for (var a = 0; a < RequiredInputArray.length;a++) {

     ElemObject = RequiredInputArray[a];

    if (ElemObject.type == "checkbox" || ElemObject.type == "radio" || ElemObject.type.indexOf("select") != -1) {
       ElemValue = FindValues(ElemObject);
       //alert(ElemObject.id + " " + ElemValue);
       
       if (ElemValue === false) {
           if (ElemObject.errorMsg != undefined) {
             errormsg = ElemObject.errorMsg;
           } else {
             errormsg = SetErrorText(reqmsg1,"_OBJECTNAME_|" + ElemObject.name + "");
           }
        
           ElemObject.value = ElemObject.last_value;
           ShowErrorAlert(errormsg,ElemObject);
           return false;
       }
     }
    
     
    if (ElemObject.type == "text" || ElemObject.type == "textarea") {
      if (ElemObject.value.length < 1) {
     
        if (ElemObject.errorMsg != undefined) {
           errormsg = ElemObject.errorMsg;
        } else {
           errormsg = SetErrorText(reqmsg1,"_OBJECTNAME_|" + ElemObject.name + "");
        }
 
        ElemObject.value = ElemObject.last_value;
        ShowErrorAlert(errormsg,ElemObject);
        return false;
      }
    }  
     ElemObject.haserror = false;
     ChangeColor(ElemObject);
   }
   if (ErrorHandling == "display") HideError(ElemObject);
   return true;
}

//********************set datalocation function ***********************
//
//Purpose:  Set a check for a valid data location
//
//Arguments: id location and id of length inputs
//Date:     05/15/07
//Changes:
//
//****************************************************************

function SetDataLocation(LocObject) {

     LocObject.chkloc = true;
     LocObject.onblur = function() {CheckAll(this);}

     DataLocArray[DataLocArray.length] = LocObject;

     AddCheckAll(CheckDataLocation);
     AddOnSubmit(DataLocationSubmit);
}

//********************check datalocation function ***********************
//
//Purpose:  Check to make sure both a valid location and length have been entered
//
//Arguments: object to check
//Date:     05/15/07
//Changes:
//
//****************************************************************

function CheckDataLocation(ElemObject) {

   if (ElemObject.chkloc != true) return true;

   //alert(ElemObject.value);
   if (ElemObject.value.length == 0) return true;
   
   CheckPeriod = ElemObject.value.indexOf(".");
   
   if (CheckPeriod == -1) {
      ShowErrorAlert(datamsg1,ElemObject);   
      return false;
   }   
 
   DataLocation = ElemObject.value.split(".");
   
   ThisLocationValue = DataLocation[0] - 0;
   ThisLengthValue = DataLocation[1] - 0;

   //alert(ThisLocationValue + " " + ThisLengthValue);

   if (isNaN(ThisLocationValue) || isNaN(ThisLengthValue)) {
        ShowErrorAlert(datamsg5,ElemObject);
        return false;
   }            


   if (ThisLocationValue == 0 || ThisLengthValue == 0) {
        ShowErrorAlert(datamsg4,ElemObject);
        return false;
   }            
               
   if (ThisLocationValue > 0 && ThisLengthValue.length == 0) {
         ShowErrorAlert(datamsg2,ThisLength);
         return false;
   }

   if (ThisLengthValue > 0 && ThisLocationValue.length == 0) {
         ShowErrorAlert(datamsg3,ThisLocation);
         return false;
   }

  if (ErrorHandling == "display") HideError(ElemObject);

  //alert(ElemObject.value.length); 
  for (var a = 0; a <= ElemObject.value.length; a++) {
     ThisChar = ElemObject.value.substr(a,1);
     
     if (ThisChar.charCodeAt(0) == 32) {
       NewLoc = a + 1;
       ElemObject.value = ElemObject.value.substr(NewLoc,ElemObject.value.length+1);
     }
     //alert(a + " " +  ElemObject.value.length);
  }   
 

ElemObject.haserror = false;
ChangeColor(ElemObject);
return true;
}


//********************data location on submit function ***********************
//
//Purpose:  Checks to make sure that a location and length are provided on submit
//
//Arguments: none
//Date:     12/08/06
//Changes:
//
//****************************************************************

function DataLocationSubmit() {

  for (var a = 0; a < DataLocArray.length;a++) {

      ThisLoc = DataLocArray[a];
      
      //alert(ThisLoc.id);
      if (ThisLoc.value.length == 0) return true;

      CheckPeriod = ThisLoc.value.indexOf(".");
   
      if (CheckPeriod == -1) {
         ShowErrorAlert(datamsg1,ThisLoc);   
         return false;
      }   
 
      DataLocation = ThisLoc.value.split(".");
   
      ThisLocationValue = DataLocation[0] - 0;
      ThisLengthValue = DataLocation[1] - 0;

      if (ThisLocationValue > 0 && ThisLengthValue == 0) {
           ShowErrorAlert(datamsg2,ThisLength);
           return false;
      }

      if (ThisLengthValue > 0 && ThisLocationValue == 0) {
          ShowErrorAlert(datamsg3,ThisLocation);
          return false;
      }

  }
 
  if (ErrorHandling == "display") HideError(ElemObject);
  
  ElemObject.haserror = false;
  ChangeColor(ElemObject);

return true;
}


//********************set phone text length function ***********************
//
//Purpose:  Set the length of the phone text area
//
//Arguments: id or name of input to use
//Date:     12/08/06
//Changes:
//
//****************************************************************


function SetPhoneTextLength(ElemObject) {

   ElemObject.chk_phone = true;
   ElemObject.onblur = function() {CheckAll(this);}

   AddCheckAll(ChangePhoneTextLength);
   AddOnLoad(PhoneTextLengthLoad);
}

//********************PhoneTextLengthLoad function ***********************
//
//Purpose:  Set the length of the phone text area on load
//
//Arguments: id or name of input to use
//Date:     12/08/06
//Changes:
//
//****************************************************************

function PhoneTextLengthLoad() {

    PhoneText = FindObjects("phoneTextlength");
    PhoneText = PhoneText[0];

    //alert(PhoneText.value);
    PhoneAreaLength = (PhoneText.value - 0);
}

//********************set phone text area function ***********************
//
//Purpose:  Set up to check if location and length entered are
//          inside the user area of the phone file.
//
//Arguments: location object
//
//Date:     12/08/06
//Changes:
//
//****************************************************************


function SetPhoneTextArea(ElemObject) { //**start setphonetextarea function

   //alert(ElemObject.id)
   ElemObject.phone_chk = true;
   ElemObject.last_value = ElemObject.value;
   ElemObject.onblur = function() { CheckAll(this);}

   PhoneLocArray[PhoneLocArray.length] = ElemObject;

   AddCheckAll(CheckPhoneTextArea);

} //**end setphonetextarea function


//********************change phone text length function ***********************
//
//Purpose:  Reset the length of the phone text area
//
//Arguments: id or name of input to use
//Date:     12/08/06
//Changes:
//
//****************************************************************


function ChangePhoneTextLength(ElemObject) {

   //alert("ChangePhoneTextLength is running");
   if (ElemObject.chk_phone != true) return true;

   for (var a = 0; a < PhoneLocArray.length; a++) { 

     ThisLoc = PhoneLocArray[a];
     DataLocation = ThisLoc.value.split(".");
     ThisLocation = DataLocation[0] - 0;
     ThisLength = DataLocation[1] - 0;
     
     ThisDataLoc = ThisLocation + ThisLength;

      //alert(ThisDataLoc + " " + PhoneAreaLength);
      if (ThisDataLoc > PhoneAreaLength) {
        var errormsg = SetErrorText(phonemsg1,"_LOCATION NAME_|"+ ThisLoc.name + "");
        ThisLoc.value = ThisLoc.last_value;
        ShowErrorAlert(errormsg,ThisLoc);
        return false;
      }
      ThisLoc.last_value = ThisLoc.value;
   }


  if (ErrorHandling == "display") HideError(ElemObject);
  ElemObject.haserror = false;
  ChangeColor(ElemObject);

  return true;
}


//********************check phone text area function ***********************
//
//Purpose:  Check if location and length entered are
//          inside the user area of the phone file.
//          Check that there are no overlaps with other fone text entries
//
//Arguments: automatic
//Date:     12/08/06
//Changes:
//
//****************************************************************

function CheckPhoneTextArea(ElemObject) {
 
  if (ElemObject.phone_chk != true) return true;
  if (ElemObject.id == "phoneOutputLoc") return true;
  
  //alert("checking phone text area");
  
   DataLocation = ElemObject.value.split(".");
   ThisLocationValue = DataLocation[0] - 0;
   ThisLengthValue = DataLocation[1] - 0;

    if (ThisLocationValue > 0 && ThisLocationValue < 50) {
       ElemObject.value = ElemObject.last_value;;
       ShowErrorAlert(phonemsg4,ElemObject);
       return false;
    }

    ThisDataLoc = ThisLocationValue + ThisLengthValue;

    //alert(PhoneAreaLength);
    if (ThisDataLoc > PhoneAreaLength) {
       var errormsg = SetErrorText(phonemsg5,"_PHONEAREALENGTH_|"+ PhoneAreaLength + "");
       ElemObject.value = ElemObject.last_value;;
       ShowErrorAlert(errormsg,ElemObject);
       return false;
    }
  
  var phone_ok = ChangePhoneTextLength(ElemObject);
  if (!phone_ok) return false;

  if (ElemObject.id != "secIndex" && ElemObject.id != "terIndex") {
    var OverlapOK = CheckPhoneOverlap(ElemObject);
    if (!OverlapOK) return false;
  }  

  ElemObject.last_value = ElemObject.value;
  if (ErrorHandling == "display") HideError(ElemObject);
  ElemObject.haserror = false;
  ChangeColor(ElemObject);
  return true;
}


//********************check phone overlap function ***********************
//
//Purpose:  Used by phone functions to determine overlaps.
//          Returns false and errors if there. Returns true if there aren't
//Arguments: id or name of input to use
//Date:     12/08/06
//Changes:
//
//****************************************************************


function CheckPhoneOverlap(ElemObject) {

     DataLocation = ElemObject.value.split(".");
     ElemObjectLocation = DataLocation[0] - 0;
     ElemObjectLength = DataLocation[1] - 0;
     
     ElemObjectDataLoc = ElemObjectLocation + ElemObjectLength;

     ElemObjectLocationMax = ElemObjectDataLoc - 1;

   for (var a = 0; a < PhoneLocArray.length; a++) {

      CurrentLoc = PhoneLocArray[a];
      CurrentDataLocation = CurrentLoc.value.split(".");
      CurrentLocation = CurrentDataLocation[0] - 0;
      CurrentLength = CurrentDataLocation[1] - 0;

      if (CurrentLoc.id == "secIndex" || CurrentLoc.id == "terIndex") continue;
      if (ElemObject == CurrentLoc) continue;

      CurrentLocationMax = (CurrentLocation + CurrentLength) - 1;

      if (CurrentLocationMax == 0 || ElemObjectLocationMax == 0) continue;

      if ((CurrentLocationMax >= ElemObjectLocation && CurrentLocationMax <= ElemObjectLocationMax) || (CurrentLocation >= ElemObjectLocation && CurrentLocation <= ElemObjectLocationMax)) {
        var errormsg = SetErrorText(phonemsg7,"_LOCATIONNAME_|"+ ElemObject.name + "|_LOCATIONVALUE_|"+ ElemObjectLocation + "|_FIELDNAME_|"+ CurrentLoc.name +"");
        ElemObject.value = ElemObject.last_value;
        ShowErrorAlert(errormsg,ElemObject);
        return false;
      }
    }

  ElemObject.last_value = ElemObject.value;
  if (ErrorHandling == "display") HideError(ElemObject);
  ElemObject.haserror = false;
  ChangeColor(ElemObject);
  return true;
}

//********************set time function ***********************
//
//Purpose:  Set up check for valid time entries.  Check for military
//          and/or am and pm
//
//Arguments: ElemObject = object to check
//           All arguments are required
//
//Date:     12/11/06
//Changes:
//
//****************************************************************

function SetTime(ElemObject) {

    ElemObject.time_chk = true;
    ElemObject.last_value = ElemObject.value;
    ElemObject.onblur = function() { CheckAll(this);}

    AddCheckAll(CheckTime);
}


//********************check time function ***********************
//
//Purpose:  Check for valid time entries.  Check for military
//          and/or am and pm
//
//Arguments:automatic
//
//Date:     12/11/06
//Changes:
//
//****************************************************************

function CheckTime(ElemObject) {

     if (ElemObject.time_chk != true) return true;

     var GetValue = FindValues(ElemObject);

     if (GetValue == false && GetValue[0] == undefined) {
        ElemObject.value = ElemObject.last_value;
        ShowErrorAlert(nummsg1,ElemObject);
        return false;
     }

     ThisValue = GetValue[0];

     //check am/pm

     FoundColon = ThisValue.indexOf(":")

     if (FoundColon == -1) {
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(timemsg1,ElemObject);
       return false;
     }

   Time = ThisValue.split(":");
   Hour = Time[0] - 0;
   Minutes = Time[1].substr(0,2);

   if (isNaN(Hour) || isNaN(Minutes-0)) {
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(timemsg1,ElemObject);
       return false;
   }

   AMPM = Time[1].substr(2,2).toLowerCase();

   if (AMPM.length == 2 && (AMPM != "am" && AMPM != "pm")) {
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(timemsg1,ElemObject);
       return false;
   }

   AMPM = ThisValue.toLowerCase();

   FoundAM = AMPM.indexOf("am")
   FoundPM = AMPM.indexOf("pm")

   if (FoundAM != -1 || FoundPM != -1) {
       TimeOK = CheckTwelve(ElemObject);
   } else {
       TimeOK = CheckTwentyFour(ElemObject);
   }

     if (!TimeOK) return false;

   ElemObject.last_value = ElemObject.value;
  if (ErrorHandling == "display") HideError(ElemObject);

  ElemObject.haserror = false;
  ChangeColor(ElemObject);
  return true;
}


//********************SetCompareTime function **************************************************
//  Purpose:  Creates relationship to compare two times
//  Usage,    Pass the object for the first, the id for the second and the operator
//  Date:     05/17/07
//  Changes:
//*****************************************************************************************


function SetCompareTime(BaseObject,CompareID,Operator) {

    CompareObject = FindObjects(CompareID);
    CompareObject =  CompareObject[0];

    BaseObject.compare = CompareObject;
    BaseObject.comp_time = true;
    BaseObject.start_value = BaseObject.value;
    BaseObject.operator = Operator;
    BaseObject.onblur = function() {CheckAll(this);}

    CompareObject.base = BaseObject;
    CompareObject.comp_time = true;
    CompareObject.start_value = CompareObject.value;
    CompareObject.operator = Operator;
    CompareObject.onblur = function() {CheckAll(this);}

    AddCheckAll(CompareTime);
}

//********************CompareTime function **************************************************
//  Purpose:  Compares two times and returns true if relationship passed is ok
//  Usage,    Pass the object to check
//  Date:     05/17/07
//  Changes:
//*****************************************************************************************


function CompareTime(ElemObject) {

   if (ElemObject.comp_time != true) return true;

   if (ElemObject.compare != undefined) {
      BaseObject = ElemObject;
   } else {
      BaseObject = ElemObject.base;
   }

   if (ElemObject.base != undefined) {
      CompObject = ElemObject;
   } else {
      CompObject = ElemObject.compare;
   }

   BaseValue = BaseObject.value.toLowerCase();
   CompValue = CompObject.value.toLowerCase();

   BaseAM = BaseValue.indexOf("am")
   BasePM = BaseValue.indexOf("pm")
   CompAM = CompValue.indexOf("am")
   CompPM = CompValue.indexOf("pm")

   if (BaseAM != -1 || BasePM != -1)  BaseTime = ConvertTo24(BaseValue);
   if (CompAM != -1 || CompPM != -1)  CompTime = ConvertTo24(CompValue);

   if (BaseAM == -1 && BasePM == -1) BaseTime = BaseValue;
   if (CompAM == -1 && CompPM == -1) CompTime = CompValue;

   var BadTime = false;

   BaseString = BaseTime.split(":");
   BaseHour = BaseString[0] - 0;
   BaseMinutes = BaseString[1] - 0;
   CompString = CompTime.split(":");
   CompHour = CompString[0] - 0;
   CompMinutes = CompString[1] - 0;

   CalcBaseTime = BaseHour + (BaseMinutes/60);
   CalcCompTime = CompHour + (CompMinutes/60);

   TimeOK = CompareValues(CalcBaseTime,CalcCompTime,BaseObject.operator);

   if (!TimeOK) {

     if (BaseObject.operator == "eq") BaseObjText = "=";
     if (BaseObject.operator == "gt") BaseObjText = ">";
     if (BaseObject.operator == "lt") BaseObjText = "<";
     if (BaseObject.operator == "ge") BaseObjText = ">=";
     if (BaseObject.operator == "le") BaseObjText = "<=";

     if (CompObject.operator == "eq") CompObjText = "=";
     if (CompObject.operator == "gt") CompObjText = "<";
     if (CompObject.operator == "lt") CompObjText = ">";
     if (CompObject.operator == "ge") CompObjText = "<=";
     if (CompObject.operator == "le") CompObjText = ">=";

     if (ElemObject.compare != undefined) {
       errormsg = SetErrorText(timemsg2,"_BASETIME_|"+BaseValue+"|_COMPARE_|"+BaseObjText+"|_COMPARETIME_|"+CompValue+"");
     }

     if (ElemObject.base != undefined) {
       errormsg = SetErrorText(timemsg3,"_COMPARETIME_|"+CompValue+"|_COMPARE_|"+CompObjText+"|_BASETIME_|"+BaseValue+"");
     }

     ElemObject.value = ElemObject.start_value;
     ShowErrorAlert(errormsg,ElemObject);
     return false;
   }

    ElemObject.start_value = ElemObject.value;
  if (ErrorHandling == "display") HideError(ElemObject);
  ElemObject.haserror = false;
  ChangeColor(ElemObject);
  return true;
}

//********************Set Compare Numeric **************************************************
//  Purpose:  Set up relation between two numeric entries
//  Usage,    Pass the object id/names to check, the type of check (value or length of value)
//            and the relationship (eq,gt,lt,ge,le,ne);
//  Date:     12/08/06
//  Changes:
//*****************************************************************************************


function SetCompareNum(ElemObject,CompareID,Operator) {

    CompareObject = FindObjects(CompareID);
    CompareObject = CompareObject[0];

    ElemObject.comp_num = true;
    ElemObject.compare = CompareObject
    ElemObject.operator = Operator;
    ElemObject.onblur = function() {CheckAll(this);}

    CompareObject.comp_num = true;
    CompareObject.base = ElemObject;
    CompareObject.operator = Operator;
    CompareObject.onblur = function() {CheckAll(this);}

    AddCheckAll(CompareNum);

return true;
}

//********************Compare Num function **************************************************
//  Purpose:  Compares two values.  Returns false if comparison is not true
//  Usage,    Pass the object id/name to check
//  Date:     12/08/06
//  Changes:
//*****************************************************************************************


function CompareNum(ElemObject) {

   if (ElemObject.comp_num != true) return true;

   var BaseObj;

   if (ElemObject.base != undefined) {
       BaseObj = ElemObject.base;
       CompObj = ElemObject;
   }

   if (ElemObject.compare != undefined) {
       BaseObj = ElemObject;
       CompObj = ElemObject.compare;
   }

   if (CompObj.value.length == 0) return true;

   String1 = BaseObj.value;
   String2 = CompObj.value;

   CompObjValue = CompareValues(String1,String2,BaseObj.operator);

   if (!CompObjValue) {

     if (BaseObj.operator == "eq") BaseObjText = "=";
     if (BaseObj.operator == "gt") BaseObjText = ">";
     if (BaseObj.operator == "lt") BaseObjText = "<";
     if (BaseObj.operator == "ge") BaseObjText = ">=";
     if (BaseObj.operator == "le") BaseObjText = "<=";

     if (CompObj.operator == "eq") CompObjText = "=";
     if (CompObj.operator == "gt") CompObjText = "<";
     if (CompObj.operator == "lt") CompObjText = ">";
     if (CompObj.operator == "ge") CompObjText = "<=";
     if (CompObj.operator == "le") CompObjText = ">=";

     if (ElemObject.base != undefined) {
      errormsg = SetErrorText(comnum_msg1,"_VALUE1_|"+String1+"|_COMPARE_|"+BaseObjText+"|_VALUE2_|"+String2+"");
     }

     if (ElemObject.compare != undefined) {
      errormsg = SetErrorText(comnum_msg2,"_VALUE2_|"+String2+"|_COMPARE_|"+CompObjText+"|_VALUE1_|"+String1+"");
     }
     ShowErrorAlert(errormsg,ElemObject);
     return false;
  }
  if (ErrorHandling == "display") HideError(ElemObject);
  ElemObject.haserror = false;
  ChangeColor(ElemObject);
  return true;
}

//********************SetReveal function ***********************
//
//Purpose:  Set up questions on a page based on whether an
//          answer is given to a specific quesiton.  If it is reveal
//          that section of the page.  If it isn't hide it and clear
//          any responses to the inputs provided.
//Arguments:ElementId: Id of base object
//          Parameters:  separated by colon
//              value to check for:
//              relation to check (eq,gt,lt,ge,le,ne allowed):
//              id of container to hide/reveal:
//              name/id of input objects to clear if hidden (can provide as many as you want.
//              separate by comma's
//          Can have multiple parameter options of the value:relation:show:data as needed
//          just surround each with quotes and separate with a comma.
//
//Date:     02/27/07
//Changes:
//
//****************************************************************

function SetReveal(ElementID,DisplayType,Parameters) {

  var ValueArray = new Array();
  var ShowArray = new Array();
  var CompareArray = new Array();

  var QObject = FindObjects(ElementID,true);
  if (DisplayType == undefined || DisplayType == "") DisplayType = "block";

  for (var a = 2;a < SetReveal.arguments.length;a++) {

    var DataArray = new Array();

    ThisOption = SetReveal.arguments[a];
    ThisOption = ThisOption.split(":");
    var CheckValue;

    //if (QObject[0].type == "checkbox" || QObject[0].type == "radio" || QObject[0].type == "select-one" || QObject[0].type == "select-multiple") {
    //  CheckValue = CheckForValues(QObject[0],ThisOption[0]);
    //}

    ValueArray[ValueArray.length] = ThisOption[0];

    ThisOption[1] = ThisOption[1].toLowerCase();

    CompareArray[CompareArray.length] = ThisOption[1];
    ShowName = FindObjects(ThisOption[2]);
    ShowName = ShowName[0];
    ShowArray[ShowArray.length] = ShowName;
    ShowName.data_object;

    for (var b = 3; b < ThisOption.length;b++) {
      ThisDataObject = ThisOption[b];
      ThisDataObject = FindObjects(ThisDataObject);

      if (ThisDataObject !== false) {
        for (var c = 0; c < ThisDataObject.length; c++) {
           ThisObject = ThisDataObject[c];
           DataArray[DataArray.length] = ThisObject;
           if (c == 0) ShowName.data_object = DataArray;
        }
      }
    }
      if (ShowName.data_object == undefined) ShowName.data_object=DataArray;
  }

  for (var a = 0; a < QObject.length;a++) {
    ThisObject = QObject[a];
    ThisObject.values = ValueArray;
    ThisObject.compares = CompareArray;
    ThisObject.reveals = ShowArray;
    ThisObject.display_type = DisplayType;
    if (a==0) RevealArray[RevealArray.length] = ThisObject;
    ThisObject.chk_reveal = true;

    if (ThisObject.type == "text" || ThisObject.type == "textarea") {
      ThisObject.onblur = function() {CheckAll(this);}
    } else {
      ThisObject.onclick = function() {CheckAll(this);}
    }
    //alert(ThisObject.id + " " + ThisObject.values + " " + ThisObject.compares + " " + ThisObject.reveals);
  }

  AddCheckAll(CheckReveal);
  AddOnLoad(LoadReveal);
}

//********************CheckReveal function ***********************
//
//Purpose:  Run check to see if an area on the page should be be
//          revealed.  If so reveal it.  If not then clear the inputs.
//          If cleareded inputs will also run through the function.
//Date:     02/27/07
//Changes:
//
//****************************************************************


function CheckReveal(ElemObject) {

   if (ElemObject.chk_reveal != true) return true;

   ThisValue = FindValues(ElemObject);

   //alert("values to check for " + ElemObject.id + " " + ThisValue + " " + ElemObject.values); 
   //alert(ElemObject.values.length);
   
   for (var a = 0; a < ElemObject.values.length;a++) {

       FindValue = ElemObject.values[a];
       ElemObject.reveals[a].answered = false;

      //alert("looking for " + FindValue);
      
       if (ThisValue === false)  ElemObject.reveals[a].answered = false;

       if (ThisValue !== false) {
        for (var b = 0; b < ThisValue.length; b++) {

          CurrentValue = ThisValue[b];
          CheckValue = CompareValues(CurrentValue,FindValue,ElemObject.compares[a]);

          //alert(FindValue + " " + CurrentValue + " " + CheckValue);

           if (CheckValue && ElemObject.compares[a] != "ne") {
              SetStyle(ElemObject.reveals[a].id,"display:"+ ElemObject.display_type+""); 
              ElemObject.reveals[a].answered = true;
              break;
           }

           if (CheckValue && ElemObject.compares[a] == "ne") {
              ElemObject.reveals[a].answered = true;
           }

           if (!CheckValue && ElemObject.compares[a] == "ne") {
              ElemObject.reveals[a].answered = false;
              break;
           }
        }
       }
       
       //alert(ElemObject.reveals[a].answered);  
 
       if (ElemObject.reveals[a].answered == true && ElemObject.compares[a] == "ne")  {
                  SetStyle(ElemObject.reveals[a].id,"display:"+ ElemObject.display_type+"");
       }


      if (ElemObject.reveals[a].answered == false)  {
         SetStyle(ElemObject.reveals[a].id,"display:none");

        for (var c = 0; c < ElemObject.reveals[a].data_object.length;c++) {
           ThisReveal = ElemObject.reveals[a];
           ThisObject = ThisReveal.data_object[c];

           ClearInputs(ThisObject);
           if (ThisObject.values != undefined) {
             CheckRevealSecondary(ThisObject);
           }
        }
      }
      ElemObject.reveals[a].answered = "";
   }

  if (ErrorHandling == "display") HideError(ElemObject);
  ElemObject.haserror = false;
return true;
}

//********************CheckRevealSecondary function ***********************
//
//Purpose:  Run check to see if an area on the page should be be
//          revealed.  If so reveal it.  If not then clear the inputs.
//Date:     02/27/07
//Changes:
//
//****************************************************************


function CheckRevealSecondary(ElemObject) {

   if (ElemObject.chk_reveal != true) return true;

   ThisValue = FindValues(ElemObject);

   //alert("values to check for " + ElemObject.id + " " + ThisValue + " " + ElemObject.values); 
   //alert(ElemObject.values.length);
   
   for (var a = 0; a < ElemObject.values.length;a++) {

       FindValue = ElemObject.values[a];
       ElemObject.reveals[a].answered = false;

      //alert("looking for " + FindValue);
      
       if (ThisValue === false)  ElemObject.reveals[a].answered = false;

       if (ThisValue !== false) {
        for (var b = 0; b < ThisValue.length; b++) {

          CurrentValue = ThisValue[b];
          CheckValue = CompareValues(CurrentValue,FindValue,ElemObject.compares[a]);

          //alert(FindValue + " " + CurrentValue + " " + CheckValue);

           if (CheckValue && ElemObject.compares[a] != "ne") {
              SetStyle(ElemObject.reveals[a].id,"display:"+ ElemObject.display_type+""); 
              ElemObject.reveals[a].answered = true;
              break;
           }

           if (CheckValue && ElemObject.compares[a] == "ne") {
              ElemObject.reveals[a].answered = true;
           }

           if (!CheckValue && ElemObject.compares[a] == "ne") {
              ElemObject.reveals[a].answered = false;
              break;
           }
        }
       }
       
       //alert(ElemObject.reveals[a].answered);  
 
       if (ElemObject.reveals[a].answered == true && ElemObject.compares[a] == "ne")  {
                  SetStyle(ElemObject.reveals[a].id,"display:"+ ElemObject.display_type+"");
       }

       if (ElemObject.reveals[a].answered == false)  {
          SetStyle(ElemObject.reveals[a].id,"display:none");
       }

      ElemObject.reveals[a].answered = "";
   }

  if (ErrorHandling == "display") HideError(ElemObject);
  ElemObject.haserror = false;
return true;
}



//********************LoadReveal function ***********************
//
//Purpose:  Run check when page load to hide or reveal area of
//          the page based on existing data.
//Date:     02/27/07
//Changes:
//
//****************************************************************


function LoadReveal() {

   for (var a = 0; a < RevealArray.length;a++) {

     ElemObject = RevealArray[a];

     for (var b = 0; b < ElemObject.reveals.length;b++) {
        ThisElemObject = ElemObject.reveals[b];
        SetStyle(ThisElemObject.id,"display:none")
     }

     ThisValue = FindValues(ElemObject);

     //alert("the object is " + ElemObject.id + " current value is "  + ThisValue);

     if (ThisValue === false) continue;

     for (var b = 0; b < ThisValue.length;b++) {
         CurrentValue = ThisValue[b];

      for (var c = 0;c < ElemObject.values.length;c++) {

        //alert("the object is " + ElemObject.id + " The current value is " + CurrentValue+ " Looking for " + ElemObject.values[c] + "  and the comparison is " + ElemObject.compares[c]);

         CheckValue = CompareValues(CurrentValue,ElemObject.values[c],ElemObject.compares[c]);

        if (CheckValue) {
             //alert(ElemObject.reveals[c] + " " + ElemObject.reveals[c].name + " " + ElemObject.reveals[c].id);
             SetStyle(ElemObject.reveals[c].id,"display:"+ ElemObject.display_type+"");
        }
      }
     }
  }
return true;
}

//********************SetFillData function ***********************
//
//Purpose:  Set up so that data from one element can fill in data from another element
//          Or in the case of checkboxes/radio buttons - sets checked to
//          true
//
//Arguments: The object data is filled from
//           The id or string of ID's (separated by a colon)
//Date:     05/17/07
//Changes:
//
//****************************************************************

function SetFillData(ElemObject,FillID) {

    var FillArray = new Array();

    Fillit = FillID.split(":");

    for (var a = 1; a < Fillit.length;a++) {
       ThisID= Fillit[a];

      FillObject = FindObjects(ThisID);
      if (FillObject === false) continue;
      //alert(ThisID + " " + FillObject);
      FillObject = FillObject[0];
      FillObject.fillin = true;
      FillObject.base_obj = ElemObject;
      FillArray[FillArray.length] = FillObject;
      FillObject.fill_obj = FillArray;
    }

    ElemObject.fillin = true;
    ElemObject.fill_obj = FillArray;

    if (ElemObject.type == "checkbox" || ElemObject.type == "radio" || ElemObject.type == "button" || ElemObject.type == undefined) {
       ElemObject.onclick = function() {CheckAll(this);}
    } else {
       ElemObject.onblur = function() {CheckAll(this);}
    }

    AddCheckAll(FillData);

return true;

}

//********************FillData function ***********************
//
//Purpose:  Fill in data for another element from the object passed
//Date:     05/17/07
//Changes:
//
//****************************************************************

function FillData(ElemObject) {

    if (ElemObject.fillin != true) return true;

    if (ElemObject.type == "checkbox"  || ElemObject.type == "radio" || ElemObject.type == "button" || ElemObject.type == undefined) {
       if (ElemObject.checked == true || ElemObject.type == "button" || ElemObject.type == undefined) {
         for (var a = 0; a < ElemObject.fill_obj.length;a++) {
            ThisObject = ElemObject.fill_obj[a];
            if (ThisObject.type == "text" || ThisObject.type == "textarea") {
              FillInputs(ThisObject,ElemObject.value);
            } else {
              FillInputs(ThisObject);
            }
         }
       } else {
         for (var a = 0; a < ElemObject.fill_obj.length;a++) {
       
            ThisObject = ElemObject.fill_obj[a];
            ClearInputs(ThisObject);
         }
       }
     }

     if (ElemObject.type == "select-one" || ElemObject.type == "select-multiple") {

       if (ElemObject.selected == true) {
         for (var a = 0; a < ElemObject.fill_obj.length;a++) {
            ThisObject = ElemObject.fill_obj[a];
            if (ThisObject.type == "text" || ThisObject.type == "textarea") {
              FillInputs(ThisObject,ElemObject.value);
            } else {
              FillInputs(ThisObject);
            }
         }
       } else {
         for (var a = 0; a < ElemObject.fill_obj.length;a++) {
            ThisObject = ElemObject.fill_obj[a];
            ClearInputs(ThisObject);
         }
       }
      }

     if (ElemObject.type == "text" || ElemObject.type == "textarea") {

       if (ElemObject.value.length > 0) {
         for (var a = 0; a < ElemObject.fill_obj.length;a++) {
            ThisObject = ElemObject.fill_obj[a];
            if (ThisObject.type == "text" || ThisObject.type == "textarea") {
              FillInputs(ThisObject,ElemObject.value);
            } else {
              FillInputs(ThisObject);
            }
         }
       } else {
         for (var a = 0; a < ElemObject.fill_obj.length;a++) {
            ThisObject = ElemObject.fill_obj[a];
            ClearInputs(ThisObject);
         }
       }
     }

return true;
}

//********************SetClearData function ***********************
//
//Purpose:  Set up so that all data for an input element(s) is cleared
//
//Arguments: The object that clears the data
//           The id or string of ID's (separated by a colon)
//Date:     08/13/07
//Changes:
//
//****************************************************************

function SetClearData(ElemObject,ClearID) {

    var ClearArray = new Array();

    Clearit = ClearID.split(":");

    for (var a = 1; a < Clearit.length;a++) {
       ThisID= Clearit[a];
       
       ClearObject = FindObjects(ThisID);
       if (ClearObject === false) continue;
       ClearObject = ClearObject[0];
       ClearObject.Clearit = false;
       ClearObject.base_obj = ElemObject;
       ClearArray[ClearArray.length] = ClearObject;
       ClearObject.clear_obj = ClearArray;
     }

     ElemObject.Clearit = true;
     ElemObject.clear_obj = ClearArray;

     if (ElemObject.type == "checkbox" || ElemObject.type == "radio" || ElemObject.type == "button" || ElemObject.type == undefined) {
        ElemObject.onclick = function() {CheckAll(this);}
     } else {
        ElemObject.onblur = function() {CheckAll(this);}
     }

    AddCheckAll(ClearData);

return true;
}

function ClearData(ElemObject) {

    //alert(ElemObject.Clearit + " " + ElemObject.id);

    if (ElemObject.Clearit != true) return true;

    //alert(ElemObject.clear_obj.length);
    if ((ElemObject.type == "radio" || ElemObject.type == "checkbox") && ElemObject.checked == true) return true;
    if ((ElemObject.type == "select-one" || ElemObject.type == "select-multiple") && ElemObject.selected == true) return true;
    if ((ElemObject.type == "text" || ElemObject.type == "textarea") && ElemObject.value.length > 0) return true;
    
     for (var a = 0; a < ElemObject.clear_obj.length;a++) {
         ThisObject = ElemObject.clear_obj[a];
         //alert(ThisObject);
         ClearInputs(ThisObject);
     }
   
   return true;
}

//********************SetSelectOptions function ***********************
//
//Purpose:  Build a link between 2 select inputs. One the base
//          select, the other the select to fill in
//          When Items are selected from the base select
//          they are added to the second select.
//          Assign buttons to add items from one list to another
//          and to remove items from the second select
//
//Arguments: SourceList= the id of the base select
//           DestinationList = the id of the second select
//           AddButton = the id of the button used to add elements
//           RemoveButton = the id of the button used to remove elements
//
//Date:     06/08/07
//Changes:
//
//****************************************************************


function SetSelectOptions(SourceList,DestinationList,AddButton,RemoveButton,RemoveOption,SortOptions) {
    
    if (RemoveOption != false) RemoveOption = true;
    if (SortOptions != false) SortOptions = true;

    SourceObj = FindObjects(SourceList);
    SourceObj = SourceObj[0];
    SourceObj.remove = RemoveOption;
    SourceObj.sort = SortOptions;

    DestinationObj = FindObjects(DestinationList);
    DestinationObj = DestinationObj[0];
    DestinationObj.source = SourceObj;
    DestinationObj.destination = DestinationObj;
    DestinationObj.remove = RemoveOption;
    DestinationObj.sort = SortOptions;
    DestObjArray[DestObjArray.length] = DestinationObj;

    AddButtonObj = FindObjects(AddButton);
    AddButtonObj = AddButtonObj[0];

    RemoveButtonObj = FindObjects(RemoveButton);
    RemoveButtonObj = RemoveButtonObj[0];

    AddButtonObj.source = SourceObj;
    AddButtonObj.destination = DestinationObj;
    AddButtonObj.onclick = function() {AddToSelect(this,"destination");}

    RemoveButtonObj.destination = DestinationObj;
    RemoveButtonObj.onclick = function() {RemoveFromSelect(this,"destination");}

    AddOnSubmit(FillSelectOnSubmit);
}

//********************AddToSelect function ***********************
//
//Purpose:  Add items selected in on select input to a second select input
//
//Arguments: ElemObject - the button clicked
//
//Date:     06/08/07
//Changes:
//
//****************************************************************

function AddToSelect(ElemObject,WhichObjectType) {

        var SelectObj = ElemObject.source;
        var DestObj = ElemObject.destination;
        var ListLength = DestObj.options.length;

        var SelectArray;
        var DestArray;

  
   if (WhichObjectType == "destination") {
        for(var a = 0; a < DestObj.options.length; a++) {
            ThisOptionText = DestObj.options[a].text;
            ThisOptionValue = DestObj.options[a].value;

            if (DestArray == undefined) {
              DestArray = ThisOptionText + ":" + ThisOptionValue;
            } else {
              DestArray += "|" + ThisOptionText + ":" + ThisOptionValue;
            }
        }

        for(var a = 0; a < SelectObj.options.length; a++) {

           if ((SelectObj.options[a] != null) && (SelectObj.options[a].selected)) {

              //Check if this value already exist in the destList or not
              //if not then add it otherwise do not add it.
              var found = false;
              for(var Count = 0; Count < ListLength; Count++) {

                  if (DestObj.options[Count] != null) {
                     if (SelectObj.options[a].text == DestObj.options[Count].text) {
                        found = true;
                        break;
                     }
                  }
              }

             if (found != true) {
               ThisOptionText = SelectObj.options[a].text;
               ThisOptionValue = SelectObj.options[a].value;

               //alert("Text: " + ThisOptionText + " Value: " + ThisOptionValue);

               if (DestArray == undefined) {
                 DestArray = ThisOptionText + ":" + ThisOptionValue;
               } else {
                 DestArray += "|" + ThisOptionText + ":" + ThisOptionValue;
               }
                 //alert("Ending: " + DestArray);
             }
          }
        }

        if (DestObj.options.length > 0) {
            for(var a = (DestObj.options.length-1); a >= 0; a--) {
               DestObj.options[a] = null;
            }
        }

       //alert(DestObj.sort);
       if (DestObj.sort == true) {
          //alert(DestArray);
          if (DestArray.length > 0) SortDestArray = SortSelect(DestArray);
          //alert(SortDestArray); 
          PopulateSelect(DestObj.id,SortDestArray);
       } else {
          //alert(DestArray);
          PopulateSelect(DestObj.id,DestArray); 
       } 
       
       if (DestObj.remove == true) {
         var SourceListLength = SelectObj.options.length;

          for(var a = (SourceListLength-1); a >= 0; a--) {
            if ((SelectObj.options[a] != null) && (SelectObj.options[a].selected == true)) {
                SelectObj.options[a] = null;
            }
         }
       }  
    }

   if (WhichObjectType == "source") {

        var SourceListLength = SelectObj.options.length;

        for(var a = 0; a < SelectObj.options.length; a++) {
            ThisOptionText = SelectObj.options[a].text;
            ThisOptionValue = SelectObj.options[a].value;

            if (SelectArray == undefined) {
              SelectArray = ThisOptionText + ":" + ThisOptionValue;
            } else {
              SelectArray += "|" + ThisOptionText + ":" + ThisOptionValue;
            }
        }

        for(var a = 0; a < DestObj.options.length; a++) {

           if ((DestObj.options[a] != null) && (DestObj.options[a].selected)) {

              //Check if this value already exist in the SelectList or not
              //if not then add it otherwise do not add it.
              var found = false;
              for(var Count = 0; Count < SourceListLength; Count++) {

                  if (SelectObj.options[Count] != null) {
                     if (DestObj.options[a].text == SelectObj.options[Count].text) {
                        found = true;
                        break;
                     }
                  }
              }

             if (found != true) {
               ThisOptionText = DestObj.options[a].text;
               ThisOptionValue = DestObj.options[a].value;

               //alert("Text: " + ThisOptionText + " Value: " + ThisOptionValue);

               if (SelectArray == undefined) {
                 SelectArray = ThisOptionText + ":" + ThisOptionValue;
               } else {
                 SelectArray += "|" + ThisOptionText + ":" + ThisOptionValue;
               }
                 //alert("Ending: " + SelectArray);
                 SourceListLength++;
             }
          }
        }

        if (SelectObj.options.length > 0) {
            for(var a = (SelectObj.options.length-1); a >= 0; a--) {
               SelectObj.options[a] = null;
            }
        }

       if (DestObj.sort == true) {
        if (SelectArray.length > 0) SortSelectArray = SortSelect(SelectArray);
         PopulateSelect(SelectObj.id,SortSelectArray);
       } else {
         PopulateSelect(SelectObj.id,SelectArray);
       }
    }
}


//********************RemoveFromSelect function ***********************
//
//Purpose:  Remove items selected from a select input
//
//Arguments: ElemObject - the button clicked
//
//Date:     06/08/07
//Changes:
//
//****************************************************************

function RemoveFromSelect(ElemObject,WhichObjectType) {

        var SourceObj = ElemObject.source;
        var DestinationObj = ElemObject.destination;

     if (WhichObjectType == "destination") {
        var ListLength = DestinationObj.options.length;

        for(var a = (ListLength-1); a >= 0; a--) {
           if ((DestinationObj.options[a] != null) && (DestinationObj.options[a].selected == true)) {
               if (DestinationObj.remove == true) AddToSelect(DestinationObj,"source");
               DestinationObj.options[a] = null;
           }
        }
     }
}


//********************Sort Select function ***********************
//
//Purpose:  Alpha sort a select list before adding to an existing list
//
//Arguments: OptionList - The list of options to add
//             Needs the text and value (separated by a colon)
//             Needs all the options to add (separated by a comma)
//             SortSelect("option 1:01,option 2:02");
//
//Date:     08/09/07
//Changes:
//
//****************************************************************

function SortSelect(Options) {

   SelectListArray = Options.split("|");
   //alert(SelectListArray);
   SelectListArray.sort();
   //alert(SelectListArray);

   return SelectListArray;

}

//********************Populate Select function ***********************
//
//Purpose:  Add options to a select list
//
//Arguments: ElemID - the id of the select input
//           OptionList - The list of options to add
//           Needs the value and text to use (separated by a colon)
//           Needs all the options to add (separated by a comma)
//           PopulateSelect("id","01:option 1,02:option 2);
//
//Date:     06/08/07
//Changes:
//
//****************************************************************


function PopulateSelect(ElemId,OptionList) {

     ElemObject = FindObjects(ElemId);
     ElemObject = ElemObject[0];
     ListLength = ElemObject.options.length;

     OptionList = OptionList + "";
     //alert(OptionList);
     if (ElemObject.sort == true) var OptionListArray = OptionList.split(",");
     if (ElemObject.sort != true) var OptionListArray = OptionList.split("|");
     
     for (var a = 0; a < OptionListArray.length; a++) {

        ThisOption = OptionListArray[a];
        ThisOptionArray = ThisOption.split(":");
       
         //alert(ThisOptionArray[0] + " " + ThisOptionArray[1]);

        ThisText = ThisOptionArray[0];
        ThisValue = ThisOptionArray[1];

        //alert(ThisText + " " + ThisValue);

        ElemObject.options[ListLength] = new Option(ThisText,ThisValue);
        ListLength++;
     }
}


//********************DePopulate Select function ***********************
//
//Purpose:  Remove options to a select list
//
//Arguments: ElemID - the id of the select input
//           OptionList - The list of options to add
//           Needs the value and text to use (separated by a colon)
//           Needs all the options to add (separated by a comma)
//           DePopulateSelect("id","01:option 1,02:option 2);
//
//Date:     08/09/07
//Changes:
//
//****************************************************************


function DePopulateSelect(ElemId,OptionList) {

     ElemObject = FindObjects(ElemId);
     ElemObject = ElemObject[0];
     ListLength = ElemObject.options.length;

     OptionList = OptionList + "";
     if (ElemObject.sort == true) var OptionListArray = OptionList.split(",");
     if (ElemObject.sort != true) var OptionListArray = OptionList.split("|");


     for (var a = 0; a < OptionListArray.length; a++) {

        ThisOption = OptionListArray[a];
        ThisOptionArray = ThisOption.split(":");

        ThisText = ThisOptionArray[0];
        ThisValue = ThisOptionArray[1];

        //alert(ThisText + " " + ThisValue);

        //Remove objects from the base list when placing them in the DestList

        for (var b = 0; b < ElemObject.options.length; b++) {
          BaseOption = ElemObject.options[b];
          if (BaseOption.text == ThisText) {
             //alert("Base = " + BaseOption.text + " This = " + ThisText);
             ElemObject.options[b] = null;
          }
        }
     }
}


//********************FillSelectOnSubmit function ***********************
//
//Purpose:  Select all items on the destination list on form submit
//
//Arguments: None
//
//Date:     07/18/07
//Changes:
//
//****************************************************************

function FillSelectOnSubmit() {

  for (var a = 0; a < DestObjArray.length; a++) {
       DestObj = DestObjArray[a];
       //alert(DestObj.name);

       for (var b = 0; b < DestObj.options.length; b++) {

          ThisOption = DestObj.options[b];
          //alert(ThisOption);
          ThisOption.selected = true;
       }
  }
}


//********************check twelve function ***********************
//
//Purpose:  Check for valid time entries on a 12 hour clock.
//
//Arguments:automatic
//
//Date:     12/11/06
//Changes:
//
//****************************************************************


function CheckTwelve(ElemObject) {

   var GetValue = FindValues(ElemObject);
   ThisValue = GetValue[0];

   Time = ThisValue.split(":");
   Hour = Time[0] - 0;
   Minutes = Time[1].substr(0,2);

   if (Minutes.length < 2) {
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(chk12msg1,ElemObject);
       return false;
   }

   if (Hour < 1 || Hour > 12) {
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(chk12msg2,ElemObject);
       return false;
   }

   Minutes = Minutes - 0;

   if (Minutes < 0 || Minutes > 59) {
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(chk12msg3,ElemObject);
       return false;
   }
  if (ErrorHandling == "display") HideError(ElemObject);
  ElemObject.haserror = false;
  ChangeColor(ElemObject);
  return true;
}

//********************check twentyfour function ***********************
//
//Purpose:  Check for valid time entries on a 24 hour clock.
//
//Arguments:automatic
//
//Date:     12/11/06
//Changes:
//
//****************************************************************


function CheckTwentyFour(ElemObject) {

   //alert("running check 24");
   var GetValue = FindValues(ElemObject);
   ThisValue = GetValue[0];

   Time = ThisValue.split(":");
   Hour = Time[0];
   Minutes = Time[1].substr(0,2);

   if (Hour.length != 2) {
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(chk24msg1,ElemObject);
       return false;
   }

   if (Minutes.length != 2) {
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(chk24msg2,ElemObject);
       return false;
   }

   Hour = Hour - 0;
   Minutes = Minutes - 0;

   if (Hour < 0 || Hour > 23) {
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(chk24msg3,ElemObject);
       return false;
   }

   if (Minutes < 0 || Minutes > 59) {
       ElemObject.value = ElemObject.last_value;
       ShowErrorAlert(chk24msg4,ElemObject);
       return false;
   }
  if (ErrorHandling == "display") HideError(ElemObject);
  ElemObject.haserror = false;
  ChangeColor(ElemObject);
  return true;
}

//********************convert to 24 function ***********************
//
//Purpose:  Converts times in a 12 hour clock to a 24 hour clock
//
//Arguments:automatic
//
//Date:     12/11/06
//Changes:
//
//****************************************************************


function ConvertTo24(Time) {

   TimeAM = Time.indexOf("am")
   TimePM = Time.indexOf("pm")

   if (TimeAM != -1) {
       ThisTime = Time.substr(0,TimeAM);
       TimeString = ThisTime.split(":");
       ThisHour = TimeString[0];
       ThisMinutes = TimeString[1].substr(0,2);

       if (ThisHour == "12") ThisHour = 0;
       TimeOut = ThisHour + ":" + ThisMinutes;
   }

  if (TimePM != -1) {
      ThisTime = Time.substr(0,TimePM);
      TimeString = ThisTime.split(":");
      ThisHour = TimeString[0];
      ThisMinutes = TimeString[1].substr(0,2);
      if (ThisHour != "12") ThisHour = (ThisHour-0) + 12;
      TimeOut = ThisHour + ":" + ThisMinutes;
  }

  return TimeOut;
}

//********************convert to 12 function ***********************
//
//Purpose:  Converts times in a 24 hour clock to a 12 hour clock
//
//Arguments:automatic
//
//Date:     12/11/06
//Changes:
//
//****************************************************************


function ConvertTo12(Time) {

   TimeString = Time.split(":");
   Hour = TimeString[0] - 0;
   Minutes = TimeString[1];


  if (Hour != 0 && Hour < 12) {
    if (Hour < 10) Hour = "0" + Hour;
    TimeOut = Hour + ":" + Minutes + "am";
    return TimeOut;
  }

  if (Hour == 0) {
    Hour = 12;
    TimeOut = Hour + ":" + Minutes + "am";
    return TimeOut;
  }

  if (Hour == 12) {
    TimeOut = Hour + ":" + Minutes + "pm";
    return TimeOut;
  }

  if (Hour >= 12) {
    Hour = Hour - 12;
    if (Hour < 10) Hour = "0" + Hour;
    TimeOut = Hour + ":" + Minutes + "pm";
    return TimeOut;
  }
}

//********************compare values function ***********************
//
//Purpose:  Pass 2 values and their comparison operator.  Returns true
//          if comparison is true otherwise returns false.
//Date:     10/28/03
//Changes:
//
//****************************************************************


function CompareValues(String1,String2,Operator) { //**start compare function

  String1 = String1;
  String2 = String2;

  if (Operator == "eq") {
    if (String1 == String2) {
        return true;
    } else {
        return false;
     }
  }

  if (Operator == "ne") {
      if (String1 != String2) {
         return true;
      } else {
          return false;
       }
  }

  if (Operator == "gt") {
      if (String1 > String2) {
          return true;
      } else {
          return false;
      }
  }

  if (Operator == "lt") {
      if (String1 < String2) {
          return true;
      } else {
          return false;
      }
  }

  if (Operator == "ge") {
      if (String1 >= String2) {
          return true;
      } else {
          return false;
      }
  }

  if (Operator == "le") {
      if (String1 <= String2) {
         return true;
      } else {
         return false;
      }
  }
  return false;
} //** end compare function


//********************FindValues function ***********************
//
//Purpose:  find and return the current value or values of an object
//          returns an array of values or false if no values present
//Date:     06/20/06
//Changes:
//
//****************************************************************


function FindValues(ElemObject) {

    var ElemValue = new Array();
    
    //alert(ElemObject.id + " " + ElemObject.type);
    if (ElemObject.type == "radio") {
      //alert(ElemObject.id + " " + ElemObject.buttons);
      for (var a = 0; a < ElemObject.buttons.length;a++) {
        ThisObject = ElemObject.buttons[a]
        if (ThisObject.checked == true) ElemValue[ElemValue.length] = ThisObject.value;
      }
    }

    if (ElemObject.type == "select-one" || ElemObject.type == "select-multiple") {
       for (var a = 0; a < ElemObject.options.length; a++) {
         ThisOption = ElemObject.options[a];
         if (ThisOption.selected == true) ElemValue[ElemValue.length] = ThisOption.value;
       }
    }

    if (ElemObject.type == "checkbox") {
      for (var a = 0; a < ElemObject.buttons.length;a++) {
         ThisObject = ElemObject.buttons[a]
         if (ThisObject.checked == true) ElemValue[ElemValue.length] = ThisObject.value;
      }
    }

    if (ElemObject.type == "text" || ElemObject.type =="textarea" || ElemObject.type == "hidden") {
       if (ElemObject.value.length > 0) ElemValue[ElemValue.length] = ElemObject.value;
    }

  //alert(ElemObject.id + " " + ElemValue + " " + ElemValue.length);
 if (ElemValue.length > 0) {
   return ElemValue;
 } else {
   return false;
 }
}


//********************ClearInputs function **************************************************

//  Purpose:  To clear all responses from an input
//  Argument:  The objects to clear
//  Date:     07/07/06
//  Changes:

//************************************************************************************************

function ClearInputs(ElemObject1,ElemObject2) {

  //alert(ElemObject1 + " " + ElemObject1.id);
  for (var a = 0; a < ClearInputs.arguments.length;a++) {

    ThisObject = ClearInputs.arguments[a];

    //alert(ThisObject.id);
   
    if (ThisObject.type == "checkbox" || ThisObject.type == "radio") {
        for (var b = 0; b < ThisObject.buttons.length; b++) {
         ThisObject.buttons[b].checked = false;
        }
    }

    if (ThisObject.type == "select-one") {
       for (var b = 1; b < ThisObject.options.length; b++) {
         thisopt = ThisObject.options[b];
         thisopt.selected = false;
         ThisObject.options[0].selected = true;
       }
    }

    if (ThisObject.type == "text" || ThisObject.type == "textarea") {
        ThisObject.value = "";
    }
 }
}

//********************FillInputs function **************************************************

//  Purpose:  To fill a list of inputs based on another input
//  Argument: ElemObject:  The object to fill in
//            FillValue - the value to fill if text or textarea input
//  Date:     06/07/07
//  Changes:

//************************************************************************************************

function FillInputs(ElemObject,FillValue) {

    ThisObject = ElemObject;

    if (ThisObject.type == "checkbox" || ThisObject.type == "radio") {
        for (var a = 0; a < ThisObject.buttons.length; a++) {
         ThisObject.buttons[a].checked = true;
        }
    }

    if (ThisObject.type == "select-one" || ThisObject.type == "select-multiple") {
       for (var a = 1; a < ThisObject.options.length; a++) {
         thisopt = ThisObject.options[a];
         thisopt.selected = true;
       }
    }

    if (ThisObject.type == "text" || ThisObject.type == "textarea") {
        ThisObject.value = FillValue;
    }
}



//********************chkdec function ***************************
//
//Purpose:  Check the format of a number based on number of decimals passed
//          used by numeric check functions.  Returns false if the wrong number
//          of decimals or true if correct number of decimals
//
//Arguments: ThisValue = the value to check
//           ThisDec = the number of decimals to check for
//
//Date:     10/28/03
//Changes:
//
//****************************************************************


function CheckDecimals(ThisValue,ThisDec) {

  var ChkDec = ThisValue + ".000000000";
  var FixDecArray = ChkDec.split(".");

  if (FixDecArray.length == 3 && ThisDec == 0) {
      return false;     //decimal input but no decimals needed
  }

  if (FixDecArray.length < 3 && ThisDec > 0) {
      return false;     //there were no decimals input
  }

  if (FixDecArray.length == 3) {//there were decimals input; also checks whether we input the required number of decimals
     if (FixDecArray[1].length > ThisDec) {
      return false;
     }
  }
 return true;
}


//********************FormatDecimals function ***************************
//
//Purpose:  properly format a number based on number of decimals passed
//          used by constant sum functions
//Date:     02/22/07
//Changes:
//
//****************************************************************

function FormatDecimals(ThisValue,NumDecs) {//**start FormatDecimals function

    var ChkDec = ThisValue + ".000000000";
    var ChkDecArray = ChkDec.split(".");
    var TextValue = ThisValue;

    if (ChkDecArray.length < 3 && NumDecs == 0) {
       return ThisValue;
    }

    if (NumDecs == 0) {
       if (ChkDecArray[1].substr(0,1) < 5) {
           TextValue = ChkDecArray[0];
       } else {
          TextValue = (ChkDecArray[0] - 0) + 1;
       }
       return TextValue;
    }

    if (NumDecs > 0) {
      if (ChkDecArray.length < 3) {
        TextValue += ".";
        for (var j = 0; j < NumDecs; j++) {//**start numdec loops
          TextValue += "0";
        }  //**end NumDecs loop
          return TextValue;
      } else {
           var NeedZero = NumDecs - ChkDecArray[1].length;

              if (NumDecs == ChkDecArray[1].length)  return TextValue;

              if (NumDecs > ChkDecArray[1].length) {
                 for (var a = 0; a < NeedZero;a++) {
                    TextValue += "0";
                 }
              }

              if (NumDecs < ChkDecArray[1].length) {
                var DecimalValue = ChkDecArray[1];
                var FirstTwoChars = DecimalValue.substr(0,NumDecs);
                var ThirdChar = DecimalValue.substr(NumDecs,1);
                    ThirdChar = ThirdChar - 0;
                    FirstTwoChars = FirstTwoChars - 0

              if (ThirdChar < 5) {
                  FirstTwoChars = FirstTwoChars;
              } else {
                  FirstTwoChars = FirstTwoChars + 1;
              }

              FirstTwoChars = FirstTwoChars + "";

              if (DecimalValue.substr(0,1) == "0") {
                 while (FirstTwoChars.length < NumDecs) {
                    FirstTwoChars = "0" + FirstTwoChars;
                 }
              }

              if (FirstTwoChars.length > NumDecs) {
                ChkDecArray[0] = (ChkDecArray[0] - 0) + 1;
                FirstTwoChars="";
              }

              while (FirstTwoChars.length < NumDecs) {
                FirstTwoChars = FirstTwoChars + "0";
              }

              TextValue = ChkDecArray[0]+"."+FirstTwoChars;
              }
         return TextValue;
      }
    }
    return TextValue;
}  //**end FormatDecimals function

//********************DecimalMath function ***************************
//
//Purpose:  Properly add numbers with decimals. Since the js processor won't.
//
//Arguments: FirstNum = the first number for the equation
//           Operator = the math operator (+,-,*,/)
//           SecondNum = the second number for the equation
//           NumDecs = The number of decimals to return
//Date:     7/18/06
//Changes:
//
//****************************************************************


function DecimalMath(FirstNum,Operator,SecondNum,NumDecs) {

     var Factor = 1;
     var TempNum = 0;
     var FinalValue = 0;

     if (Operator == undefined) Operator = "+";
     if (Operator == "/" && SecondNum == 0) {
       alert("PROGRAMMER ALERT - Something is wrong with the call to DecimalMath as you are trying to divide by 0.  Go back and check your function");
       return false;
     }

     for (var a = 0; a < NumDecs;a++) {
       Factor = Factor * 10;
     }
     
     FirstNum = FirstNum*Factor;
     SecondNum = SecondNum*Factor;
     if (Operator == "+") TempNum = FirstNum + SecondNum;
     if (Operator == "-") TempNum = FirstNum - SecondNum;
     if (Operator == "/") TempNum = FirstNum/SecondNum;
     if (Operator == "*") TempNum = FirstNum*SecondNum;

     //alert(TempNum + " " + Factor);
     if (Operator == "+" || Operator == "-") FinalValue = TempNum/Factor;
     if (Operator == "/") FinalValue = TempNum;
     if (Operator == "*") FinalValue = (TempNum/Factor)/Factor;

     return FinalValue;
}

//********************SetErrorText function ***************************
//
//Purpose:  To parse an error string and make text replacements.
//
//Arguments: MsgText = The text of the message to search
//           replace = The string and the value to replace separated by a :
//           Can pass as many replace arguments as needed.  Separate with a |
//Date:     7/18/06
//Changes:
//
//****************************************************************


function SetErrorText(MsgText,replace) { //**start SetErrorText function

        tempmsg = MsgText;
    
        ReplaceArray = replace.split("|");

        for (var a = 0; a < ReplaceArray.length; a++) {
        
            RepText = ReplaceArray[a];
            RepValue = ReplaceArray[a+1]; 
            RepLength = RepText.length;
            MsgLength = tempmsg.length;

            //alert(RepText + " : " + RepValue + " : " + MsgLength);
            k = tempmsg.indexOf(""+RepText+""); 
            tempmsg = tempmsg.substr(0, k) + RepValue + tempmsg.substr(k+RepLength,MsgLength);
 
           a++;    
        }            

       return tempmsg;
} //**end SetErrorText function



//********************ShowErrorAlert function ***********************
//
//Purpose:  manages error alert and focus placement.
//          A Firefox bug causes the focus not to be placed on the input text box.
//          Calling focus() and select() with a slight delay fixes it.
//          How alerts are handled is controlled with the Global variable ErrorHandling
//
//          normal_alert - uses a standard browser alert
//          custom_alert - uses a div set for the alert - SEE README FILE
//          display - Displays the messages in a div on the page - - SEE README FILE
//
//Arguments: ErrorText - the text error to display
//           ErrorObject - the object with the error
//
//Date:     06/07/07
//Changes:
//
//****************************************************************

function ShowErrorAlert(ErrorText,ErrorObject) {

   //alert(ErrorHandling);

  if (ErrorHandling == "normal_alert") {
    if (window.navigator.userAgent.toLowerCase().match("ie")) {
      error_display = 1;
    } else {
      error_display = 2;
    }

    ThisObject = ErrorObject;

    if (error_display == 1) {
      alert(ErrorText);
      ErrorObject.focus();
      if (ErrorObject.type == "text" || ErrorObject.type=="textarea") {
        ErrorObject.select();
      }
    }

    if (error_display == 2) {
      alert(ErrorText);
      if (ErrorObject.type == "text" || ErrorObject.type=="textarea") {
        erroralert = setTimeout("ThisObject.focus();ThisObject.select();",100);
      } else {
        erroralert = setTimeout("ThisObject.focus();",100);
      }
    }
  }

  if (ErrorHandling == "custom_alert") {
     OpenAlert(ErrorText,ErrorObject);
     ErrorObject.haserror = true;
     if (color_error == true && (ErrorObject.type == "text" || ErrorObject.type=="textarea")) {
         set_color = "backgroundColor:"+cs_error_background_color;
         SetStyle(ErrorObject.id,set_color);
         set_fontcolor = "color:" + cs_error_font_color;
         SetStyle(ErrorObject.id,set_fontcolor);
     }    
  }

  if (ErrorHandling == "display") {

    ErrorObjectId = ErrorObject.id + "_error";
    //var ErrorObject = FindObjects(ErrorObjectId);
    //ErrorObject = ErrorObject[0];

    SetStyle(ErrorObjectId,"display:inline");
    InsertText(ErrorObjectId,ErrorText);
    ErrorObject.focus();
    if (ErrorObject.type == "text" || ErrorObject.type=="textarea")  ErrorObject.select();
  }

  //alert(ErrorObject.id + " " + ErrorObject.container);
  if (ErrorObject != undefined && ErrorObject.container != undefined) {
       SetStyle(ErrorObject.container.id,"display:block");
       ErrorObject.container.opened = true; 
       ErrorObject.container.section.opened = false; 
       ChangeBox(ErrorObject.container.section);
  }     
  
} //**end ShowErrorAlert

//********************HideError function ***********************
//
//Purpose:  Hides the error message if ErrorHandling="display"
//          Call at end of data check functions when there are no errors
//
//Arguments: ElemObject - the object with the error
//
//Date:     06/07/07
//Changes:
//
//****************************************************************


function HideError(ElemObject) {
    ErrorObjectId = ElemObject.id + "_error";
    SetStyle(ErrorObjectId,"display:none");
}

//********************OpenAlert function ***********************
//
//Purpose:  Fills error meesage into hidden div
//          Then displays the div
//
//Arguments: AlertText - the text of the error
//           ElemObject - the object with the error
//
//Date:     06/07/07
//Changes:
//
//****************************************************************

function OpenAlert(ErrorText,ElemObject){

   //alert(ElemObject.id);
   PosTop = findPosY(ElemObject);
   //alert(PosTop);
   NewPosTop = PosTop - 100;

   ThisObject = ElemObject;
   AlertObject = FindObjects("alert_layer");
   AlertObject = AlertObject[0];
   //AlertObject.InputObject = ElemObject;

   InsertText("error_text",ErrorText);
   SetStyle("alert_layer","display:block","top:"+NewPosTop+"px");
}

//********************CloseAlert function ***********************
//
//Purpose:  Closes the alert opened with OpenAlert
//
//Arguments: ElemObject - the object with the error
//
//Date:     06/07/07
//Changes:
//
//****************************************************************

function CloseAlert(ElemObject){

  SetStyle("alert_layer","display:none");

  if (ElemObject.InputObject != undefined && (ElemObject.InputObject.type == "text" || ElemObject.InputObject.type == "textarea")) {
    ElemObject.InputObject.focus();
    ElemObject.InputObject.select();
  }
}

//********************ConfirmAction function ***********************
//
//Purpose:  Open a browser confirm with the text passed
//          returns true if ok, false if not
//
//Arguments: AlertText - the text of the message
//
//Date:     06/07/07
//Changes:
//
//****************************************************************

function confirmAction(AlertText){
  var msg = AlertText;
  if(confirm(msg)) {
    return true;
  }else{
    return false;
  }
}

//********************ChangeColor function ***********************
//
//Purpose:  Reverts inputs to original color after a corrected error.
//
//Arguments: Object to change
//
//Date:     07/10/08
//Changes:
//
//****************************************************************


function ChangeColor(ElemObject) {
    
    if (color_error == true && (ElemObject.type == "text" || ElemObject.type=="textarea")) {
      SetStyle(ElemObject.id,"backgroundColor:"+restore_error_background_color);
      SetStyle(ElemObject.id,"color:"+restore_error_font_color);      
    }
}    

//********************SetContainer function ***********************
//
//Purpose:  Sets link between a container and the inputs inside it.
//          For opening containers when an error occurs
//
//Arguments: ElemObject: Container Object
//           InputArray:  Array of input Id's
//
//Date:     07/10/08
//Changes:
//
//****************************************************************

function SetContainer(ElemObject,InputArray) {

   var InputObjectArray = new Array();
   
   for (var a = 0; a < InputArray.length; a++) {
      ThisArrayId = InputArray[a];

      //alert(ThisArrayId);      
      ThisArrayElement = FindObjects(ThisArrayId);
      ThisArrayElement = ThisArrayElement[0];
      ThisArrayElement.container = ElemObject;
      InputObjectArray[InputObjectArray.length] = ThisArrayElement;
      ElemObject.inputs = InputObjectArray;
   }
}   
      
//********************CheckAllBoxes function ***********************
//
//Purpose:  Check all checkboxes associated with the name passed.
//
//Arguments: ElemName: Name of checkbox objects
//
//Date:     08/05/08
//Changes:
//
//****************************************************************

function CheckAllBoxes(ElemName) {

   var CurrentBox = FindObjects(ElemName,true);
   CurrentBox = CurrentBox[0];
   
   for (var a = 0; a < CurrentBox.buttons.length; a++) {
   
       ThisBox = CurrentBox.buttons[a];
       ThisBox.checked = true;
   }    
}   
   
//********************UnCheckAllBoxes function ***********************
//
//Purpose:  UnCheck all checkboxes associated with the name passed.
//
//Arguments: ElemObject: Name of checkbox objects
//
//Date:     08/05/08
//Changes:
//
//****************************************************************

function UnCheckAllBoxes(ElemName) {

   var CurrentBox = FindObjects(ElemName,true);
   CurrentBox = CurrentBox[0];
   
   for (var a = 0; a < CurrentBox.buttons.length; a++) {
   
       ThisBox = CurrentBox.buttons[a];
       ThisBox.checked = false;
   }    
}   
   
//********************SetCheckBoxtoRadio function ***********************
//
//Purpose:  To make checkboxes behave as radio buttons (single response)
//
//Arguments: ElemObject: Name of checkbox objects
//
//Date:     01/13/09
//Changes:
//
//****************************************************************

function SetCheckBoxtoRadio(ElemName) {

   var CurrentObj = FindObjects(ElemName,true);
   
   for (var a = 0; a < CurrentObj.length; a++) {
       ThisBox = CurrentObj[a];
       ThisBox.chk_radio = true;
       ThisBox.onclick = function() {CheckAll(this);}
   }    
  AddCheckAll(CheckBoxtoRadio);
}   

   
//********************CheckBoxtoRadio function ***********************
//
//Purpose:  To make checkboxes behave as radio buttons (single response)
//
//Arguments: ElemObject: Name of checkbox objects
//
//Date:     01/13/09
//Changes:
//
//****************************************************************

function CheckBoxtoRadio(ElemObj) {

   if (ElemObj.chk_radio != true) return true; 

   for (var a = 0; a < ElemObj.buttons.length; a++) {
       ThisBox = ElemObj.buttons[a];
       if (ThisBox.id != ElemObj.id) ThisBox.checked = false;
   }    
   return true;
}   