var dsMinimumPriceSales = new Array();
var dsMaximumPriceSales = new Array();
var dsMinimumPriceLettings = new Array();
var dsMaximumPriceLettings = new Array();

function PropertySearch_ListingType_OnClick(o){
	if (o.value == 5) {
	document.getElementById("hide").style.display ="block";
	}
	if (o.value ==6) {
	document.getElementById("hide").style.display ="none";
	if ( document.getElementsByTagName("*").igid[0].checked == false ){
		document.getElementsByTagName("*").igid[0].click();
	}
	
	}
}

function PropertySearch_ListingType_OnSelectionChange(o, categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs){
	//retrieves the price ranges
	//if we dont have any information for the current listingtype then query
	var invalidInformation = false;
	if ( o.value.toString() == "5" && ( dsMinimumPriceSales.length == 0 && dsMaximumPriceSales.length == 0 ) ){
		invalidInformation = true;
	}
	if ( o.value.toString() == "6" && ( dsMinimumPriceLettings.length == 0 && dsMaximumPriceLettings.length == 0 ) ){
		invalidInformation = true;
	}
	//invalid information?
	if ( invalidInformation ){
		var opriceranges = Searching.GetPriceRanges("homepage");
		if ( opriceranges.value != null ){
			for ( var i = 0; i < opriceranges.value.length; i ++ ){
				var oitem = opriceranges.value[i];
				switch ( oitem[0] ){
					case "5":
						//min or max price range?
						switch ( oitem[1] ){
							case "minimum":
								dsMinimumPriceSales[dsMinimumPriceSales.length] = oitem;
								break;
							case "maximum":
								dsMaximumPriceSales[dsMaximumPriceSales.length] = oitem;
								break;
						}
						//
						break;
					case "6":
						//min or max price?
						switch ( oitem[1] ){
							case "minimum":
								dsMinimumPriceLettings[dsMinimumPriceLettings.length] = oitem;
								break;
							case "maximum":
								dsMaximumPriceLettings[dsMaximumPriceLettings.length] = oitem;
								break;
						}
						//
						break;
				}
			}
		}
	}

	//start binding data
	var ominprice = document.isc.minprice;
	var omaxprice = document.isc.maxprice;
	if ( ominprice ){ ominprice.options.length = 0; }
	if ( omaxprice ){ omaxprice.options.length = 0; }
	
	switch ( o.value.toString() ){
		case "5":
			if ( ominprice ){
				for ( var i = 0; i < dsMinimumPriceSales.length; i ++ ){
					var oitem = dsMinimumPriceSales[i];
					ominprice.options[ominprice.options.length] = new Option( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] );
				}
			}
			if ( omaxprice ){
				for ( var i = 0; i < dsMaximumPriceSales.length; i ++ ){
					var oitem = dsMaximumPriceSales[i];
					omaxprice.options[omaxprice.options.length] = new Option( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] );
				}
			}
			break;
		case "6":
			if ( ominprice ){
				for ( var i = 0; i < dsMinimumPriceLettings.length; i ++ ){
					var oitem = dsMinimumPriceLettings[i];
					ominprice.options[ominprice.options.length] = new Option( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] );
				}
			}
			if ( omaxprice ){
				for ( var i = 0; i < dsMaximumPriceLettings.length; i ++ ){
					var oitem = dsMaximumPriceLettings[i];
					omaxprice.options[omaxprice.options.length] = new Option( ( oitem[4].length > 0 ? String.fromCharCode(parseInt(oitem[2])) : "" ) + oitem[3], oitem[4] );
				}
			}
			break;
	}
	var pnlLocation = document.getElementById("pnlLocation");	
	generateLocationBox( pnlLocation,  HttpManager.Document.Forms.HtmlRadio.GetSelectedItem("isc","listingtype").value);

	//PropertySearch_GetAreas( o.value.toString(), categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );
	//PropertySearch_GetPropertyTypes( o.value.toString(), categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );
	//PropertySearch_GetOffices( o.value.toString() );
}

/*function PropertySearch_GetAreas(listingtype,categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs){
	var oareas = document.isc.area;	
	if ( oareas ){
		var response = Searching.GetAreas( listingtype, categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );
		if ( response.value != null ){
			//remove options except the first
			oareas.options.length = 1;
			for ( var i = 0; i < response.value.length; i++ ){
				oareas.options[oareas.options.length] = new Option(response.value[i], response.value[i]);
			}
		}
	}
}
*/
function PropertySearch_GetPropertyTypes(listingtype,categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs){
	var opropertytypes = document.isc.pt;
	if ( opropertytypes ){
		var response = Searching.GetPropertyTypes( listingtype, categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs );
		if ( response.value != null ){
		if (opropertytypes.options) {
			opropertytypes.options.length = 1;
			for ( var i = 0; i < response.value.length; i ++ ){
				opropertytypes.options[opropertytypes.options.length] = new Option(response.value[i][1], response.value[i][0]);
			}
			}
		}
	}
}
/*
function PropertySearch_GetOffices(listingtype,offices){
	if (!offices) { offices = "" }
	var ooffices = document.isc.location;	
	if ( ooffices ){
		var response = Searching.GetOffices( listingtype, offices );
		if ( response.value != null ){
			//remove options except the first
			ooffices.options.length = 1;
			for ( var i = 0; i < response.value.length; i++ ){
				var aitem = response.value[i];
				ooffices.options[ooffices.options.length] = new Option(aitem[1], aitem[0]);
			}
		}
	}
}
*/

function PropertySearch_GetAreas(officeid,listingtype,categoryID, includedGBSIDs, excludedGBSIDs, includedMasterGBSIDs, excludedMasterGBSIDs){
	//alert(officeid)

		var response = Searching.GetAreas( officeid, listingtype, 1, '', '', includedMasterGBSIDs, '');
		
		return response;

}

function PropertySearch_GetOffices(listingtype){
	//if (!offices) { offices = "" }
	//var ooffices = document.isc.location;	
	//if ( ooffices ){
		var response = Searching.GetOffices( listingtype );
		return response;
		/*if ( response.value != null ){
			//remove options except the first
			ooffices.options.length = 1;
			for ( var i = 0; i < response.value.length; i++ ){
				var aitem = response.value[i];
				ooffices.options[ooffices.options.length] = new Option(aitem[1], aitem[0]);
			}
		}*/
//	}
}


function PropertySearch_OnSubmit(o){
	if (o.minprice && o.maxprice){
		if (!isNaN(o.minprice.value) && !isNaN(o.maxprice.value)){
			if (parseInt(o.minprice.value) >= parseInt(o.maxprice.value)){
				alert('Please select a maximum price higher than the minimum price selected');
				return false;
			}
		}
	}

	//Set properties for CIP
	var olistingtype = null;
	var ilistingtype = -1;
	if ( typeof o.listingtype != "undefined" ){
		olistingtype = o.listingtype;
	}else if ( typeof o.ListingType != "undefined" ){
		olistingtype = o.ListingType;
	}
	if ( typeof olistingtype.length == "number" ){
		//radio
		for ( var i = 0; i < olistingtype.length; i ++ ){
			if ( olistingtype[i].checked ){
				ilistingtype = olistingtype[i].value;
				break;
			}
		}
	}else{
		//select
		ilistingtype = olistingtype.value;
	}
	if ( typeof CIPInlineRegistration != "undefined" ){
		with ( CIPInlineRegistration ){
			setListingType( ilistingtype );
			setMinimumPrice( o.minprice.value );
			setMaximumPrice( o.maxprice.value );
			if ( typeof o.bedrooms != "undefined" ){
				setMinimumBedrooms( o.bedrooms.value );
			}
			if ( typeof o.area != "undefined" ){
				setAreaNames( o.area.value );
			}
		}			
	}
	//
		return false;
}

function PropertySearch_Locations_OnClick(o){
	for ( var i = 0; i < document.getElementsByTagName("*").igid.length; i ++ ){
		var oitem = document.getElementsByTagName("*").igid[i];
		if ( o.checked && o.value.length > 0 ){
			document.getElementsByTagName("*").igid[0].checked = false;
		}else if ( o.checked && o.value.length == 0 && oitem.value.length > 0 ){
			oitem.checked = false;
		}
	}
}



function pnlLocation_onClick(){
	var pnlLocation = document.getElementById("pnlLocation")
	pnlLocation.style.display="block"
}

var aLocationItems = [];
var itemsSelected =0;
 function generateLocationBox(container,listingtype){
					
						
					var qsAreas = [];
					var qsLocation = [];
					if  (HttpManager.Request.QueryString().indexOf("area") > 0){
							qsAreas = HttpManager.Request.QueryString("area").split(",")
					}
					
					if  (HttpManager.Request.QueryString().indexOf("location") > 0){
							qsLocation = HttpManager.Request.QueryString("location").split(",")
					}
			
					var offices = PropertySearch_GetOffices(listingtype);
					if ( container.childNodes.length > 0 ){
						container.removeChild( container.firstChild );
					}
					var pnlMain = document.createElement("DIV");
					pnlMain.className = "officeCheckBoxHolder"
					if ( offices.value != null ){
					
					
					var pnlHeader = document.createElement("DIV")
					pnlHeader.className = "CloseBarTop";
					//pnlFooter.onclick = showHideDIV('locationdd');
					pnlMain.appendChild(pnlHeader);
					
					var lnkClose = document.createElement("A")
					lnkClose.onclick = function(){container.style.display='none';}
					lnkClose.className="WhiteLink";
					lnkClose.innerHTML= "Close "
					pnlHeader.appendChild(lnkClose)
					
					var txtClose = document.createElement("SPAN")
					txtClose.className ="RedTxt";
					txtClose.innerHTML = "x"
					lnkClose.appendChild(txtClose)
					
					for ( var i = 0; i < offices.value.length; i++ ){
							var aitem = offices.value[i];
						
							var areas = PropertySearch_GetAreas(aitem[0],
																		listingtype,HttpManager.Document.GetObject("categoryID").value, 
																		document.isc.igid.value, 
																		document.isc.egid.value, 
																		document.isc.imgid.value, 
																		document.isc.emgid.value );	
																		
																			
							if ( areas.value.length > 0 ){
						
								var pnlLocationContainer = document.createElement("DIV")
								pnlLocationContainer.className = "pnlLocationContainer"
							
								var pnlAreaContainer = document.createElement("DIV");
								pnlAreaContainer.id = "pnlAreaContainer_" + aitem[0];
								
								var pnlOfficeContainerTitle = document.createElement("DIV");
								
								pnlLocationContainer.appendChild(pnlOfficeContainerTitle)
								pnlLocationContainer.appendChild(pnlAreaContainer)
									
									
								chkOffice = document.createElement("INPUT")
								chkOffice.type="checkbox";
								chkOffice.className = "officeCheckBoxHolder"
								chkOffice.name="location"
								chkOffice.value= aitem[0];
								pnlAreaContainer.appendChild(chkOffice)
								

								if (qsLocation.length > 0){
									for (var j =0; j< qsLocation.length; j++){
										if(chkOffice.value == qsLocation[j] ){
											chkOffice.checked = true;
										}
									}
								}
								
								var txtOfficeName = document.createElement("SPAN");
								txtOfficeName.innerHTML = aitem[1] + ' Office';
								pnlAreaContainer.appendChild(txtOfficeName);
								

								if ( areas.value != null ){
									for ( var x = 0; x <  areas.value.length ; x++ ){
										var item = areas.value[x];
										var pnlArea = document.createElement("DIV");
										pnlArea.className="OfficeSubAreas";
										
										var chkArea = document.createElement("INPUT");
										chkArea.name = "area"
										chkArea.type="checkbox";
										chkArea.classname = "OfficeCheckBox"
										chkArea.value = item;
										var txtLocation = document.getElementById("txtLocation")
										chkArea.onclick = function(){
											if (this.checked){
												 itemsSelected++
											} else { itemsSelected--}
											
											if (txtLocation){
												txtLocation.innerHTML = "(" + (itemsSelected) + ") Selected" ;
											}
										}
										
										pnlArea.appendChild(chkArea);
										aLocationItems.push( chkArea );
										
										if (qsAreas.length > 0){
											for (var j =0; j< qsAreas.length; j++){
												if(chkArea.value == qsAreas[j].replace("+"," ") ){
													chkArea.checked = true;
													itemsSelected++;
												}
											}
											txtLocation.innerHTML = "(" + (qsAreas.length)  + ") Selected" ;
										}
										
										txtAreaName = document.createElement("SPAN");
										txtAreaName.innerHTML = item;
										pnlArea.appendChild(txtAreaName);
										pnlAreaContainer.appendChild(pnlArea);
										
									}
								}
								
								chkOffice.onclick = chkOffice_onClick;
								pnlMain.appendChild(pnlLocationContainer);		
											
							}					
							
							
							

						
						}
						
							var pnlFooter = document.createElement("DIV")
							pnlFooter.className = "CloseBar";
							//pnlFooter.onclick = showHideDIV('locationdd');
							pnlMain.appendChild(pnlFooter);
							
							var lnkClose = document.createElement("A")
							lnkClose.onclick = function(){container.style.display='none';}
							lnkClose.className="WhiteLink";
							lnkClose.innerHTML= "Close "
							pnlFooter.appendChild(lnkClose)
							
							
							var txtClose = document.createElement("SPAN")
							txtClose.className ="RedTxt";
							txtClose.innerHTML = "x"
							lnkClose.appendChild(txtClose)
						}
					

					if (container){
						container.appendChild(pnlMain);
					}
}


function chkOffice_onClick(){
//if(this.checked){itemsSelected++}else{itemsSelected--}
var container = document.getElementById("pnlAreaContainer_" + this.value)
    if(container){
        for (var i=0; i<container.childNodes.length; i++){
            var item = container.childNodes[i]
            if(item.tagName == "DIV"){
               if(item.childNodes[0].type == "checkbox"){
					if(!this.checked){
						if(item.childNodes[0].checked){
							item.childNodes[0].checked = false; 
							itemsSelected--;
						}
					}else{
						if(!item.childNodes[0].checked){
							item.childNodes[0].checked = true;
							itemsSelected++;
						}
					}
               }
            }
        }
    }
    
    var txtLocation = document.getElementById("txtLocation")
    if (txtLocation){
		txtLocation.innerHTML = "(" + itemsSelected + ") Selected" ;
    }
   // alert(itemsSelected)
}
