var brokerOverlays= new Array();
var flagHnd = 0;

function loadFormStatesSearch (loadTo) {
	
	
	if(loadTo.length>0){
		for(var i = 0;i<loadTo.length;i++){
			
			//alert(loadTo[i]);
			var formu = document.getElementById(loadTo[i]);
			formu.onsubmit=searchBrokers;
			var state = formu.state;
			var region = formu.region;
			var subRegion1 = formu.subRegion1;
			var subRegion2 = formu.subRegion2;
			if(state&&region&&subRegion1&&subRegion2){
				//state onchange event
				//state.addEvent("change", loadRegionGeneric);				
				state.onchange=loadRegionGeneric;
				region.onchange=loadRegionGeneric;
				subRegion1.onchange=loadRegionGeneric;
				subRegion2.onchange=loadRegionGeneric;
			} else {
				alert("Malformed search form.");
			}
			
		}
	}
}

function loadRegionGeneric(shoot) {
	gsmap.closeExtInfoWindow();	
	var that=this;
	if(that.name=="region"){
		if(that[that.selectedIndex].value==0){
			that.form.region.disabled=false;
			that.form.subRegion1[0].selected=true;
			that.form.subRegion1.disabled=true;
			that.form.subRegion2[0].selected=true;
			that.form.subRegion2.disabled=true;
			if(shoot!="waitReg"&&shoot!="reset") {
				searchBrokers(0);
			}
			return;
		}
		$("subRegion1Label").innerHTML=that.options[that.selectedIndex].getAttribute("hLevel");
		$("subRegion2Label").innerHTML=that.options[that.selectedIndex].getAttribute("lLevel");
	}
	else if(that.name == "state"&&that[that.selectedIndex].value==0){
		that.form.region[0].selected=true;
		that.form.region.disabled=true;
		that.form.subRegion1[0].selected=true;
		that.form.subRegion1.disabled=true;
		that.form.subRegion2[0].selected=true;
		that.form.subRegion2.disabled=true;
		if(shoot!="wait"&&shoot!="reset") {
			stateCallbackFunction(that);
		}		
		
	}
	else if(that.name=="subRegion1"&&that[that.selectedIndex].value==0) {
		that.form.subRegion2[0].selected=true;
		that.form.subRegion2.disabled=false;
		that.form.region.onchange("wait");
		return;
	}	
	else if(that.name=="subRegion2"&&that[that.selectedIndex].value!=0) {
		if(window.subRegion2CallbackFunction&&shoot!="wait"&&shoot!="reset") { 
			subRegion2CallbackFunction(that);
			searchBrokers(0);
		}
		return;
	}

	if(that[that.selectedIndex].value==0) {
		if(shoot!="wait"&&shoot!="reset") { 		
			searchBrokers(0);
		}
		return;
	}
	
	var req = new Request({  
		method: 'get',  
		url: 'gateway/genericRegion.php?type='+that.name+'&id='+that.options[that.selectedIndex].value ,
		onComplete: function(result) {
			var Json = JSON.decode(result);
			var inLoad= new Array();

			if(that.name == "state"){ 
				inLoad[0] = that.form.region;
				inLoad[0].innerHTML="";
				that.form.region.disabled=false;
				that.form.subRegion1.disabled=true;
				that.form.subRegion2.disabled=true;
				if(window.stateCallbackFunction&&shoot!="wait"&&shoot!="reset") { 
					stateCallbackFunction(that);				
				}
			}
			else if(that.name == "region"){
				inLoad[0] = that.form.subRegion1; 
				inLoad[0].innerHTML="";
				inLoad[1] = that.form.subRegion2;
				inLoad[1].innerHTML="";
				that.form.region.disabled=false;
				that.form.subRegion1.disabled=false;
				that.form.subRegion2.disabled=false;
				if(window.regionCallbackFunction&&shoot!="waitReg"&&shoot!="resetReg") regionCallbackFunction(that);
			}
			else if(that.name == "subRegion1"){ 
				inLoad[0] = that.form.subRegion2;
				
				inLoad[0].innerHTML="";
				if(window.subRegion1CallbackFunction&&shoot!="waitSub1"&&shoot!="resetSub1") { 
				 subRegion1CallbackFunction(that);
				}
			}
			
			for (i=0; i< Json.length; i++){
				var option = new Element('option');
				option.value = Json[i].value;
				option.text = Json[i].text;
				option.label = Json[i].text;
				if(Json[i].hLevel)option.setAttribute("hLevel",Json[i].hLevel);
				if(Json[i].lLevel)option.setAttribute("lLevel",Json[i].lLevel);
				if(Json[i].lat)option.setAttribute("lat",Json[i].lat);
				if(Json[i].lng)option.setAttribute("lng",Json[i].lng);
				if(Json[i].zoomLevel)option.setAttribute("zoomLevel",Json[i].zoomLevel);

				try {//this try catch is for ie / FF incompatibility con metohd add on OPTION elements
					if(that.name != "region")inLoad[0].add(option);
					else {
						if(Json[i].hLevel=="true")inLoad[0].add(option);
						else if(Json[i].hLevel=="false")inLoad[1].add(option);
						else {inLoad[0].add(option); // para poner el ALL en los dos cositos
							var opt2=new Element('option');opt2.value=option.value;opt2.text=option.text;opt2.label=option.label;
							inLoad[1].add(opt2);
							}
						}
				}
				catch (e){//this is for FF
					if(that.name != "region")inLoad[0].add(option,null);
					else {
						if(Json[i].hLevel=="true")inLoad[0].add(option,null);
						else if(Json[i].hLevel=="false")inLoad[1].add(option,null);
						else {inLoad[0].add(option,null);inLoad[1].add(new Element('option',{'value':option.value,'text':option.text,'label':option.label}),null);}
						}
				}
			}
			
			if(that.name == "state"&&(shoot=="wait"||shoot=="reset")){
				for(i=0;i<that.form.region.options.length;i++)
				{
					if(that.form.region.options[i].value == defaultRegion)
					{
						that.form.region.selectedIndex=i;
						if(shoot=="wait"){						
							that.form.region.onchange("waitReg");
						} else {
							that.form.region.onchange("resetReg");
						}
					}
				}
			}
			if(that.name == "region"&&(shoot=="waitReg"||shoot=="resetReg")){
				for(i=0;i<that.form.subRegion1.options.length;i++)
				{
					if(that.form.subRegion1.options[i].value == defaultSubRegion1)
					{					
						that.form.subRegion1.selectedIndex=i;
						if(shoot=="waitReg"){
							that.form.subRegion1.onchange("waitSub1");
						} else {
							that.form.subRegion1.onchange("resetSub1");
						}
					}
				}
			}			
			
			
			if(shoot==undefined||shoot=="resetSub1") {
				//alert(that.name);
				searchBrokers(0);
			}
			
		} 
	}).send();
}

function genericMapMoveTo(that){
	var lat = that[that.selectedIndex].getAttribute("lat");
	var lng = that[that.selectedIndex].getAttribute("lng");
	var zoomLevel = parseInt(that[that.selectedIndex].getAttribute("zoomLevel"));
	gsmap.setCenter(new GLatLng(lat,lng));
	
	if(zoomLevel) {
		if(that.name=="rregion" || that.name == "rstate" || that.name=="rsubRegion1" || that.name=="rsubRegion2"){
			searchRental();
		}else if(that.name=="cregion" || that.name == "cstate" || that.name=="csubRegion1" || that.name=="csubRegion2"){
			searchCommercial();
		}else{
			gsmap.setZoom(zoomLevel);
		}
	}
}

function stateCallbackFunction(that){	
	genericMapMoveTo(that);
}

function regionCallbackFunction(that){
	genericMapMoveTo(that);
}

function subRegion1CallbackFunction(that){
	genericMapMoveTo(that);
}

function subRegion2CallbackFunction (that) {
	genericMapMoveTo(that);
}

function searchBrokers(actualPage){
	actualPage=(actualPage==null||actualPage==undefined||(actualPage*1)==NaN)?"":actualPage;
	var formu = $("brokerSearch");
	var typeElm=(formu.subRegion2[formu.subRegion2.selectedIndex].value!=0)?formu.subRegion2:false;
	if(typeElm==false)typeElm=(formu.subRegion1[formu.subRegion1.selectedIndex].value!=0)?formu.subRegion1:false;
	if(typeElm==false)typeElm=(formu.region[formu.region.selectedIndex].value!=0)?formu.region:false;
	if(typeElm==false)typeElm=(formu.state[formu.state.selectedIndex].value!=0)?formu.state:false;
	var type=(typeElm)?typeElm.name:"";
	var zoneId=(typeElm)?typeElm[typeElm.selectedIndex].value:"";
	var keyWords=formu.keywordBroker.value;
	var req = new Request({  
		method: 'get',  
		url: 'includes/brokerSearchResults.php?type='+type+'&zoneId='+zoneId+'&keyWords='+keyWords+'&actual='+actualPage ,
		onComplete: function(result) {
				if($("brokerSearchMenu"))$("brokerSearchMenu").innerHTML=result;
			}
		}).send();
	if(actualPage==""){
	var reqB = new Request({  
		method: 'get',  
		url: 'gateway/brokersOverlays.php?type='+type+'&zoneId='+zoneId+'&keyWords='+keyWords ,
		onComplete: function(result) {
				gsmap.clearOverlays();
				brokerOverlays= new Array();
				var brokers = JSON.decode(result);
				var subRegionsCount=new Array();
				for(var i=0; i<brokers.length;i++){
					
					var broker=brokers[i];
					broker.thumburl=(broker.thumburl)?broker.thumburl:"images/brokers/default.jpg";
					if(!subRegionsCount[broker.idSubRegion])subRegionsCount[broker.idSubRegion]=0;
					subRegionsCount[broker.idSubRegion]++;
					var num=subRegionsCount[broker.idSubRegion];
					var desfasa=0.0023;
					switch (num){
						case 1: broker.lat=(broker.lat*1)-desfasa;broker.lng=(broker.lng*1)-desfasa;break;
						case 2: broker.lat=(broker.lat*1)-desfasa;broker.lng=(broker.lng*1)+desfasa;break;
						case 3: broker.lat=(broker.lat*1)+desfasa;broker.lng=(broker.lng*1)-desfasa;break;
						case 4: broker.lat=(broker.lat*1)+desfasa;broker.lng=(broker.lng*1)+desfasa;break;
					}
					
					var iconMap = new GIcon({iconAnchor:new GPoint(7, 7),shadowSize:new GSize(0, 0),iconSize:new GSize(26,35), image:broker.mapicon});
					iconMap.iconAnchor = new GPoint(21, 9);
					iconMap.infoWindowAnchor = new GPoint(2, 1);
					var marker = new GMarker(new GLatLng(broker.lat,broker.lng), {icon:iconMap,draggable:false,bouncy:false});
					marker.idBroker=broker.idBroker;
					var html = "<div class=\"contentInfo\"><div style=\"height:80px;\"><a onClick=\"loadBrokerWindow("+broker.idBroker+");return false;\" href=\"#\"  ><img src=\""+broker.thumburl+"\" title=\"click to view detail\"/></a></div><div class=\"bgImage\"><p  onClick=\"loadBrokerWindow("+broker.idBroker+");return false;\" style='cursor:pointer;'><b>"+ broker.name +"</b></p><p>"+ broker.subRegion +"</p></div></div>";
					
					var handler = createMarkerClickHandler(marker, html);
					
					GEvent.addListener(marker, 'click', handler);
					brokerOverlays.push(marker)
					gsmap.addOverlay(marker);
				}
			}
		}).send();
	}
	
	return false;
}

function gotoBroker(idBroker){
		for(var i = 0;i< brokerOverlays.length;i++){
			if(brokerOverlays[i].idBroker==idBroker){
					gsmap.setCenter(brokerOverlays[i].getLatLng());
					GEvent.trigger(brokerOverlays[i],"click");
					break;
				}
			}
	}
	
function loadDevFormStatesSearch (loadTo) {	
	if(loadTo.length>0){
		for(var i = 0;i<loadTo.length;i++){
			var formu = document.getElementById(loadTo[i]);
			var dstate = formu.dstate;
			var dregion = formu.dregion;
			var dsubRegion1 = formu.dsubRegion1;
			var dsubRegion2 = formu.dsubRegion2;						
			if(dstate&&dregion&&dsubRegion1&&dsubRegion2){
				//state onchange event
				dstate.onchange = loadDevRegionGeneric; 
				dregion.onchange = loadDevRegionGeneric;
				dsubRegion1.onchange= loadDevRegionGeneric;
				dsubRegion2.onchange= loadDevRegionGeneric;
			} else {
				alert("Malformed search form.");
			}
			
		}
	}
}

function loadRentalFormStatesSearch (loadTo) {
	if(loadTo.length>0){
		for(var i = 0;i<loadTo.length;i++){
			var formu = document.getElementById(loadTo[i]);
			var rstate = formu.rstate;
			var rregion = formu.rregion;
			var rsubRegion1 = formu.rsubRegion1;
			var rsubRegion2 = formu.rsubRegion2;			
			if(rstate&&rregion&&rsubRegion1&&rsubRegion2){
				//state onchange event
				rstate.onchange = loadRentalRegionGeneric; 
				rregion.onchange = loadRentalRegionGeneric;
				rsubRegion1.onchange= loadRentalRegionGeneric;
				rsubRegion2.onchange= loadRentalRegionGeneric;
			} else {
				alert("Malformed search form.");
			}
			
		}
		//rstate.selectedIndex=1;
		//loadRentalRegionGeneric(loadTo);	
		//loadDevRegionGeneric("oLoadStateCommercial");
		//document.getElementById("rentalSearch").region.selectedIndex=1;
		//loadDevRegionGeneric("oLoadRegion");
	}
}
function loadCommercialFormStatesSearch (loadTo) {
	if(loadTo.length>0){
		for(var i = 0;i<loadTo.length;i++){
			var formu = document.getElementById(loadTo[i]);
			var cstate = formu.cstate;
			var cregion = formu.cregion;
			var csubRegion1 = formu.csubRegion1;
			var csubRegion2 = formu.csubRegion2;			
			if(cstate&&cregion&&csubRegion1&&csubRegion2){
				//state onchange event
				cstate.onchange = loadCommercialRegionGeneric; 
				cregion.onchange = loadCommercialRegionGeneric;
				csubRegion1.onchange= loadCommercialRegionGeneric;
				csubRegion2.onchange= loadCommercialRegionGeneric;
			} else {
				alert("Malformed search form.");
			}
			
		}
		//cstate.selectedIndex=1;
		//loadCommercialRegionGeneric(loadTo);	
		//loadDevRegionGeneric("oLoadStateCommercial");
		//document.getElementById("rentalSearch").region.selectedIndex=1;
		//loadDevRegionGeneric("oLoadRegion");
	}
}
function loadDevRegionGeneric(shoot) {
	gsmap.closeExtInfoWindow();	
	var that=this;
	//alert(that.name);
	if(that.name=="dregion"){
		if(that[that.selectedIndex].value==0){
			that.form.dregion.disabled=false;
			that.form.dsubRegion1[0].selected=true;
			that.form.dsubRegion1.disabled=true;
			that.form.dsubRegion2[0].selected=true;
			that.form.dsubRegion2.disabled=true;
			callSearchCheck();
			return;
		}
		$("subRegion1LabelDev").innerHTML=that.options[that.selectedIndex].getAttribute("hLevel");
		$("subRegion2LabelDev").innerHTML=that.options[that.selectedIndex].getAttribute("lLevel");
	}
	else if(that.name == "dstate"&&that[that.selectedIndex].value==0){
		that.form.dregion[0].selected=true;
		that.form.dregion.disabled=true;
		that.form.dsubRegion1[0].selected=true;
		that.form.dsubRegion1.disabled=true;
		that.form.dsubRegion2[0].selected=true;
		that.form.dsubRegion2.disabled=true;
		if(window.stateCallbackFunction&&shoot!="wait"&&shoot!="reset") { 
			stateCallbackFunction(that);				
		}		
	}
	else if(that.name=="dsubRegion1"&&that[that.selectedIndex].value==0) {
		that.form.dsubRegion2[0].selected=true;
		that.form.dsubRegion2.disabled=false;
		that.form.dregion.onchange();
		return;
	}	
	else if(that.name=="dsubRegion2"&&that[that.selectedIndex].value!=0) {
		if(window.subRegion2CallbackFunction&&shoot!="wait"&&shoot!="reset") {
			subRegion2CallbackFunction(that);
			callSearchCheck();
		}
		return;
	}
	
	if(that[that.selectedIndex].value==0) {
			that.form.dsubRegion1.onchange();
			callSearchCheck();
		return;
	}	
	//alert('gateway/genericRegion.php?type='+that.name+'&id='+that.options[that.selectedIndex].value);
	var req = new Request({  
		
		method: 'get',  
		url: 'gateway/genericRegion.php?type='+that.name+'&id='+that.options[that.selectedIndex].value ,
		onComplete: function(result) {
			var Json = JSON.decode(result);
			var inLoad= new Array();
			if(that.name == "dstate"){ 
				inLoad[0] = that.form.dregion;
				inLoad[0].innerHTML="";
				that.form.dregion.disabled=false;
				that.form.dsubRegion1.disabled=true;
				that.form.dsubRegion2.disabled=true;
				if(window.stateCallbackFunction&&shoot!="wait"&&shoot!="reset") { 
					stateCallbackFunction(that);				
				}
			}
			else if(that.name == "dregion"){
				inLoad[0] = that.form.dsubRegion1; 
				inLoad[0].innerHTML="";
				inLoad[1] = that.form.dsubRegion2;
				inLoad[1].innerHTML="";
				that.form.dregion.disabled=false;
				that.form.dsubRegion1.disabled=false;
				that.form.dsubRegion2.disabled=false;
				if(window.regionCallbackFunction&&shoot!="waitReg"&&shoot!="resetReg") regionCallbackFunction(that);
			}
			else if(that.name == "dsubRegion1"){ 
				inLoad[0] = that.form.dsubRegion2;
				
				inLoad[0].innerHTML="";
				if(window.subRegion1CallbackFunction&&shoot!="waitSub1"&&shoot!="resetSub1")subRegion1CallbackFunction(that);
			}
			
			for (i=0; i< Json.length; i++){
				var option = new Element('option');
				option.value = Json[i].value;
				option.text = Json[i].text;
				option.label = Json[i].text;
				if(Json[i].hLevel)option.setAttribute("hLevel",Json[i].hLevel);
				if(Json[i].lLevel)option.setAttribute("lLevel",Json[i].lLevel);
				if(Json[i].lat)option.setAttribute("lat",Json[i].lat);
				if(Json[i].lng)option.setAttribute("lng",Json[i].lng);
				if(Json[i].zoomLevel)option.setAttribute("zoomLevel",Json[i].zoomLevel);

				try {//this try catch is for ie / FF incompatibility con metohd add on OPTION elements
					if(that.name != "dregion")inLoad[0].add(option);
					else {
						if(Json[i].hLevel=="true")inLoad[0].add(option);
						else if(Json[i].hLevel=="false")inLoad[1].add(option);
						else {inLoad[0].add(option); // para poner el ALL en los dos cositos
							var opt2=new Element('option');opt2.value=option.value;opt2.text=option.text;opt2.label=option.label;
							inLoad[1].add(opt2);
							}
						}
				}
				catch (e){//this is for FF
					if(that.name != "dregion")inLoad[0].add(option,null);
					else {
						if(Json[i].hLevel=="true")inLoad[0].add(option,null);
						else if(Json[i].hLevel=="false")inLoad[1].add(option,null);
						else {inLoad[0].add(option,null);inLoad[1].add(new Element('option',{'value':option.value,'text':option.text,'label':option.label}),null);}
						}
				}
			}
			
			if(that.name == "dstate"&&(shoot=="wait"||shoot=="reset")){
				for(i=0;i<that.form.dregion.options.length;i++)
				{
					if(that.form.dregion.options[i].value == defaultRegion)
					{
						that.form.dregion.selectedIndex=i;
						if(shoot=="wait"){						
							that.form.dregion.onchange("waitReg");
						} else {
							that.form.dregion.onchange("resetReg");
						}
					}
				}
			}
			if(that.name == "dregion"&&(shoot=="waitReg"||shoot=="resetReg")){
				for(i=0;i<that.form.dsubRegion1.options.length;i++)
				{
					if(that.form.dsubRegion1.options[i].value == defaultSubRegion1)
					{					
						that.form.dsubRegion1.selectedIndex=i;
						if(shoot=="waitReg"){
							that.form.dsubRegion1.onchange("waitSub1");
						} else {
							that.form.dsubRegion1.onchange("resetSub1");
						}
					}
				}
			}			
			else if((shoot!="waitReg"&&shoot!="wait"&&shoot!="waitSub1")||shoot=="resetSub1")
				callSearchCheck();
		} 
	}).send();
}
	
function loadRentalRegionGeneric(shoot) {
	gsmap.closeExtInfoWindow();
	var that = this;
	
	if(that.name=="rregion"){
		if(that[that.selectedIndex].value==0){
			that.form.rregion.disabled=false;
			that.form.rsubRegion1[0].selected=true;
			that.form.rsubRegion1.disabled=true;
			that.form.rsubRegion2[0].selected=true;
			that.form.rsubRegion2.disabled=true;
			searchRental();
			return;
		}
		$("subRegion1LabelDev").innerHTML=that.options[that.selectedIndex].getAttribute("hLevel");
		$("subRegion2LabelDev").innerHTML=that.options[that.selectedIndex].getAttribute("lLevel");
	}
	else if(that.name == "rstate"&&that[that.selectedIndex].value==0){
		//alert("1."+that.name);
		that.form.rregion[0].selected=true;
		that.form.rregion.disabled=true;
		that.form.rsubRegion1[0].selected=true;
		that.form.rsubRegion1.disabled=true;
		that.form.rsubRegion2[0].selected=true;
		that.form.rsubRegion2.disabled=true;
		if(window.stateCallbackFunction&&shoot!="wait"&&shoot!="reset") { 
			stateCallbackFunction(that);				
		}		
	}
	else if(that.name=="rsubRegion1"&&that[that.selectedIndex].value==0) {
		that.form.rsubRegion2[0].selected=true;
		that.form.rsubRegion2.disabled=false;
		//that.form.rregion.onchange();
		searchRental();
		return;
	}	
	else if(that.name=="rsubRegion2"&&that[that.selectedIndex].value!=0) {
		if(window.subRegion2CallbackFunction&&shoot!="wait"&&shoot!="reset") {
			subRegion2CallbackFunction(that);
			searchRental();
		}
		return;
	}
	
	if(that[that.selectedIndex].value==0) {
			that.form.rsubRegion1.onchange();
			searchRental();
		return;
	}	
	
	var req = new Request({  
		method: 'get',  
		url: 'gateway/genericRegion.php?type='+that.name+'&id='+that.options[that.selectedIndex].value ,
		onComplete: function(result) {
			var Json = JSON.decode(result);
			var inLoad= new Array();
			if(that.name == "rstate"){ 
			//alert("2."+that.name);
				inLoad[0] = that.form.rregion;
				inLoad[0].innerHTML="";
				that.form.rregion.disabled=false;
				that.form.rsubRegion1.disabled=true;
				that.form.rsubRegion2.disabled=true;
				if(window.stateCallbackFunction&&shoot!="wait"&&shoot!="reset") { 
					stateCallbackFunction(that);				
				}
			}
			else if(that.name == "rregion"){
				inLoad[0] = that.form.rsubRegion1; 
				inLoad[0].innerHTML="";
				inLoad[1] = that.form.rsubRegion2;
				inLoad[1].innerHTML="";
				that.form.rregion.disabled=false;
				that.form.rsubRegion1.disabled=false;
				that.form.rsubRegion2.disabled=false;
				if(window.regionCallbackFunction&&shoot!="waitReg"&&shoot!="resetReg") regionCallbackFunction(that);
			}
			else if(that.name == "rsubRegion1"){ 
				inLoad[0] = that.form.rsubRegion2;
				
				inLoad[0].innerHTML="";
				if(window.subRegion1CallbackFunction&&shoot!="waitSub1"&&shoot!="resetSub1")subRegion1CallbackFunction(that);
			}
			
			for (i=0; i< Json.length; i++){
				var option = new Element('option');
				option.value = Json[i].value;
				option.text = Json[i].text;
				option.label = Json[i].text;
				if(Json[i].hLevel)option.setAttribute("hLevel",Json[i].hLevel);
				if(Json[i].lLevel)option.setAttribute("lLevel",Json[i].lLevel);
				if(Json[i].lat)option.setAttribute("lat",Json[i].lat);
				if(Json[i].lng)option.setAttribute("lng",Json[i].lng);
				if(Json[i].zoomLevel)option.setAttribute("zoomLevel",Json[i].zoomLevel);

				try {//this try catch is for ie / FF incompatibility con metohd add on OPTION elements
					if(that.name != "rregion")inLoad[0].add(option);
					else {
						if(Json[i].hLevel=="true")inLoad[0].add(option);
						else if(Json[i].hLevel=="false")inLoad[1].add(option);
						else {inLoad[0].add(option); // para poner el ALL en los dos cositos
							var opt2=new Element('option');opt2.value=option.value;opt2.text=option.text;opt2.label=option.label;
							inLoad[1].add(opt2);
							}
						}
				}
				catch (e){//this is for FF
					if(that.name != "rregion")inLoad[0].add(option,null);
					else {
						if(Json[i].hLevel=="true")inLoad[0].add(option,null);
						else if(Json[i].hLevel=="false")inLoad[1].add(option,null);
						else {inLoad[0].add(option,null);inLoad[1].add(new Element('option',{'value':option.value,'text':option.text,'label':option.label}),null);}
						}
				}
			}
			
			if(that.name == "rstate"&&(shoot=="wait"||shoot=="reset")){
				//alert("3."+that.name);
				for(i=0;i<that.form.rregion.options.length;i++)
				{
					if(that.form.rregion.options[i].value == defaultRegion)
					{
						that.form.rregion.selectedIndex=i;
						if(shoot=="wait"){						
							that.form.rregion.onchange("waitReg");
						} else {
							that.form.rregion.onchange("resetReg");
						}
					}
				}
			}
			if(that.name == "rregion"&&(shoot=="waitReg"||shoot=="resetReg")){
				for(i=0;i<that.form.rsubRegion1.options.length;i++)
				{
					if(that.form.rsubRegion1.options[i].value == defaultSubRegion1)
					{					
						that.form.rsubRegion1.selectedIndex=i;
						if(shoot=="waitReg"){
							that.form.rsubRegion1.onchange("waitSub1");
						} else {
							that.form.rsubRegion1.onchange("resetSub1");
						}
					}
				}
			}			
			else if((shoot!="waitReg"&&shoot!="wait"&&shoot!="waitSub1")||shoot=="resetSub1")
				searchRental();
		} 
	}).send();
}

function loadCommercialRegionGeneric(shoot) {
	gsmap.closeExtInfoWindow();	
	var that = this;
	if(that.name == "csubRegion2" && parseInt($("csubRegion2").value) <= 0 && shoot != "wait" && shoot != "reset"){
		if(document.getElementById("houndFormsearch")){
			var formu = document.getElementById("houndFormsearch");
			for (var i=0; i<formu.houndBoro.options.length; i++) {
				if(formu.houndBoro.options[i].value == $("csubRegion1").value){
					formu.houndBoro.options[i].selected = true;
				}else{
					formu.houndBoro.options[i].selected = false;
				}
			}
			for (var i=0; i<formu.houndNei.options.length; i++) {
				formu.houndNei.options[i].selected = false;
			}
			formu.houndNei.options[0].selected = true;
		}else if(document.getElementById("houndBoroHid")){
			document.getElementById("houndBoroHid").value = $('csubRegion1').value;
			document.getElementById("houndNeiHid").value = '';
		}
	}else if(that.name == "csubRegion2" && parseInt($("csubRegion2").value) > 0){
		if(document.getElementById("houndFormsearch")){
			var formu = document.getElementById("houndFormsearch");
			for (var i=0; i<formu.houndNei.options.length; i++) {
				if(formu.houndNei.options[i].value == $("csubRegion2").value){
					formu.houndNei.options[i].selected = true;
				}else{
					formu.houndNei.options[i].selected = false;
				}
			}
		}else if(document.getElementById("houndBoroHid")){
			document.getElementById("houndBoroHid").value = $('csubRegion1').value;
			document.getElementById("houndNeiHid").value = $('csubRegion2').value;
		}
	}	
	if(that.name=="cregion"){
		if(that[that.selectedIndex].value==0){
			that.form.cregion.disabled=false;
			that.form.csubRegion1[0].selected=true;
			that.form.csubRegion1.disabled=true;
			that.form.csubRegion2[0].selected=true;
			that.form.csubRegion2.disabled=true;
			searchCommercial();
			return;
		}
		$("subRegion1LabelDev").innerHTML=that.options[that.selectedIndex].getAttribute("hLevel");
		$("subRegion2LabelDev").innerHTML=that.options[that.selectedIndex].getAttribute("lLevel");
	}
	else if(that.name == "cstate"&&that[that.selectedIndex].value==0){
		that.form.cregion[0].selected=true;
		that.form.cregion.disabled=true;
		that.form.csubRegion1[0].selected=true;
		that.form.csubRegion1.disabled=true;
		that.form.csubRegion2[0].selected=true;
		that.form.csubRegion2.disabled=true;
		if(window.stateCallbackFunction&&shoot!="wait"&&shoot!="reset") { 
			stateCallbackFunction(that);				
		}		
	}
	else if(that.name=="csubRegion1"&&that[that.selectedIndex].value==0) {
		that.form.csubRegion2[0].selected=true;
		that.form.csubRegion2.disabled=false;
		searchCommercial();
		//that.form.cregion.onchange();
		return;
	}	
	else if(that.name=="csubRegion2"&&that[that.selectedIndex].value!=0) {
		var nbhhId = that[that.selectedIndex].value;
		if(window.subRegion2CallbackFunction&&shoot!="wait"&&shoot!="reset") {
			subRegion2CallbackFunction(that);
			searchCommercial();
		}
		if(document.getElementById("hound_detailinfo").style.display == "block"){
			searchValuehound();
		}
		return;
	}
	
	if(that[that.selectedIndex].value==0) {
		that.form.csubRegion1.onchange();
		searchCommercial();
		if(that.name=="csubRegion2") {
			if(document.getElementById("hound_detailinfo").style.display == "block"){
				setHoundnewsearch();
			}
		}
		return;
	}	
	if(document.getElementById("hound_detailinfo").style.display == "block"){
		if(that.name=="csubRegion2"&&that[that.selectedIndex].value!=0) {
			$('houndListDiv').set("html","<center><img src='images/ajax-loader.gif' /></center>");
		}else if(that.name=="csubRegion1"&&that[that.selectedIndex].value!=0) {
			if(document.getElementById("houndFormsearch")){
				if(document.getElementById("houndBoro").value != that[that.selectedIndex].value){
					for(var b=0; b<document.getElementById("houndBoro").options.length; b++){
						if(document.getElementById("houndBoro").options[b].value == that[that.selectedIndex].value){
							document.getElementById("houndBoro").options[b].selected = true;
						}else{
							document.getElementById("houndBoro").options[b].selected = false;
						}
					}
					document.getElementById("houndFormsearch").houndBoro.onchange();
				}
			}
		}
	}
	var req = new Request({  
		method: 'get',  
		url: 'gateway/genericRegion.php?type='+that.name+'&id='+that.options[that.selectedIndex].value ,
		onComplete: function(result) {
			var Json = JSON.decode(result);
			var inLoad= new Array();
			if(that.name == "cstate"){ 
				inLoad[0] = that.form.cregion;
				inLoad[0].innerHTML="";
				that.form.cregion.disabled=false;
				that.form.csubRegion1.disabled=true;
				that.form.csubRegion2.disabled=true;
				if(window.stateCallbackFunction&&shoot!="wait"&&shoot!="reset") { 
					stateCallbackFunction(that);				
				}
			}
			else if(that.name == "cregion"){
				inLoad[0] = that.form.csubRegion1; 
				inLoad[0].innerHTML="";
				inLoad[1] = that.form.csubRegion2;
				inLoad[1].innerHTML="";
				that.form.cregion.disabled=false;
				that.form.csubRegion1.disabled=false;
				that.form.csubRegion2.disabled=false;
				if(window.regionCallbackFunction&&shoot!="waitReg"&&shoot!="resetReg") regionCallbackFunction(that);
			}
			else if(that.name == "csubRegion1"){ 
				inLoad[0] = that.form.csubRegion2;
				
				inLoad[0].innerHTML="";
				if(window.subRegion1CallbackFunction&&shoot!="waitSub1"&&shoot!="resetSub1")subRegion1CallbackFunction(that);
			}
			
			for (i=0; i< Json.length; i++){
				var option = new Element('option');
				option.value = Json[i].value;
				option.text = Json[i].text;
				option.label = Json[i].text;
				if(Json[i].hLevel)option.setAttribute("hLevel",Json[i].hLevel);
				if(Json[i].lLevel)option.setAttribute("lLevel",Json[i].lLevel);
				if(Json[i].lat)option.setAttribute("lat",Json[i].lat);
				if(Json[i].lng)option.setAttribute("lng",Json[i].lng);
				if(Json[i].zoomLevel)option.setAttribute("zoomLevel",Json[i].zoomLevel);

				try {//this try catch is for ie / FF incompatibility con metohd add on OPTION elements
					if(that.name != "cregion")inLoad[0].add(option);
					else {
						if(Json[i].hLevel=="true")inLoad[0].add(option);
						else if(Json[i].hLevel=="false")inLoad[1].add(option);
						else {inLoad[0].add(option); // para poner el ALL en los dos cositos
							var opt2=new Element('option');opt2.value=option.value;opt2.text=option.text;opt2.label=option.label;
							inLoad[1].add(opt2);
							}
						}
				}
				catch (e){//this is for FF
					if(that.name != "cregion")inLoad[0].add(option,null);
					else {
						if(Json[i].hLevel=="true")inLoad[0].add(option,null);
						else if(Json[i].hLevel=="false")inLoad[1].add(option,null);
						else {inLoad[0].add(option,null);inLoad[1].add(new Element('option',{'value':option.value,'text':option.text,'label':option.label}),null);}
						}
				}
			}
			
			if(that.name == "cstate"&&(shoot=="wait"||shoot=="reset")){
				for(i=0;i<that.form.cregion.options.length;i++)
				{
					if(that.form.cregion.options[i].value == defaultRegion)
					{
						that.form.cregion.selectedIndex=i;
						if(shoot=="wait"){						
							that.form.cregion.onchange("waitReg");
						} else {
							that.form.cregion.onchange("resetReg");
						}
					}
				}
			}
			if(that.name == "cregion"&&(shoot=="waitReg"||shoot=="resetReg")){
				for(var i=0;i<that.form.csubRegion1.options.length;i++)
				{
					if(that.form.csubRegion1.options[i].value == defaultSubRegion1)
					{					
						that.form.csubRegion1.selectedIndex=i;
						if(shoot=="waitReg"){
							that.form.csubRegion1.onchange("waitSub1");
						} else {
							that.form.csubRegion1.onchange("resetSub1");
						}
					}
				}
			}			
			else if((shoot!="waitReg"&&shoot!="wait"&&shoot!="waitSub1")||shoot=="resetSub1")
				searchCommercial();
		} 
	}).send();
}
function loadHoundRegionGeneric(that) {
	gsmap.closeExtInfoWindow();	
	if(that!=""){
		that = this;
		if(that.name=="houndBoro"){
			var ajId = that.options[that.selectedIndex].value;
			var ajUrl = 'gateway/genericRegion.php?type=csubRegion1&id='+ajId;
			if(that[that.selectedIndex].value != document.getElementById("csubRegion1").value){
				if(document.getElementById("csubRegion1")){
					for(var b=0; b<document.getElementById("csubRegion1").options.length; b++){
						if(document.getElementById("csubRegion1").options[b].value == that[that.selectedIndex].value){
							document.getElementById("csubRegion1").options[b].selected = true;
						}else{
							document.getElementById("csubRegion1").options[b].selected = false;
						}
					}
					document.getElementById("csubRegion1").onchange();
				}
			}
		}else if(that.name=="houndNei"){
			if(document.getElementById("csubRegion2")){
				for(var b=0; b<document.getElementById("csubRegion2").options.length; b++){
					if(document.getElementById("csubRegion2").options[b].value == that[that.selectedIndex].value){
						document.getElementById("csubRegion2").options[b].selected = true;
					}else{
						document.getElementById("csubRegion2").options[b].selected = false;
					}
				}
				document.getElementById("csubRegion2").onchange();
			}
			/*var ajId = that.form.houndBoro[that.form.houndBoro.selectedIndex].value;
			var ajUrl = 'gateway/genericRegion.php?type=csubRegion1&id='+ajId;*/
		}
	}else{
		var ajId = document.getElementById("cregion").value;
		var ajUrl = 'gateway/genericRegion.php?type=cregion&id='+ajId;
	}
	
	var req = new Request({  
		method: 'get',  
		url: ajUrl ,
		onComplete: function(result) {
			var Json = JSON.decode(result);
			var inLoad= new Array();
			if(that == ""){
				inLoad[0] = document.getElementById("houndFormsearch").houndBoro; 
				inLoad[0].innerHTML="";
				inLoad[1] = document.getElementById("houndFormsearch").houndNei;
				inLoad[1].innerHTML="";
				document.getElementById("houndFormsearch").houndBoro.disabled=false;
				document.getElementById("houndFormsearch").houndNei.disabled=false;
			}
			else if(that.name == "houndBoro"){ 
				inLoad[0] = document.getElementById("houndFormsearch").houndNei;				
				inLoad[0].innerHTML="";
			}
			var boroughSel = "";
			for (i=0; i< Json.length; i++){
				var option = new Element('option');
				option.value = Json[i].value;
				option.text = Json[i].text;
				option.label = Json[i].text;
				if(Json[i].hLevel)option.setAttribute("hLevel",Json[i].hLevel);
				if(Json[i].lLevel)option.setAttribute("lLevel",Json[i].lLevel);
				if(Json[i].lat)option.setAttribute("lat",Json[i].lat);
				if(Json[i].lng)option.setAttribute("lng",Json[i].lng);
				if(Json[i].zoomLevel)option.setAttribute("zoomLevel",Json[i].zoomLevel);

				try {//this try catch is for ie / FF incompatibility con metohd add on OPTION elements
					if(that != ""){
						inLoad[0].add(option);
					}else {
						if(Json[i].hLevel=="true"){
							inLoad[0].add(option);
						}else if(Json[i].hLevel=="false"){
							inLoad[1].add(option);
						}else {
							inLoad[0].add(option); // para poner el ALL en los dos cositos
							var opt2=new Element('option');opt2.value=option.value;opt2.text=option.text;opt2.label=option.label;
							inLoad[1].add(opt2);
						}
					}
				}
				catch (e){//this is for FF
					if(that != ""){
						inLoad[0].add(option,null);
					}else {
						if(Json[i].hLevel=="true"){
							inLoad[0].add(option,null);
						}else if(Json[i].hLevel=="false"){
							inLoad[1].add(option,null);
						}else {
							inLoad[0].add(option,null);
							inLoad[1].add(new Element('option',{'value':option.value,'text':option.text,'label':option.label}),null);
						}
					}
				}
				if(document.getElementById("csubRegion1").value > 0){
					if(document.getElementById("csubRegion1").value == Json[i].value){
						boroughSel = i;
					}
				}
			}
			if(boroughSel > 0){
				document.getElementById("houndFormsearch").houndBoro.options[boroughSel].selected = true;
				document.getElementById("houndFormsearch").houndBoro.onchange();
			}
		} 
	}).send();
}
function clearSearchNew(type) {
	if(type=="developer") {
	var form = $("ajaxSearch");
	if(form.dsubRegion2.options[form.dsubRegion2.selectedIndex].value!=0){
		form.dsubRegion2.selectedIndex=0;
		form.dsubRegion2.onchange();
		return;
	}
	if(form.dsubRegion1.options[form.dsubRegion1.selectedIndex].value!=0){
		form.dsubRegion1.selectedIndex=0;
		form.dsubRegion1.onchange();
		return;
	}
	if(form.dregion.options[form.dregion.selectedIndex].value!=0){
		form.dregion.selectedIndex=0;
		form.dregion.onchange();
		return;
	}
	if(form.dstate.options[form.dstate.selectedIndex].value!=0){
		form.dstate.selectedIndex=0;
		form.dstate.onchange();
		return;
	}
	} else if(type=="rental") {
	var form = $("rentalSearch");
	if(form.rsubRegion2.options[form.rsubRegion2.selectedIndex].value!=0){
		form.rsubRegion2.selectedIndex=0;
		form.rsubRegion2.onchange();
		return;
	}
	if(form.rsubRegion1.options[form.rsubRegion1.selectedIndex].value!=0){
		form.rsubRegion1.selectedIndex=0;
		form.rsubRegion1.onchange();
		return;
	}
	if(form.rregion.options[form.rregion.selectedIndex].value!=0){
		form.rregion.selectedIndex=0;
		form.rregion.onchange();
		return;
	}
	if(form.rstate.options[form.rstate.selectedIndex].value!=0){
		form.rstate.selectedIndex=0;
		form.rstate.onchange();
		return;
	}
	} else if(type=="commercial") {
	var form = $("commercialSearch");
	if(form.csubRegion2.options[form.csubRegion2.selectedIndex].value!=0){
		form.csubRegion2.selectedIndex=0;
		form.csubRegion2.onchange();
		return;
	}
	if(form.csubRegion1.options[form.csubRegion1.selectedIndex].value!=0){
		form.csubRegion1.selectedIndex=0;
		form.csubRegion1.onchange();
		return;
	}
	if(form.cregion.options[form.cregion.selectedIndex].value!=0){
		form.cregion.selectedIndex=0;
		form.cregion.onchange();
		return;
	}
	if(form.cstate.options[form.cstate.selectedIndex].value!=0){
		form.cstate.selectedIndex=0;
		form.cstate.onchange();
		return;
	}
	}
}
