var onLoadEvents = new Array();
var onUnLoadEvents = new Array();

window.onload = function() {
	for (var n = 0; n < onLoadEvents.length; n++) {
		var func = onLoadEvents[n];
		func();
	}
}

window.onunload = function() {
	for (var n = 0; n < onUnLoadEvents.length; n++) {
		var func = onUnLoadEvents[n];
		func();
	}
}

function registerOnLoad(func)
{
	onLoadEvents.push(func);
}

function registerOnUnLoad(func)
{
	onUnLoadEvents.push(func);
}

var onResizeEvents = new Array();
window.onresize = function() {
	for (var n = 0; n < onResizeEvents.length; n++) {
		var func = onResizeEvents[n];
		func();
	}
}

function registerOnResize(func)
{
	onResizeEvents.push(func);
}

/* Returns Mouse Co-Ordinates, Must be used within an event */
function getMouseXY(e) {
	if (document.all) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	}
	else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}
	// catch possible negative values in NS4
	if (tempX < 0) {
		tempX = 0;
	}
	if (tempY < 0) {
		tempY = 0;
	}
	
	return [tempX,tempY];
}

/* Gets location of an object - http://www.quirksmode.org/js/findpos.html */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

/*
function noenter() {
  return !(window.event && window.event.keyCode == 13);
}
*/

var RPCResponseBlocks = new Array(5);
for (n = 0; n < RPCResponseBlocks.length; n ++) {
	RPCResponseBlocks[n] = false;
}


/* From http://www.quirksmode.org/js/dhtmloptions.html */
function getObj(name)
{
	this.ready = 0;
	if (document.getElementById)
	{
		if ((this.obj = document.getElementById(name)) != null) {
			this.style = document.getElementById(name).style;
			this.ready = 1;
			this.id = name;
		}
	}
	else if (document.all)
	{
		if ((this.obj = document.all[name]) != null) {
			this.style = document.all[name].style;
			this.ready = 1;
			this.id = name;
		}
	}
	else if (document.layers)
	{
		if ((this.obj = getObjNN4(document,name)) != null) {
			this.style = this.obj;
			this.ready = 1;
			this.id = name;
		}
	}
}

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}
/* End Borrowed Code */

/* http://www.quirksmode.org/viewport/compatibility.html */
function getWindowHeight()
{
	var myWidth = 0, myHeight = 0;
	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;
	}
	return myHeight;
}

function getScrollOffset()
{
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return [x,y];
}
/* End Borrowed Code */

function myRPC(command, values, func, atts)
{
	//alert("RPC : " + command);
	var msg = new XMLRPCMessage(command);
	if ((values && typeof values == 'object') && values.constructor == Array) {
		for (n = values.length; n > 0; n--) {
			msg.addParameter(values[n - 1]);
		}
	}
	else {
		msg.addParameter(values);
	}
	var req = getXMLHttpRequest();
	req.open("POST", "/rpc/index.php");
	req.onreadystatechange=function () {
		RPC_pairedResponse(req, func, atts);
	}
	
	req.send(msg.xml());
}

function getXMLHttpRequest()
{
	/* <http://www.jibbering.com/2002/4/httprequest.html> */
	var xmlhttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		}
		catch (e) {
			xmlhttp = false;
		}
	}
	/* </http://www.jibbering.com/2002/4/httprequest.html> */
	return xmlhttp;
}

function RPC_pairedResponse(req, func, atts)
{
	if (req.readyState == 4) {
		var r = req.responseText;
		//alert(r);
		//XMLError(r);
		objDom = new XMLDoc(req.responseText, XMLError);
		var params = objDom.selectNode("/params/param[0]/value/array/data");
		if (params) {
			var len = params.getElements("value").length;
		}
		if (len) {
			var values = new Array(len);
			for (x = 0; x < RPCResponseBlocks.length; x++) {
				if (RPCResponseBlocks[x] == false) {
					RPCResponseBlocks[x] = new Array(6);
					RPCResponseBlocks[x][0] = params;
					RPCResponseBlocks[x][1] = values;
					RPCResponseBlocks[x][2] = func;
					RPCResponseBlocks[x][3] = atts;
					RPCResponseBlocks[x][4] = len;
					RPCResponseBlocks[x][5] = 0;
					rpctimer = self.setTimeout("RPC_buildPairedResponse("+x+");", 50);
					break;
				}
			}
			/*
			for (var n = 0; n < len; n ++) {
				var param = params.selectNode("/value["+n+"]/array/data");
				if (param) {
					values[n] = new Array();
					var id = param.selectNode("/value[0]/int").getText();
					values[n][0] = id;
					for (var alen = 1; alen < (param.getElements().length); alen ++) {
						values[n][alen] = param.selectNode("/value[" + alen + "]/string").getText();
					}
				}
				else {
					//alert("Parse Error - /value["+n+"]/array/data");
				}
			}
			*/
			/*
			if (typeof func != "undefined") {
				func(atts, values);
			}
			*/
		}
	}
}

function RPC_buildPairedResponse(x)
{
	params = RPCResponseBlocks[x][0];
	values = RPCResponseBlocks[x][1];
	func = RPCResponseBlocks[x][2];
	atts = RPCResponseBlocks[x][3];
	len = RPCResponseBlocks[x][4];
	n = RPCResponseBlocks[x][5];
	
	var param = params.selectNode("/value["+n+"]/array/data");
	if (param) {
		values[n] = new Array();
		var id = param.selectNode("/value[0]/int").getText();
		values[n][0] = id;
		for (var alen = 1; alen < (param.getElements().length); alen ++) {
			values[n][alen] = param.selectNode("/value[" + alen + "]/string").getText();
		}
	}
	n ++;
	if (n < len) {
		RPCResponseBlocks[x] = new Array(6);
		RPCResponseBlocks[x][0] = params;
		RPCResponseBlocks[x][1] = values;
		RPCResponseBlocks[x][2] = func;
		RPCResponseBlocks[x][3] = atts;
		RPCResponseBlocks[x][4] = len;
		RPCResponseBlocks[x][5] = n;
		rpctimer = self.setTimeout("RPC_buildPairedResponse("+x+")", 50);
	}
	else {
		RPCResponseBlocks[x] = false;
		if (typeof func != "undefined") {
			func(atts, values);
		}
	}
}

function XMLError(e)
{
	var debug = new getObj('debug');
	if (debug.ready) {
		debug.obj.innerHTML = debug.obj.innerHTML + e;
	}
	else {
		alert(e);
	}
}

function nullRPCResponseFunction(atts, values)
{
	
}

function eraseList(list)
{
	if (list && list.options && list.options.length) {
		while (list.options.length > 0) {
			list.options[0] = null
		}
	}
}

function insertOption(to, option, position)
{
	if (!to || !to.options) {
		return;
	}
	var i = 0;
	var n = to.options.length;
	if (typeof(position) != "number") {
		while (i < n && to.options[i].text < option.text) {
			i ++;
		}
	}
	else {
		i = position;
	}
	while (n > i) {
		to.options[n] = new Option (
			to.options[n - 1].text,
			to.options[n - 1].value,
			false, false);
		n --;
	}
	to.options[i] = option;
}

function selectOption(obj, value)
{
	for (n = obj.options.length - 1; n >= 0; n --) {
		if (obj.options[n].value == value) {
			obj.options[n].selected = true;
			return true;
		}
	}
	return false;
}

function printProps(obj, objName)
{
	var output = "" ;
	for (var prop in obj) {
		output += objName + "." + prop + " = " + obj[prop] + "\n<br />\n" ;
	}
	return output;
}

function isdefined(variable)
{
	return (typeof(window[variable]) == "undefined")?  false: true;
}

registerOnLoad(
	function()
	{
		setScreenClass();
	}
);

registerOnResize(setScreenClass);

//  Following transition classes will be declared:
//
//	classname		  screenwidth
//	------------------------------------------
//	screen_low		  640px -  800px	
//	screen_med		  800px - 1024px	
//	screen_high		 1024px - 1280px	
//	screen_wide		> 1280px			


function setScreenClass(){
	// Obtain Logo Width to get Scale Factor // Original Width is 310
	var logo = new getObj('sitelogo');
	if (logo.ready) {
		if (logo.obj.naturalWidth) {
			var nwidth = logo.obj.naturalWidth;
		}
		else {
			var nwidth = 310;
		}
		if (logo.obj.width) {
			var cwidth = logo.obj.width;
		}
		else {
			var cwidth = 310;
		}
	}
	else {
		var nwidth = 310;
		var cwidth = 310;
	}
	
	var scale = cwidth / nwidth;
	if (!scale) {
		scale = 1;
	}
	
	// Obtained Scale Factor to work out real screen size
	
	var fmt = document.documentElement.clientWidth;
	fmt = fmt / scale;
	//http://www.alistapart.com/articles/switchymclayout
	var cls = (fmt < 800) ? 'screen_med' : (fmt >= 800 && fmt < 980) ? 'screen_med' : (fmt >= 980 && fmt < 1280) ? 'screen_high' : 'screen_wide';
	
	var s = document.body.className.split(' ');
	document.body.className = '';
	for (var n = 0; n < s.length; n++) {
		switch (s[n]) {
			case 'screen_med':
			case 'screen_high':
			case 'screen_wide': {
				break;
			}
			default: {
				document.body.className += s[n];
				break;
			}
		}
	}
	document.body.className += ' ' + cls;
};

/* http://www.killersites.com/mvnforum/mvnforum/viewthread?thread=12035 */
function getPageSize() {
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) { 
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight) {
		// all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight) {
		// Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	}
	else {
		// Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body) {
		// other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	// for small pages with total height less then height of the viewport
	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	}
	else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if (xScroll < windowWidth) { 
		pageWidth = windowWidth;
	}
	else {
		pageWidth = xScroll;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}