// $Id: util.js,v 1.43 2009-11-20 11:59:16 jbesson Exp $

function get_cookie(name) {

	var dc     = document.cookie.split(';');
	var prefix = name + "=";
	for(var i = 0; i < dc.length; i++) {
		if(dc[i].indexOf(prefix) != -1) {
			return unescape(dc[i].substring(dc[i].indexOf(prefix) + prefix.length));
		}
	}

	return null;

}

function set_cookie(name, value, expires, path, domain, secure) {

	value = escape(value);
	value = stripBadUTF8(value);

	_set_cookie(name, value, expires, path, domain, secure);
}

function set_cookie_no_escape(name, value, expires, path, domain, secure) {

	_set_cookie(name, value, expires, path, domain, secure);
}

function _set_cookie(name, value, expires, path, domain, secure) {

	var curCookie = name + "=" + value +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

	document.cookie = curCookie;
}

if (document.ELEMENT_NODE == null) {
	document.ELEMENT_NODE = 1;
	document.TEXT_NODE = 3;
}

function getTextValue(el) {
	var i, s;
	s = "";
	try {
		for (i = 0; i < el.childNodes.length; i++) {
			if (el.childNodes[i].nodeType == document.TEXT_NODE) {
				s += el.childNodes[i].nodeValue;
			} else if ((el.childNodes[i].nodeType == document.ELEMENT_NODE) && (el.childNodes[i].tagName == "BR")) {
				s += " ";
			} else {
				s += getTextValue(el.childNodes[i]);
			}
		}
	} catch(err){ }

	return normalizeString(s);
}

var whtSpEnds=new RegExp("^\\s*|\\s*$", "g");
var whtSpMult=new RegExp("\\s\\s+", "g");

function normalizeString(s) {
	s = s.replace(whtSpMult, " ");
	s = s.replace(whtSpEnds, "");
	return s;
}

function _f_sort(_a, _b) {

	var a = _a.val;
	var b = _b.val;
	return a < b ? -1 : a > b ? 1 : 0
}

function padNum(num, size) {

	var d, num_zeros, i;
	var new_num = '';

	d = num.indexOf('.');

	if (d == -1) {
		d = num.length;
	}

	num_zeros = size - d;

	for (i = 0; i < num_zeros; i++) {
		new_num += '0';
	}
	new_num += num;

	return new_num;
}

// decode a value from a cookie (convert +s to spaces etc)
function decodeCookieVal(val) {

	if (val) {
		return val.replace(/\+/g, ' ');
	} else {
		return '';
	}
}


// encode a cookie
function encodeCookieVal(val) {

	if (val) {
		return val.replace(/\+/g, "%2B");
	} else {
		return '';
	}
}


function oneYearFromNow() {

	var expires = new Date();
	expires.setFullYear(expires.getFullYear() + 1);
	return expires;
}

function getBaseURL(type) {

	var url;

	if(document.isHTTPS) {
		if(type == 'dirty') {
			url = document.dirty_scgiURL;
		} else if(type == 'hist') {
			url = document.hist_scgiURL;
		} else {
			url = document.scgiURL;
		}
	} else {
		if(type == 'dirty') {
			url = document.dirty_cgiURL;
		} else if(type == 'hist') {
			url = document.hist_cgiURL;
		} else {
			url = document.cgiURL;
		}
	}

	return url;
}


/*
	Remove bad utf8 chars (namely the %u.... for unknown characters)
*/
function stripBadUTF8(text) {

	return text.replace(new RegExp(/%u[0-9A-Fa-f]{4}/g), '');
}

function limit_type_desc(_v) {
	switch (_v) {
		case 'N': return 'No Limit';
		case 'P': return 'Pot Limit';
		case 'F': return 'Fixed Limit';
	}
	return '';
}

function game_code_desc(_v) {
	switch (_v) {
		case 'HOLDEM': return 'Holdem';
		case 'OMAHA': return 'Omaha';
		case 'OMAHA_HILO': return 'Omaha H/L';
	}
	return '';
}

function game_def_id_desc(_v) {
	var desc = ''
	if (document.game_def_code) {
		desc = game_code_desc(document.game_def_code[_v]);
	}
	return desc;
}

/*
	Removes all event handling functions from a node and
	of all its child nodes. This function should be used for
	resolving cycle references on IE 6/7. Just call it before
	removeChild or innerHTML. Taken from Douglas
	Crockford's website 

	Hm maybe we should do a check for the browser and prototype a wrapper
	for removeChild and innerHTML
*/

function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}
