var IBPHOME={}
IBPHOME.loadProvs={
	 xmlObj:objAjax.create(),
	 listOrigin:'' ,
	 listDest: '',
	 parameterName: "country",
	 init:function(){
		 IBPHOME.loadProvs.listOrigin=page.$("selectPais"),
		 IBPHOME.loadProvs.listDest=page.$("selectProv"),
		 arrOpt=new Array();
		 for( m=0; m<IBPHOME.loadProvs.listDest.options.length;m++){
			arrOpt[arrOpt.length]=IBPHOME.loadProvs.listDest.options[m];
		 }
		 IBPHOME.loadProvs.listOrigin.onchange = IBPHOME.loadProvs.load;
		 if(IBPHOME.loadProvs.listOrigin.value!="ES")IBPHOME.loadProvs.listDest.style.display="none"
	 },
	 load:function(){
	 	if(this.value=="ES")
	 		IBPHOME.loadProvs.listDest.style.display=""
		else
	 		IBPHOME.loadProvs.listDest.style.display="none"
	 }
}

IBPHOME.loadPartners={
	 xmlObj:objAjax.create(),
	 listOrigin:'' ,
	 listDest: '',
	 parameterName: "sector",
	 init:function(){
		 IBPHOME.loadPartners.listOrigin=page.$("selectSector"),
		 IBPHOME.loadPartners.listDest=page.$("selectPartner"),
		 arrOpt=new Array();
		 for( m=0; m<IBPHOME.loadPartners.listDest.options.length;m++){
			arrOpt[arrOpt.length]=IBPHOME.loadPartners.listDest.options[m];
		 }
		 IBPHOME.loadPartners.listOrigin.onchange = IBPHOME.loadPartners.load;
	 },
	 load:function(){
		 IBPHOME.loadPartners.listDest[0]=new Option(page.$("loading").value,"-1")
		 IBPHOME.loadPartners.listDest.selectedIndex=0;
		 
		 var path=page.$("loadPartnersAjx").value;
		 var xmlObj=IBPHOME.loadPartners.xmlObj;
		 var sep=url.str2concat(path);
		 var urlx = path.split("?");
		 path=urlx[0];
		 if (urlx.length > 1){
			 urlx[1] = urlx[1].replace(/&amp;/, "&");
			 var urlx = urlx[1]+"&"+IBPHOME.loadPartners.parameterName+"="+IBPHOME.loadPartners.listOrigin.value;
			 
		 }else{
		 	var urlx = IBPHOME.loadPartners.parameterName+"="+IBPHOME.loadPartners.listOrigin.value;
		 }
		 
		 xmlObj.open('POST', path, true);
		 xmlObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
		 xmlObj.send(urlx);
		 xmlObj.onreadystatechange = function(){
			 if (xmlObj.readyState == 4){
					 if (xmlObj.status == 200){
						 var firstOpt=IBPHOME.loadPartners.listOrigin.firstChild.text;
						 var firstValue="";
						 var xmlData=xmlObj.responseXML;
						 if (xmlData.getElementsByTagName("errors").length>0){
							 var errorData=xmlData.getElementsByTagName("cod")
							 var errorDesc=errorData[0].firstChild.nodeValue;
							 switch(errorDesc){
								 case "AIRPORT.SEARCH_DESTINATIONS.NO_PARAMS" :
								 alert(xmlData.getElementsByTagName("description")[0].firstChild.nodeValue);
								 break;
								 case "AIRPORT.SEARCH_DESTINATIONS.EMPTY_LIST" :
								 alert(xmlData.getElementsByTagName("description")[0].firstChild.nodeValue);
								 break;
								 case "IBPHOME.error.noHayPartners":
								 //Mensajes en jsp
								 firstOpt = xmlData.getElementsByTagName("description")[0].firstChild.nodeValue;
								 firstValue = "";
								 break;
								 default :
								 window.status=xmlData.getElementsByTagName("description")[0].firstChild.nodeValue;
								 break;
							 }
							 IBPHOME.loadPartners.listDest.length=0;
						 }else{
							 for (i = IBPHOME.loadPartners.listDest.length - 1; i>=0; i--) {
								IBPHOME.loadPartners.listDest.remove(i);
							 }
							 var allData=xmlData.getElementsByTagName("node");
							 var count=0;
							 for (var i=0; i<allData.length; i++){
								 if (i==0){
									 firstOpt = allData[i].firstChild.nodeValue;
									 firstValue = allData[i].getAttribute("value");
								 } else {
								 	 IBPHOME.loadPartners.listDest[count]=new Option(allData[i].firstChild.nodeValue, allData[i].firstChild.nodeValue)
								 }
								 count++;
							 }
						 }
					 IBPHOME.loadPartners.listDest[0]=new Option(firstOpt,firstValue)
					 IBPHOME.loadPartners.listDest.selectedIndex=0;
					 } else{
						window.status = 'ajax_obj Search error: Server returned HTTP code ' + xmlObj.status;
					 }
				 }
			 }
		 }
	 }

ev.addEvent(window, "load", IBPHOME.loadProvs.init, false);	 
ev.addEvent(window, "load", IBPHOME.loadPartners.init, false);
