



//as you can no doubt tell this file could be smaller, there could be better generic functions for
//checking each required field (handy for big forms!) but as i only have two in this form i havent
//tried too hard. by leaving it laid out like this though it should be more self explanatory as to
//what is happening

	var b_dom,http
	
	//called by body->onLoad, will not be invoked if javascript is disabled
	function init()
	{
		//the default action of the form is to submit it normally, where the asp checks
		//the required fields, by overriding that behaviour using javascript here we know
		//it can only be invoked if javascript is enabled! this is a fundamental technique
		//of degradability: make the enhancing technology invoke itself.
		//document.login.action="javascript:verify();"
		//alert("entered");
		
		http = false;
		b_dom = false;
		
		//got DOM?
		if( document.getElementById && document.createElement )
		{
			//yes!
			b_dom = true
		//alert("DOM");
			//AJAX only if got DOM
			if( window.ActiveXObject )	//IE
			{
				http = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
			else
			if( window.XMLHttpRequest )	//FF,NS and OP
			{
				http = new XMLHttpRequest();
			}
			//else http remains uninintialised and therefore can't be used
		}
		httpGo();
 	}
	
	//encode a string so it can be sent safely via http get or post, this is probably borked
	//but is supposed to be equivalent to the ASP function server.urlencode
	function enc(s) 
	{
		s = s.replace(/\+/g, '%2B');
		s = s.replace(/\ /g, '+');
		s = escape(s)
		s = s.replace(/\"/g,'%22');
		s = s.replace(/\'/g, '%27');
		s = s.replace(/\//g,'%2F');
		return s;
	}
	
	//this is the ajax request, note how we build a query exactly how we would in asp, by concacting the strings
	function httpGo() 
	{
		//alert("httpGo");
	    http.open('post', 'searchlocation.asp');
	    http.onreadystatechange = handleResponse;
       
	    var n
		var box = document.ctrySearchForm.txtCountry;
      //alert("box :"+box);
	    
		//defaults, could be named better and the http object would have to be defaulted
		//to null if we were strict-typing
		//alert("First :"+box.options[box.selectedIndex].text);
		//alert(document.getElementById('txtCountry').value);
	    n = escape(box.options[box.selectedIndex].text);
	    //  d = enc(document.forms.f.i_date.value);
	   // m = enc(document.forms.f.i_mssg.value);
	  
	    //without the header the post will not be read by the server
	    http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');  
		http.send('last_value=' + n );	
		//  alert(n);
	    //http.send('i_name=' + n + '&i_date=' + d + '&i_mssg=' + m);	
		// var ss = document.getElementById('txtBookingNo_info')
	  //  ss.style.visibility = "visible";
		//document.getElementById("txtLocation").removeChild();
		clearSelect('txtLocation');
	}
	
	function handleResponse() 
	{
	    if(http.readyState == 4)
	    {
	        var response = http.responseText;
           	//i send pre-formatted HTML back, so we just dump it into the page
	       // document.getElementById('txtBookingNo_info').innerHTML = " <FONT color=#ff0000><b>" + response +"</b></font>";
		// alert(response);
		
		
		//   document.getElementById('txtBookingNo_info').style.visibility = "hidden";
		   if(response == "") {
			   //  alert("1");
			    select = document.getElementById("txtLocation");
                document.getElementById("txtLocation").options[0]=new Option("")
		   }else {
			 //  alert("2");
			 var v_array = "";
			 v_array = response.split("~~");
			// alert(v_array[0]);
			 select = document.getElementById("txtLocation");
                    document.getElementById("txtLocation").options[0]=new Option("All")
  						for(var i=1; i<v_array.length; i++) {
                    		document.getElementById("txtLocation").options[i]=new Option(v_array[i], v_array[i])
							
						}
						}
			
			
	       //   alert(response);
			//if you wanted to do more than one http request certain browsers prefer you to kill
			//the old one and create a new one, you could do that here
                Deptinit();
	    }
            //  
	}

	//generic input checking and highlighting function, depends on consistent naming of form
	//inputs and labels, could easily be changed to work for both the DOM and non DOM cases
	//but really it's just me showing off
	function checkinput( e , b )
	{
		var te;
		te = document.getElementById("l_" + e)
		if( document.forms.f["i_" + e].value == "" )
		{
			te.style.fontWeight = "bold";
			te.style.color = "red";
			return false;
		}
		else
		{
			te.style.fontWeight = "normal";
			te.style.color = "black";
			return b;
		}
	}
	
	//this is where the form checking actually takes place
	function verify()
	{
		httpGo();
	}	


function clearSelect() {
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];

        if (typeof element == 'string')
            element = document.getElementsByName(element)[0];

        if (element && element.options) {
            element.options.length = 0;
            element.selectedIndex = -1;
        }
    }
}





//---------------------------------------------------------------------------------------------------------------------


	var b_dom,http
	
	
	function Deptinit()
	{
		
		
		//alert("department");
		http = false;
		b_dom = false;
		
		
		if( document.getElementById && document.createElement )
		{
			
			b_dom = true
		
			if( window.ActiveXObject )	//IE
			{
				http = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
			else
			if( window.XMLHttpRequest )	//FF,NS and OP
			{
				http = new XMLHttpRequest();
			}
			//else http remains uninintialised and therefore can't be used
		}
		httpGoDept();
 	}
	
	//encode a string so it can be sent safely via http get or post, this is probably borked
	//but is supposed to be equivalent to the ASP function server.urlencode
	function enc(s) 
	{
		s = s.replace(/\+/g, '%2B');
		s = s.replace(/\ /g, '+');
		s = escape(s)
		s = s.replace(/\"/g,'%22');
		s = s.replace(/\'/g, '%27');
		s = s.replace(/\//g,'%2F');
		return s;
	}
	
	//this is the ajax request, note how we build a query exactly how we would in asp, by concacting the strings
	function httpGoDept() 
	{
		//alert("httpGo");
	    http.open('post', 'searchdepartment.asp');
	    http.onreadystatechange = handleResponseDept;
       
	    var n
		var box = document.ctrySearchForm.txtCountry;
    
	    n = escape(box.options[box.selectedIndex].text);
	  
	    //without the header the post will not be read by the server
	    http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');  
		http.send('last_value=' + n );	
	
		clearSelect('txtDept');
	}
	
	function handleResponseDept() 
	{
	    if(http.readyState == 4)
	    {
	        var response = http.responseText;
          
		   if(response == "") {
			   //  alert("1");
			     select = document.getElementById("txtDept");
                       document.getElementById("txtDept").options[0]=new Option("All")
		   }else {
			 //  alert("2");
			 var v_array = "";
			 v_array = response.split("~~");
			// alert(v_array[0]);
			 select = document.getElementById("txtDept");
                    document.getElementById("txtDept").options[0]=new Option("All")
  						for(var i=1; i<v_array.length; i++) {
                    		document.getElementById("txtDept").options[i]=new Option(v_array[i], v_array[i])
							
						}
						}
			
			
	     
	    }
	}

	
	function checkinput( e , b )
	{
		var te;
		te = document.getElementById("l_" + e)
		if( document.forms.f["i_" + e].value == "" )
		{
			te.style.fontWeight = "bold";
			te.style.color = "red";
			return false;
		}
		else
		{
			te.style.fontWeight = "normal";
			te.style.color = "black";
			return b;
		}
	}
	
	//this is where the form checking actually takes place
	function verifyDept()
	{
		httpGoDept();
	}	

