﻿var DataMap = null;
var oOutput = null;
var LastHighlightedTbl = null;
var NumPerPage = 5;
var CurrentPage = 1;
var isReady = false;


function Initialize(){
	GoogleAnalytics()
	var HTML = $get("divList").innerHTML
	oOutput = $get("divOutput");
	DataClear();
	Resize();
	window.onresize = Resize;
	isReady = true;

}

function ShowFirst(){
	var oTbl = $get('tblLayout0')
	if(!oTbl) return;
	var fncClick = oTbl.onclick
	fncClick();
}

function DataClear(){	
	oOutput.innerHTML="Searching...Please wait";	
	$get("tdBegCount").innerText="";
	$get("tdEndCount").innerText = "";
	$get("tdTotalCount").innerText = "";
	CurrentPage=1;
}

function Resize(){
	// If the user resizes the window this will resize all the layers
	var Height = GetScreenHeight()-50;
	var Width =  GetScreenWidth();
	var oDiv = $get("divList");
	NumPerPage = 	Math.floor(Height/65);
	oOutput.style.width = Width + "px";	
	oOutput.style.height = Height + "px";

	$get("divNavBottom").style.top = (Height+25) + "px"
	
	DataRefresh();
}



function DataRefresh(){
	var oDiv = $get("divList");
	var HTML=new Sys.StringBuilder("");	

	if (!isReady){
		setTimeout("DataRefresh()",100);
		return;
	}


	DataMap = parent.MemberData;
	if (!DataMap) return; // No Data yet
	var ddlPg = $get("PageNum")
	var NPages  = 1+ Math.ceil(DataMap.length / NumPerPage)
	if (CurrentPage>NPages) CurrentPage=1;
	if (ddlPg) {
		while (ddlPg.options.length) ddlPg.remove(0);
		for (var i=1;i<NPages; i++) {
			ddlPg.options.add(new Option(i,i));
		}
		if (ddlPg.options.length>0) {
			ddlPg.options[Math.min(ddlPg.options.length,Math.max(1,CurrentPage))-1].selected = true; 
		} else {CurrentPage=0;}
	}
	var Start = Math.max(0,(CurrentPage-1) * NumPerPage);
	var End = Math.min(Start+NumPerPage,DataMap.length);

	for (var N=Start; N<End; N++){
		var Member = new MemberInfo(DataMap,N);
		var Num = N+1;
		var ThisHTML = oDiv.innerHTML;

		var DetailURL = "Javascript:ShowDetail("+N + ",'" + TRIM(Member.Source) + "','" + Member.SysUserID + "')"
		ThisHTML=ThisHTML.replace(/_new/gi,"");


		ThisHTML=ThisHTML.replace(/_new/gi,"");
		ThisHTML=ThisHTML.replace(/{N}/g,N)
		ThisHTML=ThisHTML.replace("{N+1}",N+1)

		ThisHTML=ThisHTML.replace("{Name}",Member.AgentName)
		ThisHTML=ThisHTML.replace("{LongName}",Member.AgentName)
		ThisHTML=ThisHTML.replace("{Phone}",Member.Phone)
		ThisHTML=ThisHTML.replace("{OfficeName}",Member.OfficeName);
		ThisHTML=ThisHTML.replace("{Email}",Member.EmailAddress)
		ThisHTML=ThisHTML.replace("{Email}",Member.EmailAddress)

		var Photo = "../icons/Clear.gif"
		if (0<Member.Photo.length) {
			Photo = Member.Photo;
			ThisHTML=ThisHTML.replace("{Border}","1");
		} else {
//			ThisHTML=ThisHTML.replace("{Border}","0")
			ThisHTML = ThisHTML.replace("105px","165px");
		} 
		ThisHTML=ThisHTML.replace("{ImageURL}",Photo)
		ThisHTML=ThisHTML.replace("%7BImageURL%7D",Photo)
		ThisHTML=ThisHTML.replace(/{MemberDetailURL}/g,""+DetailURL)
		ThisHTML=ThisHTML.replace(/%7BMemberDetailURL%7D/g,""+DetailURL)		
		
		ThisHTML=ThisHTML.replace("{SysID}",Member.SysUserID);
		ThisHTML=ThisHTML.replace("{Source}",Member.Source);
		HTML.append(ThisHTML);
	
	}
	oOutput.innerHTML = HTML.toString();

	if (parent.isDataOverflow && (End == DataMap.length)) {
		HTML.append ("&nbsp;&nbsp;<b style='color:red'>Warning</b>: display limited to " + End + " agents");
	}
	if (DataMap.length==0)HTML.append("&nbsp;&nbsp;Nothing to display");
	
	oOutput.innerHTML = HTML.toString();
	$get("tdBegCount").innerText= Start+1;
	$get("tdEndCount").innerText = End;
	$get("tdTotalCount").innerText = CommaFmt(DataMap.length);

	//setTimeout("ShowFirst()",200);

}

function ShowDetail(N,Source,SysID){
	parent.ContentFrame.location.href=CreateNoCookieURL("MemberDetail.aspx?N=" + N + "&S=" + Source + "&Id=" + SysID) ;
}	
	
function fnSearchSort(Field){
	parent.Priority = Field;
	DataClear();
	parent.Search();
}
		
function Next(Dir){
	CurrentPage = CurrentPage + Dir;
	var MaxPage = Math.ceil(DataMap.length/NumPerPage);
	CurrentPage = Math.min(Math.max(1,CurrentPage),MaxPage);
	DataRefresh();
}

function Print(){

	var whnd = window.open(CreateNoCookieURL("MemberListPrint.aspx"),"printwindow","width=680,height=500;");
}

function Move(pg){
	if (pg<0) return;
	CurrentPage = pg;
	setTimeout("Next(0)",50);
}

function HighlightTbl(evt){
	var oTbl = null;
	var eType = null;

	if (window.event){
		oTbl = window.event.srcElement;
		eType = window.event.type;
	} else {
		oTbl = evt.target;
		eType = evt.type;
	}
	
		
	if(LastHighlightedTbl) LastHighlightedTbl.style.backgroundColor="white";
	switch (eType){
		case "mouseover": 
			while (oTbl && oTbl.tagName.toUpperCase()!='TABLE')	oTbl = oTbl.parentNode;
			oTbl.style.backgroundColor="#ffcccc";	
			LastHighlightedTbl = oTbl;
			break;
		case "mouseout": break;
		default: alert("Missing EventType Arg for HighlightTbl: " + eType);
	}

}