// SHOW - HIDE OBJECTS
var lastOpened = null;
function objShowHide(id){
	
	// The object to show - hide
	objElem = findObj(id);

	// _icon is [+] OR [-] - e.g. <SPAN ID="xxx_icon">[+]</SPAN>
	objIcon = findObj(id + "_icon");
	
	// _head to display pre-description of hidden content of objElem
	objHead = findObj(id + "_head");
	
	if (objElem){
	
		// Display is open - close it!
		if (objElem.style.display == ""){
		
			if (objIcon){
				objIcon.innerHTML = "[+]";
			}
			if (objHead){
				objHead.style.display = "";
			}
			objElem.style.display = "none";
		
		// Display is closed - open it!
		}else{
		
			if (objIcon){
				objIcon.innerHTML = "[-]";
			}
			if (objHead){
				objHead.style.display = "none";
			}
			objElem.style.display = "";
			
			// Last opened object
			if (lastOpened != id){
				objLastOpened = findObj(lastOpened);
				objLastOpenedIcon = findObj(lastOpened + "_icon");
				objLastOpenedHead = findObj(lastOpened + "_head");
				if (objLastOpened){
					objLastOpened.style.display = "none";
				}
				if (objLastOpenedIcon){
					objLastOpenedIcon.innerHTML = "[+]";
				}
				if (objLastOpenedHead){
					objLastOpenedHead.style.display = "";
				}
			}
			lastOpened = id;
			
		}
	}
}
function Trim(Str){
	while(Str.charAt(0) == (" ")){
		Str = Str.substring(1);
	}
	while(Str.charAt(Str.length-1) == " "){
		Str = Str.substring(0,Str.length-1);
	}
	return Str;
}
function checkKey(objinput,event,maxlength,nextfield){

	//KEYCODE (ON KEY UP CAUSES THESE KEYS TO FIRE)
	//9  = TAB
	//16 = SHIFT
	if (event.keyCode != 9 && event.keyCode != 16){

 		var ValidChars = "0123456789";
		var Char = "";
		var sText = objinput.value;
		var nText = "";

		for (i = 0; i < objinput.value.length; i++){
			Char = sText.charAt(i);
			if (ValidChars.indexOf(Char) >= 0){
				nText = nText + Char;
				if (i + 1 >= maxlength){
					objinput.form.elements[nextfield].focus();
					objinput.form.elements[nextfield].select();
				}
			}
		}

		if (nText != null){
			objinput.value = nText;
		}else{
			objinput.value = "";
		}

	}

}
function findObj(objID){

	var n = document.getElementById(objID); 
	return n;

}

// MOVE FLOATING DIV TO TOP AND CENTER
function moveDiv(div){
	getScrollXY();
	getSize();
	var centerDiv = 0;
	centerDiv = ((myWidth / 2) - 385);
	if (centerDiv < 0){
		centerDiv = 0;
	}
	objDiv = findObj(div);
	if (objDiv){
		objDiv.style.left = centerDiv;
		objDiv.style.top = scrOfY + 35;
	}
}
var myWidth = 0, myHeight = 0;
function getSize(){
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
}
// scrOfX - Horizontal
// scrOfY - Vertical
var scrOfX = 0, scrOfY = 0;
function getScrollXY() {
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
}

// OBJECT VISIBILITY - INVISIBILITY
function objvisible(id){
	objThing = findObj(id);
	if (objThing){
		objThing.style.visibility = "visible";
	}
}
function objinvisible(id){
	objThing = findObj(id);
	if (objThing){
		objThing.style.visibility = "hidden";
	}
}
function objClear(id){
	objThing = findObj(id);
	if (objThing){
		objThing.innerHTML = '';
	}
}
function showBox(box){
	objBox = findObj(box + "Box");
	objData = findObj(box + "Data");
	if (objBox && objData){
		objBox.style.visibility = "visible";
		objData.innerHTML = "";
	}
}
function hideBox(box){
	objBox = findObj(box + "Box");
	objData = findObj(box + "Data");
	if (objBox && objData){
		objBox.style.visibility = "hidden";
		objData.innerHTML = "";
	}
}
// Open & Close ID by ID
function openClose(divID){
	if (divID){
		objDiv = findObj(divID);
		if (objDiv){
			if (objDiv.style.display == '' || objDiv.style.display == "none"){
				objDiv.style.display = "inline";
			}else{
				objDiv.style.display = "none";
			}
		}
	}
}
function objshow(divID){
	if (divID){
		objDiv = findObj(divID);
		if (objDiv){
			objDiv.style.display = "inline";
		}
	}
}
function objhide(divID){
	if (divID){
		objDiv = findObj(divID);
		if (objDiv){
			objDiv.style.display = "none";
		}
	}
}

function countChars(IdIn,maxChars){
	objIn = findObj(IdIn);
	if (objIn){
		objOut = findObj(objIn.id + "_count");
		if (!maxChars){
			maxChars = 0;
		}
		if (objOut){
			if (objIn.value.length <= maxChars){
				objOut.innerHTML = (maxChars - objIn.value.length);
			}else{
				objOut.innerHTML = 0;
				objIn.value = objIn.value.substring(0,maxChars);
			}
		}
	}
}

function stripTagsEasy(Str){
	var stripped = Str;
	stripped = stripped.replace(/(<-)/ig,"<");
	stripped = stripped.replace(/(<)/ig,"<-");
	stripped = stripped.replace(/(->)/ig,">");
	stripped = stripped.replace(/(>)/ig,"->");
	return stripped;
}

// AJAX XMLHTTP METHOD
// ---------------------------------------------------------------------------------------------------------------------------------------
function ajaxObject(URL,StatusDiv){

	var that = this;
	this.updating = false;
	this.abort = function(){
		if (that.updating) {
			that.updating = false;
			that.AJAX.abort();
			that.AJAX = null;
		}
	}
	this.update = function(Method,Mode,Vars,Div){
		// METHOD - GET OR POST
		// VARS - QUERYSTRING OR POST VARS
		// MODE - Return mode TXT or XML
		// DIV - DIV object by ID for tracking output
		if (that.updating){
			return false;
		}
		that.AJAX = null;
		// IE7, Mozilla, Safari, etc: Use native object
		if (window.XMLHttpRequest){
			that.AJAX = new XMLHttpRequest();
		 // IE5.X and IE6.X
		}else if (window.ActiveXObject){
			try{
				that.AJAX = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e){
				try{
					that.AJAX = new ActiveXObject("Microsoft.XMLHTTP");
				}catch (e){}
			}
		}
		if (that.AJAX == null) {		 		 		 			
			return false;		 		 		 			  
		}else{
			that.AJAX.onreadystatechange = function(){
				if (that.AJAX.readyState == 0) {
					that.procMsg("Initalizing");
				}
				if (that.AJAX.readyState == 1) {
					that.procMsg("Loading");
				}
				if (that.AJAX.readyState == 2) {
					that.procMsg("Loaded");
				}
				if (that.AJAX.readyState == 3) {
					that.procMsg("Interactive");
				}
				if (that.AJAX.readyState == 4) {
					that.procMsg("");
					that.procData(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
					that.updating = false;
					that.AJAX = null;
				}
			}
		}		 		 		 		 		 		 		 		 
		that.updating = new Date();
		if (Method == "POST"){
			var uri = URL + '?' + that.updating.getTime();
			that.AJAX.open("POST", uri, true);
			if (Mode == "XML"){
				that.AJAX.setRequestHeader("Content-Type", "text/xml");
			}else{
				that.AJAX.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			// SET CONTENT LENGTH FOR IE AND FIREFOX
			// DO NOT SET FOR - GOOGLE CHROME - SAFARI
			//that.AJAX.setRequestHeader("Content-Length", Vars.length);
			that.AJAX.send(Vars);
			this.setTimer();
		}else{
			var uri = URL + '?' + Vars + '&timestamp=' + (that.updating.getTime()); 
			that.AJAX.open("GET", uri, true);
			that.AJAX.send(null);
			this.setTimer();
		}
	}
	this.setTimer = function(){
		this.timeoutAJAX = setTimeout("this.AJAXStatus",3000);
	}
	this.AJAXStatus = function(){
		if (that.AJAX.readyState != 4){
			that.procMsg("Connection Aborted");
			this.abort();
		}
		clearTimeout(this.timeoutAJAX);
	}
	this.procData = function(responseText,responseStatus,responseXML){
		if (responseStatus == 200){
			eval(responseText);
		}else{
			if (responseStatus == "404"){
				this.procMsg("Page not found: " + responseStatus);
			}else if (responseStatus == "500"){
				this.procMsg("Internal Server Error: " + responseStatus);
			}else{
				this.procMsg("Undefined Error: " + responseStatus);
			}
		}
	}
	this.procMsg = function(strMsg){
		if (StatusDiv){
			divStatus = findObj(StatusDiv);
			if (divStatus){
				if (strMsg){
					if (divStatus.style.display = "none"){
						divStatus.style.display = "";
					}
					divStatus.innerHTML = strMsg;
				}else{
					divStatus.style.display = "none";
				}
			}
		}
	}
}
