﻿var FirstLogin = false;
var LoadingImage = '/images/loadingAnimation.gif';
var sParams = '';
var iAction = -1;
var bNeedLogin = true;
var bneedTrack = false;
var WebLoginSession = 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(WebLogin) {
            WebLogin = eval('(' + WebLogin + ')');
            if (bNeedLogin && !CheckLoginStatus(WebLogin)) {
                $.colorbox({ href: "/Popup/Popup_HomeAlertLogin.aspx", width: "950px", height: "950px", iframe: true, transition: "none" });
		      } else {
		          DoProcess();
		      }
		  });
    }else {
        DoProcess();
    }
}

function CheckLoginStatus(WebLogin) {
    if ((WebLogin.WEBCONTACTKEY == null) || (WebLogin.WEBCONTACTKEY == 'undefined') || (WebLogin.WEBCONTACTKEY == '')) {
        return false;
    }
    return true;
}


function DoProcess() {
    GetSessionValue('GetLoginStatus', 'time=' + Math.random(),
	      function(WebLogin) {
	          WebLogin = eval('(' + WebLogin + ')');
	          if (bNeedLogin && !CheckLoginStatus(WebLogin)) {
	              alert("Please login"); return false;
	          }
	         
	          WebLoginSession = WebLogin;
	          //Variable name can be Reference in js/Reference/WebsiteActivityEnumRef.aspx
	          //$.colorbox.close();

	          switch (iAction) {
	              case WebsiteActivity.Register: $.colorbox({ href: "/Popup/Popup_NewAccount.aspx", width: "950px", height: "950px", iframe: true, transition: "none" }); break;
	              case WebsiteActivity.RequestInformation: $.colorbox({ href: "/Popup/RequestInformation.aspx?" + sParams, width: "950px", height: "950px", iframe: true, transition: "none" }); break;
	              case WebsiteActivity.EmailListing: $.colorbox({ href: "/Popup/Popup_EmailListing.aspx?" + sParams, width: "950px", height: "950px", iframe: true, transition: "none" }); break;
	              
	              // Close login page colorbox after listing is saved
	              case WebsiteActivity.SaveListing: SaveListing(sParams);  $.colorbox.close(); 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: "950px", height: "950px", iframe: true, transition: "none" }); break; //Save Search from search page
	              case WebsiteActivity.ScheduleShowing: $.colorbox({ href: "/Popup/ScheduleAShowing.aspx?" + sParams, width: "950px", height: "950px", iframe: true, transition: "none" }); break;
	              case WebsiteActivity.RemoveListing: RemoveListings(sParams); break;
	              case WebsiteActivity.ForgotPassword: $.colorbox({ href: "/Popup/Popup_ForgotPassword.aspx?" + sParams, width: "950px", height: "950px", iframe: true, transition: "none" }); break;
	              case WebsiteActivity.ContactUs: $.colorbox({ href: "/Popup/ContactUs.aspx?" + sParams, width: "950px", height: "950px", iframe: true, transition: "none" }); break;
	              case WebsiteActivity.EditSearch: $.colorbox({ href: "/Popup/Popup_EditSearch.aspx?preSearch=true&" + sParams, width: "950px", height: "950px", iframe: true, transition: "none" }); break; //Edit the Email Frequency for the Saved Search   
	              case WebsiteActivity.ContactAgent: $.colorbox({ href: "/Popup/Popup_ContactAgent.aspx?" + sParams, width: "950px", height: "950px", iframe: true, transition: "none" }); break;
	              default: window.location = "/HomePilot.aspx"; break;
	          }

	          //if (bneedTrack) SaveWebsiteActivity(iAction, sParams);

	          return;
	      }
	 )
}

function SaveWebsiteActivity(iAction, sParams) {
    var ck = (CheckLoginStatus(WebLoginSession)) ? WebLoginSession.WEBLOGINKEY : "";
    var sPROPERTYRT = GetValueFromQueryString("?"+sParams, "PROPERTYRT");
    var sPROPERTYPK = GetValueFromQueryString("?" + sParams, "PROPERTYPK");

    $.post("/Services/WebsiteActivity.aspx",
    "iWebLoginKey=" + 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 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") {
				$(".save-listing-" + sPROPERTYPK).html("<span class=\"results-saved-property\">Property Saved</span>");
				$(".save-listing-" + sPROPERTYPK).attr("href", "javascript:void(0)");
				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 failed");
    });
}

function ShowForgotPassword() {
    //tb_remove();
    setTimeout("DoLoginAndProcess(" + WebsiteActivity.ForgotPassword + ", '', false, false);", 200)

}
function ShowLogin() {
    GetSessionValue('GetLoginStatus', 'time=' + Math.random(),
	  function(WebLogin) {
	      WebLogin = eval('(' + WebLogin + ')');
	      if (!CheckLoginStatus(WebLogin)) {
	          //tb_remove();
	          //tb_show("Login", "/Popup/Popup_HomeAlertLogin.aspx?width=450&height=225", null)
	          $.colorbox({ href: '/Popup/Popup_HomeAlertLogin.aspx', width: "950px", height: "950px", iframe: true, transition: "none" })

	      } else window.location.href = "/HomePilot.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: "950px", height: "950px", iframe: true, transition: "none" })
}
function ShowAddressPopup() {
    //tb_remove();
    //tb_show('New Account', '/Popup/Popup_NewAccount.aspx?height=450&width=820', null);
    $.colorbox({ href: '/Popup/Popup_AddressSearch.aspx', width: "950px", height: "950px" , iframe: true, transition: "none" })
}
function AddressSearc(value) {
  window.parent.$.colorbox.close();
   // $.colorbox.close();
   
   window.location.href = "/PropertyResult.aspx?"+value;
}
function ShowSchool(value) {
    //tb_remove();
    //tb_show('New Account', '/Popup/Popup_NewAccount.aspx?height=450&width=820', null);
    $.colorbox({ href: '/Popup/OnboardSchools.aspx?value=' + value, width: "950px", height: "950px", iframe: true, transition: "none" })
}
