/**
 *
 *  Script to dynamically load state or provence options based
 *  on selection of region.
 *
 *  Author: Thomas Nicolosi
 *  Red Bus Corp.
 *  Rev. Date: June 6, 2008
 *  contact: thomas@redbuscorp.com
 *
 *  Current Rev. 4.0.0.0 6/6/2008
 *  Version 4 accommodates cases where javascript is disabled by having
 *  state and provence values coded in the html and then removed onload.
 *  Removed items specific to questionairre from Rev. 3.
 *
 *  Revision History:
 *  Version 3 is customized for event and contact registration.
 *  Rev. 3.0.0.1 5/8/2008 - Corrected spelling error in list of states.
 *
 *
 */
 addLoadEvent(prepareEvents);
 addLoadEvent(prepareHiddenContent);


function regionTest(){
         var locations = locationArray();
         var thisLocation = locations[0];
         var region = thisLocation[0];
         var state = thisLocation[1];
         var messageId = thisLocation[2];
         checkRegion(region, state, messageId);
         }
function regionTestSite(){
         var locations = locationArray();
         var thisLocation = locations[1];
         var region = thisLocation[0];
         var state = thisLocation[1];
         var messageId = thisLocation[2];
         checkRegion(region, state, messageId);
         }
function locationArray(){
         /**
             0 => region || changed to "country"
             1 => state
             2 => messageId
           */
         var location0 = new Array(
                                  "country",
                                  "state",
                                  "validation-message"
                                  );
         var location1 = new Array(
                                  "site-location-region",
                                  "site-location-state",
                                  "site-validation-message"
                                  );
         var locationSum = new Array(
                                     location0,
                                     location1
                                     );
         return locationSum;
         }
function checkRegion(regionId, stateId, messageId){
         var regionSelect = document.getElementById(regionId);
         var regionText = regionSelect.value;
         if(regionText == "United States" || regionText == "Canada"){
                       resetState(stateId);
                         if(regionText == "United States"){
                             loadStates = usStates();
                             loadOptions(loadStates,stateId);
                             }
                         if(regionText == "Canada"){
                             loadStates = caStates();
                             loadOptions(loadStates,stateId);
                             }
                       }else {
                         resetState(stateId, messageId);
                         writeMessage(stateId, messageId);
                       }
         }
function loadOptions(stateArray,stateId){
         var stateSelect = document.getElementById(stateId);
         var optionMessage = "";
         var optionText = new Array();
         var newOption = new Array();
         var j = 0;
         for(j = 0; j < stateArray.length; j++){
             optionMessage = stateArray[j];
             optionText[j] = document.createTextNode(optionMessage.toString());
             newOption[j] = document.createElement("option");
             newOption[j].appendChild(optionText[j]);
             newOption[j].setAttribute("value",optionMessage);
             stateSelect.appendChild(newOption[j]);
             stateSelect.style.width = "180px";
             stateSelect.style.color = "Black";
             }
         }
function caStates(){
         var provences = new Array(
                               "Alberta",
                               "British Columbia",
                               "Manitoba",
                               "Newfoundland and Labrador",
                               "New Brunswick",
                               "Nova Scotia",
                               "Ontario",
                               "Prince Edward Island",
                               "Quebec",
                               "Saskatchewan"
                               );
         return provences;
         }
function usStates(){
         var states = new Array(
                                "Alabama",
                                "Alaska",
                                "Arizona",
                                "Arkansas",
                                "California",
                                "Colorado",
                                "Connecticut",
                                "Delaware",
                                "Florida",
                                "Georgia",
                                "Hawaii",
                                "Idaho",
                                "Illinois",
                                "Indiana",
                                "Iowa",
                                "Kansas",
                                "Kentucky",
                                "Louisiana",
                                "Maine",
                                "Maryland",
                                "Massachusetts",
                                "Michigan",
                                "Minnesota",
                                "Mississippi",
                                "Missouri",
                                "Montana",
                                "Nebraska",
                                "Nevada",
                                "New Hampshire",
                                "New Jersey",
                                "New Mexico",
                                "New York",
                                "North Carolina",
                                "North Dakota",
                                "Ohio",
                                "Oklahoma",
                                "Oregon",
                                "Pennsylvania",
                                "Rhode Island",
                                "South Carolina",
                                "South Dakota",
                                "Tennessee",
                                "Texas",
                                "Utah",
                                "Vermont",
                                "Virginia",
                                "Washington",
								"Washington, DC",
                                "West Virginia",
                                "Wisconsin",
                                "Wyoming"
                                );
         return states;
         }

function resetState(stateId,messageId){
         var stateSelect = document.getElementById(stateId);
         if(!stateSelect) return false;
         if(stateSelect.hasChildNodes){
                 var stateSelectChildren = stateSelect.childNodes;
                 var stateLength = stateSelectChildren.length;
                 var i = 0;
                 for (i = 0; i < stateLength; i++)
                 {
                      stateSelect.removeChild(stateSelect.lastChild);
                 }
                 var newOption = document.createElement("option");
                 newOption.setAttribute("id",messageId);
                 stateSelect.appendChild(newOption);
                 stateSelect.style.width = "180px";
                 }
         }
function writeMessage(stateId,messageId){
         var stateMessage = document.getElementById(messageId);
         var message = document.createTextNode("not applicable");
         stateMessage.appendChild(message);
         stateMessage.setAttribute("value",null);
         var stateSelect = document.getElementById(stateId);
         stateSelect.style.color = "Red";
         }
/*
 *
 * hideStates() removes the states and provences from the html code onload if
 * javaScript is enabled.
 *
 */
function hideStates(){
  var selectElement = document.getElementById("state");
  var i = 0;
  var theLength = selectElement.childNodes.length
  for(i = 0; i < theLength; i++)
    {
         selectElement.removeChild(selectElement.lastChild);
    }
}
/*
 * 
 * requestorInfo() returns the ids of the inputs related to the training
 * requestor.
 *
 */
function requestorInfo(){
  var requestorContact = new Array(
                                   "Requestor_FullName",
                                   "Requestor_Badge",
                                   "Requestor_Location",
                                   "Requestor_Mgr_Email"
                                   );
  return requestorContact;
}
/*
 * Disables the requestor information inputs.
 *
 */
function disableRequestorContacts(){
  var theInputs = requestorInfo();
  var howMany = theInputs.length;
  var theDisableInput;
  var i = 0;
  for (i = 0; i < howMany; i++)
  {
    theDisableInput = document.getElementById(theInputs[i]);
    if(theDisableInput)
    {
      theDisableInput.disabled = true;
      theDisableInput.style.backgroundColor = "silver";
    }
  }
}
/**
 *
 * locationAddress() returns the ids of the inputs
 * that are disabled
 *
 */
function locationAddress(){
  var locationItems = new Array(
                                "ConferenceRoom_LocationName",
                                "ConferenceRoom_Address_Street",
                                "ConferenceRoom_Address_Street2",
                                "ConferenceRoom_Address_City",
                                "ConferenceRoom_Address_State",
                                "ConferenceRoom_Address_Zip"
                                );
   return locationItems;
}
/**
 *
 * locationDisable() disables the location related input elements.
 *
 */
function locationDisable(){
  var theInputs = locationAddress();
  var howMany = theInputs.length;
  var theDisableInput;
  var i = 0;
  for (i = 0; i < howMany; i++)
  {
    theDisableInput = document.getElementById(theInputs[i]);
    if(theDisableInput)
    {
      theDisableInput.disabled = true;
      theDisableInput.style.backgroundColor = "silver";
    }
  }
}
/**
 *
 * locationEnable() enables the location related elements.
 *
 */
function locationEnable(){
       var theInputs = locationAddress(); //get the ids of the inputs to enable
       var theInput;
       var i = 0;
       for (i = 0; i < theInputs.length;i++)
       {
         theInput = document.getElementById(theInputs[i]);
         theInput.disabled = false;
         theInput.style.backgroundColor = "white";
       }
}
 /**
  *  Prepares elements that are either hidden of disabled.
  */
 function  prepareHiddenContent(){
   if(!document.getElementById) return false;
   disableRequestorContacts();
   hideStates();
   locationDisable();
 }
 /**
  * Set action for requestor.
  */
 function requestInput(){
   var theSelectList = document.getElementById("Requestor_Type");
   var theItems = requestorInfo();
   var i = 0;
   var theItem;
   if (theSelectList.value === "" || theSelectList.value === "Customer")
     {
        for (i = 0; i < theItems.length; i++)
        {
          theItem = document.getElementById(theItems[i]);
          theItem.disabled = true;
          theItem.style.backgroundColor = "silver";
        }
     }
     else
     {
        for (i = 0; i < theItems.length; i++)
        {
           theItem = document.getElementById(theItems[i]);
           theItem.disabled = false;
           theItem.style.backgroundColor = "white";
        }
     }
 }
 /**
  * Set action for training.
  */
  function trainingOption(){
    var theTrainingLocation = document.getElementById("Training_Location");
    var selectLocation = document.getElementById("Conference_Location_Same");
    if (theTrainingLocation.value === "Powerwave")
    {
       selectLocation.disabled = true;
       selectLocation.style.backgroundColor = "silver";
       locationDisable();
    }
    else
    {
       selectLocation.disabled = false;
       selectLocation.style.backgroundColor = "white";
       locationEnable();
       trainingLocationAddress();
    }
  }
 /**
  * Set action for training location address
  */
  function trainingLocationAddress(){
    var theTrainingLocation = document.getElementById("Conference_Location_Same");
    if (theTrainingLocation.value === "Yes")
    {
       locationDisable(); //disable the inputs for adding location info
    }
    else
    {
       locationEnable(); //enable the inputs for adding location info.
    }
  }
 /**
  * Adds in the onclick, onchange, and onsubmit event handlers.
  */
 function prepareEvents(){
   if(!document.getElementById) return false;

   //Prepare Reset button.
   var resetButton = document.getElementById("reset-button");
   if(resetButton) resetButton.onclick =  resetFormAll;

   //Prepare form for validation.
   var theForm = document.getElementById("request-form");
   if(theForm) theForm.onsubmit = validateForm;

   //Prepare region input.
   var regionSelect = document.getElementById("country");
   if(regionSelect) regionSelect.onchange = regionTest;
   
   //Prepare requestor information questions.
   var requestorQuestion = document.getElementById("Requestor_Type");
   if(requestorQuestion) requestorQuestion.onchange = requestInput;
   
   //Prepare training location question.
   var trainingLocation = document.getElementById("Training_Location");
   if (trainingLocation) trainingLocation.onchange = trainingOption;
   
   //Prepare training location address action.
   var trainingAddress = document.getElementById("Conference_Location_Same");
   if (trainingAddress) trainingAddress.onchange = trainingLocationAddress;
   
   var theForm = document.forms[0];
   if(theForm) theForm.onsubmit = validateForm;

 }
 /**
  *Resets the form elements
  */
function requestInputReset(){
   var theSelectList = document.getElementById("Requestor_Type");
   var theItems = requestorInfo();
   var i = 0;
   var theItem;
    for (i = 0; i < theItems.length; i++)
    {
      theItem = document.getElementById(theItems[i]);
      theItem.disabled = true;
      theItem.style.backgroundColor = "silver";
    }
}
function resetForm(){
         var locations = locationArray();
         var thisLocation = locations[0];
         var state = thisLocation[1];
         var messageId = thisLocation[2];
         resetState(state, messageId);
         }
function resetFormAll(){
         var locations = locationArray();
         var i;
         var thisLocation;
         var state;
         var messageId;
         for(i = 0; i < locations.length; i++){
               thisLocation = locations[i];
               state = thisLocation[1];
               messageId = thisLocation[2];
               resetState(state, messageId);
               }
         locationDisable();
         requestInputReset();
}
  /**
   *
   *  Adds functions to window.onload.
   *
   */
function addLoadEvent(func){
var oldonload = window.onload;
  if(typeof window.onload != 'function'){
    window.onload = func;
  } else {
    window.onload = function(){
      oldonload();
      func();
    }
  }
}
