//*******************************************************
//* Function: ListApplet
//* Language: JavaScript
//* Written by: Steve Street
//* Creation Date: 07/11/2007
//* Description: The application creates a scollable
//* set of tables
//*******************************************************
var browser;
var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 4));
if(navigator.appName.indexOf("Microsoft Internet Explorer") >= 0) { browser = "IE"; } 
else { browser = "NS"; }

var lastSelection = null;
var lastDefColour = null;
var lastOrderCol = null;
var lastOrderBy = null;
var elHead = null;
var elData = null;
var rowid = null;

function doOrderBy(orderCol) {	
	try {
		var curpage = new String(window.location);
		var posn = curpage.indexOf("?");
		if((posn) && (posn > 0)) {
			curpage = curpage.substring(0, curpage.indexOf("?"));
		}
			
		if(lastOrderCol != null) {
			if(lastOrderCol == orderCol) {
				if(lastOrderBy == null) {
					lastOrderBy = true;
				} else {
					if(lastOrderBy) {
						lastOrderBy = false;
					} else {
						lastOrderBy = true;
					}
				}
			} else {
				lastOrderCol = orderCol;
				lastOrderBy = true;
			}
		} else {
			lastOrderCol = orderCol;
			lastOrderBy = true;
		}

		query = curpage + "?ordercol=" + lastOrderCol + "&orderby=" + lastOrderBy;
		if(typeof(additParms) != "undefined") query += additParms;
		document.location = query;
	} catch(e) {
		alert(e.message);
	}
}

function setCurrentRow() {
	try {

		var wndx = -1, wndy = -1, lax = -1, lay = -1, rowid = "";
		var query = window.location.search.substring(1);
		var pairs = query.split("&");
	
		for(var i = 0; i < pairs.length; i++) {
			var pos = pairs[i].indexOf('=');
			if(pos >= 0) {
				var argname = pairs[i].substring(0, pos);
				var value = pairs[i].substring(pos + 1);
				switch(argname) {
					case "rowid": rowid = value; break;
					case "wx": wndx = parseInt(value); break;
					case "wy": wndy = parseInt(value); break;
					case "lx": lax = parseInt(value); break;
					case "ly": lay = parseInt(value); break;
				}
			}
		}
		
		if((wndx > -1) && (wndy > -1)) window.scrollTo(parseInt(wndx), parseInt(wndy));
		if((lax > -1) && (lay > -1)) {
			document.getElementById('spnData').scrollTop = parseInt(lay);
			document.getElementById('spnData').scrollLeft = parseInt(lax);
			document.getElementById('spnHeader').scrollLeft = parseInt(lax); 
		}

		if(rowid != null) {
			var obj = document.getElementById("tblData");
			if(obj != null) {
				var rows = obj.getElementsByTagName("tr");
				for(var i = 0; i < rows.length; i++) {
					if(rows[i].id == rowid) {
						if(lastSelection != null) {
							deselectRowOrCell(lastSelection);
						}
						lastDefColour = rows[i].style.backgroundColor;
						selectRowOrCell(rows[i]);
						lastSelection = rows[i];
						rowid = rows[i].id;
						//document.getElementById("spnData").scrollTop = rows[i].offsetTop;
						break;
					}
				}
			}				
		}		
	} catch(e) {
		alert(e.message);
	}
}

function setHeaders() {
	try {
		var d = document.getElementById("spnData");
		var h = document.getElementById("spnHeader");
		h.scrollLeft = d.scrollLeft;
	} catch(e) {
		alert(e.message);
	}
}

function getPosition() {
	try {
		var curpage = new String(window.location);
		var posn = curpage.indexOf("?");
		
		if((posn) && (posn > 0)) {
			curpage = curpage.substring(0, curpage.indexOf("?"));
		}
				
		var wndx = 0; var wndy = 0; var lax = 0; var lay = 0; 
		var laobj;
		
	    if (document.all) {
	        wndx = document.body.scrollLeft;
	        wndy = document.body.scrollTop;
	    } else {
	        wndx = window.pageXOffset;
	        wndy = window.pageYOffset;
	    }
	    
	    laobj = document.getElementById("spnData");
	    if(laobj != null) {
	    	lay = laobj.scrollTop;
			lax = laobj.scrollLeft;
		} else {
			lay = 0; lax = 0;
		}
		
		var query = "wx=" + wndx + "&wy=" + wndy + "&lx=" + lax + "&ly=" + lay;
		if(lastOrderCol != null) query += "&ordercol=" + lastOrderCol + "&orderby=" + lastOrderBy;
		if(typeof(additParms) != "undefined") query += additParms;
		
		return query;
	} catch(e) {
		alert(e.message);
	}
}

function doOnSubmit() {
	try {
		var fobj = document.forms[0];
		//alert(fobj.tagName + ", " + fobj.id);
		if((fobj != null) && (fobj.tagName == "FORM")) {
			if(document.getElementById("spnData")) {
				if(fobj.action.indexOf("?") > 0) {
					fobj.action += "&rowid=" + rowid + "&" + getPosition();
				} else {
					fobj.action += "?rowid=" + rowid + "&" + getPosition();
				}
				fobj.submit();
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	} catch(e) {
		alert(e.message);
	}
}	

window.onsubmit = function() {
	doOnSubmit();
}

function select(element) {
	var e, r, c, theEvent, browser;

	try {
		if(!Nav4) {
			if(window.event) {
				theEvent = window.event;
			} else if(arguments.callee.caller) {
				theEvent = arguments.callee.caller.arguments[0];
			}

			if(theEvent) {
				if(theEvent.srcElement) {
					e = theEvent.srcElement;
				} else if(theEvent.currentTarget) {
					e = theEvent.currentTarget;
				} else if(theEvent.target) {
					e = theEvent.target;
				}
			}
		} else {
			theEvent = element;
			e = element.target;
		}

		if(e.tagName == "TR") {
			r = e;
		} else {
			r = findRow(e);
		}

		if((r != null) && (r.id != "")) {
			if(lastSelection != null) {
				deselectRowOrCell(lastSelection);
			}
			lastDefColour = r.style.backgroundColor;
			selectRowOrCell(r);
			lastSelection = r;
			rowid = r.id;
			
			var curpage = new String(window.location);
			var posn = curpage.indexOf("?");
			if((posn) && (posn > 0)) {
				curpage = curpage.substring(0, curpage.indexOf("?"));
			}
			
			var wndx = 0; var wndy = 0; var lax = 0; var lay = 0; 
			var laobj;
	
    		if (document.all) {
        		wndx = document.body.scrollLeft;
        		wndy = document.body.scrollTop;
    		} else {
        		wndx = window.pageXOffset;
        		wndy = window.pageYOffset;
    		}
    
    		laobj = document.getElementById("spnData");
    		if(laobj != null) {
    			lay = laobj.scrollTop;
				lax = laobj.scrollLeft;
			}
			query = curpage + "?rowid=" + rowid + "&" + getPosition();
			//if(lastOrderCol != null) query += "&ordercol=" + lastOrderCol + "&orderby=" + lastOrderBy;
			//if(typeof(additParms) != "undefined") query += additParms;
			//query += "&wx=" + wndx + "&wy=" + wndy + "&lx=" + lax + "&ly=" + lay
			window.location = query;
			
		}
	} catch(e) {
		alert(e.message);
	}
}

function cancelSelect() {	
	var theElement = null;
	var theEvent = null;
	var e = null;
	var r = null;

	if(window.event) {
		theEvent = window.event;
	} else if(arguments.callee.caller) {
		theEvent = arguments.callee.caller.arguments[0];
	}

	if(theEvent != null) {
		if(theEvent.srcElement) {
			e = theEvent.srcElement;
		} else if(theEvent.currentTarget) {
			e = theEvent.currentTarget;
		} else if(theEvent.target) {
			e = theEvent.target;
		}
	}

	if(e != null) {
		if (e.tagName != "BODY") return;
		if (lastSelection != null) {
   			deselectRowOrCell(lastSelection);
    		lastSelection = null;
  		}
  	}
}

document.onclick = cancelSelect;

function findRow(e) {	
	var p;

	if(e.tagNAme == "TR") {
		return e;
	} else {
		p = e.parentNode;
		while((p.tagName != "TABLE") && (p.tagName != "BODY") && (p.tagName != "TR")) {
			p = p.parentNode;
		}
		if(p.tagName == "TR") {
			return p;
		} else {
			return null;
		}
	}
}

function findCell(e) {
	if (e.tagName == "TD") {
    	return e;
  	} else if (e.tagName == "BODY") {
    	return null;
  	} else {
    	return findCell(e.parentNode);
  	}
}

function deselectRowOrCell(r) {
  	if(browser == "IE") {
  		r.runtimeStyle.backgroundColor = lastDefColour;
  		r.runtimeStyle.color = "";
  	} else {
  		r.style.backgroundColor = lastDefColour;
  		r.style.color = "";
  	}
}

function selectRowOrCell(r) { 	
  	var foreColour = "#000000";
	var backColour = "#BDBFFF";
	var e, r, browser;
  	
  	if(browser == "IE") {
  		r.runtimeStyle.backgroundColor = backColour;
  		r.runtimeStyle.color = foreColour;
  	} else {
  		r.style.backgroundColor = backColour;
  		r.style.color = foreColour;
  	}
}

function nothingSelected() {
  	return (lastSelection == null);
}

function rowSelected() {
  	var c;
  	if(lastSelection == null) return false;
  	c = lastSelection;
  	return (c.tagName == "TR");
}

function cellSelected() {
  	var c;
  	if (lastSelection == null) return false;
	c = lastSelection;
	return (c.tagName == "TD");
}

function whatIsSelected() {
  	if(lastSelection == null) 
    	return "Table";
  	if(lastSelection.tagName == "TD") 
    	return "Cell";
  	if(lastSelection.tagName == "TR")
    	return "Row";
}	

function GetValue(xNode, IsActiveX) {
	var retval = "";
	if(IsActiveX) {
		retval = xNode.text;
	} else {
		retval = xNode.firstChild.nodeValue;
	}
	return retval;	
}

function ListApplet(parObj, xmlData) {

	document.write("Running the ListApplet function<br>");
	//try {
		// code for IE
		IsActiveX = false;
		if (window.ActiveXObject) {
  			var doc = new ActiveXObject("Microsoft.XMLDOM");
  			doc.async = "false";
  			doc.loadXML(xmlData);
  			IsActiveX = true;
  		} else {
			// code for Mozilla, Firefox, Opera, etc.
  			var parser = new DOMParser();
  			var doc = parser.parseFromString(xmlData, "text/xml");
  		}
		var xRoot = doc.documentElement;
	
		var laName, laWidth, laHeight = "", tblWidth = -2;
		for(var i = 0; i < xRoot.childNodes.length; i++) {
			if(xRoot.childNodes[i].nodeName == "name") laName = GetValue(xRoot.childNodes[i], IsActiveX);
			if(xRoot.childNodes[i].nodeName == "width") laWidth = GetValue(xRoot.childNodes[i], IsActiveX);
			if(xRoot.childNodes[i].nodeName == "height") laHeight = GetValue(xRoot.childNodes[i], IsActiveX);
		}
		
		arTitle = new Array();
		arWidth = new Array();
		arAlign = new Array();
		arType = new Array();
		
		var xNodes = xRoot.getElementsByTagName("column");
		for(var i = 0; i < xNodes.length; i++) {
			var ctitle, cwidth, calign, ctype = "";
			for(var j = 0; j < xNodes[i].childNodes.length; j++) {
				if(xNodes[i].childNodes[j].nodeName == "title") ctitle = GetValue(xNodes[i].childNodes[j], IsActiveX);
				if(xNodes[i].childNodes[j].nodeName == "width") cwidth = GetValue(xNodes[i].childNodes[j], IsActiveX);
				if(xNodes[i].childNodes[j].nodeName == "alignment") calign = GetValue(xNodes[i].childNodes[j], IsActiveX);
				if(xNodes[i].childNodes[j].nodeName == "type") ctype = GetValue(xNodes[i].childNodes[j], IsActiveX);
			}
			arTitle[i] = ctitle;
			arWidth[i] = cwidth;
			arAlign[i] = calign;
			arType[i] = ctype;
			tblWidth += (parseInt(cwidth) + 2);
		}

		var container = document.createElement("div");
		container.id = laName + "_container";
		if(browser == "IE") {
			container.runtimeStyle.width = laWidth + "px";
			container.runtimeStyle.height = laHeight + "px";
			container.runtimeStyle.border = "1px solid #a0a0a0";
			container.runtimeStyle.color = "#000000";
			container.runtimeStyle.backgroundColor = "#ffffff";
		} else {			 
			container.style.width = laWidth + "px";
			container.style.height = laHeight + "px";
			container.style.border = "1px solid #a0a0a0";
			container.style.color = "#000000";
			container.style.backgroundColor = "#ffffff";
		}
		
		var divhdr = document.createElement("div");
		divhdr.id = "spnHeader"
		if(browser == "IE") {
			divhdr.runtimeStyle.width = (laWidth - 18) + "px";
			divhdr.runtimeStyle.height = "24px";
			divhdr.runtimeStyle.overflow = "hidden";
		} else {
			divhdr.style.width = (laWidth - 18) + "px";
			divhdr.style.height = "24px";
			divhdr.style.overflow = "hidden";
		}
		
		var tblhdr = document.createElement("table");
		tblhdr.id = "tblHeader";
		tblhdr.cellPadding = 2;
		tblhdr.cellSpacing = 2;
		tblhdr.border = 0;
		tblhdr.width = tblWidth + "px";
		
		if(browser == "IE") {
			tblhdr.runtimeStyle.tableLayout = "fixed";
			tblhdr.runtimeStyle.width = tblWidth + "px";
		} else {
			tblhdr.style.tableLayout = "fixed";
			tblhdr.style.width = tblWidth + "px";
		}
		
		var hdrbody = document.createElement("tbody");
		var htr = document.createElement("tr");

		for(var i = 0; i < arTitle.length; i++) {
			var hdiv = document.createElement("div");
			if(browser == "IE") {
				hdiv.runtimeStyle.display = "block";
				hdiv.runtimeStyle.width = arWidth[i] + "px";
				hdiv.runtimeStyle.fontWeight = "bold";
				hdiv.runtimeStyle.whiteSpace = "nowrap";
				hdiv.runtimeStyle.textAlign = arAlign[i];
			} else {
				hdiv.style.display = "block";
				hdiv.style.width = arWidth[i] + "px";
				hdiv.style.fontWeight = "bold";
				hdiv.style.whiteSpace = "nowrap";
				hdiv.style.textAlign = arAlign[i];
			}
			hdiv.innerHTML = arTitle[i];
			
			var htd = document.createElement("td");
			if(browser == "IE") {
				htd.runtimeStyle.borderLeft = "1px solid #e0e0e0";
				htd.runtimeStyle.borderTop = "1px solid #e0e0e0";
				htd.runtimeStyle.borderRight = "1px solid #808080";
				htd.runtimeStyle.borderBottom = "1px solid #808080";
				htd.runtimeStyle.padding = "1px";
				htd.runtimeStyle.color = "#000000";
				htd.runtimeStyle.backgroundColor = "#c0c0c0";
			} else {
				htd.style.borderLeft = "1px solid #e0e0e0";
				htd.style.borderTop = "1px solid #e0e0e0";
				htd.style.borderRight = "1px solid #808080";
				htd.style.borderBottom = "1px solid #808080";
				htd.style.padding = "1px";
				htd.style.color = "#000000";
				htd.style.backgroundColor = "#c0c0c0";
			}

			htd.appendChild(hdiv);
			htr.appendChild(htd);
		}
		
		if(tblWidth < laWidth) {
			var i = arTitle.length;
			arTitle[i] = "&nbsp;";
			arWidth[i] = parseInt(laWidth) - parseInt(tblWidth);
			arAlign[i] = "left";
			arType[i] = "filler";
			
			var hdiv = document.createElement("div");
			if(browser == "IE") {
				hdiv.runtimeStyle.display = "block";
				hdiv.runtimeStyle.width = arWidth[i] + "px";
				hdiv.runtimeStyle.fontWeight = "bold";
				hdiv.runtimeStyle.whiteSpace = "nowrap";
				hdiv.runtimeStyle.textAlign = arAlign[i];
			} else {
				hdiv.style.display = "block";
				hdiv.style.width = arWidth[i] + "px";
				hdiv.style.fontWeight = "bold";
				hdiv.style.whiteSpace = "nowrap";
				hdiv.style.textAlign = arAlign[i];
			}
			hdiv.innerHTML = arTitle[i];
			
			var htd = document.createElement("td");
			if(browser == "IE") {
				htd.runtimeStyle.borderLeft = "1px solid #e0e0e0";
				htd.runtimeStyle.borderTop = "1px solid #e0e0e0";
				htd.runtimeStyle.borderRight = "1px solid #808080";
				htd.runtimeStyle.borderBottom = "1px solid #808080";
				htd.runtimeStyle.padding = "1px";
				htd.runtimeStyle.color = "#000000";
				htd.runtimeStyle.backgroundColor = "#c0c0c0";
			} else {
				htd.style.borderLeft = "1px solid #e0e0e0";
				htd.style.borderTop = "1px solid #e0e0e0";
				htd.style.borderRight = "1px solid #808080";
				htd.style.borderBottom = "1px solid #808080";
				htd.style.padding = "1px";
				htd.style.color = "#000000";
				htd.style.backgroundColor = "#c0c0c0";
			}

			htd.appendChild(hdiv);
			htr.appendChild(htd);
		}			
			
		hdrbody.appendChild(htr);
		tblhdr.appendChild(hdrbody);				
		divhdr.appendChild(tblhdr);
		container.appendChild(divhdr);
 
		var divdata = document.createElement("div");
		divdata.id = "spnData";
		if(browser == "IE") {
			divdata.runtimeStyle.width = laWidth + "px";
			divdata.runtimeStyle.height = (laHeight - 24) + "px";
			divdata.runtimeStyle.overflow = "scroll";
		} else {
			divdata.style.width = laWidth + "px";
			divdata.style.height = (laHeight - 24) + "px";
			divdata.style.overflow = "scroll";
		}		
		divdata.setAttribute("onscroll", "setHeaders();");

		var tbldata = document.createElement("table");
		tbldata.id = "tblData";
		tbldata.cellPadding = 2;
		tbldata.cellSpacing = 2;
		tbldata.border = 0;
		tbldata.width = tblWidth + "px";
		
		if(browser == "IE") {
			tbldata.runtimeStyle.tableLayout = "fixed";
			tbldata.runtimeStyle.width = tblWidth + "px";
		} else {
			tbldata.style.tableLayout = "fixed";
			tbldata.style.width = tblWidth + "px";
		}
		
		var flag = true, curColour = "";
		var databody = document.createElement("tbody");
		var rNodes = xRoot.getElementsByTagName("row");
		
		for(var i = 0; i < rNodes.length; i++) {

			flag = (flag == false);
			if(flag) { 
				curColour = "#ffffff"; 
			} else { 
				curColour = "#f0f0f0"; 
			}

			var dtr = document.createElement("tr");
			dtr.id = rNodes[i].getAttribute("id");
			if(browser == "IE") {
				dtr.runtimeStyle.backgroundColor = curColour;
			} else {
				dtr.style.backgroundColor = curColour;
			}
			dtr.setAttribute("onclick", "doEditRecord('" + rNodes[i].getAttribute("id") + "');");
		
			var fNodes = rNodes[i].getElementsByTagName("col");
			for(var j = 0; j < arTitle.length; j++) {
				var ddiv = document.createElement("div");
				if(browser == "IE") {
					ddiv.style.display = "block";
					ddiv.style.width = arWidth[j] + "px";
					ddiv.style.whiteSpace = "nowrap";
					ddiv.style.textAlign = arAlign[j];
				} else {
					ddiv.style.display = "block";
					ddiv.style.width = arWidth[j] + "px";
					ddiv.style.whiteSpace = "nowrap";
					ddiv.style.textAlign = arAlign[j];
				}
				if((arType[j] == "FILLER") || (arType[j] == "filler")) {
					ddiv.innerHTML = "&nbsp;"; 
				} else {
					var curText = GetValue(fNodes[j], IsActiveX);
					if((arType[j] == "CHECK") || (arType[j] == "check")) {
						if(curText == "Y") {
							ddiv.innerHTML = "<img src=images/checked.png border=0>";
						} else {
							ddiv.innerHTML = "<img src=images/unchecked.png border=0>";
						}
					} else {
						ddiv.innerHTML = curText;
					}
				}
				
				var dtd = document.createElement("td");
				dtd.appendChild(ddiv);
				dtr.appendChild(dtd);
			}
			databody.appendChild(dtr);
		
		}
						
		tbldata.appendChild(databody);
		divdata.appendChild(tbldata);								
		container.appendChild(divdata);
		parObj.appendChild(container);
		
		document.getElementById("spnData").onclick = select;		
	
	//} catch(e) {
	//	alert(e.message);
	//}

}
