function did(string) {
	return document.getElementById(string);
}

function xhr() {
	if ( window.XMLHttpRequest ) {
		request = new XMLHttpRequest();
	} else if ( window.ActiveXObject ) {
		try {
			request = new ActiveXObject( "Msxml2.XMLHTTP" );
		} catch ( error ) {
			try {
				request = new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch ( error2 ) {
				return alert("Fatal Error: No XMLHttp Interface Available");
			}
		}
	} else {
		return alert("Fatal Error: No XMLHttp Interface Available");
	}
	return request;
}


function rpc(path, input) {
	var requestType = "POST";
	
	if ( !input ) {
		requestType = "GET";
		input = null;
	} else if ( input instanceof Object ) {
		var newinput = '';
		for ( var prop in input ) {
			if ( input[prop] instanceof Array ) {
				for ( var x=0; x<input[prop].length; x++ ) {
					newinput += prop+'[]='+encodeURIComponent(input[prop][x])+'&';
				}
			} else if ( input[prop] instanceof Object ) {
				for ( var prop2 in input[prop] ) {
					if ( input[prop][prop2] instanceof Array ) {					
						for ( var x=0; x<input[prop][prop2].length; x++ ) {
							newinput += prop+'['+prop2+'][]='+encodeURIComponent(input[prop][prop2][x])+'&';
						}
					} else if ( !(input[prop][prop2] instanceof Function) ) {
						newinput += prop+'['+prop2+']='+encodeURIComponent(input[prop][prop2])+'&';
					}
				}
			} else if ( !(input[prop] instanceof Function) ) {
				newinput += prop+'='+encodeURIComponent(input[prop])+'&';
			}
		}
		input = newinput;
	}
	
	var request = xhr();
	
	
	request.open(requestType, 'rpc/'+path, false);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");		
	request.send(input);
	eval("var res="+request.responseText);
	return res;
}

var async_rpc_objects = [];

function callWhenReady(func) {
	if ( async_rpc_objects.length > 0 ) {
		window.setTimeout(function() { callWhenReady(func); }, 250);
		return;
	}
	func();
}

function async_rpc(path, callback, input, errHandler) {
	if ( this == window ) {
		return new async_rpc(path, callback, input, errHandler);
	}
	this.requestType = "POST";
	this.myCallback = callback;
	this.errHandler = errHandler;
	
	this.callback = function () {
		if ( 4 == this.request.readyState && 200 == this.request.status ) {
			async_rpc_objects.remove(async_rpc_objects.find(this));
			eval("var res="+this.request.responseText);
			if ( res instanceof activaException ) {
				if ( typeof(this.errHandler) == 'undefined' ) {
					if ( console ) {
						if ( console.error ) {
							console.error(res);
						}
					}
					return;
				} else {
					this.errHandler(res);
					return;
				}
			}
			this.myCallback(res);
		} else if ( 4 == this.request.readyState && this.request.status != 0 ) {
			async_rpc_objects.remove(async_rpc_objects.find(this));
			if ( typeof(this.errHandler) != "undefined" ) {
				this.errHandler(new activaException("Server returned error code "+this.request.status));
			}
		}
	}
	
	this.abort = function (){
		this.request.abort();
		async_rpc_objects.remove(async_rpc_objects.find(this));
	}

	if ( !input ) {
		//this.requestType = "GET";
		this.input = "";
	} else {
		if ( input instanceof Object ) {
			var newinput = '';
			for ( var prop in input ) {
				if ( input[prop] instanceof Array ) {
					for ( var x=0; x<input[prop].length; x++ ) {
						newinput += prop+'[]='+encodeURIComponent(input[prop][x])+'&';
					}
				} else if ( input[prop] instanceof Object ) {
					for ( var prop2 in input[prop] ) {
						if ( input[prop][prop2] instanceof Array ) {					
							for ( var x=0; x<input[prop][prop2].length; x++ ) {
								newinput += prop+'['+prop2+'][]='+encodeURIComponent(input[prop][prop2][x])+'&';
							}
						} else if ( !(input[prop][prop2] instanceof Function) ) {
							newinput += prop+'['+prop2+']='+encodeURIComponent(input[prop][prop2])+'&';
						}
					}
				} else if ( !(input[prop] instanceof Function) ) {
					newinput += prop+'='+encodeURIComponent(input[prop])+'&';
				}
			}
			input = newinput;
		}
	}
	
	this.request = xhr();

	this.request.onreadystatechange = createDelegate(this, 'callback');

	this.request.open(this.requestType, 'rpc/'+path, true);
	this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	async_rpc_objects.push(this);
	this.request.send(input);
	
}

function activaException(message, file, line, trace, url) {
	this.message = message;
	this.file = file;
	this.line = line;
	this.trace = trace;
	this.url = url;
};

function activaX(element, url, input) {
	this.element = domcheck(element);
	this.url = url;
	
	var request = xhr();
	this.request.onreadystatechange = createDelegate(this, 'callback');
	this.request.open("GET", this.url, true);
	this.request.send(null);
	
	this.callback = function() {
		this.element.innerHTML = this.request.responseText;
		this.element.innerHTML = this.element.innerHTML //ie fix
	}
	
}

function urlencode(string) {
	return encodeURIComponent(string).replace("%20", "+");
}

function createDelegate(oObject, sMethodName, data) {
	return function () {
		if ( data ) {
			var args = [];
			var i = 0;
			while ( arguments[i] ) {
				args[i] = arguments[i];
				i++;
			}
			args[i] = data;
			return oObject[sMethodName].apply(oObject, args);
		}
		return oObject[sMethodName].apply(oObject, arguments);
	};
}

function domcheck(elem) {
	if ( typeof(elem) == 'object' ) {
		return elem;
	}
	
	return did(elem);
}

function registerEvent(elem, event, callback, capture) {
	capture = Boolean(capture);
	if ( event == 'allchange' ) {
		registerEvent(elem, 'change', callback, capture);
		registerEvent(elem, 'click', callback, capture);
		registerEvent(elem, 'keyup', callback, capture);
		return;
	}
	elem = domcheck(elem);
	
	if ( elem.addEventListener ) {
		elem.addEventListener(event, callback, capture);
	} else {
		elem.attachEvent('on'+event, callback); 
	}
	return new registeredEvent(elem, event, callback, capture);
}

function unregisterEvent(elem, event, callback, capture) {
	capture = Boolean(capture);
	if ( event == 'allchange' ) {
		unregisterEvent(elem, 'change', callback, capture);
		unregisterEvent(elem, 'click', callback, capture);
		unregisterEvent(elem, 'keyup', callback, capture);
		return;
	}
	elem = domcheck(elem);
	
	if ( elem.removeEventListener ) {
		elem.removeEventListener(event, callback, capture);
	} else {
		elem.detachEvent('on'+event, callback); 
	}
}

function registeredEvent(elem, event, callback, capture) {
	this.elem = elem;
	this.event = event;
	this.callback = callback;
	this.capture = capture;
	
	this.unregister = function() {
		unregisterEvent(this.elem, this.event, this.callback, this.capture);
	}
}

function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=yes'
	win = window.open(mypage, myname, winprops)
	if ( parseInt(navigator.appVersion) >= 4 ) { 
		win.window.focus(); 
	}
	return win;
}

String.prototype.ltrim = function() {
	return this.replace(/^\s*/, '');
}

String.prototype.rtrim = function() {
	return this.replace(/\s*$/, '');
}

String.prototype.trim = function() {
	return this.rtrim().ltrim();
}

Array.prototype.find = function(targ) {
	for ( x in this ) {
		if ( this[x] == targ ) {
			return x;
		}
	}
	return false;
}

Array.prototype.inArray = function(targ) {
	if ( this.find(targ) ) {
		return true;
	} else {
		return false;
	}
}

Array.prototype.remove = function(pos) {
	if ( pos ) {
		this.splice(pos, 1);
	}	
}

function getNextSibling(elem) {
	elem = domcheck(elem);
	if ( !elem.nextSibling ) {
		return false;
	}
	if ( elem.nextSibling.nodeName == '#text' ) {
		return getNextSibling(elem.nextSibling);
	}
	return elem.nextSibling;
}

function getPreviousSibling(elem) {
	elem = domcheck(elem);
	if ( !elem.previousSibling ) {
		return false;
	}
	if ( elem.previousSibling.nodeName == '#text' ) {
		return getPreviousSibling(elem.previousSibling);
	}
	return elem.previousSibling;
}

function createCookie(name,value,days) {
	if ( days ) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ( start < 0 ) {
		return null;
	}
	if ( start == 1 ) { return null; }
	var end = document.cookie.indexOf(';', len);
	if ( end == -1 ) { end = document.cookie.length; }
	return unescape( document.cookie.substring(len, end) );
}

function hideID(name) {
	if ( domcheck(name) ) {
		domcheck(name).style.display = 'none';
	}
}

function showID(name) {
	if ( domcheck(name) ) {
		domcheck(name).style.display = '';
	}
}

function toggleID(name) {
	if ( !domcheck(name) ) {
		return;
	}
	if ( domcheck(name).style.display == 'none' ) {
		domcheck(name).style.display = '';
	} else {
		domcheck(name).style.display = 'none';
	}
}

Date.prototype.format = function(format) {
	var res = '';
	for ( var i = 0; i < format.length; i++ ) {
		switch(format.charAt(i)) {
			case 'm':
				res += padString(this.getMonth() + 1, 2);
				break;
				
			case 'd':
				res += padString(this.getDate(), 2);
				break;
				
			case 'Y':
				res += this.getFullYear();
				break;
				
			case 'H':
				res += padString(this.getHours(), 2);
				break;
				
			case 'i':
				res += padString(this.getMinutes(), 2);
				break;
				
				
			default:
				res += format.charAt(i);
				break;
				
		}
	}
	return res;
}

function padString(str, len) {
	if ( typeof(str) != 'string' ) {
		str = String(str);
	}
	while ( str.length < len ) {
		str = '0'+str;
	}
	return str;
}

function gaTrack(str) {
	try {
		if ( typeof(pageTracker) != 'undefined' ) {
			pageTracker._trackPageview(str);
		} else if ( typeof(urchinTracker) != 'undefined' ) {
			urchinTracker(str);
		}
	} catch ( err ) {}
}

/**
 * fixEvent - Returns an event object with common properties/methods normalized for easier cross browser usage.
 * @param e		object	optional;Event object to normalize
 * @return		object	Event object after normalization
 */
function fixEvent(e) {
	var evnt = e || window.event;
	if ( !evnt ) { 
		return null; 
	}
	if ( !evnt.target ) { 
		evnt.target = evnt.srcElement; 
	}
	evnt.preventDefault = (evnt.preventDefault)? evnt.preventDefault : function() { this.returnValue = false; };
	evnt.stopPropagation = (evnt.stopPropagation)? evnt.stopPropagation : function() { this.cancelBubble = true; };
	return evnt;
}

