﻿/* SLIDER INITIALIZATION -START */

function pageLoad() {
    initSearchType(SearchType, true);
    
    if (ActionType >= -1 && ActionType != 2) {
        initializeMap();
    }

   // alert(actionQS);
   // if (ActionType != -1) {
    switch (actionQS.toUpperCase()) {
        case "MY-SEARCH-RESULTS":
           // document.getElementById('Footer').style.display = '';
            Search(ActionType,true);
            break;
        case "MY-LISTING-DETAIL":
            // document.getElementById('Footer').style.display = '';
            GetListingDetail(listingIdQS, MLSIdQS);
            showTab('Detail');
            break;
        case "MY-LISTINGS":
            // document.getElementById('Footer').style.display = '';
            // GetListingByListingID(multiple_ListingId);
            //showTab('Detail');
            break;
        case "IDX-ALERTS":
            document.getElementById("ListingResult").innerHTML = "Enter user information, search criteria and click find listings button.";
            openEmailAlerts();
            break;
        default:
            document.getElementById("ListingResult").innerHTML = DefaultMessage;
            document.getElementById('Footer').style.display = '';
            break;
        
    }
    ActionType = 2;
//    } else {
//        document.getElementById("ListingResult").innerHTML = DefaultMessage;
//    }

    document.getElementById("hiAgentId").value = AgentId;
    document.getElementById("hiUId").value = UId;
    
       
}

function getAID() {
    var url = location.href;
    if (url.toLowerCase().indexOf('aid') < 0)
        return '';
    else {
        var indexAID = url.toLowerCase().indexOf('aid');
        var indexAnd = url.indexOf('&');
        if (indexAnd < 0)
            indexAnd = url.indexOf('#');
        var result = url.substring(indexAID, indexAnd);
    }
    return result;
}
function setSliderValue(sender, agrs) {
    document.getElementById('ValueSliderSF1').innerHTML = $('#tbSliderSF1').attr('value') + ' sqft';
    document.getElementById('ValueSliderSF2').innerHTML = $('#tbSliderSF2').attr('value') + ' sqft';
}

function setSliderValueBed(sender, agrs) {
    document.getElementById('ValueSliderBed').innerHTML = $('#tbSliderBed').attr('value') + '+';
}

function setSliderValueBath(sender, agrs) {
    document.getElementById('ValueSliderBath').innerHTML = $('#tbSliderBath').attr('value') + '+';
}

function setSliderValuePR(sender, agrs) {

    document.getElementById('ValueSliderPR1').innerHTML = '$' + addCommas($('#tbSliderPR1').attr('value'));
    document.getElementById('ValueSliderPR2').innerHTML = '$' + addCommas($('#tbSliderPR2').attr('value'));
        
 }

function setSliderValueFloor(sender, agrs) {
    document.getElementById('ValueSliderFloor1').innerHTML = $('#tbSliderFloor1').attr('value') + '+';
    document.getElementById('ValueSliderFloor2').innerHTML = $('#tbSliderFloor2').attr('value') + '+';
}

function setSliderValueLotSize(sender, agrs) {
    document.getElementById('ValueSliderLotSize1').innerHTML = $('#tbSliderLotSize1').attr('value') + '+';
    document.getElementById('ValueSliderLotSize2').innerHTML = $('#tbSliderLotSize2').attr('value') + '+';
}

/* SLIDER INITIALIZATION -END */
function ResizeImage(elImage, MaxWidth, MaxHeight) {
    var newImg = new Image();
    var resImg = elImage;
    newImg.src = resImg.src;		
    if (newImg.height > MaxHeight && MaxHeight != 0) {
        resImg.height = MaxHeight;
    }
    if (newImg.width > MaxWidth && MaxWidth != 0) {
        resImg.width = MaxWidth;
    } 
}

function ViewImage(elImage) {
    var elLargeImage = document.getElementById("imgLargePhoto");
    elLargeImage.src = elImage.src;
}

function popupImageWin(img,target) {
  var w = window.open('',target,'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=700,height=540,left = 390,top = 284');
  if (w) { // handle popup blockers
    w.document.write('<body margin="0" topmargin="30"><div align="center" valign="middle" width="100%" height="100%"><img src="'+img+'"></div>')
    w.document.close();
    return false; // cancel the link
  }
  return true; // allow the link
}

function extendTabDIV() {
    var obj = document.getElementById('TabDIVToTal');
   // var quicksearchobj = document.getElementById('QuickSearchDIV');
    if (obj.style.width <= "80%") {
        extendDIV();
        
    }
    else {
        collapseDIV();
    }
}

function extendTabDIV2(extend) {
   // var obj = document.getElementById('TabDIVToTal');
   // var quicksearchobj = document.getElementById('QuickSearchDIV');
    if (extend == true) {
        extendDIV();
    }
    else {
        collapseDIV();
    }
}

function extendDIV() {

    $('#spanExpand').attr('innerHTML', 'SHOW CONTROLS');
    $('#pnlControl').css('display', 'none');
    $('#pnlControl').hide(10, function() {
        $('#TabDIVToTal').animate({
            width: '99%'
        }, 'slow');
    });
    $('#imageExpand').css('background-image', 'url(images/expand.jpg)');
}

function collapseDIV() {
    $('#spanExpand').attr('innerHTML', 'HIDE CONTROLS');
    $('#TabDIVToTal').animate({
        width: '67%'
    }, 'slow', function() { $('#pnlControl').show(); });
    $('#imageExpand').css('background-image', 'url(images/collapse.jpg)');
}

function checkEmpty() {
    if ($.trim($('#tbSearchText').attr('value')) == "") {
        $('#tbSearchText').attr('value', $('#lblActiveValue').attr('innerHTML'));
        $('#tbSearchText').css('color', 'gray');
    }
}

function setDefaultStyle() {
    $('#tbSearchText').css('color', 'black');
    if ($.trim($('#tbSearchText').attr('value')) == $('#lblActiveValue').attr('innerHTML')) {
        $('#tbSearchText').attr('value', '');
    }
};

var callbackTest = {
    displayTextInfo: function(opts) {
        if (!("currentPage" in opts)) { ; return; }

        var p = document.createElement('p'),
                   t = document.getElementById('theTable-fdtablePaginaterWrapTop');
        // b = document.getElementById('theTable-fdtablePaginaterWrapBottom');
        p.className = "paginationText";
        // alert('here');
        //document.getElementById("divPaginationText").innerHTML = "Showing page " + opts.currentPage + " of " + Math.ceil(opts.totalRows / opts.rowsPerPage) + " - " + Math.floor(opts.totalRows / 2) + " Listings";
        //document.getElementById("divPaginationText").style.diplay = '';
        p.appendChild(document.createTextNode("Showing page " + opts.currentPage + " of " + Math.ceil(opts.totalRows / opts.rowsPerPage) + " - " + Math.floor(opts.totalRows / 2) + " Listings"));

        //alert('here');
        var d = document.getElementById('pagination');
        if (d.childNodes.length > 0) {
            d.removeChild(p);
        }
        if (t == undefined) {
            d.appendChild(p.cloneNode(true));
        } else {
            t.insertBefore(p.cloneNode(true), t.firstChild);
        }

        //b.appendChild(p);
    }
};

function showProgressBar() {
    var obj = document.getElementById("Loading");
//    if (obj.style.display == "none") {
    obj.style.display = "block";
    //}
}

function hideProgressBar() {
    var obj = document.getElementById("Loading");
    //if (obj.style.display == "block") {
        obj.style.display = "none";
    //}
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

$(function() {
    $('a.tooltip').click(function() {
        return false;
    });

});


function initSearchType(sSearchType, bPageload) {

    document.getElementById('lbCity').className = 'normalLink';
    document.getElementById('lbZip').className = 'normalLink';
    document.getElementById('lbAddress').className = 'normalLink';
    document.getElementById('lbMLS').className = 'normalLink';
    document.getElementById('lblActive').innerHTML = sSearchType.toUpperCase();
    document.getElementById('btnSaveSearch').style.display = 'none';
    document.getElementById('HideShowControl').style.display = '';
    
    var PreviousActive = document.getElementById('lblPreviousActive').innerHTML;
    var SearchText = document.getElementById('tbSearchText').value.trim();
    
    SearchType = sSearchType.toUpperCase();

    switch (sSearchType.toUpperCase()) {
        case "CITY":
            if (bPageload == false)
                document.getElementById('tbSearchText').value = formatCity;
            else {
                if (SearchText == "")
                    document.getElementById('tbSearchText').value = formatCity;
                
            }
            document.getElementById('lbCity').className = 'activeLink';
            document.getElementById('btnSaveSearch').style.display = '';
            extendTabDIV2(false);

            break;
        case "ZIP":
            if (bPageload == false)
                document.getElementById('tbSearchText').value = formatZip;
            else {
                if (SearchText == "")
                    document.getElementById('tbSearchText').value = formatZip;

            }
            document.getElementById('lbZip').className = 'activeLink';
            document.getElementById('btnSaveSearch').style.display = '';
            extendTabDIV2(false);
            
            break;
        case "ADDRESS":
            if (bPageload == false)
                document.getElementById('tbSearchText').value = formatAddress;
            else {
                if (SearchText == "")
                    document.getElementById('tbSearchText').value = formatAddress;

            }
            document.getElementById('lbAddress').className = 'activeLink';
            extendTabDIV2(false);
            break;
        case "MLS":
            if (bPageload == false)
                document.getElementById('tbSearchText').value = formatMLS;
            else {
                if (SearchText == "")
                    document.getElementById('tbSearchText').value = formatMLS;

            } 
            document.getElementById('lbMLS').className = 'activeLink';
            document.getElementById('HideShowControl').style.display = 'none';
            extendTabDIV2(true);

            break;
        default:
            if (bPageload == false)
                document.getElementById('tbSearchText').value = formatCity;
            else {
                if (SearchText == "")
                    document.getElementById('tbSearchText').value = formatCity;

            }   
            SearchType = "CITY";
            document.getElementById('lblActive').innerHTML = "City";
            document.getElementById('lbCity').className = 'activeLink';
            extendTabDIV2(false);

            break;
    }

    document.getElementById('lblPreviousActive').innerHTML = sSearchType.toUpperCase();
    document.getElementById('lblActiveValue').value = document.getElementById('tbSearchText').value;

}


function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}


