﻿
jQuery(document).ready(function(){
	searchBox.Init();
});

function clearText(input){
	if (input.defaultValue==input.value)
	input.value = ''
}
function restoreText(input){
	if (input.value=='')
	input.value = input.defaultValue
}

var searchBox = {
	Init:function(){
		// objects
		searchBox.AutoCompleteTextBox = jQuery('#searchBoxLocation')[0];
		searchBox.AutoCompleteTypeBox = jQuery('#searchIntellType')[0];
		searchBox.SubmitButton = jQuery('#submitSearchForm')[0];
		//searchBox.MessageBox = jQuery('#searchMessageBox')[0];
        searchBox.CountriesDropDown = jQuery('#country')[0];
		

       
		jQuery(searchBox.SubmitButton).click(function(e){searchBox.onSubmit(e);});
       
		
		 jQuery(this.AutoCompleteTextBox).autocomplete('/services/GetLocations.php', {
		    delay: 400,
		    width: 400,
		    scroll: false,
		    formatItem: formatItem,
		    formatResult: formatResult,
			selectFirst: true,
			cacheLength: 0,
			max:15,
		    extraParams: {
				country: function() { 
                                    // currentCountry should be initalised to "" on the page itself
                                    return searchBox.CountriesDropDown.value;
                                        
                               },
		        locationID: 0,
				GetLocationsByPrefix:"a"
		    }
	    });
		
		jQuery(this.AutoCompleteTypeBox).autocomplete('/services/GetTypes.aspx', {
            delay: 100,
            width: 400,
            scroll: false,
            formatItem: formatItem,
            formatResult: formatResult,
            selectFirst: true,
            extraParams: {
            country: "",
            countryID: "0",
            GetTypesByPrefix:"a"
            }
         }); 
		
		jQuery(this.AutoCompleteTypeBox).result(function(event,data,formatted){
			var url = data[3];
			var name = data[1];
			var id = data[2];
			
			searchBox.SelectedTypeID = id;
			searchBox.SelectedTypeURL = url;
		});
		
		jQuery(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]; }
			
					
			// set loation id
			searchBox.SelectedLocationID = data[2];
			if(searchBox.SelectedLocationID!=0)
				jQuery("#searchBoxLocationID").val(searchBox.SelectedLocationID) ;
			else
				jQuery("#searchBoxLocationID").val(jQuery("#searchBoxLocation").val()) ;	
			
			searchBox.SelectedLocationURL = data[1];
			
			// 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();
		if(jQuery('#country').val()=='') 
		{
			alert('Please Select Country');
			jQuery('#country').focus();
			return false;
		}
		if(searchBox.SelectedLocationID!=0)
			jQuery("#searchBoxLocationID").val(searchBox.SelectedLocationID) ;	
		else
			jQuery("#searchBoxLocationID").val(jQuery("#searchBoxLocation").val()) ;	
			
        jQuery('#frmQuickSearch').submit();
	},
    FillCountryDropDown:function(data,obj){
		//jQuery(obj).removeOption(/./);
		
		jQuery(data).each(function(index){
			var cols = data[index].split('|');
			//jQuery(obj).addOption(cols[1],cols[0]);
			
			var optn = document.createElement("option");
			optn.text = cols[0];
			optn.value = cols[0];
			if (cols[1]!=undefined){
				jQuery(obj)[0].options.add(optn);}
		});
	},
	FillDropDown:function(data,obj){
		//jQuery(obj).removeOption(/./);
		
		jQuery(data).each(function(index){
			var cols = data[index].split('|');
			//jQuery(obj).addOption(cols[1],cols[0]);
			
			var optn = document.createElement("option");
			optn.text = cols[0];
			optn.value = cols[1];
			if (cols[1]!=undefined){
				jQuery(obj)[0].options.add(optn);}
		});
	},
	AutoCompleteTextBox:null,
	AutoCompleteTypeBox:null,
	SubmitButton:null,
	MessageBox:null,
	SelectedLocationID:0,
	SelectedLocationURL:'',
	SelectedTypeID:0,
	SelectedTypeURL:'',
	SelectedCountryLocationID:function(){
		return this.SelectedCountryLocationIDByName(siteRootLocation);
	},
	SelectedCountryLocationIDByName:function(name){
		var i = null;
		var countries = this.getCountries();
		jQuery(countries).each(function(index){
			var cols = countries[index].split('|');
			//jQuery(obj).addOption(cols[1],cols[0]);
			if(cols[0]==name)
			{
				i = cols[1];
				index = 1000;
			}
		});
		return i;
	},
	CacheRegions:true,
	UseFreshCountryData:true,
	isLondonLocation:false,
	SubmitURL:"property/search",
	SubmitMethod:"POST",
	UpdateSubmitButton:function(){
		var msg = "";
		
		if(msg!=""){
			msg = "Please select " + msg;
			jQuery(this.SubmitButton).hide();
			jQuery(this.MessageBox).text(msg);
		}else{
			jQuery(this.MessageBox).hide();
			jQuery(this.SubmitButton).show();
		}
	},	
	remoteCountryDataURL:"/services/getactivecountries.ashx",
	EnableMinControls:function(){
		//this.AutoCompleteTextBox.disabled = false;
		//this.AutoCompleteTypeBox.disabled = false;
		/*this.UpdateSubmitButton();*/
	},
    selectedCountry:function() { 
                                    // currentCountry should be initalised to "" on the page itself
                                    if (strCurrentPageCountry == "Please Select Country") 
                                    return "";
                                        else 
                                    return strCurrentPageCountry;
                                        
                               },
	getCountries:function(){
		if (this.UseFreshCountryData){
			var dummy = 'Please Select Country|0';
			jQuery.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) {
    try {
        //var row = row.split("|");
        //return row[0] + " (id: " + row[1] + ")";
        if(row!=''){
        return row[1].replace(/[/]/gi," &#187 ").replace(/[_]/gi," ");}
        else{ return '';}
    }catch(err){
        return '';
    }
}

function formatResult(row) {
	return row[0];
}

function isInteger(s) {
  return (s.toString().search(/^-?[0-9]+$/) == 0);
}
