

function setCookie(name, value, expires) { 
document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString()); 
}

function getCookie(check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = '';
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return '';
	}
}




function delCookie(name) { 
document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/"; 
} 

function in_array(needle, haystack, argStrict) {
var key = '', strict = !!argStrict;
if (strict) {
	for (key in haystack) {
		if (haystack[key] === needle) {
			return true;
			}
		}
	} else {
		for (key in haystack) {
			if (haystack[key] == needle) {
				return true;
				}
			}
	}
return false;
}


function arrayRemoveItemByVal(val, arr, removeEmpty) {
if (typeof removeEmpty == 'undefined') removeEmpty = false;
var res = new Array();
for (var i = 0, len = arr.length; i < len; i++) {
//	if (arr[i] != val | (removeEmpty & (arr[i] != '' & arr[i] != null & typeof arr[i] != 'undefined'))) res[i] = arr[i];
	if (arr[i] != val) {
		if (removeEmpty & arr[i] == '') continue;
			else res[i] = arr[i];
		}
//	if (arr[i] != val) res[i] = arr[i];
	}
return res;
}

function implode(glue, arr) {
var res = '';
var n = 0;
for (var i in arr) {
	if (n > 0) res += glue;
	if (typeof arr[i] == 'function') continue;
	res += arr[i];
	n++;
	}
return res;
}

function getArrayKeyByVal(val, arr) {
for (var i = 0, len = arr.length; i < len; i++) {
	if (arr[i] == val) return i;
	} 
}
function getArrayKeyBySubval(arr, subkey, subval) {
for (i in arr) {
	if (typeof arr[i][subkey] != 'undefined')
		if (arr[i][subkey] == subval) return i;
	}
return false;
}


function domEmptyNode(node) {
if (typeof node == 'undefined') return false;
var childs = node.childNodes;
for (i = 0; i < childs.length; i++) {
	node.removeChild(childs[i]);
	}
return true;
}


function print_r(x, max, sep, l) {

	l = l || 0;
	max = max || 10;
	sep = sep || ' ';

	if (l > max) {
		return "[WARNING: Too much recursion]\n";
	}

	var
		i,
		r = '',
		t = typeof x,
		tab = '';

	if (x === null) {
		r += "(null)\n";
	} else if (t == 'object') {

		l++;

		for (i = 0; i < l; i++) {
			tab += sep;
		}

		if (x && x.length) {
			t = 'array';
		}

		r += '(' + t + ") :\n";

		for (i in x) {
			try {
				r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
			} catch(e) {
				return "[ERROR: " + e + "]\n";
			}
		}

	} else {

		if (t == 'string') {
			if (x == '') {
				x = '(empty)';
			}
		}

		r += '(' + t + ') ' + x + "\n";

	}

	return r;

};

function fillTemplate(tpl, data) {
var res = tpl;
//alert('fillTemplate tpl:' + print_r(tpl));
//alert('fillTemplate data:' + print_r(data));
for (var i in data) {
//		alert('fillTemplate i:' + i + ', data[i]:' + data[i]);
		var re = new RegExp('\<\!\-\-\=(' + i + ')\=\-\-\>', 'g'); 
		res = res.replace(re, data[i])
	}
//alert(res);
return res;
//*/
}


//*/

function clone(s) {
for(p in s)
	this[p] = (typeof(s[p]) == 'object')? new clone(s[p]) : s[p];
}

function str_repeat(str, i) {
if (i < 1) i = 1;
return new Array(i + 1).join(str); 
}

function parseObjTpl(tpl, obj) {
var tpl = String(tpl);
var i = 0;
while (tpl.match(/\<\!\-\-\=[^\<]*\=\-\-\>/)) {
	var re = new RegExp('\<\!\-\-\=([^\<]*)\=\-\-\>', ''); 
	var matches = re.exec(tpl);
	var re2 = new RegExp('(\<\!\-\-\=' + matches[1] + '\=\-\-\>)', '');
	var found = matches[1];
//	if (typeof this[found] == 'undefined') found = '';
	if (found.match(/(\.|\[|\])/gi)) {
		found = eval(found);
//		if (typeof )
		} else found = obj[found];
	tpl = tpl.replace(re2, found);
	i++;
	if (i > 50) { console.error('siteWinTab_parse - Iteration limit exceeded'); return tpl; }
	}
return tpl;
}
