﻿
var SearchCriteria = new Array();
var Channel = "sales"; // default
var url = "";
var SmartQueryString = "";

function Search()
{
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //	Get channel
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if ($("#channel_rentals").is(':checked'))
    {
        Channel = $("#channel_rentals").val();
    }
    else if ($("#channel_sales").is(':checked'))
    {
        Channel = $("#channel_sales").val();
    }
    else
    {
        Channel = $("#channel_sales").val();
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //	Validate Price Range
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if (parseFloat($("#price_min").val()) >= parseFloat($("#price_max").val()))
    {
        window.alert("The minimum price must be less than the maximum price");
        return;
    }

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //	Gather all search criteria
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	if ($("#open_house").is(':checked'))
    {
        SearchCriteria.push($("#open_house").val());
    }
    if ($("#data_source_type").val() != "")
    {
        SearchCriteria.push($("#data_source_type").val());
    }
    if ($("#search_type").val() != "")
    {
        SearchCriteria.push($("#search_type").val());
    }
    if ($("#price_min").attr("selectedIndex") > 0 || $("#price_max").attr("selectedIndex") > 0)
    {
        SearchCriteria.push($("#price_min").val() + "-" + $("#price_max").val() + "-price");
    }
    if ($("#beds").val() != "")
    {
        SearchCriteria.push($("#beds").val());
    }
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //	if no suggestd value, and a Term is present,
    //	go get the response from JSon
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if ($("#LocationSuggested").val() == "")
    {
        if ($("#Term").val() == "")
        {
            window.alert("Please enter a search term");
            return;
        }
        else
        {
            GetJSonResponse();
        }
    }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //	if we're here, the user selected something from suggest
    //	Smart Search is NOT called, direct link is generated and go
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    else
    {
		SearchCriteria.reverse();
		SearchCriteria.push($("#LocationSuggested").val());
        SearchCriteria.push(Channel);
        SearchCriteria.reverse();
		url = CacheObject.CanonicalWebRoot + "/" + SearchCriteria.join("/") + "/";
		CompleteSearch(url);
    }
}

var LauncherForm = null;

function CompleteSearch(url, QueryString)
{
	if (LauncherForm != null)
	{
		try
		{
			document.body.removeChild(LauncherForm);
		}
		catch(Error) {}
	}
	LauncherForm = document.createElement("form");
	LauncherForm.setAttribute("action", url);
	LauncherForm.setAttribute("method", "get");
	LauncherForm.setAttribute("target", "_top");

	if (QueryString != undefined)
	{
		var QS = QueryString.split("&");
		for(var x=0;x<QS.length;x++)
		{
			var part = QS[x].split("=");
			var element = document.createElement("input");
			element.setAttribute("type", "hidden");
			element.setAttribute("name", part[0]);
			element.setAttribute("value", part[1]);
			LauncherForm.appendChild(element);
		}
	}
	
	document.body.appendChild(LauncherForm);
	LauncherForm.submit();
}

function GetJSonResponse()
{
    var Criteria = "";
    var SmartUrl = "";
    
    if (SearchCriteria.length > 0)
    {
		Criteria = "/" + SearchCriteria.join("/");
    }
    
    SmartQueryString = String.Format("ss=true&channel={0}&criteria={1}&keyword={2}",
							Channel,
							Criteria,
							$("#Term").val());

	var JSonTargetUrl = "/Reno/Search/SmartSearch.aspx?" + SmartQueryString;
						
    //window.location.href = JSonTargetUrl;

    $.getJSON(JSonTargetUrl,
    function(JsonResponse, textStatus)
    {
        if (JsonResponse != null &&
	    JsonResponse.matchgroup != null &&
	    JsonResponse.matchgroup.length > 0 &&
	    JsonResponse.matchgroup[0].match != null &&
	    JsonResponse.matchgroup[0].match.length > 0)
        {
            window.setTimeout(function() // timeout required to set history, since this is an asynch call
            {
                url = JsonResponse.matchgroup[0].match[0].seopath;
                
                if (JsonResponse.matchgroup.length > 1 || JsonResponse.matchgroup[0].match.length > 1)
                {
					CompleteSearch(url, SmartQueryString);
                } 
                else
                {
					CompleteSearch(url, "");
                }
            }, 10);
        }
        else 
        {
            window.alert("No results found for '" + $("#Term").val() + "'");
            return false;
        }
    });
}

