function IsAllDigitChar(val) {
	var i;
	for (i=0; i<val.length; i++) 
		if (val.charAt(i)<'0' || val.charAt(i)>'9')
			return false;
	return true;
}

// return number of check box which has been checked
function checkBoxCount(templateName, mask, startIndex) {
	var idx = startIndex;
	var chkID = templateName.replace(mask, idx.toString());
	var chk = window.document.getElementById(chkID);
	var cnt=0;
	while (chk) {
		cnt += 1;
		idx += 1;
		chkID = templateName.replace(mask, idx.toString());
		chk = window.document.getElementById(chkID);
	}
	return cnt;		
}

function OpenWindow(url, title, width, height) {
	var iTop = (screen.height-height)/2;
	var iLeft = (screen.width-width)/2;

	var sFeature = 'scrollbars=yes,top=' + iTop.toString() + ',left=' + iLeft.toString() + ',width=' + width.toString() + ',height=' + height.toString();
	window.open(url, title, sFeature);
}


function OpenPrintWindow(url, width, height) {
	var iTop = (screen.height-height)/2;
	var iLeft = (screen.width-width)/2;

	var sFeature = 'resizable=yes, scrollbars=yes,top=' + iTop.toString() + ',left=' + iLeft.toString() + ',width=' + width.toString() + ',height=' + height.toString();
	window.open(url, '_blank', sFeature);
}


function doNothing() {
}

function calcBelowPosition(obj,position,width,height) 
{
	// position=1 POPUP: makes screen display up and/or left, down and/or right 
	// depending on where cursor falls and size of window to open
	// position=2 CENTER: makes screen fall in center
	var retObj = new Object();
	var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;
	if(navigator.appName == "Microsoft Internet Explorer") 
	{
		screenY = document.body.offsetHeight;
		screenX = window.screen.availWidth;
	}
	else 
	{
		screenY = window.outerHeight
		screenX = window.outerWidth
	}
	if(position == 1)	
	{ // if POPUP not CENTER
		cursorX = event.screenX;
		cursorY = event.screenY;
		padAmtX = 10;
		padAmtY = 10;
		if((cursorY + height + padAmtY) > screenY) 
		{
			// make sizes a negative number to move left/up
			padAmtY = (-30) + (height * -1);
			// if up or to left, make 30 as padding amount
		}
		if((cursorX + width + padAmtX) > screenX)	
		{
			padAmtX = (-30) + (width * -1);	
			// if up or to left, make 30 as padding amount
		}
		if(navigator.appName == "Microsoft Internet Explorer") 
		{
			leftprop = cursorX + padAmtX;
			topprop = cursorY + padAmtY;
		}
		else 
		{
			leftprop = (cursorX - pageXOffset + padAmtX);
			topprop = (cursorY - pageYOffset + padAmtY);
		}
	}
	else
	{
		leftvar = (screenX - width) / 2;
		rightvar = (screenY - height) / 2;
		if(navigator.appName == "Microsoft Internet Explorer") 
		{
			leftprop = leftvar;
			topprop = rightvar;
		}
		else 
		{
			leftprop = (leftvar - pageXOffset);
			topprop = (rightvar - pageYOffset);
		}
	}
	retObj.left = leftprop;
	retObj.top = topprop;
	return retObj;
}


/*function calcBelowPosition(obj, w, h) {
	var retObj = new Object();
	retObj.left = event.screenX - event.offsetX;
	retObj.top = event.screenY + obj.offsetHeight - event.offsetY;	
	return retObj;			
}*/
		
function CheckDouble() {
	if (String.fromCharCode(event.keyCode) == '.')
		return true;
	else
		return NumberOnly();
}

function CheckOneDecimalNumber(source, agruments)
{
	if ((agruments.Value.indexOf('.') != -1)  && (agruments.Value.indexOf('.') != (agruments.Value.length - 2)))
		agruments.IsValid = false;
	else
		agruments.IsValid = true;
}

function CheckTwoDecimalNumber(source, agruments)
{
	if ((agruments.Value.indexOf('.') != -1)  && (agruments.Value.indexOf('.') < (agruments.Value.length - 3)))
		agruments.IsValid = false;
	else
		agruments.IsValid = true;
}

function CheckFourDecimalNumber(source, agruments)
{
	if ((agruments.Value.indexOf('.') != -1)  && (agruments.Value.indexOf('.') < (agruments.Value.length - 5)))
		agruments.IsValid = false;
	else
		agruments.IsValid = true;
}

function KeyInputShortType(textBox)
{
	// Nghia modified on 10/15/2002
	// replace:: if (event.keyCode < 47 || event.keyCode > 57) event.returnValue = false;
	// for key "/"
	if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;
	
	if (textBox.value.length > 4){
		event.returnValue = false
	}
}

// Author	: Nghia Cao
// Purpose	: Check for Short Type value
function CheckRequiredPositiveShort(textBox){
	var MAX_VALUE = 32767;
	
	if (parseInt(textBox.value, 10) > 0 && parseInt(textBox.value, 10) <= 32767){
		return true;
	}
	
	/*if (!confirm("Short value out of range (1 - " + MAX_VALUE + ")"))
		return true;
	
	setFocus(textBox);
	try {
		textBox.select();
	}
	catch (e){}
	
	event.returnValue = false; */
	
	return false;
}

// Author	: Nghia Cao
// Purpose	: Check for Short Type value
function CheckRequiredNShort(textBox){
	var MAX_VALUE = 32767;
	
	if (parseInt(textBox.value, 10) >= 0 && parseInt(textBox.value, 10) <= 32767){
		return true;
	}
	/*if (!confirm("Short value out of range (0 - " + MAX_VALUE + ")"))
		return true;	
	
	setFocus(textBox);
	try {
		textBox.select();
	}
	catch (e){} */
	
	return false;
}

// Author	: Nghia Cao
// Purpose	: Check for Short Type value
function CheckRequiredNShortKeyPress(textBox){
	var MAX_VALUE = 32767;
	
	if (parseInt(textBox.value, 10) >= 0 && parseInt(textBox.value, 10) <= 32767){
		return true;
	}
	/* if (!confirm("Short value out of range (0 - " + MAX_VALUE + ")"))
		return true;	
	
	
	setFocus(textBox);
	try {
		textBox.select();
	}
	catch (e){}
	*/
	return false; 
}

function setFocus(obj){
	try {
		obj.focus();
	} catch (e){}
}

// Author: Di Truong The
// Description: Only allow number input in text box
// Param: 
//		param=1: with dot  (11.22, 0.2323, .232)
//		param=0: no dot	(integer only)

function NumberOnly(param) 
{ 
	if ((event.keyCode < 47-param) || (event.keyCode > 57) ||(event.keyCode==47)) event.returnValue = false; 
} 

// Author: Di Truong The
// Description: Only allow non white-space character input
function NoSpaceInput() 
{ 
	if (event.keyCode==32) event.returnValue = false;	
} 

function ValidLength(source, agruments)
{
	if (agruments.Value.length <6 || agruments.Value.length>16)
		agruments.IsValid = false;
	else
		agruments.IsValid = true;	
}

// Author: Nghia Cao
// Description: to validate text area's length
function MaxlengthTextArea(obj, maxlen) {	
try {
	if (obj.value.length<maxlen)
		event.returnValue = true;
	else 
		event.returnValue = false;
} catch (e) {}
}


// Author : Di Truong
// Description: forbid input those chars in chs parameter
function ForbidChar(chs) {
	try {
		var i;
		
		for (i=0; i<chs.length; i++) {
			
			if (event.keyCode == chs.charCodeAt(i)) {
				event.returnValue = false;
				return false;
			}				
		}		
	} catch (e) {}
}

// repeat a char for a number of times
function RepeatChar(ch, num) {
		var st;
		var i;
		st = "";
		for (i=0; i<num; i++)
		st += ch;
		return st;
	}


// unformat a number
function UnformatNumber(num) {
	// remove group character  (,) 
	var re = /,/g;
	
	var value = num.replace(re, "");
	
	return value;
}

// return a number in format x'xxx.xx
function FormatNumber(num) {

	// remove group character  (,) before calculate
	var re = /,/g;
	
	var value = num.toString().replace(re, "");		
	var str = "";
	
	var fraction;
	var idx = value.indexOf(".");
	
	if (idx >= 0) {
		fraction = parseInt(value.substring(idx + 1, Math.min(idx + 4, value.length)));
		if (fraction >= 100) {		// max = 999
			if (fraction % 10 >= 5)
				fraction += (10 - (fraction % 10));
			else
				fraction -= fraction % 10;
			fraction = fraction / 10;
		}
		
		if (fraction >= 10) 
			fraction = fraction / 100;
		else 
			fraction = fraction / 10;					
		
		str = (parseInt(value.substring(0, idx)) + fraction).toString();				
		
		//add trail zero				
		idx = str.indexOf(".");								
		if (idx<0)
			str += ".00";
		else {
			var len = str.length;				
			
			str += RepeatChar("0", 3-(len-idx))
		}
	}			
	else
		str = value + ".00";
	
	// add group separator character (,) after calculated
	var temp=str;
	var idx=temp.indexOf(".");
	temp = str.substr(idx, 3); // get .xx
	while (idx-3>0) {
		temp = ","+str.substr(idx-3, 3) + temp //concat
		idx -= 3;
	}
	//concat the rest
	if (idx>0)
		temp = str.substr(0, idx) + temp;
	
	str = temp;
	
	return str;
}

function trimLeft(val)
{
	while(val.charAt(0) == ' ')
		val = val.substring(1, val.length);
	return val;
}
function trimRight(val)
{
	while(val.charAt(val.length - 1) == ' ')
		val = val.substring(0, val.length - 1);
	return val;
}

function ValidateEmail(obj)
{
	try 
	{	var pattern = "^(([a-zA-Z\d-_]+)?([a-zA-Z\d-_]+(\.[a-zA-Z\d-_]+)*)@(([a-zA-Z0-9][-a-zA-Z0-9]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,})*(([a-zA-Z0-9][-a-zA-Z0-9]*[a-zA-Z0-9]*\.)+[a-zA-Z]{2,}|\[(([0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\]))?$"
		return obj.test(pattern);
		
		} catch (e) {}
}

function NumberFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	//if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
} // function CurrencyFormatted()

function CommaFormattedForNumber(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = new Array(2);
	a[0] = amount;
	a[1] = "";
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	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 CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
} // function CurrencyFormatted()

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	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;
}
