var map, manager, batch;
var radOwn, radPrice, chkHouseTypes, scPriceMin, scPriceMax, scInterestRate, scDownPayment, scYearofloan, SearchOptions, scBedroomsMax, scBedroomsMin, scBathroomsMax, scBathroomsMin, scSQFTMax, scSQFTMin;
var scMonthlyPriceMax, scMonthlyPriceMin, scGarageMax, scGarageMin, scLevelMax, scLevelMin;
var scids, scid_models;
var global_floorCount = 0;
var communityCount = 0;
var tempFloorCount = 0;
var AutoNavigationBarSelect = 0;
var ManualNavigationBarSelect = 0;
var resterOnOwnRentClick = "";

var lastNavigator, lastRadOwn, lastRadRent, lastHouseType, lastBedroomMin, lastBedroomMax, lastBathroomMin, lastBathroomMax, lastGarageMin, lastGarageMax, lastStoriesMin, lastStoriesMax;
var lastSQFTMin, lastSQFTMax, lastHomeSearch, lastCommunitySearch, lastPriceMin, lastPriceMax, lastPriceMonthlyMin, lastPriceMonthlyMax, lastInterestRate, lastDownPayment, lastYearOfLoan;
var bound;
var oNavigator;
//Home Finder Location Navegator
function initialize() {
    //make the "progress" div appear full screen even when the map cannot load.
    document.getElementById("divProgress").style.height = document.body.clientHeight + "px";
    document.getElementById("divProgress").style.display = "block";
    
    iniNavigator();
    iniSearchCriteria();
    iniGMap();
    iniHomesearch();
    locationFilter();
    iniSavedSearchCriteria();
    saveLastSearchValues();
    document.getElementById("divProgress").style.display = "none";
}

function iniNavigator() {
    var divLocation = document.getElementById("divLocation");
    var odivSelect = document.getElementById("divSelect");
    //Get Selected Navigator Value
    oNavigator = document.getElementById("Navigator");
    var sNavigator = oNavigator.value.split("_");
////    //Build Navigator String
    var strNavigator = "<span id='spanL0'><a href=\"javascript:resetNavigator()\" class=\"hfLocationLink\">" + _LevelDetail[0][3] + "</a>";
    for (i = 1; i < sNavigator.length; i++) {
        for (j = 0; j < _LevelDetail.length; j++) {
            if (i == _LevelDetail[j][0] && sNavigator[i] == _LevelDetail[j][1]) {
                strNavigator += "></span><span id='spanL" + i + "'><a href=\"javascript:showNavigatorSelector(" + i + "," + sNavigator[i - 1] + ")\" class=\"hfLocationLink\">" + _LevelDetail[j][3] + "</a>";
//                if (i == sNavigator.length - 1) {
//                    document.getElementById("ctl00_ContentPlaceHolder1_btnViewAll").innerHTML = "View all homes in " + _LevelDetail[j][3];
//                }
            }
        }
    }
    if (sNavigator.length < _Level.length) {
        strNavigator += "></span><span id='spanL" + sNavigator.length + "'><a href=\"javascript:showNavigatorSelector(" + sNavigator.length + "," + sNavigator[sNavigator.length - 1] + ")\" class=\"hfLocationLink\">Select " + _Level[sNavigator.length] + "</a></span>";
    }
    divLocation.innerHTML = "<span id='spanNavigator'>" + strNavigator + "</span>";
    odivSelect.style.visibility = "hidden";
    //odivSelect.style.display = "none";
    //debugger;
    if (map) 
    {
        if (AutoNavigationBarSelect != 1) 
        {
            ManualNavigationBarSelect = 1;
            locationFilter();
            ManualNavigationBarSelect = 0;
        }
    }
}
function resetNavigator() {
    //debugger;
    document.getElementById('Navigator').value = 0;
    iniNavigator();
    document.getElementById("ctl00_ContentPlaceHolder1_btnViewAll").innerHTML = "View all homes";
    //    document.getElementById("ctl00_ContentPlaceHolder1_btnViewAll").value = "View all homes" + _LevelDetail[0][3];
}
function iniSearchCriteria() {
    //Load from cookie

}

//Show Hide Navigator Selector Panel
function showNavigatorSelector(level, pItem) {
    //debugger;
    var divLocation = document.getElementById("divLocation");
    var odivSelect = document.getElementById("divSelect");
    odivSelect.innerHTML = "";
    for (var i = 0; i < _LevelDetail.length; i++) {
        if (level == _LevelDetail[i][0] && pItem == _LevelDetail[i][2]) {
            odivSelect.innerHTML += "<a href=\"javascript:NavigatorSelected(" + level + "," + _LevelDetail[i][1] + ")\" class=\"hfLocationLink\">" + _LevelDetail[i][3] + "</a><br />";
        }
    }
    //Find Select div position
    var oNavigator = document.getElementById("Navigator");
    var sNavigator = oNavigator.value.split("_");
    var position = 0;
    for (var i = 0; i < level; i++) {
        position += document.getElementById("spanL" + i).offsetWidth;
    }
    odivSelect.style.left = position + 12; //Position + padding of the navigator
    odivSelect.style.visibility = (odivSelect.style.visibility == "visible") ? "hidden" : "visible";
    //odivSelect.style.display = (odivSelect.style.display == "block") ? "none" : "block";
}
//Select from Navigator dropdown
function NavigatorSelected(level, item) {
    //debugger;
    var oNavigator = document.getElementById("Navigator");
    var tmp = oNavigator.value.split("_");
    tmp[level] = item;
    var tmp2 = "";
    for (var i = 0; i <= level; i++) {
        if ("" != tmp2) tmp2 += "_";
        tmp2 += tmp[i];
    }
    oNavigator.value = tmp2;
    iniNavigator();
}

//Show Hide More Options Panel
function showMoreOptions() {
    var odivB = document.getElementById("divOptionB");
    var odiv1 = document.getElementById("divCoreSearch");
    var odiv2 = document.getElementById("divOption");
    var spaceHolder = document.getElementById("divSearchSaveSpaceHolder"); 
    var oimgSearchoption = document.getElementById("imgSearchoption");
    var oimgMoreoption = document.getElementById("imgMoreoption");
    //odivB.style.visibility = (odivB.style.visibility == "visible") ? "hidden" : "visible";
    
    //if (odiv1.style.top == "22px") {
    //if (odiv2.style.visibility == "visible")
    if (odiv2.style.display == "block")
    {
        odiv1.style.top = "0px";
        odiv1.style.display = "block";
        //odiv1.style.visibility = "visible";
        
        oimgSearchoption.src = "images/max.png";
        oimgMoreoption.src = "images/min.png";
        odiv2.style.display = "none";
        spaceHolder.style.display = "block";
        //odiv2.style.visibility = "hidden";
    }
    else {
        //odiv1.style.top = "22px";
        //odiv2.style.top = "60px";
        odiv2.style.top = "10px";
        odiv1.style.display = "none";
        //odiv1.style.visibility = "hidden";

        oimgSearchoption.src = "images/min.png";
        oimgMoreoption.src = "images/max.png";
        spaceHolder.style.display ="none"
        odiv2.style.display = "block";
        //odiv2.style.visibility = "visible";
    }
}

//new fucntion
function showHideSearchResult() {
    var odivB = document.getElementById("dvSearchResult");
    var oimgSearchResult = document.getElementById("imgSearchResult");

    if (odivB.style.display == "block" || odivB.style.display == "") 
    {
        odivB.style.display = "none";
        oimgSearchResult.src = "images/min.png";
    }
    else {
        odivB.style.display = "block";
        oimgSearchResult.src = "images/max.png";
    }
}

function showSalesCenter(id, show) {
    var dv = document.getElementById("divSalesCenter_" + id);
    if (null != dv) {
        
        if (!show) {
            dv.parentNode.parentNode.style.display = "none";
           
            return true;
        }
        else {
            if (!searchOptionFilter(dv)) {
                if (dv.parentNode) dv.parentNode.parentNode.style.display = "none";
                global_floorCount = global_floorCount - tempFloorCount;
                return false;
            }
            else {
                //alert(counter);
                if (dv.parentNode) {

                    dv.parentNode.parentNode.style.display = "";
                    return true;
                }
                else {
                    return false;
                }
            }
        }
    }
}

//Hide More Options Panel
function showModelsList(id) {
    //debugger;
    var dv = document.getElementById("divModels_" + id);
    var img = document.getElementById("imgSection_" + id);
    
    if (dv.style.visibility == "visible") {
        dv.style.visibility = "hidden";
        dv.style.height = "0px";
        img.src = "images/min.png";
    }
    else {
        dv.style.visibility = "visible";
        dv.style.height = "";
        img.src = "images/max.png";
    }
}

function ExpandCollapse(id) {
    var dv = document.getElementById("divDetails_" + id);
    var img = document.getElementById("imgSection_" + id);

    if (dv.className == "CompareDivDetailsVisible") 
    {
        dv.className = "CompareDivDetailsNotVisible";
        img.src = "images/min.png";
    }
    else 
    {
        dv.className = "CompareDivDetailsVisible";
        img.src = "images/max.png";
    }
}

function largerMap() {
    var gMap = document.getElementById("gMap");
    //var mapBack = document.getElementById("divMapBack");
    var topLeftPanel = document.getElementById("divTopLeftPanel");

    var rightPanel = document.getElementById("divTopRightPaenl");
    var bottomPanel = document.getElementById("divBottonPanel");
    rightPanel.style.display = "none";
    //bottomPanel.style.display = "none";
    topLeftPanel.style.width = "99%";
//    topLeftPanel.style.borderStyle = "solid";
//    topLeftPanel.style.borderColor = "#00a9df";
//    mapBack.style.width = "100%";
//    mapBack.style.height = "100%";
    
    

//    var MapBack = document.getElementById("divMapBack");
//    var gMap = document.getElementById("gMap");
//    MapBack.className = "hfdivMapBackFull";
//    gMap.className = "hfdivMapFull";
    document.getElementById("imgLarger").style.display = "none";
    document.getElementById("imgSmaller").style.display = "";
    map.checkResize();
}

function smallerMap() {
    //var MapBack = document.getElementById("divMapBack");
    var gMap = document.getElementById("gMap");
    var rightPanel = document.getElementById("divTopRightPaenl");
    var topLeftPanel = document.getElementById("divTopLeftPanel");
    topLeftPanel.style.width = "76%";
    rightPanel.style.display = "block";
    //MapBack.style.width = "100%";
//    MapBack.className = "hfdivMapBack";
//    gMap.className = "hfdivMap";
    document.getElementById("imgLarger").style.display = "";
    document.getElementById("imgSmaller").style.display = "none";
    map.checkResize();
}

//Open URL from Javascript
function openURL(URL) {
    self.location = URL;
}
//Open List All page
function ShowModels() {
    CompareAndAnonyousSave();
    if(scids !='')
        openURL('ShowModels.aspx?scids=' + scids);
}

//Open Availability page
function Availability(scid) {
    CompareAndAnonyousSave();
    var chks = document.getElementById("divSalesCenter_" + scid).getElementsByTagName('input');
    var strscid_models = "";
    var strID;
    for (var i = 0; i < chks.length; i++) {
        if (chks[i].parentNode.parentNode.style.display == "") {
            if ("" != strscid_models) strscid_models += ",";
            strID = chks[i].id.split('_');
            strscid_models += (strID[1] + '_' + strID[2]);
        }
    }
    openURL('Availability.aspx?ModelIDs=' + strscid_models);
}

//Open Compare page
function Compare() {
    //debugger;
    CompareAndAnonyousSave();
    var models = getModels(document.forms[0], 'chkCompare');
    if ("" != models) {
        openURL('Compare.aspx?scid=' + document.getElementById("SCID").value + '&Sales_CenterIDs=' + getModels(document.forms[0], 'chkCompare'));
    }
}

//Open Save page
function SaveTo247(dataType, dataValue, scid) {
    try {
        set_cookie("saveSearch", dataValue, 999999);
        var URL = document.getElementById("247URL").value;
        var oifrm = document.getElementById("ifrmSave");
        var sURL = document.location.href.split('/');
        sURL.length = 3;
        var domain = sURL.join('/');
        //oifrm.src = URL + '/Marketing/SaveTo247.aspx?domain=' + domain + '&dataType=' + dataType + '&dataValue=' + dataValue + '&source=hideframe' + '&SCID=' + scid;
        window.parent.document.getElementById("divGrayBG").className = "hfVisible";
        window.parent.document.getElementById("divGrayBG").style.height = document.body.clientHeight + "px";
        //new code
        if (document.getElementById("hdnAnonymousSearch") && document.getElementById("hdnAnonymousSearch").value == "1") 
        {
            //debugger;
            document.getElementById("hdnAnonymousSearch").value = "";
            window.parent.document.getElementById("divGrayBG").className = "hfNone";
            //var location = URL + '/Marketing/AnonymousSearch.aspx?domain=' + domain + '&dataType=' + dataType + '&dataValue=' + dataValue + '&source=hideframe' + '&SCID=' + scid;
            var location = URL + '/Marketing/AnonymousSearch.aspx?dataType=' + dataType + dataValue + '&source=hideframe' + '&SCID=' + scid;
            try {
                xss_ajax(location);
            }
            catch (ex) { }
        }
        else 
        {
            window.window.frames['frmLogin'].location.href = URL + '/Marketing/SaveTo247.aspx?domain=' + domain + '&dataType=' + dataType + '&dataValue=' + dataValue + '&source=hideframe' + '&SCID=' + scid;
            window.parent.showElement();
        }
        //oifrm.src = 'http://localhost:1000/247/Marketing/SaveTo247.aspx?dataType=' + dataType + '&dataValue=' + dataValue;
        //oifrm.style.display = "";
        //setTimeout("checkSaveTo247Statue()", 2000);
    }
    catch (err) {
        alert("This functionality is not available from the backend");
    }
}

function addToFavorite(me, model_scID, scID) 
{
    CompareAndAnonyousSave();
    if (me.src.toLowerCase().indexOf('addtofavorite.png') > 0) 
    {
        //me.src = "../images/hearts.gif";
        
        SaveTo247('AddFavorites', '&modelid=' + model_scID, scID);
    }
    else 
    {
        //me.src = "../images/addtofavorite.png";
        SaveTo247('RemoveFavorites', '&modelid=' + model_scID, scID);
    }
}

function getFavoriteList(scID) {
    //debugger;
    CompareAndAnonyousSave();
    SaveTo247('ListFavHomes',scID);
}

function setHref(url) {
    document.location.href = url; 
}
//function checkSaveTo247Statue() {
//    var oifrm = document.getElementById("ifrmSave");
//    alert(oifrm.document.body.scrollWidth);
//    if (oifrm.style.display == "") {
//        if (window.frames["ifrmSave"].document.getElementsByTagName("body")[0].innerHTML.indexOf('Save succeed') >-1)
//            oifrm.style.display = "none";
//        else
//            setTimeout("checkSaveTo247Statue()", 10000);
//    }
//}

function SaveSearchCriteria() {
    findSearchControls();
    var dataValue = "&Location=";
    dataValue += document.getElementById("Navigator").value;
    dataValue += "&OwnRent=";
    if (radOwn.checked)
        dataValue += "Own";
    else
        dataValue += "Rent";
    dataValue += "&HouseType=";
    var strTemp = "";
    for (var i = 0; i < chkHouseTypes.length; i++) {
        if (chkHouseTypes[i].checked) {
            if (strTemp != "") strTemp += ",";
            strTemp += chkHouseTypes[i].id.split('_')[3];
        }
    }
    dataValue += strTemp;

    dataValue += "&BedroomMinimum=";
    dataValue += (scBedroomsMin.value.toLowerCase() == "min") ? "" : scBedroomsMin.value;
    dataValue += "&BedroomMaximum=";
    dataValue += (scBedroomsMax.value.toLowerCase() == "max") ? "" : scBedroomsMax.value;
    dataValue += "&BathroomMinimum=";
    dataValue += (scBathroomsMin.value.toLowerCase() == "min") ? "" : scBathroomsMin.value;
    dataValue += "&BathroomMaximum=";
    dataValue += (scBathroomsMax.value.toLowerCase() == "max") ? "" : scBathroomsMax.value;
    dataValue += "&SQFTMinimum=";
    dataValue += (scSQFTMin.value.toLowerCase() == "min") ? "" : scSQFTMin.value;
    dataValue += "&SQFTMaximum=";
    dataValue += (scSQFTMax.value.toLowerCase() == "max") ? "" : scSQFTMax.value; 
    dataValue += "&GarageMinimum=";
    dataValue += (scGarageMin.value.toLowerCase() == "min") ? "" : scGarageMin.value;
    dataValue += "&GarageMaximum=";
    dataValue += (scGarageMax.value.toLowerCase() == "max") ? "" : scGarageMax.value;
    dataValue += "&StoriesMinimum=";
    dataValue += (scLevelMin.value.toLowerCase() == "min") ? "" : scLevelMin.value;
    dataValue += "&StoriesMaximum=";
    dataValue += (scLevelMax.value.toLowerCase() == "max") ? "" : scLevelMax.value;
//    dataValue += "&Bedroom=";
//    dataValue += scBedrooms.value;
//    dataValue += "&Bathroom=";
//    dataValue += scBathrooms.value;
//    dataValue += "&SQFT=";
//    dataValue += scSQFT.value;
    //debugger;
    if (document.getElementById("dvPrice").style.visibility != "hidden" && (scPriceMin.value.toLowerCase() != "min" || scPriceMax.value.toLowerCase() != "max")) 
    {
        dataValue += "&PriceMinimum=";
        dataValue += (scPriceMin.value.toLowerCase() == "min") ? "" : scPriceMin.value;
        dataValue += "&PriceMaximum=";
        dataValue += (scPriceMax.value.toLowerCase() == "max") ? "" : scPriceMax.value;
    }
    else 
    {
        dataValue += "&MonthlyMinimum=";
        dataValue += (scMonthlyPriceMin.value.toLowerCase() == "min") ? "" : scMonthlyPriceMin.value;
        dataValue += "&MonthlyMaximum=";
        dataValue += (scMonthlyPriceMax.value.toLowerCase() == "max") ? "" : scMonthlyPriceMax.value;

        //    dataValue += "&MonthlyMinimal=";
        //    dataValue += scMonthlyMin.value;
        //    dataValue += "&MonthlyMaximal=";
        //    dataValue += scMonthlyMax.value;
        dataValue += "&InterestRate=";
        if (scInterestRate.value.toLowerCase() == "rate") {
            dataValue += "";
        }
        else {
            var InterestRate = scInterestRate.value > 0 ? scInterestRate.value : 0;
            InterestRate = InterestRate < 100 ? InterestRate : 100;
            dataValue += InterestRate;
        }

        dataValue += "&DownPayment=";
        if (scDownPayment.value.toLowerCase() == "deposit") {
            dataValue += "";
        }
        else {
            var DownPayment = scDownPayment.value > 0 ? scDownPayment.value : 0;
            DownPayment = scDownPayment.value < 999999999 ? scDownPayment.value : 999999999;
            dataValue += DownPayment;
        }
        dataValue += "&Yearofloan=";
        if (scYearofloan.value.toLowerCase() == "term") {
            dataValue += "";
        }
        else {
            var Yearofloan = scYearofloan.value > 0 ? scYearofloan.value : 0;
            Yearofloan = scYearofloan.value < 100 ? scYearofloan.value : 100;
            dataValue += Yearofloan;
        }
    }
    
    
//    if (radPrice.checked) {
//        dataValue += "&PriceMinimal=";
//        dataValue += scPriceMin.value;
//        dataValue += "&PriceMaximal=";
//        dataValue += scPriceMax.value;
//    }
//    else {
//        dataValue += "&MonthlyMinimal=";
//        dataValue += scMonthlyMin.value;
//        dataValue += "&MonthlyMaximal=";
//        dataValue += scMonthlyMax.value;
//        dataValue += "&InterestRate=";
//        var InterestRate = scInterestRate.value > 0 ? scInterestRate.value : 0;
//        InterestRate = InterestRate < 100 ? InterestRate : 100;
//        dataValue += InterestRate;
//        dataValue += "&DownPayment=";
//        var DownPayment = scDownPayment.value > 0 ? scDownPayment.value : 0;
//        DownPayment = scDownPayment.value < 999999999 ? scDownPayment.value : 999999999;
//        dataValue += DownPayment;
//        dataValue += "&Yearofloan=";
//        var Yearofloan = scYearofloan.value > 0 ? scYearofloan.value : 0;
//        Yearofloan = scYearofloan.value < 100 ? scYearofloan.value : 100;
//        dataValue += Yearofloan;
//    }
    dataValue += "&SearchOptions=";
    strTemp = "";

    //set filter from both tabs
    var tempSearchOption = document.getElementById("ctl00_ContentPlaceHolder1_panHomeSearch").getElementsByTagName('input');
    for (var i = 0; i < tempSearchOption.length; i++) {
        if (tempSearchOption[i].checked) {
            if (strTemp != "") strTemp += ",";
            strTemp += tempSearchOption[i].id.split('_')[3];
        }
    }

    var tempSearchOption = document.getElementById("ctl00_ContentPlaceHolder1_panSearchOption").getElementsByTagName('input');
    for (var i = 0; i < tempSearchOption.length; i++) {
        if (tempSearchOption[i].checked) {
            if (strTemp != "") strTemp += ",";
            strTemp += tempSearchOption[i].id.split('_')[3];
        }
    }
//    for (var i = 0; i < SearchOptions.length; i++) {
//        if (SearchOptions[i].checked) {
//            if (strTemp != "") strTemp += ",";
//            strTemp += SearchOptions[i].id.split('_')[3];
//        }
//    }
    dataValue += strTemp;

    var scid = document.getElementById('SCID').value;
    SaveTo247("SearchCriteria", dataValue, scid);
}

function openLogin(modelid, RURL) {
    openURL('Login.aspx?modelid=' + modelid + '&RURL=' + RURL);
}

//get selected models
function getModels(formobj, ControlNameStartWith) {
    var strModels = "";
    for (var i = 0; i < formobj.elements.length; i++) {
        var e = formobj.elements[i];
        if ((e.type == 'checkbox' && e.id.indexOf(ControlNameStartWith) > -1 && e.checked)) {
            if ("" != strModels) strModels += ",";
            strModels += e.id.substring(ControlNameStartWith.length + 1);
        }
    }
    return strModels;
}

function showCommunity(id) 
{
    window.location = "CommunityDetails.aspx?scid=" + document.getElementById("SCID").value + "&sid=" + id;
}

function showDirection(to) {
    window.open('http://maps.google.ca/?q=from:+' + document.getElementById('txtFrom').value + '+to:+' + to, 'Direction');
}

//Functions of Search option-------------------------------------------------------------------------
function OwnRent(OR) {
    chkHouseTypes = document.getElementById("ctl00_ContentPlaceHolder1_chkHouseType").getElementsByTagName('input');
    resterOnOwnRentClick = 1;
    clearSearchResault();
    resterOnOwnRentClick = "";
    //debugger;
    if ("Own" == OR) {
        document.getElementById("dvPrice").style.visibility = "visible";
        //document.getElementById("ctl00_ContentPlaceHolder1_radPrice").disabled = "";
        //document.getElementById("ctl00_ContentPlaceHolder1_radMonthly").disabled = "";
        //document.getElementById("ctl00_ContentPlaceHolder1_radPrice").checked = true;
        //document.getElementById("ctl00_ContentPlaceHolder1_divPrice").style.display = "";
        //document.getElementById("ctl00_ContentPlaceHolder1_divMonthly").style.display = "none";
    }
    else {
        document.getElementById("ctl00_ContentPlaceHolder1_txtPriceMin").value = "";
        document.getElementById("ctl00_ContentPlaceHolder1_txtPriceMax").value = "";
        document.getElementById("dvPrice").style.visibility = "hidden";
        //document.getElementById("ctl00_ContentPlaceHolder1_radMonthly").checked = true;
        //document.getElementById("ctl00_ContentPlaceHolder1_radPrice").disabled = "disabled";
        //document.getElementById("ctl00_ContentPlaceHolder1_radMonthly").disabled = "disabled";
        //document.getElementById("ctl00_ContentPlaceHolder1_divPrice").style.display = "none";
        //document.getElementById("ctl00_ContentPlaceHolder1_divMonthly").style.display = "block";
        //document.getElementById("ctl00_ContentPlaceHolder1_divInterestRate").style.visibility = "hidden";
        
        //        for (var i = 1; i < chkHouseTypes.length; i++) {
        //            chkHouseTypes[i].checked = false;
        //        }
    }
    for (var i = 0; i < chkHouseTypes.length; i++) {
        chkHouseTypes[i].checked = true;
    }
    locationFilter();
} OwnRent
function PriceMonthly(M) {
    if ("Price" == M) {
        document.getElementById("ctl00_ContentPlaceHolder1_divPrice").style.display = "";
        document.getElementById("ctl00_ContentPlaceHolder1_divMonthly").style.display = "none";
        document.getElementById("ctl00_ContentPlaceHolder1_divInterestRate").style.visibility = "hidden";
    }
    else {
        document.getElementById("ctl00_ContentPlaceHolder1_divPrice").style.display = "none";
        document.getElementById("ctl00_ContentPlaceHolder1_divMonthly").style.display = "block";
        document.getElementById("ctl00_ContentPlaceHolder1_divInterestRate").style.visibility = "visible";
    }
}
function clearInput(me, text) {
    //debugger;
    if (me.value == text) {
        if (me.className == "")
            me.className = "regularTextbox";
        else
            me.className = me.className.replace("textbox", "regularTextbox");
        //me.className = "regularTextbox";
        me.value = "";
        var o = me.createTextRange();
        o.moveStart('character', (me.value.length));
        o.collapse();
        o.select();
    }
}
function restoreInput(me, text) {
    if (me.value == "") {
        if (me.className == "")
            me.className = "textbox";
        else
            me.className = me.className.replace("regularTextbox", "textbox");
        me.value = text;
    }
    else {
        if (me.className == "")
            me.className = "regularTextbox";
        else
            me.className = me.className.replace("textbox", "regularTextbox");
        scPriceMin.value = document.getElementById("ctl00_ContentPlaceHolder1_txtPriceMin").value;
        scPriceMax.value = document.getElementById("ctl00_ContentPlaceHolder1_txtPriceMax").value;
        
        scMonthlyPriceMin.value = document.getElementById("ctl00_ContentPlaceHolder1_txtMonthlyPriceMin").value;
        scMonthlyPriceMax.value = document.getElementById("ctl00_ContentPlaceHolder1_txtMonthlyPriceMax").value;
        
        scInterestRate.value = document.getElementById("ctl00_ContentPlaceHolder1_txtInterestRate").value;
        scDownPayment.value = document.getElementById("ctl00_ContentPlaceHolder1_txtDownPayment").value;
        scYearofloan.value = document.getElementById("ctl00_ContentPlaceHolder1_txtYearofloan").value;
        
        scBedroomsMin.value = document.getElementById("ctl00_ContentPlaceHolder1_txtBedroomMin").value;
        scBedroomsMax.value = document.getElementById("ctl00_ContentPlaceHolder1_txtBedroomMax").value;
        
        scBathroomsMin.value = document.getElementById("ctl00_ContentPlaceHolder1_txtBathroomMin").value;
        scBathroomsMax.value = document.getElementById("ctl00_ContentPlaceHolder1_txtBathroomMax").value;

        scSQFTMin.value = document.getElementById("ctl00_ContentPlaceHolder1_txtSqftMin").value;
        scSQFTMax.value = document.getElementById("ctl00_ContentPlaceHolder1_txtSqftMax").value;

        scLevelMin.value = document.getElementById("ctl00_ContentPlaceHolder1_txtStoriesMin").value;
        scLevelMax.value = document.getElementById("ctl00_ContentPlaceHolder1_txtStoriesMax").value;

        scGarageMin.value = document.getElementById("ctl00_ContentPlaceHolder1_txtGarageMin").value;
        scGarageMax.value = document.getElementById("ctl00_ContentPlaceHolder1_txtGarageMax").value;
        //debugger;
    }
    locationFilter();
//    else {
//        radPrice = document.getElementById("ctl00_ContentPlaceHolder1_radPrice");
//        if (radPrice.checked) {
//            locationFilter();
//        }
//        else {
//            scInterestRate.value = document.getElementById("ctl00_ContentPlaceHolder1_txtInterestRate").value;
//            scDownPayment.value = document.getElementById("ctl00_ContentPlaceHolder1_txtDownPayment").value;
//            scYearofloan.value = document.getElementById("ctl00_ContentPlaceHolder1_txtYearofloan").value;
//            if (scInterestRate.value >= 0 && scDownPayment.value >= 0 && scYearofloan.value >= 0) {
//                locationFilter();
//            }

//        }
//    }
}
//---------------------------------------------------------------------------------------------------------


//Map Functions--------------------------------------------------------------------------------------------

function createMarkerClickHandler(marker, text, link) {
    return function() {
        openInfoWindow(marker.labelText_);
        return false;
    };
}

function openInfoWindow(id) {
    for (var i = 0; i < batch.length; i++) {
        if (batch[i].labelText_ == id) {
            //debugger;
            //map.setZoom(12);
            //map.setMapType(G_NORMAL_MAP);
            map.setCenter(batch[i].getLatLng(), 15);
            batch[i].openInfoWindowHtml(batch[i].labelHTML_);
        }
    }
}
function createMarker(pointData) {
    var latlng = new GLatLng(pointData.latitude, pointData.longitude);
    ////
   
    //bound.extend(new GPoint(pointData.latitude, pointData.longitude));    
    ////
    var iconOptions = {};
    iconOptions.primaryColor = "#3CE415FF";
    iconOptions.cornerColor = "#D2F2D1FF";
    iconOptions.strokeColor = "#0C4F02FF";
    var icon = MapIconMaker.createMarkerIcon(iconOptions);
    icon.image = '../images/chart.png';
    icon.iconSize = new GSize(32, 32);
    icon.iconAnchor = new GPoint(18, 18);
    icon.infoWindowAnchor = new GPoint(20, 3);
    var HTML = '<div class="hfLabelBold">' + pointData.name + '</div><div class="hfLabel">' + pointData.address + '</div><input id="txtFrom" type="text" value="From address or Zip code" class="hfTxtInput" onclick="clearInput(this,\'From address or Zip code\')" onblur="restoreInput(this,\'From address or Zip code\')" style="width:140px;"><input type="submit" value="To here" onclick="showDirection(' + pointData.zip + '); return false;" class="hfButton" style="position:relative; top:-3px; left:2px;" />';
    opts = {
        "icon": icon,
        "clickable": true,
        "labelText": pointData.abbr,
        "labelHTML": HTML,
        "labelOffset": new GSize(-6, -16)
    };

    var marker = new LabeledMarker(latlng, opts);
    var handler = createMarkerClickHandler(marker, pointData.name, pointData.wp);
    GEvent.addListener(marker, "click", handler);
    return marker;
}

function iniGMap() {
    map = new GMap2(document.getElementById("gMap"));
    map.addControl(new GSmallMapControl());
    map.addMapType(G_PHYSICAL_MAP);
    map.setMapType(G_PHYSICAL_MAP);
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
    map.addControl(new GMapTypeControl());
    manager = new MarkerManager(map);

    // This is a sorting trick, don't worry too much about it.
    //markers.sort(function(a, b) { return (a.abbr > b.abbr) ? +1 : -1; });
    
    batch = [];
    for (id in markers) {
        if (markers[id]) batch.push(createMarker(markers[id]));
    }
//    for (var i = 0; i < batch.length; i++) {
//        bound.extend(batch[i]);
    //    }
    
    
    manager.addMarkers(batch, 1);
    
    
    manager.refresh();
    
    //map.centerAndZoomOnBounds(bound);
}

function locationFilter() 
{
    //debugger;
    //alert("location filter");
    bound = new GLatLngBounds();
    global_floorCount = 0;
    //document.getElementById("divProgress").style.display = "";
    document.getElementById("divProgress").style.height = document.body.clientHeight + "px";
    
    var sID = [_LevelDetail.length];
    var sNavigator = oNavigator.value.split("_");
    var pid;
    //Generate Parent ID array and SalesCenterID array
    for (var i = 0; i < _LevelDetail.length; i++) {
        sID[i] = (_LevelDetail[i][0] == _Level.length - 1) ? _LevelDetail[i][1] : 0;
    }
    //Filter id
    for (var i = 0; i < _LevelDetail.length; i++) {
        if (sID[i] != 0) {
            pid = sID[i];
            for (j = _Level.length - 1; j >= sNavigator.length; j--) {
                for (k = 0; k < _LevelDetail.length; k++) {
                    if (j == _LevelDetail[k][0] && pid == _LevelDetail[k][1]) {
                        pid = _LevelDetail[k][2];
                        k = _LevelDetail.length;
                    }
                }
            }
            if (pid != sNavigator[sNavigator.length - 1]) sID[i] = 0;
        }
    }
    findSearchControls();
    scids = "";
    communityCount = 0;
    scid_models = [batch.length];
    var show;
    for (var i = 0; i < batch.length; i++) {
        show = false;
        for (j = 0; j < sID.length; j++) {
            if (batch[i].labelText_ == sID[j]) { show = true;}
        }
        if (show) {
            if (showSalesCenter(batch[i].labelText_, true)) {
                batch[i].show();
                bound.extend(batch[i].getLatLng());
                communityCount++;
                //Salescenter List
                if ("" != scids) scids += ",";
                scids += batch[i].labelText_;
            }
            else 
            {
                //debugger;
                batch[i].hide();
            }
        }
        else {
            batch[i].hide();
            showSalesCenter(batch[i].labelText_, false);
        }
    }
    //debugger;
    bestFitAndZoom();
    showHomeCount(communityCount);
    
    if (ManualNavigationBarSelect != 1)
        doExtraTask();
}

function doExtraTask() 
{
    //debugger;
    var count = 0;
    var salesCenter;
    var city; var country;
    var tempSalesCenter = new Array(); var tempCity = new Array(); var tempCountry = new Array(); ;
    var count = 0;
    for (var i = 0; i < batch.length; i++) 
    {
        //debugger;
        if (batch[i].N) //if batch is shown
        {
            for (var j = 0; j < _LevelDetail.length; j++) 
            {
                if (batch[i].labelText_ == _LevelDetail[j][1] && _LevelDetail[j][0] == 3) 
                {
                    salesCenter = _LevelDetail[j][3];
                    for (var k = 0; k < _LevelDetail.length; k++) 
                    {
                        if (_LevelDetail[k][0] == 2 && _LevelDetail[k][1] == _LevelDetail[j][2]) 
                        {
                            city = _LevelDetail[k][3];
                            for (var l = 0; l < _LevelDetail.length; l++) 
                            {
                                if (_LevelDetail[l][0] == 1 && _LevelDetail[l][1] == _LevelDetail[k][2]) 
                                {
                                    country = _LevelDetail[l][3];
                                    tempSalesCenter[count] = salesCenter;
                                    tempCity[count] = city;
                                    tempCountry[count] = country;
                                    count++;
                                }
                            }
                        }   
                    }
                }
            }
        }
    }
    //debugger;
    
    //generate the dropdown list clicks
    AutoNavigationBarSelect = 1;
    resetNavigator();
    //check which menu to select
    if (tempCountry.length > 0) 
    {
        //check if country is common for all the bubbles
        var countryCount = 0;
        var counName = tempCountry[0];

        for (var i = 0; i < tempCountry.length; i++)
            if (tempCountry[i] == counName) countryCount++;
        //search for the country ID
        if (countryCount == tempCountry.length) 
        {
            for (var j = 0; j < _country.length; j++) 
            {

                if (_country[j][1] == counName) 
                {
                    var countryID = _country[j][0];
                    showNavigatorSelector(1, 0);
                    NavigatorSelected(1, countryID);

                    //check if city is common for all the bubbles
                    var cityCount = 0;
                    var cityName = tempCity[0];

                    for (var i = 0; i < tempCity.length; i++)
                        if (tempCity[i] == cityName) cityCount++;

                    if (cityCount == tempCity.length) {
                        for (var j = 0; j < _city.length; j++) {
                            if (_city[j][1] == cityName) {
                                var cityID = _city[j][0];
                                showNavigatorSelector(2, 0);
                                NavigatorSelected(2, cityID);

                                //check if salesCenter is common for all the bubbles
                                var salesCenterCount = 0;
                                var salesCenterName = tempSalesCenter[0];

                                for (var i = 0; i < tempSalesCenter.length; i++)
                                    if (tempSalesCenter[i] == salesCenterName) salesCenterCount++;

                                if (salesCenterCount == tempSalesCenter.length) {
                                    for (var j = 0; j < _salesCenter.length; j++) {
                                        if (_salesCenter[j][1] == salesCenterName) {
                                            var salesCenterID = _salesCenter[j][0];
                                            showNavigatorSelector(3, 0);
                                            NavigatorSelected(3, salesCenterID);
                                            break;
                                        }
                                    }
                                }
                                break;
                            }

                        }
                    }
                    break;
                }
            }
        }
    }
    AutoNavigationBarSelect = 0; 
    
}

function showHomeCount(communityCount) {
    var resultDiv = document.getElementById("SearchResultText");
    if (resultDiv) {
        if (communityCount == 0)
            resultDiv.innerHTML = "0 Communitie(s) & 0 Home(s) Match Your Search";
        else
            resultDiv.innerHTML = communityCount +" Communitie(s) & " +  global_floorCount + " Home(s) Match Your Search";
    }
}

function findSearchControls() {
    //debugger;
    radOwn = document.getElementById("ctl00_ContentPlaceHolder1_radOwn");
    radPrice = document.getElementById("ctl00_ContentPlaceHolder1_radPrice");
    chkHouseTypes = document.getElementById("ctl00_ContentPlaceHolder1_chkHouseType").getElementsByTagName('input');

    scPriceMin = document.getElementById("ctl00_ContentPlaceHolder1_txtPriceMin");
    scPriceMax = document.getElementById("ctl00_ContentPlaceHolder1_txtPriceMax");
    scMonthlyPriceMin = document.getElementById("ctl00_ContentPlaceHolder1_txtMonthlyPriceMin");
    scMonthlyPriceMax = document.getElementById("ctl00_ContentPlaceHolder1_txtMonthlyPriceMax");
    scInterestRate = document.getElementById("ctl00_ContentPlaceHolder1_txtInterestRate");
    scDownPayment = document.getElementById("ctl00_ContentPlaceHolder1_txtDownPayment");
    scYearofloan = document.getElementById("ctl00_ContentPlaceHolder1_txtYearofloan");
    scBedroomsMin = document.getElementById("ctl00_ContentPlaceHolder1_txtBedroomMin");
    scBedroomsMax = document.getElementById("ctl00_ContentPlaceHolder1_txtBedroomMax");
    scBathroomsMin = document.getElementById("ctl00_ContentPlaceHolder1_txtBathroomMin");
    scBathroomsMax = document.getElementById("ctl00_ContentPlaceHolder1_txtBathroomMax");
    scSQFTMin = document.getElementById("ctl00_ContentPlaceHolder1_txtSqftMin");
    scSQFTMax = document.getElementById("ctl00_ContentPlaceHolder1_txtSqftMax");
    scLevelMin = document.getElementById("ctl00_ContentPlaceHolder1_txtStoriesMin");
    scLevelMax = document.getElementById("ctl00_ContentPlaceHolder1_txtStoriesMax");

    scGarageMin = document.getElementById("ctl00_ContentPlaceHolder1_txtGarageMin");
    scGarageMax = document.getElementById("ctl00_ContentPlaceHolder1_txtGarageMax");
    
    
//    scBedrooms = document.getElementById("ctl00_ContentPlaceHolder1_ddlBedroom");
//    scBathrooms = document.getElementById("ctl00_ContentPlaceHolder1_ddlBathroom");
//    scSQFT = document.getElementById("ctl00_ContentPlaceHolder1_ddlSQFT");
//    scPriceMin = document.getElementById("ctl00_ContentPlaceHolder1_ddlPricemin");
//    scPriceMax = document.getElementById("ctl00_ContentPlaceHolder1_ddlPricemax");
//    scMonthlyMin = document.getElementById("ctl00_ContentPlaceHolder1_ddlMonthlymin");
//    scMonthlyMax = document.getElementById("ctl00_ContentPlaceHolder1_ddlMonthlymax");
//    scInterestRate = document.getElementById("ctl00_ContentPlaceHolder1_txtInterestRate");
//    scDownPayment = document.getElementById("ctl00_ContentPlaceHolder1_txtDownPayment");
//    scYearofloan = document.getElementById("ctl00_ContentPlaceHolder1_txtYearofloan");
    //SearchOptions = document.getElementById("ctl00_ContentPlaceHolder1_panSearchOption").getElementsByTagName('input');
    //debugger;
    if (document.getElementById("divCoreSearch").style.display == "" || document.getElementById("divCoreSearch").style.display == "block") 
    {
        SearchOptions = document.getElementById("ctl00_ContentPlaceHolder1_panHomeSearch").getElementsByTagName('input');
    }
    else 
    {
        SearchOptions = document.getElementById("ctl00_ContentPlaceHolder1_panSearchOption").getElementsByTagName('input');
    }
}

//function searchOptionFilter(dv) {
//    var totalAvailability = 0;
//    var totalFloorPlans = 0;
//    var spanTotalAvailability = null;
//    var spanTotalFloorPlans = null;
//    var show = true;
//    var PriceMin = 0;
//    var PriceMax = 0;

//    //Get Div ID
//    var dvid = dv.id;
//    var spans = dv.getElementsByTagName('span');

//    //Filter By Search Criteria
//    for (var i = 0; i < spans.length; i++) {
//        if (spans[i].id == "Availability") spanTotalAvailability = spans[i];
//        if (spans[i].id == "FloorPlans") spanTotalFloorPlans = spans[i];
//        //Own Rent (SalesCenter Level)
//        if (spans[i].id == "Building_Use") {
//            if (radOwn.checked) {
//                if (spans[i].innerHTML.indexOf('S') == -1) show = false;
//            }
//            else {
//                if (spans[i].innerHTML.indexOf('R') == -1) show = false;
//            }
//        }
//        //More Option (SalesCenter Level)
//        if (spans[i].id == "SearchCriteria") {
//            for (var j = 0; j < SearchOptions.length; j++) {
//                if (SearchOptions[j].checked) {
//                    var checkID = SearchOptions[j].id.split('_')[SearchOptions[j].id.split('_').length - 1];
//                    if (spans[i].innerHTML.indexOf(checkID) == -1) show = false;
//                }
//            }
//        }
//        //Loop through Models 
//        if (spans[i].id.indexOf("lblModelName") > -1) {
//            var showModel = true;
//            //get Model values
//            var id = spans[i].id;
//            id = id.replace(id.split('_')[id.split('_').length - 1], '');

//            //----------------------------------------------------------------------------- Todo performance improve
//            var spanModelType = document.getElementById(id + "lblModelType");
//            var spanStartingPrice = document.getElementById(id + "lblStartingPrice");
//            var spanBedrooms = document.getElementById(id + "lblBedrooms_No");
//            var spanBaths = document.getElementById(id + "lblBaths_No");
//            var spanSqft = document.getElementById(id + "lblSqft");
//            var spanAvailability = document.getElementById(id + "lblAvailability");
//            //-----------------------------------------------------------------------------

//            //Filter House type (Model Level)
//            for (var j = 0; j < chkHouseTypes.length; j++) {
//                if (!chkHouseTypes[j].checked) {
//                    var checkID = chkHouseTypes[j].id.split('_')[chkHouseTypes[j].id.split('_').length - 1];
//                    if (spanModelType.innerHTML == checkID) {
//                        showModel = false;
//                    }
//                }
//            }
//            //Filter Price (Model Level)
//            if (radPrice.checked) {
//                PriceMin = scPriceMin.value;
//                PriceMax = scPriceMax.value;
//            }
//            else {
//                if (radOwn.checked) {
//                    if (scInterestRate.value >= 0 && scDownPayment.value >= 0 && scYearofloan.value >= 0) {
//                        //US Calculate
//                        var monthlyinterest = scInterestRate.value / 12 / 100.0;
//                        var monthsofloan = scYearofloan.value * 12;
//                        PriceMin = scDownPayment.value * 1.0 + scMonthlyMin.value * (1 - Math.pow(1 + monthlyinterest, -1 * monthsofloan)) / monthlyinterest;
//                        PriceMax = scDownPayment.value * 1.0 + scMonthlyMax.value * (1 - Math.pow(1 + monthlyinterest, -1 * monthsofloan)) / monthlyinterest;
//                    }
//                }
//                else {
//                    PriceMin = scMonthlyMin.value;
//                    PriceMax = scMonthlyMax.value;
//                }
//            }
//            if (parseFloat(spanStartingPrice.innerHTML) < parseFloat(PriceMin) || parseFloat(spanStartingPrice.innerHTML) >= parseFloat(PriceMax)) showModel = false;
//            //Filter Home Type Bedroom Bathroom SQFT (Model Level)
//            if (scBedrooms.value >= 0 && spanBedrooms.innerHTML != scBedrooms.value) showModel = false;
//            if (scBathrooms.value >= 0 && spanBaths.innerHTML != scBathrooms.value) showModel = false;
//            var scSQFTsplit = scSQFT.value.split('-');
//            if (scSQFTsplit.length == 2) {
//                if (parseFloat(spanSqft.innerHTML) < parseFloat(scSQFTsplit[0]) || parseFloat(spanSqft.innerHTML) >= parseFloat(scSQFTsplit[1])) showModel = false;
//            }
//            //Show Hide Models & Recount
//            
//            if (showModel) {
//                if (spans[i].parentNode) {
//                    spans[i].parentNode.parentNode.style.display = "";
//                    totalAvailability += spanAvailability.innerHTML * 1;
//                    totalFloorPlans++;
//                }
//            }
//            else {
//                if (spans[i].parentNode) {
//                    spans[i].parentNode.parentNode.style.display = "none";
//                }
//            }
//        }
//    }
//    tempFloorCount = totalFloorPlans;
//    global_floorCount = global_floorCount + totalFloorPlans;
//    if (0 == totalFloorPlans) {
//        show = false;
//    }
//    else {
//        spanTotalAvailability.innerHTML = totalAvailability;
//        //spanTotalFloorPlans.innerHTML = totalFloorPlans;
//    }
//    return show;
//}
//----------------------------------------------------------------------------------------------------------
function iniSiteNavigate(label, rURL) {
    //Read Cookie
    var cLabel = new Array();
    var cURL = new Array();
    var i = 0;
    do {
        if (fetch_cookie('L' + i + 'Label')) {
            cLabel[i] = fetch_cookie('L' + i + 'Label');
            cURL[i] = fetch_cookie('L' + i + 'URL');
            delete_cookie('L' + i + 'Label');
            delete_cookie('L' + i + 'URL');
            i++;
        }
        else {
            i = -1;
        }

    }
    while (i > 0)
    //Generate Site Navigate
    var strSiteNavigate = '';
    var Navigate = document.getElementById("divNavigatorBar");
    var page = rURL.split('?')[0];
    i = 0;
    while (i < cLabel.length && page != cURL[i].split('?')[0] && label != "HOME SEARCH") {
        strSiteNavigate += '<a href="' + cURL[i] + '" class="hfNavigatorButton">' + cLabel[i] + '</a><img src="images/arrow.png" />';
        set_cookie('L' + i + 'Label', cLabel[i]);
        set_cookie('L' + i + 'URL', cURL[i]);
        i++;
    }
    strSiteNavigate += '<a href="' + rURL + '" class="hfNavigatorButton">' + label + '</a><img src="images/arrow.png" />';
    set_cookie('L' + i + 'Label', label);
    set_cookie('L' + i + 'URL', rURL);
    Navigate.innerHTML = strSiteNavigate;
}

function iniHomesearch() {
    iniSiteNavigate("HOME SEARCH", "HomeSearch.aspx");
    //    var Navigate = document.getElementById("divNavigatorBar");
    //    Navigate.innerHTML += '<img src="images/navbar_separator.gif" style="position:absolute; left:685px;"/><span class="hfNavigatorButton" style="position:absolute; left:685px; top:5px;" onclick="clearSearchResault()">CLEAR SEARCH RESULTS</span>'
}

function clearSearchResault() {
    //debugger;
    findSearchControls();
    if (resterOnOwnRentClick != "1") {
        if (!radOwn.checked)
            radOwn.click();
    }
    //radPrice.click();
    //debugger;
    clearRegularPriceDiv(null, null);
    clearMonthlyDiv(null, null);
    clearOtherFilters();

    document.getElementById("monthlyPayment").style.visibility = "visible";
    document.getElementById("ctl00_ContentPlaceHolder1_divInterestRate").style.visibility = "visible";
    document.getElementById('dvPrice').style.visibility = "visible";
    
    
    //scBedrooms.selectedIndex = 0;
    //scBathrooms.selectedIndex = 0;
    //scSQFT.selectedIndex = 0;
    //scPriceMin.selectedIndex = 0;
    //scPriceMax.selectedIndex = 0;
    //scMonthlyMin.selectedIndex = 0;
    //scMonthlyMax.selectedIndex = 0;
    //scInterestRate.value = "Interest Rate";
    //scDownPayment.value = "Down Payment";
    //scYearofloan.value = "Year of loan";
    for (var i = 0; i < chkHouseTypes.length; i++) {
        chkHouseTypes[i].checked = true;
    }
    //debugger;
    //reset filters from both the tabs
    var tempSearchOption = document.getElementById("ctl00_ContentPlaceHolder1_panHomeSearch").getElementsByTagName('input');
    for (var i = 0; i < tempSearchOption.length; i++) {
        tempSearchOption[i].checked = false;
    }
    tempSearchOption = document.getElementById("ctl00_ContentPlaceHolder1_panSearchOption").getElementsByTagName('input');
    for (var i = 0; i < tempSearchOption.length; i++) {
        tempSearchOption[i].checked = false;
    }
//    for (var i = 0; i < SearchOptions.length; i++) {
//        SearchOptions[i].checked = false;
//    }
    resetNavigator();
    //initialize();
    iniGMap();
    locationFilter();
    saveLastSearchValues();
    //map.setMapType(G_PHYSICAL_MAP);
    //map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
}

function addremoveFavorite(param) {
    //    if (prop == "value") {
    //        if (document.getElementById("btnFavorites_" + param).value == "Add to Favorites") {
    //            SaveTo247('AddFavorites', "&modelid=" + param);
    //            document.getElementById("btnFavorites_" + param).value = "Remove from Favotires"
    //        }
    //        else {
    //            SaveTo247('RemoveFavorites', "&modelid=" + param);
    //            document.getElementById("btnFavorites_" + param).value = "Add to Favorites";
    //        }
    //    }
    //    else {
    //debugger;
    var scid = document.getElementById("SCID").value;
    try {
        window.parent.document.getElementById("ctl00_hdnModelId").value = param;
        var image = window.parent.document.getElementById("imgFavorite");
        if (document.getElementById("btnFavorites_" + param).checked == true) {
            if (image) image.src = "../images/hearts.gif";
            SaveTo247('AddFavorites', "&modelid=" + param, scid);
            //document.getElementById("lblFavorites_" + param).innerText = "Remove from Favotires";
            //document.getElementById("btnFavorites_" + param).checked = "true";
        }
        else {
            if (image) image.src = "../images/AddToFavorite.png";
            SaveTo247('RemoveFavorites', "&modelid=" + param, scid);
            //document.getElementById("lblFavorites_" + param).innerText = "Add to Favorites";
            //document.getElementById("btnFavorites_" + param).src = "images/uncheck.gif";
        }
    }
    catch (err) {
        alert('This functionality is not available from backend');
    }
    //    }

}
function iniSavedSearchCriteria() {
//    if (document.getElementById("ctl00_ContentPlaceHolder1_radRent").checked) {
//        OwnRent("Rent");
//    }
//    if (document.getElementById("ctl00_ContentPlaceHolder1_radMonthly").checked) {
//        PriceMonthly("Monthly");
//    }
}

function searchOptionFilter(dv) {
    var totalAvailability = 0;
    var totalFloorPlans = 0;
    var spanTotalAvailability = null;
    var spanTotalFloorPlans = null;
    var show = true;
    var PriceMin = 0;
    var PriceMax = 0;

    //Get Div ID
    var dvid = dv.id;
    var spans = dv.getElementsByTagName('span');

    //Filter By Search Criteria
    for (var i = 0; i < spans.length; i++) {
        if (spans[i].id == "Availability") spanTotalAvailability = spans[i];
        if (spans[i].id == "FloorPlans") spanTotalFloorPlans = spans[i];
        //Own Rent (SalesCenter Level)
        if (spans[i].id == "Building_Use") {
            if (radOwn.checked) {
                if (spans[i].innerHTML.indexOf('S') == -1) show = false;
            }
            else {
                if (spans[i].innerHTML.indexOf('R') == -1) show = false;
            }
        }
        //More Option (SalesCenter Level)
        if (spans[i].id == "SearchCriteria") {
            for (var j = 0; j < SearchOptions.length; j++) {
                if (SearchOptions[j].checked) {
                    var checkID = SearchOptions[j].id.split('_')[SearchOptions[j].id.split('_').length - 1];
                    if (spans[i].innerHTML.indexOf(checkID) == -1) show = false;
                }
            }
        }
        //Loop through Models 
        if (spans[i].id.indexOf("lblModelName") > -1) {
            var showModel = true;
            //get Model values
            var id = spans[i].id;
            id = id.replace(id.split('_')[id.split('_').length - 1], '');

            //----------------------------------------------------------------------------- Todo performance improve
            var spanModelType = document.getElementById(id + "lblModelType");
            var spanStartingPrice = document.getElementById(id + "lblStartingPrice");
            var spanBedrooms = document.getElementById(id + "lblBedrooms_No");
            var spanBaths = document.getElementById(id + "lblBaths_No");
            var spanSqft = document.getElementById(id + "lblSqft");
            var spanAvailability = document.getElementById(id + "lblAvailability");
            var spanGarage = document.getElementById(id + "lblGarage");
            var spanStories = document.getElementById(id + "lblStories");
            var imageFavorites = document.getElementById(id + "imgFavorites");
            //-----------------------------------------------------------------------------

            //Filter House type (Model Level)
            for (var j = 0; j < chkHouseTypes.length; j++) {
                if (!chkHouseTypes[j].checked) {
                    var checkID = chkHouseTypes[j].id.split('_')[chkHouseTypes[j].id.split('_').length - 1];
                    if (spanModelType.innerHTML == checkID) {
                        showModel = false;
                    }
                }
            }
            //debugger;
            //Filter Price (Model Level)
            //if (radPrice.checked) {
            PriceMin = (scPriceMin.value.toLowerCase() == "min") ? "" : scPriceMin.value;
            PriceMax = (scPriceMax.value.toLowerCase() == "max") ? "" : scPriceMax.value;
            BedroomMin = (scBedroomsMin.value.toLowerCase() == "min") ? "" : scBedroomsMin.value;
            BedroomMax = (scBedroomsMax.value.toLowerCase() == "max") ? "" : scBedroomsMax.value;
            BathroomMin = (scBathroomsMin.value.toLowerCase() == "min") ? "" : scBathroomsMin.value;
            BathroomMax = (scBathroomsMax.value.toLowerCase() == "max") ? "" : scBathroomsMax.value;
            SQFTMin = (scSQFTMin.value.toLowerCase() == "min") ? "" : scSQFTMin.value;
            SQFTMax = (scSQFTMax.value.toLowerCase() == "max") ? "" : scSQFTMax.value;
            DownPayment = (scDownPayment.value.toLowerCase() == "deposit") ? "" : scDownPayment.value;
            YearOfLoan = (scYearofloan.value.toLowerCase() == "term") ? "" : scYearofloan.value;
            Interest = (scInterestRate.value.toLowerCase() == "rate") ? "" : scInterestRate.value;
            MonthlyMin = (scMonthlyPriceMin.value.toLowerCase() == "min") ? "" : scMonthlyPriceMin.value;
            MonthlyMax = (scMonthlyPriceMax.value.toLowerCase() == "max") ? "" : scMonthlyPriceMax.value;
            GarageMin = (scGarageMin.value.toLowerCase() == "min") ? "" : scGarageMin.value;
            GarageMax = (scGarageMax.value.toLowerCase() == "max") ? "" : scGarageMax.value;
            StoriesMin = (scLevelMin.value.toLowerCase() == "min") ? "" : scLevelMin.value;
            StoriesMax = (scLevelMax.value.toLowerCase() == "max") ? "" : scLevelMax.value;

            var startingPriceWithoutDecimal = spanStartingPrice.innerHTML.split('.')[0];          
            if (radOwn.checked && document.getElementById("dvPrice").style.visibility != "hidden") {
                if (parseFloat(startingPriceWithoutDecimal) < parseFloat(PriceMin) || parseFloat(startingPriceWithoutDecimal) > parseFloat(PriceMax)) showModel = false;
            }
            else 
            {
                //debugger;
                if (radOwn.checked)//calculate monthly
                {
                    if (DownPayment != "" && YearOfLoan != "" && Interest != "") {
                        if (DownPayment > 0 && YearOfLoan > 0 && Interest > 0) {
                            //US Calculate
                            var monthlyinterest = Interest / 12 / 100.0;
                            var monthsofloan = YearOfLoan * 12;
                            MonthlyPriceMin = scDownPayment.value * 1.0 + MonthlyMin * (1 - Math.pow(1 + monthlyinterest, -1 * monthsofloan)) / monthlyinterest;
                            MonthlyPriceMax = scDownPayment.value * 1.0 + MonthlyMax * (1 - Math.pow(1 + monthlyinterest, -1 * monthsofloan)) / monthlyinterest;
                            if (parseFloat(startingPriceWithoutDecimal) < parseFloat(MonthlyPriceMin) || parseFloat(startingPriceWithoutDecimal) > parseFloat(MonthlyPriceMax)) showModel = false;
                        }
                    }
                }
                else //calculate regular
                {
                    if (parseFloat(startingPriceWithoutDecimal) < parseFloat(MonthlyMin) || parseFloat(startingPriceWithoutDecimal) > parseFloat(MonthlyMax)) showModel = false; 
                } 
            }

            
            
            
            //}
//            else {
//                if (radOwn.checked) {
//                    if (scInterestRate.value >= 0 && scDownPayment.value >= 0 && scYearofloan.value >= 0) {
//                        //US Calculate
//                        var monthlyinterest = scInterestRate.value / 12 / 100.0;
//                        var monthsofloan = scYearofloan.value * 12;
//                        PriceMin = scDownPayment.value * 1.0 + scMonthlyMin.value * (1 - Math.pow(1 + monthlyinterest, -1 * monthsofloan)) / monthlyinterest;
//                        PriceMax = scDownPayment.value * 1.0 + scMonthlyMax.value * (1 - Math.pow(1 + monthlyinterest, -1 * monthsofloan)) / monthlyinterest;
//                    }
//                }
//                else {
//                    PriceMin = scMonthlyMin.value;
//                    PriceMax = scMonthlyMax.value;
//                }
//            }
            //debugger;
            
            //Filter Home Type Bedroom Bathroom SQFT (Model Level)
            
            if (BedroomMin != "" && parseInt(BedroomMin) >= 0 && parseInt(spanBedrooms.innerHTML) < parseInt(BedroomMin)) showModel = false;
            if (BedroomMax != "" && parseInt(BedroomMax) >= 0 && parseInt(spanBedrooms.innerHTML) > parseInt(BedroomMax)) showModel = false;

            if (BathroomMin != "" && parseFloat(BathroomMin) >= 0 && parseFloat(spanBaths.innerHTML) < parseFloat(BathroomMin)) showModel = false;
            if (BathroomMax != "" && parseFloat(BathroomMax) >= 0 && parseFloat(spanBaths.innerHTML) > parseFloat(BathroomMax)) showModel = false;

            if (SQFTMin != "" && parseFloat(SQFTMin) >= 0 && parseFloat(spanSqft.innerHTML) < parseFloat(SQFTMin)) showModel = false;
            if (SQFTMax != "" && parseFloat(SQFTMax) >= 0 && parseFloat(spanSqft.innerHTML) > parseFloat(SQFTMax)) showModel = false;

            if (GarageMin != "" && parseFloat(GarageMin) >= 0 && parseFloat(spanGarage.innerHTML) < parseFloat(GarageMin)) showModel = false;
            if (GarageMax != "" && parseFloat(GarageMax) >= 0 && parseFloat(spanGarage.innerHTML) > parseFloat(GarageMax)) showModel = false;

            if (StoriesMin != "" && parseFloat(StoriesMin) >= 0 && spanStories.innerHTML != "" && parseFloat(spanStories.innerHTML) < parseFloat(StoriesMin)) showModel = false;
            if (StoriesMax != "" && parseFloat(StoriesMax) >= 0 && spanStories.innerHTML != "" && parseFloat(spanStories.innerHTML) > parseFloat(StoriesMax)) showModel = false;
            
//            var scSQFTsplit = scSQFTMin.value.split('-');
//            if (scSQFTsplit.length == 2) {
//                if (parseFloat(spanSqft.innerHTML) < parseFloat(scSQFTsplit[0]) || parseFloat(spanSqft.innerHTML) >= parseFloat(scSQFTsplit[1])) showModel = false;
//            }
            //Show Hide Models & Recount

            if (document.getElementById("hdnImagefilter").value == "true") {
                //debugger;
                if (imageFavorites.src.indexOf("AddToFavorite.png") > 0) showModel = false;
            }
            if (showModel) {
                if (spans[i].parentNode) {
                    spans[i].parentNode.parentNode.style.display = "";
                    totalAvailability += spanAvailability.innerHTML * 1;
                    totalFloorPlans++;
                }
            }
            else {
                if (spans[i].parentNode) {
                    spans[i].parentNode.parentNode.style.display = "none";
                }
            }
        }
    }
    tempFloorCount = totalFloorPlans;
    global_floorCount = global_floorCount + totalFloorPlans;
    if (0 == totalFloorPlans) {
        show = false;
    }
    else {
        spanTotalAvailability.innerHTML = totalAvailability;
        //spanTotalFloorPlans.innerHTML = totalFloorPlans;
    }
    return show;
}

function clearMonthlyDiv(me, e) {
    //document.getElementById('monthlyPayment').style.visibility = "hidden";
    //document.getElementById('ctl00_ContentPlaceHolder1_divInterestRate').style.visibility = "hidden";
    //debugger;
    if (!me || (me && me.value.length != 0 && me.valueOf != "")) //check for "tab" keyup
    {
        scInterestRate.value = "Rate";
        scDownPayment.value = "Deposit";
        scYearofloan.value = "Term";
        scMonthlyPriceMin.value = "Min";
        scMonthlyPriceMax.value = "Max";
        if (scInterestRate.className == "")
            scInterestRate.className = "textbox";
        else
            scInterestRate.className = scInterestRate.className.replace("regularTextbox", "textbox");

        if (scDownPayment.className)
            scDownPayment.className = "textbox";
        else
            scDownPayment.className = scDownPayment.className.replace("regularTextbox", "textbox");

        if (scYearofloan.className == "")
            scYearofloan.className = "textbox";
        else
            scYearofloan.className = scYearofloan.className.replace("regularTextbox", "textbox");

        if (scMonthlyPriceMin.className == "")
            scMonthlyPriceMin.className = "textbox";
        else
            scMonthlyPriceMin.className = scMonthlyPriceMin.className.replace("regularTextbox", "textbox");

        if (scMonthlyPriceMax.className == "")
            scMonthlyPriceMax.className = "textbox";
        else
            scMonthlyPriceMax.className = scMonthlyPriceMax.className.replace("regularTextbox", "textbox");
    }    
    if (e !=null && e.keyCode == 13) //Enter key
    {
        locationFilter();
    }
    
    
}

function clearRegularPriceDiv(me, e) {
    //document.getElementById('dvPrice').style.visibility = "hidden";
    if(!me || (me && me.value.length !=0 && me.valueOf != "")) //check for "tab" keyup
    {
        scPriceMin.value = "Min";
        scPriceMax.value = "Max";
        if (scPriceMin.className == "")
            scPriceMin.className = "textbox";
        else
            scPriceMin.className = scPriceMin.className.replace("regularTextbox", "textbox");

        if (scPriceMax.className == "")
            scPriceMax.className = "textbox";
        else
            scPriceMax.className = scPriceMax.className.replace("regularTextbox", "textbox");
     }
    if (e != null && e.keyCode == 13) //Enter key
    {
        locationFilter();
    }
}

function clearOtherFilters() {
    scBedroomsMax.value = "Max";
    if (scBedroomsMax.className == "") scBedroomsMax.className = "textbox";
    else scBedroomsMax.className = scBedroomsMax.className.replace("regularTextbox", "textbox");
    
    scBedroomsMin.value = "Min";
    if (scBedroomsMin.className == "") scBedroomsMin.className = "textbox";
    scBedroomsMin.className = scBedroomsMin.className.replace("regularTextbox", "textbox");
    
    scBathroomsMax.value = "Max";
    if (scBathroomsMax.className == "") scBathroomsMax.className = "textbox";
    else scBathroomsMax.className = scBathroomsMax.className.replace("regularTextbox", "textbox");
    
    scBathroomsMin.value = "Min";
    if (scBathroomsMin.className == "") scBathroomsMin.className = "textbox";
    else scBathroomsMin.className = scBathroomsMin.className.replace("regularTextbox", "textbox");
    
    scSQFTMax.value = "Max";
    if (scSQFTMax.className == "") scSQFTMax.className = "textbox";
    else scSQFTMax.className = scSQFTMax.className.replace("regularTextbox", "textbox");
    
    scSQFTMin.value = "Min";
    if (scSQFTMin.className == "") scSQFTMin.className = "textbox";
    else scSQFTMin.className = scSQFTMin.className.replace("regularTextbox", "textbox");

    scGarageMax.value = "Max";
    if (scGarageMax.className == "") scGarageMax.className = "textbox";
    else scGarageMax.className = scGarageMax.className.replace("regularTextbox", "textbox");
    
    scGarageMin.value = "Min";
    if (scGarageMin.className == "") scGarageMin.className = "textbox";
    else scGarageMin.className = scGarageMin.className.replace("regularTextbox", "textbox");
    
    scLevelMax.value = "Max";
    if (scLevelMax.className == "") scLevelMax.className = "textbox";
    else scLevelMax.className = scLevelMax.className.replace("regularTextbox", "textbox");
    
    scLevelMin.value = "Min";
    if (scLevelMin.className == "") scLevelMin.className = "textbox";
    else scLevelMin.className = scLevelMin.className.replace("regularTextbox", "textbox");
}

function checkAndFilter(me, e) {
    if (e.keyCode == 13) //Enter key
    {
        locationFilter();
    }
}

function initiateImageFilter() {
    locationFilter();
    document.getElementById("hdnImagefilter").value = "";
}

function anonymousSave() 
{
    document.getElementById("hdnAnonymousSearch").value = 1;
    SaveSearchCriteria();
}

 
function saveLastSearchValues() 
{
    lastNavigator = document.getElementById("Navigator").value;
    lastRadOwn = radOwn.checked;
    if (radOwn.checked) lastRadRent = "false";
    else lastRadRent = "true";
    lastHouseType = new Array();
    for (var i = 0; i < chkHouseTypes.length; i++) {
        lastHouseType[i] = chkHouseTypes[i].checked;
    }

    lastBedroomMin = (scBedroomsMin.value.toLowerCase() == "min") ? "" : scBedroomsMin.value;
    lastBedroomMax = (scBedroomsMax.value.toLowerCase() == "max") ? "" : scBedroomsMax.value;
    lastBathroomMin = (scBathroomsMin.value.toLowerCase() == "min") ? "" : scBathroomsMin.value;
    lastBathroomMax = (scBathroomsMax.value.toLowerCase() == "max") ? "" : scBathroomsMax.value;
    lastSQFTMin = (scSQFTMin.value.toLowerCase() == "min") ? "" : scSQFTMin.value;
    lastSQFTMax = (scSQFTMax.value.toLowerCase() == "max") ? "" : scSQFTMax.value;
    lastGarageMin = (scGarageMin.value.toLowerCase() == "min") ? "" : scGarageMin.value;
    lastGarageMax = (scGarageMax.value.toLowerCase() == "max") ? "" : scGarageMax.value;
    lastStoriesMin = (scLevelMin.value.toLowerCase() == "min") ? "" : scLevelMin.value;
    lastStoriesMax = (scLevelMax.value.toLowerCase() == "max") ? "" : scLevelMax.value;

    if (document.getElementById("dvPrice").style.visibility != "hidden" && (scPriceMin.value.toLowerCase() != "min" || scPriceMax.value.toLowerCase() != "max")) 
    {
        lastPriceMin = (scPriceMin.value.toLowerCase() == "min") ? "" : scPriceMin.value;
        lastPriceMax = (scPriceMax.value.toLowerCase() == "max") ? "" : scPriceMax.value;
        //initialize others as blank
        lastPriceMonthlyMin = (scMonthlyPriceMin.value.toLowerCase() == "min") ? "" : scMonthlyPriceMin.value;
        lastPriceMonthlyMax = (scMonthlyPriceMax.value.toLowerCase() == "max") ? "" : scMonthlyPriceMax.value;
        lastInterestRate = "";
        lastDownPayment = "";
        lastYearOfLoan = "";
     }
     else
     {
         lastPriceMonthlyMin = (scMonthlyPriceMin.value.toLowerCase() == "min") ? "" : scMonthlyPriceMin.value;
         lastPriceMonthlyMax = (scMonthlyPriceMax.value.toLowerCase() == "max") ? "" : scMonthlyPriceMax.value;
         lastInterestRate = (scInterestRate.value.toLowerCase() == "rate") ? "" : scInterestRate.value;
         lastDownPayment = (scDownPayment.value.toLowerCase() == "deposit") ? "" : scDownPayment.value;
         lastYearOfLoan = (scYearofloan.value.toLowerCase() == "term") ? "" : scYearofloan.value;
         //initialize others as blank
         lastPriceMin = "";
         lastPriceMax = "";
     }

     //set value from both tabs
    lastHomeSearch = new Array();
    var tempSearchOption = document.getElementById("ctl00_ContentPlaceHolder1_panHomeSearch").getElementsByTagName('input');
    for (var i = 0; i < tempSearchOption.length; i++) 
    {
        lastHomeSearch[i] = tempSearchOption[i].checked;
    }

    lastCommunitySearch = new Array();
    var tempSearchOption = document.getElementById("ctl00_ContentPlaceHolder1_panSearchOption").getElementsByTagName('input');
    for (var i = 0; i < tempSearchOption.length; i++) 
    {
        lastCommunitySearch[i] = tempSearchOption[i].checked;
    }
}

function compareWithLastSavedValue() {
    if (document.getElementById("Navigator").value != lastNavigator) return true;
    if (radOwn.checked != lastRadOwn) return true;
    for (var i = 0; i < chkHouseTypes.length; i++) {
        if (lastHouseType[i] != chkHouseTypes[i].checked) return true; ;
    }
    if (lastBedroomMin != ((scBedroomsMin.value.toLowerCase() == "min") ? "" : scBedroomsMin.value)) return true;
    if (lastBedroomMax != ((scBedroomsMax.value.toLowerCase() == "max") ? "" : scBedroomsMax.value)) return true;
    if (lastBathroomMin != ((scBathroomsMin.value.toLowerCase() == "min") ? "" : scBathroomsMin.value)) return true;
    if (lastBathroomMax != ((scBathroomsMax.value.toLowerCase() == "max") ? "" : scBathroomsMax.value)) return true;
    if (lastSQFTMin != ((scSQFTMin.value.toLowerCase() == "min") ? "" : scSQFTMin.value)) return true;
    if (lastSQFTMax != ((scSQFTMax.value.toLowerCase() == "max") ? "" : scSQFTMax.value)) return true;
    if (lastGarageMin != ((scGarageMin.value.toLowerCase() == "min") ? "" : scGarageMin.value)) return true;
    if (lastGarageMax != ((scGarageMax.value.toLowerCase() == "max") ? "" : scGarageMax.value)) return true;
    if (lastStoriesMin != ((scLevelMin.value.toLowerCase() == "min") ? "" : scLevelMin.value)) return true;
    if (lastStoriesMax != ((scLevelMax.value.toLowerCase() == "max") ? "" : scLevelMax.value)) return true;

    if (document.getElementById("dvPrice").style.visibility != "hidden" && (scPriceMin.value.toLowerCase() != "min" || scPriceMax.value.toLowerCase() != "max")) {
        if (lastPriceMin != (lastPriceMin = (scPriceMin.value.toLowerCase() == "min") ? "" : scPriceMin.value)) return true;
        if (lastPriceMax != (lastPriceMax = (scPriceMax.value.toLowerCase() == "max") ? "" : scPriceMax.value)) return true;
    }
    else 
    {
        if (lastPriceMonthlyMin != ((scMonthlyPriceMin.value.toLowerCase() == "min") ? "" : scMonthlyPriceMin.value)) return true;
        if (lastPriceMonthlyMax != ((scMonthlyPriceMax.value.toLowerCase() == "max") ? "" : scMonthlyPriceMax.value)) return true;
        if (lastInterestRate != ((scInterestRate.value.toLowerCase() == "rate") ? "" : scInterestRate.value)) return true;
        if (lastDownPayment != ((scDownPayment.value.toLowerCase() == "deposit") ? "" : scDownPayment.value)) return true;
        if(lastYearOfLoan != ((scYearofloan.value.toLowerCase() == "term") ? "" : scYearofloan.value)) return true;
     }
     
     //check value for both the tabs
     var tempSearchOption = document.getElementById("ctl00_ContentPlaceHolder1_panHomeSearch").getElementsByTagName('input');
     for (var i = 0; i < tempSearchOption.length; i++) 
     {
        if(lastHomeSearch[i] != tempSearchOption[i].checked) return true;
     }
     var tempSearchOption = document.getElementById("ctl00_ContentPlaceHolder1_panSearchOption").getElementsByTagName('input');
     for (var i = 0; i < tempSearchOption.length; i++) 
     {
        if(lastCommunitySearch[i] != tempSearchOption[i].checked) return true;
     }
     return false;
 }

 function CompareAndAnonyousSave() 
 {
     if (compareWithLastSavedValue()) 
     {
         saveLastSearchValues();
         anonymousSave();
     }
 }

 function bestFitAndZoom() 
 {
     var marginRatio = 0.1;
     var swLL = bound.getSouthWest();
     var neLL = bound.getNorthEast();

     var minLat = Math.min(2 * bound.getCenter().lat() - neLL.lat(), swLL.lat());
     var maxLat = Math.max(2 * bound.getCenter().lat() - swLL.lat(), neLL.lat());
     var minLng = Math.min(2 * bound.getCenter().lng() - neLL.lng(), swLL.lng());
     var maxLng = Math.max(2 * bound.getCenter().lng() - swLL.lng(), neLL.lng());

     var minLatLng = new GLatLng(minLat - marginRatio, minLng - marginRatio);
     var maxLatLng = new GLatLng(maxLat + marginRatio, maxLng + marginRatio);
     bound.extend(maxLatLng);
     bound.extend(minLatLng);

     document.getElementById("divProgress").style.display = "none";
     map.setZoom(map.getBoundsZoomLevel(bound));
     map.setCenter(bound.getCenter());

     var bubbleCount = 0;
     for (var i = 0; i < batch.length; i++) {
         if (batch[i].N) bubbleCount++;
     }
     if (bubbleCount == 1) { map.setZoom(15); }
 }

 function xss_ajax(url) {
     //debugger;
        var script_id = null;
        var script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        var date = new Date();
        try {
            script.setAttribute('src', url + "&date=" + date);
            script.setAttribute('id', 'script_id');
        }
        catch (ex) { }
        script_id = document.getElementById('script_id');
        var browser = navigator.appName;
        
        if (script_id) {
            document.getElementsByTagName('head')[0].removeChild(script_id);
        }
        // Insert <script> into DOM
        if (browser != "Microsoft Internet Explorer") 
        document.getElementsByTagName('head')[0].appendChild(script);
}
