// JavaScript Document

var ClassSearch = function(){
	//________________________________ PROPERTIES PUBLIC ________________________________
	

	//________________________________ METHODS PUBLIC ________________________________
	this.show_form = function(pagename){
		if( working ) return;
		if( pagename==0 ){
			document.getElementById("form_advancedsearch").style.display="none";
			return;
		}else{
			document.getElementById("form_advancedsearch").style.display="block";			
		}
		
		
		Progress.show(document.getElementById("cboSearchFor"));
		document.getElementById("cboSearchFor").disabled = true;
		var Ajax = new ClassAjax();
			Ajax.on_finalizer = function(){
				document.getElementById("form_advancedsearch").innerHTML = this.responseHTML;
				Progress.hidden(document.getElementById("cboSearchFor"));
				document.getElementById("cboSearchFor").disabled = false;
			}
			Ajax.execute("POST", pagename);
	}

	this.show_regions = function(codcountry){
		if( codcountry==0 ) return;
		
		Progress.show(document.formSearch.cboCountry);
		document.formSearch.cboCountry.disabled = true;
		
		var Ajax = new ClassAjax();
		Ajax.on_finalizer = function(){
			
			if( this.responseXML==null ){
				alert("");
			}else{				
			   document.formSearch.cboRegion.length=1;
				
			   var rows = this.responseXML.getElementsByTagName("row");
			   for( var i=0; i<=rows.length-1; i++ ){
				   codregion = rows[i].getElementsByTagName("codregion")[0].childNodes[0].nodeValue;
				   name = rows[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
				   document.formSearch.cboRegion.options[i+1] = new Option(name, codregion);
			   }
			   
			   document.formSearch.cboCountry.disabled = false;
		   }
		   Progress.hidden(document.formSearch.cboCountry);
		}
		Ajax.execute("POST", "includes/advancedsearch/data.php?action=show_region", "codcountry="+codcountry);				
	}

	this.show_destination = function(codregion){
		if( codregion==0 ) return;
		
		Progress.show(document.formSearch.cboRegion);
		document.formSearch.cboRegion.disabled = true;
		
		var Ajax = new ClassAjax();
		Ajax.on_finalizer = function(){
			
			if( this.responseXML==null ){
				alert("");
			}else{				
			   document.formSearch.cboDestination.length=1;
				
			   var rows = this.responseXML.getElementsByTagName("row");
			   for( var i=0; i<=rows.length-1; i++ ){
				   coddestiny = rows[i].getElementsByTagName("coddestiny")[0].childNodes[0].nodeValue;
				   name = rows[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
				   document.formSearch.cboDestination.options[i+1] = new Option(name, coddestiny);
			   }
			   
			   document.formSearch.cboRegion.disabled = false;
		   }
		   Progress.hidden(document.formSearch.cboRegion);
		}
		Ajax.execute("POST", "includes/advancedsearch/data.php?action=show_destiny", "codregion="+codregion);				
	}

	this.advanced_search = function(){
		if( working ) return;
		
		if( document.formSearch.txtSearch.value.length==0 ){
			alert("This field is required");
			document.formSearch.txtSearch.focus();
			return false;
		}
				
		Progress.show();
		document.formSearch.btnGo.disabled=true;
		var Ajax = new ClassAjax();
		Ajax.on_finalizer = function(){
			document.getElementById("resultSearch").innerHTML = this.responseHTML;
			
		    Progress.hidden();
			document.formSearch.btnGo.disabled=false;
		}
		Ajax.execute("POST", "includes/advancedsearch/data.php?action=advanced_search", Ajax.request_form(document.formSearch));
		
		return false;
	}
	
	this.validate_field = function(){
		if( document.formSimpleSearch.txtSearch.getAttribute("edit")==null || document.formSimpleSearch.txtSearch.value.length==0 ){
			alert('The field "Search" is required');
			document.formSimpleSearch.txtSearch.value="";
			document.formSimpleSearch.txtSearch.focus();
			return false;
		}
		
		
		return true;
	}


	
	//________________________________ PROPERTIES PRIVATE ________________________________
	var working=false;
	var This=this;

	//________________________________ METHODS PRIVATE ________________________________
	Progress={
		show: function(el){
			working=true;
			if( !el ) return;
			
			var progress = getElementsByClassName("progress", "div", el.parentNode);
			if( progress.length>0 ){
				progress[0].style.display="block";
			}			
		},
		hidden: function(el){
			working=false;
			if( !el ) return;
			var progress = getElementsByClassName("progress", "div", el.parentNode);
			if( progress.length>0 ){
				progress[0].style.display="none";
			}			
		}
	}
		
}
var Search = new ClassSearch();