//This function to fix the BC product search function
//Powered by Inhouse Web
//Code by Alex Sun
//Support email: alex@inhouseweb.cn

function fixsearch()
{
	   var minprice = document.getElementById("CAT_ProductPriceMin").value;
	   var maxprice = document.getElementById("CAT_ProductPriceMax").value;
	   
	   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;
	   
       if(document.getElementById("CAT_ProductSearchExt").checked)
       {
            var tmp2 = document.getElementById("CAT_ProductSearchRoom").value + " prestige";
            document.getElementById("CAT_ProductSearch").value = tmp2;
       }
       else
       {
            document.getElementById("CAT_ProductSearch").value = document.getElementById("CAT_ProductSearchRoom").value;
       }

     var tmp3 = document.getElementById("CAT_ProductPriceMin").value;
     var tmp4 = document.getElementById("CAT_ProductPriceMax").value;
     var tmp5 = document.getElementById("CAT_ProductSearchRoom").value;
     var tmp6 = document.getElementById("CAT_ProductSearchExt").checked;
    
     if(tmp3=="-1" && tmp4=="-1" && tmp5==" ")
     {
            document.getElementById("CAT_ProductSearch").value = "noroom";
			if(tmp6)
			{
				//document.getElementById("CAT_ProductCatalogue").value = 54959; //All Properties Cataogs, does not include x rooms
				document.getElementById("CAT_ProductSearch").value = "noroom" + " prestige";
			}
     }
	 
	  var x = document.getElementById("homesearch");
	  x.action = x.action + "&minprice=" + document.getElementById("CAT_ProductPriceMin").value + "&maxprice=" + document.getElementById("CAT_ProductPriceMax").value;

//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";