﻿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=225&width=480&fun="+fnty, null);
			   }	else {
					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)
{
	 if(isNaN(loginStatus))
	 {	
		  $("#msgLoginStatus").html(loginStatus);
		  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=200&width=400&"+params, null); break;
		  case 3:  tb_remove(); SaveListings(params); break;
		  case 4:  tb_show("Inquire About this Property", "Popup_RequestInfo.aspx?height=425&width=480&"+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 6: window.open("https://www.homeloans.com/loans/homeserviceslending-centralkentuckybranchky/tools/index.page"); 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=200&width=400&preSearch=true&"+params, null); break; //Save Search from search page
		  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;
		  }
	 }
	 )

	 $.get("services/SaveMatchedProperty.aspx",params,
	    function (output)
	    {	    
            var usage=GetValueFromQueryString(params,"usage");
            
            if(usage == 3)
            {
	            var rating =GetValueFromQueryString(params,"rating");
	            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");	            
	            }
            }
            
            var fav=GetValueFromQueryString(params,"fav");

            if((usage==1)&&(fav!=1))
            {
                alert('Thank you !! This Property has been added to your Save Listing');
            }
            else if(usage==5)
            {
                alert('Thank you !! Your Notes has  been added');
            }
            if(fav==1)
            {
                alert('Thank you !! This Property has been added to Favorite Listing');
            }	           
	    }
	    
	 
	    
	    
	 ); 
}