﻿var ContentFrame = null;
var MenuFrame = null;
var DetailFrame = null;
var oDetail = null;
var oContent = null;
var oMenu = null;
var Priority = "NewID()"; // Random
var Max = 999; 
var MapAddr="";
var CurrentTab="Map"; // Need for PropDetail "Return to Map"

var isSearching = false;
var isDataOverflow = false;
var IgnoreMapView = true

var PropMapData = null;
var PropListData = null;

var MapCoord = "0,0,0,0"  
var MapCenterLat = 0;
var MapCenterLon = 0;
var MapZoom = 0;
var MapAddr = "";
var Site = null;


function Initialize(){
	GoogleAnalytics()
	ContentFrame = window.frames["Content"] 
	MenuFrame = window.frames["Menu"];
	DetailFrame = window.frames["Detail"];
	
	MapCoord = $get("hMapCoord").value 
	MapZoom = parseInt($get("hMapZoom").value);
	
	var aMap = MapCoord.split(",")
	MapCenterLat = .5 * (MakeFloat(aMap[0]) + MakeFloat(aMap[2]));
	MapCenterLon = .5 * (MakeFloat(aMap[1]) + MakeFloat(aMap[3]));
			
	
	oContent = $get("Content")
	oMenu = $get("Menu");
	oDetail = $get("Detail");
	
	
		
	Resize();
	window.onresize = Resize;
	Search(); 

}

function SaveMapView(Rect, Lat, Lon, Zoom){
	MapCoord = Rect;
	MapCenterLat = Lat;
	MapCenterLon = Lon;
	MapZoom = Zoom;
	var EncryptedVisitorID = $get("hEncryptedVisitorID").value
	MyAjaxService.UpdateSavedCriteriaMap(EncryptedVisitorID, MapZoom, MapCoord);
	 
}

function Resize(){
		var h = Math.max(75,GetScreenHeight() - 80);
		var w = Math.min(GetScreenWidth()-100,350);
		var ContentWidth = GetScreenWidth()-(w+6);

		oContent.style.left = w + "px";	
		oContent.style.height = h + "px";
		oContent.style.width = ContentWidth + "px";

		oMenu.style.height = (h-163) + "px";
		oMenu.style.width = w + "px";

		oDetail.style.left = w + "px";	
		oDetail.style.height = h + "px";
		oDetail.style.width = ContentWidth + "px";


		var oNav = $get("divNav");
		oNav.style.width = w + "px";

		FrameRefresh();
}

function Search(){
	if (isSearching) return; // don't jam up with multiple searches at once.
	PropData = null;
	SelectedProp=null;	

	if (MenuFrame && MenuFrame.DataClear) MenuFrame.DataClear();
	if (ContentFrame && ContentFrame.DataClear) ContentFrame.DataClear();
	DisplayMsg("Searching...");

	var Max = 100;
	var EncryptID = $get("hEncryptedVisitorID").value
	isSearching=true;

	var Type=($get("FavType0").checked)?"F":"V";

	MyAjaxService.MyFavoritesSearch(
			Max,
			EncryptID, 
			Type,
			SearchCallBack, 
			FailedCallback
	);
	DisplayMsg("Retrieving Favorites");
			
}

function ShowDetailFrame(URL)
{
			oDetail.src = URL;
			oDetail.style.display = "block";
			oContent.style.display = "none";
}
function HideDetailFrame()
{
			oDetail.style.display = "none";
			oContent.style.display = "block";
			oDetail.src = "../blank.htm";
}
		
function SearchCallBack(Data){
	isSearching = false;
	PropListData = Data;
	PropMapData = Data;

	SelectedProp=null;
	LastSearchCountTxt = Data.length + "." + Data.length;
	
	DisplayMsg ("Retrieved " + (Data.length) + " Matches");
	if (Data.length==0) DisplayMsg("No matches found");
	setTimeout("FrameRefresh()",500); // Wait a bit before asking frames to refresh (to give OS time to clean up)
}

 
function FrameRefresh(){
	if(ContentFrame.Resize) ContentFrame.Resize(); // Trigger refresh of child frame.
	if(MenuFrame.Resize) MenuFrame.Resize(); // Trigger refresh of child frame.
}

function DisplayMsg(Text){
	$get("tdFeedBack").innerHTML = Text;
}
function DisplayStatus(Text){
	$get("tdStatus").innerHTML = Text;
}


function Login(){
	var Email = $get("txtVisitorEmail").value;
	var Email2 = $get("txtVisitorEmail2").value;
	var Password = $get("txtPassword").value;
	var VisitorID = $get("hEncryptedVisitorID").value
	var LastName = $get("txtLastName").value
	var FirstName = $get("txtFirstName").value
	//alert(Email)
	//alert(Email2)
	if (Email.length==0) {
		alert("Email Address Required");
		return;
	}
	
	if (Email!=Email2) {
		alert("Your Email Address was not verified")
		return;
	}
	if (0==Password.length) {
		alert("Password Required");
		return;
	}
	createCookie("VisitorEmail",Email,60);
	createCookie("VisitorPassword",Password);
	MyAjaxService.VisitorLogin(VisitorID, LastName, FirstName, Email, Password, LoginCallBack, FailedCallback);
	
}

function ForgotPassword(){
	var Email = $get("txtVisitorEmail").value;
	var Email2 = $get("txtVisitorEmail2").value;
	var VisitorID = $get("EhncryptedVisitorID").value
	if (Email.length==0) {
		alert("Email Address Required");
		return;
	}
	
	if (Email!=Email2) {
		alert("Your Email Address was not verified")
		return;
	}
	MyAjaxService.ForgotPassword(VisitorID, ForgotPasswordCallBack, FailedCallback);
}

function ForgotPasswordCallBack(Msg)
{
	if (0<Msg.length) alert(Msg);
}


function LoginCallBack(Response)
{
	var ID=parseInt(Response[0]);
	var EncryptedID = Response[1];
	if (0==ID) {
		alert("Incorrect login, try again." );
		return;
	}
	$get("hEncryptedVisitorID").value = EncryptedID;
	$get("divLogin").style.display = "none";
	// Remember this user's ID and email for 60 days and the password just for the session
	createCookie("VisitorID",ID,60)
	Search();
}