// utility.js file

function $ () {

	var elements = new Array();

	for(var i = 0; i < arguments.length; i++) {

		var element = arguments[i];
		if('string'	== typeof element) element = document.getElementById(element);
		if(1 == arguments.length) return element;
		elements.push(element);
	}

	return elements;
}

function hide () {

	for (i = 0; i < arguments.length; i++) {

		var obj	= $(arguments[i]);
		if(obj) obj.style.display	= 'none';
	}
}

function show () {

	for (i = 0; i < arguments.length; i++) {

		var obj	= $(arguments[i]);
		if(obj) obj.style.display	= '';
	}
}

function toggleDisplay () {

	for (i = 0; i < arguments.length; i++) {

		var obj	= $(arguments[i]);
		if(obj) obj.style.display = ('none' == obj.style.display) ? '' : 'none';
	}
}

function getRadioGroupValue (objs) {

// USAGE: getRadioGroupValue(document.forms['myForm'].elements['myRadioGroup']);

	for(var i = 0; i < objs.length; ++i) {

		if(objs[i].checked) return objs[i].value;
	}
}

function setRadioGroupValue (objs, value) {

// USAGE: getRadioGroupValue(document.forms['myForm'].elements['myRadioGroup'], 'someValue');

	for(var i = 0; i < objs.length; ++i) {

		objs[i].checked = value == objs[i].value;
	}
}

function getSelectValue (obj) {

	return obj.options[obj.selectedIndex].value;
}

function setDisabled (list, value) {

	if(!(list instanceof Array)) var list = [list];

	for(var i = 0; i < list.length; ++i) {

		$(list[i]).disabled = value;
	}
}

function trim (value) {

	return value.replace(/^\s+|\s+$/g, '');
}

function empty (value) {

	var type = typeof value;

	// check for undefined values
	if(undefined == value || null == value) return true;

	// check for empty string
	if('' == value) return true;

	// check for empty array
	if('array' == type && !value.length) return true;

	// check for empty object
	var isEmpty = true;

	if('object' == type) {
		for(var i in value) {
			isEmpty = false;
			break;
		}

		if(isEmpty) return true;
	}

	// value is not empty
	return false;
}

function map (func, list) {

	var out = [];

	for(var i = 0; i < list.length; ++i) {

		out.push(func(list[i]));
	}

	return out;
}

function mapfilter (func, list) {

	var out = [];

	for(var i = 0; i < list.length; ++i) {

		var result	= func(list[i]);

		if(result != undefined) out.push(result);
	}

	return out;
}

/*
function map(func,list){
	ret = [];
	for(li in list){
		ret[ret.length] = func(list[li]);
	}
	return ret;
}
function filter(func,list){
	ret = [];
	for(li in list){
		if(func(list[li]) == true)
			ret[ret.length] = list[li];
	}
	return ret;
}
function reduce(func,list){
	last = new Object();
	if(list.length <= 1) return;
	if(list.length == 2) return func(list[0],list[1]);

	last = list[0];
	for(var li = 1;li < list.length;li++){
		last = func(last,list[li]);
	}
	return last;
}
*/

function addEvent (elm, evType, fn, useCapture) {

	if(elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else { 
		elm['on' + evType] = fn;
	}
}

function getEvent (e) { return !e ? window.event : e; }

function getEventTarget (e) { return !e.target ? e.srcElement : e.target; }

function getMousePosition (e) {

	var	out	= {x:0, y:0};

	if(e.pageX || e.pageY) {

		out.x	= e.pageX;
		out.y	= e.pageY;

	} else if(e.clientX || e.clientY) {

		out.x	= e.clientX;
		out.y	= e.clientY;

		var isOpera = (navigator.userAgent.indexOf('Opera') != -1);
		var isIE = (!isOpera && navigator.userAgent.indexOf('MSIE') != -1);

		if(isIE) {

			out.x += document.body.scrollLeft;
			out.y += document.body.scrollTop;
		}
	}

	return out;
}

function getUrlParameter (name, url) {

	return getUrlParameters(url)[name];
}

function getUrlParameters (url) {

	if(undefined == url) var url = String(window.location);
	
	var index	= url.indexOf('?');
	var qs		= url.substring(index + 1);

	return decodeAsObject(qs);
}

function encodeAsUrl (obj) {

	var out	= [];

	for(var i in obj) {

		out.push(i + '=' + encodeURIComponent(obj[i]));
	}

	return out.join('&');
}

function decodeAsObject (str) {

	var out		= {};
	var values	= str.split('&');

	for(var i = 0; i < values.length; ++i) {

		if(!values[i].length) continue;

		var pair		= values[i].split('=');
		out[pair[0]]	= decodeURIComponent(pair[1]);
	}

	return out;
}

function getObjectPosition (obj) {

	var	out		= {x:0, y:0};
	var offset	= obj;

	while (offset != null) {

		out.x += offset.offsetLeft;
		out.y += offset.offsetTop;

		offset = offset.offsetParent;
	}

	return out;
}

function ifNull (value, replace) {

	return value == undefined || value == null ? replace : value;
}

function callIfDefined (func, args) {

	if(args == undefined) var args = [];

	if(typeof func == 'function') func.apply(null, args);
}

function clone (obj, add) {

	if(typeof(obj) != 'object' || null == obj) return obj;
	if(undefined == add) add = {};

	var out = {};

	for(var i in add) out[i] = add[i];
	for(var i in obj) out[i] = clone(obj[i]);

	return out;
}

function extend (subClass, baseClass) {

   function inheritance () { }
   inheritance.prototype = baseClass.prototype;

   subClass.prototype = new inheritance();
   subClass.prototype.constructor = subClass;
   subClass.baseConstructor = baseClass;
   subClass.superClass = baseClass.prototype;
}

function getCookie (name) {

	var out 	= null;
	var cookies	= document.cookie;

	if (cookies && cookies != '') {
		var cookies = cookies.split(';');
		for (var i = 0; i < cookies.length; i++) {
			var pair	= trim(cookies[i]).split('=');
			var key		= pair[0];
			var value	= pair[1];
			if(key == name) {
				out = decodeURIComponent(value);
				break;
			}
		}
	}

	return out;
}

function setCookie (name, value, options) {

	var options = options || {};

	var cookie	= [name + '=' + encodeURIComponent(value)];

	if(options.expires)	cookie.push('expires=' + options.expires);
	if(options.path)	cookie.push('path=' + options.path);
	if(options.domain)	cookie.push('domain=' + options.domain);
	if(options.secure)	cookie.push('secure');

	document.cookie = cookie.join('; ');
}

function launchWindow (url, name, obj) {

	if(name == undefined) name = 'win';

	var properties = {location:'no', status:'no', toolbar:'no', resizable:'yes', menubar:'no', scrollbars:'yes'};

	for(var i in obj) {

		properties[i]	= obj[i];
	}

	var list	= [];

	for(var i in properties) {

		var value	= properties[i] == undefined ? i : i + '=' + properties[i];

		list.push(value);
	}

	var win = window.open(url, name, list.join(','));

	if(!win) alert('This action requires a popup window. Please disable any popup blocking software to continue.');

	win.focus();

	return win;
}

function createHttpRequest () {

    if (window.XMLHttpRequest) {

        return new XMLHttpRequest();

    } else if (window.ActiveXObject) {

		var msxmls = ['Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];

		for(var i = 0; i < msxmls.length; i++) {
			try {
				return new ActiveXObject(msxmls[i]);
			} catch (e) {
			}
		}
    }

	alert("XMLHttpRequest not supported");

	return false;
}

function sendHttpRequest (url, callback, method, params) {

	if(undefined == method) var method = 'GET';
	if(undefined == params) var params = {};

	var request	= createHttpRequest();
	var body	= null;

	//request.__callback	= callback;
	window.__httpCallback	= callback; // HACK: IE6 won't allow for defining a property on ActiveX object, so we must store our callback in the global window space
	request.onreadystatechange	= function () {

		if(4 == request.readyState) {

			// this.__callback(request.responseText, request.status);
			window.__httpCallback(request.responseText, request.status); // HACK: see above
		}
	};

	request.open(method, url, true);

	if('POST' == method) {

		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		body	= '';

		for(var key in params) {

			body += key + '=' + escape(params[key]) + '&';
		}

		if(empty(body)) body = null;
	}

	request.send(body);
}
