function setOpacity(e, o) {
	if(e && e.style.opacity != null) {
		e.style.opacity = o;
	} else if(e) {
		e.style.filter = 'alpha(opacity = ' + o * 100 + ')';
		if(e && e.currentStyle)
			if(!e.currentStyle.hasLayout)
				e.style.zoom = '1';
	}
}

function toggleVisibility(e) {
	if(e.style.display == "block")
		e.style.display = "none";
	else
		e.style.display = "block";
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function getPosition(e) {
	var l = 0;
	var t = 0;

	do {
		l += e.offsetLeft;
		t += e.offsetTop;
	} while ( e = e.offsetParent );

	return [l, t];
}

function makeElementCoverScreen(e) {
	var y = 0; // 32 for bugpanel
	setFixedPosition(e, 0, y);
	if(getInternetExplorerVersion() != -1 && getInternetExplorerVersion() <= 6.0)
		e.style.height = (viewableSize()[1] - y) + "px";
	else
		e.style.height = "100%";
	e.style.width = "100%";
}

function setFixedPosition(e, x, y) {
	if(getInternetExplorerVersion() != -1 && getInternetExplorerVersion() <= 6.0)
		// IE6 doesn't do fixed position, so we do the next best thing
		e.style.position = "absolute";
	else
		e.style.position = "fixed";
	if(y >= 0)
		e.style.top = y + "px";
	else
		e.style.bottom = -y + "px";
	if(x >= 0)
		e.style.left = x + "px";
	else
		e.style.right = -x + "px";

}

function viewableSize() {
	var viewableWidth = 0;
	if(window.innerWidth)
		viewableWidth = innerWidth;
	else if(document.body.clientWidth)
		viewableWidth = document.body.clientWidth;
	var viewableHeight = 0;
	if(window.innerHeight)
		viewableHeight = innerHeight;
	else if(document.body.clientHeight)
		viewableHeight = document.body.clientHeight;

	return new Array(viewableWidth, viewableHeight);
}

function get(url, callback) {
	var xmlHttp;
	try {   
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}   
	catch (e) {   
		// Internet Explorer
		try {   
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}   
		catch (e) {   
			try {   
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}   
			catch (e) {   
				return false;
			}   
		}   
	}   

	xmlHttp.onreadystatechange=function() {   
		if(xmlHttp.readyState==4) {   
			callback(xmlHttp.responseText);
		}   
	}   

	xmlHttp.open("GET", url ,true);
	xmlHttp.setRequestHeader("Content-type", "text/plain");
	xmlHttp.send('');

	return xmlHttp;
}
/*
function syncGet(url, callback) {
	var xmlHttp;
	try {   
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}   
	catch (e) {   
		// Internet Explorer
		try {   
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}   
		catch (e) {   
			try {   
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}   
			catch (e) {   
				return false;
			}   
		}   
	}   

	xmlHttp.onreadystatechange=function() {   
		if(xmlHttp.readyState==4) {   
			callback(xmlHttp.responseText);
		}   
	}   

	xmlHttp.open("GET", url ,false);
	xmlHttp.setRequestHeader("Content-type", "text/plain");
	xmlHttp.send('');

	return xmlHttp;
}*/

function post(url, args, callback) {
	var xmlHttp;
	try {   
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}     
	catch (e) {                                                                                                                                     
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			if(callback)
			callback(xmlHttp.responseText);
		}
	}

	xmlHttp.open("POST", url ,true);
	xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

	var a = "";
	var outputted = false;
	for(i in args) {
		if(outputted) {
			a += "&";
		} else outputted = true;
		var arg = args[i];
		a += encodeURIComponent(i) + "=";
		a += encodeURIComponent(arg);
	}

	xmlHttp.send(a);

	return xmlHttp;
}

function syncPost(url, args, callback) {
	var xmlHttp;
	try {   
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}     
	catch (e) {                                                                                                                                     
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			if(callback)
			callback(xmlHttp.responseText);
		}
	}

	xmlHttp.open("POST", url ,false);
	xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

	var a = "";
	var outputted = false;
	for(i in args) {
		if(outputted) {
			a += "&";
		} else outputted = true;
		var arg = encodeURIComponent(args[i]);
		a += i + "=";
		a += encodeURIComponent(arg);
	}

	xmlHttp.send(a);

	return xmlHttp;
}

function hasClass(ele, cls) {
        return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

function addClass(ele,cls) {
        if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
        if (hasClass(ele,cls)) {
                var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
                ele.className=ele.className.replace(reg,' ');
        }
}


function swapNodes(item1,item2) {
 // We need a clone of the node we want to swap
 var itemtmp = item1.cloneNode(1);
 
 // We also need the parentNode of the items we are going to swap.
 var parent = item1.parentNode;
 
 // First replace the second node with the copy of the first node
 // which returns a the new node
 item2 = parent.replaceChild(itemtmp,item2);
 
 //Then we need to replace the first node with the new second node
 parent.replaceChild(item2,item1);
 
 // And finally replace the first item with it's copy so that we
 // still use the old nodes but in the new order. This is the reason
 // we don't need to update our Behaviours since we still have 
 // the same nodes.
 parent.replaceChild(item1,itemtmp);
 
 // Free up some memory, we don't want unused nodes in our document.
 itemtmp = null;
} 

function filterImproperKeys(e) {
	if(!e)
		e = window.event;
	if(!e)
		return;
	var code;
	if(e.keyCode)
		code = e.keyCode;
	else if (e.which) code = e.which;

	if(code == 13 || code == 9 || code == 8 || code ==37 || code == 39 || code == 127)
		return true;

	if( (code >= 65 && code <= 65+26) ||
	    (code >= 97 && code <= 97+26) ||
	    (code >= 48 && code <= 48+10) )
	    	return true;

	return false;
}

function filterNonNumbers(e) {
	if(!e)
		e = window.event;
	if(!e)
		return;
	var code;
	if(e.keyCode)
		code = e.keyCode;
	else if (e.which) code = e.which;

	if(code == 13 || code == 9 || code == 8 || code ==37 || code == 39 || code == 127)
		return true;

	if( (code >= 48 && code <= 48+10) )
	    	return true;

	return false;
}


function getRadioValue(group) {
	var i;
	if(typeof group != "undefined" &&
		typeof group.length != "undefined")
	for(i = 0; i < group.length; i++) {
		if(group[i].checked)
			return group[i].value;
	}

	if(typeof group.value != "undefined")
		return group.value

	return null;
}

