// Used by search_class.js to look after the input controls of the search panel

function search_panel_element_class(_element_name,_parent_ob){
	var _me=this
	this.element_name=_element_name //name of the primary element on the page this is linked to, could be a form element or a div
	this.element_type=""
	this.parent_ob=_parent_ob //object which contains an instance of this class in a list - probably search_ob,or a leve2 search element
	this.selected_id=""

	this.populated=0

	this.key_is_pressed=0
	this.pre_keyup_value=""
	this.key_has_been_pressed=0
	
	this.setup_interface=function(){
		
		var element_ob=document.getElementById(_me.element_name)
		_me.element_type=element_ob.type
		
		switch(_me.element_type){
			case "select-one":

				element_ob.onchange=_me.menu_change	
				switch(_me.element_name){
					case "area":
						_me.selected_id=search_area
						element_ob.disabled=0
						
					break;
					case "category":
						_me.selected_id=search_category
						if(search_area!=""){
							element_ob.disabled=0	
						}
						
					break;
				}
				//_me.prepare_results_query()
				search_ob.result_category_ID=_me.selected_id
				break;
			case "text":
			
				element_ob.onkeypress=_me.check_keypress
									
				element_ob.onkeyup=_me.check_keyup
				
				element_ob.onblur=_me.check_blur
				
				element_ob.disabled=0	
				break;
		}
	}
	
	this.prepare_results_query=function(){
		//GET THE ELEMENTS VALUE AND TELL IT TO THE SEARCH OB
		var element_ob=document.getElementById(_me.element_name)
		var category_ID=""
		search_ob.result_summary_search_or_refine="search"
		switch(_me.element_type){	
			case "select-one":
				
				search_ob.set_result_category_ID(_me.selected_id)
			break;
			case "text":
				search_ob.set_result_search_text(element_ob.value)
			break;
		}
	}
	this.menu_change=function(){
		
		var element_ob=document.getElementById(_me.element_name)
		_me.selected_id=element_ob.options[element_ob.selectedIndex].value
	
		for(var i=0;i<_me.parent_ob.search_element_ob_arr.length;i++){
			
			if(_me.parent_ob.search_element_ob_arr[i].element_name!=_me.element_name){
				switch(_me.element_name){
					case "area":
						_me.parent_ob.search_element_ob_arr[i].reset_interface()
						search_category=""
					break;
					case "category":
						if(_me.parent_ob.search_element_ob_arr[i].element_name!="area"){
							_me.parent_ob.search_element_ob_arr[i].reset_interface()
						}
					break;
				}
			}
		}

		if(_me.selected_id!=""){
			switch(_me.element_name){
				case "area":
					//DRAW THE SECOND LEVEL MENU
					_me.parent_ob.search_element_ob_arr[1].populate_menu()
					_me.parent_ob.clear_results_query()
				break;
				case "category":
					//if(is_on_listing_page==1){
						//GET THE KEY CRITERIA XML FOR LEVEL3
						//_me.parent_ob.load_key_criteria(_me.selected_id)
						_me.prepare_results_query()
					//}
				break;
				
			}
		}else{
			_me.parent_ob.clear_results_query()
		}
		
	}

	this.populate_menu=function(){
		var element_ob=document.getElementById(_me.element_name)
		
		switch(_me.element_name){
			case "area":
				//GET FIRST LEVEL CATEGORY NODES
				element_ob.disabled=0
				 
				//USE THIS LINE TO ONLY RETURN NODES WHICH WILL CONTAIN SOME ITEMS
				var l1_nodes=search_ob.search_xml.selectNodes("//data/category[.//category[@item_count > 0]]")
				
				//USE THIS LINE TO RETURN ALL NODES
				//var area_nodes=_me.parent_ob.search_xml.selectNodes("//data/category")
			
				if(l1_nodes.length>0){
					
					element_ob.options.length=1
					selected_pos=0
					for(var i=0;i<l1_nodes.length;i++){
						if(l1_nodes[i].getAttribute("category_name").indexOf("(temp)")==-1){
							//alert("ADD")
						
							//element_ob.options[element_ob.options.length]=new Option(l1_nodes[i].getAttribute("category_name"),l1_nodes[i].getAttribute("category_ID"));
							
							var option=document.createElement("option")
							option.value=l1_nodes[i].getAttribute("category_ID")
							option.innerHTML=l1_nodes[i].getAttribute("category_name")
							element_ob.appendChild(option)
							if(search_area!=""){
								if(l1_nodes[i].getAttribute("category_ID")==search_area){
									selected_pos=element_ob.options.length-1
								}
							}
						}
					}
					if(selected_pos>0){
						element_ob.options[selected_pos].selected=1
						_me.menu_change()
					}
					search_area=""
				}
			break;
			case "category":
				//GET SECOND AND BEYOND LEVEL CATEGORY NODES
				
				element_ob.disabled=0
				selected_pos=0
				
				var l2_nodes=search_ob.search_xml.selectNodes("//data/category[@category_ID="+search_ob.search_element_ob_arr[0].selected_id+"]/category")
			
				if(l2_nodes.length>0){
					for(var i=0;i<l2_nodes.length;i++){
						var l3_nodes=search_ob.search_xml.selectNodes("//category[@category_parent_ID="+l2_nodes[i].getAttribute("category_ID")+" and @item_count>0]")
						
						if(l3_nodes.length==0){
							if(parseInt(l2_nodes[i].getAttribute("item_count"))>0){
								var option=document.createElement("option")
								option.value=l2_nodes[i].getAttribute("category_ID")
								option.innerHTML=l2_nodes[i].getAttribute("category_name")
								element_ob.appendChild(option)
								if(search_category!=""){
									if(l2_nodes[i].getAttribute("category_ID")==search_category){
										selected_pos=element_ob.options.length-1
									}
								}
							}
						}else{
						
							var optgroup=document.createElement("optgroup")
							optgroup.label=l2_nodes[i].getAttribute("category_name")
							element_ob.appendChild(optgroup)
		
							for(var j=0;j<l3_nodes.length;j++){
								var option=document.createElement("option")
								option.value=l3_nodes[j].getAttribute("category_ID")
								option.innerHTML=l3_nodes[j].getAttribute("category_name")
								optgroup.appendChild(option)
								if(search_category!=""){
									
									if(l3_nodes[j].getAttribute("category_ID")==search_category){
										selected_pos=element_ob.options.length
									}
								}
							}
						}
						
						
					}
					if(selected_pos>0){
						element_ob.options[selected_pos].selected=1
						_me.menu_change()
					}
					search_category=""
				}	
				
				
			
			break;
		}
	}
	
	
	this.default_interface=function(){
		var element_ob=document.getElementById(_me.element_name)
		if(_me.level==1){
			element_ob.options[0].selected=1
		}
	}
	
	this.reset_interface=function(){
		var element_ob=document.getElementById(_me.element_name)
		switch(_me.element_name){
			case "area":
				element_ob.options[0].selected=1
			break;
			case "category":
				element_ob.innerHTML=""
				element_ob.options[element_ob.options.length]=new Option("Select category...","");
				element_ob.disabled=1
			break;
			case "search":
				element_ob.value=""
			break;
			
		}
	}
	
	
	this.check_keypress=function(evt){
		//CHECKS TO SEE IF THE CHARACTER ABOUT TO BE ENTERED IS VALID AND IF NOT PREVENTS THE CHARACTER FROM BEING ENTERED
		if(typeof evt=="undefined" || !evt){
			evt=window.event
		}
		if(evt.keyCode){
			//IE
			var cd=evt.keyCode
		}else{
			//MOZ
			var cd=evt.charCode
		}
		
		var form_element=document.getElementById(_me.element_name)
		var valid_key=1
		
		if(_me.key_is_pressed==0){
			_me.pre_keyup_value=_me.validate_text_field_value(form_element.value)
			_me.key_is_pressed=1
			if(cd==9 || cd==13 || cd==9 || cd==37 || cd==39){
				//TAB OR RETURN OR LEFT OR RIGHT
			}else{
				
				
				
					
			}
		}else{
			valid_key=0
		}
		//return valid_key==1
		_me.key_has_been_pressed=1
		
	}
	
	this.check_keyup=function(){
		
		
		//CHECKS TO SEE IF THE CURRENT VALUE OF THE TEXT FIELD IF VALID, AND IF NOT SETS THE VALUE OF THE TEXT FIELD BACK TO THE LAST VALID VALUE
		var form_element=document.getElementById(_me.element_name)
		var valid_value=1
		_me.key_is_pressed=0
		
			
					
		if(valid_value==0){
			
			form_element.value=_me.validate_text_field_value(_me.pre_keyup_value)
		}
		
		
		for(var i=0;i<_me.parent_ob.search_element_ob_arr.length;i++){
			if(_me.parent_ob.search_element_ob_arr[i].element_name!=_me.element_name){
				_me.parent_ob.search_element_ob_arr[i].reset_interface()
			}
		}
		if(form_element.value!=""){
		   
		  
			_me.prepare_results_query()
		}else{
			 _me.parent_ob.clear_results_query()	
		}
	
	}
	
	this.check_blur=function(){
		
		
		if(_me.key_has_been_pressed==1){
			_me.check_keyup()
		}
		_me.key_has_been_pressed=0
		
	}
	this.validate_text_field_value=function(val){
	
		var valid_value=1
		
		


		if(valid_value==1){
			return val
		}else{
			return ""	
		}
	}
	
}