/**
*
*    GLOBAL
*        JAVASCRIPT
*            FILE
*
**/

function url_escape(url) {
	url = escape(url);
	return url.replace(/\+/g, '%2B');
}

$ = function(id) { return document.getElementById(id); }

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unurl_escape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +url_escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

function getElementsByClassName(clsName){
	/**
	* This function returns an array containing references to 
	* all elements that have a Class Name that matches the given
	* /clsName/ parameter.
	**/
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}

function createXMLHttpRequest() {
	var request = false;
	/* Does this browser support the XMLHttpRequest object? */
	if (window.XMLHttpRequest) {
		if (typeof XMLHttpRequest != 'undefined') {
			/* Try to create a new XMLHttpRequest object */
			try {
				request = new XMLHttpRequest();
			} catch (e) {
				request = false;
			}
		}
	}
	/* Does this browser support ActiveX objects? */
	else if (window.ActiveXObject) {
		/* Try to create a new ActiveX XMLHTTP object */
		try {
			request = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				request = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
				request = false;
			}
		}
	}
	return request;
}

function requestData(obj, url, data, func, method) {
	if (obj) {
		if (method == 'GET')
			obj.open('GET', url + '?' + data, true);
		else {
			obj.open('POST', url, true);
			obj.setRequestHeader('Content-Type',
  'application/x-www-form-urlencoded');
		}
		obj.onreadystatechange = func;
		if (method == 'GET')
			obj.send('');
		else 
			obj.send(data);
	}
}

function reverse() {
	/**
	* This function reverses the text of the string it's used on
	* It is accessible through the String object, example:
	* var str = 'This is a test';
	* document.write(str.reverse());
	* This will write "tset a si sihT" on the page.
	**/
	str = '';
	for (i = this.length; i >= 0;) {
		str += this.charAt(i--);
	}
	return str;
}

function rand(u_min, u_max, u_amount, u_repeat) {
	/**
	* In this function, /u_min/ and /u_max/ represent the minimum and 
	* maximum random value that can be generated respectively.
	* The /u_amount/ is optional. It specifies the amount of numbers to 
	* be generated. When omitted, the default value of 1 is used.
	* The /u_repeat/ is optional as well. It is a boolean value that 
	* specifies whether or not the RNG is allowed to return the 
	* same number twice. When omitted and when /u_amount/ is set
	* to a number greater than 1, the default value of false is used 
	* (meaning duplicate numbers are allowed). When there are less 
	* numbers to be generated than the /u_amount/ setting specified 
	* and /u_repeat/ is set to false, it is set to true instead.
	*
	* The returned result is always an array, even if /u_amount/ is
	* set to 1, or if it has been omitted.
	**/
	if (u_min == null || u_max == null) return;
	var min = Math.floor(Math.min(parseInt(u_max), parseInt(u_min)));
	var max = Math.floor(Math.max(parseInt(u_max), parseInt(u_min))) + 1;
	var range = parseInt(max-min);
	var amount = (u_amount != null) ? Math.abs(u_amount) : 0;
	var res = new Array();
	var repeat = (u_repeat == null || u_repeat == true) ? true : false;
	if (amount > range) { repeat = true; }
	var newnum;
	for (i = 0; i < amount; i++) {
		var newnum = parseInt(parseInt(Math.random() * range) + min);
		if (repeat == false) {
			dup = false;
			for (j = 0; j < res.length; j++) { if (res[j] == newnum) { dup = true; break; } }
			if (dup == false) {	
				res[res.length] = newnum; 
			} else { i--; }
		} else {
			res[res.length] = newnum;
		}
	}
	return res;
}

String.prototype.reversed = reverse;
