/**
 *
 *  Script to dynamically load state or provence options based
 *  on selection of region.
 *
 *  Author: Thomas Nicolosi
 *  Red Bus Corp.
 *  Date: Mar. 25, 2008
 *  contact: thomas@redbuscorp.com
 *
 *  Current Revision 2.4.0
 *  Revision Date: 2008-09-03
 *  Summary of changes:
 *  1) Removed code for conditional loading conditional campaign ID.
 *
 *  Revision:  2.3.0 Revision Date: 2008-08-21
 *  Summary of changes:
 *  1) Removed site-location-region related code.
 *
 *  Revision: 2.2.0.0 Date: 2008-06-30
 *
 *  Revision History:
 *  2.2.0.0  addLoadEvent(setCampaignId) for campaignId.js added
 *
 *
 */
 addLoadEvent(prepareEvents);
 //addLoadEvent(idFromQuery);
 /**
  * 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("region");
   if(regionSelect) regionSelect.onchange = regionTest;
 }

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 locationArray(){
         /**
             0 => region
             1 => state
             2 => messageId
           */
         var location0 = new Array(
                                  "region",
                                  "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 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);
               }
         }
function resetState(stateId,messageId){
         var stateSelect = document.getElementById(stateId);
         if(!stateSelect) return false;
         if(stateSelect.hasChildNodes){
                 var stateSelectChildren = stateSelect.childNodes;
                 var stateLength = stateSelectChildren.length;
                 for(var i = stateLength - 1; i > 0; i--){
                         if(stateSelectChildren[i].nodeType == 1){
                             stateSelect.removeChild(stateSelectChildren[i]);
                             }
                     }
                 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";
         }
  /**
   * 
   *  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();
    }
  }
}
