/*Copyright © 2007 RedZee Search, All rights reserved.*/

function evtCancel(evt) {
	return false;
}
function evtAccept(evt) {
	if (evt) {
		evt.cancelBubble = true;
		evt.stopPropagation();
	}
	else {
		window.event.cancelBubble = true;
	}
	return true;
}

function disableSelection(elem) {
    if ("undefined" != typeof(elem.onselectstart))
    	elem.onselectstart = evtCancel;

    elem.unselectable = "on";
    elem.style.MozUserSelect = "none";
    elem.style.cursor = "default";
}

function enableSelection(elem) {
    if ("undefined" != typeof(elem.onselectstart))
    	elem.onselectstart = evtAccept;

    elem.unselectable = "off";
    elem.style.MozUserSelect = "text";
    elem.style.cursor = "auto";
}

function showDbgMsg(msg) {
	var rsd = document.getElementById("res_dbg");
	if (null == rsd || "undefined" == typeof(rsd))
		return;

	rsd.style.display = "inline";
	rsd.innerHTML += "<br>"+msg;
}

function getScrWidth() {
	if (document.body.offsetWidth)
		return document.body.offsetWidth;

	if (window.innerWidth)
		return window.innerWidth;

	return 0;
}

function getScrHeight() {
	if (document.body.offsetHeight)
		return document.body.offsetHeight;

	if (window.innerHeight)
		return window.innerHeight;

	return 0;
}

function getDomId(n) {
	if (null == n)
		return null;

	if (n.id)
		return n.id;
	else
		return getAttValue(n, "id");
}

function getAttValue(node, attName) {
	var atts = node.attributes;
	if (null == atts || 0 == atts.length)
		return null;

	var u = atts.getNamedItem(attName);
	if (null == u || "undefined" == typeof(u))
		return null;

	if (null == u.value || 0 == u.value.length)
		return null;
	else
		return u.value;
}

function getTs() {
	return (new Date()).getTime();
}

function showWin(name, isFocus, xLoc, yLoc) {
	var n;

	n = document.getElementById(name);
	if (n) {
		n.style.display = "inline";
		posWin(n, xLoc, yLoc);
	}

	if (isFocus) {
		n = document.getElementById(name+"_ok");
		if (n) n.focus();
	}
}
function posWin(n, xLoc, yLoc) {
	var sw = getScrWidth();
	var sh = getScrHeight();

	var nw = n.style.width+"";
	if ("px" == nw.substr(nw.length-2))
		nw = nw.substr(0,nw.length-2);
	nw = nw-0;
	var nh = n.style.height+"";
	if ("px" == nh.substr(nh.length-2))
		nh = nh.substr(0,nh.length-2);
	nh = nh-0;

	if ("center" == xLoc) {
		var nl = (sw/2)-(nw/2);
		nl += "px";
	}
	else {
		nl = xLoc;
	}
	if ("center" == yLoc) {
		var nt = (sh/2)-(nh/2);
		nt += "px";
	}
	else {
		nt = yLoc;
	}

	n.style.left = nl;
	n.style.top = nt;
}
function hideWin(name) {
	var n = document.getElementById(name);
	if (n) n.style.display = "none";
}

function truncate(str, len, tstr) {
	if (null == str || "undefined" == typeof(str) || len >= str.length)
		return str;
	else
		return str.substr(0, len-tstr.length)+tstr;
}

/**** COOKIES ************************************************************/
var COOKIES_EXP = 90;
var COOKIES_DOMAIN = "redzee.com";
var COOKIES_PATH = "/";
var COOKIES_PFX = "RZK__";

function getCookie(key) {
	key = _getCookieKey(key);
	var buff = _getCookies();
	for (var i=0,j=buff.length;j>i;i++) {
		var c = buff[i].split("=");
		if (key == c[0])
			return unescape(c[1]);
	}
	return null;
}
function setCookie(key,val,exp) {
	if(!exp) exp=COOKIES_EXP;
	key = _getCookieKey(key);
	if (null == val) {
		val = "";
		exp*=-1;
	}
	else {
		val = escape(val);
	}

	// expiry
	var expdtm = new Date();
	expdtm.setTime(expdtm.getTime()+(exp*24*60*60*1000));
	exp = "; expires="+expdtm.toGMTString();

	// assign/overwrite/delete
	document.cookie = key+"="+val+exp+"; domain="+COOKIES_DOMAIN+"; path="+COOKIES_PATH;
}
function delCookie(key) {
	setCookie(key, null);
}
function _getCookieKey(key) {
	return COOKIES_PFX+key;
}
function _getCookies() {
	var buff = document.cookie;
	if (null == buff || "undefined" == typeof(buff))
		return new Array();
	else
		return buff.split("; ");
}
function _updtCookies() {
	var buff = _getCookies();
	for (var i=0,j=buff.length;j>i;i++) {
		var c = buff[i].split("=");
		if (null !== c[0] && COOKIES_PFX.length < c[0].length)
		if (COOKIES_PFX == c[0].substr(0,COOKIES_PFX.length))
			setCookie(c[0].substr(COOKIES_PFX.length), unescape(c[1]));
	}
}

// auto-refresh
_updtCookies();
