function trim(str) {
	var l = str.length
		
	for (var i = 0; i < l; i++)
		if (str.substr(i, 1) != ' ') break;
		
	for (var j = l - 1; j > 0; j--)
		if (str.substr(j, 1) != ' ') break;
			
	return str.substr(i, j-i+1);
}

function isEmpty(obj, msg) {
	if (trim(obj.value) == '') {
		alert(msg + ' diperlukan.');
		try {
			obj.focus();
			obj.select();
		} catch (e) {}
		return true;
	}

	return false;
}

function isError(exp, obj, msg) {
	if (exp) {
		alert(msg + ' tidak sah.');
		obj.focus();
		try {
			obj.select();
		} catch (e) {}
	}
	
	return exp;
}

function compareDate(date1, date2) {
	if (date1 < date2)
		return -1;
	else if (date1 > date2)
		return 1;
	else
		return 0;
}

function compareTime(actTime, currTime) {
	if (actTime > currTime) 
		return -1;
	else if (actTime < currTime)
		return 1;
	else
		return 0;
}

function openWindow(url, name, top, left, width, height, attr) {
	return window.open(url, name, attr + 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);
}

function openCenterWindow(url, name, width, height, attr) {
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	
	if (attr == null) attr = '';
	if (attr != '') attr += ',';

	return window.open(url, name, attr + 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);
}

function getRadioValue(radioName) {
	var obj = document.getElementsByName(radioName);
	for (var i=0; i < obj.length; i++) {
		if (obj[i].checked) return obj[i].value;
	}
	
	return '';
}

function setRadioValue(radioName, value) {
	var obj = document.getElementsByName(radioName);
	for (var i=0; i < obj.length; i++) {
		if (obj[i].value == value) {
			obj[i].checked = true;
			return;
		}
	}
}

function isChecked(name) {
	var obj = document.getElementsByName(name);
	for (var i=0; i < obj.length; i++)
		if (obj[i].tagName == 'INPUT' && (obj[i].type == 'checkbox' || obj[i].type == 'radio') && obj[i].checked && !obj[i].disabled) return true;
		
	return false;
}

function showDialog(height, width, strTitle, strBody) {
	return window.showModalDialog('../Scripts/dialog.asp?title=' + strTitle, strBody, 'scroll:no;status:no;help:no;dialogHeight:' + height + 'px;dialogWidth:' + width + 'px');
}

function Prompt(height, width, strTitle, strPrompt, strValue, maxLength) {
	var strBody = '<table width="95%" height="95%" align="center" valign="middle"><tr><td>';
	strBody += '</td></tr>';
	strBody += '<tr><td>';
	strBody += strPrompt;
	strBody += '</td></tr>';
	strBody += '<tr><td>';
	strBody += '<input id="txtValue" type="text" style="width:100%" maxlength="'+maxLength+'" value="'+strValue.replace(/"/gi, '&quot')+'">';
	strBody += '</td></tr>';
	strBody += '<tr><td align="right">';
	strBody += '<button type="submit" onclick="window.returnValue=txtValue.value;window.close()">&nbsp;Okay&nbsp;</button>&nbsp;<button onclick="window.returnValue=null;window.close()">Cancel</button>';
	strBody += '</td></tr>';
	strBody += '</table>';
	strBody += '<script'+'>function window.onload() {txtValue.focus();txtValue.select();}';
	strBody += 'function window.document.body.onkeypress() {if (event.keyCode==27) window.close()}</'+'script>';

	return showDialog(height, width, strTitle, strBody);
}

function formatAmount(amount) {
	amount = amount.toString();
	var num = amount.split('.')[0];
	var dec = amount.split('.')[1];
	if (dec == null) dec = '';
	
	while (dec.length < 2)
	  dec += '0';
	  
	var n = '';
	while (num.length > 3) {
	  n = ',' + num.substr(num.length - 3, 3) + n;
	  num = num.substr(0, num.length - 3);
	}
	
	n = num + n;
	
	return n + '.' + dec;
}

function confirmText() {
	return confirm('Adakah kamu pasti?');
}
