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

function trim(value) {
  value = value.replace(/^\s+/, '');
  value = value.replace(/\s+$/, '');
  return value;
}

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 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 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;
}

function validateEmail(email) {
	return (email.match(/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i)) ? true : false;
}

function validateURL(url) {
		url = (url.match(/^https?:\/\/?/)) ? 'http://'+url : url;
		return (url.match(/^https?\:\/\/(www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/)) ? true : false;
}
