﻿///=====================================
// SearchCriterias class
// Require: jquery-urlquery.js
///=====================================
function SearchCriterias(clientID) {    
    $Self = this;
    $ClientID = clientID;
    $ParamList = $.query;
    this.SearchAgent = new SearchAgentJson();
    //Reset the param string in the url    
}

///------------------------------
// Generate URL and reload page
///------------------------------
SearchCriterias.prototype.Search = function() {
    $.query.EMPTY();
    $ParamList = $.query.EMPTY();
    this.GetFormParams();
}

///------------------------------
// Return a copy of the URL param
///------------------------------
SearchCriterias.prototype.GetParamList = function() {
    return decodeURIComponent($ParamList.copy());
}

///-----------------------------------------------------------------
// Get all facilities and add the selected to the param list
///-----------------------------------------------------------------
SearchCriterias.prototype.GetFacilities = function() {
    var listres = "";
    var counter = 0;
    $("#" + $ClientID + "_chklst_Facilities span").each(function() {
        if (this.childNodes[0].checked) {
            listres = listres + this.getAttribute("itemID") + ",";
        }
    });
    listres = listres.substring(0, listres.lastIndexOf(','));    
    return listres;
}

///-----------------------------------------------------------------
// Get parameters from the input form and add param list
// Return: SearchAgentJson()
///-----------------------------------------------------------------
SearchCriterias.prototype.GetFormParams = function() {

    this.SearchAgent = new SearchAgentJson();
    this.SearchAgent.rentmin = $("#" + $ClientID + "_txt_RentMin").val();
    this.SearchAgent.rentmax = $("#" + $ClientID + "_txt_RentMax").val();

    this.SearchAgent.roomsmin = $("#" + $ClientID + "_txt_RoomsMin").val();
    this.SearchAgent.roomsmax = $("#" + $ClientID + "_txt_RoomsMax").val();

    this.SearchAgent.m2min = $("#" + $ClientID + "_txt_AreaMin").val();
    this.SearchAgent.m2max = $("#" + $ClientID + "_txt_AreaMax").val();

    this.SearchAgent.rentedhousing = document.getElementById($ClientID + "_chk_ResidenceType_RentedHousing").checked + '';
    this.SearchAgent.youthhousing = document.getElementById($ClientID + "_chk_ResidenceType_YouthHousing").checked + '';

    this.SearchAgent.notlimited = document.getElementById($ClientID + "_chk_NotLimited").checked + '';
    this.SearchAgent.timelimited = document.getElementById($ClientID + "_chk_TimeLimited").checked + '';

    this.SearchAgent.where = $("#" + $ClientID + "_txt_WhereRoad").val();
    this.SearchAgent.where = this.SearchAgent.where + "|" + $("#" + $ClientID + "_txt_PostalCode").val();

    this.SearchAgent.facilities = this.GetFacilities();

    this.SearchAgent.openday = document.getElementById($ClientID + "_chk_ShowOnlyOpenDay").checked + '';

    this.SearchAgent.maptype = $("#" + $ClientID + "_MapType").val();

    this.SearchAgent.extend = $("#" + $ClientID + "_hid_IsSearchOptionsExtended").val();

    this.SearchAgent.xmax = $("#" + $ClientID + "_Xmax").val();
    this.SearchAgent.xmin = $("#" + $ClientID + "_Xmin").val();
    this.SearchAgent.ymax = $("#" + $ClientID + "_Ymax").val();
    this.SearchAgent.ymin = $("#" + $ClientID + "_Ymin").val();

    this.SearchAgent.clat = $("#" + $ClientID + "_Clat").val();
    this.SearchAgent.clng = $("#" + $ClientID + "_Clng").val();
    this.SearchAgent.z = $("#" + $ClientID + "_zoom").val(); 
    this.SearchAgent.postalcode = $("#" + $ClientID + "_PostalCodeID").val();
    this.SearchAgent.id = $("#" + $ClientID + "_hidden_SearchAgentID").val();
    
    $Self.AddParam("rentmin", $("#" + $ClientID + "_txt_RentMin").val());
    $Self.AddParam("rentmax", $("#" + $ClientID + "_txt_RentMax").val());

    $Self.AddParam("roomsmin", $("#" + $ClientID + "_txt_RoomsMin").val());
    $Self.AddParam("roomsmax", $("#" + $ClientID + "_txt_RoomsMax").val());

    $Self.AddParam("m2min", $("#" + $ClientID + "_txt_AreaMin").val());
    $Self.AddParam("m2max", $("#" + $ClientID + "_txt_AreaMax").val());

    $Self.AddParam("rentedhousing", document.getElementById($ClientID + "_chk_ResidenceType_RentedHousing").checked + '');
    $Self.AddParam("youthhousing", document.getElementById($ClientID + "_chk_ResidenceType_YouthHousing").checked + '');

    $Self.AddParam("notlimited", document.getElementById($ClientID + "_chk_NotLimited").checked + '');
    $Self.AddParam("timelimited", document.getElementById($ClientID + "_chk_TimeLimited").checked + '');

    $Self.AddParam("where", encodeURIComponent($("#" + $ClientID + "_txt_WhereRoad").val() + "|" + $("#" + $ClientID + "_txt_PostalCode").val()));
    $Self.AddParam("fa", this.GetFacilities());

    $Self.AddParam("openday", document.getElementById($ClientID + "_chk_ShowOnlyOpenDay").checked + '');

    $Self.AddParam("maptype", $("#" + $ClientID + "_MapType").val());

    $Self.AddParam("extend", $("#" + $ClientID + "_hid_IsSearchOptionsExtended").val());

    $Self.AddParam("xmax", $("#" + $ClientID + "_Xmax").val());
    $Self.AddParam("xmin", $("#" + $ClientID + "_Xmin").val());
    $Self.AddParam("ymax", $("#" + $ClientID + "_Ymax").val());
    $Self.AddParam("ymin", $("#" + $ClientID + "_Ymin").val());

    $Self.AddParam("clat", $("#" + $ClientID + "_Clat").val());
    $Self.AddParam("clng", $("#" + $ClientID + "_Clng").val());
    $Self.AddParam("z", $("#" + $ClientID + "_zoom").val());
    $Self.AddParam("postalcode", $("#" + $ClientID + "_PostalCodeID").val());
    $Self.AddParam("id", $("#" + $ClientID + "_hidden_SearchAgentID").val());
}

///-----------------------------------------------------------------
// Get XY coodinates from the input form and add to querystring
///-----------------------------------------------------------------
SearchCriterias.prototype.UpdateXYAndResidrect = function(redirectPath) {
    $Self.Remove("sa");
    $Self.AddParam("where", encodeURIComponent($("#" + $ClientID + "_txt_WhereRoad").val() + "|" + $("#" + $ClientID + "_txt_PostalCode").val()));
    $Self.AddParam("xmax", $("#" + $ClientID + "_Xmax").val());
    $Self.AddParam("xmin", $("#" + $ClientID + "_Xmin").val());
    $Self.AddParam("ymax", $("#" + $ClientID + "_Ymax").val());
    $Self.AddParam("ymin", $("#" + $ClientID + "_Ymin").val());

    $Self.AddParam("extend", $("#" + $ClientID + "_hid_IsSearchOptionsExtended").val());
    $Self.AddParam("maptype", $("#" + $ClientID + "_MapType").val());

    $Self.AddParam("clat", $("#" + $ClientID + "_Clat").val());
    $Self.AddParam("clng", $("#" + $ClientID + "_Clng").val());
    $Self.AddParam("z", $("#" + $ClientID + "_zoom").val());
    $Self.AddParam("postalcode", $("#" + $ClientID + "_PostalCodeID").val());    
    this.Redirect(redirectPath);
}

///-----------------------------------------------------------------
// Redirect
///-----------------------------------------------------------------
SearchCriterias.prototype.Redirect = function(redirectPath) {
    //alert($ParamList.copy());
    //alert(decodeURIComponent($ParamList.copy()));    
    document.location.href = redirectPath + decodeURIComponent($ParamList.copy());
}          

//-------------------------------------
// Add a parameter to the URL
//-------------------------------------
SearchCriterias.prototype.AddParam = function(key, value) {
    $ParamList = $ParamList.set(key, value);
}

SearchCriterias.prototype.Remove = function(key) {
    $ParamList = $ParamList.remove(key);

}




//=====================================
//      SearchAgent class
//=====================================

function SearchAgentJson() {
    this.id;
    this.name;    
    
    this.rentmin;
    this.rentmax;

    this.roomsmin;
    this.roomsmax;

    this.m2min;
    this.m2max;

    this.rentedhousing;
    this.youthhousing;

    this.notlimited;
    this.timelimited;

    this.where;
    this.facilities;

    this.openday;

    this.maptype;

    this.extend;

    this.xmax;
    this.xmin;
    this.ymax;
    this.ymin;

    this.clat;
    this.clng;
    this.z = 6;

    this.postalcode;    
}