﻿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 	= 0 : Just login, no functionality after login into the system.
					= 1 : Save this Property
					= 2 : Save Search (getAlert)
					= 3 : rating
					= 4 : Inquire About this Property
					= 5 : Map/Directions
				    = 6 : Mortgage Calculator
				    = 7 : Email This Property
				    = 8 : Forgot Password
				    = 9 : Contact Us
					
*/
function DoLoginAndProcess(fnty, param, needLogin)
{	
    if(needLogin)
    {
	    GetSessionValue('GetLoginStatus',
		  'time=' + Math.random(),
		  function(Session) {
			   params = param;
			   functionality = fnty;
			   
			   NeedLogin = false;
			   
			   if( (Session.ContactKey==null) || (Session.ContactKey=='undefined') || (Session.ContactKey=='') ) NeedLogin = true;
			   
			   if(NeedLogin) {
					tb_show("Login", "/Popup_HomeAlertLogin.aspx?height=300&width=565&fun="+fnty, null);
			   }	else {
					DoProcess(fnty);
			   }
		  }
		  );
    }
    else
    {
        params = param;
        functionality = fnty;
        DoProcess(fnty);
    }
}

function DoLoginPolicyRegisterAndProcess(fnty, param, needLogin)
{	
    if(needLogin)
    {

	    GetSessionValue('GetLoginStatus',
		  'time=' + Math.random(),
		  function(Session) {
			   params = param;
			   functionality = fnty;
			   
			   NeedLogin = false;
			   
			   if( (Session.ContactKey==null) || (Session.ContactKey=='undefined') || (Session.ContactKey=='') ) NeedLogin = true;
			   
			   if(NeedLogin) {
			
					tb_show("Login", "/Popup_PolicyRegister.aspx?price=1&height=500&width=600&fun="+fnty, null);
			   }	else {
		
					DoProcess(fnty);
			   }
		  }
		  );
    }
    else
    {
        params = param;
        functionality = fnty;
        DoProcess(fnty);
    }
}

function DoLoginAndProcess1(fnty, param, needLogin)
{	
    if(needLogin)
    {
	    GetSessionValue('GetLoginStatus',
		  'time=' + Math.random(),
		  function(Session) {
			   params = param;
			   functionality = fnty;
			   
			   NeedLogin = false;
			   
			   if( (Session.ContactKey==null) || (Session.ContactKey=='undefined') || (Session.ContactKey=='') ) NeedLogin = true;
			   
			   if(NeedLogin) {
					tb_show("Login", "/Popup_HomeAlertLogin.aspx?price=1?height=300&width=565&fun="+fnty, null);
			   }	else {
					DoProcess(fnty);
			   }
		  }
		  );
    }
    else
    {
        params = param;
        functionality = fnty;
        DoProcess(fnty);
    }
}


function DoLogin(msgcontainer, frm, OnSucess)
{
    $("#"+msgcontainer).html('<img src="/images/loadingAnimation.gif" />');
    
    //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 DoLoginReg(msgcontainer, frm, OnSucess)
{
    $("#"+msgcontainer).html('<img src="/images/loadingAnimation.gif" />');
    
    //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 NewUser() {
    tb_show("New Account", "/Popup_HomeAlertRegister.aspx?height=380&width=600", null);
}

function NewUser1() {
    tb_show("Price History", "/Popup_PolicyRegister.aspx?height=500&width=600", null);
}
function DoProcess(loginStatus)
{

	 if(isNaN(loginStatus))
	 {      
        $("#msgLoginStatus").html(loginStatus);
		return;
	 }
	 /*
	 else if(loginStatus == "1")
	 {
	    location.replace('HomeAlertList.aspx');
	    return;
	 }
	 */

	 switch(functionality)
	 {
		  case 0:  location.replace('/NewAccount.aspx'); break;
		  case 1:  tb_remove(); SaveListings(params); break;/////tb_show("Save this Property", "Popup_SaveListing.aspx?height=200&width=400&"+params, null); break;
		  case 2:  tb_show("Save Search", "/Popup_SaveSearch.aspx?height=250&width=535&"+params, null); break;
		  case 3:  tb_remove(); SaveListings(params); break;
		  case 4:  tb_show("Inquire About this Property", "/Popup_RequestInfo.aspx?height=555&width=560&"+params, null); break; 
		  case 5:  tb_show("Map/Directions", "/Popup_PropertyDirection.aspx?height=550&width=700&"+params, null); break;
		  case 6:  tb_show("Mortgage Calculator", "/Popup_MortgageCalculator.aspx?height=540&width=480&"+params, null); break;
		  case 7:  tb_show("Email This Property", "/Popup_EmailListing.aspx?height=325&width=400&"+params, null); break;
		  case 8:  tb_show("Forgot Password", "/Popup_ForgotPassword.aspx?height=100&width=300&"+params, null); break;
		  case 9:  tb_show("Contact Us", "/Popup_ContactUs.aspx?height=300&width=300&"+params, null); break;
		  case 10: tb_show("Save Search", "/Popup_SaveSearch.aspx?height=250&width=535&preSearch=true&" + params, null); break; //Save Search from search page
		  case 11: tb_remove(); $("#ctl00_pnlLogin").html("<a href='/HomeAlertList.aspx'>My Account</a> | <a href='/Services/ProcessHomeAlertLogout.aspx'>Logout</a>"); window.location.href = '/HomeAlertList.aspx'  //Just login
		  case 12: tb_remove(); AddNotes(); break;   //Add Notes (only on detail page)
		  case 13: tb_remove(); $("#ctl00_pnlLogin").html("<a href='/HomeAlertList.aspx'>My Account</a> | <a href='/Services/ProcessHomeAlertLogout.aspx'>Logout</a>"); window.location.href = "/PropertyResultDropSeach.aspx?" + params;  //Just login
		  case 14:  tb_show("Contact Us", "/PopContactUs.aspx?width=900&height=600&"+params, null); break;
		  case 100: tb_show("Edit Mail Frequency ", "/Popup_EditSearch.aspx?height=200&width=535&preSearch=true&" + params, null); break; //Edit the Email Frequency for the Saved Search
		  case 101: tb_show("Edit Saved Search ", "/Popup_EditSaveSearch.aspx?height=200&width=535&preSearch=true&" + params, null); break; //EditSave Saved Search   
		  default: break;
	 }
//	 SaveWebsiteActivity(functionality);
	 return false;
}
function SaveWebsiteActivity(pEnumWebsiteActivity, pPropertyIndex){  
    if (_UserKey != "0"){
        $("#ctl00_cphMainContainer_txtWebTrackActionType").val(pEnumWebsiteActivity);
        $("#ctl00_cphMainContainer_txtWebTrackPropertyIndex").val(pPropertyIndex);
        __doPostBack('ctl00$cphMainContainer$lnkTrackAction','');
    }
    
}
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/SaveMatchedProperty.aspx",params,
	    function (output)
	    {   
            /*if(usage == 3)
            {
	            
	            var i = 1;
	            for(; i<=rating; i++)
	            {
	                $("#star" + i).attr("src", "img/rated.jpg");	            
	            }
	            for(; i<=5; i++)
	            {
	                $("#star" + i).attr("src", "img/star.jpg");	            
	            }
            }*/
                                  
            if (rating!="")
	        {
	            var i = 1;
	            for(; i<=rating; i++)
	            {
	                $("#star" + i).attr("src", "/images/rated_s.jpg");	            
	            }
	            for(; i<=5; i++)
	            {
	                $("#star" + i).attr("src", "/images/star_s.jpg");	            
	            }
            }

            if((usage==1)&&(fav!=1))
            {
                alert('Thank you !! This Property has been added to your Save Listing');
            }
            else if(usage==5)
            {
                alert('Your Note has been saved.');
                window.location.href=window.location.href;                
            }
            else if(usage==3)
            {
               //alert('Thank you !! 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');
            }	           
	    }
	    
	 
	    
	    
	 ); 
}

function GetValueFromQueryString(queryString, key)
{    
    key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
    var qs = regex.exec(queryString);
    if(qs == null)
    {
        return "";
    }
    else
    {        
        return qs[1];
    }
}

function ReplaceQueryString(queryString, key, value) {
    if (queryString == "" || queryString == null || queryString == "undifined") return "";

    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(queryString);
    if (qs == null) {
        if (value != "") return queryString + "&" + key + "=" + value; else return queryString;
    }
    else {
        if (value != "" || value > -1) {
            return queryString.replace(key + "=" + qs[1], key + "=" + value);
        }
        else {
            queryString = queryString.replace(key + "=" + qs[1], "");
            if (queryString.substr(queryString.length - 1, 1) == "&") queryString = queryString.substr(0, queryString.length - 1);
            queryString = queryString.replace("&&", "&");
            queryString = queryString.replace("?&", "?");

            return queryString.replace(key + "=" + qs[1], "");
        }
    }
}
