/*
Author: 		Petar Sabev
Description: 	Base Functions and Objects
Modified Date: 	23.10.2008
*/

function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
		window.onload = func;
	else 
	{
		window.onload = function() 
						{
							oldonload();
							func();
						}
	}
}
function getElementsByClassName(element_class) 
{ 
	var arr = new Array(); 
	var elems = document.getElementsByTagName("*");
	for (i=0; elem = elems[i]; i++ )
		if (elem.className == element_class)
			arr[arr.length] = elem;
	return arr;
}
function setCookie(name, setting)
{
	document.cookie = name + "=" + escape(setting);
}
function setCookiePath(name, setting)
{
	document.cookie = name + "=" + escape(setting) + ";path =/";
}
function deleteCookie(name)
{
	var value = getCookieValue(name);
	var cookie = name + "=" + value + ";";
	var date = new Date();
    document.cookie = cookie + ";expires=" + date.toGMTString() + ";" + ";";
}
function getCookieValue(name)
{
	var label = name + "=";
	var labelLen = label.length;
	var cLen = document.cookie.length;
	var i=0;
	while (i < cLen)
	{
		var j = i + labelLen;
		if (document.cookie.substring(i,j) == label)
		{
			var cEnd = document.cookie.indexOf(";", j)
			if (cEnd == -1)
				cEnd = document.cookie.length;
					
			return unescape(document.cookie.substring(j, cEnd))
		}
		i++;
	}
	return "";
}
function operateCookieSubvalue(name, subvalue)
{
	var value = getCookieValue(name);

	if (value != "")
	{
		var arr = value.split(",");
		var foundid = 0;
		for (var i=0; i<arr.length; i++)
		{
			if(arr[i]==subvalue)
			{
				arr.splice(i,1);
				foundid = 1;
				break;
			}
		}
		if (foundid)
			setCookie(name,arr);
		else
			document.cookie = name+"="+escape(value)+","+escape(subvalue);
	}
	else
		setCookie(name,subvalue)
}
function operateCookieSubvaluePath(name, subvalue)
{
	var value = getCookieValue(name);

	if (value != "")
	{
		var arr = value.split(",");
		var foundid = 0;
		for (var i=0; i<arr.length; i++)
		{
			if(arr[i]==subvalue)
			{
				arr.splice(i,1);
				foundid = 1;
				break;
			}
		}
		if (foundid)
			setCookiePath(name,arr);
		else
			document.cookie = name+"="+escape(value)+","+escape(subvalue)+";path =/";
	}
	else
		setCookiePath(name,subvalue)
}
function ajax()
{
	this.ajaxObject = null;
	this.createAjaxObject = 	function()
								{
									var xmlHttp;
									try{xmlHttp = new XMLHttpRequest();}
									catch(e)
									{
										var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
										'MSXML2.XMLHTTP.5.0',
										'MSXML2.XMLHTTP.4.0',
										'MSXML2.XMLHTTP.3.0',
										'MSXML2.XMLHTTP',
										'Microsoft.XMLHTTP');

										for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
											try{xmlHttp = new ActiveXObject(XmlHttpVersions[i]);}
											catch(e){} 
									}

									if (!xmlHttp)
										this.errorDisplay("Error creating the XMLHttpRequest object.");
									else
										this.ajaxObject = xmlHttp;
								}
	this.createAjaxObject();
	this.request =				function(value,link)
								{
									if(!link)
										link = BASEURL+"index.php/ajax/index";
									var self = this;
									try
									{
										if (this.ajaxObject.readyState == 4 || this.ajaxObject.readyState == 0)
										{
											this.ajaxObject.open("POST", link, true);
											this.ajaxObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
											this.ajaxObject.onreadystatechange = function(){self.handleRequestStateChange()};
											this.ajaxObject.send(value);
										}
									}
									catch(e){this.errorDisplay(e.toString());}
								}
	this.handleRequestStateChange = function()
									{
										if(this.ajaxObject.readyState == 4)
											if(this.ajaxObject.status == 200)
											{
												try{this.handleResult();}
												catch(e){this.errorDisplay("handleResult() function error: " + e.toString());}
											}
											else
												this.errorDisplay("There was a problem retrieving the data!");
									}
	this.handleResult =				function()
									{	
										response = this.ajaxObject.responseXML.documentElement;
										eval(response.nodeName+"(response)");
									}
	this.errorDisplay =				function(message)
									{
										alert(message);
									}
}