// The following objects are used for XML-based operations
var httpObj = new ActiveXObject("Microsoft.XMLHTTP");
var domObj = new ActiveXObject("Microsoft.XMLDOM");

// Globals
var oPopup = window.createPopup();
var RefreshInProgress = false;
var elArray = new Array();
var elDisabledByArray = new Object();
var ResizeInProgress = false;
var changeFlag = false;

// basic timer class
var timer = {
  time: 0,
  now: function(){ return (new Date()).getTime(); },
  start: function(){ this.time = this.now(); },
  since: function(){ return this.now()-this.time; }
}

// dump is a debug function for showing all properties belonging to a JS Object (quite useful!)
var props = null;

function checkDump(event) {
	if ((event.altKey) && (event.ctrlKey)) {
		dump(event.srcElement);
	}
}

function dump(theObj, bDrillDown) {
	var tx = "<body topmargin='0' leftmargin='0' style='background-color: lightyellow; text-align: right;'>"
	
	if (theObj == null) {
		props.unset('Idx' + (props.size() - 1));
		var setKey = 'Idx' + (props.size() - 1);
	}
	else {
		if (!bDrillDown) {
			props = new Hash();
		}
		var setKey = 'Idx' + props.size();
		props.set(setKey, $H(theObj));
	}

	if (props.size() > 1) {
		tx += "<input id='BackButton' type='button' value='Back' onclick='window.opener.dump(null);'>";
	}
	tx += "<div style='overflow-y: scroll; height: expression(300 - (typeof document.all.BackButton == \"undefined\" ? 0 : document.all.BackButton.offsetHeight)); width: 400;'><table width='100%'>";
	props.get(setKey).keys().sort().each(function(s) {
		prop = s;
		if ((typeof props.get(setKey).get(s) == "object") && (props.get(setKey).get(s) != null)) {
			prop = "<a href='javascript: window.opener.dump(window.opener.props.get(\"" + setKey + "\").get(\"" + s + "\"), true);'>" + s + "</a>";
		}
		tx += "<tr><td valign='top'>" + prop + "</td><td>" + String(props.get(setKey).get(s)).stripScripts().stripTags() + "</td></tr>";
	});
	tx += "</table></div></body>";
	
	var debugWindow = window.open("", null, "height=300, width=400, status=no, toolbar=no, menubar=no, location=no, scrollbars=no");
	debugWindow.document.write(tx);
	debugWindow.document.close();
}

/* The purge function takes a reference to a DOM element as an argument. It loops through the element's attributes. If it finds any functions, it nulls them out. This breaks the cycle, allowing memory to be
reclaimed. It will also look at all of the element's descendent elements, and clear out all of their cycles as well. The purge function is harmless on Mozilla and Opera. It is essential on IE. The purge
function should be called before removing any element, either by the removeChild method, or by setting the innerHTML property. */
function purge(d) {
	var a = d.attributes, i, l, n;
	if (a) {
		l = a.length;
		for (i = 0; i < l; i += 1) {
			n = a[i].name;
			if (typeof d[n] === 'function') {
				d[n] = null;
			}
		}
	}
	a = d.childNodes;
	if (a) {
		l = a.length;
		for (i = 0; i < l; i += 1) {
			purge(d.childNodes[i]);
		}
	}
}

/* function to create "sets" that can be inspected like;  var foundInTheSet = theValue in set(2, 3, 4, 7, 9); */
function set() {
	var result = {};

	for (var i = 0; i < arguments.length; i++)
		result[arguments[i]] = true;

	return result;
}

/* function to dynamically load an external script into a document */
function loadScript(docEl, url) {
	var e = docEl.createElement("script");
	e.src = url;
	e.type = "text/javascript";
	docEl.getElementsByTagName("head")[0].appendChild(e);
}

function checkChanges() {
	if (changeFlag) {
		if (confirm("Changes Made!  Do you want to proceed without saving?")) {
			changeFlag = false;
			return true;
		}
		return false;
	}
	return true;
}

function checkUnload() {
	var el = document.elementFromPoint(event.clientX, event.clientY);
	if ((el != null) && (el.tagName == "A") && (el.name == "TabBar")) {
		return;
	}
	if (changeFlag) {
		event.returnValue = "Data has been changed.";
	}
}

function alertFade(strMessage, iTime) {
	if (iTime == null) {
		iTime = 2;
	}
	if (oPopup.isOpen) {
		var el = oPopup.document.getElementById("Alert");
		if (el != null) {
			var opacity = parseInt(el.opacity) - 1;
			if (opacity >= 0) {
				el.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity + ")";
				el.opacity = opacity;
				window.setTimeout("alertFade('" + strMessage + "', " + iTime + ")", parseInt(((iTime / 2) * 1000) / 100));
			}
			else {
				oPopup.hide();
			}
		}
	}
	else if (strMessage != "") {
		oPopup.document.body.innerHTML = "<div id='Alert' opacity=100 style='position: absolute; left: 0px; top: 0px; height: 100%; background-color: buttonface; border:2px activeborder outset; font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: black;'><div id='AlertTitleBar' style='background-color: activecaption; font-weight: bold; color: white; padding: 2px; filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=100, FinishOpacity=30, Style=1, StartX=0, FinishX=100, StartY=0, FinishY=0)'>iTradeNetwork</div><div id='AlertMessage' style='text-align: center; padding: 20px;'>" + strMessage + "</div></div>";
		oPopup.show(0, 0, 1000, 10);
		var realHeight = oPopup.document.body.scrollHeight;
		var realWidth = oPopup.document.body.scrollWidth;
		var el = oPopup.document.getElementById("AlertTitleBar");
		el.style.width = realWidth - 4;
		oPopup.hide();
		oPopup.show((screen.width / 2) - (realWidth / 2), (screen.height / 2) - (realHeight / 2), realWidth, realHeight);
		if ((!isNaN(parseInt(iTime))) && (parseInt(iTime) > 0)) {
			window.setTimeout("alertFade('', " + iTime + ")", parseInt(iTime * 1000));
		}
	}
}

function showPopupHelp(strHelp, width, autoWidth) {
	if (autoWidth == null) {
		autoWidth = false;
	}
	if (autoWidth) {
		width = "";
	}
	else {
		if (width == null) {
			width = 100;
		}
	}
	oPopup.document.body.innerHTML = "<span id='Help' style='position:absolute; left:0px; top:0px; width:" + width + "; height:100%; border:1px solid Navy; font:normal 10pt tahoma; padding:2px; font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: 008080; filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=lightblue, EndColorStr=white)'>" + strHelp + "</span>";
	// Display "dummy" popup to get the real height and maybe height, then hide it and open the real one.
	oPopup.show(0, 0, 1000, 10);
	var realHeight = oPopup.document.body.scrollHeight;
	if (autoWidth) {
		width = oPopup.document.body.scrollWidth;
	}
	oPopup.hide();
	oPopup.show(0, 22, width, realHeight, event.srcElement);
}

function showPopupText(e) {
	var header, strval;

	header = "<div id='header' style='padding-right: 15px; width: 100%; height: 15px; text-align:right; background:326598; filter: Alpha(Opacity=10, FinishOpacity=100, Style=1, StartX=100, StartY=0, FinishX=0, FinishY=0);'><div style='position:absolute;'><label style='cursor: hand;' onclick='parent.oPopup.hide();'><font face='Wingdings' size='3' color='navy'>ý</font></label></div></div>"
	strval = e.innerHTML.replace(/\n/g, "<br>")
	oPopup.document.body.innerHTML = header + "<span style='word-wrap: break-word; overflow-y:scroll; width:100%; height:expression(document.body.clientHeight - document.all.header.offsetHeight); border:1px solid Navy; font:normal 10pt tahoma; padding:2px scrollbar-3d-light-color:999999; scrollbar-arrow-color:maroon; scrollbar-base-color:326598; scrollbar-dark-shadow-color:red; scrollbar-face-color:white; scrollbar-highlight-color:white; scrollbar-shadow-color:white; scrollbar-track-color:F4F4F4;border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: 008080;'>" + strval + "</span>";
	oPopup.show(0 , -1 * (e.offsetHeight * 3), e.offsetWidth, e.offsetHeight * 4, e)
}

function truncateText(e, len) {
	var textTruncated = false;

	if (e.truncated == 1) {
		return 'none';
	}
	e.truncated = 0;
	var trunc = Trim(e.innerText);
	if ((trunc.length > 0) && (trunc.length > len)) {
		var truncwholewords = e.getAttribute("truncatewholewords");
		trunc = trunc.substring(0, len);
		if (truncwholewords == 1) {
			trunc = Trim(trunc.replace(/\w+$/, ''));
		}
		trunc += "...";
		textTruncated = true;
	}
	if (e.previousSibling != null) {
		if (e.previousSibling.innerText != trunc) {
			e.style.cursor = "auto";
			if (textTruncated) {
				e.style.cursor = "help";
				e.truncated = 1;
			}
			e.previousSibling.mergeAttributes(e);
			e.previousSibling.dataFld = "";
			e.previousSibling.innerText = trunc;
			e.previousSibling.style.display = '';
		}
		return 'none';
	}
	return '';
}

function showPopupWindow(el, closeFlag) {
	var mode, PUdivobj, elList, eventObj, srcElement = null;
	var hAlign, vAlign;
	
	if (closeFlag == null) {
		closeFlag = false;
	}
	if (closeFlag) {
		hideAllPopupWindows();
	}

	el.style.visibility = "hidden";
	el.style.display = "";
	bringTop(el);
	
	// Create "modal" mask, if needed
	if (el.getAttribute("modal") == "1") {
		var Divobj = document.createElement('DIV');
		Divobj.id = "PopupMask";
		Divobj.style.backgroundColor = "silver";
		Divobj.style.position = "absolute";
		Divobj.style.width = document.body.clientWidth;
		Divobj.style.height = document.body.scrollHeight > document.body.clientHeight ? document.body.scrollHeight : document.body.clientHeight;
		Divobj.style.top = 0;
		Divobj.style.left = 0;
		Divobj.style.zIndex = el.style.zIndex;
		el.style.zIndex = Divobj.style.zIndex + 1;
		Element.extend(Divobj).setStyle({opacity: 0.10});
		if (document.main == null) {
			document.body.insertAdjacentElement("beforeEnd", Divobj);
		}
		else {
			document.main.insertAdjacentElement("beforeEnd", Divobj);
		}
		new fx.Style(Divobj, 'opacity', {duration: 500, transition: fx.Transitions.linear}).custom(0.10, 0.40);
	}

	// check minimum height property
	if ((el.minheight == "") || (!IsNumeric(el.minheight))) {
		el.minheight = 0;
	}
	// check minimum width property
	if ((el.minwidth == "") || (!IsNumeric(el.minwidth))) {
		el.minwidth = 0;
	}
	
	hAlign = Trim(el.getAttribute("halign").toUpperCase());
	vAlign = Trim(el.getAttribute("valign").toUpperCase());
	if (window.event == null) {
		eventObj = document.createEventObject();
		srcElement = document.elementFromPoint(eventObj.clientX, eventObj.clientY);
	}
	else {
		srcElement = window.event.srcElement;
	}
	if ((srcElement == null) || (el.getAttribute("center") == "1")) {
		if (hAlign == "") {
			hAlign = "CENTER";
		}
		if (vAlign == "") {
			vAlign = "MIDDLE";
		}
	}
	
	mode = el.getAttribute("popupmode");
	switch (mode) {
		case "FLOAT":
			switch (hAlign) {
				case "LEFT":
					el.style.left = 0;
					break;
				
				case "CENTER":
					el.style.left = (document.body.clientWidth / 2) - (el.offsetWidth / 2);
					break;
				
				case "RIGHT":
					el.style.left = document.body.clientWidth - el.offsetWidth;
					break;
					
				default:
					el.style.left = getFloatPosition(srcElement, false, el) + srcElement.offsetWidth;
			}
			
			switch (vAlign) {
				case "TOP":
					el.style.top = 0;
					break;
				
				case "MIDDLE":
					el.style.top = ((document.body.clientHeight / 2) - (el.offsetHeight / 2)) + document.body.scrollTop;
					break;
					
				case "BOTTOM":
					el.style.top = document.body.clientHeight - el.offsetHeight;
					break;
					
				default:
					el.style.top = getFloatPosition(srcElement, true, el) + srcElement.offsetHeight;
			}

			if (el.getAttribute("URL") == null) {
				if (((el.style.pixelLeft + el.offsetWidth) - document.body.clientWidth) > 0) {
					el.style.pixelLeft -= ((el.style.pixelLeft + el.offsetWidth) - document.body.clientWidth);
				}
				if (((el.style.pixelTop + el.offsetHeight) - (document.body.clientHeight + document.body.scrollTop)) > 0) {
					el.style.pixelTop -= ((el.style.pixelTop + el.offsetHeight) - (document.body.clientHeight + document.body.scrollTop));
				}
			}
			break;
			
		case "MOVEABLE":
			switch (hAlign) {
				case "LEFT":
					el.style.left = 0;
					break;
				
				case "CENTER":
					el.style.left = (document.body.clientWidth / 2) - (el.offsetWidth / 2);
					break;
				
				case "RIGHT":
					el.style.left = document.body.clientWidth - el.offsetWidth;
					break;
					
				default:
					el.style.left = getElementPosition(srcElement, false) + srcElement.offsetWidth;
			}
			
			switch (vAlign) {
				case "TOP":
					el.style.top = 0;
					break;
				
				case "MIDDLE":
					el.style.top = ((document.body.clientHeight / 2) - (el.offsetHeight / 2)) + document.body.scrollTop;
					break;
					
				case "BOTTOM":
					el.style.top = document.body.clientHeight - el.offsetHeight;
					break;
					
				default:
					el.style.top = getElementPosition(srcElement, true) + srcElement.offsetHeight;
			}
			
			if (el.getAttribute("URL") == null) {
				if (((el.style.pixelLeft + el.offsetWidth) - document.body.clientWidth) > 0) {
					el.style.pixelLeft -= ((el.style.pixelLeft + el.offsetWidth) - document.body.clientWidth);
				}
				if (((el.style.pixelTop + el.offsetHeight) - (document.body.clientHeight + document.body.scrollTop)) > 0) {
					el.style.pixelTop -= ((el.style.pixelTop + el.offsetHeight) - (document.body.clientHeight + document.body.scrollTop));
				}
			}
			break;
	}
	if (el.style.pixelLeft < 0)
		el.style.pixelLeft = 0;
	if (el.style.pixelTop < 0)
		el.style.pixelTop = 0;
		
	if (el.getAttribute("URL") != null) {
		el.firstChild.innerHTML = '<IFRAME id="' + el.id + 'frame" style="z-index: 100; width: ' + el.style.pixelWidth + '; height: ' + el.style.pixelHeight + '; position: relative;" marginWidth="0" marginHeight="0" src="' + el.getAttribute("URL") + '" frameBorder="0" scrolling="no" onload="fixPopup(this.id);"></IFRAME>';
	}
	else {
		if (el.getAttribute("FooterBarSet") == null) {
			addFooterbar(document, el);
		}
		if (el.getAttribute("TitleBarSet") == null) {
			addTitlebar(document, el);
		}
		if ((el.getAttribute("maximized") == 1) || (el.getAttribute("maximizedstate") == 1)) {
			maximizePopup(document, el, true);
		}
		el.style.visibility = "";

		var DivHeight = 0;
		var DivScrollHeight = 0;
		var ScrollDivCount = 0;
		PUdivobj = null;
		if (mode != "INLINE") {
			elList = el.getElementsByTagName('DIV');
			for (i = 0; i < elList.length; i++) {
				if (elList[i].parentElement == el) {
					PUdivobj = elList[i];
				}
				if (elList[i].id == "scrolldiv") {
					elList[i].setAttribute("insidePopup", 1);
					elList[i].style.width = elList[i].parentElement.clientWidth;
					DivHeight += elList[i].clientHeight;
					DivScrollHeight += elList[i].scrollHeight;
					ScrollDivCount ++;
				}
			}
		}
		else {
			resizeDiv();
			resizeHeader();
		}

		var adj = 0;
		var origheight = el.originalheight;
		if ((PUdivobj != null) && (origheight != "")) {
			// resize popup if contents are less than height or add scrollers if more
			var minheight = el.minheight;
			if ((PUdivobj.offsetHeight + adj) < (minheight + adj)) {
				PUdivobj.style.height = parseInt(minheight) + adj;
			}
			else {
				if ((el.scrollable == "1")) {
					if (PUdivobj.scrollHeight > origheight) {
						PUdivobj.style.height = origheight;
						PUdivobj.style.overflowY = "scroll";
					}
				}
				else {
					if (DivHeight > DivScrollHeight) {
						DivHeight = DivScrollHeight;
					}
					var Margin = PUdivobj.scrollHeight - DivHeight;
					DivScrollHeight += (Margin + adj);
					DivHeight = DivScrollHeight > origheight ? origheight : DivScrollHeight;
					PUdivobj.style.height = DivHeight;
					if (ScrollDivCount > 0) {
						for (i = 0; i < elList.length; i++) {
							if (elList[i].id == "scrolldiv") {
								elList[i].style.height = ((DivHeight - Margin) / ScrollDivCount);
							}
						}
					}
				}
				el.style.height = PUdivobj.style.height;
			}
		}
	}
}

function fixPopup(strID) {
	var pEl, el, h, w, minh, minw, hAlign, vAlign;
	
	if (event.srcElement == null) {
		return;
	}
	
	pEl = document.parentWindow.document.getElementById(strID.replace('frame', ''));
	if (pEl != null) {
		// move the border to inside the iFrame
		if (pEl.getAttribute("popupmode") != "INLINE") {
			pEl.children[0].style.border = "";
			document.frames(strID).document.body.style.border = "2px solid #4985C2";
		}
		addTitlebar(document.frames(strID).document, pEl);
		document.frames(strID).document.body.onmousedown = function () {
			bringTop(pEl);
		};
	}
	
	el = document.getElementById(strID);
	if ((el.parentElement.parentElement.getAttribute("maximized") == 1) || (el.parentElement.parentElement.getAttribute("maximizedstate") == 1)) {
		maximizePopup(document.frames(strID).document, el.parentElement.parentElement, true);
	}
	else {
		// reset popup to original height/width
		h = el.parentElement.parentElement.originalheight;
		if ((h == "") || (!IsNumeric(h))) {
			h = el.parentElement.parentElement.style.pixelHeight;
		}
		el.style.removeExpression("pixelHeight");
		el.style.pixelHeight = h;
		el.parentElement.parentElement.style.pixelHeight = h;
		w = el.parentElement.parentElement.originalwidth;
		if ((w == "") || (!IsNumeric(w))) {
			w = el.parentElement.parentElement.style.pixelWidth;
		}
		el.style.removeExpression("pixelWidth");
		el.style.pixelWidth = w;
		el.parentElement.parentElement.style.pixelWidth = w;
		
		// get minimum dimensions
		minh = el.parentElement.parentElement.minheight;
		minw = el.parentElement.parentElement.minwidth;
	
		// resize popup if contents are less than height or add scrollers if more
		if (document.frames(strID).document.main != null) {
			if ((el.parentElement.parentElement.scrollable == "1") && (document.frames(strID).document.main.offsetHeight > document.frames(strID).document.body.offsetHeight)) {
				document.frames(strID).document.body.scroll = "yes";
			}
			else {
				el.style.setExpression("pixelHeight", "doHeightResize(document.frames('" + strID +"').document, " + h + ", this, 7, " + minh + ")");
			}
		}
		// resize popup if contents are greater than width
		if (document.frames(strID).document.main != null) {
			if (document.frames(strID).document.main.offsetWidth > document.frames(strID).document.body.offsetWidth) {
				el.style.setExpression("pixelWidth", "doWidthResize(document.frames('" + strID +"').document.main, this, 4, " + minw + ")");
			}
		}
		
		// Re-calculate popup position using re-sized dimensions
		hAlign = Trim(el.parentElement.parentElement.getAttribute("halign").toUpperCase());
		vAlign = Trim(el.parentElement.parentElement.getAttribute("valign").toUpperCase());
		if (el.parentElement.parentElement.getAttribute("center") == "1") {
			if (hAlign == "") {
				hAlign = "CENTER";
			}
			if (vAlign == "") {
				vAlign = "MIDDLE";
			}
		}
		switch (hAlign) {
			case "LEFT":
				el.parentElement.parentElement.style.left = 0;
				break;
			
			case "CENTER":
				el.parentElement.parentElement.style.left = (document.body.clientWidth / 2) - (el.parentElement.parentElement.style.pixelWidth / 2);
				break;
			
			case "RIGHT":
				el.parentElement.parentElement.style.left = document.body.clientWidth - el.parentElement.parentElement.style.pixelWidth;
				break;
				
			default:
				if (((el.parentElement.parentElement.style.pixelLeft + el.parentElement.parentElement.style.pixelWidth) - document.body.clientWidth) > 0) {
					el.parentElement.parentElement.style.pixelLeft -= ((el.parentElement.parentElement.style.pixelLeft + el.parentElement.parentElement.style.pixelWidth) - document.body.clientWidth);
				}
		}
		
		switch (vAlign) {
			case "TOP":
				el.parentElement.parentElement.style.top = 0;
				break;
			
			case "MIDDLE":
				el.parentElement.parentElement.style.top = ((document.body.clientHeight / 2) - (el.parentElement.parentElement.style.pixelHeight / 2)) + document.body.scrollTop;
				break;
				
			case "BOTTOM":
				el.parentElement.parentElement.style.top = document.body.clientHeight - el.parentElement.parentElement.style.pixelHeight;
				break;
				
			default:
				if (((el.parentElement.parentElement.style.pixelTop + el.parentElement.parentElement.style.pixelHeight) - (document.body.clientHeight + document.body.scrollTop)) > 0) {
					el.parentElement.parentElement.style.pixelTop -= ((el.parentElement.parentElement.style.pixelTop + el.parentElement.parentElement.style.pixelHeight) - (document.body.clientHeight + document.body.scrollTop));
				}
		}
	}
	if (el.parentElement.parentElement.style.pixelLeft < 0)
		el.parentElement.parentElement.style.pixelLeft = 0;
	if (el.parentElement.parentElement.style.pixelTop < 0)
		el.parentElement.parentElement.style.pixelTop = 0;

	el.parentElement.parentElement.style.visibility = "";
	if (document.frames(strID).resizeDiv) {
		document.frames(strID).resizeDiv();
	}
}

function doHeightResize(frameEl, origheight, el, adj, minheight) {
	if ((frameEl == null) || (frameEl.main == null) || ((frameEl.main.offsetHeight + adj) > origheight)) {
		el.parentElement.parentElement.style.pixelHeight = origheight;
	}
	else if ((frameEl.main.offsetHeight + adj) < (minheight + adj)) {
		el.parentElement.parentElement.style.pixelHeight = minheight + adj;
	}
	else {
		var DivHeight = 0;
		var DivScrollHeight = 0;
		var els = frameEl.getElementsByName("scrolldiv");
		if (els.length > 0) {
			for (var i = 0; i < els.length; i++) {
				DivHeight += els[i].clientHeight;
				DivScrollHeight += els[i].scrollHeight;
			}
		}
		if (DivHeight > DivScrollHeight) {
			DivHeight = DivScrollHeight;
		}
		var Margin = frameEl.main.scrollHeight - DivHeight;
		DivScrollHeight += (Margin + adj);
		el.parentElement.parentElement.style.pixelHeight = DivScrollHeight > origheight ? origheight : DivScrollHeight;
	}
	return el.parentElement.parentElement.style.pixelHeight;
}

function doWidthResize(frameEl, el, adj, minwidth) {
	if (frameEl == null) {
		return el.style.pixelWidth;
	}
	else if ((frameEl.offsetWidth + adj) < (minwidth + adj)) {
		return minwidth + adj;
	}
	else {
		return frameEl.offsetWidth + adj;
	}
}

function addTitlebar(docEl, popupEl) {
	var PUdivobj, PUdivobj2, PUbtnobj;
	
	PUdivobj = docEl.createElement('DIV');
	if (popupEl.getAttribute("popupmode") == "MOVEABLE") {
		PUdivobj.onmousedown = function () {
			var eventObj = document.createEventObject();
			initiateMove(PUdivobj, eventObj, popupEl);
		};
		PUdivobj.onmouseup = function () {
			endMove(PUdivobj);
		};
		PUdivobj.onmousemove = function () {
			var eventObj = document.createEventObject();
			Move(eventObj);
		};
	}
	else {
		return;
	}
	PUdivobj.id = "grabbar";
	PUdivobj.style.cursor = "move";
	PUdivobj.style.textAlign = "right";
	PUdivobj.style.height = "23px";
	PUdivobj.style.width = "100%";
	PUdivobj.style.padding = "1px";
	PUdivobj.style.background = "lightblue";
	PUdivobj.style.filter = "Alpha(Opacity=0, FinishOpacity=100, Style=1, StartX=100, StartY=0, FinishX=0, FinishY=0)";
	PUdivobj2 = docEl.createElement('DIV');
	PUdivobj2.style.width = PUdivobj2.style.height = "100%";
	PUdivobj2.style.position = "relative";
	PUdivobj2.style.borderBottom = "3px inset #4985C2";
	PUdivobj2.ondblclick = function () {
		maximizePopup(docEl, popupEl);
	};
	PUbtnobj = docEl.createElement('BUTTON');
	PUbtnobj.id = "MaxPopup";
	PUbtnobj.value = "1";
	PUbtnobj.title = "Maximize";
	PUbtnobj.style.height = "17px";
	PUbtnobj.style.width = "17px";
	PUbtnobj.style.background = "3366CC";
	PUbtnobj.style.filter = "progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#3366CC', EndColorStr='#ffffff')";
	PUbtnobj.style.border = "1px solid";
	PUbtnobj.style.borderColor = "#c4cccc #acb5b5 #6f7777 #acb5b5";
	PUbtnobj.style.fontFamily = "webdings";
	PUbtnobj.style.fontSize = "8px";
	PUbtnobj.style.fontWeight = "bold";
	PUbtnobj.style.paddingLeft = "2px";
	PUbtnobj.style.marginLeft = "5px";
	PUbtnobj.style.cursor = "hand";
	PUbtnobj.style.color = "white";
	PUbtnobj.onclick = function () {
		maximizePopup(docEl, popupEl);
	};
	PUdivobj2.appendChild(PUbtnobj);
	PUbtnobj = PUbtnobj.cloneNode(true);
	PUbtnobj.id = "ClosePopup";
	PUbtnobj.value = "X";
	PUbtnobj.title = "Close";
	PUbtnobj.style.background = "DC143C";
	PUbtnobj.style.filter = "progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#DC143C', EndColorStr='#ffffff')";
	PUbtnobj.style.fontFamily = "Verdana,Geneva,Arial,Helvetica,sans-serif";
	PUbtnobj.style.fontSize = "9px";
	PUbtnobj.onclick = function () {
		hidePopupWindow(popupEl);
	};
	PUdivobj2.appendChild(PUbtnobj);
	PUdivobj.appendChild(PUdivobj2);
	if (popupEl.getAttribute("URL") != null) {
		for (var i = 0; i < docEl.forms.length; i++) {
			docEl.forms[i].style.margin = 0;
		}
		if (docEl.main != null) {
			docEl.main.insertAdjacentElement("afterBegin", PUdivobj);
			if (NiftyCheck()) {
				Rounded(docEl.body, "top", "transparent", "#4985C2");
			}
		}
	}
	else {
		popupEl.firstChild.insertAdjacentElement("afterBegin", PUdivobj);
		if (NiftyCheck()) {
			popupEl.firstChild.style.background = popupEl.style.background;
			popupEl.style.background = "transparent";
			popupEl.style.width = popupEl.firstChild.offsetWidth;
			Rounded(popupEl.firstChild, "top", "transparent", "#4985C2");
		}
	}
	popupEl.setAttribute("TitleBarSet", "1");
}

function maximizePopup(docEl, popupEl, bForceMax) {
	if (bForceMax == null) {
		bForceMax = false;
	}
	if ((popupEl.getAttribute("maximizedstate") == null) || (popupEl.getAttribute("maximizedstate") == 0) || (bForceMax)) {
		// maxmize window
		var currentScroll = document.body.scroll == "" ? "no" : document.body.scroll;
		if (popupEl.getAttribute("currentleft") == null) {
			popupEl.setAttribute("currentleft", popupEl.style.left);
			popupEl.setAttribute("currenttop", popupEl.style.top);
			var w = popupEl.getAttribute("originalwidth") == null ? popupEl.clientWidth : popupEl.getAttribute("originalwidth");
			popupEl.setAttribute("currentwidth", w);
			var h = popupEl.getAttribute("originalheight") == null ? popupEl.clientHeight : popupEl.getAttribute("originalheight");
			popupEl.setAttribute("currentheight", h);
		}
		popupEl.style.left = 0;
		popupEl.style.top = 0;
		frameEls = $(popupEl).select('IFRAME');
		if (frameEls.length > 0) {
			var heightPadding = popupEl.getHeight() - frameEls[0].getHeight();
			frameEls[0].style.removeExpression("pixelHeight");
			frameEls[0].style.setExpression("pixelHeight", "doHeightResize(document.frames('" + frameEls[0].id +"').document, " + (document.viewport.getHeight() - heightPadding) + ", this, 7, " + popupEl.minheight + ")");
			frameEls[0].style.removeExpression("pixelWidth");
			frameEls[0].style.width = "100%";
		}
		popupEl.style.removeExpression("pixelHeight");
		popupEl.style.setExpression("pixelHeight", "document.viewport.getHeight()");
		popupEl.style.removeExpression("pixelWidth");
		popupEl.style.setExpression("pixelWidth", "document.viewport.getWidth()");
		popupEl.setAttribute("maximizedstate", 1);
		document.body.scroll = currentScroll;
	}
	else {
		// restore window
		popupEl.style.left = popupEl.getAttribute("currentleft");
		popupEl.style.top = popupEl.getAttribute("currenttop");
		frameEls = $(popupEl).select('IFRAME');
		if (frameEls.length > 0) {
			var heightPadding = popupEl.getHeight() - frameEls[0].getHeight();
			frameEls[0].style.removeExpression("pixelHeight");
			frameEls[0].style.setExpression("pixelHeight", "doHeightResize(document.frames('" + frameEls[0].id +"').document, " + (popupEl.getAttribute("currentheight") - heightPadding) + ", this, 7, " + popupEl.minheight + ")");
			frameEls[0].style.removeExpression("pixelWidth");
			frameEls[0].style.width = "100%";
		}
		popupEl.style.removeExpression("pixelHeight");
		popupEl.style.pixelHeight = popupEl.getAttribute("currentheight");
		popupEl.style.removeExpression("pixelWidth");
		popupEl.style.pixelWidth = popupEl.getAttribute("currentwidth");
		popupEl.setAttribute("maximizedstate", 0);
	}
}

function addFooterbar(docEl, popupEl) {
	var PUdivobj, PUbtnobj;
	
	if (popupEl.getAttribute("resizeable") != 1) {
		return;
	}
	PUdivobj = docEl.createElement('DIV');
	PUdivobj.id = "footerbar";
	PUdivobj.style.textAlign = "right";
	PUdivobj.style.width = "100%";
	PUdivobj.style.padding = "1px";
	PUdivobj.style.background = "4985C2";
	PUbtnobj = docEl.createElement('SPAN');
	PUbtnobj.innerText = "v";
	PUbtnobj.title = "Resize";
	PUbtnobj.style.fontFamily = "wingdings";
	PUbtnobj.style.fontSize = "15px";
	PUbtnobj.style.fontWeight = "bold";
	PUbtnobj.style.verticalAlign = "bottom";
	PUbtnobj.style.cursor = "se-resize";
	PUbtnobj.style.color = "4985C2";
	PUbtnobj.style.filter = "progid:DXImageTransform.Microsoft.Emboss()";
	PUbtnobj.style.width = "15px";
	PUbtnobj.onmousedown = function () {
		var eventObj = document.createEventObject();
		initiateResize(PUbtnobj, eventObj, popupEl);
	};
	PUbtnobj.onmouseup = function () {
		endResize(PUbtnobj);
	};
	PUbtnobj.onmousemove = function () {
		var eventObj = document.createEventObject();
		Resize(eventObj);
	};
	PUdivobj.appendChild(PUbtnobj);
	if (popupEl.getAttribute("URL") != null) {
		if (docEl.main != null) {
			docEl.main.insertAdjacentElement("beforeEnd", PUdivobj);
		}
	}
	else {
		popupEl.firstChild.insertAdjacentElement("beforeEnd", PUdivobj);
	}
	popupEl.setAttribute("FooterBarSet", "1");
}

function hidePopupWindow(el) {
	if (el == null) {
		el = document.getElementById("ClosePopup");
		if (el != null) {
			el.fireEvent("onclick");
		}
	}
	else {
		// Check if popup already closed, no point in closing it again
		if (el.style.display == "none") {
			return;
		}
		el.style.display = "none";
		if (el.getAttribute("popupmode") == 'MOVEABLE') {
			var PUdivobj = document.getElementById(el.PlaceHolderID);
			if (PUdivobj != null) {
				el.swapNode(PUdivobj);
				PUdivobj.removeNode(true);
			}
		}
		if (el.getAttribute("modal") == '1') {
			var PUdivobj = document.getElementById('PopupMask');
			if (PUdivobj != null) {
				PUdivobj.removeNode(true);
			}
		}
		resizeDiv();
		el.style.removeExpression("pixelHeight");
		el.style.removeExpression("pixelWidth");
		if (el.getAttribute("URL") != null) {
			el.firstChild.firstChild.style.removeExpression("pixelHeight");
			el.firstChild.firstChild.style.removeExpression("pixelWidth");
			el.firstChild.firstChild.src = '';
			el.firstChild.firstChild.removeNode(true);
		}
		if (Trim(el.getAttribute("onclose")) != "") {
			eval(el.getAttribute("onclose"));
		}
	}
}

function hideAllPopupWindows() {
	var pList, i;
	
	pList = document.getElementsByTagName("DIV");
	for (i = 0; i < pList.length; i++) {
		if (pList[i].getAttribute("popupmode") != null) {
			hidePopupWindow(pList[i]);
		}
	}
}

function getFloatPosition(el, isGetTop, popupEl) {
	var pos = 0;
	
	if (el != null) {
		pos = (isGetTop) ? el.offsetTop : el.offsetLeft;
		while (((el = el.offsetParent) != null)) {
			if ((el.tagName == "DIV") && (el.contains(popupEl))) {
				break;
			}
	    	pos += (isGetTop) ? el.offsetTop : el.offsetLeft;
			if ((el.tagName == "DIV") && (isGetTop)) {
				pos -= el.scrollTop;
			}
    	}
	}
	return pos;
}

function bringTop(el) {
	var i, pList, maxZ = 100;
	
	pList = document.getElementsByTagName("DIV");
	for (i = 0; i < pList.length; i++) {
		if (pList[i].getAttribute("popupmode") != null) {
			if (pList[i].style.zIndex >= maxZ) {
				maxZ = pList[i].style.zIndex + 1;
			}
		}
	}
	el.style.zIndex = maxZ;
}

function checkEnter() {
	var ecode;

	if (Trim(event.srcElement.value) != "") {
		if (event.keyCode == 13) {
			if ((checkUniqueness()) && (checkValidation())) {
				ecode = Trim(event.srcElement.getAttribute("onenter"));
				if (ecode != null) {
					eval(ecode.replace(/this/g, event.srcElement.id));
				}
			}
			event.returnValue = false;
		}
	}
}

function AddItemToDropDown(oDropDown, cValue, cText) {
	oOption = document.createElement('OPTION');
	oOption.value = cValue;
	oOption.text = cText;
	oDropDown.add(oOption);
	return oDropDown.options.length - 1;
}

function RemoveItemFromDropDown(oDropDown, i) {
	oDropDown.remove(i);
	return oDropDown.options.length - 1;
}

function ClearDropDown(oDropDown) {
	var i, j = 0;
	
	if (oDropDown.getAttribute("defaultset")) {
		j = 1;
	}
	oDropDown.selectedIndex = -1;
	for (i = oDropDown.options.length - 1; i >= j; i--) {
		oDropDown.remove(i);
	}
}

var checkElArray = null;
var checkStart = 0;

function checkElements() {
	var selectedObj;

	if (checkElArray == null) {
		checkElArray = document.main.elements;
	}
	
	checkIdx = checkStart;
	while (checkIdx < checkStart + 50) {
		if (checkIdx >= checkElArray.length) {
			checkElArray = null;
			checkStart = 0;
			return;
		}
		selectedObj = checkElArray[checkIdx];
		if ((checkIdx < checkElArray.length) && (selectedObj.disabled)) {
			switch (selectedObj.type) {
				case "button":
					if (selectedObj.getAttribute("disabledclass") != null) {
						if (selectedObj.className != selectedObj.disabledclass) {
							selectedObj.className = selectedObj.disabledclass;
						}
					}
					break;
			
				case "radio":
				case "checkbox":
					if ((selectedObj.previousSibling != null) && (selectedObj.previousSibling.previousSibling != null) && (selectedObj.previousSibling.previousSibling.tagName == "LABEL") && (!selectedObj.previousSibling.previousSibling.disabled)) {
						selectedObj.previousSibling.previousSibling.disabled = true;
						selectedObj.previousSibling.previousSibling.style.cursor = "default";
					}
					break;
					
				case "text":
				case "textarea":
					if (selectedObj.style.borderColor != "#cdcdcd") {
						selectedObj.style.borderColor = "CDCDCD";
						selectedObj.style.background = "F3F3F3";
						if (selectedObj.getAttribute("datefield") && selectedObj.getAttribute("hideFB") != 1) {
								var lel = selectedObj.previousSibling.previousSibling;
								var rel = selectedObj.nextSibling.nextSibling;
								lel.style.display = "none";
								rel.style.display = "none";
						}
					}
					break;
					
				case "hidden":
					if (selectedObj.getAttribute("timefield")) {
						selectedObj = selectedObj.parentElement.children[0];
						selectedObj.style.visibility = "hidden";
					}
					break;
			}
		}
		checkIdx++;
	}
	checkStart = checkIdx;
}

function KeyFilterStrip(KeyFilterTemp, KeyFilterMask) {
	KeyFilterMask = KeyFilterMask.replace(/[xX9]/gi, '');
	if (KeyFilterMask.length > 0) {
		var objRegex = new RegExp("[" + KeyFilterMask + "]", "gi");
		KeyFilterTemp = KeyFilterTemp.replace(objRegex, '');
	}
	return KeyFilterTemp;
}

var rFlag = false;

function KeyFilter() {
	if ((event.propertyName == "value") && (document.readyState == "complete")) {
		if (rFlag) {
			rFlag = false;
			return;
		}
		rFlag = true;
		var textbox = event.srcElement;
		var KeyFilterMask = textbox.getAttribute("mask");
		var oRng = document.selection.createRange();
		if (oRng.parentElement() == textbox) {
			oRng.text = "~";
		}
		var KeyFilterText = KeyFilterStrip(textbox.value, KeyFilterMask);
	    var KeyFilterFinal = "";
		for (var KeyFilterStep = 0; KeyFilterStep < KeyFilterMask.length; KeyFilterStep++) {
			if (KeyFilterText.length != 0) {
				if (KeyFilterText.charAt(0) == "~") {
					KeyFilterFinal = KeyFilterFinal + "~";
	                KeyFilterText = KeyFilterText.substring(1, KeyFilterText.length);
					KeyFilterStep--;
				}
				else {
					switch (KeyFilterMask.charAt(KeyFilterStep)) {
						case '9':
							if ((KeyFilterText.charCodeAt(0) == 46) || ((KeyFilterText.charCodeAt(0) > 47) && (KeyFilterText.charCodeAt(0) < 58))) {
								KeyFilterFinal = KeyFilterFinal + KeyFilterText.charAt(0);
							}
		    	            KeyFilterText = KeyFilterText.substring(1, KeyFilterText.length);
							break;

						case 'X':
						case 'x':
							if (KeyFilterMask.charAt(KeyFilterStep) == 'X') {
								KeyFilterFinal = KeyFilterFinal + KeyFilterText.charAt(0).toUpperCase();
							}
							else {
								KeyFilterFinal = KeyFilterFinal + KeyFilterText.charAt(0).toLowerCase();
							}
			                KeyFilterText = KeyFilterText.substring(1, KeyFilterText.length);
							break;

			        	default:
							KeyFilterFinal = KeyFilterFinal + KeyFilterMask.charAt(KeyFilterStep);
			        }
				}
			}
		}
		var i = KeyFilterFinal.search("~");
		if (i == -1) {
			i = KeyFilterFinal.length;
		}
		KeyFilterFinal = KeyFilterFinal.replace(/~/, "");
		var LastValue = textbox.getAttribute("lastvalue") != null ? KeyFilterStrip(textbox.getAttribute("lastvalue"), KeyFilterMask) : "";
		if (KeyFilterStrip(KeyFilterFinal, KeyFilterMask) != LastValue) {
			textbox.value = KeyFilterFinal;
			textbox.setAttribute("lastvalue", KeyFilterFinal);
		}
		else {
			textbox.value = textbox.getAttribute("lastvalue") != null ? textbox.getAttribute("lastvalue") : "";
		}
		window.setTimeout(function() {
			var range = textbox.createTextRange();
		    range.move('character', i);
		    range.select();
		}, 1);
	}
}

var copyEl = null;

function setCopy() {
	copyEl = event.srcElement;
}

function extendedCopy() {
	var el = null, i, Start, End;

	if (copyEl != null) {
		if ((Trim(copyEl.value) != "") && (event.shiftKey) && (copyEl.id == event.srcElement.id)) {
			el = document.getElementsByName(event.srcElement.id);
			if ((el != null) && (el.length != null)) {
				for (i = 0; i < el.length; i++) {
					if (el[i] == copyEl) {
						Start = i;
					}
					if (el[i] == event.srcElement) {
						End = i;
					}
				}
				if (Start < End) {
					for (i = Start; i <= End; i++) {
						el[i].value = copyEl.value;
					}
				}
				else {
					for (i = End; i <= Start; i++) {
						el[i].value = copyEl.value;
					}
				}
			}
		}
		else if ((Trim(copyEl.value) != "") && (event.ctrlKey) && (copyEl.id == event.srcElement.id)) {
			event.srcElement.value = copyEl.value;
		}
	}
}

function updateButton() {
	if (event.propertyName == "disabled") {
		if (event.srcElement.getAttribute("enabledclass") != null) {
			event.srcElement.className = event.srcElement.enabledclass;
		}
	}
}

function updateText() {
	if (event.propertyName == "disabled") {
		event.srcElement.style.borderColor = "";
		event.srcElement.style.background = "F4FAFF";
		if (event.srcElement.getAttribute("datefield") && event.srcElement.getAttribute("hideFB") != 1) {
				var lel = event.srcElement.previousSibling.previousSibling;
				var rel = event.srcElement.nextSibling.nextSibling;
				lel.style.display = "";
				rel.style.display = "";
		}
		else if (event.srcElement.getAttribute("timefield")) {
			var selectedObj = event.srcElement.parentElement.children[0];
			selectedObj.style.visibility = "visible";
		}
	}
}

function updateCheckRadio() {
	if (event.propertyName == "disabled") {
		event.srcElement.previousSibling.previousSibling.disabled = false;
		event.srcElement.previousSibling.previousSibling.style.cursor = "hand";
	}
	else if (event.propertyName == "checked") {
		if (event.srcElement.checked) {
			event.srcElement.previousSibling.previousSibling.innerText = event.srcElement.onchar;
		}
		else {
			event.srcElement.previousSibling.previousSibling.innerText = event.srcElement.offchar;
		}
	}
}

function checkDisable() {
	switch (event.propertyName) {
		case "value":
		case "checked":
			compareDisable(event.srcElement);
			break;
	}
}

function checkAllDisable() {
	var elList, j, checkedList, t, elType, elArray, bFound = false;
	
	delete elDisabledByArray;
	elDisabledByArray = new Object();

	for (t = 1; t <= 4; t++) {
		switch (t) {
			case 1:
				elType = "SELECT";
				break;
			case 2:
				elType = "INPUT";
				break;
			case 3:
				elType = "TEXTAREA";
				break;
			case 4:
				elType = "A";
				break;
		}
		elList = document.getElementsByTagName(elType);
		checkedList = "";
		for (j = 0; j < elList.length; j++) {
			if ((checkedList.indexOf("|" + elList[j].name + "|") == -1) && (elList[j].getAttribute("disabledby") != null) && (Trim(elList[j].getAttribute("disabledby")) != "")) {
				bFound = true;
				checkedList = checkedList.concat("|" + elList[j].name + "|");
				if (elDisabledByArray[Trim(elList[j].getAttribute("disabledby"))] == null) {
					elDisabledByArray[Trim(elList[j].getAttribute("disabledby"))] = new Array();
				}
				elArray = elDisabledByArray[Trim(elList[j].getAttribute("disabledby"))];
				elArray[elArray.length] = elList[j];
			}
		}
	}
	
	if (bFound) {
		for (t = 1; t <= 4; t++) {
			switch (t) {
				case 1:
					elType = "SELECT";
					break;
				case 2:
					elType = "INPUT";
					break;
				case 3:
					elType = "TEXTAREA";
					break;
				case 4:
					elType = "A";
					break;
			}
			elList = document.getElementsByTagName(elType);
			checkedList = "";
			for (j = 0; j < elList.length; j++) {
				if (checkedList.indexOf("|" + elList[j].name + "|") == -1) {
					checkedList = checkedList.concat("|" + elList[j].name + "|");
					compareDisable(elList[j]);
				}
			}
		}
	}
}

function compareDisable(el) {
	var selectedObj, bDisable, i, k, rList, lel, rel, elArray;

	elArray = elDisabledByArray[el.name];
	if (elArray == null) {
		return;
	}
	
	for (i = 0; i < elArray.length; i++) {
		selectedObj = elArray[i];
		if ((el.name != selectedObj.name) && (el.name == selectedObj.disabledby) && (Trim(selectedObj.disabledby) != "")) {
			if ((el.type == "radio") || (el.type == "checkbox")) {
				bDisable = true;
				rList = document.getElementsByName(el.name);
				for (k = 0; k < rList.length; k++) {
					if (rList[k].checked) {
						el = rList[k];
					}
				}
				if ((selectedObj.disabledvalue == selectedObj.enabledvalue) || (Trim(selectedObj.disabledvalue) != "")) {
					if ((el.checked) && (el.value != selectedObj.disabledvalue)) {
						bDisable = false;
					}
				}
				else {
					if ((el.checked) && (el.value == selectedObj.enabledvalue)) {
						bDisable = false;
					}
				}
			}
			else if (el.tagName == "SELECT") {
				if (el.selectedIndex == -1) {
					bDisable = true;
				}
				else {
					if ((selectedObj.disabledvalue == selectedObj.enabledvalue) || (Trim(selectedObj.disabledvalue) != "")) {
						if (el.options[el.selectedIndex].value == selectedObj.disabledvalue) {
							bDisable = true;
						}
						else {
							bDisable = false;
						}
					}
					else {
						if (el.options[el.selectedIndex].value == selectedObj.enabledvalue) {
							bDisable = false;
						}
						else {
							bDisable = true;
						}
					}
				}
			}
			else {
				if ((selectedObj.disabledvalue == selectedObj.enabledvalue) || (Trim(selectedObj.disabledvalue) != "")) {
					if (el.value == selectedObj.disabledvalue) {
						bDisable = true;
					}
					else {
						bDisable = false;
					}
				}
				else {
					if (el.value == selectedObj.enabledvalue) {
						bDisable = false;
					}
					else {
						bDisable = true;
					}
				}
			}
			if (selectedObj.tagName == "A") {
				if (bDisable) {
					selectedObj.href = selectedObj.disabledhref;
				}
				else {
					selectedObj.href = selectedObj.enabledhref;
				}
			}
			else if (selectedObj.type == "hidden") {
				selectedObj = getReal(selectedObj, "className", "select");
				if (selectedObj.tagName == "SPAN") {
					selectedObj = selectedObj.getElementsByTagName('TABLE');
					selectedObj = selectedObj[0];
				}
			}
			selectedObj.disabled = bDisable;
		}
	}
}

function finalizePage() {
	var elList, j, selectedObj, url, RefreshEl;
	var ns = 0, nf = 0, strSub = "";

	elList = document.getElementsByTagName('SPAN');
	for (j = 0; j < elList.length; j++) {
		RefreshEl = elList[j];
		if ((RefreshEl.className == "select") && (RefreshEl.getAttribute("listurl") != null)) {
			url = RefreshEl.getAttribute("listurl");
			ns = url.indexOf('|', ns);
			while (ns != -1) {
				nf = url.indexOf('|', ns + 1);
				strSub = url.substring(ns + 1, nf);
				selectedObj = eval("document.all." + strSub);
				if (selectedObj != null) {
					elArray[elArray.length] = RefreshEl;
					selectedObj.attachEvent("onpropertychange", checkRefresh);
				}
				ns = nf + 1;
				ns = url.indexOf('|', ns);
			}
			RefreshDSO(RefreshEl);
		}
		else if ((RefreshEl.className == "select") && (RefreshEl.getAttribute("listsrc") != null)) {
			if (Trim(RefreshEl.getAttribute("listsrcfilter")) != "") {
				if (document.main[RefreshEl.getAttribute("listsrcfilter")]) {
					selectedObj = eval("document.all." + RefreshEl.getAttribute("listsrcfilter"));
					if (selectedObj != null) {
						elArray[elArray.length] = RefreshEl;
						selectedObj.attachEvent("onpropertychange", checkRefresh);
					}
				}
			}
			RefreshDSO(RefreshEl);
		}
	}
	elList = document.getElementsByTagName('SELECT');
	for (j = 0; j < elList.length; j++) {
		RefreshEl = elList[j];
		if (RefreshEl.getAttribute("listurl") != null) {
			url = RefreshEl.getAttribute("listurl");
			ns = url.indexOf('|', ns);
			while (ns != -1) {
				nf = url.indexOf('|', ns + 1);
				strSub = url.substring(ns + 1, nf);
				selectedObj = eval("document.all." + strSub);
				if (selectedObj != null) {
					elArray[elArray.length] = RefreshEl;
					selectedObj.attachEvent("onpropertychange", checkRefresh);
				}
				ns = nf + 1;
				ns = url.indexOf('|', ns);
			}
			RefreshDSO(RefreshEl);
		}
		else if (RefreshEl.getAttribute("listsrc") != null) {
			if (Trim(RefreshEl.getAttribute("listsrcfilter")) != "") {
				selectedObj = eval("document.all." + RefreshEl.getAttribute("listsrcfilter"));
				if (selectedObj != null) {
					elArray[elArray.length] = RefreshEl;
					selectedObj.attachEvent("onpropertychange", checkRefresh);
				}
			}
			RefreshDSO(RefreshEl);
		}
	}
	elList = document.getElementsByTagName('INPUT');
	for (j = 0; j < elList.length; j++) {
		selectedObj = elList[j];
		if ((selectedObj.getAttribute("uniquedsn") != null) && (selectedObj.getAttribute("uniquetable") != null) && (selectedObj.getAttribute("uniquecolumn") != null)) {
			selectedObj.attachEvent("onblur", checkUniqueness);
		}
		if ((selectedObj.type == "text") && (selectedObj.getAttribute("autocompletelist") != null) && (selectedObj.getAttribute("autocompletelist") != "")) {
			var actbObj = actb(selectedObj, selectedObj.getAttribute("autocompletelist").split(","));
			actbObj.actb_timeOut = 10000;
			actbObj.actb_firstText = true;
			actbObj.actb_lim = -1;
			actbObj.actb_bgColor = "#4985C2";
			actbObj.actb_hColor = "#000000";
			actbObj.actb_fFamily = "Arial, Helvetica, sans-serif";
			actbObj.actb_fSize = "11px";
			actbObj.actb_hStyle = "text-decoration:underline; font-weight=bold";
			selectedObj.setAttribute("autocompletelist", "");
		}
	}
	checkElements();
	window.setInterval(checkElements, 40);
}

function checkRefresh() {
	var t, objReg;

	if (((event.propertyName == "value") || (event.propertyName == "checked")) && (document.readyState == "complete")) {
		for (t = 0; t < elArray.length; t++) {
			if (elArray[t].getAttribute("listsrc") != null) {
				RefreshDSO(elArray[t]);
			}
			else {
				objReg = new RegExp("\\|" + event.srcElement.name + "\\|", "i");
				if ((!RefreshInProgress) && (elArray[t].getAttribute("listurl").search(objReg) != -1)) {
					RefreshDSO(elArray[t]);
				}
			}
		}
	}
}

function RefreshDSO(RefreshEl) {
	var ns = 0, nf = 0, strSub = "", url, lasturl, listsrc, listcutoff;
	var i, j, nodeObj;
	var strXML, strXSL, el, vEl, searchStr = "";
	
	RefreshInProgress = true;
	url = RefreshEl.getAttribute("listurl");
	lasturl = RefreshEl.getAttribute("lasturl");
	listsrc = RefreshEl.getAttribute("listsrc");
	listcutoff = parseInt(RefreshEl.getAttribute("listcutoff"));
	if (isNaN(listcutoff)) {
		listcutoff = 0;
	}
	if (url != null) {
		ns = url.indexOf('|', ns);
		while (ns != -1) {
			nf = url.indexOf('|', ns + 1);
			strSub = url.substring(ns + 1, nf);
			url = url.replace('|' + strSub + '|', eval("document.all." + strSub + ".value"));
			ns = url.indexOf('|', ns);
		}
		if ((lasturl == null) || ((lasturl != null) && (url != lasturl))) {
			httpObj.open("GET", url, false);
			httpObj.setRequestHeader("Content-Type", "text/xml");
			httpObj.send();
			if (httpObj.readyState == 4) {
				RefreshEl.setAttribute("lasturl", url);
				domObj.loadXML(httpObj.ResponseText);
				if ((domObj.lastChild != null) && (domObj.lastChild.getAttribute("Error") != 1)) {
					ClearDropDown(RefreshEl);
					if ((listcutoff == 0) || (domObj.documentElement.childNodes.length <= listcutoff)) {
						for (i = 0; i < domObj.documentElement.childNodes.length; i++) {
							nodeObj = domObj.documentElement.childNodes.item(i);
						    j = AddItemToDropDown(RefreshEl, nodeObj.childNodes[0].text, nodeObj.childNodes[1].text);
							if (nodeObj.childNodes[0].text == RefreshEl.getAttribute("selectedvalue")) {
								RefreshEl.selectedIndex = i;
								if (RefreshEl.getAttribute("defaultset")) {
									RefreshEl.selectedIndex += 1;
								}
							}
						}
						if ((RefreshEl.options.length > 0) && (RefreshEl.selectedIndex == -1) && (!RefreshEl.combo)) {
							RefreshEl.selectedIndex = 0;
						}
					}
				}
			}
		}
	}
	else if (listsrc != null) {
		if (Trim(RefreshEl.getAttribute("listsrcfilter")) == "") {
			el = eval("document.all." + listsrc);
			if (el == null) {
				return;
			}
			strXML = el.XMLDocument.xml;
		}
		else {
			var objXML = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
			var objXSL = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
			var objTemplate = new ActiveXObject("MSXML2.XSLTemplate");

			el = eval("document.all." + listsrc + "FilterXSL");
			if (el == null) {
				return;
			}
			if (!document.main[RefreshEl.getAttribute("listsrcfilter")]) {
				strXSL = el.innerHTML.replace(/filter/gi, RefreshEl.getAttribute("listsrcfilter"));
			}
			else {
				vEl = $F(RefreshEl.getAttribute("listsrcfilter"));
				if (vEl == null) {
					strXSL = el.innerHTML.replace(/filter/gi, "record");
				}
				else {
					searchStr = vEl.toUpperCase();
					// replace special chars
					searchStr = searchStr.replace(/&/gi, "&amp;");
					searchStr = searchStr.replace(/'/gi, "`");
					strXSL = el.innerHTML.replace(/filter/gi, "record[starts-with(translate(*[2], $lowerCase, $upperCase), '" + searchStr + "')]");
				}
			}
			objXSL.loadXML(strXSL);
			objTemplate.stylesheet = objXSL;
			var objProcessor = objTemplate.createProcessor();
			el = eval("document.all." + listsrc);
			if (el == null) {
				return;
			}
			objXML.loadXML(el.XMLDocument.xml);
			objProcessor.input = objXML;
			objProcessor.transform();
			strXML = objProcessor.output;
		}
		domObj.loadXML(strXML);
		ClearDropDown(RefreshEl);
		if ((listcutoff == 0) || (domObj.documentElement.childNodes.length <= listcutoff)) {
			for (i = 0; i < domObj.documentElement.childNodes.length; i++) {
				nodeObj = domObj.documentElement.childNodes.item(i);
			    j = AddItemToDropDown(RefreshEl, nodeObj.childNodes[0].text, nodeObj.childNodes[1].text);
				if (nodeObj.childNodes[0].text == RefreshEl.getAttribute("selectedvalue")) {
					RefreshEl.selectedIndex = i;
					if (RefreshEl.getAttribute("defaultset")) {
						RefreshEl.selectedIndex += 1;
					}
				}
			}
			if ((RefreshEl.options.length > 0) && (RefreshEl.selectedIndex == -1) && (!RefreshEl.combo)) {
				RefreshEl.selectedIndex = 0;
			}
		}
	}
	RefreshInProgress = false;
}

function encode(str) {
	return escape(str.gsub(' ', '+'));
}

function checkUniqueness() {
	var strURL, el, nodeObj, bUnique = true, objRegex;
	
	el = event.srcElement;
	objRegex = new RegExp("^" + Trim(el.initialvalue) + "$", "i");
	if ((Trim(el.value) != "") && (!objRegex.test(Trim(el.value))) && (el.getAttribute("uniquedsn") != null) && (el.getAttribute("uniquetable") != null) && (el.getAttribute("uniquecolumn") != null)) {
		strURL = "../common/custom_tags/checkunique.cfm?DSN=" + el.getAttribute("uniquedsn") + "&Table=" + el.getAttribute("uniquetable") + "&Column=" + el.getAttribute("uniquecolumn");
		if (el.getAttribute("uniquewhere") != null) {
			strURL = strURL + "&Where=" + el.getAttribute("uniquewhere");
		}
	
		httpObj.open("POST", strURL, false);
		httpObj.setRequestHeader("Content-Type", "text/xml");
		httpObj.send("Value=" + encode(el.value));
		
		if (httpObj.readyState == 4)
		{
			domObj.loadXML(httpObj.ResponseText);
			nodeObj = domObj.documentElement.childNodes.item(0);
			if (nodeObj.getAttribute("Error") != 1) {
				if (nodeObj.childNodes[0].text != "0") {
					bUnique = false;
				}
			}
			else {
				alert("Check Unique Error!\n\n" + nodeObj.childNodes[0].text);
				event.srcElement.focus();
				return false;
			}
		}
		if (!bUnique) {
			if (document.getElementById("LanguageResource") != null) {
				alert(LanguageResource.recordset.Fields("ID45").value);
			}
			else {
				alert("Value already exists. It must be unique.");
			}
			event.srcElement.value = "";
			event.srcElement.focus();
		}
	}
	return bUnique;
}

function checkRequired() {
	var j, elList, selectedObj, bOK = true;

	elList = document.getElementsByTagName('INPUT');
	for (j = 0; j < elList.length; j++) {
		selectedObj = elList[j];
		bOK = checkRequiredElement(selectedObj);
		if (!bOK) {
			return bOK;
		}
	}
	elList = document.getElementsByTagName('TEXTAREA');
	for (j = 0; j < elList.length; j++) {
		selectedObj = elList[j];
		bOK = checkRequiredElement(selectedObj);
		if (!bOK) {
			return bOK;
		}
	}
	return bOK;
}

function checkRequiredElement(el) {
	if ((el.getAttribute("required") == 1) && (Trim(el.value) == "") && (!el.disabled)) {
		if (document.getElementById("LanguageResource") != null) {
			alert(LanguageResource.recordset.Fields("ID46").value);
		}
		else {
			alert("Entry Required.  Please Try Again.");
		}
		if (el.type != "hidden") {
			el.select();
			el.focus();
		}
		return false;
	}
	return true;
}

function checkGroup() {
	var elList, selectedObj, grp;

	if ((event.propertyName == "checked") && (event.srcElement.checked == true)) {
		grp = event.srcElement.getAttribute("group");
		elList = document.getElementsByTagName('INPUT');
		for (j = 0; j < elList.length; j++) {
			selectedObj = elList[j];
			if (((selectedObj.type == "radio") || (selectedObj.type == "checkbox")) && (selectedObj.getAttribute("group") == grp)) {
				if (selectedObj != event.srcElement) {
					selectedObj.checked = false;
				}
			}
		}
	}
}

function checkLength() {
	var ml, strChk = "";
	
	if (event.type == "paste") {
		strChk = event.srcElement.value + window.clipboardData.getData("Text");
	}
	else if (event.type == "keydown") {
		strChk = event.srcElement.value;
	}
	ml = parseInt(event.srcElement.getAttribute("maxlength"));
	if (!isNaN(ml)) {
		if (strChk.length >= ml) {
			event.srcElement.value = strChk.substring(0, ml - 1);
			event.returnValue = false;
		}
	}
}

function checkValidation() {
	var el, minv, maxv, msg, langdso;
	var bValid = true, bRunValidation = true;

	langdso = document.getElementById("LanguageResource");
	el = document.elementFromPoint(event.clientX, event.clientY);
	if (el == null) {
		el = event.srcElement;
	}
	if ((event.srcElement.value != "") && (Trim(event.srcElement.getAttribute("validation")) != "") && (bRunValidation)) {
		minv = event.srcElement.getAttribute("minvalue");
		if ((minv == "") || ((!IsNumeric(minv)) && (!IsDate(minv)))) {
			minv = null;
		}
		maxv = event.srcElement.getAttribute("maxvalue");
		if ((maxv == "") || ((!IsNumeric(maxv)) && (!IsDate(maxv)))) {
			maxv = null;
		}
		switch(event.srcElement.getAttribute("validation").toUpperCase()) {
			case "NUMERIC":
				if (!IsNumeric(event.srcElement.value)) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID39").value);
					}
					else {
						alert("Invalid Number.  Please Try Again.");
					}
					bValid = false;
				}
				break;
				
			case "PRICE":
				if (!IsPrice(event.srcElement.value)) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID40").value);
					}
					else {
						alert("Invalid Price.  Please Try Again.");
					}
					bValid = false;
				}
				break;

			case "QUANTITY":
				if (!IsQTY(event.srcElement.value)) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID41").value);
					}
					else {
						alert("Invalid Quantity.  Please Try Again.");
					}
					bValid = false;
				}
				break;

			case "WEIGHT":
				if (!IsWeight(event.srcElement.value)) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID42").value);
					}
					else {
						alert("Invalid Weight.  Please Try Again.");
					}
					bValid = false;
				}
				break;

			case "INTEGER":
				if (!isInt(event.srcElement.value)) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID43").value);
					}
					else {
						alert("Invalid Integer.  Please Try Again.");
					}
					bValid = false;
				}
				break;
				
			case "USZIP":
				var zipnum  = /^\d{5}(\s*-?\s*\d{4})?$/;
				if (zipnum.test(event.srcElement.value) == false) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID44").value);
					}
					else {
						alert("Invalid U.S. Zip Code.  Please Try Again.");
					}
					bValid = false;
				}
				break;
				
			case "CANZIP":
				var zipnum_canada  = /^([A-Z][0-9][A-Z])(\s)([0-9][A-Z][0-9])$/;
				if (zipnum_canada.test(event.srcElement.value) == false) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID48").value);
					}
					else {
						alert("Invalid Canadian Postal Code.  Please Try Again.");
					}
					bValid = false;
				}
				break;
				
			case "EMAIL":
				var isemail = /[a-zA-Z][-_a-zA-Z0-9]+@[-_a-zA-Z0-9.]+\.[a-zA-Z]{2,3}/
				if (isemail.test(event.srcElement.value) == false) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID66").value);
					}
					else {
						alert("Invalid Email address.  Please Try Again.");
					}
					bValid = false;
				}
				break;
				
			case "IP":
				var ipnum = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
				if (ipnum.test(event.srcElement.value) == false) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID67").value);
					}
					else {
						alert("Invalid IP address.  Please Try Again.");
					}
					bValid = false;
				}
				break;
				
			case "PHONE":
				var phnum = /^(1\s*-?\s*)?(\(?\d{3}\)?\s*-?\s*)?\d{3}(\s*-?\s*)?\d{4}\s*((x|ext\.?)\s*\d{1,}\s*)?$/i;
				if (phnum.test(event.srcElement.value) == false) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID69").value);
					}
					else {
						alert("Invalid Phone Number.  Please Try Again.");
					}
					bValid = false;
				}
				break;
				
			case "DATE":
				if (getDateFromFormat(event.srcElement.value, "MM/dd/yyyy") == 0) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID47").value);
					}
					else {
						alert("Invalid Date.  Please Try Again.");
					}
					bValid = false;
				}
				break;
				
			case "TIME":
				if (getDateFromFormat(event.srcElement.value, "hh:mm a") == 0) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID116").value);
					}
					else {
						alert("Invalid Time.  Please Try Again.");
					}
					bValid = false;
				}
				break;

			case "DATETIME":
				if (getDateFromFormat(event.srcElement.value, "MM/dd/yyyy hh:mm a") == 0) {
					if (langdso != null) {
						alert(LanguageResource.recordset.Fields("ID117").value);
					}
					else {
						alert("Invalid Date/Time.  Please Try Again.");
					}
					bValid = false;
				}
				break;
		}
		if ((bValid) && ((minv != null) || (maxv != null))) {
			if ((maxv == null) && ((event.srcElement.value < parseFloat(minv)) || (Date.parse(event.srcElement.value) < Date.parse(minv)))) {
				if (langdso != null) {
					msg = LanguageResource.recordset.Fields("ID38").value + " " + minv + ". " + LanguageResource.recordset.Fields("ID36").value;
					alert(msg);
				}
				else {
					alert("Minimum value is " + minv + ".  Please Try Again.");
				}
				bValid = false;
			}
			else if ((minv == null) && ((event.srcElement.value > parseFloat(maxv)) || (Date.parse(event.srcElement.value) > Date.parse(maxv)))) {
				if (langdso != null) {
					msg = LanguageResource.recordset.Fields("ID37").value + " " + maxv + ". " + LanguageResource.recordset.Fields("ID36").value;
					alert(msg);
				}
				else {
					alert("Maximum value is " + maxv + ".  Please Try Again.");
				}
				bValid = false;
			}
			else if (((minv != null) && (maxv != null)) && (((event.srcElement.value < parseFloat(minv)) || (event.srcElement.value > parseFloat(maxv))) || ((Date.parse(event.srcElement.value) < Date.parse(minv)) || (Date.parse(event.srcElement.value) > Date.parse(maxv))))) {
				if (langdso != null) {
					msg = LanguageResource.recordset.Fields("ID34").value + " " + minv + " " + LanguageResource.recordset.Fields("ID35").value + " " + maxv + ". " + LanguageResource.recordset.Fields("ID36").value;
					alert(msg);
				}
				else {
					alert("Value must be between " + minv + " and " + maxv + ".  Please Try Again.");
				}
				bValid = false;
			}
		}
		if (!bValid) {
			event.srcElement.select();
			event.srcElement.focus();
			return false;
		}
	}
	return true;
}

function _isInteger(val) {
	var digits="1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
		}
	return true;
}

function _getInt(str,i,minlength,maxlength) {
	for (var x=maxlength; x>=minlength; x--) {
		var token=str.substring(i,i+x);
		if (token.length < minlength) { return null; }
		if (_isInteger(token)) { return token; }
		}
	return null;
}

function _padZeros(num, totalLen) {
	var numStr = num.toString();
	var numZeros = totalLen - numStr.length;
	if (numZeros > 0) {
		for (var i = 1; i <= numZeros; i++) {
			numStr = "0" + numStr;
		}
	}
	return numStr;
}

var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');

function formatDateString(displayDate, dateOutput) {
	dateOutput = dateOutput.replace(/'{2}/,"'");
	var formatStringYears = dateOutput.match(/y/g);
	var formatStringMonths = dateOutput.match(/M/g);
	var formatStringDays = dateOutput.match(/d/g);
	if (formatStringYears) dateOutput = dateOutput.replace(/y+/,displayDate.getFullYear().toString().substring(4-formatStringYears.length,4));
	if (formatStringMonths){
	  dateOutput = dateOutput.replace(/M+/,(formatStringMonths.length >= 3) ? MONTH_NAMES[displayDate.getMonth()] : _padZeros(displayDate.getMonth()+1,formatStringMonths.length));
	}
	if (formatStringDays) dateOutput = dateOutput.replace(/d+/,_padZeros(displayDate.getDate(),formatStringDays.length));
	if (dateOutput.search(/E/) != -1) dateOutput = dateOutput.replace(/E+/,DAY_NAMES[displayDate.getDay()]);

	var formatStringHours12 = dateOutput.match(/h/g);
	var formatStringHours23 = dateOutput.match(/H/g);
	var formatStringMinutes = dateOutput.match(/m/g);
	var formatStringSeconds = dateOutput.match(/s/g);
	var formatStringHours24 = dateOutput.match(/k/g);
	var formatStringHours11 = dateOutput.match(/K/g);
	if (formatStringHours12) {
		var hours12 = (displayDate.getHours() < 12) ? displayDate.getHours() : (displayDate.getHours() - 12);
		if (hours12 == 0) hours12 = 12;
		dateOutput = dateOutput.replace(/h+/,_padZeros(hours12,formatStringHours12.length));
	}
	if (formatStringHours23) dateOutput = dateOutput.replace(/H+/,_padZeros(displayDate.getHours(),formatStringHours23.length));
	if (formatStringMinutes) dateOutput = dateOutput.replace(/m+/,_padZeros(displayDate.getMinutes(),formatStringMinutes.length));
	if (formatStringSeconds) dateOutput = dateOutput.replace(/s+/,_padZeros(displayDate.getSeconds(),formatStringSeconds.length));
	if (formatStringHours24) dateOutput = dateOutput.replace(/k+/,_padZeros(displayDate.getHours() + 1,formatStringHours24.length));
	if (formatStringHours11) {
		var hours11 = (displayDate.getHours() < 12) ? displayDate.getHours() : (displayDate.getHours() - 12);
		dateOutput = dateOutput.replace(/h+/,_padZeros(hours12,formatStringHours12.length));
	}
	var ampm = (displayDate.getHours() < 12) ? "AM" : "PM";
	dateOutput = dateOutput.replace(/a$/,ampm);

	return Trim(dateOutput);
}

function getDateFromFormat(val, format) {
	val=Trim(val)+"";
	format=Trim(format)+"";
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var now=new Date();
	var year=now.getYear();
	var month=now.getMonth()+1;
	var date=1;
	var hh=now.getHours();
	var mm=now.getMinutes();
	var ss=now.getSeconds();
	var ampm="";
	
	while (i_format < format.length) {
		// Get next token from format string
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		// Extract contents of value based on format token
		if (token=="yyyy" || token=="yy" || token=="y") {
			if (token=="yyyy") { x=4;y=4; }
			if (token=="yy")   { x=2;y=2; }
			if (token=="y")    { x=2;y=4; }
			year=_getInt(val,i_val,x,y);
			if (year==null) { return 0; }
			i_val += year.length;
			if (year.length==2) {
				if (year > 70) { year=1900+(year-0); }
				else { year=2000+(year-0); }
				}
			}
		else if (token=="MMM"||token=="NNN"){
			month=0;
			for (var i=0; i<MONTH_NAMES.length; i++) {
				var month_name=MONTH_NAMES[i];
				if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
					if (token=="MMM"||(token=="NNN"&&i>11)) {
						month=i+1;
						if (month>12) { month -= 12; }
						i_val += month_name.length;
						break;
						}
					}
				}
			if ((month < 1)||(month>12)){return 0;}
			}
		else if (token=="EE"||token=="E"){
			for (var i=0; i<DAY_NAMES.length; i++) {
				var day_name=DAY_NAMES[i];
				if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
					i_val += day_name.length;
					break;
					}
				}
			}
		else if (token=="MM"||token=="M") {
			month=_getInt(val,i_val,token.length,2);
			if(month==null||(month<1)||(month>12)){return 0;}
			i_val+=month.length;}
		else if (token=="dd"||token=="d") {
			date=_getInt(val,i_val,token.length,2);
			if(date==null||(date<1)||(date>31)){return 0;}
			i_val+=date.length;}
		else if (token=="hh"||token=="h") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>12)){return 0;}
			i_val+=hh.length;}
		else if (token=="HH"||token=="H") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>23)){return 0;}
			i_val+=hh.length;}
		else if (token=="KK"||token=="K") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<0)||(hh>11)){return 0;}
			i_val+=hh.length;}
		else if (token=="kk"||token=="k") {
			hh=_getInt(val,i_val,token.length,2);
			if(hh==null||(hh<1)||(hh>24)){return 0;}
			i_val+=hh.length;hh--;}
		else if (token=="mm"||token=="m") {
			mm=_getInt(val,i_val,token.length,2);
			if(mm==null||(mm<0)||(mm>59)){return 0;}
			i_val+=mm.length;}
		else if (token=="ss"||token=="s") {
			ss=_getInt(val,i_val,token.length,2);
			if(ss==null||(ss<0)||(ss>59)){return 0;}
			i_val+=ss.length;}
		else if (token=="a") {
			if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
			else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
			else {return 0;}
			i_val+=2;}
		else {
			if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
			else {i_val+=token.length;}
			}
		}
	// If there are any trailing characters left in the value, it doesn't match
	if (i_val != val.length) { return 0; }
	// Is date valid for month?
	if (month==2) {
		// Check for leap year
		if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
			if (date > 29){ return 0; }
			}
		else { if (date > 28) { return 0; } }
		}
	if ((month==4)||(month==6)||(month==9)||(month==11)) {
		if (date > 30) { return 0; }
		}
	// Correct hours value
	if (hh<12 && ampm=="PM") { hh=hh-0+12; }
	else if (hh>11 && ampm=="AM") { hh-=12; }
	var newdate=new Date(year,month-1,date,hh,mm,ss);
	return newdate.getTime();
}

function SubmitForm() {
	document.main.submit();
}

function pageTable(tEl, direction) {
	var dsObject = $(tEl.dataSrc.substr(1, tEl.dataSrc.length));
	var pagingFunction = Trim(tEl.getAttribute("pagingfunction"));
	
	if (pagingFunction != "") {
		eval(pagingFunction);
	}
	
	if ((dsObject != null) && (dsObject.getAttribute("remote") == 1)) {
		var totalRecords = dsObject.recordset.recordCount;
		var objChild = dsObject.XMLDocument.documentElement.firstChild;
		if (objChild != null) {
			totalRecords = parseInt(objChild.getAttribute("totalrecords"));
		}
		var start = tEl.getAttribute("startRow");
		if (start == null) {
			start = 1;
		}
		var limit = tEl.getAttribute("dataPageSize");
		switch (direction.toUpperCase()) {
			case "FIRST":
				start = 1;
				break;
				
			case "PREVIOUS":
				start = start - limit < 1 ? start = 1 : start - limit;
				break;
				
			case "NEXT":
				start = start + limit > totalRecords ? start : start + limit;
				break;
				
			case "LAST":
				start = totalRecords > limit ? (totalRecords - limit) + 1 : start;
				break;
		}
		tEl.setSrc(null, start);
	}
	else {
		switch (direction.toUpperCase()) {
			case "FIRST":
				tEl.firstPage();
				break;
				
			case "PREVIOUS":
				tEl.previousPage();
				break;
				
			case "NEXT":
				tEl.nextPage();
				break;
				
			case "LAST":
				tEl.lastPage();
				break;
		}
	}
}

function adjustPageSize(tEl) {
	var pEl = document.getElementById(tEl.id + 'ItemsPerPage');
	
	if (pEl != null) {
		var dps = tEl.getAttribute("datapagesize");
		if (dps == null) {
			dps = 20;
		}
		oPopup.document.body.innerHTML = pEl.outerHTML;
		var sEl = oPopup.document.getElementById(tEl.id + 'PageSizeScroller');
		var iEl = oPopup.document.getElementById(tEl.id + 'ItemsPerPageInd');
		if ((sEl != null) && (iEl != null)) {
			sEl.value = dps;
			iEl.innerText = dps;
			sEl.attachEvent("change", eval(tEl.id + "PageSizeScrollerChange"));
			sEl.attachEvent("scroll", eval(tEl.id + "PageSizeScrollerScroll"));
			oPopup.document.body.firstChild.style.visibility = "visible";
			oPopup.show(0, -159, 38, 162, event.srcElement);
		}
	}
}

function updateStatus(tEl) {
	var sEl, mEl, pEl = null;

	if (tEl.readyState == 'complete') {
		var dsObject = document.getElementById(tEl.dataSrc.substr(1, tEl.dataSrc.length));
		if (dsObject != null) {
			sEl = document.getElementById(tEl.id + 'Status');
			if ((sEl != null) && (dsObject.XMLDocument.lastChild != null)) {
				sEl.style.visibility = "hidden";
				if (dsObject.XMLDocument.lastChild.hasChildNodes) {
					var totalRecords = dsObject.recordset.recordCount;
					var objChild = dsObject.XMLDocument.documentElement.firstChild;
					if (objChild != null) {
						var totalNode = objChild.getAttributeNode("totalrecords");
						if (totalNode != null) {
							totalRecords = parseInt(totalNode.value);
						}
					}
					var startRow = tEl.rows[0].recordNumber;
					if (tEl.getAttribute("startRow") != null) {
						startRow = parseInt(tEl.getAttribute("startRow"));
					}
					sEl.innerHTML = startRow + ' - ' + (startRow + (parseInt(tEl.rows.length) / parseInt(tEl.rowCount)) - 1) + ' of ' + totalRecords;
				}
			}
			mEl = document.getElementById(tEl.id + 'Msg');
			if ((mEl != null) && (dsObject.XMLDocument.lastChild != null)) {
				tEl.style.display = "none";
				if (tEl.getAttribute("scrollable") == 1) {
					pEl = $(tEl).up('.scrollable');
					pEl.style.display = "none";
				}
				if (dsObject.XMLDocument.lastChild.getAttribute("Error") == 1) {
					mEl.innerHTML = dsObject.XMLDocument.selectSingleNode("/*").text;
					mEl.style.color = "red";
					mEl.style.display = "";
				}
				else if (!dsObject.XMLDocument.lastChild.hasChildNodes) {
					mEl.innerHTML = "No Records";
					mEl.style.color = "green";
					mEl.style.display = "";
				}
				else {
					if ((tEl.dataFld == '') && (dsObject.XMLDocument.documentElement.firstChild != null) && (Trim(dsObject.XMLDocument.documentElement.firstChild.xml) != "") && (dsObject.XMLDocument.documentElement.firstChild.getAttribute("message") != null) && (Trim(dsObject.XMLDocument.documentElement.firstChild.getAttribute("message")) != "")) {
						mEl.innerHTML = Trim(dsObject.XMLDocument.documentElement.firstChild.getAttribute("message"));
						mEl.style.color = "990000";
						mEl.style.display = "";
					}
					else {
						mEl.style.display = "none";
					}
					tEl.style.display = "";
					if (pEl != null) {
						pEl.style.display = "";
					}
					if (sEl != null) {
						sEl.style.visibility = "";
					}
				}
			}
			resizeDiv();
			resizeHeader();
		}
	}
}

function refreshTable(tEl) {
	if ((document.readyState == 'complete') && (tEl.readyState == 'complete')) {
		checkAllDisable();
		resizeDiv();
		resizeHeader();
		if (tEl.getAttribute("sortOnComplete") != null) {
			tEl.removeAttribute("sortOnComplete");
			sortTable(tEl);
		}
		else if (tEl.getAttribute("selectedRow") != null) {
			if (tEl.dataPageSize != "") {
				for (var i = 0; i < Math.floor(tEl.selectedRow / tEl.dataPageSize); i++) {
					tEl.nextPage();
				}
				tEl.selectedRow = tEl.selectedRow % tEl.dataPageSize;
			}
			tEl.parentElement.scrollTop = tEl.rows(tEl.selectedRow).offsetTop;
			tEl.removeAttribute("selectedRow");
		}
	}
}

function filterTable(tEl, strFilter) {
	var objXML = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
	var objXSL = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
	var objTemplate = new ActiveXObject("MSXML2.XSLTemplate");
	var strXSL, dsName, el;
	
	dsName = tEl.getAttribute("dataSrc").substr(1);
	el = eval("document.all." + dsName + "FilterXSL");
	if (el == null) {
		return;
	}
	strXSL = el.innerHTML.replace(/filter/gi, strFilter);
	objXSL.loadXML(strXSL);
	objTemplate.stylesheet = objXSL;
	var objProcessor = objTemplate.createProcessor();
	el = eval("document.all." + dsName);
	if (el == null) {
		return;
	}
	if (el.getAttribute("src") == null) {
		objXML.loadXML(el.innerHTML);
	}
	else {
		var url = el.getAttribute("src");
		httpObj.open("Get", url, false);
		httpObj.setRequestHeader("Content-Type", "text/xml");
		httpObj.send();
		if (domObj.loadXML(httpObj.responseText)) {
			objRoot = domObj.documentElement;
			if ((objRoot.getAttribute("Error") == null) || (objRoot.getAttribute("Error") == "0")) {
				objXML.loadXML(objRoot.xml);
			}
			else {
				var objMessage = objRoot.selectSingleNode("//ErrorMessage");
				alert("Error retrieving data.\r\r" + objMessage.text);
			}
		}
	}
	objProcessor.input = objXML;
	objProcessor.transform();
	objXML.loadXML(objProcessor.output);
	el.loadXML(objXML.lastChild.xml);
	if (tEl.getAttribute("sortable") == 1) {
		tEl.sortOnComplete = 1;
	}
}

function sortTable(tEl, sortColumn, sortOrder) {
	var el = eval(tEl.id + "Obj");
	if (el != null) {
		var bSortOrder = false;
		var elSortOrder = eval("document.all." + tEl.id + "SortOrder");
		var elSortColumn = eval("document.all." + tEl.id + "SortColumn");
		if ((elSortOrder != null) && (elSortColumn != null)) {
			if (sortOrder != null) {
				elSortOrder.value = sortOrder;
			}
			if (elSortOrder.value == 1) {
				bSortOrder = true;
			}
			if (sortColumn != null) {
				elSortColumn.value = sortColumn;
			}
			el.sort(elSortColumn.value, null, bSortOrder);
		}
	}
}

function searchTable(tEl, cName, cVal, cDisplay) {
	var objRegex = new RegExp("^" + cVal + "$");
	var obj = null;
	var dsName, el;
	
	dsName = tEl.getAttribute("dataSrc").substr(1);
	el = eval("document.all." + dsName);
	if ((el == null) || (!el.XMLDocument.lastChild.hasChildNodes)) {
		return;
	}
	obj = document.createElement("DIV");
	el.recordset.MoveFirst();
	while (!el.recordset.EOF) {
		obj.innerHTML = el.recordset(cDisplay);
		if (obj.lastChild.getAttribute("oldColor") != null) {
			obj.lastChild.style.color = obj.lastChild.getAttribute("oldColor");
			obj.lastChild.removeAttribute("oldColor");
			el.recordset(cDisplay) = obj.innerHTML;
		}
		if (objRegex.test(el.recordset(cName))) {
			obj.lastChild.oldColor = obj.lastChild.style.color;
			obj.lastChild.style.color = "red";
			el.recordset(cDisplay) = obj.innerHTML;
			tEl.selectedRow = el.recordset.AbsolutePosition - 1;
		}
		el.recordset.MoveNext();
	}
}

var _startPosition = 0;
var _diffPosition = 0;
var _allowMove = false;
var _resizerHandle = null;
var _firstColumn = null;
var _secondColumn = null;
var _firstColumnLeft = 0;
var _firstColumnWidth = 0;
var _secondColumnLeft = 0;
var _secondColumnWidth = 0;

function setPosition() {
	var el = document.elementFromPoint(event.clientX, event.clientY);
	if ((el.tagName == "TD") && ((event.srcElement.offsetWidth - event.offsetX) < 7) && (!_allowMove)) {
		_allowMove = true;
		_resizerHandle.setCapture();
		_startPosition = event.clientX;
		_firstColumnLeft = findPosX(_firstColumn);
		_firstColumnWidth = _firstColumn.offsetWidth;
		_secondColumnLeft = findPosX(_secondColumn);
		_secondColumnWidth = _secondColumn.offsetWidth;
	}
}

function setResizeColumns(resizerHandle) {
	_resizerHandle = resizerHandle;
	_firstColumn = resizerHandle;
	_secondColumn = resizerHandle.cellIndex + 1 < resizerHandle.parentElement.cells.length ? resizerHandle.parentElement.cells(resizerHandle.cellIndex + 1) : _firstColumn;
	if (!_allowMove) {
		var el = document.elementFromPoint(event.clientX, event.clientY);
		if ((el.tagName == "TD") && ((event.srcElement.offsetWidth - event.offsetX) < 7)) {
			if (_resizerHandle.getAttribute("oldcursor") == null) {
				_resizerHandle.setAttribute("oldcursor", _resizerHandle.style.cursor);
			}
			_resizerHandle.style.cursor = "w-resize";
			return;
		}
		if (_resizerHandle.getAttribute("oldcursor") != null) {
			_resizerHandle.style.cursor = _resizerHandle.getAttribute("oldcursor");
		}
	}
}

function stopColumnResize() {
	if (_allowMove) {
		_resizerHandle.releaseCapture();
		_allowMove = false;
		_firstColumn.style.behavior = "url('#default#userData')";
		_firstColumn.setAttribute(_firstColumn.id + "UserWidth", _firstColumn.offsetWidth);
		_firstColumn.save(document.title + _firstColumn.id);
		_secondColumn.style.behavior = "url('#default#userData')";
		_secondColumn.setAttribute(_secondColumn.id + "UserWidth", _secondColumn.offsetWidth);
		_secondColumn.save(document.title + _secondColumn.id);
		resizeHeader();
	}
}

function setNewPosition() {
	if (_allowMove)	{
		newPosition = event.clientX;
		_diffPosition = _startPosition - newPosition;
		var tpos1 = parseInt(_firstColumnWidth) - parseInt(_diffPosition);
		if (tpos1 > 10) {
			var tpos2 = parseInt(_secondColumnWidth) + parseInt(_diffPosition);
			if (tpos2 > 10) {
				_firstColumn.style.width = tpos1 + "px";
				_secondColumn.style.width = tpos2 + "px";
				if (parseInt(_resizerHandle.livesizing) == 1) {
					updateSubTable();
				}
			}
		}
	}
}

function updateSubTable() {
	var ObjTable = getReal(_resizerHandle, "tagName", "TABLE");
	var ObjSubTable = null;
	if (Trim(_resizerHandle.subtable) != "") {
		ObjSubTable = document.getElementById(Trim(_resizerHandle.subtable));
	}
	if (ObjSubTable != null) {
		resizeBody(ObjTable, ObjSubTable);
	}
}

function findPosX(obj) {
	var cOffset = $(obj).cumulativeOffset();
	return cOffset['left'];
}

function findPosY(obj) {
	var cOffset = $(obj).cumulativeOffset();
	return cOffset['top'];
}

function isVisible(el) {
	while ((el != null) && (el.tagName != "BODY")) {
		if ((el.style.display == 'none') || (el.style.visibility == 'hidden')) {
			return false;
		}
		el = el.parentNode;
   	}
	return true;
}

function resizeDiv() {
	var DivHeight = 0, Margin = 0, Padding = 0, tEl = null;

	if (ResizeInProgress) {
		return;
	}
	ResizeInProgress = true;
	var el = document.getElementsByName("scrolldiv");
	if (el.length > 0) {
		var ScrollDivCount = 0;
		for (var i = 0; i < el.length; i++) {
			tEl = el[i].getElementsByTagName("TABLE")[0];
			if ((isVisible(tEl)) && (el[i].getAttribute("insidePopup") == null)) {
				// Reset Div height to nothing and then get it's 'natural' height
				el[i].style.height = "";
				DivHeight += el[i].clientHeight;
				ScrollDivCount++;
			}
		}
		if (ScrollDivCount > 0) {
			Margin = document.main.scrollHeight - DivHeight;
			// Run through the scroll divs and adjust margin for those that have a scrollable height less than the calculated height (i.e. add padding)
			var PaddingDivCount = 0;
			var CalcHeight = Math.ceil((document.body.clientHeight - Margin) / ScrollDivCount);
			for (i = 0; i < el.length; i++) {
				tEl = el[i].getElementsByTagName("TABLE")[0];
				if ((isVisible(tEl)) && (el[i].getAttribute("insidePopup") == null)) {
					if (CalcHeight > el[i].scrollHeight) {
						Padding += (CalcHeight - el[i].scrollHeight);
						PaddingDivCount++;
					}
				}
			}
			var needScrollBar = "no";
			// Now, run through and perform the resize
			for (i = 0; i < el.length; i++) {
				tEl = el[i].getElementsByTagName("TABLE")[0];
				if ((isVisible(tEl)) && (el[i].getAttribute("insidePopup") == null) && (document.body.clientHeight - Margin >= 25)) {
					if ((event != null) && (el[i] != event.srcElement) && (el[i].sizeable == 1)) {
						// The user data store is for holding user size bar preferences
						el[i].load(document.title + tEl.id);
						el[i].style.height = el[i].getAttribute(tEl.id + "UserHeight");
						needScrollBar = "yes";
					}
					else {
						CalcHeight = Math.ceil((document.body.clientHeight - Margin) / ScrollDivCount);
						if ((PaddingDivCount > 0) && (PaddingDivCount < ScrollDivCount) && (CalcHeight < el[i].scrollHeight)) {
							CalcHeight += Math.ceil((Padding / (ScrollDivCount - PaddingDivCount)));
						}
						if (CalcHeight > el[i].scrollHeight) {
							CalcHeight = el[i].scrollHeight;
						}
						el[i].style.height = CalcHeight;
					}
				}
			}
			document.body.scroll = needScrollBar;
		}
	}
	ResizeInProgress = false;
}

function resizeHeader() {
	var thead = null, tbody = null, tbodyrows = null;
	var theadcols = null, tbodycols = null;
	var el, i, j, bColResize;
	
	el = document.getElementsByTagName("TABLE");
	if (el.length > 0) {
		for (j = 0; j < el.length; j++) {
			bColResize = false;
			tbody = el[j];
			if (tbody.getAttribute("alignto") != null) {
				thead = window.document.getElementById(tbody.getAttribute("alignto"));
				if ((thead != null) && (tbody != null)) {
					theadcols = thead.getElementsByTagName("TD");
					// Check for column resizing
					for (i = 0; i < theadcols.length; i++) {
						if (theadcols[i].sizeable == 1) {
							// The user data store is for holding column size preferences
							theadcols[i].load(document.title + theadcols[i].id);
							theadcols[i].width = theadcols[i].getAttribute(theadcols[i].id + "UserWidth");
							bColResize = true;
						}
					}
					if (bColResize) {
						resizeBody(thead, tbody);
					}
					else {
						tbodyrows = tbody.rows;
						if (tbodyrows.length > 0) {
							tbodycols = tbodyrows[0].cells;
							if ((theadcols != null) && (tbodycols != null) && (theadcols.length == tbodycols.length)) {
								if (tbody.offsetWidth > 0) {
									thead.width = 1;
									thead.width = tbody.offsetWidth;
									for (i = 0; i < theadcols.length; i++) {
										$(theadcols[i]).clonePosition(tbodycols[i], 'setWidth');
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

function resizeBody(thead, tbody) {
	if (tbody.offsetWidth == 0) {
		return;
	}
	thead.width = tbody.offsetWidth;
	tbody.style.display = "none";
	var theadcols = thead.getElementsByTagName("TD");
	var tbodyrows = tbody.children[0].rows;
	if (tbodyrows.length > 0) {
		var tbodycols = tbodyrows[0].cells;
		if ((theadcols != null) && (tbodycols != null) && (theadcols.length == tbodycols.length)) {
			for (var i = 0; i < theadcols.length; i++) {
				if ((theadcols[i].clientWidth > 0) && (theadcols[i].clientWidth != tbodycols[i].clientWidth)) {
					tbodycols[i].width = theadcols[i].clientWidth;
				}
			}
		}
	}
	tbody.style.display = "";
}

