function onloadadd(func)
{
	if (typeof(window.onload) != 'function')
	{
		window.onload = func;
	}
	else
	{
		var oldonload = window.onload;
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

addEvent = function(o, e, f, s) {
	var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
	r[r.length] = [f, s || o], o[e] = function(e)
	{
		try
		{
			(e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
			e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
			e.target || (e.target = e.srcElement || null);
			e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
		}
		catch(f)
		{}
		for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
			return e = null, !!d;
	}
};

doenterAsTab = function() {
    function next(e){
        var l, i, f, j, o = e.target;
                //ubah disini
        if(e.key == 13 && !/textarea|select|button|submit/i.test(o.type)) {
            for(i = l = (f = o.form.elements).length; f[--i] != o;);
            for(j = i; (j = (j + 1) % l) != i && (!f[j].type || f[j].disabled || f[j].readOnly || f[j].type.toLowerCase() == "hidden"););
            e.preventDefault(), j != i && f[j].focus();
	    if (typeof(o.onkeyup) == 'function')
		o.onkeyup(e);
        };
    }
    for(var f, i = (f = document.forms).length; i; addEvent(f[--i], "keypress", next));
};

function nl2br(str)
{
	return str.replace(/\n/gi, '<br>');
}

function enterAsTab()
{
	var oldonload = window.onload;

	if (typeof(window.onload) != 'function')
	{
		window.onload = function () {doenterAsTab();}
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			doenterAsTab();
		}
	}
}

(function()
{
	var UA = navigator.userAgent;
	is_gecko = /gecko/i.test(UA);
	is_opera = /opera/i.test(UA);
	is_mac = /mac_powerpc/i.test(UA);
	is_ie = /msie/i.test(UA) && !is_opera && !is_gecko && !is_mac;
	is_ie5 = is_ie && /msie 5\.[^5]/i.test(UA);
	is_nn4 = document.layers ? true : false;
})();

function getX(o)
{
	if (is_nn4)
	{
		return parseInt(o.left);
	}
	else if (is_ie)
	{
		return parseInt(o.style.pixelLeft);
	}
	else
	{
		return parseInt(o.style.left);
	}
}

function getY(o)
{
	if (is_nn4)
	{
		return parseInt(o.top);
	}
	else if (is_ie)
	{
		return parseInt(o.style.pixelTop);
	}
	else
	{
		return parseInt(o.style.top);
	}
}

function setXY(o, x, y)
{
	if (is_nn4)
	{
		o.left = x;
		o.top = y;
	}
	else if (is_ie)
	{
		o.style.pixelLeft = x;
		o.style.pixelTop = y;
	}
	else
	{
		o.style.left = x + "px";
		o.style.top = y + "px";
	}
}

function topPos(e)
{
	var x = 0;
	var a = [];
	var s = '';
	while (e)
	{
		x += e.offsetTop;

		s += e.nodeName+':'+e.offsetTop+' ';
		a[a.length]=e;
		e = e.offsetParent;
	}
/*
	var y =0;
	for (i=a.length-1;i>=0;i--)
	{
		var objDebug;
		y += a[i].offsetTop;
		objDebug = createElement(document.body, 'div', {}, {position:"absolute",border:"1px solid rgb(0,0,0)"});

		setXY(objDebug, 0, y);
		objDebug.style.visibility = "visible";
		objDebug.style.width = 10+"px";
	}
*/
	return x;
}

function leftPos(e)
{
	var x = 0;
	while (e)
	{
		x += e.offsetLeft;
		e = e.offsetParent;
	}
	return x;
}

function getHtml(o)
{
	if (!o)
		return "";

	if (o['innerContent'])
	{
		return o.innerContent;
	}
	else if (o['innerHTML'])
	{
		return o.innerHTML;
	}
	else
	{
		return o.innerText;
	}
}

function setHtml(object,html)
{
	if (!object)
		return;

	if (object["innerHTML"])
	{
		object.innerHTML = html;
	}
	else if (object['innerText'])
	{
		object.innerText = html;
	}
	else if (object["innerContent"])
	{
		object.innerContent = html;
	}
}

function ut8(text)
{
	if (encodeURIComponent)
	{
	    	return encodeURIComponent(text);
	}
	else
	{
		return escape(text);
	}
}

function setStyles(element, style)
{
	var i;
	for (i in style)
		element.style[i] = style[i];
}

function getPosition(e)
{
	var cursor = {x:0, y:0};
	if (e.pageX || e.pageY)
	{
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	}
	else
	{
		var de = document.documentElement;
		var b = document.body;
		cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
		cursor.y = e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
	}

	return cursor;
}

function maxpage()
{
	var x = 0;
	var y = 0;
	if (window.innerHeight && window.scrollMaxY)
	{// Firefox
		y = window.innerHeight + window.scrollMaxY;
		x = window.innerWidth + window.scrollMaxX;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		y = document.body.scrollHeight;
		x = document.body.scrollWidth;
	}
	else
	{ // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		y = document.body.offsetHeight;
		x = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = {x:x,y:y};
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

function scrollwidth()
{
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;

   return w ? w : 0;
}

function scrollheight()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;

   return h ? h : 0;
}

function pause(msec)
{
	var date = new Date();
	var curDate = null;

	do
	{
		curDate = new Date();
	}
	while(curDate-date < msec);
}

function token(str, delim, ind)
{
	var i = 1;
	var p;
	for (;;)
	{
		p = str.indexOf(delim);
		if (p == -1)
		{
			if (i == ind)
				return str;
			else
				return '';
		}
		if (i == ind)
			return str.substr(0,p);
		str = str.substr(p+1);
		i ++;
	}
}

function setText(object, text)
{
	if (!object)
		return;

	if (object["innerText"])
	{
		object.innerText = text;
	}
	else if (object['innerHTML'])
	{
		object.innerHTML = text;
	}
	else if (object["innerContent"])
	{
		object.innerContent = text;
	}
}

function getText(object)
{
	if (object["innerContent"])
	{
		return object.innerContent;
	}
	else if (object["innerText"])
	{
		return object.innerText;
	}
	else
	{
		return object.innerHTML;
	}
}

function text(object, text)
{
	if (!object)
		return;

	if (object["innerContent"])
	{
		object.innerContent = text;
	}
	else if (object["innerText"])
	{
		object.innerText = text;
	}
	else
	{
		object.innerHTML = text;
	}
}

function dump(arr,level)
{
	var dumped_text = "";
	if (!level)
		level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	var value;
	for( var j=0; j<level+1; j++)
		level_padding += "    ";

	var nl = '<br>';

	if(typeof(arr) == 'object')
	{ //Array/Hashes/Objects
		for (var it in arr)
		{
			if (!it)
				continue;

			value = arr[it];

			if (typeof value == 'object' )
			{
				dumped_text += level_padding + it + " ..."+nl;
				if (typeof arr != 'object')
					dumped_text += dump(value,level+1);
			}
			else
			{
				if (typeof value == "function")
					continue;

					dumped_text += level_padding + it + " => " + value + nl;
			}
		}
	}
	else
	{ //Stings/Chars/Numbers etc.
		dumped_text = arr+" ("+typeof(arr)+")";
	}
	return dumped_text;
}

function dd(arr)
{
	var objDebug = byid('debug');
	if (!objDebug)
	{
		objDebug = createfrom('<div id=debug></div>');
		before(document.body, objDebug);
		objDebug = byid('debug');
	}

	setText(objDebug, dump(arr,10));
}

function assign(objId, objValue)
{
	var obj = byid(objId);
	if (obj)
		obj.value = objValue;
}

function iremove(obj)
{
	obj.parentNode.removeChild(obj);
}

function nodeName(elem, name)
{
	return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
}

// args is for internal usage only
function each( obj, fn, args )
{
	if ( obj.length == undefined )
//		for ( var i in obj )
			fn.apply( obj[i], args || [i, obj[i]] );
	else
		for ( var i = 0, ol = obj.length; i < ol; i++ )
			if ( fn.apply( obj[i], args || [i, obj[i]] ) === false ) break;
	return obj;
}

function DOMmanip(o, args, table, dir, fn)
{
	var clone = o.length > 1;
	var a = createfrom(args);
	if ( dir < 0 )
		a.reverse();

	return each(o, function(){
		var obj = o;

		if ( table && nodeName(o, "table") && nodeName(a[0], "tr") )
			obj = o.getElementsByTagName("tbody")[0] || o.appendChild(document.createElement("tbody"));

		each( a, function(){
			fn.apply( obj, [ clone ? this.cloneNode(true) : this ] );
		})});
}


function iparent(obj, type)
{
	cur = obj.parentNode;
	while ( cur && cur != document )
	{
		if (cur.nodeName == type)
			return cur;
		cur = cur.parentNode;
	}
}

function byid(id)
{
	var object = document.getElementById(id);
	return object;
}

function before(obj, ins)
{
	return DOMmanip(obj, ins, false, 1, function(a) {
		obj.parentNode.insertBefore( a, obj );
		});
}

function createfrom(s)
{
	if (!s)
		return;

	var r = [];

	if ( typeof s == "string" )
	{
		var b = navigator.userAgent.toLowerCase();

		// Figure out what browser is being used
		var browser = {
			safari: /webkit/.test(b),
			opera: /opera/.test(b),
			msie: /msie/.test(b) && !/opera/.test(b),
			mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
		};

		var wrap =
			 // option or optgroup
			!s.indexOf("<opt") &&
			[1, "<select>", "</select>"] ||

			(!s.indexOf("<thead") || !s.indexOf("<tbody") || !s.indexOf("<tfoot")) &&
			[1, "<table>", "</table>"] ||

			!s.indexOf("<tr") &&
			[2, "<table><tbody>", "</tbody></table>"] ||

		 	// <thead> matched above
			(!s.indexOf("<td") || !s.indexOf("<th")) &&
			[3, "<table><tbody><tr>", "</tr></tbody></table>"] ||

			[0,"",""];

		div = create("div");
		div.innerHTML = wrap[1] + s + wrap[2];

		// Move to the right depth
		while ( wrap[0]-- )
			div = div.firstChild;

		// Remove IE's autoinserted <tbody> from table fragments
		if ( browser.msie )
		{
			var tb = [];
			// String was a <table>, *may* have spurious <tbody>
			if ( !s.indexOf("<table") && s.indexOf("<tbody") < 0 )
				tb = div.firstChild && div.firstChild.childNodes;

			// String was a bare <thead> or <tfoot>
			else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 )
				tb = div.childNodes;

			for ( var n = tb.length-1; n >= 0 ; --n )
				if ( jQuery.nodeName(tb[n], "tbody") && !tb[n].childNodes.length )
					tb[n].parentNode.removeChild(tb[n]);
		}

		for (var i=0, l=div.childNodes.length; i<l; i++)
			r.push(div.childNodes[i]);
	}
	else
	{
		r = s;
	}

	return r;
}


function create(type)
{
	if (document.createElementNS)
		return document.createElementNS("http://www.w3.org/1999/xhtml", type);
	else
		return document.createElement(type);
}
