﻿var MlsBoard = new Array();
    MlsBoard[1] = 0; // FMLS
    MlsBoard[2] = 0; // GMLS
    MlsBoard[3] = 0; // Northeast Georgia Board of Realtors
    MlsBoard[4] = 0; // River Counties
    MlsBoard[5] = 0; // Highlands Cashiers Board of Realors
    MlsBoard[6] = 0; // Darien
    MlsBoard[7] = 0; // Savannah Board of Realors

var MlsBoardString = new Array();
	MlsBoardString[1] = "FMLS";
	MlsBoardString[2] = "GMLS";
	MlsBoardString[3] = "Northeast Georgia Board of Realtors";
	MlsBoardString[4] = "River Counties";
	MlsBoardString[5] = "Highlands Cashiers Board of Realtors";
	MlsBoardString[6] = "Darien";
	MlsBoardString[7] = "Savannah Board of Realtors";

var CurrentAgentKey;

$(document).ready(function(){
    
    $(".view-biz-card").hover(
        function() {
            $(".biz-card").hide();
            $(this).parent().next().show();
        },
        function() {
            //$(this).parent().next().hide();
        }
    );
    
    $(".close-biz-card").click(function() {
        $(".biz-card").hide();
    });
    
    // Count selected properties for Compare Properties
    var selectedItems = 0;
    if (document.cookie.length > 0) 
        selectedItems = document.cookie.split("chkcmp").length-1;
    
    $("#TotalCompareProperties").html(selectedItems);
    $("#TotalComparePropertiesBelow").html(selectedItems);

    for (var i=1; i<=7; i++)
    {
        if (MlsBoard[i]>0) $("#disclaimer_"+i).show(); 
    }
    RetrieveCompareCheckProperties();
})

function gotoPage(iPage){
    $("#ctl00_mainContent_AgoraInternalPage").val(iPage);
    __doPostBack('ctl00$mainContent$LinkButtonPaging','')
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}

function deleteCookie(c_name) {
    setCookie(c_name, "", -1);
}

function ClearCompare()
{
    if (document.cookie.length > 0) 
    {
        var ckistr = document.cookie.split("chkcmp");
        var ckistrlen=ckistr.length;
        var ind;
        var ckistrval="";
        for(var i=1;i<ckistrlen;i++) 
        {   ckistrval = ckistr[i];
            ind = ckistrval.indexOf("=true",0);
            deleteCookie("chkcmp"+ckistr[i].substring(0,ind),"",-1);
        }
    }
    deleteCookie('comp_props');
    deleteCookie('TotalCompareProperties');
    deleteCookie('TotalComparePropertiesBelow');
    $(".comp_props:checked").attr("checked", false);
    $("#TotalCompareProperties").html('0');
    $("#TotalComparePropertiesBelow").html('0');
    
    var a = getCookie('comp_props');
    var b = getCookie('TotalCompareProperties');
    var c = getCookie('TotalComparePropertiesBelow');
   // alert('comp_props: ' + a);
   // alert('TotalCompareProperties: ' + b);
   // alert('TotalComparePropertiesBelow: ' + c);
}

function AddCompareProperty(obj) {
    var prevChecked=0;
    
    if (document.cookie.length > 0) 
    {
        if(document.cookie.indexOf("chkcmp") != -1);
            prevChecked = document.cookie.split("chkcmp").length-1
    }

    var checked_items_total = prevChecked; //$(".comp_props:checked").length + 
    if ($(obj).attr("checked")) {
        if (checked_items_total == 4) {
            $(obj).attr('checked', false);
            alert('You have already selected the maximum of 4 properties');
        }
        else
        {
            setCookie('chkcmp'+$(obj).attr("value"),true,1);
            checked_items_total += 1;
         }
    }
    else
        {
            setCookie('chkcmp'+$(obj).attr("value"), "", -1);
            checked_items_total -= 1;
        }
    
    setCookie('comp_props',true,1);
    setCookie('TotalCompareProperties', checked_items_total, 1);
    setCookie('TotalComparePropertiesBelow', checked_items_total, 1);
    
    $("#TotalCompareProperties").html(checked_items_total);
    $("#TotalComparePropertiesBelow").html(checked_items_total);

    var a = getCookie('comp_props');
    var b = getCookie('TotalCompareProperties');
    var c = getCookie('TotalComparePropertiesBelow');
   // alert('comp_props: ' + a);
   // alert('TotalCompareProperties: ' + b);
   // alert('TotalComparePropertiesBelow: ' + c);
}

function CompareProperties()
{
    // Creating comparison object from ClassName
    var ctrl_comp_mls = $('.comp_props');
    var str_comp_mls = '';
    
    if (document.cookie.length > 0) 
    {
        var ckistr = document.cookie.split("chkcmp");
        var ckistrlen=ckistr.length;
        var ind;
        var ckistrval="";
        for(var i=1;i<ckistrlen;i++) 
        {   ckistrval = ckistr[i];
            ind = ckistrval.indexOf("=true",0);
            if(str_comp_mls != '') str_comp_mls += ',';
            str_comp_mls += ckistr[i].substring(0,ind);
        }
    }
//    for(i=0; i<ctrl_comp_mls.length; i++)
//    {
//        if(!ctrl_comp_mls[i].checked) continue;
//        
//        if(str_comp_mls != '') str_comp_mls += ',';
//        
//        str_comp_mls += ctrl_comp_mls[i].value;
//    }


    if(str_comp_mls.split(',').length < 2)
    {
        alert('Please select at least 2 properties for comparison');
        return;
    }
    if(str_comp_mls.split(',').length > 4)
    {
        alert('You can compare maximum 4 properties');        
        return;
    }
    //window.open ('PropertyCompare.aspx' + location.search + '&n_k=' + str_comp_mls, "HarryNorman");
    var CurrentURL = window.location.href;
    CurrentURL = ReplaceQueryString(CurrentURL, "n_k", str_comp_mls);
    window.open(CurrentURL.replace(/PropertyResults.aspx\?/i,'PropertyCompare.aspx?'));
    //location.replace('PropertyCompare.aspx' + location.search + '&0110_exc=' + str_comp_mls));
}

function RetrieveCompareCheckProperties()
{
    // Creating comparison object from ClassName
    var ctrl_comp_mls = $('.comp_props');
    var str_comp_mls = '';
    
    if (document.cookie.length > 0) {
        for(i=0; i<ctrl_comp_mls.length; i++)
        {
            c_name = 'chkcmp'+ctrl_comp_mls[i].value;
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
    
                if (c_end == -1) c_end = document.cookie.length;
    
                if (unescape(document.cookie.substring(c_start, c_end))=='true')
                    ctrl_comp_mls[i].checked = true;
                else
                    ctrl_comp_mls[i].checked = false;
            }            
        }
    }
}

function GoRefineSearch() {
    var tab = getQuerystring("SearchPageName", "PropertySearchChicago.aspx");
    var BackToResultString = window.location.href;
    BackToResultString = BackToResultString.replace("PropertyResults.aspx",tab);
   
    window.location = BackToResultString;
    
}

function ShowPropertyNote(itmNum)
{
    $("#divNote"+itmNum).show();
}

function OpenVirtualTour(URL)
{
    if (URL.indexOf("http")!=-1)
    {
        window.open(URL);
    }else window.open("Http://"+URL);
}


function OnChangeSelection(obj) {

        var Title = $(obj).attr("title");
        var TitleClass = Title.replace(/\s+/g, "");
        var IsPrice = $(obj).hasClass("price");
        var IsText = $(obj).hasClass("OnSelectionText") ? true : $(obj).hasClass("OnSelectionKeyUp");
        var Value = "";
        if (!IsText) {
            $(obj).children("option:selected").each(function() {
                if (Value != "") Value += ",";
                Value += unescape($(this).html());
            })
        } else {
            Value = unescape($(obj).val().toString());
        }

        if ($(obj).val() == null || $.trim($(obj).val().toString()) == "") {
            $("#MySearchOuter" + TitleClass).remove();
        } else if ($("#MySearch").html().indexOf("MySearch" + TitleClass) == -1) {
            if (IsPrice) {
                $("#MySearch").append("<div class='clearfix MySearchTitle' id='MySearchOuter" + TitleClass + "'><strong>" + Title + "</strong>: <span class='MySearchDetail MySearch" + TitleClass + "'>" + Value + "</span></div>");
            } else {
                $("#MySearch").append("<div class='clearfix MySearchTitle' id='MySearchOuter" + TitleClass + "'><strong>" + Title + "</strong>: <span class='MySearchDetail MySearch" + TitleClass + "'>" + Value + "</span></div>");
            }
        } else {
            if (IsPrice) {
                $(".MySearch" + TitleClass).html(Value);
            } else {
                $(".MySearch" + TitleClass).html(Value);
            }
        }
}

function OnChangeCheckbox(obj) {
    var Title = $(obj).attr("title");
    var TitleClass = Title.replace(/\s+/g, "");
    var IsYesNoField = $(obj).hasClass("YesNoField");


    $("input", $(obj)).bind("click",function() {
        var Value = $(this).siblings("label").html();
        Value = (IsYesNoField) ? "Yes" : Value;
        if ($(this).attr("checked")) {
            if ($("#MySearch").html().indexOf("MySearch" + TitleClass) == -1) {
                $("#MySearch").append("<div class='clearfix MySearchTitle' id='MySearchOuter" + TitleClass + "'><strong>" + Title + "</strong>: <span class='MySearchDetail MySearch" + TitleClass + "'>" + Value + "</span></div>");
            } else {
                $(".MySearch" + TitleClass).append(Value);
            }
        } else {
            var string_to_remove = new RegExp(Value + '<br>', 'i');
            $(".MySearch" + TitleClass).html($(".MySearch" + TitleClass).html().replace(string_to_remove, ""));
            
            // DOES NOT WORK IN IE8
            //$(".MySearch" + TitleClass).html($(".MySearch" + TitleClass).html().replace(Value + "<br>", ""));
        }
        if ($.trim($(".MySearch" + TitleClass).html()) == "") {
            $("#MySearchOuter" + TitleClass).remove();
        }
    })
}

function RestoreCheckbox(obj) {
    var Title = $(obj).attr("title");
    var TitleClass = Title.replace(/\s+/g, "");
    var IsYesNoField = $(obj).hasClass("YesNoField");

    $("input:checked", $(obj)).each(function() {
        var Value = $(this).siblings("label").html();
        Value = (IsYesNoField) ? "Yes" : Value;
        if ($(this).is(":checked")) {
            if ($("#MySearch").html().indexOf("MySearch" + TitleClass) == -1) {
                $("#MySearch").append("<div class='clearfix MySearchTitle' id='MySearchOuter" + TitleClass + "'><strong>" + Title + "</strong>: <span class='MySearchDetail MySearch" + TitleClass + "'>" + Value + "</span></div>");
            } else {
                $(".MySearch" + TitleClass).append(", " + Value);
            }
        } else {
            $(".MySearch" + TitleClass).html($(".MySearch" + TitleClass).html().replace(Value, ""));
        }
        if ($.trim($(".MySearch" + TitleClass).html()) == "") {
            $("#MySearchOuter" + TitleClass).remove();
        }
    })
}



$(document).ready(function() {
    // Attach the event to the elements
    $(".OnSelection, .OnSelectionText").change(function() {
        OnChangeSelection(this);
    })

    $(".OnSelectionKeyUp").bind("focus keyup", function() {
        OnChangeSelection(this);
    })

    $(".OnSelectionCheckbox").each(function() {
        OnChangeCheckbox(this);
    })

    // Loop thru the value
    $(".OnSelection:has(:selected)").each(function() {
        OnChangeSelection(this);
    })

    $(".OnSelectionKeyUp[value!='']").each(function() {
        OnChangeSelection(this);
    })

    $("input:checked").parents("table").each(function() {
        RestoreCheckbox(this);
    })
})
