/*!
 * Base functions
 * require_once('static/js/base/prototype');
 * @author Tomas Simonaitis, 2006
 */

/*!
 * Submit specified form
 */
function submitForm(form)
{
	try {
		formobj = $(form);
		formobj.submit();
	}
	catch (e) {
		alert(e);
	}
}


/*!
 * Helper function, set value to 1/0 based on checkbox state
 */
function followCheckbox(chbox, fitem)
{
	$(fitem).value = $F(cbox);
}

/*!
 * Helper function to check/uncheck checkbox
 */
function setChecked(chbox, checked)
{
	if (checked == 1) {
		$(chbox).setAttribute('checked', 'checked');
	} else {
		$(chbox).setAttribute('checked', '');
		$(chbox).removeAttribute('checked');
	}
}

/*!
 * Helper toggle elements disabled/enabled state
 * @param elem element or element ID
 * @param state if true disables element, otherwise enables
 */
function disableElement(elem, state)
{
	if (state)
		$(elem).disabled = true; else
			$(elem).disabled = false;
}

/*!
 * Helper function, gets global element position
 */
function getElemPos(elem)
{
	var tmpe = elem;
	var posx = 0;
	var posy = 0;

	tmpe = elem;
	
	while (tmpe) {
		if (tmpe.offsetLeft)
			posx += tmpe.offsetLeft;

		if (tmpe.offsetTop)
			posy += tmpe.offsetTop;
		tmpe = tmpe.offsetParent;
	}

	return { x:posx, y:posy, width:elem.offsetWidth, height:elem.offsetHeight };
}

/*!
 * Helper function, return event position in screen (page) coordinates
 */
function getEventPos(event)
{
	var posx, posy;

	if (typeof(event.pageY) == 'number') {
		posx = event.pageX;
		posy = event.pageY;
	} else if (typeof(event.clientX) == 'number') {
		posx = event.clientX;
		posy = event.clientY;

		var delem = (document.documentElement) ? document.documentElement : document.body;
		if (delem) {
			posx += delem.scrollLeft;
			posy += delem.scrollTop;
		}
	}

	return { x:posx, y:posy };
}

/*!
 * Helper function.
 * Climb parent tree until table owning object is found
 */
function getElementTable(tblobj)
{
	return Try.these (
		function () { while (!(tblobj instanceof HTMLTableElement) && tblobj.parentNode) tblobj = tblobj.parentNode; return tblobj; },
		function () { while (tblobj.tagName != 'TABLE' && tblobj.parentNode) tblobj = tblobj.parentNode; return tblobj; }
	);
}

/*!
 * Helper function.
 * Find child with specified tagname
 * @param tname can be either array with tags to check or single tag name
 */
function getChildByType(obj, tname)
{
	if (!(tname instanceof Array))
		tname = new Array(tname);
	
	var i, ni;
	for (i = 0; i < tname.length; i++)
		for (ni = 0; ni < obj.childNodes.length; ni++)
			if (obj.childNodes[i].tagName == tname[i]) return obj.childNodes[i];
}

/*!
 * Helper function. Decodes base64 string. Assumes UTF-8 encoding
 */
function base64_decode(str)
{
	var key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var block = 0;
	var dec_str = "";
	var dec_count = 0;
	var i = 0, k = 0;
	var cur_char = 0;

	for (i = 0; i < str.length; i++) {
		cur_char = key.indexOf(str.charAt(i));
		if (cur_char != 64) dec_count++;

		block = block | ( cur_char << ( (3-i%4)*6) );

		if ( i%4 == 3 ) {
			for (k=0; k<dec_count - 1; k++)
				dec_str = dec_str + String.fromCharCode( (block >> ((2-k%3)*8)) & 255 );
			
			dec_count = 0;
			block = 0;
		}
	}

	return toUCS(dec_str);
}


/*!
 * Helper function. Converts string from utf8 encoding to UCS
 * based on RFC2279 (http://www.faqs.org/rfcs/rfc2279.html)
 */
function toUCS(str)
{
	var code, i, k;
	var res = "";
	var utf_cnt = 0;
	var utf_code = 0;
	
	for (i = 0; i < str.length; i++) {
		code = str.charCodeAt(i);
		if (code < 128)
			res = res + String.fromCharCode(code); else {
				if (utf_cnt == 0) { // first byte
					k = 7;
					while ( ((code >> k) & 1) == 1) k--;
					utf_cnt = 6 - k;
					utf_code = ( (code << utf_cnt) & 127 ) >> utf_cnt;
				} else { // any other
					utf_code = utf_code << 6;
					utf_code = utf_code | (code & 127);
					if (utf_cnt == 1)
						res = res + String.fromCharCode(utf_code);
					utf_cnt--;
				}
			}
	}
	return res;
}

/*!
 * Helper function, explodes string to array by separating on _ symbol
 */
function stringToArray(str)
{
	return str.split('_');
}

function arrayToString(arr)
{
	str = '';
	var i;
	for (i = 0; i < arr.length; i++)
		if (str == '') 
			str = '' + arr[i]; else
				str = str + '_' + arr[i];
	
	return str;
}



