/*
    Framework javascript functions.
    DON'T MODIFY!
*/

function var_dump_str(val, ofs, depth)
{
	try
	{
		if (typeof(val) == "number") return "(number) "+val;
		if (typeof(val) == "string") return "(string) \""+val+"\"";
		if (typeof(val) == "boolean") return "(boolean) "+val;
		if (typeof(val) == "function") return "(* function *)";
		if (typeof(val) == "undefined") return "(undefined)";

		// object
		var prop;
		var str = "(object)<br>";

		for (i = 0; i < ofs; i++) str += "  ";
		str += "{<br>";

		for (prop in val)
		{
			for (i = 0; i <= ofs; i++) str += "  ";
			str += prop+": ";

			if (depth) str += var_dump_str(val[prop], ofs+1, depth-1);
			else str += "(* depth *)";

			str += "<br>";
		}

		for (i = 0; i < ofs; i++) str += "  ";
		str += "}";
		return str;
	}
	catch (e)
	{
		return "(* exception "+e+" *)";
	}
}

function var_dump(val, depth)
{
	if (typeof(depth) == "undefined") depth = 2;
	document.write("<pre>"+var_dump_str(val, 0, depth)+"</pre>");
}


function clone(obj)
{
    for (i in obj)
    {
        if (typeof obj[i] == 'object') this[i] = new clone(obj[i]);
        else this[i] = obj[i];
    }
}

function __doPostBack(fname, action)
{
	document.forms[fname].elements["_f_"+fname+"_action"].value = action;
	document.forms[fname].submit();
}

function __chbCheckOthers(th, othersId)
{
	var k, el, els=th.form.elements;
	for (k = 0; k < els.length; k++) {
		el = els[k];
		if (String(el.id).indexOf(othersId)!=-1 && !el.disabled) el.checked = th.checked;
	}
}

function __chbCheckMain(th, mainId, othersId)
{
	var k, el, cnt=0, chk=0, elm=undefined, els=th.form.elements;

	for (k = 0; k < els.length; k++)
	{
		el = els[k];
		if (String(el.id).indexOf(othersId) != -1) {
			cnt++;
			if (el.checked || el.disabled) chk++;
		}
		else if (el.id == mainId) {
			elm = el;
		}
	}

	if (elm!=undefined && cnt!=0) elm.checked = (cnt == chk);
}

function __chbIsAllEmpty(fname, chbId)
{
	var k, el, chk=0, els=document.forms[fname].elements;
	for (k = 0; k < els.length; k++) {
		el = els[k];
		if (String(el.id).indexOf(chbId)!=-1 && el.checked) return false;
	}
	return true;
}

function __chbIsOnlyOne(fname, chbId)
{
	var k, el, chk=0, els=document.forms[fname].elements;
	for (k = 0; k < els.length; k++) {
		el = els[k];
		if (String(el.id).indexOf(chbId)!=-1 && el.checked) chk++;
	}
	return (chk == 1);
}

function __chbIsAtLeastOne(fname, chbId)
{
	var k, el, chk=0, els=document.forms[fname].elements;
	for (k = 0; k < els.length; k++) {
		el = els[k];
		if (String(el.id).indexOf(chbId)!=-1 && el.checked) return true;
	}
	return false;
}

function __popUp(src, w, h)
{
	var l = (screen.availWidth - w) / 2;
	var t = (screen.availHeight - h) / 2;
	window.open(src, "_blank", "scrollbars=yes,toolbar=yes,status=yes,resizable=yes,width="+w+",height="+h+",top="+t+",left="+l);
}

function __getFormElementById(frm, eid)
{
	var k, el, els=frm.elements;
	for (k = 0; k < els.length; k++) {
		el = els[k];
		if (el.id == eid) return el;
	}
	return null;
}

function __getLeftPos(el)
{
	var res;
	for (res = 0; el; el = el.offsetParent) res += el.offsetLeft;
	return res;
}

function __getTopPos(el)
{
	var res;
	for (res = 0; el; el = el.offsetParent) res += el.offsetTop;
	return res;
}

/*
    For pop-up windows
*/

function __adjustPopup()
{
	var w, h, fw, fh, dw, dh;

	if (document.all)
	{
		fw = document.body.clientWidth;
		fh = document.body.clientHeight;
		window.resizeTo(fw, fh);
		dw = fw - document.body.clientWidth;
		dh = fh - document.body.clientHeight;
	}
	else
	{
		fw = window.innerWidth;
		fh = window.innerHeight;
		window.resizeTo(fw, fh);
		dw = fw - window.innerWidth;
		dh = fh - window.innerHeight;
	}

	w = fw + dw;
	h = fh + dh;	// +8

	if (h >= screen.availHeight) w += 16;
	if (w >= screen.availWidth)  h += 16;

	w = Math.min(w, screen.availWidth);
	h = Math.min(h, screen.availHeight);

	window.resizeTo(w, h);
	window.moveTo((screen.availWidth-w)/2, (screen.availHeight-h)/2);
}


function _el(e) {
	if(typeof(e)!='string') return e;
	if(document.getElementById) e=document.getElementById(e);
	else if(document.all) e=document.all[e];
	else e=null;
	return e;
}

function xPrevSib(ele, tag)
{
  var s = ele ? ele.previousSibling : null;
  if (tag) while (s && s.nodeName != tag) { s = s.previousSibling; }
  else while (s && s.nodeType != 1) { s = s.previousSibling; }
  return s;
}

function xNextSib(ele, tag)
{
  var s = ele ? ele.nextSibling : null;
  if (tag) while (s && s.nodeName != tag) { s = s.nextSibling; }
  else while (s && s.nodeType != 1) { s = s.nextSibling; }
  return s;
}
