var enquiry_xhr = {
	init: function() {
		// Grab the elements we'll need.
      
		enquiry_xhr.form_enquiry = document.getElementById('enquiry_form');
		enquiry_xhr.div_enquiry = document.getElementById('id_div_enquiry');
		
		enquiry_xhr.select_towns = document.getElementById('id_bus_town');
		enquiry_xhr.select_l0 = document.getElementById('id_bus_l0');
		enquiry_xhr.select_l1 = document.getElementById('id_bus_l1');
		enquiry_xhr.select_l2 = document.getElementById('id_bus_l2');
		          
		enquiry_xhr.sort_selectbox(enquiry_xhr.select_towns);
		enquiry_xhr.sort_selectbox(enquiry_xhr.select_l0);
		enquiry_xhr.init_selectbox(enquiry_xhr.select_l1,true);
		enquiry_xhr.init_selectbox(enquiry_xhr.select_l2,true);         
	
	
		YAHOO.util.Event.addListener(enquiry_xhr.select_l0, 'change', enquiry_xhr.submit_select_l0);
		YAHOO.util.Event.addListener(enquiry_xhr.select_l1, 'change', enquiry_xhr.submit_select_l1);

		},
	
	init_selectbox: function(element,sticky){
		var selected_value="none";
		var selected_html="none";
		for(var i=0; i<element.length; i++){
  			if (element.options[i].selected && sticky)
				{selected_value=element.options[i].value;
				 selected_html=element.options[i].text;
				}	
			}
		while (element.firstChild)
			{element.removeChild(element.firstChild);
			} 
		option=document.createElement('option');
		if(selected_value=="")
			{selected_value="none";
			 selected_html="none";
			}
		option.value=selected_value;
		option.innerHTML=selected_html;
		option.selected=true;
		element.appendChild(option);
 			
		},

	enable_form: function(form){
		for(var i=0; i<form.elements.length; i++) 
					{form.elements[i].disabled = false;
					}

		},

	disable_form: function(form){
		for(var i=0; i<form.elements.length; i++) 
					{form.elements[i].disabled = true;
					}

		},

	sort_selectbox: function(element){
		if(element.length<=1)
			{return;
			}
		arrText = new Array();
		arrVal = new Object();
		var selected_value="none";
		var selected_html="none";
		for(var i=0; i<element.length; i++){
  			var key= element.options[i].text; 
			if (element.options[i].selected)
				{selected_value=element.options[i].value;
				 selected_html=key;
				}
			arrText[i] =key;
			arrVal[key] = element.options[i].value;
			}
		arrText.sort();
		while (element.firstChild)
			{element.removeChild(element.firstChild);
			} 
		for(var i=0; i<arrText.length; i++){
			var key= arrText[i];
			option=document.createElement('option');
			option.value=arrVal[key];
			option.innerHTML=key;
			if(key==selected_html)
				{option.selected=true;
				}
			element.appendChild(option);
			}	
		},
	
	disable_selectboxes: function(form){
		var elements=form.getElementsByTagName("select");
		for(var i=0; i<elements.length; i++) 
					{if(elements[i].length<=1)
						elements[i].disabled = true;
					}

		},


	submit_select_l0: function(e) {
		YAHOO.util.Event.preventDefault(e);
		YAHOO.util.Connect.setForm(enquiry_xhr.form_enquiry);
      
		enquiry_xhr.disable_form(enquiry_xhr.form_enquiry);
	
      		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/directory/listing/free/?xhr=get_l1', enquiry_xhr.ajax_callback_l0);

   		},
	submit_select_l1: function(e) {
		YAHOO.util.Event.preventDefault(e);
		YAHOO.util.Connect.setForm(enquiry_xhr.form_enquiry);
      		
		enquiry_xhr.disable_form(enquiry_xhr.form_enquiry);
	
      		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/directory/listing/free/?xhr=get_l2', enquiry_xhr.ajax_callback_l1);
   		},
   
	ajax_callback_l0: {
		success: function(o) {
			// This turns the JSON string into a JavaScript object.
			var response_obj = eval('(' + o.responseText + ')');

			if(response_obj.errors) 
				{ // The form had errors.
	    			} 
			else if(response_obj.success) { 
				// The form went through successfully.
	    	
				enquiry_xhr.init_selectbox(enquiry_xhr.select_l1,false);
				enquiry_xhr.init_selectbox(enquiry_xhr.select_l2,false);
				enquiry_xhr.option_l0=response_obj.option_l0;
				var option_dict=response_obj.options_l1;			
				for(var key in option_dict){
					option=document.createElement('option');
					option.value=key;
					option.innerHTML=option_dict[key];
					enquiry_xhr.select_l1.appendChild(option);	
					}
				enquiry_xhr.sort_selectbox(enquiry_xhr.select_l1);
				enquiry_xhr.enable_form(enquiry_xhr.form_enquiry);	
				}
			},
      
		failure: function(o) { 
			// In this example, we shouldn't ever go down this path.
			alert('An error has occured'); 	
   			enquiry_xhr.enable_form(enquiry_xhr.form_enquiry);
			}
   	
	},

	ajax_callback_l1: {
		success: function(o) {
			// This turns the JSON string into a JavaScript object.
			var response_obj = eval('(' + o.responseText + ')');

			if(response_obj.errors) 
				{ // The form had errors.
	    			} 
			else if(response_obj.success) { 
				// The form went through successfully.
	    	
				enquiry_xhr.init_selectbox(enquiry_xhr.select_l2,false);
				enquiry_xhr.option_l1=response_obj.option_l1
				var option_dict=response_obj.options_l2
				for(var key in option_dict){    
					option=document.createElement('option');
					option.value=key;
					option.innerHTML=option_dict[key];
					enquiry_xhr.select_l2.appendChild(option);	
					}
				enquiry_xhr.sort_selectbox(enquiry_xhr.select_l2);
				enquiry_xhr.enable_form(enquiry_xhr.form_enquiry);
				}
			},
      
		failure: function(o) { 
			// In this example, we shouldn't ever go down this path.
			alert('An error has occured'); 	
   			enquiry_xhr.enable_form(enquiry_xhr.form_enquiry);
			}
   	
	},

	display_errors: function(error_obj) {
			for(var err in error_obj) 
				{
				}
 			}

};

YAHOO.util.Event.addListener(window, 'load', enquiry_xhr.init);
