﻿// JScript File

//Name of the error div.
var ErrorElem = 'ErrorDiv';

//Add a handler to execute when async operation is completed.
var asyncPage = Sys.WebForms.PageRequestManager.getInstance();
asyncPage.add_endRequest(AsyncEndRequestHandler);

asyncPage.add_initializeRequest(InitializeRequest);

function pageLoad()
{
    //$addHandler($get("showModalPopupCalculator"), 'click', showModalPopupViaClient);
    //GetRecentlyViewedListings();
}

function GetRecentlyViewedListings()
{
    var SystemIDs = GetSavedListingsFromCookie();
    
    if(SystemIDs.length > 0)
        CTMLS.CTMLSService.GetRecentlyViewedListings(SystemIDs, ShowRecentlyViewedListings);
}

function GetSavedListingsFromCookie()
{
    var CookieName = "ctmlslistings";
    
    var theCookie = ""+document.cookie;
    var ind=theCookie.indexOf(CookieName);
    if(ind==-1 || CookieName=="") return '';
    var ind1=theCookie.indexOf(';',ind);
    if(ind1==-1) ind1=theCookie.length;
    return unescape(theCookie.substring(ind+CookieName.length+1,ind1));
}

function ShowRecentlyViewedListings(result, userContext, methodName)
{
    if(result.length > 0)
    {
        var listings = result.split("#");
        for(var i=0; i<listings.length; i++)
        {
            var listing = listings.split(",");
            
            for(var j=0; j<listing.length; j++)
            {
                
            }
        }
    }
}

function InitializeRequest(sender, args)
{
    var postbackElement = args.get_postBackElement();
   
    if (postbackElement.id == 'btnPropertyDetailPB')
    {
        $get('upProgressDetail').style.display = 'block';
    }
    if(postbackElement.id == 'btnMapSearch')
    {
        $get('uprogressMap').style.display = 'none';
    }
}

function AsyncEndRequestHandler(sender , e)
{
    if(e.get_error() != undefined)
    {
        var errorMessage = '';

        errorMessage = e.get_error().message;
        e.set_errorHandled(true);
        TimeOutClearMap();
        //ToggleErrorDiv('visible');
        //$get(ErrorElem).innerHTML = errorMessage;
	document.getElementById("ErrorDiv").innerHTML = errorMessage;
    }
    //else
        //ClearErrorState();
}

//Toggle the error div.
function ToggleErrorDiv(isVisible)
{
    $get(ErrorElem).style.visibility = isVisible;
}

//Clear the error div.
function ClearErrorState()
{
    $get(ErrorElem).innerHTML = '';
    ToggleErrorDiv('hidden');
}

////Shows the detail view of the property.
////Activated when "Detail View" link is cliked on the map or on the list.
//function showDetailView(e)
//{ 
//    document.getElementById('hMarkerID').value = e.name;
//    $get('btnPropertyDetailClick').click();
//}

function showDetailViewFromList(e)
{
    document.getElementById('hMarkerID').value = e;
    $get('btnPropertyDetailClick').click();
}

function showModalPopupViaClient(ev) 
{
    ev.preventDefault();
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    modalPopupBehavior.show();
    
    //Close the property Detail Pop up.
    var PropertyDetailBehavior = $find('PropertyDetailBehavior');
    PropertyDetailBehavior.hide();
}

function hideModalPopupViaClient(ev) 
{
    ev.preventDefault();
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    modalPopupBehavior.hide();
}

function ClosePopUp(behaviorName)
{
    var modalPopUp = $find(behaviorName);
    modalPopUp.hide();
}


function openModalPopUp(PDBehaviorName, CPBehaviorName)
{   
    var modalPopUp = $find(PDBehaviorName);
    modalPopUp.show();
    
    var CPModalPopUp = $find(CPBehaviorName);
    CPModalPopUp.hide();
}
