var GE_contact_form = new function () {
 
   //declare all the class member variables
   this.request_hash = location.search.toQueryParams();
   this.curPage = this.request_hash["page"];
   this.contextSubject = this.request_hash["subject"];
   this.contextArea = this.request_hash["area"];
   this.contactType = this.request_hash["contact_type"];
   this.aoiBlock; //div which holds aoi_label and aoi_select_box
   this.subjectSelect; //extended subject select object
   this.aoiSelect; //extended area of interest select object
   /*this.faq; //faq wrapper div*/
   this.contactComments; //comments text area
   this.optgroups = new Array();
   this.aoi_selects = new Array(); //to be initialized after they are created
   this.ps_select; //product and services area of interest select box
   
   //initalize the contact form class
   this.initialize = function() {
      //initialize the appropriate variables
      this.subjectSelect = $("contact_subject");
      this.aoiSelect = $("contact_area_of_interest");
      this.contactComments = $("contact_comments");
      this.optgroups = this.aoiSelect.immediateDescendants();
      this.aoiBlock = $("aoi_block");
      /*this.faq = $("faq");*/
      
      //set the contact type radio button
      if(this.contactType == "consumer"){
         $("contact_type_consumer").checked = true;
      } else if(this.contactType == "press"){
         $("contact_type_press").checked = true;
      } else if(this.contactType == "investor"){
         $("contact_type_investor").checked = true;
      }
      
      //hide the inital select
      this.aoiSelect.hide();
	  $('ps_note_block').hide();
      this.aoiSelect.setAttribute("id","");
      
      //create one area of interest select for each subject
      for(var i=0;i<this.optgroups.length;i++){
         var cur_aoi_options = this.optgroups[i].immediateDescendants();
         var cur_aoi_select = document.createElement("select");
         cur_aoi_select.setAttribute("name","area_of_interest");
         var default_option = document.createElement("option");
         default_option.setAttribute("value","NULL");
         default_option.innerHTML = "Select One...";
         cur_aoi_select.appendChild(default_option);
         for(var j=0;j<cur_aoi_options.length;j++){
            cur_aoi_select.appendChild(cur_aoi_options[j]);
         }
         this.aoiBlock.appendChild(cur_aoi_select);
         this.aoi_selects.push(cur_aoi_select);
         cur_aoi_select.selectedIndex = 0;
         this.optgroups[i].remove();
      }
      
      //hide all of the aoi_select boxes which were created
      this.hide_aoi_selects();

	  //set the ps_select variable
      this.ps_select = this.aoi_selects[4];

      //show the appropriate subject and aoi based on request_url
      if(this.contextSubject > 0 && this.contextSubject < 7){
         this.subjectSelect.selectedIndex = this.contextSubject;
         this.show_aoi(this.contextSubject-1);
         if(this.contextArea > -1 && this.contextArea < this.aoiSelect.immediateDescendants().length){
            this.aoiSelect.selectedIndex = this.contextArea;
         }
      }
      
      //populate the hidden form fields
      var myDate = new Date();
      $("user_browser").value = navigator.appName;
      $("user_os").value = navigator.platform;
      $("user_timestamp").value = myDate.toUTCString(myDate.getTime())
      $("user_url").value = document.referrer;
      
      //add the onchange event listener to the subject_select
       Event.observe(this.subjectSelect,"change", function() {
		  this.hide_ps_note();
          this.hide_aoi_selects();
          this.show_aoi(this.subjectSelect.selectedIndex-1)
       }.bind(this));

	   Event.observe(this.ps_select,"change", function() {
		  if(this.ps_select.value == "Appliances") {
		  	 this.show_ps_note(1);
		  } else  if(this.ps_select.value == "Lighting"){
			 this.show_ps_note(2);
		  } else {
			 this.hide_ps_note();
		  }
	   }.bind(this));
   }
   
   //hide all the aoi selects
   this.hide_aoi_selects = function () {
      for(var i=0;i<this.aoi_selects.length;i++){
		 /*this.faq.hide();*/
         this.aoiBlock.hide()
         Element.hide(this.aoi_selects[i]);
         this.aoi_selects[i].disabled = true;
         this.aoi_selects[i].setAttribute("id","");
      }
   }
   
   //show the appropriate aoi select
   this.show_aoi = function (curSubject) {
      if(curSubject>-1 && curSubject<6){
		 /*if(curSubject==4){ this.faq.show(); }*/
         this.aoiBlock.show();
         this.aoi_selects[curSubject].show();
         this.aoi_selects[curSubject].disabled = false;
         this.aoi_selects[curSubject].setAttribute("id","contact_area_of_interest");
      } else {
         this.aoi_selects[6].disabled = false;
         this.aoi_selects[6].setAttribute("id","contact_area_of_interest");
      }
      //re-initialize the aoiSelect object (because the id has been transferred to the new one)
      this.aoiSelect = $("contact_area_of_interest");
   }

   //show special note for the special items
	this.show_ps_note = function (key) {
		if(key==1){
			$('ps_note1').show();
			$('ps_note2').hide();
		} else {
			$('ps_note1').hide();
			$('ps_note2').show();
		}
		$('ps_note_block').show();
	}

   //show special note for the special items
   this.hide_ps_note = function () {
      $('ps_note_block').hide();    
   }
}

//call the initialize function
GE_Main.addInitFn(
   function() {
      GE_contact_form.initialize();
   }
);