//Simon Champion, Skycom Ltd, 2006. These functions are common between pages that use my javascript select list box.
//There are other funcs that are related to the select list or called here (eg ClearForm()),
//but are in the individual pages because they are different for each page.
//If you want to add this feature to another page, you'll need the following:
//HTML code for the list box: (this is taken from routeplans.php)
//   <div style='overflow:scroll; height:200px; width:330px; border:inset black 1px;'>
//     <table id='datatable' cellpadding='2' cellspacing='0' style='white-space:nowrap;'>
//       <tr style='background-color:#C9C9C9'><th style='white-space:nowrap;'>Route Plan Name</th><th>Number 1</th><th>Number 2</th><th>Number 3</th></tr>
//       <tr><td colspan='10' align='center'><br />Please Wait<br />Loading Route plans...</td></tr>
//       <!-- no data here yet; the JS code will deal with that. -->
//     </table>
//   </div>
//Save, Delete and Add buttons that look like this:
//(The Add button is typically above the top-right corner of the selbox; the save and delete would be part of your edit form)
// <img id='savbtn_enabled'  height='19' width='56' src='images/save1.gif'   class='clickable hideblock' onclick='PressSaveButton();' onmouseover='this.src="images/save2.gif";' onmouseout='this.src="images/save1.gif";' /><img id='savbtn_disabled' height='19' width='56' src='images/save3.gif'   class='showblock' />
// <img id='delbtn_enabled'  height='19' width='56' src='images/delete1.gif' class='clickable hideblock' onclick='PressDelButton();'  onmouseover='this.src="images/delete2.gif";' onmouseout='this.src="images/delete1.gif";' /><img id='delbtn_disabled' height='19' width='56' src='images/delete3.gif' class='showblock' />
// <img id='addbtn_enabled'  height='19' width='69' src='images/add1.gif'    class='clickable showblock' onclick='PressAddButton();'  onmouseover='this.src="images/add2.gif";' onmouseout='this.src="images/add1.gif";' /><img id='addbtn_disabled' height='19' width='69' src='images/add3.gif'    class='hideblock' onclick='PressAddButton();' />
//See also routeplans.php and dayparting.php, and their related javascript files to learn more.

function BuildTable() {
	//Called once when table needs to be built. Adds all records in the array to the table.
	if(document.getElementById('temp_info_msg')) {document.getElementById('temp_info_msg').innerHTML+="...";}
	EmptyList();
	for(var rnum in G_Table) {
		AddLineToList(G_Table[rnum]);
	}
	document.body.style.cursor="";
}

function EmptyList() {
	//delete all table rows. (except the header)
	var tb=document.getElementById('datatable').tBodies[0];
	while(tb.rows.length>1) {
		tb.deleteRow(1);
	}
	ClearForm();
}

function AddLineToList(rec) {
	//Add the specified record to the end of the display list.
	//params: rec must be an array containing all the data for a single record.
	var newTR = document.createElement('tr');
	if(rec.id>G_MaxRec) {G_MaxRec=rec.id;}
	newTR.setAttribute('id', "dtr_"+rec.id);

	newTR.onmouseover=function() {RowMouseover(rec.id);}	//MSIE doesn't like using setAttribute for event handlers, so we have to do this instead. (fortunately, this method works with all browsers)
	newTR.onmouseout=function() {RowMouseout(rec.id);}
	newTR.onclick=function() {SelectRow(rec.id);}

	if(newTR.style.setAttribute) {	//setting styles in MSIE works differently to other browsers.
		newTR.style.setAttribute('cursor', "pointer");
		newTR.style.setAttribute('whiteSpace', "nowrap");
	} else {
		newTR.setAttribute('style', "cursor:pointer; cursor:hand; white-space:nowrap;");
	}
	newTR.setAttribute('bgcolor', "#FFFFFF");

	for(var fnum in rec) {
		var fldokay=false;
		for(var fld in selbox_fieldlist) {if(fnum.match(selbox_fieldlist[fld])){fldokay=true;}}
		if(fldokay) {
			var newTD = document.createElement('td');
			newTD.setAttribute('id', "dtd_"+rec.id+"|"+fnum);
			if(selbox_align) if(selbox_align[fnum]) { newTD.setAttribute('align',selbox_align[fnum]); }
			if(newTR.style.setAttribute) {	//setting styles in MSIE works differently to other browsers.
				newTD.style.setAttribute('paddingRight', "7px");
				newTD.style.setAttribute('whiteSpace', "nowrap");
			} else {
				newTD.setAttribute('style', "padding-right:7px; white-space:nowrap;");
			}
			var newTX = document.createTextNode(rec[fnum]);
			newTD.appendChild(newTX);
			newTR.appendChild(newTD);
		}
	}
	document.getElementById('datatable').tBodies[0].appendChild(newTR);
}

function RefreshRow(rec) {
	//Refresh the data displayed in the specified line on the list.
	var refrow=document.getElementById('dtr_'+rec.id);
	if(!refrow) {return;}	//didn't find the row, so can't update it.

	for(rc in refrow.cells) {				//loop through all table cells in this row.
		var refcell=refrow.cells[rc];
		if(refcell.id) {
			var tmp=refcell.id.split("|");
			if(tmp[1]) {
				refcell.innerHTML=rec[tmp[1]];	//update the cell with the appropriate value.
			}
		}
	}
}

function SelectRow(recid) {
	if(G_ListActive) {
		if(G_CurrRec) {document.getElementById("dtr_"+G_CurrRec).style.backgroundColor=bg_def;}
		if(recid) {document.getElementById("dtr_"+recid).style.backgroundColor=bg_selover;}
		G_CurrRec=recid;
		G_AddMode=false;
	}
	EnableButton('sav',true);
	PopulateForm();
	EnableButton('del',(G_CurrRec && !G_Table[G_CurrRec]['inuse']));
}

function EnableButton(btn,enabled) {
	//Toggle a named button either enabled or disabled.
	//(We do this by having two buttons for each, and making one visible and the other invisible at any given time. This allows us to have different roll-over and click functions for enabled vs disabled)
	if(document.getElementById(btn+'btn_enabled')) {
		document.getElementById(btn+'btn_enabled').className=(enabled?'clickable showblock':'hideblock');
	}
	if(document.getElementById(btn+'btn_disabled')) {
		document.getElementById(btn+'btn_disabled').className=(enabled?'hideblock':'showblock');
	}
}

function RowMouseover(recid) {
	//Flip the colour of the specified row to the hover colour.
	document.getElementById("dtr_"+recid).style.backgroundColor=(G_CurrRec==recid?bg_selover:bg_over);
}
function RowMouseout(recid) {
	//Flip the colour of the specified row to the default.
	document.getElementById("dtr_"+recid).style.backgroundColor=(G_CurrRec==recid?bg_sel:bg_def);
}
