var CSV_APPLET_NAME="getpage";		// Name of the CSV applet
var MAIN_LISTBOX=null;				// Listbox object used for displaying the event data
var FILTER_LISTBOX=null;			// Listbox object used for displaying the event filter options
var LISTBOX_MSG1="";				// Required because of translation (typically "Downloading events database")
var LISTBOX_MSG2="";				// Required because of translation (typically "please wait....")
var LISTBOX_NO_MATCH="";			// Required because of translation (typically "No matching records")
var LISTBOX_SEARCHING="";			// Required because of translation (typically "Searching the database")
var LIST_SIZE=20;					// Default event listbock size
var LAST_EVNT_URL="";				// Last events URL issued to the CSV applet
var events=new Array();				// Array of event objects

// Sets the reference to the <select> listboxs used to display the event info
function setMainList(lst) {MAIN_LISTBOX=lst;}
function setFilterList(lst) {FILTER_LISTBOX=lst;}

// Functions to set strings used in the listbox which require translation
function setListMsg1(s) {LISTBOX_MSG1=s;}
function setListMsg2(s) {LISTBOX_MSG2=s;}
function setListNoMatch(s) {LISTBOX_NO_MATCH=s;}
function setListSearching(s) {LISTBOX_SEARCHING=s;}

// Returns required HTML to create the CSV applet used for retrieving events
function GetCSVAppletHTML() {
	var s='<applet '+
	'	archive="'+CSV_APPLET_NAME+'.jar"'+
	'	codebase="/common/"'+
	'	code="'+CSV_APPLET_NAME+'.class"'+
	'	name="'+CSV_APPLET_NAME+'"'+
	'	width=10'+
	'	height=10'+
	'>\n'+
	'<param name="BrowserBackgroundColor" value="2B377F">\n'+
	'<param name="debug_level" value="1">\n'+
	'</applet>\n';
	return s;
}

// Object used to represent a database event
function EventObj(cam,alm,julian,offset,file,ondisk,index,duration,pre_alarm,archive) {
	this.cam=parseInt(cam,10);
	this.alm=trim(alm);
	this.julian=parseInt(julian);
	this.time=Julian2String(this.julian,UTC_OFFSET);
	this.offset=parseInt(offset);
	this.file=trim(file);
	this.ondisk=(ondisk=="exists")?true:false;
	this.exists=(ondisk=="exists")?'Y':'N';
	this.index=parseInt(index);
	this.duration=parseInt(duration);
	this.pre_alarm=parseInt(pre_alarm);
	this.archive=parseInt(archive);
	return this;
}

// Sends a CGI command to the CSV Applet and returns the raw data as sent back by the applet
function GetRawData(url) { return myCSV.getdata(url)+''; }

// Parses the raw event block delimitered string from the applet and adds the data to the event object
function GetEvents(url) {
	var strEvents=GetRawData(url);
	for(i in events) { delete events[i]; }
	events.length=0;
	if (strEvents!="") {
		var aL=strEvents.split("\n");
		var NumEvnts=0;
		for(i in aL) { 
			if (aL[i]!="") {
				aV=aL[i].split(",");
				aV[2] = unescape(aV[2]);
				events[aV[0]]=new EventObj(aV[1],aV[2],aV[3],aV[4],aV[5],aV[6],aV[7],aV[8],aV[9],aV[10]);
				NumEvnts++;
			}
		}
	}
	return strEvents;
}

// Clears the listbox and displays a please wait message
function ResetEventsListBox(s) {
	if (s==null){s=LISTBOX_MSG1;}
	MAIN_LISTBOX.length=0;
	addOption(s,'',MAIN_LISTBOX);
	addOption(LISTBOX_MSG2,'',MAIN_LISTBOX);
}

// Adds an item to the listbox
function addOption(n,v,l) {
	var o=new Option(n,v);
	l.options[l.options.length]=o;
}

// Main function used to display a block of events in the listbox
function WriteEventsListbox(dir,init,justfilter,params) {
	var cam=''; var str='';
	
	// Build up CGI URL to retrieve the events from the applet
	var url=BASE_URL+CGI_EVENTS+'rnd='+GetRnd()+'&format=csv&listlength='+dir+LIST_SIZE;
	
	// Are we searching the database for a text string?
	if (params=="") {
		if (!init) {
			if(dir=='-'){url+='&time='+events[MAIN_LISTBOX.options[0].value].julian;}
			else {url+='&time='+events[MAIN_LISTBOX.options[(MAIN_LISTBOX.length-1)].value].julian;}
			ResetEventsListBox();
		}
		if (justfilter) {url=LAST_EVNT_URL;}
		LAST_EVNT_URL=url;
		url+='&'+FILTER_LISTBOX.options[FILTER_LISTBOX.selectedIndex].value
	} else { 
		ResetEventsListBox(LISTBOX_SEARCHING);
		url+='&'+params; 
	}
	GetEvents(url);
	MAIN_LISTBOX.length=0;
	for(i in events) { 
		if (events[i].ondisk) {
//			var Spacer="?";
			var Spacer= ' ';

			cam=((events[i].cam==0)?'-':events[i].cam)+Spacer+Spacer;
			cam=Spacer+cam.substr(0,2);
			if (events[i].alm==LISTBOX_NO_MATCH) { str=events[i].alm; }
			else { str=cam+Spacer+Spacer+events[i].time+Spacer+events[i].alm.substring(0,16); }			
//			addOption(str,events[i].julian,MAIN_LISTBOX);
			addOption(str,i,MAIN_LISTBOX);
		}
	}
	if (MAIN_LISTBOX.length==0) { 
		var julian=GetRawData(BASE_URL+CGI_VARIABLE+'variable=current_julian');
		if (julian=="") { julian=events[(events.length-1)].julian; }
//		addOption(LISTBOX_NO_MATCH,julian,MAIN_LISTBOX); 
		addOption(LISTBOX_NO_MATCH,0,MAIN_LISTBOX); 
	}
	return false;
}

// Return the value for the specified event name
function GetEventValue(time,name) {
	str="";
	for(i in events) {
		for(j in events[i]) { 
			if (parseInt(events[i][j])==parseInt(time)) {
				str=events[i][name];
				break;
			}
		}
		if (str!="") { break; }
	}
	return str;
}

function GetEventIndexValue(index,name) {
	return events[index][name];
}

// Play back the selected event in the listbox
function PlayEvent(obj) {
	var julian=UpdateGoto();
	var cam=GetEventIndexValue(MAIN_LISTBOX.options[MAIN_LISTBOX.selectedIndex].value,"cam");
	GotoTime(obj,cam);
}

// Emulates onDblClick in Netscape
var timeA=GetRnd();
function PlayEventNS(obj) {
	if (bw.ns||bw.gecko) {
		var timeB=GetRnd();
		var iTimeDiff=timeB-timeA;
		var iDblClickMilliSeconds=300;
		if (iTimeDiff < iDblClickMilliSeconds){PlayEvent(obj);}
		timeA=timeB;
		MAIN_LISTBOX.blur();
	} else { return true; }
}

// Update the date/time text boxes with using the selected event in the listbox
function UpdateGoto(blnUseList){
	if (blnUseList==null){blnUseList=true;}
	var time=new Date();
	if (blnUseList) {
		var offset = time.getTimezoneOffset();
		var julian=events[MAIN_LISTBOX.options[MAIN_LISTBOX.selectedIndex].value].julian;
		time.setTime((julian*1000)+(UTC_OFFSET*1000)+(offset*60000));
	}
	with(FRM){
		elements["gotoD"].value=pad(time.getDate(),2,0);
		elements["gotoM"].value=pad((time.getMonth()+1),2,0);
		elements["gotoYr"].value=(time.getYear()+((bw.ns||bw.gecko)?1900:0));
		elements["gotoHr"].value=pad(time.getHours(),2,0);
		elements["gotoMin"].value=pad(time.getMinutes(),2,0);
		elements["gotoSec"].value=pad(time.getSeconds(),2,0);
	}
	var desc="";
	if(MAIN_LISTBOX)
		 desc=GetEventIndexValue(MAIN_LISTBOX.options[MAIN_LISTBOX.selectedIndex].value,"alm");
	
	window.status=desc;
	return julian;
}

// Playback the specified time/date
function GotoTime(obj,cam){
	if (cam==null){cam=0;}
	if (cam<=0){cam=viewerGC(obj);}
	with(FRM){
		var iDay=parseInt(elements["gotoD"].value,10);
		var iMonth=parseInt(elements["gotoM"].value,10);
		var iYear=parseInt(elements["gotoYr"].value,10);
		var iHour=parseInt(elements["gotoHr"].value,10);
		var iMin=parseInt(elements["gotoMin"].value,10);
		var iSec=parseInt(elements["gotoSec"].value,10);
	}
	var td=new Date(Date.UTC(iYear,iMonth-1,iDay,iHour,iMin,iSec));
	offset=td.getTimezoneOffset()			
	var julian=td.getTime()/1000;
	viewerPE(obj,julian,cam);
}
