//================================================================================
//INTERFACE:
//================================================================================
//	isDate(str)			- Kiem tra xem date nhap co dung ko
//	trim(str)			- Cat ky tu blank o cuoi string
//	isNumber(obj,AlertText)		- Kiem tra input box cho kieu so
//	isDateInput(obj,AlertText)	- Kiem tra input box kieu date
//	isEmpty(obj,AlerText)		- Kiem tra input box kieu string
//	currencyInput(obj)		- Ho tro nhap kieu currency
//================================================================================

//--------------------------------------------------------------------------------
// Ham nay de kiem tra mot string co dung la kieu date hay khong
//--------------------------------------------------------------------------------
function isDate(str) {
    var strDay, strMonth, strYear, nMonth, nDay, nYear, nPos1, nPos2;

    //str = trim(str);
    if (str.length > 10) return false;

    if (str =="") return false;

    //Get the position of the backslash "/" character for MONTH
    nPos1 = str.indexOf( "/" );
    if ( nPos1 < 0 ) return false;

    //Get the position of the backslash "/" character for DAY
    nPos2 = str.lastIndexOf( "/" );
    if ( nPos2 < 0 ) return false;

    //Checking the duplicate position of the backslash character
    if ( nPos1 == nPos2 ) return false;

    //Breaking the string into 3 parts of DAY, MONTH, YEAR
    strMonth = str.substring( 0, nPos1  );
    strDay = str.substring( nPos1 + 1, nPos2 );
    strYear = str.substring( nPos2 + 1 );

    //Rounding MONTH & DAY by adding 0
	if (strMonth.length < 2) strMonth = "0" + strMonth;
	if (strDay.length < 2) strDay = "0" + strDay;

    // Check whether 3 substrings of DAY, MONTH, YEAR are null
    if ( !strMonth.length || !strDay.length || !strYear.length ) return false;

    //Check if the substrings are number or not
    if ( isNaN(strMonth) || isNaN(strDay) || isNaN(strYear) ) return false;

    //Converts all substrings to a number
	nMonth = parseInt(strMonth,10);
	nDay = parseInt(strDay,10);
	nYear = parseInt(strYear,10);

	//---Check out of range errors
	if ( nMonth<=0 || nDay<=0 || nYear<=0 ) return false
    if ( nMonth > 12 ) return false;
    if ( nDay > 31 ) return false;

    if (nMonth==4 || nMonth==6 || nMonth==9 || nMonth==11 ) {
		if ( nDay > 30 ) return false;
	}
    if (nMonth==2) {
	   // leap year
	   if ( (nYear % 4 == 0) && (nYear % 100 != 0)) {
	       if ( nDay > 29 ) return false;
	   } else if ( nDay > 28 ) return false;
	} //nMonth

	if ((nYear<=1900) ||(nYear>=9999)) return false;
	return strMonth + "/" + strDay + "/" + strYear ;
}
//--------------------------------------------------------------------------------
function trim(str) {
	var ok, pos, strTemp;
	ok = "true";
	strTemp = str.substring(0, 1);
	if (strTemp != " ") {
		ok = "false";
	}
	while (ok=="true") {
		str = str.substring(1, str.length);
		strTemp = str.substring(0, 1);
		if (strTemp != " ") {
			ok = "false";
		}
	}
	return str;
}
//--------------------------------------------------------------------------------
// Ham nay de kiem tra mot input box (obj) co phai la kieu so khong
// Neu khong phai la kieu so, show alert text roi focus vao textbox do
//--------------------------------------------------------------------------------
function isNumber(obj,AlertText) {
	if (trim(obj.value)=="") {
		alert(AlertText);
		obj.focus();
		return false;
	}
	if (isNaN(obj.value)) {
		alert(AlertText);
		obj.focus();
		return false;
	}
	return true;
}

//--------------------------------------------------------------------------------
// Ham nay de kiem tra mot input box (obj) co phai la kieu so khong
// Neu khong phai la kieu date, show alert text roi focus vao textbox do
//--------------------------------------------------------------------------------
function isDateInput(obj,AlertText) {
	if (!isDate(obj.value)) {
		alert(AlertText);
		obj.focus();
		return false;
	}
	return true;
}

//--------------------------------------------------------------------------------
// Ham nay de kiem tra mot input box (obj) co bi empty khong
// Neu bi empty, show alert text roi focus vao textbox do
//--------------------------------------------------------------------------------
function isEmpty(obj,AlertText) {
	if (trim(obj.value)=="") {
		alert(AlertText);
		obj.focus();
		return true;
	}
	return false;
}

//--------------------------------------------------------------------------------
// Ham nay dung de xu ly du lieu nhap cho kieu currency, 
// Ham se handle onkeyup event de dam bao du lieu hien len luon la x,xxx,xxx.xx
//--------------------------------------------------------------------------------
function currencyInput(obj) {
	//
	//Do nothing now :)
	//
}

//--------------------------------------------------------------------------------
// Ham nay de kiem tra viec nhap diem, gioi han trong khoang 0->10 va co the nhap blank
//--------------------------------------------------------------------------------
function isPointNumber(obj) {
	if (trim(obj.value)=="") {
		obj.value = "";
		return true;
	}
	if (isNaN(obj.value)) {
		alert("Invalid Point");
		obj.focus();
		return false;
	}
	if (((obj.value/1)<0)||((obj.value/1)>10)) {
		alert("Invalid Point");
		obj.focus();
		return false;
	}
	return true;
}
