String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
};
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
};

XTransport = function() {
	var result = false;
	
	if (window.XMLHttpRequest)
		result = new XMLHttpRequest();
	else if (window.ActiveXObject)
		try {
			result = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e1) {
			try {
				result = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e2) {
				throw "Couldn't initialize xmlhttp!";
			}
		}

	this.transport = result;

	var xt  = this;
	var xtt = this.transport;
	var reqTimeout = false;
	var th;
	
	this.onReceive = function() {};
	this.onTimeout = function() {};
	
	this.asyncRequest = function(type, url, params, timeout) {
		type	= type || 'GET';
		timeout = timeout || 5000;
				
		th = setTimeout(function() {
			reqTimeout = true;
			xt.onTimeout();
		}
		,timeout);
				
		xtt.onreadystatechange = function() {
			if (xtt.readyState == 4 && !reqTimeout) {
				var data = xtt.responseText;
				clearTimeout(th);
				xt.onReceive(data);
			};
		};
		
		if (type == 'GET') {
			xtt.open(type, url+'?'+params, true);
			xtt.send(null);	
		}
		else if (type == 'POST') {
			xtt.open(type, url, true);
			xtt.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    xtt.setRequestHeader("Content-length", params.length);
		    xtt.setRequestHeader("Connection", "close");
			xtt.send(params);
		}
	};
};

function setCookie(cname, value, expires, path, domain, secure) {
 document.cookie = cname + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookie(cname) {
	cookies = document.cookie.split("; ");
	for (i = 0; i < cookies.length; i++) {
		var cc = cookies[i].split("=");
		if (cname == cc[0])
			return cc[1];
	}
	return null;
}

function bindEvent(obj, event, fun) {
	if (obj.attachEvent)
		obj.attachEvent("on"+event, fun);				
	else
		obj.addEventListener(event, fun, false);
}

function hideElement(elem) {
	if (elem) elem.style.visibility = 'hidden';
}

function showElement(elem) {
	if (elem) elem.style.visibility = 'visible';
}

function pageRef(p) {
	function extractRef(n) {
		var l = n.length;
		
		if (p.indexOf(n) > -1)
			if (p.charAt(p.length-1) == '/')
				return p.substring(p.indexOf(n)+l, p.length-1);
			else
				return p.substring(p.indexOf(n)+l);	
		else
			return null;
	}
	
	var loc = 'http://'+window.location.host;
	var mod = '';
	
	if (p.indexOf('cat') > -1) {
		loc += '/cat/';
		mod = extractRef('p_=') || 'all';
	}
	else if (p.indexOf('search') > -1) {
		loc += "/search.php?";
		mod = (extractRef('?') || '');
	}
	else if (p.indexOf('item')) {
		loc += '/item/';
		if (p.indexOf('item_content') > -1)
			mod = (extractRef('item_content/') || '');
		else
			mod = (extractRef('item/') || '');
	}
	
	
	return loc+mod;
}