/**
 * Only enable Javascript functionality if all required features are supported.
 */

function isJsEnabled() {
	if (typeof document.jsEnabled == 'undefined') {
	// Note: ! casts to boolean implicitly.
		document.jsEnabled = !(
			!document.getElementsByTagName ||
			!document.createElement        ||
    		!document.createTextNode       ||
			!document.getElementById
		);
  }
  return document.jsEnabled;
}

/* $()
 *
 * Returns elements by their ids
*/
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1) 
			return element;
		elements.push(element);
  		}
	return elements;
}


/* getXY()
 *
 * Returns the position of any element as an object.
 *
 * Typical usage:
 * var pos = getXY(object);
 * alert(pos.x + " " +pos.y);
 */
function getXY(el) {
        var x = el.offsetLeft;
        var y = el.offsetTop;
        if (el.offsetParent != null) {
                var pos = getXY(el.offsetParent);
                x += pos.x;
                y += pos.y;
        }
        return {x: x, y: y}
}

/**
 * Make IE's XMLHTTP object accessible through XMLHttpRequest()
 */
if (typeof XMLHttpRequest == 'undefined') {
		window.XMLHttpRequest = function () {
			var msxmls = ['MSXML3', 'MSXML2', 'Microsoft']
			for (var i=0; i < msxmls.length; i++) {
			try {
				return new ActiveXObject(msxmls[i]+'.XMLHTTP')
			}
			catch (e) { }
		}
		throw new Error("No XML component installed in here!")
	}
}

/**
 * Creates an HTTP request and sends the response to the callback function
 */
function HTTPGet(uri, callbackFunction, callbackParameter) {
	var xmlHttp = new XMLHttpRequest();
	var bAsync = true;
	if (!callbackFunction)
		bAsync = false;

	xmlHttp.open('GET', uri, bAsync);
	xmlHttp.send(null);

	
	if (bAsync) {
		if (callbackFunction) {
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				callbackFunction(xmlHttp.responseText, xmlHttp, callbackParameter)
			}
		}
	}
    return true;
  }
  else {
    return xmlHttp.responseText;
  }
}

/**
 * Creates an HTTP POST request and sends the response to the callback function
 */
function HTTPPost(uri, object, callback_function, callback_parameter) {
	var xmlhttp = new XMLHttpRequest();
	var bAsync = true;
	if (!callback_function)
		bAsync = false;
	
	xmlhttp.open('POST', uri, bAsync);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.setRequestHeader('Accept-Charset','ISO-8859-7');
	var form_contents = '';
	for (var i in object)
		form_contents += (form_contents ? '&' : '') + i + '=' + escape(object[i]);
	
	xmlhttp.send(form_contents);

	if (bAsync) {
		if (callback_function)
			xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4)
			callback_function(xmlhttp.responseText, xmlhttp, callback_parameter)
	}
    return true;
  } else
    return xmlhttp.responseText;
}

/**
 * Adds a function to the window onload event
 */
function addLoadEvent(func) {
  var oldOnload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      oldOnload();
      func();
    }
  }
}



/**
 * Returns true if an element has a specified class name
 */
function hasClass(node, className) {
  if (node.className == className) {
    return true;
  }
  var reg = new RegExp('(^| )'+ className +'($| )')
  if (reg.test(node.className)) {
    return true;
  }
  return false;
}

/**
 * Adds a class name to an element
 */
function addClass(node, className) {
  if (hasClass(node, className)) {
    return false;
  }
  node.className += ' '+ className;
  return true;
}

/**
 * Removes a class name from an element
 */
function removeClass(node, className) {
  if (!hasClass(node, className)) {
    return false;
  }
  node.className = eregReplace('(^| )'+ className +'($| )', '', node.className);
  return true;
}

/**
 * Toggles a class name on or off for an element
 */
function toggleClass(node, className) {
  if (!removeClass(node, className) && !addClass(node, className)) {

    return false;
  }
  return true;
}

/**
 * Emulate PHP's ereg_replace function in javascript
 */
function eregReplace(search, replace, subject) {
  return subject.replace(new RegExp(search,'g'), replace);
}

/**
 * Removes an element from the page
 */
function removeNode(node) {
  if (typeof node == 'string') {
    node = document.getElementById(node);
  }
  if (node && node.parentNode) {
    return node.parentNode.removeChild(node);
  }
  else {
    return false;
  }
}


/* PROTOTYPES */
String.prototype.trim = function () {
	return this.replace(/^\s*|\s*$/g, "")
}
/*
String.prototype.stripTags = function () {
	return this.replace( /<\/?[^]+>/gi, "" );
}
*/
if (!Array.prototype.push) {
	Array.prototype.push = function() {
		var startLength = this.length;
		for (var i = 0; i < arguments.length; i++)
      		this[startLength + i] = arguments[i];
	  	return this.length;
	}
}


/**
 * makeTransparent
 *
 * Applies transparency to objects
 */
function makeTransparent ( obj, opacity ) {
	if ( !opacity ) opacity = 100;

  	if ( navigator.userAgent.indexOf( "Firefox" ) != -1 )
		if (opacity == 100) { opacity = 99.999; } // This is majorly retarded

	obj.style.filter			= "alpha(opacity=" + opacity.toString() + ")"; //IE/Win
	obj.style.MozOpacity 		= parseInt( opacity )/100;   // Older Mozilla+Firefox
	obj.style.KhtmlOpacity		= parseInt( opacity )/100; // Safari 1.1 or lower, Konqueror
	obj.style.opacity			= parseInt( opacity )/100; // Safari 1.2, Firefox+Mozilla
} 

function getStyle(el, style) {

        if (!document.getElementById || !el) return;

        if (document.defaultView
                && document.defaultView.GetComputedStyle) {
        return document.defaultView.
                GetComputedStyle(el, "").GetPropertyValue(style);
        }
        else if (el.currentStyle) {
                return el.currentStyle[style];
        }
        else {
                return 0;
        }
 }
