$j = jQuery.noConflict();
$j(document).ready(function(){
	//();	
});

var searchBox = {
	Init:function(){
		
		// objects
		searchBox.CountryDropDown = $j('#selectCountry')[0];
		searchBox.CountryDropDownOuter = $j('#selectCountryOuter')[0];
		searchBox.RegionDropDown = $j('#selectRegion')[0];
		searchBox.RegionDropDownOuter = $j('#selectRegionOuter')[0];
		
		searchBox.MinPriceDropDown = $j('#selectMinPrice')[0];
		searchBox.MinPriceDropDownOuter = $j('#selectMinPriceOuter')[0];
		
		searchBox.MaxPriceDropDown = $j('#selectMaxPrice')[0];
		searchBox.MaxPriceDropDownOuter = $j('#selectMaxPriceOuter')[0];
		
		searchBox.MinBedDropDown = $j('#selectMinBed')[0];
		searchBox.MinBedDropDownOuter = $j('#selectMinBedOuter')[0];
		
		searchBox.MaxBedDropDown = $j('#selectMaxBed')[0];
		searchBox.MaxBedDropDownOuter = $j('#selectMaxBedOuter')[0];
		
		searchBox.CurrencyDropDown = $j('#selectCurrency')[0];
		searchBox.CurrencyDropDownOuter = $j('#selectCurrencyOuter')[0];
		
		searchBox.AutoCompleteTextBox = $j('#searchIntellLocation')[0];
		searchBox.PropertyNameTextBox = $j('#searchPropertyName')[0];
		searchBox.ReferenceTextBox = $j('#searchPropertyID')[0];
		searchBox.SubmitButton = $j('#searchSubmit')[0];
		searchBox.MessageBox = $j('#searchMessageBox')[0];
		searchBox.PriceUnitValueControl = $j('#ctr_searchBox_PriceUnitValue');
		searchBox.KeywordsTextBox = $j('#ctr_searchBox_Keywords');
		searchBox.isLondonLocation = false; //$j('#ctr_searchBox_isLondonLocation');
		
		// prepare Controls
		this.EnableMinControls();
		
		// Watermark Controls
		$j('#searchIntellLocation').Watermark("Enter Location","#0271D4");
		$j('#searchPropertyName').Watermark("Enter Property Name","#0271D4");
		$j('#searchPropertyID').Watermark("Enter Property ID","#0271D4");
		$j(searchBox.KeywordsTextBox).Watermark("Enter Keywords","#0271D4");
		
		// DataBind()
		this.FillDropDown(this.getCountries(),this.CountryDropDown);
		this.FillDropDown(this.getCurrencies(),this.CurrencyDropDown);
		//this.FillDropDown(this.getPriceRanges(),this.MinPriceDropDown);
		//this.FillDropDown(this.getPriceRanges(),this.MaxPriceDropDown);
		//this.FillDropDown(this.getBedRanges(),this.MinBedDropDown);
		//this.FillDropDown(this.getBedRanges(),this.MaxBedDropDown);
		
		// events
		$j(this.SubmitButton).click(function(e){searchBox.onSubmit(e);});
		$j(this.CountryDropDown).change(function(e){searchBox.onCountryChanged(e);});
		$j(this.RegionDropDown).change(function(e){searchBox.onRegionChanged(e);});
		$j(this.CurrencyDropDown).change(function(e){searchBox.onCurrencyChanged(e);});
		$j(this.MinPriceDropDown).change(function(e){searchBox.onMinPriceChanged(e);});
		$j(this.MaxPriceDropDown).change(function(e){searchBox.onMaxPriceChanged(e);});
		$j(this.MinBedDropDown).change(function(e){searchBox.onMinBedChanged(e);});
		$j(this.MaxBedDropDown).change(function(e){searchBox.onMaxBedChanged(e);});
		
		// get current controls value
		this.SelectedCurrencyCode = $j("option:selected",this.CurrencyDropDown).val();
		
		// auto complete
		$j(this.AutoCompleteTextBox).autocomplete('/tmc.system/tmc.services.locations.asp', {
		    delay: 100,
		    width: 400,
		    scroll: false,
		    formatItem: formatItem,
		    formatResult: formatResult,
			selectFirst: true,
		    extraParams: {
				country: "Myanmar",
		        countryID: "",
				GetLocationsByPrefix:"a"
		    }
	    });
		
		// auto complete for keywords
		$j(this.KeywordsTextBox).autocomplete('/tmc.system/tmc.services.locations.asp', {
		    delay: 100,
		    width: 400,
		    scroll: false,
		    formatItem: formatItem,
		    formatResult: formatResult,
			selectFirst: true,
		    extraParams: {
				country: "Myanmar",
		        countryID: "",
				GetkeywordsByPrefix:"a"
		    }
	    });
		
		$j(this.AutoCompleteTextBox).result(function(event,data,formatted){
			//alert(data);
			var level = data[4];
			var IDs;
			var regionID;
                //alert(data);
            IDs = data[3].split('_');
			if(level==1){ regionID = data[2]; }
			else{ regionID = IDs[0]; }
			
			Utils.DropDownSelect(searchBox.RegionDropDown,regionID);
			
			// set loation id
			searchBox.SelectedLocationID = data[2];
			
			// now check if the selected location is in Greater London?
			// if (IDs[0]==50503&&IDs[1]==0) { - Russ - 06/11/2009 remove IDs[1] check to allow /Barking and Dagenham/ level
			if (IDs[0]==50503) {
				searchBox.isLondonLocation = true;
			}
		});
	},
	onSubmit:function(e){
		e.preventDefault();
		//alert("here");
		/*alert($j('#searchForm #ctr_searchBox_minPriceIndex').length);
		alert(searchBox.MinPriceDropDown.selectedIndex);
		alert(searchBox.MaxPriceDropDown.selectedIndex);
		alert(searchBox.MinBedDropDown.selectedIndex);
		alert(searchBox.MaxBedDropDown.selectedIndex);*/
		
		//alert('submitting form now');
		// now update form control with price bands index
		if (searchBox.AutoPriceIndexSelect){
			$j('#searchForm #ctr_searchBox_minPriceIndex').attr("value",searchBox.MinPriceDropDown.selectedIndex);
			$j('#searchForm #ctr_searchBox_maxPriceIndex').attr("value",searchBox.MaxPriceDropDown.selectedIndex);
		}else{
			$j('#searchForm #ctr_searchBox_minPriceIndex').attr("value",-1);
			$j('#searchForm #ctr_searchBox_maxPriceIndex').attr("value",-1);
		}
		if (searchBox.AutoBedroomIndexSelect){
			$j('#searchForm #ctr_searchBox_minBedsIndex').attr("value",searchBox.MinBedDropDown.selectedIndex);
			$j('#searchForm #ctr_searchBox_maxBedsIndex').attr("value",searchBox.MaxBedDropDown.selectedIndex);
		}else{
			$j('#searchForm #ctr_searchBox_minBedsIndex').attr("value",-1);
			$j('#searchForm #ctr_searchBox_maxBedsIndex').attr("value",-1);
		}
		$j('#searchForm #ctr_searchBox_countryName').attr("value",$j("option:selected",searchBox.CountryDropDown).text());
		$j('#searchForm #ctr_searchBox_countryID').attr("value",$j("option:selected",searchBox.CountryDropDown).val());
		$j('#searchForm #ctr_searchBox_isLondonLocation').attr("value",searchBox.isLondonLocation);
		
		$j('#searchForm').attr("action",searchBox.SubmitURL);
		$j('#searchForm').attr("method",searchBox.SubmitMethod);
		
		// now write searchFrom values to cookies
		var cookieContent;
		cookieContent = "country="+encodeURIComponent($j("option:selected",searchBox.CountryDropDown).text());
		cookieContent += "&currency="+encodeURIComponent($j("option:selected",searchBox.CurrencyDropDown).val());
		cookieContent += "&minprice="+encodeURIComponent($j("option:selected",searchBox.MinPriceDropDown).val());
		cookieContent += "&maxprice="+encodeURIComponent($j("option:selected",searchBox.MaxPriceDropDown).val());
		cookieContent += "&minbed="+encodeURIComponent($j("option:selected",searchBox.MinBedDropDown).val());
		cookieContent += "&maxbed="+encodeURIComponent($j("option:selected",searchBox.MaxBedDropDown).val());
		cookieContent += "&name="+encodeURIComponent(searchBox.PropertyNameTextBox);
		cookieContent += "&locationID="+encodeURIComponent(searchBox.SelectedLocationID);
		cookieContent += "&region="+encodeURIComponent($j("option:selected",searchBox.RegionDropDown).text());
		cookieContent += "&autocomplete="+encodeURIComponent(searchBox.AutoCompleteTextBox);
		cookieContent += "&propertyRef="+encodeURIComponent(searchBox.ReferenceTextBox);
		
		$j('#searchSelectedLocationID').attr("value",this.SelectedLocationID);
		var msg = $j("option:selected",searchBox.CurrencyDropDown).text();
		
		$j.cookie('tmc_searchForm',cookieContent);
		// -- end of cookies
		
		$j('#searchForm').submit();
		
	},
	onCountryChanged:function(e){
		e.preventDefault();
		
		// if no country selected? then reset form back to original state.
		if($j("option:selected",this.CountryDropDown).val()==0){
			this.Init();
			this.SelectedDropdown = "";
			return false;
		}
		
		this.SelectedDropdown = "country";
		
		// enable controls
		if($j(this.RegionDropDown).is(":disabled")){this.RegionDropDown.disabled=false;}
		if($j(this.PropertyNameTextBox).is(":disabled")){this.PropertyNameTextBox.disabled=false;}
		if($j(this.AutoCompleteTextBox).is(":disabled")){this.AutoCompleteTextBox.disabled=false;}
		if($j(this.MinBedDropDown).is(":disabled")){this.MinBedDropDown.disabled=false;}
		
		// set loation id
		this.SelectedLocationID = $j("option:selected",this.CountryDropDown).val();
		
		//alert($j("option:selected",this.CountryDropDown).text() + " is selected.");
		this.FillMinBedsDropDown();
		this.FillRemoteDropDown(this.RegionDropDown,this.CountryDropDown);
		//alert(Utils.getCurrencyRate($j("option:selected",this.CountryDropDown).text()));
		
		// check currency
		//alert('Currency : '+searchBox.SelectedCurrencyCode);
		//alert($j("option:selected",searchBox.CurrencyDropDown).val());
		
		// if current selected currency is not GBP EUR USD
		var currentCurrency = $j("option:selected",searchBox.CurrencyDropDown).val();
		if ((currentCurrency!='GBP'&&currentCurrency!='USD'&&currentCurrency!='EUR')||currentCurrency=='Currency'){
			// then refill dropdown with default currencies or no currency is selected
			if($j(this.MinPriceDropDown).is(":disabled")){
				this.MinPriceDropDown.disabled=false;
				this.FillMinPriceDropDown();
			}
			this.FillDropDown(this.getCurrencies(),this.CurrencyDropDown);
		}
		
		// first get default currency and rate of selected Country
		var countryRate = parseInt(Utils.getCurrencyRate($j("option:selected",this.CountryDropDown).text()));
		var countryCurrency = Utils.getCurrencyCode($j("option:selected",this.CountryDropDown).text());
		
		// if country's default Currency is not GBP EUR USD
		if(countryCurrency!='GBP'&&countryCurrency!='EUR'&&countryCurrency!='USD'){
			// now add a new currency to the drop down list
			this.AddCurrency(countryCurrency);
			//this.CurrencyCode = countryCurrency;
			//this.ExchangeRate = countryRate;
			if($j(this.MinPriceDropDown).is(":disabled")){this.MinPriceDropDown.disabled=false;}
			//this.FillMinPriceDropDown();
			
		}
		
		// check current selected CUR on the drop-down contorl
		var selectedCurrency = $j("option:selected",this.CurrencyDropDown).val();
		if(selectedCurrency == 'Currency'){
			// if not currency is selected then, set the country default currency as selected currency;
			this.SelectCurrencyByCode(countryCurrency);
		}
		
		searchBox.SelectedCurrencyCode = $j("option:selected",searchBox.CurrencyDropDown).val();
				
		this.UpdateSubmitButton();
		
		$j(this.AutoCompleteTextBox).setOptions({
			extraParams : { country:$j('option:selected',$j(this.CountryDropDown)).text(), countryID:"0",GetLocationsByPrefix:"a"}
		}).flushCache();
	},
	onRegionChanged:function(e){
		e.preventDefault();
		//alert($j("option:selected",this.RegionDropDown).text()+" region is selected.");
		//alert($j(this.AutoCompleteTextBox).val());
		this.SelectedDropdown = "region";
		
		// set loation id
		this.SelectedLocationID = $j("option:selected",this.RegionDropDown).val();
		
		if($j(this.AutoCompleteTextBox).val() != 'Enter Location'){
			$j(this.AutoCompleteTextBox).val('');
		}
		
		$j(this.AutoCompleteTextBox).setOptions({
			extraParams : { restrictedLocationID:this.SelectedLocationID,country:$j('option:selected',$j(this.CountryDropDown)).text(), countryID:"0",GetLocationsByPrefix:"a"}
		}).flushCache();
	},
	onCurrencyChanged:function(e){
		e.preventDefault();
		//alert($j(this.CurrencyDropDown).val() + " is selected.");
		searchBox.SelectedCurrencyCode = $j("option:selected",searchBox.CurrencyDropDown).val();
		//alert(searchBox.SelectedCurrencyCode);
		
		this.UpdateSubmitButton();
		
		if(this.SelectedCurrencyCode=='Currency'){
			this.SelectedMixPrice = 0;
			this.SelectedMaxPrice = 0;
			this.FillMinPriceDropDown();
			this.FillMaxPriceDropDown();
			this.MinPriceDropDown.disabled=true;
			this.MaxPriceDropDown.disabled=true;
			return false;
		}
		
		if(this.SelectedCurrencyCode=='GBP'||this.SelectedCurrencyCode=='EUR'||this.SelectedCurrencyCode=='USD'){
			//alert(searchBox.SelectedCurrencyCode);
			this.ExchangeRate = Utils.getCurrencyRateByCurrency(this.SelectedCurrencyCode);
			this.CurrencyCode = this.SelectedCurrencyCode;
		}else{
			this.ExchangeRate = parseInt(Utils.getCurrencyRate($j("option:selected",this.CountryDropDown).text()));
			//alert(this.ExchangeRate);
			this.CurrencyCode = $j("option:selected",this.CurrencyDropDown).val();
		}
		
		if(this.ExchangeRate==0){ExchangeRate=1;}
		this.FillMinPriceDropDown();
		if($j(this.MinPriceDropDown).is(":disabled")){this.MinPriceDropDown.disabled=false;}
		this.MaxPriceDropDown.disabled=true;
		this.SelectedMixPrice = 0;
		this.SelectedMaxPrice = 0;
	},
	onMinPriceChanged:function(e){
		e.preventDefault();
		if($j("option:selected",this.MinPriceDropDown).text()!="Min Price"){
			if($j(this.MaxPriceDropDown).is(":disabled")){this.MaxPriceDropDown.disabled=false;}
			this.SelectedMinPrice = $j("option:selected",this.MinPriceDropDown).val();
			this.SelectedMinPriceIndex = this.MinPriceDropDown.selectedIndex;
			this.FillMaxPriceDropDown();
		}
		else{
			this.MaxPriceDropDown.disabled=true;
			this.SelectedMixPrice = 0;
			this.SelectedMinPriceIndex = this.MinPriceDropDown.selectedIndex;
			this.SelectedMaxPrice = 0;
			this.SelectedMaxPriceIndex = this.MaxPriceDropDown.selectedIndex;
		}
	},
	onMaxPriceChanged:function(e){
		e.preventDefault();
		if($j("option:selected",this.MaxPriceDropDown).text()!="Max Price"){
			this.SelectedMaxPrice = $j("option:selected",this.MaxPriceDropDown).val();
			this.SelectedMaxPriceIndex = this.MaxPriceDropDown.selectedIndex;
		}
	},
	onMinBedChanged:function(e){
		e.preventDefault();
		if($j("option:selected",this.MinBedDropDown).text()!="Min Beds"){
			if($j(this.MaxBedDropDown).is(":disabled")){this.MaxBedDropDown.disabled=false;}
			this.SelectedMinBeds = $j("option:selected",this.MinBedDropDown).val();
			this.FillMaxBedsDropDown();
		}
		else{
			this.MaxBedDropDown.disabled=true;
			this.SelectedMinBeds = 0;
			this.SelectedMaxBeds = 0;
		}
		this.FillMaxBedsDropDown();
	},
	onMaxBedChanged:function(e){},
	FillBedDropDown:function(data,obj){
		$j(obj).removeOption(/./);
		//alert(searchBox.ExchangeRate);
		var label = "Min Beds";
		var skip = false;
		var lastItemIndex = data.length;
		
		if(obj.id=='selectMaxBed'){label="Max Beds"};
		
		$j(data).each(function(index){
			var cols = data[index].split('|');
			//$j(obj).addOption(cols[1],cols[0]);
			
			var optn = document.createElement("option");
			var value = cols[1]; //parseInt(cols[1])*searchBox.ExchangeRate;
			optn.text = cols[0].replace("@value",value).replace("@label",label);
			
			if(lastItemIndex==(index+1)&&obj.id=='selectMaxBed'){optn.text = optn.text + " > ";}
			
			optn.value = value;
			if (cols[1]!=undefined){
				if(obj.id=='selectMaxBed'){
					if(searchBox.SelectedMinBeds > value){skip=true;}else{skip=false;}
				}else{
					if(lastItemIndex==index){skip=true;}//skip the last highest item for max drop-down
				}
				if(optn.text=="Studio"&&obj.id!='selectMaxBed'){skip=false;}// if min bed label, do not skip
				if(!skip){
					$j(obj)[0].options.add(optn);
				}
			}
		});
	},
	FillPriceDropDown:function(data,obj){
		$j(obj).removeOption(/./);
		//alert(searchBox.ExchangeRate);
		var label = "Min Price";
		var skip = false;
		var lastItemIndex = data.length-1;
		
		if(obj.id=='selectMaxPrice'){label="Max Price"};
		
		$j(data).each(function(index){
			var cols = data[index].split('|');
			//$j(obj).addOption(cols[1],cols[0]);
			
			var optn = document.createElement("option");
			var value = parseInt(cols[1])*searchBox.ExchangeRate;
			if (cols.length==3) {
				optn.text = cols[2];
			}else{
				optn.text = cols[0].replace("@value",value).replace("@label",label);
				optn.text = optn.text.replace("1000k","1m").replace("2000k","2m");
			}
			
			if(lastItemIndex==index){optn.text = optn.text + " > ";}
			
			optn.value = value;
			if (cols[1]!=undefined){
				if(obj.id=='selectMaxPrice'){
					if(searchBox.SelectedMinPrice > value){skip=true;}else{skip=false;}
				}else{
					if(lastItemIndex==index){skip=true;}//skip the last highest item for max drop-down
				}
				// if label, do not skip
				if(parseInt(cols[1])<0){ skip=false; }
				
				if(!skip){
					$j(obj)[0].options.add(optn);
				}
			}
		});
	},
	FillDropDown:function(data,obj){
		$j(obj).removeOption(/./);
		
		$j(data).each(function(index){
			var cols = data[index].split('|');
			//$j(obj).addOption(cols[1],cols[0]);
			
			var optn = document.createElement("option");
			optn.text = cols[0];
			optn.value = cols[1];
			if (cols[1]!=undefined){
				$j(obj)[0].options.add(optn);}
		});
	},
	FillRemoteDropDownPopulateData:function(obj,parent,s){
						var ary = s.split('\n');
						var cols;
						$j(obj).removeOption(/./);
						Utils.ClearDropDown(obj);
						/*$j(ary).each(function(index){*/
							//cols = ary[index].split('|');
							
							//$j(obj).addOption(cols[1],cols[0]);
							var showAddlocation = allowUKNewLocation;
							if ($j(searchBox.CountryDropDown).val()<0 && obj.id == 'selectRegion'){
								showAddlocation = false;}
							var temp = allowNewLocation;
							if ($j(searchBox.CountryDropDown).val()<0){ 
							// but if UK, then dont allow user to add first region level
								allowNewLocation=showAddlocation; }
							if ($j(searchBox.CountryDropDown).val()<0 || obj.id == 'selectRegion'){allowNewLocation=false;}
							
							if(obj.id=='selectLocation1'){
								//alert($j('#selectCountry option:selected').text());
								if(secondLevelFixedCountries.exists($j('#selectCountry option:selected').text())){
									allowNewLocation = false;
								}
								if(secondLevelFixedRegions.exists($j('#selectRegion option:selected').text())){
									allowNewLocation = false;
								}
							}	
							if(obj.id=='selectLocation3'){ allowNewLocation = allowNewLocation3; }
							
							if (showPromotedLocation){
								
								var hAry = new Array();
								var pAry = new Array();
								var nAry = new Array();
								var gAry = new Array();
								var pcounter = 0;
								var ncounter = 0;
								var hcounter = 0;
								var gcounter = 0;
								var curGroupLabel = '';
								var headerAdded = false;
								
								$j(ary).each(function(index){
									cols = ary[index].split('|');
										var ltype = cols[7];
										var skipthis = false;
										if(!showUserLocationForFixedLevel){
											if(secondLevelFixedCountries.exists($j('#selectCountry option:selected').text())){
												if(obj.id=='selectRegion'||obj.id=='selectLocation1')
												{
													if($j.trim(cols[8])=='U'){
														skipthis = true;
													}
												}
											}
										}
										
										if(!skipthis){
											if(ltype==undefined){
												ltype="";
												hAry[hcounter]=ary[index];
												hcounter++;
											}else if(ltype.replace(/^\s\s*/, '').replace(/\s\s*$/, '')=='P'){
												pAry[pcounter]=ary[index];
												pcounter++;
											}else{
												nAry[ncounter]=ary[index];
												ncounter++;
											}
										}
								});
								
								if($j(hAry).length > 0){
									searchBox.AddLocationsToDropDown(hAry,$j(obj))
								}
								if($j(pAry).length > 0){
									if(groupLocationsByLabel){
										for(var i = 0;i<nAry.length;i++){
											var cols = nAry[i].split('|');
											
											if($j.trim(cols[2])=='Top Locations:'&&showTopLocationsWithPromoted){
												pAry[pcounter] = nAry[i];
											}
										}
									}
									
									var group = searchBox.AddGroupLabelToDropDown('Top Locations :',obj);
									searchBox.AddLocationsToDropDown(pAry,obj);
									
									//locations.AddLocationsToGroup(pAry,group);
								}							
								if($j(nAry).length > 0){
									if(groupLocationsByLabel){
										for(var i = 0;i<$j(nAry).length;i++){
											var cols = nAry[i].split('|');
											var skipThisLocation = false;
											//alert('cur:'+$j.trim(cols[2])+'=old:'+curGroupLabel);
											if($j.trim(cols[2])=='Top Locations:'&&showTopLocationsWithPromoted){
												skipThisLocation = true;
											}
											if(!skipThisLocation){
												gAry = new Array();										
												
												if($j.trim(cols[2])!=curGroupLabel){
													var group = searchBox.AddGroupLabelToDropDown(cols[2],obj);
												}
												gAry[0] = nAry[i];
												searchBox.AddLocationsToDropDown(gAry,obj);
												curGroupLabel = $j.trim(cols[2]);
												//alert(cols[2]);
											}
										}
									}else{
										var group = searchBox.AddGroupLabelToDropDown('Locations :',obj);
										searchBox.AddLocationsToDropDown(nAry,obj);
									}
									//locations.AddLocationsToGroup(nAry,group);
								}
								
							}else{
								searchBox.AddLocationsToDropDown(ary,obj);
							}
							allowNewLocation = temp;
						/*});*/
	},
	FillRemoteDropDown:function(obj,parent){
		
		var getURL = this.remoteURL+$j(parent).val();
		var cacheKey = "Locations:"+$j(parent).val();
		
		if(this.CacheRegions){
			// check cached locations if it already there
			if ($j.jCache.hasItem(cacheKey)){
				var data = $j.jCache.getItem(cacheKey);
				// now load data to region drop down;
				searchBox.FillRemoteDropDownPopulateData(obj,parent,data);
				return;
			}
		}
		
		if (this.SelectedDropdown=='country'){
			var countryName = $j('option:selected',parent).html();
			if(countryName=='England'){
				getURL = this.remoteURL+'-10001';}
			else if(countryName=='Scotland'){
				getURL = this.remoteURL+'-10002';}
			else if(countryName=='Northern Ireland'){
				getURL = this.remoteURL+'-10003';}
			else if(countryName=='Wales'){
				getURL = this.remoteURL+'-10004';}
		}
		
		$j.ajax({
			type: "GET",
			url:getURL,
			async:true,
			beforeSend: function(xhr) {
			},
			success:function(s){
				if(searchBox.CacheRegions){
					if (!$j.jCache.hasItem(cacheKey)){
						$j.jCache.setItem(cacheKey,s);
					}
				}
				searchBox.FillRemoteDropDownPopulateData(obj,parent,s);
			}
		});
	},
	AddGroupLabelToDropDown:function(label,dropdown){
				var optn = document.createElement("optgroup");
					optn.label = label;
					$j(optn).addClass('locationGroup');
					//optn.value = '-3';
					$j(dropdown)[0].appendChild(optn);
				return optn;
	},
	AddLocationsToDropDown:function(ary,dropdown){
				$j(ary).each(function(index){
					cols = ary[index].split('|');
					//$j(obj).addOption(cols[1],cols[0]);
					var optn = document.createElement("option");
					
				    optn.text = cols[0];
					optn.value = $j.trim(cols[1]);
					if(showPromotedLocation){
						$j(optn).attr("data",ary[index]);
					}
					
					if (cols[1]!=undefined){
						if (allowNewLocation){
							$j(dropdown)[0].options.add(optn);}
						else{
							//alert(cols[1]);
							if(cols[1]!=-2){
							$j(dropdown)[0].options.add(optn);}
						}
					}
				});
	},
	CountryDropDown:null,
	RegionDropDown:null,
	Location1DropDown:null,
	Location2DropDown:null,
	Location3DropDown:null,
	AutoCompleteTextBox:null,
	SubmitButton:null,
	CurrencyDropDown:null,
	MinPriceDropDown:null,
	MaxPriceDropDown:null,
	PriceUnitValue:0,
	KeywordsTextBox:null,
	MinBedDropDown:null,
	MaxBedDropDown:null,
	PropertyNameTextBox:null,
	ReferenceTextBox:null,
	MessageBox:null,
	ExchangeRate:1,
	CurrencyCode:"GBP",
	SelectedDropdown:"",
	SelectedMinBeds:0,
	SelectedMaxBeds:0,
	SelectedMinPrice:0,
	SelectedMinPriceIndex:0,
	SelectedMaxPrice:0,
	SelectedMaxPriceIndex:0,
	SelectedCurrencyCode:"",
	SelectedLocationID:0,
	CacheRegions:true,
	UseFreshCountryData:false,
	AutoPriceIndexSelect:true,
	AutoBedroomIndexSelect:true,
	isLondonLocation:false,
	SubmitURL:"property/search",
	SubmitMethod:"POST",
	SelectCountryByID:function(id){
		Utils.DropDownSelect(searchBox.CountryDropDown,id);
	},
	SelectRegionByID:function(id){
		Utils.DropDownSelect(searchBox.RegionDropDown,id);
	},
	SelectCurrencyByCode:function(CurrencyCode){	
		Utils.DropDownSelect(searchBox.CurrencyDropDown,CurrencyCode);
	},
	SelectMinPriceByValue:function(value){
		Utils.DropDownSelect(searchBox.MinPriceDropDown,value);
	},
	SelectMaxPriceByValue:function(value){
		Utils.DropDownSelect(searchBox.MaxPriceDropDown,value);
	},
	SelectMinBedByValue:function(value){
		Utils.DropDownSelect(searchBox.MinBedDropDown,value);
	},
	SelectMaxBedByValue:function(value){
		Utils.DropDownSelect(searchBox.MaxBedDropDown,value);
	},
	UpdateSubmitButton:function(){
		var msg = "";
		
		if($j("option:selected",searchBox.CurrencyDropDown).text()=="Currency"){  msg = "Currency";}
		if($j("option:selected",searchBox.CountryDropDown).text()==""){ if(msg==""){msg = "a Country";}else{msg="a Country and "}} 
		//alert(msg);
		
		if(msg!=""){
			msg = "Please select " + msg;
			$j(this.SubmitButton).hide();
			$j(this.MessageBox).text(msg);
		}else{
			$j(this.MessageBox).hide();
			$j(this.SubmitButton).show();
		}
	},
	IsCurrencyExist:function(CurrencyCode){
		var exist = false;
		$j("option",searchBox.CurrencyDropDown).each(function(index){
			if(searchBox.CurrencyDropDown.options[index].value==CurrencyCode && exist==false)
				exist = true;
		});
		return exist;
	},
	AddCurrency:function(CurrencyCode){
		if(!this.IsCurrencyExist(CurrencyCode)){
			//alert('addnow'+CurrencyCode);
			if(CurrencyCode!=0){
				Utils.AddOptionToDropDown(CurrencyCode,CurrencyCode,searchBox.CurrencyDropDown);
			}
		}
	},
	RemoveCurrency:function(CurrencyCode){
		if(!this.IsCurrencyExist(CurrencyCode)){
			//alert('remove now');
			alert($j("option[value='"+CurrencyCode+"']",this.CurrencyDropDown).text());
		}
	},
	FillMinPriceDropDown:function(){
		this.FillPriceDropDown(this.getPriceRanges(),this.MinPriceDropDown);
	},
	FillMaxPriceDropDown:function(){
		this.FillPriceDropDown(this.getPriceRanges(),this.MaxPriceDropDown);
	},
	FillMinBedsDropDown:function(){
		this.FillBedDropDown(this.getBedRanges(),this.MinBedDropDown);
	},
	FillMaxBedsDropDown:function(){
		this.FillBedDropDown(this.getBedRanges(),this.MaxBedDropDown);
	},
	FillPriceDropDowns:function(){
		this.FillMinPriceDropDown();
		this.FillMaxPriceDropDown
	},
	FillBedDropDowns:function(){
		this.FillBedDropDown(this.getBedRanges(),this.MinBedDropDown);
		this.FillBedDropDown(this.getBedRanges(),this.MaxBedDropDown);
	},
	remoteURL:"/tmc.system/tmc.services.locations.asp?IncludePromoted="+showPromotedLocation+"&IsDeep="+deepPromotedIncluded+"&GetLMSChildLocations=",
	remoteCountryDataURL:"/tmc.system/tmc.services.locations.asp?Country=ActiveLMS",
	EnableMinControls:function(){
		this.CountryDropDown.disabled = false;
		this.MinPriceDropDown.disabled = true;
		this.RegionDropDown.disabled = true;
		this.MaxPriceDropDown.disabled = true;
		this.MinBedDropDown.disabled = true;
		this.MaxBedDropDown.disabled = true;
		this.CurrencyDropDown.disabled = false;
		this.AutoCompleteTextBox.disabled = true;
		this.PropertyNameTextBox.disabled = true;
		this.ReferenceTextBox.disabled = false;
		this.UpdateSubmitButton();
	},
	getCountries:function(){
		if (this.UseFreshCountryData){
			var dummy = 'Please Select Country|0';
			$j.ajax({
				type: "GET",
				url:this.remoteCountryDataURL,
				async:false,
				beforeSend: function(xhr) {
				},
				success:function(s){
					// var ukLocations = 'England|35283\nScotland|50505\nWales|50535\nNorthern Ireland|50558\n';
					// s = ukLocations + s;
					var ary = s.split('\n').join(':');
					dummy = dummy + ':' + ary;
					//alert(dummy);
				}
			});
			
			return dummy.split(":");
		}else{
			return countries;
		}
	},
	getCurrencies:function(){return defaultCurrencies;},
	getPriceRanges:function(){return ("@label|-1:@value|0:@valuek|25:@valuek|50:@valuek|75:@valuek|100:@valuek|150:@valuek|200:@valuek|300:@valuek|500:@valuek|1000").split(":");},
	getBedRanges:function(){return ("@label|@label:Studio|0:1|1:2|2:3|3:4|4:5|5:6|6:7|7:8|8:9|9:10|10").split(":");}
}

function Init(){
	searchBox.Init();
}

function formatItem(row) {
    //var row = row.split("|");
    //return row[0] + " (id: " + row[1] + ")";
    return row[1].replace(/[/]/gi," &#187 ").replace(/[_]/gi," ");
}

function formatResult(row) {
	return row[0];
}

