// Creates a hidden field with the given id, name, and value
// and appends it to the first form.
function CreateHiddenField(id, name, value) {
	var frm = document.forms[0];
	var hid = document.createElement("<INPUT type=\"hidden\">")
	
	hid.id = (id == null) ? "" : id ;
	hid.name = (name == null) ? "" : name ;
	hid.value = (value == null) ? "" : value ;
	
	frm.appendChild(hid);
	
	return hid;
}

// Returns or creates (and appends to the first form) a 
// hidden field with the given Id. Optionally, name and
// initial value may be specified for the creation if 
// hidden field does not already exist.
function GetOrCreateHiddenField(id) {
	var hid;
	try {
		hid = document.getElementById(id);
	} catch (e) {}
	
	if (hid == null) {
		var arg1;
		var arg2;
		if ((arguments.length > 1) && (arguments[1] != null)) {
			arg1 = arguments[1];
		} else {
			arg1 = id;
		}
		if ((arguments.length > 2) && (arguments[2] != null)) {
			arg2 = arguments[2];
		} else {
			arg2 = "";
		}
		hid = CreateHiddenField(id, arg1, arg2);
	}
	
	return hid;
}

// Removes (destroys) a hidden field (or any object) with the
// given Id.
function DestroyHiddenField(id) {
	var hid = document.getElementById(id);
	
	if (hid != null) {
		var parent = hid.parentElement;
			
		if (parent != null) {
			parent.removeChild(hid);
		}
	}
}


// Returns an element from the page with the given Id
function GetElementById(id) {
	if (document.getElementById != undefined) {
		return document.getElementById(id);
	} else if (document.all != undefined) {
		return document.all(id);
	} else {
		try {
			return eval("document." + id);
		} catch (e) { return null; }
	}
	return null;
}


// Returns the Top position of an object, relative to the page.
// Remarks: Object must be displaying to return a position.
function GetTop(obj) {
	var nPos = new Number(0);
	var oCurrent;
	
	oCurrent = obj;
	
	while (oCurrent) {
		nPos += oCurrent.offsetTop;
		oCurrent = oCurrent.offsetParent;
	}
	
	return nPos;
}


// Returns the Left position of an object, relative to the page.
// Remarks: Object must be displaying to return a position.
function GetLeft(obj) {
	var nPos = new Number(0);
	var oCurrent;
	
	oCurrent = obj;
	
	while (oCurrent) {
		nPos += oCurrent.offsetLeft;
		oCurrent = oCurrent.offsetParent;
	} 

	return nPos;
}








// Returns the height of the browser viewport.
function GetClientHeight() {
	var clientHeight = new Number();
	if (document.clientHeight != undefined) {
		clientHeight = document.clientHeight;
	} else if ((document.documentElement != undefined) && (document.documentElement.clientHeight != undefined)) {
		clientHeight = document.documentElement.clientHeight;
	} else if ((document.body != undefined) && (document.body.clientHeight != undefined)) {
		clientHeight = document.body.clientHeight;
	} else if (window.innerHeight != undefined) {
		clientHeight = window.innerHeight;
	}
	return clientHeight;
}


// Returns the width of the browser viewport.
function GetClientWidth() {
	var clientWidth = new Number();
	if (document.clientWidth != undefined) {
		clientWidth = document.clientWidth;
	} else if ((document.body != undefined) && (document.body.clientWidth != undefined)) {
		clientWidth = document.body.clientWidth;
	} else if ((document.documentElement != undefined) && (document.documentElement.clientWidth != undefined)) {
		clientWidth = document.documentElement.clientWidth;
	} else if (window.innerWidth != undefined) {
		clientWidth = window.innerWidth;
	}
	return clientWidth;
}










// Gets the Inner Text markup of an object
function GetInnerText(obj) {
	var sInnerText = new String();
	if ((obj != undefined)&&(obj != null)) {
		if (obj.innerText != undefined) {
			sInnerText = obj.innerText;
		} else if (obj.textContent != undefined) {
			sInnerText = obj.textContent;
		} else if (obj.text != undefined) {
			sInnerText = obj.text;
		}
	}
	return sInnerText;
}

// Sets the Inner Text markup of an object
function SetInnerText(obj, innerText) {
	if ((obj != undefined)&&(obj != null)) {
		var sInnerText = new String();
		if ((innerText != undefined)&&(innerText != null)) {
			sInnerText = innerText.toString();
		}
		
		if (obj.innerText != undefined) {
			obj.innerText = sInnerText;
		} else if (obj.textContent != undefined) {
			obj.textContent = sInnerText;
		} else if (obj.text != undefined) {
			obj.text = sInnerText;
		}
	}
}




// Gets the Inner HTML markup of an object
function GetInnerHtml(obj) {
	var sInnerHtml = new String();
	if ((obj != undefined)&&(obj != null)) {
		if (obj.innerHTML != undefined) {
			sInnerHtml = obj.innerHTML;
		} else {
			sInnerHtml = _getInnerHTML(obj);
		}
	}
	return sInnerHtml;
}

// Sets the Inner HTML markup of an object
function SetInnerHtml(obj, innerHtml) {
	if ((obj != undefined)&&(obj != null)) {
		var sInnerHtml = new String();
		if ((innerHtml != undefined)&&(innerHtml != null)) {
			sInnerHtml = innerHtml.toString();
		}
		
		if (obj.innerHTML != undefined) {
			obj.innerHTML = sInnerHtml;
		} else if (obj.document != undefined) {
			obj.document.open();
			obj.document.write(sInnerHtml);
			obj.document.close();
		}
	}
}


// Private function. Traverses the nodes of an object, returning
// The inner HTML by scouring attributes. W3C DOM-compliant
function _getInnerHTML(node) {
	var str = new String();
	for (var i=0; i<node.childNodes.length; i++)
		str += _getOuterHTML(node.childNodes.item(i));
	return str;
}

// Private function. Traverses the nodes of an object, returning
// The outer HTML by scouring attributes. W3C DOM-compliant
function _getOuterHTML(node) {
	var str = new String();
	
	switch (node.nodeType) {
		case 1: // ELEMENT_NODE
			str += "<" + node.nodeName;
			for (var i=0; i<node.attributes.length; i++) {
				if (node.attributes.item(i).nodeValue != null) {
					var sValue = new String();
					var sQuoteChar = new String("\"");
					sValue = node.attributes.item(i).nodeValue;
					if (sValue.indexOf(sQuoteChar) >= 0) { sQuoteChar = "\'"; }
					
					str += " "
					str += node.attributes.item(i).nodeName;
					str += "=";
					str += sQuoteChar;
					str += node.attributes.item(i).nodeValue;
					str += sQuoteChar;
				}
			}
			
			if (node.childNodes.length == 0) {
				str += " />";
			} else {
				str += ">";
				str += _getInnerHTML(node);
				str += "</" + node.nodeName + ">"
			}
			
			break;
				
		case 3:	//TEXT_NODE
			str += node.nodeValue;
			break;
			
		case 4: // CDATA_SECTION_NODE
			str += "<![CDATA[" + node.nodeValue + "]]>";
			break;
					
		case 5: // ENTITY_REFERENCE_NODE
			str += "&" + node.nodeName + ";"
			break;

		case 8: // COMMENT_NODE
			str += "<!--" + node.nodeValue + "-->"
			break;
	}

	return str;
}




//removes an onchange event handler and returns a
//reference to the original event handler
function UnwireOnChangeEventGeneric(input) {
	var fxOnchange = input.onchange;
	input.onchange = null;
	return fxOnchange
}

//reassigns an event handler to an onchange event
function RewireOnChangeEventGeneric(input, fxOnChange) {
	input.onchange = fxOnChange;
}


//removes an onclick event handler and returns a
//reference to the original event handler
function UnwireOnClickEventGeneric(input) {
	var fxOnClick = input.onclick;
	input.onclick = null;
	return fxOnClick
}

//reassigns an event handler to an onclick event
function RewireOnClickEventGeneric(input, fxOnClick) {
	input.onclick = fxOnClick;
}


//removes an onblur event handler and returns a
//reference to the original event handler
function UnwireOnBlurEventGeneric(input) {
	var fxOnBlur = input.onblur;
	input.onblur = null;
	return fxOnBlur
}

//reassigns an event handler to an onblur event
function RewireOnBlurEventGeneric(input, fxOnBlur) {
	input.onblur = fxOnBlur;
}















function SetFocus(id)
{
	var bScrollIntoView = new Boolean(false);
	var bScrollTop = new Boolean(false);
	
	if ((arguments.length > 1) && (arguments[1] != null) && (arguments[1] == true)) { bScrollIntoView = true }
	if ((bScrollIntoView == true) && (arguments.length > 2) && (arguments[2] != null) && (arguments[2] == true)) { bScrollTop = true }
	
	var oInput = GetElementById(id);
	if (oInput != null) {
	  if (bScrollIntoView == true) {
			if (oInput.scrollIntoView != undefined) {
				try {
					oInput.scrollIntoView(bScrollTop);
				} catch (e) { }
			}		
	  }
	  
		if (oInput.focus) {
			try {
				oInput.focus();
			} catch (e) { }
	  }		
	}
}
