var Error_ShouldBeAlphabetic 					= "~~1 should be alphabetic.";
var Error_ShouldBeNumeric 						= "~~1 should be numeric.";
var Error_ShouldBeAlphaNumeric 					= "~~1 should be alpha-numeric.";
var Error_ContainInvalidCharacter 				= "~~1 contain invalid character.";
var Error_ShouldBeInteger 						= "~~1 should be integer.";
var Error_IsAnInvalidNumber 					= "~~1 should be deicmal.";
var Error_IsNotValidDate 						= "~~1 is not a valid date.";
var Error_MustNotContainAnyBlankSpace 			= "~~1 must not contained any blank space.";
var Error_MustBeAtLease_AlphanumericCharacter 	= "~~1 must be at least ~~2 alphanumeric characters.";
var Error_ShouldBeAtLeast_Characters 			= "~~1 should be at least ~~2 characters.";
var Error_ShouldbeLongerThan_Character 			= "~~1 should not be longer than ~~2 characters.";
var Error_ShouldNotBeSmallerThan_ 				= "~~1 should not be smaller than ~~2";
var Error_ShouldNotBeGreaterThan_ 				= "~~1 should not be greater than ~~2.";
var Error_ShouldNotBeEarlierThan_ 				= "~~1 should not be earlier than ~~2.";
var Error_ShouldNotBeLaterThan_ 				= "~~1 should not be later than ~~2.";
var Error_ShouldNotBeBlank 						= "~~1 should not be blank.";
var Error_PleaseSelect_ 						= "Please select ~~1";
var Error_HasNotbeenChanged 					= "~~1 has not been changed.";
var Error_ShouldNotBeLessThen_ 					= "~~1 should be less than ~~2.";
var Error_ShouldIncludeOnlyBlankOrSpace 		= "~~1 should include only blank or space.";
var Error_IsNotValidEmail 				= "~~1 is not a valid email address.";
var Error_IsNotValidCondition				= "~~1 cannot be empty.";
var Error_IsNotEqual					= "~~1 do not match.";

var ErrorMessage;
var TotalErrorMessage = "";
var Flag = false;

// check field
// check character
function chkAlphabetic(x, fieldDesc, obj)
{
	for (var i = 0; i<x.length; i++)
	{
		charCode = x.charCodeAt(i);
		if ((charCode >= 65 && charCode <= 90) || 
			(charCode >=97 && charCode <=122))
			continue;
		else
			return(setErrorMessage(Error_ShouldBeAlphabetic.replace(/~~1/, fieldDesc), obj));

		return(true);
	}
	return(true);
}

function chkNumeric(x, fieldDesc, obj)
{
	if (chknum(x))
		return(true);
	else
		return(setErrorMessage(Error_ShouldBeNumeric.replace(/~~1/, fieldDesc), obj));
}

function chkAlphaNumeric(x, fieldDesc, obj)
{
	for (var i = 0; i<x.length; i++)
	{
		charCode = x.charCodeAt(i);
		if ((charCode >= 48 && charCode <= 57) || 
		    (charCode >= 64 && charCode <= 90) || 
		    (charCode >= 97 && charCode <= 122))
			continue;
		else
			return(setErrorMessage(Error_ShouldBeAlphaNumeric.replace(/~~1/, fieldDesc), obj));
	}
	return(true);
}

function chkPrintable(x, fieldDesc, obj)
{
	for (var i=0;i<x.length;i++){
		if (x.charCodeAt(i)<32 || x.charCodeAt(i)>126)
			return(setErrorMessage(Error_ContainInvalidCharacter.replace(/~~1/,fieldDesc),obj));
	}
	return true;
}

function chkSeparator(x,fieldDesc,obj){
	if(x.charAt(0) == "-")
		x = x.substring(1, x.length);
	
	FirstSeparator = x.indexOf(",", 0);
	if(FirstSeparator <=3 && FirstSeparator > 0)
	{
		Start_Point = FirstSeparator + 1;
		Found = true;
		
		while(Found)
		{
			End_Point = x.indexOf(",", Start_Point);
			Offset = End_Point - Start_Point;
			if(End_Point == -1)
			{
				Tail = x.substring(Start_Point, x.length).length;

				if(x.substring(Start_Point+3, Start_Point+4) == ".")
				{
					Start_Point += 4;
					if(x.indexOf(",", Start_Point) == -1 && x.indexOf(".", Start_Point)== -1)
						return true;
					else
						return false;

				}
				else if(Tail != 3 || (Tail==3 && x.charAt(x.length-1)==".")){
					return false;
				}
				else if(Tail == 3)	
					return(true);
			
			}
			else if(Offset != 3){
				return false;
			}
			Start_Point = End_Point + 1;
		}
	}
	else if(x.indexOf(",",0) != -1 && FirstSeparator > 3)
		return false;
	else if(x.indexOf(".", 0) == -1 && x.indexOf(",",0) == -1)
		return(true);
	else if(x.indexOf(".", 0) != -1)
	{
		return true;
	}
	else
		return false;
}
// check data type 

function chkInteger(x,fieldDesc, obj) {
	var isInt;

	if (x.length == 0)
		return true;
	isInt = true;
	tmpx=trimSeparator(x);
	if (isNaN(tmpx))
		isInt = false;
	if (tmpx.indexOf(".",0) != -1)
		isInt = false;
	if (chkSeparator(x) == false)
		isInt = false;
	if ((tmpx-parseInt(tmpx,10)) != 0)
		isInt = false;
	if (isInt)
		return true;
	else
		return(setErrorMessage(Error_ShouldBeInteger.replace(/~~1/, fieldDesc), obj));
}

function chkFloat(x,fieldDesc, obj)
{
	if (x.length == 0)
		return true;
	ErrorMessage = Error_IsAnInvalidNumber.replace(/~~1/, fieldDesc);
	if (chkFloatingPoint(x))
		return true;
	return(setErrorMessage(ErrorMessage, obj));
}

/* modified by Ben for OCCD purpose */
function chkDate(x, fieldDesc, obj)
{
	if (x.length == 0)
		return true;
	var tmpx=convertDateFormat(x, false);
//	var tmpx=x;
	Err =0;
	if (tmpx.length != 10)
		Err =1;
	else if (tmpx.charAt(4) != "-" || 
			 tmpx.charAt(7) != "-")
		Err =1;
	else if (!chknum(tmpx.substring(0,4)) || 
			 !chknum(tmpx.substring(5,7)) || 
			 !chknum(tmpx.substring(8,10)))
		Err = 1;
		
	if (Err != 1 )
	{ 
		YYYY = tmpx.substring(0,4);
		MM = tmpx.substring(5,7);
		DD = tmpx.substring(8,10);

		if (MM <= 12 && MM >= 1)
		{
			if (MM==1 || MM==3 || MM==5 || MM==7 || MM==8 || MM==10 || MM==12)
			{
				if (DD > 31 || DD < 1)
					Err = 1;
			}
			else if (MM == 2)
			{
				if (chkLeapYear(YYYY))
				{
					if (DD > 29 || DD < 1)
						Err = 1;
				}
				else
				{
					if (DD > 28 || DD < 1)
						Err = 1;
				}
			}
			else{
				if (DD > 30 || DD < 1)
					Err = 1;
			}
		}
		else
			Err = 1;
	}
	if (Err == 1)
		return(setErrorMessage(Error_IsNotValidDate.replace(/~~1/, fieldDesc), obj));

	obj.value=tmpx;
	return(true);	
}

/* added by Ben */
function chkEqual(x, y, fieldDesc, obj) {
	if (x.length == 0 && y.length == 0) {
		return(true);
	} else {
		if (x == y) {
			return(true);
		} else {
			return(setErrorMessage(Error_IsNotEqual.replace(/~~1/, fieldDesc), obj));
		}
	}
}

/* added by Ben */
function chkEmail(x,fieldDesc,obj) {
//	var regExp = /^[A-Za-z]\w+@{1}\w+.\w+$/g;
	var regExp = /^[A-Za-z][A-Za-z0-9_\-.]+@{1}\w+.[A-Za-z0-9_.\-]+\w$/g;
	if ((x.length == 0) || (regExp.test(x))) {
		return(true);
	} else {
		return(setErrorMessage(Error_IsNotValidEmail.replace(/~~1/, fieldDesc), obj));
	}
}

/* added by Ben */
function chkInputCondition(x,fieldDesc,obj) {
	var regExp = /\w+/;
	if ((x.length > 0) && (regExp.test(x))) {
		return(true);
	} else {
		return(setErrorMessage(Error_IsNotValidCondition.replace(/~~1/, fieldDesc), obj));
	}
}

/* added by Ben */
function chkSearchCondition(x,fieldDesc,obj) {
	var regExp = /\w+/;
	if ((x.length > 0) && (regExp.test(x))) {
		return(true);
	} else {
		return(setErrorMessage(Error_IsNotValidSearchCondition.replace(/~~1/,fieldDesc),obj));
	}
}

function chkTelephone(x,fieldDesc, obj) {
	if (x.length == 0)
		return true;
	var regExp = /[^\s\d]/;
	if (x.search(regExp) == -1)
		return true;
	else
		return(setErrorMessage(Error_ShouldIncludeOnlyBlankOrSpace.replace(/~~1/, fieldDesc), obj));
}

function chkPassword(x, fieldDesc, obj)
{
	for (var i=0;i<x.length;i++)
		if (x.charCodeAt(i)==32)
			return(setErrorMessage(Error_MustNotContainAnyBlankSpace.replace(/~~1/,fieldDesc),obj));
	if (x.length<6){
		ErrorMessage=Error_MustBeAtLease_AlphanumericCharacter.replace(/~~1/,fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/,6),obj));
	}
	if (!chkAlphaNumeric(x,fieldDesc,obj))
	    return(false);

	return(true);
}

// two value
function chkStringMinLength(x, fieldDesc, obj, minLength)
{
	if (parseInt(x.length) < parseInt(minLength)){
		ErrorMessage = Error_ShouldBeAtLeast_Characters.replace(/~~1/, fieldDesc);
		ErrorMessage = ErrorMessage.replace(/~~2/, minLength);
		setErrorMessage(ErrorMessage,obj);
		return(false);
	}
	else {
		return true;
	}
}

function chkStringMaxLength(x, fieldDesc, obj, maxLength)
{
	if (parseInt(x.length) > parseInt(maxLength))
	{
		ErrorMessage = Error_ShouldbeLongerThan_Character.replace(/~~1/ , fieldDesc);
		ErrorMessage = ErrorMessage.replace(/~~2/, maxLength);
		setErrorMessage(ErrorMessage, obj);
		return(false);
	}
	else {
		return(true);
	}
}

//comparison
function chkNotSmallerThan(x, fieldDesc, obj, minValue)
{
	var tempx;
	tempx = trimSeparator(x);
	
	if (parseFloat(tempx) < minValue){
		ErrorMessage = Error_ShouldNotBeSmallerThan_.replace(/~~1/, fieldDesc);
		ErrorMessage = ErrorMessage.replace(/~~2/, minValue);
		setErrorMessage(ErrorMessage, obj);
		return(false);
	}
	else
		return(true);
}

function chkNotGreaterThan(x, fieldDesc, obj, maxValue)
{
	var tempx;
	tempx = trimSeparator(x);

	if (parseFloat(tempx) > maxValue){
		ErrorMessage = Error_ShouldNotBeGreaterThan_.replace(/~~1/, fieldDesc);
		ErrorMessage = ErrorMessage.replace(/~~2/, maxValue);
		setErrorMessage(ErrorMessage, obj);
		return(false);
	}
	else
		return(true);
}

function chkNotEarlierThan(x, fieldDesc, obj, earliestDate)
{
	x = convertDateFormat(x, true);
	var y = convertDateFormat(earliestDate, true);
	if (Date.parse(x) >= Date.parse(y))
		return(true);	
	else
	{
		ErrorMessage = Error_ShouldNotBeEarlierThan_.replace(/~~1/, fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/, earliestDate),obj));
	}
}

function chkNotLaterThan(x, fieldDesc, obj, latestDate)
{
	if (Date.parse(x) <= Date.parse(latestDate))
		return(true);	
	else
	{
		ErrorMessage = Error_ShouldNotBeLaterThan_.replace(/~~1/, fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/, latestDate),obj));
	}
}

// other
function chkNotBlank(x, fieldDesc,obj, Compulsory) 
{
	if (obj.type==null){
		for (var i=0;i<obj.length;i++){
			if (obj[i].checked)
				return(true);
		}
		if (Compulsory){
			TotalErrorMessage += Error_ShouldNotBeBlank.replace(/~~1/, fieldDesc) + "\n";
			chkFocus(obj[0]);
		}
		return(false);
	}
	else
	{
		var regExp = /^(\s)+/g;
		x = x.replace(regExp, "");
		var regExp = /(\s)+$/g;
		x = x.replace(regExp, "");
		if (x.length==0 || (obj.type=="checkbox" && !obj.checked)){
			
			if (Compulsory){
				if (obj.type == "select-one")
					TotalErrorMessage += Error_PleaseSelect_.replace(/~~1/, fieldDesc) + "\n";
				else
					TotalErrorMessage += Error_ShouldNotBeBlank.replace(/~~1/, fieldDesc) + "\n";
				chkFocus(obj);}
			return(false);
		}
	}
	return(true);
}

function chkChanged(x, fieldDesc, Compulsory, obj)
{
	var Err = 0;
	if (obj.type=="select-one")
	{
		for (var i = 0; i < obj.length; i++)
			if (obj[i].defaultSelected)
				if (obj[i].value == x)
					Err = 1;
	}
	else if (obj.type == "text" || obj.type == "textarea")
	{	
		if (obj.defaultValue == x)
			Err = 1;
	}
	else
	{
		var defaultIndex;
		for (var i =0; i< obj.length; i++){
			if (obj[i].defaultChecked){
				defaultIndex = i;
				break;
			}			
		}
		if (isNaN(obj.length))	//for only one checkbox
		{
			if (obj.defaultChecked == obj.checked)
				Err = 1;
		}
		else if (obj[defaultIndex].type=="checkbox")	//for more than one checkbox 
		{
			for (var i=0; i<obj.length;i++){				
				if (obj[i].defaultChecked != obj[i].checked)
					return(true);}
			Err = 1;
			obj=obj[0];
		}
		else	//for radio button
			if (obj[defaultIndex].value == x)
			{
				obj = obj[defaultIndex];
				Err = 1;
			}
	}
	if (Err == 1){
		if (Compulsory)
			return(setErrorMessage(Error_HasNotbeenChanged.replace(/~~1/, fieldDesc), obj));
		}
	return(true);

}

function chkTextArea(x, col, row, fieldDesc, obj)
{	
	ErrorMessage = Error_ShouldNotBeLessThen_.replace(/~~1/, fieldDesc);
	ErrorMessage = ErrorMessage.replace(/~~2./, (row+1)) + " rows.";
	var temp = 0;
	var line = new Array();
	var y =0;
	var token="";
	var end=false;
	var offset = 0;
	for (var i=0; i<(row+1); i++){
		var breakexit=false;
		while(x.charCodeAt(0)==13 && y<(row)){
			if (breakexit || y==0){
				line[y]="";
				y++;
			}
			x=x.substring(2,x.length);
			breakexit=true;
		}
		temp = x.indexOf("\n", 0);
		if (temp!=-1){
			offset = temp-2;
			token=x.substr(0,offset+1);
		}else{
			token=x.substr(0,x.length);
			end=true;
		}
		while(token.length>col && y<=row){
			line[y]=token.substr(0,col);
			var i = col-1;
			var done=false;
			while(!done){
				if (line[y].charCodeAt(col-1)==45||line[y].charCodeAt(col-1)==32){					
					token=token.substring(col,token.length);
					y++;
					done=true;				
				}
				else if (line[y].charCodeAt(i)==32){
					if (token.charCodeAt(col)!=32){
						tmp=token.lastIndexOf(" ",col-1);
						line[y]=token.substring(0,tmp);					
						token=token.substring(tmp+1,token.length);
					}else
						token=token.substring(col,token.length);
					y++;
					done=true;
				}
				else if (i==0 && token.charCodeAt(0)!=32)
				{
					token=token.substring(col,token.length);
					y++;
					done=true;
				}
				i--;
			}
		}
		if (token.length<=col){
			line[y]=token.substr(0,token.length);
			y++;
		}
		if (y>row)
		{
			var j=0;
			while(j < x.length)
			{
				if (x.charCodeAt(j) != 10 && x.charCodeAt(j) != 13)
					return(setErrorMessage(ErrorMessage, obj));
				j++;
			}
			return true;
		}
		if (end)
			return true;
		else{
			start=offset+1;
			x=x.substring(start,x.length);
		}
	}
	return true;
}

function chkLeapYear(x)
{
	if (x/400 == parseInt(x/400))
		return(true);
	else if (x/100 == parseInt(x/100))
		return(false);
	else if (x/4 == parseInt(x/4))
		return(true);
	else
		return(false);	
}

/* modified by Ben */
function convertDateFormat(x, isEngStyle)
{
	var regExp = /^(\s)+/g;				// trim space
	x = x.replace(regExp, "");
	var regExp = /(\s)+$/g;
	x = x.replace(regExp, "");
	
	var DD;
	var MM;
	var YYYY;
	var slash1 = x.indexOf("-",0);
	YYYY = x.substring(0,slash1);
	
	var slash2 = x.indexOf("-",slash1+1);
	MM = x.substring(slash1+1,slash2);
	if (MM.length==1)
		MM="0"+MM;
	
	DD = x.substring(slash2+1,x.length);
	if (DD.length==1)
		DD="0"+DD;

	if (isEngStyle) {
		return MM+"/"+DD+"/"+YYYY;
	} else {
		return YYYY+"-"+MM+"-"+DD;
	}
}

function trimSeparator(x)
{
	Start_Point = 0;
	End_Point = x.indexOf(",", Start_Point);
	while(End_Point != -1){
		x = x.substring(0, End_Point) + x.substring(End_Point+1, x.length);
		End_Point = x.indexOf(",", End_Point + 1);
	}
	return(x);
}

function chknum(x)
{
	for (var i=0; i<x.length; i++)
	{
 		if (x.charCodeAt(i) > 57 || x.charCodeAt(i) < 48)
 			return(false);
	}
	return(true);
}

function chkFloatingPoint(x) 
{
	var string_len = x.length;
	var decimal_counter = 0;
	var temp_string;
	var start = 0;
	
	if (x.substring(0,1) == "-")
		start = 1;
	else if (x.substring(0,1) == "."){
		start = 1;
		decimal_counter++;
	}
		
	if (x.substring(start,x.length).length ==0)
		return(false);

	for (var i=start; i<string_len; i++)
	{
		temp_string = x.substring(i,i+1);
		if (isNaN(parseInt(temp_string)))
		{
			if (temp_string == "." && decimal_counter == 0)
				decimal_counter++;
			else
				return(false);
		}
	}
	return (true);	
}

function radioChecker(obj) 
{
   for (var i=0; i<obj.length; i++) {
      if (obj[i].checked) 
      	return (obj[i].value);
   }
   return(null);
}

function setLocation(obj, loc, waitFlag)
{
	if (waitFlag)
	{
		popupWait();
	}
	obj.href = loc;
}

function OpenWin(form)
{
	window.open(form,'PopUp','width=550,height=500,resizable=no,menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=yes');
}

//End of Check
function processFormResult(form, bSubmit)
{
	if (TotalErrorMessage.length > 0){
		alertErrorMessage();
		return false;
	}
	else {
		if (bSubmit)	
			form.submit();
		return true;
	}
}

function alertErrorMessage()
{
	if (TotalErrorMessage.length > 0)
	{
		Temp = TotalErrorMessage;
		TotalErrorMessage = "";
		Flag = false;
		alert(Temp);
	}
}

function setErrorMessage(Message, obj)
{
	TotalErrorMessage+=Message + "\n";
	chkFocus(obj);
	return(false);
}

function chkFocus(x)
{
	if (!Flag)
	{
		x.focus();
		Flag = true;
	}
}

