﻿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;
    //alert("inside DoLoginAndProcess");
    if (needLogin) {
        GetSessionValue('GetLoginStatus', 'time=' + Math.random(),
		  function(Contact) {
		      Contact = eval('(' + Contact + ')');
		      if (bNeedLogin && !CheckLoginStatus(Contact)) {
		          $.colorbox({ href: "/Popup/Popup_HomeAlertLogin.aspx", width: "630px", height: "365px", iframe: true });
		      } else {
		          DoProcess();
		      }
		  });
    } else {
        DoProcess();
    }
}

function CheckLoginStatus(Contact) {
    if ((Contact.CONTACTKEY == null) || (Contact.CONTACTKEY == 'undefined') || (Contact.CONTACTKEY == '')) {
        return false;
    }
    return true;
}


function DoProcess(url) {
    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();
	          //alert("DoProcess() iAction = " + iAction);
	          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: "590px", height: "570px" }); 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?" + sParams, 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
	              case WebsiteActivity.MapProperty: window.open("/Popup/Popup_PropertyDirection.aspx?" + sParams, "new", "menubar=1,resizable=1,width=900,height=925"); break;
	              case WebsiteActivity.MortgageCalculator: $.colorbox({ href: "/Popup/Popup_MortgageCalculator.aspx?" + sParams, width: "520px", height: "353px", iframe: true }); break;
	              case WebsiteActivity.MortgageInquiry: $.colorbox({ href: "/Popup/Popup_MortgageInquiry.aspx", width: "600px", height: "500px", iframe: true }); break;
	              default:
	                  if (url != null && url.length > 0)
	                      window.location = "/" + url; 
	                  else
	                  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("RemoveListings failed"); });

}

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("RemoveRatedListings failed"); });
}

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("RemoveNotedListings failed"); });
}

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("RemoveRecommendedListings failed"); });
}

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("SaveListing failed"); });
}

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("RateListing failed"); });
}

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 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: "630px", height:"365px", 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=550&width=497', width: "497px", height: "550px", iframe: true })
}

