// General purpose JavaScript functions for site

/***** Multiple window onload handling *****/
function windowonload() {
	// This add functions to an array for executing onload - optional 2nd arg is for a different window
	var win=window;
	if (arguments.length > 1) { win=arguments[1]; }
	
	if(win.onloadfunctions==undefined) { win.onloadfunctions=new Array(); }
	win.onloadfunctions.push(arguments[0]);
	win.onload=execwindowonload;
}

function execwindowonload() {
	if(!(window.onloadfunctions==undefined)) {
		for(var i=0; i< window.onloadfunctions.length; i++) {
			window.onloadfunctions[i]();
		}
	}
}
/***** End Multiple window onload handling *****/

windowonload(function () {
	//Determine browser, we only need this for Internet Explorer
	if (navigator.appName == "Microsoft Internet Explorer") {
		var arrElements = new Array(3);
		arrElements[0] = "object";
		arrElements[1] = "embed";
		arrElements[2] = "applet";
		for (n = 0; n < arrElements.length; n++) {
			replaceObj = document.getElementsByTagName(arrElements[n]);
			for (i = 0; i < replaceObj.length; i++ ) {
				parentObj = replaceObj[i].parentNode;
				newHTML = parentObj.innerHTML;
				parentObj.removeChild(replaceObj[i]);
				parentObj.innerHTML = newHTML;
				}
			}
		}
	})



/**** Popup windows - uses modelessdialog if in IE *****/
function ow(address) {
	var popupwin = null;
	var width=(arguments.length>1)?arguments[1]:500;
	var height=(arguments.length>2)?arguments[2]:250;
	var modal=(arguments.length>3)?arguments[3]:false;
	var normal=(arguments.length>4)?arguments[4]:false;
	if (window.showModelessDialog && !normal) {
		if(modal) {
			popupwin  = window.showModelessDialog(address, window, 'dialogLeft: 5px; dialogTop: 5px; dialogHeight: '+(height-100)+'px; dialogWidth: '+width+'px; help: no; resizable: no; status: no;');
		} else {
			popupwin = window.showModalDialog(address, window, 'dialogLeft: 5px; dialogTop: 5px; dialogHeight: '+(height-100)+'px; dialogWidth: '+width+'px; help: no; resizable: no; status: no;');
		}
	} else {
		popupwin = window.open(address,'popupwin','width='+width+'px, height='+height+'px');
	}
	return (popupwin);
}
/**** End popup windows *****/

/**** Retrieve a document over the web - welcome to ajax *****/
function getXMLDoc(url, callback) {
	//If the xml obj is passed in then defer the call
	if(arguments.length>2) {
		var http_request=arguments[2];
	} else {	
		var http_request=false;
	}
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		return ('Giving up :( Cannot create an XMLHTTP instance');
	}
	http_request.onreadystatechange = callback;
	if(arguments.length>2) {
		return (http_request);
	} else {
		http_request.open('GET', url, true);
		http_request.send(null);
		return (http_request);
	}
}
/***** End getXMLDoc *****/

/**** Populate select control with ajax resultset with display and value columns ****/
function populateDropDown(req, ctl) {
	if(req) {
		if (req.readyState) {
			if (req.readyState == 4) {
				if (req.status == 200) {
					var rows = (document.all) 
						? req.responseXML.getElementsByTagName('z:row')
						: req.responseXML.getElementsByTagName('row');
					// Clear options
					while(ctl.options.length>0) { ctl.options[0]=null; }
		
					for(var i=0; i<rows.length;i++) {
						ctl.options[ctl.options.length]=
							new Option(rows[i].getAttribute('Display'), rows[i].getAttribute('Value'));
					}	
		
				} else {
					alert('Invalid Status Code: '+req.status);
				}
			}
		}
	}
}
/*** End populate dropdown ***/

/*** Interrogate object ***/
function showproperties(obj, showvalues) {
	var output='';
	var separator=arguments.length>2?arguments[2]:'\t';
	for (var i in obj) {
		output+=i+(showvalues?'='+obj[i]:'')+separator;
	}
	return (output);
}

function getAbsLeft(obj) {
	return (obj.offsetLeft+(obj.offsetParent?getAbsLeft(obj.offsetParent):0));
}

function getAbsTop(obj) {
	return (obj.offsetTop+(obj.offsetParent?getAbsTop(obj.offsetParent):0));
}