/* ---------------------- JavaScript functions ---------------------- */

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function randXY(minVal,maxVal,floatVal) {
	var randVal = minVal+(Math.random()*(maxVal-minVal));
	return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

/* ---------------------- AJAX ---------------------- */

function Connector() {

	var xmlreq = false;
	if (window.XMLHttpRequest) {
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
			}
		}
	}
	return xmlreq;

}

var http = new Array();

function AjaxGet(id, url, callback_function, params){

	http[id] = Connector();

	if ('undefined' == typeof(http[id])){
		alert('Sorry, but your web browser does not support Ajax\nPlease, update your browser with latest version');
		return;
	}

	http[id].onreadystatechange = function() {
		if (http[id].readyState == 4) {
			if (http[id].status == 200) {

				if (http[id].responseText != ''){
					var output  = eval("(" + http[id].responseText + ")");
					callback_function(output);
				}
			}
		}
	}

	if ('undefined' != typeof(params)){
		str = '';	
		for (key in params){
			str += key + '=' + params[key] + '&';
			
		}
		str = str.substr(0,(str.length - 1));
		http[id].open('POST', url, true);
		http[id].setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		http[id].send(str);
	}else{

		http[id].open('GET', url, true);
		http[id].send(null);
	}
	
}

function AjaxReq(id, url){

	http[id] = Connector();

	if ('undefined' == typeof(http[id])){
		alert('Sorry, but your web browser does not support Ajax\nPlease, update your browser with latest version');
		return;
	}
		http[id].open('GET', url, true);
		http[id].send(null);
}
