﻿var FirstLogin = false;
var LoadingImage = '/img/loadingAnimation.gif';
function GetSessionValue(service, params, OnSuccess) {
    $.ajaxSetup(
	 {
	     timeout: 100000,
	     dataType: "json",
	     error: function(xhr) {
	         if (xhr.status == 404) return null;
	     }
	 }
	 )

    $.post("services/" + service + ".aspx", params, OnSuccess);
}

/*
functionality 
itm="1" nam="View Property (Result)
itm="2" nam="View Property (Detail)
itm="3" nam="Logon
itm="4" nam="Inquire Property
itm="5" nam="Print Property Detail Page
itm="6" nam="Email this Property Detail Page
itm="7" nam="Save this Listing
itm="8" nam="Rate this Listing
itm="9" nam="Property Comments
itm="10" nam="Save this Search
itm="11" nam="View Virtual Tour
itm="12" nam="View Mortgage Calculator
itm="13" nam="Map Property
itm="14" nam="Schedule A Showing
itm="15" nam="Remove Saved Listing
*/
function DoLoginAndProcess(fnty, param, needLogin) {
    if (needLogin) {
        GetSessionValue('GetLoginStatus',
		  'time=' + Math.random(),
		  function(Session) {
		      params = param;
		      functionality = fnty;
		      NeedLogin = false;
		      var sPage = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1);
		      if ((Session.ContactKey == null) || (Session.ContactKey == 'undefined') || (Session.ContactKey == '')) NeedLogin = true;
		      if (NeedLogin) {
		          tb_show("Login", "Popup_HomeAlertLogin.aspx?height=285&width=480&fun=" + fnty + "&PageName=" + sPage + "&param=" + escape(param), null);
		      } else {
		          DoProcess(fnty);
		      }
		  }
		  );
    }
    else {
        params = param;
        functionality = fnty;
        DoProcess(fnty);

    }
}




function DoLogin(msgcontainer, frm, OnSucess) {

    $("#" + msgcontainer).html('<img src="' + LoadingImage + '" />');

    //values in the forms
    var myfrmval = $("#" + frm).serialize();

    //setting up the ajax values
    $.ajaxSetup(
    {
        timeout: 100000,
        dataType: "text",
        error: function(xhr) {
            $("#" + msgcontainer).html(xhr.status);
        }
    }
    )
    //ajax call
    $.post("services/UserLogin.aspx", myfrmval, OnSucess);
}

function DoProcess(loginStatus) {
    GetSessionValue('GetLoginStatus', 'time=' + Math.random(),
	        function(Session) {
	            _UserKey = Session.ContactKey;
	            if (isNaN(loginStatus)) {
	                $("#msgLoginStatus").html(loginStatus);
	                return;
	            }
	            if (_UserKey != '') {
	                $("#RegisterLi").hide();
	                $("#LoginLi").children("a").attr("style", "color:red");
	                $("#LoginLi").children("a").html("Return to My Portfolio");
	            }







	            switch (functionality) {
	                case 3: location.replace('NewAccount.aspx'); break;
	                case 4: tb_show("Inquire About this Property", "Popup_RequestInfo.aspx?height=490&width=480&" + params, null); break;
	                case 6: tb_show("Email This Property", "Popup_EmailListing.aspx?height=400&width=440&" + params, null); break;
	                case 7: tb_remove(); SaveListings(params); break; /////tb_show("Save this Property", "Popup_SaveListing.aspx?height=200&width=400&"+params, null); break;
	                case 8: tb_remove(); SaveListings(params); break;
	                case 9: tb_remove(); SaveListings(params); break;
	                case 10: tb_show("Save Search", "Popup_SaveSearch.aspx?height=390&width=430&preSearch=true&" + params, null); break; //Save Search from search page
	                //case 12: tb_show("Mortgage Calculator", "Popup_MortgageCalculator.aspx?height=425&width=480&"+params, null); break;  
	                //case 13: window.open(params); break;     

	                case 14: tb_show("Schedule a showing", "Popup_ScheduleShowing.aspx?height=425&width=480&" + params, null); break;
	                case 15: tb_remove(); RemoveListings(params); break;
	                //case 16: window.open(params); break;    

	                case 101: tb_show("Forgot Password", "Popup_ForgotPassword.aspx?height=100&width=300&" + params, null); break;
	                case 102: tb_show("Contact Us", "Popup_ContactUs.aspx?height=500&width=430&" + params, null); break;
	                case 103: tb_show("Save Map Search", "Popup_SaveMapSearch.aspx?height=200&width=400&preSearch=true&" + params, null); break; //Save Search from search page
	                default: break;

	            }
	            var iPropertyIndex = GetValueFromQueryString(params, "PropertyIndex");
	            var idcmlsNo = GetValueFromQueryString("?" + params, "mls");



	            if (functionality < 100 && (idcmlsNo == "0" || idcmlsNo == "")) {
	                SaveWebsiteActivity(functionality, iPropertyIndex);
	            }
	            // To ensure that the tracking is not going to be done ,when clicking on request Information .
	            // It will be done ,only on clicking the submit button on the Request Information Form .
	            else if (functionality != 4 && functionality != 101) {
	                SaveWebsiteActivitywithMLSNum(functionality, iPropertyIndex, idcmlsNo);

	            }

	            return false;

	        }
	 )
}

function CallTrack(Val) {
    $("#ctl00_mainContent_txtWebTrackAction").val(Val);
    __doPostBack('ctl00$mainContent$lnkWebTrack', '');
}

function SaveWebsiteActivity(pEnumWebsiteActivity, pPropertyIndex) {

    //if (_UserKey != "0" && _UserKey!=""){    
    if ($("#ctl00_mainContent_txtWebTrackAction").size()) {
        $("#ctl00_mainContent_txtWebTrackAction").val(pEnumWebsiteActivity);
        __doPostBack('ctl00$mainContent$lnkWebTrack', '');
    } else {
        $("#ctl00_mainContent_txtWebTrackActionType").val(pEnumWebsiteActivity);
        $("#ctl00_mainContent_txtWebTrackPropertyIndex").val(pPropertyIndex);
        __doPostBack('ctl00$mainContent$lnkTrackAction', '');
    }
    //    }
}


function SaveWebsiteActivitywithMLSNum(pEnumWebsiteActivity, pPropertyIndex, idcmlsNo) {

    //if (_UserKey != "0" && _UserKey!=""){    
    if ($("#ctl00_mainContent_txtWebTrackAction").size()) {
        $("#ctl00_mainContent_txtWebTrackAction").val(pEnumWebsiteActivity);
        __doPostBack('ctl00$mainContent$lnkWebTrack', '');
    } else {
        $("#ctl00_mainContent_txtWebTrackActionType").val(pEnumWebsiteActivity);
        $("#ctl00_mainContent_txtWebTrackPropertyIndex").val(pPropertyIndex);
        $("#ctl00_mainContent_txtWebTrackMlsNumber").val(idcmlsNo);
        __doPostBack('ctl00$mainContent$lnkTrackActionMod', '');
    }
    //    }
}




function RemoveListings(params) {
    $.ajaxSetup(
	 {
	     timeout: 100000,
	     dataType: "text",
	     error: function(xhr) {
	         if (xhr.status == 404) return null;
	     }
	 })

    $.get("services/Popup_RemoveListings.aspx", params,
	    function(output) {
	        var rating = GetValueFromQueryString(params, "rating");
	        var i = 1;
	        for (; i <= rating; i++) {
	            $("#star" + i).attr("src", "img/property_details/rateOn.gif");
	        }
	        for (; i <= 5; i++) {
	            $("#star" + i).attr("src", "img/property_details/rateOff.gif");
	        }
	        var usage = GetValueFromQueryString(params, "usage");
	        var fav = GetValueFromQueryString(params, "fav");

	        if ((usage == 1) && (fav != 1)) {
	            alert('Thank you !! This Property has been removed to your Save Listing');
	        }

	        else if (usage == 5) {
	            addnote();
	            alert('Thank you !! Your Notes has  been removed');
	        }
	        if (fav == 1) {
	            alert('Thank you !! This Property has been removed from Favorite Listing');
	        }
	        if (header = "My Favorite properties") {
	            var MLS = GetValueFromQueryString("?" + params, "mls");
	            URL = URL.replace(MLS + ",", "");
	            window.location.href = URL;
	        }
	    }
    );
}


function SaveListings(params) {
    $.ajaxSetup({
        timeout: 100000,
        dataType: "text",
        error: function(xhr) {
            if (xhr.status == 404) return null;
        }
    })

    var usage = GetValueFromQueryString(params, "usage");
    var fav = GetValueFromQueryString(params, "fav");
    var rating = GetValueFromQueryString(params, "rating");

    $.get("services/Popup_SaveListings.aspx", params,
	    function(output) {
	        if (rating != "") {
	            var i = 1;
	            for (; i <= rating; i++) {
	                $("#star" + i).attr("src", "img/rateOn.gif");
	            }
	            for (; i <= 5; i++) {
	                $("#star" + i).attr("src", "img/rateOff.gif");
	            }
	        }

	        if ((usage == 1) && (fav != 1)) {
	            $("#TotalFav").val(parseInt($("#TotalFav").val()) + 1);
	            alert('Thank you !! This Property has been added to your Save Listing');
	            //window.location.href = window.location.href;
	        }
	        else if (usage == 5) {
	            addnote();
	            alert('Thank you! Your notes have been added');
	            //window.location.href = window.location.href;
	        } else if (usage == 3) {
	            $("#TotalRated").val(parseInt($("#TotalRated").val()) + 1);
	            alert('This property has been rated ');
	            //window.location.href = window.location.href;
	        }
	        if (fav == 1) {
	            alert('Thank you !! This Property has been added to Favorite Listing');
	            $("#TotalFav").val(parseInt($("#TotalFav").val()) + 1);
	            //window.location.href = window.location.href;
	        }
	        //window.location.href = window.location.href;
	        __doPostBack('', '');
	    }




	 );

    function addnote() { }
}

function doWebTrack(Mlsnum, webTrack) {

    $.ajaxSetup(
    {
        timeout: 100000,
        dataType: "text",
        error: function(xhr) {
            if (xhr.status == 404) return null;
        }
    });
    paramsForTrack = "Mlsnum=" + Mlsnum + "&webTrack=" + webTrack;
    if (_UserKey != null && _UserKey != "") {
        paramsForTrack = paramsForTrack + "&userKey=" + _UserKey;
    }
    $.post("services/UserWebTrack.aspx", paramsForTrack, function(result) { });
}
