//This function to fix the BC product search function
//Powered by Inhouse Web
//Code by Alex Sun
//Support email: alex@inhouseweb.cn

function fixsearch(tab,stat)
{
    	if(stat==0){
		elem="#search-"+tab;
		}else{
		elem="#new-search-"+tab;
		}		
		
	   var minprice = $(elem+' #CAT_ProductPriceMin').val();
	   var maxprice = $(elem+' #CAT_ProductPriceMax').val();
	  
	   var tmp1 = minprice;
	
	 /*  if ((maxprice == "-1") || (tmp1 < maxprice))
	   {
			for(var m=0; m<allprice.length; m++)
			{
				if ((tmp1>allprice[m][0]) && (tmp1<allprice[m][1]))
				{
					tmp1 = allprice[m][0];
				}
			}   
	   }*/
	   minprice = tmp1;
		// document.getElementById("CAT_ProductPrice").value = minprice + "," + maxprice;
	   minp = minprice + "," + maxprice;
	   $(elem+' #CAT_ProductPrice').attr('value',minp);
	  
       if(document.getElementById("CAT_ProductSearchExt").checked)
       {
            var tmp2 =  $(elem+' #CAT_ProductSearchRoom').val() + " prestige";
             $(elem+' #CAT_ProductSearch').val() = tmp2;
       }
       else
       {
			sr=$(elem+' #CAT_ProductSearchRoom').val();
			$(elem+' #CAT_ProductSearch').attr('value',sr);  
       }
		
     var tmp3 =  $(elem+' #CAT_ProductPriceMin').val();
     var tmp4 =  $(elem+' #CAT_ProductPriceMax').val();
     var tmp5 =  $(elem+' #CAT_ProductSearchRoom').val();
     var tmp6 = document.getElementById("CAT_ProductSearchExt").checked;
    
     if(tmp3=="-1" && tmp4=="-1" && tmp5=="")
     {
             $(elem+' #CAT_ProductSearch').attr('value','noroom');
			if(tmp6)
			{
				//document.getElementById("CAT_ProductCatalogue").value = 54959; //All Properties Cataogs, does not include x rooms
				// $('#new-search-'+tab+' #CAT_ProductSearch').val() = "noroom" + " prestige";
				 $(elem+' #CAT_ProductSearch').attr('value','noroom prestige');
			}
     }
	 
	  //var x = document.getElementById("homesearch");
	  var y = $(elem+' #homesearch').attr('action');
	  var mprice =$(elem+' #CAT_ProductPriceMin').val();
	  var mxprice = $(elem+' #CAT_ProductPriceMax').val();
	  //var no_bedrooms=$('#new-search-'+tab+' #CAT_ProductSearchRoom').val();
	  var sel_loc= $(elem+" #CAT_ProductCatalogue option:selected").text();
	 
	  if( $(elem+' #CAT_ProductSearchRoom').val() == " " ||  $(elem+' #CAT_ProductSearchRoom').val() == null ){ var no_bedrooms='NoMin';}else{ var no_bedrooms= $(elem+' #CAT_ProductSearchRoom').val();}
	  var top_url = y + "&minprice="+mprice+"&maxprice="+mxprice+"&bedrooms=" + no_bedrooms + "&location=" + sel_loc;
	  $(elem+' #homesearch').attr('action',top_url);
	  
//alert ("Backend Price Range:" + document.getElementById("CAT_ProductPrice").value);
//alert ("Search Word:" + document.getElementById("CAT_ProductSearch").value);

}


function hidethis(){
	myurl=new MyUrl();
	
	var searchprice_min = myurl.GetQuery("minprice");
	var searchprice_max = myurl.GetQuery("maxprice");
	
	if (searchprice_min == "-1")
	{
		searchprice_min = 1;
	}
	if (searchprice_max == "-1")
	{
		searchprice_max = 999999999;	
	}
//alert ("searchprice_mix:" + searchprice_min);
//alert ("searchprice_max:" + searchprice_max);
	
	var hiddenArray = new Array();
	var showArray = new Array();
	var tmp_id;
	
	var t_hiddenArray = new Array(); //
	var t_showArray = new Array(); //
	for(var m=0; m<allprice.length; m++)
	{
		if ((searchprice_min>allprice[m][1]) || (searchprice_max<allprice[m][0]))
		{
			tmp_id = "p"+allprice[m][0];
			t_hiddenArray.push("{"+allprice[m][0],allprice[m][1]+"}"); //
			hiddenArray.push(tmp_id);
			if (document.getElementById(tmp_id))
			{
				document.getElementById(tmp_id).style.display = 'none';
				document.getElementById(tmp_id).style.visibility = 'hidden';
			}
		}
		else
		{
			t_showArray.push("{"+allprice[m][0],allprice[m][1]+"}"); //
			showArray.push(tmp_id);
		}
	}
	
	//Test
	//box1.innerHTML = "<b>Debug</b><br /><br />Search MinPrice="+myurl.GetQuery("minprice") + "<br />" + "Search MaxPrice="+myurl.GetQuery("maxprice") + "<br /><br />"+ "<b>Hidden:</b><br />" + t_hiddenArray + "<br /><br />" + "<b>Showed:</b><br />" + t_showArray;
}

function MyUrl(_url)
{

    this.url=document.URL; 
    if(_url)
        this.url=_url;

    this.reg=new RegExp("(http|https)://([-a-z0-9_.]+)(/[-a-z0-9_.!/@&=\+,.~%\$\?]*)","gmi")
    this.reg.test(this.url);

    this.HostName=RegExp.$2; //Domain name
    this.Protocal=RegExp.$1; //url Protocal
    this.PathAndQuery=RegExp.$3; //Path and queryString
    this.Path=this.url.indexOf('?')>0?RegExp.$3.substring(0,RegExp.$3.indexOf('?')):RegExp.$3; //Path
    this.QueryString=this.url.indexOf('?')>0?RegExp.$3.substring(RegExp.$3.indexOf('?')+1):''; //queryString
}

//Get URL queryString
MyUrl.prototype.GetQuery = function (name)
{
        if(/\?(.+)$/.test(this.url))
        {
            var ta=RegExp.$1;
        
            var r = new RegExp(name+"=([^&]+)","gmi");
            if(r.test(ta))
            {
                return unescape(RegExp.$1);
            }else
            {
                return '';
            }

        }else
            return '';
}

//Test
//myurl=new MyUrl();
//var myurl=new MyUrl("https://www.google.com?id=12345&name=678"); 
 
//Get URL queryString
//alert("path: "+myurl.Path);
//alert("pathandQuery: "+myurl.PathAndQuery);
//alert("queryString: "+myurl.QueryString);
//alert("MinPrice="+myurl.GetQuery("minprice"));
//alert("MaxPrice="+myurl.GetQuery("maxprice"));

//document.getElementByClass("productItemNotFound").innertext = "Sorry, no properties matched your criteria, expand the search criteria and try again or search through our catalogs, by location and property development";
