
/**********************************************************************************************************************************************

	****************************************
	Author: Belsien Thomas
	Date: 6/22/06
	****************************************

	Description: Java Script functions that use AJAX (Asynchronus JavaScript and XML) to pull in dynamic values for dropdown / <select> objects from XML files on webservers.
	These functions will pull data from XML files and dynamically insert the text values and id's into the <select> object using these functions. Goal is to save resources on the Database
	server by not having to query everytime the page loads and still have the data be dynmically generated through XML files.
	
	****************************************
	
	//example of XML structure, for a file, (i.e. HOW_DID_YOU_HEAR_ABOUT_MURAD.xml)
	
		<muradoptions>

			<option>						
			  	<description>TV</description> 
			  	<value>100000</value> 
			</option>
				
			<option>
				<description>Adveristment</description>
				<value>200000</value>
			</option>
					
			.
			..
		   	... more <option> elements
			..
			.						
	   
		</muradoptions>	


*************************************************************************************************************************************************/


function getoptions( xmlDoc, selectId ) {

	  //get the correct XML document, reassign so you can retry the function below
	  var xmlFile = xmlDoc;	  
		 
		
	  var mozillaFlag = false;
      var XMLHttpRequestObject = false; 
	  
	  //use this to append to the XML file so that browsers dont cache the xml file!!!
	  var uniqueDate = new Date();
	 

      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
		mozillaFlag = true;
        //XMLHttpRequestObject.overrideMimeType("text/xml");
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
      }
	  
	   
        if(XMLHttpRequestObject) {
          
		  XMLHttpRequestObject.open("GET", "/murad/xml/" + xmlFile + "?t=" + uniqueDate.getTime(), true); 
		  //XMLHttpRequestObject.open("GET", "murad/xml/MOST_IMPORTANT_PURCHASING_FACTOR.xml" , true); 

		  //the onreadystatechange event will fire as each stage of loading occurs! (0 - 4) 
          XMLHttpRequestObject.onreadystatechange = function() 
          { 
		   
            if (XMLHttpRequestObject.readyState == 4) {
			
				if(XMLHttpRequestObject.status == 200) { 
				
				 				 
	            var xmlDocument = XMLHttpRequestObject.responseXML;
				
								 
				 //remove whitespaces for Mozilla browsers
				 if(mozillaFlag){				 	
             	 	removeWhitespace(xmlDocument);
            		}				
								
				 
 				 //Gets all the individual <option> elements in one container, loop through this to get a <description> and <value> 
    	         var optionsContainer = xmlDocument.getElementsByTagName("option");
				 
			 	 //get the HTML select object / dropdown box
				 var selectControl = document.getElementById(selectId);
				 
				 //clear the old options in the list
				 selectControl.options.length=0;

				 for(i=0; i < optionsContainer.length; i++) {
				 		
					   //iterate through the array of <option> elements in the container, the <description> element is always the firstChild
	   				   var descriptionNode = optionsContainer[i].firstChild;
					   var valueNode = descriptionNode.nextSibling;
	
					   //create the new HTML option elements in the webpage
					   selectControl.options[i]= new Option(descriptionNode.firstChild.nodeValue); //use .nodeValue or firefox wont work
					   selectControl.options[i].value = valueNode.firstChild.nodeValue;
					   
				 
					} //end for loop
	  
		      }//end if  status = 200 etc..
			 
			   else {			 
			 
			   //if it didn't get loaded, try again
			   getoptions(xmlFile, selectId)
			 	
			      } 
			 
			 } //end if readySate=4
			 

           }//end anonymous function
			
		  XMLHttpRequestObject.send(null); 
			
        } //end if XMLhttpRequestObjecct 

          

     }//end getoptions function
     
	  
function removeWhitespace(xml) {
        var loopIndex;

        for (loopIndex = 0; loopIndex < xml.childNodes.length; 
          loopIndex++) {

          var currentNode = xml.childNodes[loopIndex];

          if (currentNode.nodeType == 1) {
            removeWhitespace(currentNode);
          }

          if (((/^\s+$/.test(currentNode.nodeValue))) &&   
            (currentNode.nodeType == 3)) {
              xml.removeChild(xml.childNodes[loopIndex--]);
          }
        }
}
	  
	  



function showValues(selectId) {

	var newDropDown = document.getElementById(selectId);
	
	for( i = 0; i < newDropDown.options.length; i++) 
		alert("Option: " + newDropDown.options[i].text + " == " + newDropDown.options[i].value);
		
	return false;

}




function returnValues(xmlDoc) {

	  var IDs = new Array();
	  
	  var xmlFile = xmlDoc;	  	 
		
	  var mozillaFlag = false;
      var XMLHttpRequestObject = false; 
	  
	  //use this to append to the XML file so that browsers dont cache the xml file!!!
	  var uniqueDate = new Date();
	 

      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
		mozillaFlag = true;
        //XMLHttpRequestObject.overrideMimeType("text/xml");
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
      }
	  
	   
        if(XMLHttpRequestObject) {
          
		  XMLHttpRequestObject.open("GET", "/murad/xml/" + xmlFile + "?t=" + uniqueDate.getTime(), true); 

		  //the onreadystatechange event will fire as each stage of loading occurs! (0 - 4) 
          XMLHttpRequestObject.onreadystatechange = function() 
          { 
		   
            if (XMLHttpRequestObject.readyState == 4) {
			
				if(XMLHttpRequestObject.status == 200) { 
				
				 				 
	            var xmlDocument = XMLHttpRequestObject.responseXML;
				
								 
				 //remove whitespaces for Mozilla browsers
				 if(mozillaFlag){				 	
             	 	removeWhitespace(xmlDocument);
            		}				
								
				 
 				 //Gets all the individual <option> elements in one container, loop through this to get a <description> and <value> 
    	         var optionsContainer = xmlDocument.getElementsByTagName("option");

				 for(i=0; i < optionsContainer.length; i++) {
				 		
					   //iterate through the array of <option> elements in the container, the <description> element is always the firstChild
	   				   var descriptionNode = optionsContainer[i].firstChild;
					   var valueNode = descriptionNode.nextSibling;
					   
					   IDs[i] = valueNode.firstChild.nodeValue;
					   
				 
					} //end for loop
	  
		      }//end if  status = 200 etc..
			 
			   else {
			   		
			   //if it didn't get loaded, try again
			   returnValues(xmlDoc);
			  
			      } 
			 
			 } //end if readySate=4
			 

           }//end anonymous function
			
		  XMLHttpRequestObject.send(null); 		  
		  
			
      } //end if XMLhttpRequestObjecct 
	  
	  return IDs;


} //end returnValues function










