﻿function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft += obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}
function setDropdownValue(obj, val) {
    if (obj) {
        for (i = 0; i < obj.length; i++) {
            if (obj.options[i].value == val)
                return obj.selectedIndex = i;
        }
    }
}
var DHTML = (document.getElementById || document.all || document.layers);
function getObj(name) {
    if (document.getElementById) {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
    }
    else if (document.all) {
        this.obj = document.all[name];
        this.style = document.all[name].style;
    }
    else if (document.layers) {
        this.obj = document.layers[name];
        this.style = document.layers[name];
    }
}
function toggle(div, flag) {
    if (!DHTML) return;
    var x = new getObj(div);
    x.style.display = (flag) ? 'none' : 'block'
    x.style.visibility = (flag) ? 'hidden' : 'visible';
}
function showPageCover() {
    var widCover = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth) + "px";
    var htCover = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) + "px";
    document.getElementById('pageCover').style.width = widCover;
    document.getElementById('pageCover').style.height = htCover;
    document.getElementById('pageCover').style.display = 'block';
    document.getElementById('pageCover').style.visibility = 'visible';
}
function hideDropDowns() {
    if (document.all) {
        var arrDD = document.documentElement.getElementsByTagName('select');
        for (i = 0; i < arrDD.length; i++) {
            arrDD[i].style.display = "none";
        }
    }
}
function showDropDowns() {
    if (document.all) {
        var arrDD = document.getElementsByTagName('select');
        for (i = 0; i < arrDD.length; i++) {
            arrDD[i].style.display = "block";
            //arrDD[i].style.visibility = "visible";
        }
    }
}
function GetWindowHeight() {
    var height = 0;
    if (typeof (window.innerHeight) == 'number') {
        height = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        height = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        height = document.body.clientHeight;
    }
    if (!height || height < 100) {
        height = 100;
    }
    return height;
}

function GetWindowWidth() {
    var width = 0;
    if (typeof (window.innerWidth) == 'number') {
        width = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        width = document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        width = document.body.clientWidth;
    } if (!width || width < 100) {
        width = 100;
    }
    return width;
}
// This populates the price combo box 
function PopulatePriceDropDown(priceOption, priceDropDown) {
    var priceOptionText = "";
    var priceOptionValue = "";
    var item = "";
    priceDropDown.innerHTML = "";
    var items = priceOption.split("~");
    for (i = 0; i < items.length; i++) {
        item = items[i].split("|");
        priceOptionText = item[1];
        priceOptionValue = item[0];

        if (priceOptionText != "") {
            optionItem1 = new Option(priceOptionText, priceOptionValue, false, false);
            priceDropDown.options[priceDropDown.length] = optionItem1;
        }
    }
}
function setFnaDivPosition() {
    fnaAnchorObj = document.getElementById('FnaAnchor');
    fnaSearchDivObj = document.getElementById('FnaSearchDiv');

    fnaSearchDivObj.style.position = 'absolute';
    fnaSearchDivObj.style.top = findPosY(fnaAnchorObj) + 'px';
    fnaSearchDivObj.style.left = 30 + 'px'; //findPosX(fnaAnchorObj) - 300 + 'px';
}

function SearchResults(obj) {
    this.MapDetalis = obj[0]!= "" ? obj[0] : null;
    this.ListingSummary = obj[1]!= "" ? obj[1] : null;
    this.ListingCount = obj[2]!= "" ? obj[2] : null;
    this.Layer = obj[3]!= "" ? obj[3] : null;
    this.ToggleFirstMlsNo= obj[4]!= ""  ? obj[4] : null;
    this.ToggleFirstMlsID= obj[5]!= ""  ? obj[5] : null;
    this.ResultType= obj[6] != "" ? obj[6] : null;
    if(typeof(obj[7]) != "undefined")
        this.ExceedLimit= obj[7] ;
    else
        this.ExceedLimit=''; 
    this.ShowResult = function() {
                var str = String(window.location);
                eval(this.MapDetalis);
                document.getElementById(g_ctrl_prefix+"ListingHeaderField").value = this.ListingSummary;
                document.getElementById("listingCountText").innerHTML="";
                document.getElementById("listingCount").innerHTML="";
                // Do some common stuff here like loading the map result and summary result.
                // even though they are empty, eval or assigning innerHTML is not going to give any error
                // assign the Listing Count
                
                // to handle the layer types
                switch (this.ResultType) {
                    case "StateSearch":
                        // show the appropriate layer
                        var searchText = document.getElementById(g_ctrl_prefix +'SearchTextBox');
                        document.getElementById(g_ctrl_prefix +'firstTimeMsgDiv').style.display = 'block';
                        document.getElementById(g_ctrl_prefix +'countMsg').innerHTML ="<br /><br /><br />Please narrow down your search to an address, city, zip code or Mls.";
                        searchText.value =  "";                   
                        GetSummary("");                  
                        onUpdated(); 
                        break;
                    case "Layer":
                        document.getElementById(g_ctrl_prefix +'firstTimeMsgDiv').style.display = 'block';
                        document.getElementById(g_ctrl_prefix +'countMsg').innerHTML =this.Layer+" ";
                        if (this.ListingCount != null )
                        {
                        document.getElementById("listingCountText").innerHTML="Listing";
                        document.getElementById("listingCount").innerHTML=this.ListingCount+" ";
                        }
                        GetSummary("");
                        onUpdated(); 
                        break;
                    default:
                        if (this.ExceedLimit.search("http")!=-1)
                            {window.location=this.ExceedLimit;}
                        else if (this.ExceedLimit>0)
                            {document.getElementById(g_ctrl_prefix +'ErrorMessage').innerHTML="<b>Top "+this.ExceedLimit+ " Listings are shown.</b>";}
                        document.getElementById(g_ctrl_prefix +'firstTimeMsgDiv').style.display = 'none';
                        if (this.ListingCount <= 1 )
                        {
                            if (str.indexOf("Sold") != -1)
                            document.getElementById("listingCountText").innerHTML="Sold Listing";
                            else 
                            document.getElementById("listingCountText").innerHTML="My Listing";
                        }
                        else if(this.ListingCount > 1)
                        {
                            if (str.indexOf("Sold") != -1)
                            document.getElementById("listingCountText").innerHTML="Sold Listings";
                            else 
                            document.getElementById("listingCountText").innerHTML="My Listings";
                        }
                        document.getElementById("listingCount").innerHTML=this.ListingCount+" ";
                        GetSummary(parseInt(this.ListingCount));
                        toggleBodyDiv(this.ToggleFirstMlsNo,this.ToggleFirstMlsID,1,1);
                        onUpdated();   
                        // show no layer
                        
                        break;
                }
            };
}

function CalculateMultiSelectionValue() {
    var totalValue = 0;
           if (document.getElementById("SingleFamily").checked == true)
                totalValue = totalValue | parseInt(document.getElementById("SingleFamily").name);
           if (document.getElementById("TownHouse").checked == true)
                totalValue = totalValue | parseInt(document.getElementById("TownHouse").name);
           if (document.getElementById("MultiFamily").checked == true)
                totalValue = totalValue | parseInt(document.getElementById("MultiFamily").name);
           if (document.getElementById("Commercial").checked == true)
                totalValue = totalValue | parseInt(document.getElementById("Commercial").name);
           if (document.getElementById("Lots").checked == true)
                totalValue = totalValue | parseInt(document.getElementById("Lots").name);
           if (document.getElementById("Other").checked == true)
                totalValue = totalValue | parseInt(document.getElementById("Other").name);
                
           if (totalValue==0)totalValue=63;
    return totalValue;
}
function onUpdating() {
    toggle('mapCover', 0);
    var updateProgressDiv = $get('updateProgressDiv');
    updateProgressDiv.style.display = '';
    var UpdatePanel = $get('myMap');
    // get the bounds of both the map and the progress div
    var updatePanelBounds = Sys.UI.DomElement.getBounds(UpdatePanel);
    var updateProgressDivBounds = Sys.UI.DomElement.getBounds(updateProgressDiv);
    //	do the math to figure out where to position the element (the center of the map)
    var x = updatePanelBounds.x + Math.round(updatePanelBounds.width / 2) - Math.round(updateProgressDivBounds.width / 2);
    var y = updatePanelBounds.y + 250 - Math.round(updateProgressDivBounds.height / 2);

    //	set the progress element to this position
    Sys.UI.DomElement.setLocation(updateProgressDiv, x, y);
}

function onUpdated() {
    var updateProgressDiv = $get('updateProgressDiv');
    updateProgressDiv.style.display = 'none';
    toggle('mapCover', 1);
}

function setPositionElement(value,elementId)
{
    var scrollY=0;    
    if(window.pageYOffset)
    {
        scrollY=window.pageYOffset;
    }
    if(document.all)
    {
        scrollY= document.documentElement.scrollTop;
    }
    var adjustedY=scrollY+value+'px';    
    document.getElementById(elementId).style.top = adjustedY;
}
function hideAncrButtons() {
    document.getElementById('FnaAnchor').style.visibility = 'hidden';
    document.getElementById(g_ctrl_prefix + 'SearchBtn').style.visibility = 'hidden';
}
function showAncrButtons() {
    document.getElementById('FnaAnchor').style.visibility = 'visible';
    document.getElementById(g_ctrl_prefix + 'SearchBtn').style.visibility = 'visible';
}

