﻿
var txtControl = "ctl00_cphMain_txtLocation1";
var txtchange = "ctl00_cphMain_txtLocation1";
var pageIndex = 0;


function loadFeed(feed){
    __doPostBack('bSearch', feed);
}


function MaxSelected(){
    document.getElementById("ctl00_cphMain_lblInfo").innerHTML = "You cannot select more than eight cities and areas<br />";
}

function HideSelect(){
    document.getElementById("ctl00_cphMain_divSelect").style.display = "none";
    document.getElementById("ctl00_cphMain_divSelect").style.visibility = "hidden";
}

function ShowSelect(elem){
    var xPos = findPosX(elem);
    var yPos = findPosY(elem);

    xPos = xPos + 20;
    yPos = yPos + 19;
        
    document.getElementById("ctl00_cphMain_divSelect").style.left = xPos + 'px'
    document.getElementById("ctl00_cphMain_divSelect").style.top = yPos + 'px';
    document.getElementById("ctl00_cphMain_divSelect").style.display = "Block";
    document.getElementById("ctl00_cphMain_divSelect").style.visibility = "visible";
    document.getElementById("ctl00_cphMain_divSelect").style.position = "absolute";
}


function setFocus(id){
    alert(id);
    var elm = document.getElementById(id);
    elm.focus();
}

function chooseLocation(){
    
    var sHtml = "";
    
    AdvancedSearch.AddCity(callback_AddCity);
    
    function callback_AddCity(result){
    
        sHtml = result.toString();
        document.getElementById("ctl00_cphMain_divLocations").innerHTML = sHtml;
        
        pageIndex = 0;
        document.getElementById("divPrev").style.display = "none";
        document.getElementById("divPrev").style.visibility = "hidden";
        
        if (document.getElementById("txtSearchCrit").value == null || document.getElementById("txtSearchCrit").value == ""){
            document.getElementById("divNext").style.display = "none";
            document.getElementById("divNext").style.visibility = "hidden";
        }
        
        SearchCrit=document.getElementById("txtSearchCrit").value;

        if (SearchCrit == null || SearchCrit == ""){
            document.getElementById("txtSearchCrit").value = getParamsLoc();
            SearchCrit=document.getElementById("txtSearchCrit").value;
        }
        
        splitstring = SearchCrit.split(",");
        //add city checkboxes back to checked
        if (splitstring != null || splitstring != "" || splitstring != "undefined"){
            for (i=0; i < splitstring.length; i++){
                if (document.getElementById("chk" + splitstring[i]) != null){
                    document.getElementById("chk" + splitstring[i]).checked = true;
                }
            }
            
                document.getElementById("divNext").style.display = "Block";
                document.getElementById("divNext").style.visibility = "visible";
        }
        else {
            
        }   
    }
}

function Prev(){

    if (pageIndex == 0){
        return;
    }
    
    if (pageIndex == 1){
        chooseLocation();
    }
    
    if (pageIndex == 2){
        FindNZ();
    }
    
    if (pageIndex == 3){
    
    }
}


function Next(){
        
    if (pageIndex == 0){
        FindNZ();
        return;
    }
    
    if (pageIndex == 1){
        GetAllLocationsForSearch();
        return;
    }
    
    if (pageIndex == 2){
        return;
    }
    
    if (pageIndex == 3){
        return;
    }

}

function FindNZ(){
    
    pageIndex = 1;
    document.getElementById("divPrev").style.display = "Block";
    document.getElementById("divPrev").style.visibility = "visible";
    document.getElementById("divNext").style.display = "none";
    document.getElementById("divNext").style.visibility = "hidden";
    
    AdvancedSearch.GetAllCitys(callback_GetAllCitys);
    
    
    function callback_GetAllCitys(result){
    
        var citys = result.documentElement.getElementsByTagName("Table");
        GetCheckedCitys(citys);
    
    }
    
    //1. get XML with City's
    //2. Compare to Checkboxes
    //3

}

function GetCheckedCitys(data){
    
    var CheckedCities="";
    
    for (i=0; i<data.length; i++) {
    
        var sCity = data[i].childNodes[0].firstChild.nodeValue.toString();
        
        chk = document.getElementById("chk" + sCity );
        
        if (chk != null){           
            if (chk.checked == true){
                
                CheckedCities += sCity + ",";
            }
        }
    }
    
    if (CheckedCities == ""){ document.getElementById("divError").innerHTML = "Please Check a City"; window.setTimeout("chooseLocation();","300");  return; }
    else { LoadDivByCheckedCities(CheckedCities); }

}

function LoadDivByCheckedCities(chk){

    AdvancedSearch.LoadDivByCheckedCities(chk.toString(), callback_LoadDivByCheckedCities);
    function callback_LoadDivByCheckedCities(result){
        sHtml = result.toString();
        
        if (sHtml == "" || sHtml == null){
            return;
        }
        else{
            document.getElementById("ctl00_cphMain_divLocations").innerHTML = sHtml;
        }
    }
}


//parameter idALL: THE 'ALL' CHECKBOX
//parameter id: THE CHECKBOX UNCHECKED
function UnCheckAll(idAll,id,Type){
        alert("idAll: " + idAll + "     id: " + id + "   Type: " + Type); 
        chk = document.getElementById("chk" + Type + "ALL" + idAll);
        chk.checked = false;
        chkBox = document.getElementById("chk" + Type + id);
        
        if (chkBox.checked == false){
            //remove crit
            if (document.getElementById("txtSearchCrit").value.indexOf(",") == -1){
                sFinal = removeCrit(document.getElementById("txtSearchCrit").value,id);
            }
            else {
                sFinal = removeCrit(document.getElementById("txtSearchCrit").value, "," + id);
            }
             document.getElementById("txtSearchCrit").value = sFinal;
            
        }
        else {
            //add crit
            //remove city       
            sFinal = removeCrit(document.getElementById("txtSearchCrit").value, idAll).replace(",,", ",");
            //now add zip
            document.getElementById("txtSearchCrit").value = sFinal + "," + id;
        }
}


function UnCheck(elem,type){
    if (elem.checked == true){
        if (type == "HUD"){
            document.getElementById("ctl00_cphMain_chkHUD").checked = true; 
            document.getElementById("ctl00_cphMain_chkLender").checked = false; 
        }
        
        if (type == "LENDER"){
            document.getElementById("ctl00_cphMain_chkHUD").checked = false; 
            document.getElementById("ctl00_cphMain_chkLender").checked = true; 
        }
    }
    if (elem.checked == false){
    
    }
}


function GetAllLocationsForSearch(){

}

function Checked(type,crit){

    var txt = document.getElementById("txtSearchCrit").value;
    i = txt.indexOf(crit);
    
    //add criteria
    if (i == -1){
        if (document.getElementById("txtSearchCrit").value == null || document.getElementById("txtSearchCrit").value == ""){
            document.getElementById("txtSearchCrit").value += crit;
        }
        else {
            document.getElementById("txtSearchCrit").value += "," + crit;
        }
        
    }
    else {//remove criteria
        if (txt.indexOf(",") == -1){
            var finalS = removeCrit(txt,crit);
        }
        else {
            var finalS = removeCrit(txt,"," + crit);
        }
        
        document.getElementById("txtSearchCrit").value = finalS;
    }
    
    if (document.getElementById("txtSearchCrit").value == null || document.getElementById("txtSearchCrit").value == ""){
        document.getElementById("divNext").style.display = "none";
        document.getElementById("divNext").style.visibility = "hidden";
    }
    else {
        document.getElementById("divNext").style.display = "Block";
        document.getElementById("divNext").style.visibility = "visible";
    }
}

function removeCrit(string,tstring) {
    return string.replace(tstring, "");
}

function bSearchClick(){
    __doPostBack('bSearch', "");
}

function bSaveSearchClick(){
    __doPostBack('BSAVESEARCH', "");
}

function bResetClick(){
    removeAllOptions(document.getElementById("ctl00_cphMain_ddSubArea"))
    document.getElementById("ctl00_cphMain_ddLocation").selectedIndex = -1;
    document.getElementById("ctl00_cphMain_ddMinPrice").selectedIndex = 0;
    document.getElementById("ctl00_cphMain_ddMaxPrice").selectedIndex = document.getElementById("ctl00_cphMain_ddMaxPrice").length-1;
    
    document.getElementById("ctl00_cphMain_comboBeds").selectedIndex = 0;
    document.getElementById("ctl00_cphMain_comboBaths").selectedIndex = 0;
    document.getElementById("ctl00_cphMain_comboSqFt").selectedIndex = 0;
    
    document.getElementById("ctl00_cphMain_ddYearBuiltMin").selectedIndex = 0;
    document.getElementById("ctl00_cphMain_ddYearBuiltMax").selectedIndex = 0;
    document.getElementById("ctl00_cphMain_ddDays").selectedIndex = 0;
    
    document.getElementById("ctl00_cphMain_chkRes").checked = true;
    document.getElementById("ctl00_cphMain_chkCondo").checked = true;
    
    document.getElementById("ctl00_cphMain_chkAC").checked = false;
    document.getElementById("ctl00_cphMain_chkDishwasher").checked = false;
    document.getElementById("ctl00_cphMain_chkRefrigerator").checked = false;
    document.getElementById("ctl00_cphMain_chkWahser").checked = false;
    document.getElementById("ctl00_cphMain_chkLender").checked = false;
    document.getElementById("ctl00_cphMain_chkDeck").checked = false;
    document.getElementById("ctl00_cphMain_chkWoodFloors").checked = false;
    document.getElementById("ctl00_cphMain_chkMountainViews").checked = false;
    document.getElementById("ctl00_cphMain_chkRemodel").checked = false;
    document.getElementById("ctl00_cphMain_chkDryer").checked = false;
    document.getElementById("ctl00_cphMain_chkFireplace").checked = false;
    document.getElementById("ctl00_cphMain_chkStove").checked = false;
    document.getElementById("ctl00_cphMain_chkHUD").checked = false;
    document.getElementById("ctl00_cphMain_chkGolf").checked = false;
    document.getElementById("ctl00_cphMain_chkPool").checked = false;
    document.getElementById("ctl00_cphMain_chkCityViews").checked = false;
    document.getElementById("ctl00_cphMain_chkQuick").checked = false;
    document.getElementById("ctl00_cphMain_chkGarage").checked = false;
    
    //Phrases
    document.getElementById("ctl00_cphMain_chkBank").checked = false;
    document.getElementById("ctl00_cphMain_chkLender").checked = false;
    document.getElementById("ctl00_cphMain_chkHUD").checked = false;
    document.getElementById("ctl00_cphMain_chkForeclosure").checked = false;
    document.getElementById("ctl00_cphMain_chkMakeOffer").checked = false;
    document.getElementById("ctl00_cphMain_chkMotivatedSeller").checked = false;
    document.getElementById("ctl00_cphMain_chkPriceReduced").checked = false;
    document.getElementById("ctl00_cphMain_chkNeedToSell").checked = false;
    document.getElementById("ctl00_cphMain_chkAuction").checked = false;
    document.getElementById("ctl00_cphMain_chkBringOffer").checked = false;
    document.getElementById("ctl00_cphMain_chkCorpRelo").checked = false;
    document.getElementById("ctl00_cphMain_chkSellerSaysSell").checked = false;
    document.getElementById("ctl00_cphMain_chkPricereduction").checked = false;
    document.getElementById("ctl00_cphMain_chkMustSell").checked = false;
    document.getElementById("ctl00_cphMain_chkSoldAsIs").checked = false;
    document.getElementById("ctl00_cphMain_chkShortSale").checked = false;
    document.getElementById("ctl00_cphMain_chkVacant").checked = false;

    
}

function Help(elem,type){
//    if (type == "locations"){
//        document.getElementById("imgLHelp").style.cursor = 'hand';
//    }
}

function getParamsLoc(){
var qsParm = new Array();
var query = window.location.search.substring(1);

    //No query
    if (query == ""){ return " "; }

    var parms = query.split('&');
    
    if (parms == "") { return; }
    
    for (var i=0; i<parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
        
            var key = parms[i].substring(0,pos);
            var val = parms[i].substring(pos+1);            
            qsParm[key] = val;
        }
    }
    return qsParm["City"];
}

//sets focus on textbox and selects all text
function OnFocus(elem){
    elem.select();
}

function ToggleBoxes(id){
    
    imgid = id.replace("divE", "img");

    if (document.getElementById(id).style.display == "none" || document.getElementById(id).style.visibility == "hidden"){
        document.getElementById(id).style.display = "Block";
        document.getElementById(id).style.visibility = "visible";
        document.getElementById(imgid).src = "../../App_Themes/Default/Images/minus.gif";
    }
    else {
        
        document.getElementById(id).style.display = "none";
        document.getElementById(id).style.visibility = "hidden";
        document.getElementById(imgid).src = "../../App_Themes/Default/Images/plus.gif";
    }
}

function ShowAdvancedSearch(event, elem, c, p){
    
    txtControl = c;
    txtchange = elem;
    HideAdvancedSearch();
    
    var xPos = findPosX(elem);
    var yPos = findPosY(elem);

    xPos = xPos + 20;
    yPos = yPos + 19;
        
    document.getElementById(p).style.left = xPos + 'px'
    document.getElementById(p).style.top = yPos + 'px';
    document.getElementById(p).style.display = "Block";
    document.getElementById(p).style.visibility = "visible";
    document.getElementById(p).style.position = "absolute";
}

function HideAdvancedSearch(){
//    document.getElementById("ctl00_cphMain_divNeighborhood").style.display = "none";
//    document.getElementById("ctl00_cphMain_divNeighborhood").style.visibility = "hidden";
//    
//    document.getElementById("ctl00_cphMain_divZipCode").style.display = "none";
//    document.getElementById("ctl00_cphMain_divZipCode").style.visibility = "hidden";
//    
//    document.getElementById("ctl00_cphMain_divMlsArea").style.display = "none";
//    document.getElementById("ctl00_cphMain_divMlsArea").style.visibility = "hidden";
//    
//    document.getElementById("ctl00_cphMain_divCity").style.display = "none";
//    document.getElementById("ctl00_cphMain_divCity").style.visibility = "hidden";
}

function WebAsyncRefreshPanel1_InitializePanel(oPanel){
    var pi = oPanel.getProgressIndicator();
    pi.setTemplate("<div style=\"font-weight: bold; background: white; border: solid 1px #006699; padding: 3px 3px 3px 3px; \">Loading...</div>");
    pi.setLocation({x:70,y:212});
}
   
function Change(changeto){
    if (changeto == "CITY"){
        ShowAdvancedSearch(null, txtchange, txtControl, "ctl00_cphMain_divCity");
    }
    if (changeto == "NEIGHBORHOOD"){
        ShowAdvancedSearch(null, txtchange, txtControl, "ctl00_cphMain_divNeighborhood");
    }
    if (changeto == "AREA"){
        ShowAdvancedSearch(null, txtchange, txtControl, "ctl00_cphMain_divMlsArea");
    }
    if (changeto == "ZIPCODE"){
        ShowAdvancedSearch(null, txtchange, txtControl, "ctl00_cphMain_divZipCode");
    }
}
   
   // find the x position of an object
function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// find the y position of an object
function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}



function ShowFOverLib(type,elem){
    
    
    var xPos = findPosX(elem);
    var yPos = findPosY(elem);

    xPos = xPos + 20;
    yPos = yPos + 19;
        
    document.getElementById("OverDivAll").style.left = xPos + 'px'
    document.getElementById("OverDivAll").style.top = yPos + 'px';
    document.getElementById("OverDivAll").style.display = "Block";
    
    
    if (type == "LENDER"){
        document.getElementById("OverDivAll").style.visibility = "visible";
        document.getElementById("OverDivAll").style.position = "absolute";
         document.getElementById("OverDivAll").innerHTML = "Lender is.... text here";
    }
    
    if (type == "HUD"){
        document.getElementById("AddToWatchlistDiv").style.visibility = "visible";
        document.getElementById("AddToWatchlistDiv").style.position = "absolute";
        document.getElementById("OverDivAll").innerHTML = "HUD is.... text here";
    }

}


function HideFOverLib(){
    document.getElementById("overDivAll").style.display = "none";
    document.getElementById("overDivAll").style.visibility = "hidden";
}


// new script code to support overlib
function ShowOverLib( picUrl ) {
    var overLibContent = "<div style='padding:5px 4px 4px 4px;'>" + 
        "<div style='height:30px;'><div style='float:left;'><img src='../../App_Themes/Default/Images/rollover_logo.gif' /></div>" +
        "<div style='float:right;font-weight:bolder;color:#006699; padding-top:7px;padding-right:5px;'>1.866.697.3564</div></div>" +
        "<img src='" + picUrl + "' /></div>" +
        "<div align='center' style='padding-bottom:5px;color:#006699;font-weight:bolder;'>Click to see detailed property info<br /> and more pics</div>";
    return overlib( overLibContent, VAUTO, FGCOLOR, '#FFFFFF' );
}

function Select(max){

}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

function HideWatchListDiv() {
    // alert(propID);
    document.getElementById("AddToWatchlistDiv").style.display = "none";
    document.getElementById("AddToWatchlistDiv").style.visibility = "hidden";
}

function ShowWatchListDiv(event, elem, p) {
    propID = p;
    var xPos = findPosX(elem);
    var yPos = findPosY(elem);
    
    // alert(propID);
    //alert("X :" + e.clientX);
    //alert("Y :" + e.clientY);
    document.getElementById("AddToWatchlistDiv").style.left = xPos + 'px'
    document.getElementById("AddToWatchlistDiv").style.top = yPos + 'px';
    document.getElementById("AddToWatchlistDiv").style.display = "Block";
    document.getElementById("AddToWatchlistDiv").style.visibility = "visible";
    document.getElementById("AddToWatchlistDiv").style.position = "absolute";
    
    // document.getElementById("WatchListContent").innerHTML = "Saving Property To Watchlist...";
    
    
    // now call web service to save the property to the watchllist
    // check to see if the user is logged in
    PageMethods.GetUserName(OnSucceeded,OnFailed);
}


// completion of the page method.
function OnSucceeded(result, userContext, methodName) 
{
    
    // alert("Succeeded : " + result);
    if( result == "" ) {
        document.getElementById("WatchListContent").innerHTML = "<a href=\"../../account/default.aspx\">Please Login/Register to save properties to your watchlist.</a>";
    } else { 
        document.getElementById("WatchListContent").innerHTML = "Saving Property To Watchlist...";
        PageMethods.AddToWatchlist(propID, AddedToWatchlist, OnFailed);
    }
    /*
    if (methodName == "GetSessionValue")
    {
        displayElement.innerHTML = "Current session state value: " + 
            result;
    }
    */
}

// Callback function invoked on failure 
// of the page method.
function OnFailed(error, userContext, methodName) 
{
    alert(error.get_message());
    /*
    if(error !== null) 
    {
        displayElement.innerHTML = "An error occurred: " + 
            error.get_message();
    }
    */
}

function AddedToWatchlist(result, userContext, methodName) {
    document.getElementById("WatchListContent").innerHTML = "Property Added To Watchlist<br />" + 
        "<a href=\"../../account/manage/watchlist.aspx\">Go To Watchlist&nbsp;>></a>";
}


function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
selectbox.remove(i);
}
}


         function open_window2(url,w,h) {
            var status_string = "resizable=1,location=no,menubar=1,status=no,toolbar=1,scrollbars=1,left=10,top=10,width="+w+",height="+h;
            open(url, "_blank", status_string); 
        } 
