
// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function process(boxid,entitytype,cat,cat3,region,city,date)
{
 
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {    
    
  	if(boxid=='city'){
  	
	  	// retrieve the name typed by the user on the form
	    newregion = document.getElementById("region").value;
	   		
	    if(newregion!='all'){	    	
		    // execute the quickstart.php page from the server
		    xmlHttp.open("GET", "/cms/cms.php?cmspage=../ajax/selectboxes.php&newregion=" + newregion + "&entitytype=" + entitytype + "&cat=" + cat + "&cat3=" + cat3 + "&region=" + region + "&city=" + city + "&date=" + date, true);  
		    // define the method to handle server responses
		    xmlHttp.onreadystatechange = handleServerResponse;
		    // make the server request
		    xmlHttp.send(null);
		    
	    
	    } else {
	    	 document.getElementById("ajax" + boxid).style.display = 'none' ;
	    }
  	 } else if(boxid=='cat'){
  		
	  	// retrieve the name typed by the user on the form
	    newentitytype = document.getElementById("entitytype").value;
   		 
		// execute the quickstart.php page from the server
		xmlHttp.open("GET", "/cms/cms.php?cmspage=../ajax/selectboxes.php&newentitytype=" + newentitytype + "&entitytype=" + entitytype + "&cat=" + cat + "&cat3=" + cat3 + "&region=" + region + "&city=" + city + "&date=" + date, true);  
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleServerResponse;
		// make the server request
		xmlHttp.send(null);	    
		
		if(newentitytype!='e'){	
			    		       
	    	if(document.getElementById('ajaxdate'))document.getElementById('ajaxdate').style.display = 'none' ;
	    } else {
	    	if(document.getElementById('ajaxdate'))document.getElementById('ajaxdate').style.display = 'block' ;
	    	if(document.getElementById('ajaxcat3'))document.getElementById('ajaxcat3').style.display = 'none' ;
	    }
	    
  	 } else if(boxid=='cat3'){  		
  	 
	  	// retrieve the name typed by the user on the form
	    newcat = document.getElementById("cat").value;
	
	    if(newcat!='all' && newcat.length>3){  
	    	    	
			// execute the quickstart.php page from the server
			xmlHttp.open("GET", "/cms/cms.php?cmspage=../ajax/selectboxes.php&newcat=" + newcat  + "&entitytype=" + entitytype + "&cat=" + cat + "&cat3=" + cat3 + "&region=" + region + "&city=" + city + "&date=" + date, true);  
			// define the method to handle server responses
			xmlHttp.onreadystatechange = handleServerResponse;
			// make the server request
			xmlHttp.send(null);	    				
	    } else {
	    	document.getElementById("ajax" + boxid).style.display = 'none' ;
	    }
		 
  	 } 
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('process(boxid)', 1000);
}

// executed automatically when a message is received from the server
function handleServerResponse() 
{
    
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;               
      
      // obtain the document element (the root element) of the XML structure
      xmlRoot = xmlResponse.documentElement;
        		
      if(xmlRoot){
		  
	      boxidArray = xmlRoot.getElementsByTagName("boxid");
	      
	      if(boxidArray.length>0){
	      			      	 
		      var boxid = '';
		      
		      boxid = boxidArray.item(0).firstChild.data;
	      	
	      	  // get the text message, which is in the first child of
		      // the the document element
		      optgroupsArray = xmlRoot.getElementsByTagName("optgroup");
		      	     
		      // update the client display using the data received from the server
		      var html = '';
		      
		      if(optgroupsArray.length>0){
		      	
			      for (var i=0; i<optgroupsArray.length; i++){      	
			      	
			      	optgroupRoot = optgroupsArray.item(i);
			      	
			      	titleArray = optgroupRoot.getElementsByTagName("title");
			      	
			      	html += '<optgroup label="' + titleArray.item(0).firstChild.data + '">';
			      	
			      	optionsArray = optgroupRoot.getElementsByTagName("option");	      		      	
			      	
			      	for (var z=0; z<optionsArray.length; z++){		 
			      		
			      		optionsRoot = optionsArray.item(z);     	
				      	
			      		textArray = optionsRoot.getElementsByTagName("text");
				      	valueArray = optionsRoot.getElementsByTagName("value");
				      	attribArray = optionsRoot.getElementsByTagName("attrib");		    

				      	optionText = textArray.item(0).firstChild.data;				     
				      	
				      	optionText = optionText.replace(/--/g, "&ndash;");				   		
				      				      	
				      	if(attribArray.item(0).firstChild){		      	
				      		html += '<option value="' + valueArray.item(0).firstChild.data + '" ' + attribArray.item(0).firstChild.data + '>' + optionText + '</option>'
				      	} else {
				      		html += '<option value="' + valueArray.item(0).firstChild.data + '">' + optionText + '</option>'
				      	}
				     }
			      	
			      	html += '</optgroup>';
			      }
		      } else {
		      	
			      optionsArray = xmlRoot.getElementsByTagName("option");	      		      	
			      	
			      for (var z=0; z<optionsArray.length; z++){		 
			      		
			      	optionsRoot = optionsArray.item(z);     	
				     	
			      	textArray = optionsRoot.getElementsByTagName("text");
				   	valueArray = optionsRoot.getElementsByTagName("value");
				   	attribArray = optionsRoot.getElementsByTagName("attrib");		    

				   	optionText = textArray.item(0).firstChild.data;				 
				   	      	
				   	optionText = optionText.replace(/--/g, "&ndash;");			
				   	
				   	if(attribArray.item(0).firstChild){		      	
				   		html += '<option value="' + valueArray.item(0).firstChild.data + '" ' + attribArray.item(0).firstChild.data + '>' + optionText + '</option>'
				   	} else {
				   		html += '<option value="' + valueArray.item(0).firstChild.data + '">' + optionText + '</option>'
				   	}
				 }	      		      
		      }
		                           
		      if(html!="" && optionsArray.length>0){
		      	
		      	entitytype = encodeURIComponent(document.getElementById("entitytype").value);
		      	
		      	if(boxid=='cat' && entitytype!='e'){
		      		html = '<select name="' + boxid + '" id="' + boxid + '" onchange="process(\'cat3\')">' + html + '</select>';
		      	} else {
		      		html = '<select name="' + boxid + '" id="' + boxid + '">' + html + '</select>';
		      	}
		      	
		      	document.getElementById("ajax" + boxid).innerHTML = html;
		      	document.getElementById("ajax" + boxid).style.display = 'inline' ;
		      } else {
	       		document.getElementById("ajax" + boxid).style.display = 'none' ;
	      	  }
	       } 
      	}
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}