//------------------------------------------------------------------------------
// Form & variable handlers
//------------------------------------------------------------------------------

function trim (txt) {
	// Take white spaces and tabs out of txt
	return txt.replace(/^\s+|\s+$/g, "");
}

function dec (n,d) {
	// Converts a (n) numbers string/integer to a float with (d) point decimal
	n = replace(n.toString(), ",", "");
	var t = parseFloat(n);
	if (!t) t = 0;
	t = t.toFixed(d);		
	return t;
}

function strval (n)
{
	if (!n) n = 0;
	n = replace(n.toString(), ",", "");
	var t = parseFloat(n);
	if (!t) t = 0;
	return t;	
}

function comma(amount) {
	// Adds comma to a (amount) numbers string/integer
	amount = replace(amount.toString(), ",", "");
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0],10);
	if(isNaN(i)) return '';
	if(isNaN(d)) d = '';	// JS gives an object error without this =S
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3) 	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function sci (n, z) {
	// Zerofill (n) to (z) digits.  Returns string.
	n = trim(n.toString());
	while (n.length < z) n = "0" + n;
	return n;
}

function replace (haystack, oldNeedle, newNeedle) {
	// Replaces (oldNeedle) to (newNeedle) in a (haystack)
	var i = haystack.indexOf(oldNeedle);
	var r = "";
	if (i == -1) return haystack;
	r += haystack.substring(0,i) + newNeedle;
	if (i + oldNeedle.length < haystack.length)
		r += replace(haystack.substring(i + oldNeedle.length, haystack.length), oldNeedle, newNeedle);
	return r;
}

function defined(variable) {
	// Checks whether or not a variable is defined.  Returns true/false.
	return (!(!(document.getElementById(variable))))
}

function disable_form(frm) {
	// Completely disables a form
	for (var i=0; i<frm.elements.length; i++) {
		frm.elements[i].disabled = true;
}	}

//------------------------------------------------------------------------------
// D/HTML
//------------------------------------------------------------------------------

function urlize (txt)
{
	// Credits to Albion Research Ltd. www.albionresearch.com
	var safechr	= "0123456789" +
				  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
				  "abcdefghijklmnopqrstuvwxyz" +
				  "-_.!~*'()";
	var hex		= "0123456789ABCDEF";
	var encoded	= "";
	for (var i = 0; i < txt.length; i++ )
	{
		var ch = txt.charAt(i);
	    if (ch == " ")
	    {
			encoded += "%20";
		}
		else if (safechr.indexOf(ch) != -1)
		{
			encoded += ch;
		}
		else
		{
			var charCode = ch.charCodeAt(0);
			if (charCode > 255)
			{
				alert("Unicode Character '"+ ch +"' "+
					  "cannot be encoded using standard URL encoding.\n"+
					  "(URL encoding only supports 8-bit characters.)\n"+
					  "A space (+) will be substituted.");
				encoded += "+";
			}
			else
			{
				encoded += "%";
				encoded += hex.charAt((charCode >> 4) & 0xF);
				encoded += hex.charAt(charCode & 0xF);
			}
		}
	}
	return encoded;
};

function writein(id, txt) {
	//if (document.getElementById(id).innerHTML) {
	document.getElementById(id).innerHTML = txt; //}
}

function popup(pg, id) {
	if (!id) id = "popup";
	return window.open(pg, id, "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=360,height=480");
}

function hidediv(id) {
	// Hides an element.  Uses CSS attribute 'display:none'
	if (document.getElementById)	document.getElementById(id).style.display = "none";
	else if (document.layers)		document.id.display = "none";
	else							document.all.id.style.display = "none";
}
	
function showdiv(id) {
	// Unhides an element.  Uses CSS attribute 'display:block'.
	// This may not be useful for inline elements such as <span> etc.
	if (document.getElementById)	document.getElementById(id).style.display = "block";
	else if (document.layers)		document.id.display = "block";
	else							document.all.id.style.display = "block";
}

function showhide(id) {
	// Toggles show and hide an element.
	var itm=null;
	if(document.getElementById)	itm=document.getElementById(id);
	else if(document.all)		itm=document.all[id];
	else if(document.layers)	itm=document.layers[id];
	if(!itm) { /* buat bodo */ }
	else if(itm.style) {
		if(itm.style.display=="none")	itm.style.display="";
		else							itm.style.display="none";
	}
	else itm.visibility="show";
}


//------------------------------------------------------------------------------
// Date & Time functions
//------------------------------------------------------------------------------

function datediff (d1, d2) {
	// Compare the difference (in days) between the two dates.
	d1 = Date.parse(d1);
	d2 = Date.parse(d2);
	var dd = ((d2-d1)/(24*60*60*1000));
	if (isNaN(dd)) dd = 0;
	return dd;
}

function timediff (t1, t2) {
	// Compare the difference (in point hours) between the two times.
	t1 = hrs(t1);
	t2 = hrs(t2);
	t  = t2 - t1;
	if (isNaN(t)) return 0;
	return t;
}

function hrs (t) {
	// Converts 12:30 to float hours 12.5.
	t = timeize(t);
	var h = parseInt(t.substr(0,2),10); if(isNaN(h)) h = 0;
	var m = parseInt(t.substr(3,2),10); if(isNaN(m)) m = 0;
	return (h+m/60);
}

function timeize (t) {
	//Checks a time input string (t) and correct if possible.
	t = trim(t.toString());
	t = replace(t, ":", "");
	t = replace(t, ".", "");
	t = replace(t, "-", "");
	t = sci(t,4);
	var h = parseInt(t.substr(0,2),10); if(isNaN(h)) h = 0;
	var m = parseInt(t.substr(2,2),10); if(isNaN(m)) m = 0;
	while (m > 59) { m = m - 60; h = h + 1; }
	while (h > 23) { h = h - 24; }
	t = sci(h,2)+":"+sci(m,2);
	return t;
}

function dateize (d) {
	// Converts javascript date to CWorks date format
	d = Date.parse(d);
	var months	= new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	return d.getDate() +" "+ months[d.getMonth()] +" "+ now.getFullYear();
}

function hr24 (t) {
	// Converts 01:23 to 123 integer
	return parseInt(replace(t, ":", ""), 10);
}

function show_date (el) {
	// Display today's date within an element (el) tag.
	var now		= new Date();
	var days	= new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	var months	= new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var date	= ((now.getDate() < 10) ? "0" : "") + now.getDate();
	var year	= ((now.getYear() < 1000) ? now.getYear() + 1900 : now.getYear());
	var today	= days[now.getDay()]+", "+date+" "+months[now.getMonth()]+" "+year;
	if (document.layers) {
		document.layers[el].document.write(today);
		document.layers[el].document.close();
	}
	else if (document.all) document.all[el].innerHTML = today;
}


//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Here onwards are for &copy; scripts
//------------------------------------------------------------------------------

Array.prototype.inArray = function( needle ) {
	// Credits to Andrew Hedges
	var i = this.length;
	if ( i > 0 ) do {
		if ( this[i-1] === needle ) return true;
	} while (--i);
	return false;
}