﻿var FirstLogin = false;
var LoadingImage = '/img/loadingAnimation.gif';
var sParams = '';
var iAction = -1;
var bNeedLogin = true;
var bneedTrack = false;
var ContactSession = null;

//Import Enumeration Reference
$(document).ready(function() { ImportJS("/_js/Reference/WebsiteActivityEnumRef.ashx"); })

function GetSessionValue(service, params, OnSuccess) {
    $.post("services/" + service + ".aspx", params, OnSuccess);
}

function DoLoginAndProcess(action, param, needLogin, needTrack) {
    
    sParams = param;
    iAction = action;
    bNeedLogin = needLogin;
    bneedTrack = needTrack;

    if (needLogin) {
        GetSessionValue('GetLoginStatus', 'time=' + Math.random(),
		  function(Contact) {
		      Contact = eval('(' + Contact + ')');
		      if (bNeedLogin && !CheckLoginStatus(Contact)) {
		          $.colorbox({ href: "/Popup/Popup_HomeAlertLogin.aspx", width: "480px", height: "305px", iframe: true });
		      } else {
		          DoProcess();
		      }
		  });
    } else {
        DoProcess();
    }
}

function CheckLoginStatus(Contact) {
    if ((Contact.CONTACTKEY == null) || (Contact.CONTACTKEY == 'undefined') || (Contact.CONTACTKEY == '')) {
        return false;
    }
    return true;
}


function DoProcess() {

    GetSessionValue('GetLoginStatus', 'time=' + Math.random(),
	      function(Contact) {
	          Contact = eval('(' + Contact + ')');
	          if (bNeedLogin && !CheckLoginStatus(Contact)) {
	              alert("Please login"); return false;
	          }
	          ContactSession = Contact;

	          //Variable name can be Reference in js/Reference/WebsiteActivityEnumRef.aspx
	             $.colorbox.close();
	          switch (iAction) {
	              case WebsiteActivity.Register: $.colorbox({ href: "/Popup/Popup_NewAccount.aspx", width: "500px", height: "650px", iframe: true }); break;
	              case WebsiteActivity.RequestInformation: $.colorbox({ href: "/Popup/Popup_RequestInfo.aspx?" + sParams, width: "500px", height: "550px", iframe: true, onLoad: function() {$('#cboxClose').hide();}}); break;
	              case WebsiteActivity.EmailListing: $.colorbox({ href: "/Popup/Popup_EmailListing.aspx?" + sParams, width: "500px", height: "600px"}); break;
	              case WebsiteActivity.SaveSearch: $.colorbox({ href: "/Popup/Popup_SaveSearch.aspx?" + sParams, width: "500px", height: "200px", onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } }); break; //Save Search from search page
	              case WebsiteActivity.SaveListing: SaveListing(sParams); break; /////tb_show("Save this Property", "Popup_SaveListing.aspx?height=200&width=400&"+params, null); break; 
	              case WebsiteActivity.RateListing: RateListing(sParams); break;
	              case WebsiteActivity.NoteListing: NoteListing(sParams); break;
	              case WebsiteActivity.SaveSearch: $.colorbox({ href: "/Popup/Popup_SaveSearch.aspx?" + sParams, width: "500px", height: "600px", iframe: true }); break; //Save Search from search page
	              case WebsiteActivity.ScheduleShowing: $.colorbox({ href: "/Popup/Popup_ScheduleShowing.aspx?" + sParams, width: "500px", height: "580px", iframe: true, onLoad: function() { $('#cboxClose').hide(); }}); break;
	              case WebsiteActivity.RemoveListing: RemoveListings(sParams); break;
	              case WebsiteActivity.RemoveRatedProperty: RemoveRatedListings(sParams); break;
	              case WebsiteActivity.RemoveListingNotes: RemoveNotedListings(sParams); break;
	              case WebsiteActivity.RemoveRecommendedProperty: RemoveRecommendedListings(sParams); break;
	              case WebsiteActivity.ForgotPassword: $.colorbox({ href: "/Popup/Popup_ForgotPassword.aspx?height=600&width=500", width: "500px", height: "600px", iframe: true }); break; 
	              case WebsiteActivity.ContactUs: $.colorbox({ href: "/Popup/Popup_ContactUs.aspx?" + sParams, width: "500px", height: "600px", iframe: true }); break;
	              case WebsiteActivity.EditSearch: $.colorbox({ href: "/Popup/Popup_EditSearch.aspx?preSearch=true&" + sParams, width: "500px", height: "600px", iframe: true }); break; //Edit the Email Frequency for the Saved Search   
	              default: window.location = "/MyAccount.aspx"; break;
	          }

	          //if (bneedTrack) SaveWebsiteActivity(iAction, sParams);

	          return;
	      }
	 )
}

function SaveWebsiteActivity(iAction, sParams) {
    var ck = (CheckLoginStatus(ContactSession)) ? ContactSession.CONTACTKEY : "";
    var sPROPERTYRT = GetValueFromQueryString("?"+sParams, "PROPERTYRT");
    var sPROPERTYPK = GetValueFromQueryString("?" + sParams, "PROPERTYPK");

    $.post("/Services/WebsiteActivity.aspx",
    "iContactKey=" + ck + "&iAction=" + iAction + "&sRecordTypeName=" + sPROPERTYRT + "&sPrimaryKey=" + sPROPERTYPK,
    function(msg) {});
}

function RemoveListings(params) {
    var sPROPERTYRT = GetValueFromQueryString("?" + sParams, "PROPERTYRT");
    var sPROPERTYPK = GetValueFromQueryString("?" + sParams, "PROPERTYPK");
    $.post("/Services/RemoveSavedListing.aspx",
    "sRecordTypeName=" + sPROPERTYRT + "&sPrimaryKey=" + sPROPERTYPK,
    function(msg) { if (msg == "Success") { alert("Property is removed"); window.location.reload(); } else alert("Update fail"); });

}

function RemoveRatedListings(params)
{
    var sPROPERTYRT = GetValueFromQueryString("?" + sParams, "PROPERTYRT");
    var sPROPERTYPK = GetValueFromQueryString("?" + sParams, "PROPERTYPK");
    $.post("/Services/RemoveRatedProperty.aspx",
    "sRecordTypeName=" + sPROPERTYRT + "&sPrimaryKey=" + sPROPERTYPK,
    function(msg) { if (msg == "Success") { alert("Property is removed"); window.location.reload(); } else alert("Update fail"); });
}

function RemoveNotedListings(params)
{
    var sPROPERTYRT = GetValueFromQueryString("?" + sParams, "PROPERTYRT");
    var sPROPERTYPK = GetValueFromQueryString("?" + sParams, "PROPERTYPK");
    $.post("/Services/RemoveListingNotes.aspx",
    "sRecordTypeName=" + sPROPERTYRT + "&sPrimaryKey=" + sPROPERTYPK,
    function(msg) { if (msg == "Success") { alert("Property is removed"); window.location.reload(); } else alert("Update fail"); });
}

function RemoveRecommendedListings(params)
{
    var sPROPERTYRT = GetValueFromQueryString("?" + sParams, "PROPERTYRT");
    var sPROPERTYPK = GetValueFromQueryString("?" + sParams, "PROPERTYPK");
    $.post("/Services/RemoveRecommendedListing.aspx",
    "sRecordTypeName=" + sPROPERTYRT + "&sPrimaryKey=" + sPROPERTYPK,
    function(msg) { if (msg == "Success") { alert("Property is removed"); window.location.reload(); } else alert("Update fail"); });
}

function SaveListing(params) {
    var sPROPERTYRT = GetValueFromQueryString("?" + sParams, "PROPERTYRT");
    var sPROPERTYPK = GetValueFromQueryString("?" + sParams, "PROPERTYPK");
    $.post("/Services/SaveListing.aspx",
    "sRecordTypeName=" + sPROPERTYRT + "&sPrimaryKey=" + sPROPERTYPK,
    function(msg) { if (msg == "Success") alert("Property is saved"); else alert("Update fail"); });
}

function RateListing(params) {
    var sPROPERTYRT = GetValueFromQueryString("?" + sParams, "PROPERTYRT");
    var sPROPERTYPK = GetValueFromQueryString("?" + sParams, "PROPERTYPK");
    var iRating = GetValueFromQueryString("?" + sParams, "RATING");

    $.post("/Services/RateListing.aspx",
    "sRecordTypeName=" + sPROPERTYRT + "&sPrimaryKey=" + sPROPERTYPK + "&iRating=" + iRating,
    function(msg) { if (msg == "Success"){ alert("Property is rated"); window.location.reload();} else alert("Update fail");  });
}

function NoteListing(params) {
    var sPROPERTYRT = GetValueFromQueryString("?" + sParams, "PROPERTYRT");
    var sPROPERTYPK = GetValueFromQueryString("?" + sParams, "PROPERTYPK");
    var sNotes = GetValueFromQueryString("?" + sParams, "NOTES");

    $.post("/Services/ListingNotes.aspx",
    "sRecordTypeName=" + sPROPERTYRT + "&sPrimaryKey=" + sPROPERTYPK + "&sNotes=" + sNotes,
    function(msg) {
        if (msg == "Success") {
            alert("Property notes are updated");
            $('#divNote').hide();
        }
        else alert("Update fail");
    });
}

function ShowForgotPassword() {
    //tb_remove();
   // setTimeout("DoLoginAndProcess(" + WebsiteActivity.ForgotPassword + ", '', false, false);", 200)
   //alert("here");
    //DoLoginAndProcess(WebsiteActivity.ForgotPassword , '', false, false);
    $.colorbox({ href: '/Popup/Popup_ForgotPassword.aspx', width: "480px", height:"305px", iframe:true })

}
function ShowLogin() {
    GetSessionValue('GetLoginStatus', 'time=' + Math.random(),
	  function(Contact) {
	      Contact = eval('(' + Contact + ')');
	      if (!CheckLoginStatus(Contact)) {
	          //tb_remove();
	          //tb_show("Login", "/Popup/Popup_HomeAlertLogin.aspx?width=450&height=225", null)
	          $.colorbox({ href: '/Popup/Popup_HomeAlertLogin.aspx', width: "480px", height:"305px", iframe:true })
	          
	      } else window.location.href = "/MyAccount.aspx";
	  });
}
function ShowRegister() {
    //tb_remove();
    //tb_show('New Account', '/Popup/Popup_NewAccount.aspx?height=450&width=820', null);
    $.colorbox({ href: '/Popup/Popup_NewAccount.aspx?height=450&width=820', width: "860px", height: "600px", iframe: true })
}

function RedirectToAgent(params)
{
    
    $.colorbox.close();
    
     GetSessionValue('GetLoginStatus', 'time=' + Math.random(),
	  function(Contact) {
	      Contact = eval('(' + Contact + ')');
	      if (!CheckLoginStatus(Contact)) {
	          //tb_remove();
	          //tb_show("Login", "/Popup/Popup_HomeAlertLogin.aspx?width=450&height=225", null)
	       //  alert("here");
	          
	      } else window.location = params;
	  });
    //window.location = params;
}
