﻿var FirstLogin = false;
var LoadingImage = '/img/loadingAnimation.gif';

var SessionVariable = null;
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);
}

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) {
		          $.colorbox({ href: "/Popup/Popup_HomeAlertLogin.aspx?fun=" + fnty + "&PageName=" + sPage + "&param=" + escape(param), onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } });
		      } else {
		          SessionVariable = Session; 
		          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;
	          $("#RegisterLi").hide();
	          if (isNaN(loginStatus)) {
	              $("#msgLoginStatus").html(loginStatus);
	              return;
	          }
	          $.colorbox.remove(); $.colorbox.init();
	          switch (functionality) {
	              //case 3: location.replace('NewAccount.aspx'); break;        
	              case 2: break;
	              case 3: $.colorbox({ href: '/Popup/Popup_NewAccount.aspx', height: "500px", width: "600px", onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } }); break;
	              case 4: $.colorbox({ href: '/Popup/Popup_RequestInfo.aspx?' + params, height: "530px", width: "400px", onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } }); break;
	              case 6: $.colorbox({ href: '/Popup/Popup_EmailListing.aspx?' + params, height: "530px", onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } }); break;
	              case 7: SaveListings(params); break; /////tb_show("Save this Property", "Popup_SaveListing.aspx?height=200&width=400&"+params, null); break;
	              case 8: SaveListings(params); $(".rate_property").click(function() { $(".rate_property").html(""); }); break;
	              case 9: SaveListings(params); break;
	              case 10: $.colorbox({ href: "/Popup/Popup_SaveSearch.aspx?preSearch=true&" + params, width: "500px", height: "200px", onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } }); 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: $.colorbox({ href: "/Popup/Popup_ScheduleShowing.aspx?" + params, height: "530px", width: "400px", onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } }); break;
	              case 15: RemoveListings(params); break;
	              case 16: window.location = "/HomeAlertList.aspx"; break;
	              //case 101: tb_show("Forgot Password", "/Popup/Popup_ForgotPassword.aspx?KeepThis=true&TB_iframe=true&height=200&width=450&" + params, null); break; 
	              case 101: $.colorbox({ href: "/Popup/Popup_ForgotPassword.aspx?" + params, onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } }); break;
	              case 102: $.colorbox({ href: "/Popup/Popup_ContactUs.aspx?" + params, onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } }); break;
	              case 103: $.colorbox({ href: "/Popup/Popup_SaveMapSearch.aspx?preSearch=true&" + params, width: "500px", height: "300px", onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } }); break; //Save Search from search page
	              case 106: $.colorbox({ href: "/Popup/Popup_EditSearch.aspx?preSearch=true&" + params, onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } }); break; //Edit the Email Frequency for the Saved Search   
	              default: break;
	          }
	          if ($.browser.safari == undefined) {
	              var iPropertyIndex = GetValueFromQueryString(params, "PropertyIndex");
	              if (functionality < 100) SaveWebsiteActivity(functionality, iPropertyIndex);
	          }
	          return false;
	      }
	 )
}

function SaveWebsiteActivity(pEnumWebsiteActivity, pPropertyIndex) {
        if ($("#ctl00_mainContent_txtWebTrackAction").size()){
            $("#ctl00_mainContent_txtWebTrackAction").val(pEnumWebsiteActivity);                    
            __doPostBack('ctl00$mainContent$lnkWebTrack','');
        }
        else if ($("#ctl00_mainContent_txtWebTrackActionType").size()){
            $("#ctl00_mainContent_txtWebTrackActionType").val(pEnumWebsiteActivity);
            $("#ctl00_mainContent_txtWebTrackPropertyIndex").val(pPropertyIndex);
            __doPostBack('ctl00$mainContent$lnkTrackAction', '');
        }else if ($("#ctl00_txtWebTrackAction").size()){
            $("#ctl00_txtWebTrackAction").val(pEnumWebsiteActivity);
            __doPostBack('ctl00$lnkWebTrack', '');
        }
}
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 from Favorites');
	        }

	        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');
	        }
	        var URL = window.location.href;
	        var header = GetValueFromQueryString(URL, "header");
	        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/property_details/rateOn.gif");
	            }
	            for (; i <= 5; i++) {
	                $("#star" + i).attr("src", "img/property_details/rateOff.gif");
	            }
	        }

	        if ((usage == 1) && (fav != 1)) {
	            $("#TotalFav").val(parseInt($("#TotalFav").val()) + 1);
	            alert('Thank you! This property has been added to your Saved Properties.');
	            //window.top.location.href = window.top.location.href;
	        }
	        else if (usage == 5) {
	            addnote();
	            alert('Thank you! Your notes have been added');
	        } else if (usage == 3) {
	            $("#TotalRated").val(parseInt($("#TotalRated").val()) + 1);
	            alert('This property has been rated ');
	            $(".rate_property").hide();
	        }
	        if (fav == 1) {
	            alert('Thank you !! This Property has been added to Favorite Listing');
	            $("#TotalFav").val(parseInt($("#TotalFav").val()) + 1);
	        }
	        //window.top.location.href = window.top.location.href;
	    }
	 ); 
	 
	 
	 function addnote()
	 { }
}
function ShowForgotPassword() {
    //tb_remove();
   setTimeout("DoLoginAndProcess(101, '', false);", 200)
}
function ShowLogin() {
    //tb_remove();
    setTimeout("DoLoginAndProcess(16, '', true);", 200)
}
function ShowRegister(redirectParam) {
        $.colorbox({ href: '/Popup/Popup_NewAccount.aspx?' + redirectParam, onClosed: function() { $.colorbox.remove(); $.colorbox.init(); } });
}

