/*
Developer Name: Soong Gin Woon
Project: Tender2U
*/


//Remove spaces
function Trim(string) {
	var i;
	var intCount;
	intCount = 0;
	for (i = 0; i < string.length; i++) 
		if ((string.charAt(i)) != " ") 
			break;
		else 
			intCount = intCount + 1;
	string = string.substring(intCount, string.length);

	intCount = 0;
	for (i = string.length-1; i >= 0; i--) 
		if ((string.charAt(i)) != " ") 
			break;
		else 
			intCount = intCount + 1;
		
	string = string.substring(0, string.length-intCount);	
	return string;		
	
}

//return true if is blank data else false
function isBlank(objTxtNm){
	
	if (Trim(objTxtNm) == '')
		return true;
	else
		return false;
}

// Returns true if is number
function isNumeric( strNum ) {
	var intCounter=0 , intLoop;

	strNum = Trim(strNum);
	if (strNum == '') return false;
	if (strNum == '.') return false;

	for (intLoop = 0; intLoop < strNum.length; intLoop++) { 
		if (strNum.charAt(intLoop) == ".") intCounter++;
		if (intCounter > 1) return false;
	}
	for (intLoop = 0; intLoop < strNum.length; intLoop++) { 
		if (((strNum.charAt(intLoop) < "0") || (strNum.charAt(intLoop) > "9")) && 
				(strNum.charAt(intLoop) != ".")	)
			return false;
	}

	return true; // value is 0
}

//return true if  is number 0 - 9 
function isNumber(strNum) {

	var intCounter=0 , intLoop;

	strNum = Trim(strNum);
	for (intLoop = 0; intLoop < strNum.length; intLoop++) {
		if (((strNum.charAt(intLoop) < "0") || (strNum.charAt(intLoop) > "9") 
				))
			return false;
	}
	return true; 
}


//check date 
function DateValid( intDay, intMonth, intYear ) {

	var arrMonthDays = new Array (	"31", "29", "31",
					"30", "31", "30",
					"31", "31", "30",
					"31","30","31"	  );
					
	if (intMonth == 'Jan' ) intMonth = '1'
	if (intMonth == 'Feb' ) intMonth = '2'
	if (intMonth == 'Mar' ) intMonth = '3'
	if (intMonth == 'Apr' ) intMonth = '4'
	if (intMonth == 'May' ) intMonth = '5'
	if (intMonth == 'Jun' ) intMonth = '6'
	if (intMonth == 'Jul' ) intMonth = '7'
	if (intMonth == 'Aug' ) intMonth = '8'
	if (intMonth == 'Sep' ) intMonth = '9'
	if (intMonth == 'Oct' ) intMonth = '10'
	if (intMonth == 'Nov' ) intMonth = '11'
	if (intMonth == 'Dec' ) intMonth = '12'

	if ((!isNumeric(intDay)) || (!isNumeric(intMonth)) || (!isNumeric(intYear))){
		return false;
	}
	if (intYear==0) {
        return false;
    }
    if ((intMonth>12) || (intMonth<=0)) {
	    return false;
	}

	if ((parseFloat(intDay) > parseFloat(arrMonthDays[intMonth-1])) || (parseFloat(intDay)<=0))  {
	    return false;
	}
	if ((intMonth==2)&&
	   (intDay==arrMonthDays[1])&&
	   (intYear%4>0) ) {
		//alert("The end of this month is 28");
	    return false;
	}

	return  true;
}

// check valid email address 
function isValidEmail (email_field){  
	var counterAT=0;
	var counderDOT=0;
	var n = 0;
	
	while (n < email_field.length) {
		if (email_field.substring(n, n+1) == "@") {
			counterAT++;
		}
		else if (email_field.substring(n, n+1) == ".")
			counderDOT++;
		n++;
	}
	
	if (counterAT > 1 || counderDOT < 1)
		return false;
	else
		return true;
}

//check both passwords entered must same
function isSamePasswrd (password1, password2)
{
	if (password1.length != password2.length)
		return false;
	else {
		if (password1 != password2)
			return false;
		else
			return true;
	}	
}

//Create inner HTML Display
function fnHtmlMaker(objSpan, strData){
	eval(objSpan + '.innerHTML="' +  strData + '"');
}

//Enter Key Pressed
function fnEnterPressed (obj)
{
	if (event.keyCode == 13) 
		return true;
	else
		return false;
}

//compare two date
function timeDifference(laterdate,earlierdate) {
	var difference = laterdate.getTime() - earlierdate.getTime();
	var daysDifference = Math.floor(difference/1000/60/60/24);
	
	return daysDifference;
}

//Telephoe, Mobile and Fax (Length = 9)
function fnCommFormat(strVal){
	if (isBlank(Trim(strVal)))
		return true;
	else
		if (strVal.length >= 9 && isNumber(strVal))
			return true;
		else
			return false;
}	

//Display total length in the TextArea
function fnTxtAreaLength(objTxt, objDisp, intMaxLen){
	var intLength = objTxt.value.length;

	objDisp.value = parseInt(intMaxLen) - parseInt(intLength);		
	return true;
}