// * Last Modified    : 17 Jul 2000 21:04
// *
// * Amendment History:
// * 
// * Date       By            Description
// * ---------  ------------- --------------------------------------------------
// * 09 Mar 00  Ben           Added function setRadioFocus(obj) to check radio
// *                          button when other controls are changed/clicked
// *
// * 09 Mar 00  Ben           Modified functions chkDateComparsion(), 
// *                          chkNotEarlyThanToday() and chkNotFutureDate()
// *                          for dd/MM/yyyy dates used in HASE
// *
// * 10 Mar 00  Ben           Added function chkIntAmount(x, fieldDesc, obj) to
// *                          check for new field type INT_AMOUNT
// *
// * 10 Mar 00  Ben           Modified error messages according to the spec
// *                          given by Retail MKT
// * 27 Mar 00  Wendy	      Modify function chkDate to accept dd-mm-yyyy format
// *
// * 31 Mar 00  PKS           Add function blurTextbox
// *
// * 06 Apr 00  PKS           Add function chkFutureDate
// *
// * 13 Apr 00  Ben           Modified error messages
// *
// * 09 May 00  Wendy         Hard Code system date for testing
// *
// * 17 Jul 00  Ben           Modified chkAlphaNumeric() to exclude '@' character
// * 27 Oct 00  JY	      Change Today and Now to global variables
// * 14 Jul 01  JY	      Add chkEmail()
// * 17 Jul 01  JY		migrate chkBlank from loan.js, add Error_33
// *				add chkRadioChecked, chkRadioEqual
// * 30 Jul 01  JY		amend chkRadioChecked, chkRadioEqual
// * 02 Aug 01	JY		add Error_34, isChecked, selectOneCheckBox, chkNotChecked
// *				rename chkRadioChecked as chkChecked
// * 17 Sep 01  JY		Add selectStaticComboBox
// * 18 Sep 01  JY		Migrate setLocation to common.js
// * 25 Sep 01  Peter         modified chkPassword, for setting focus
// * 4 Dec 01  Peter    add endIconDate for seasonal icon
// * 23 May 02 JY		HKEA: add constant and amend blurPayeeAcctL
// * 19 Jul 02 JY		HKEA: amend blurPayeeAcctL

var Error_01 = "~~1 should be in numbers.";
var Error_02 = "~~1 should be greater than ~~2.";
var Error_03 = "Please enter: ~~1.";
var Error_04 = "~~1 should be not more than ~~2.";
var Error_05 = "~~1 should be longer than ~~2 characters.";
var Error_06 = "~~1 is not a valid date.";
var Error_07 = "~~1 should not be later than today.";
var Error_08 = "~~1 should not be earlier than today.";
var Error_09 = "~~1 should not be earlier than ~~2.";
var Error_10 = "Both the ~~1 and the ~~2 must be entered.";
var Error_11 = "Both the ~~1 and the ~~2 must be empty.";
var Error_12 = "~~1 contains invalid character.";
var Error_13 = "~~1 should not be greater than ~~2.";
var Error_14 = "~~1 should not be smaller than ~~2";
var Error_15 = "~~1 is invalid.";
var Error_16 = "Invalid ~~1.";
var Error_17 = "~~1 should be non negative.";
var Error_18 = "~~1 should be greater than zero.";
var Error_19 = "~~1 has an incorrect numeric separator.";
var Error_20 = "~~1 should be in alphabets.";
var Error_21 = "~~1 is an invalid account.";
var Error_22 = "~~1 should be alpha-numeric.";
var Error_23 = "Please select ~~1";
var Error_24 = "~~1 has not been changed.";
var Error_25 = "~~1 should not be equal to ~~2.";
var Error_26 = "~~1 should not have any blank space.";
var Error_27 = "~~1 should have at least ~~2 alpha-numeric characters.";
var Error_28 = "~~1 should not contain more than two successive repeats of a character or digit.";
var Error_29 = "~~1 should be equal to ~~2";
var Error_30 = "~~1 should not contain decimal point.";
var Error_31 = "~~1 should be a future date.";
var Error_32 = "~~1 should be equal to ~~2";
var Error_33 = "~~1 should be blank.";
var Error_34 = "~~1 should not be selected.";

var TotalErrorMessage = "";
var Flag = false;
var submitForm = "yes";

HKEA_MERCH_NO = "2041";


// jy 27oct00 - begins
var Today = new Date(); 
var Now = Today.getFullYear() + "/" + (Today.getMonth()+1) + "/" +Today.getDate(); // production
//var Now = "2002/05/03"; 
// jy 27oct00 - ends

// added by HASE to check radio button
function blurTextbox(freeIdx, radio, textbox) // argument: string, radio array, textbox
{
	var i=0;
   for (i=0; i<radio.length; i++) {
      if (radio[i].checked) break;
   }
	if(i != freeIdx) {	
		textbox.value = "";	
		textbox.blur();
	}
}

function setRadioFocus(obj)
{
  obj.checked = true;
}

function chkRemarks(x,fieldDesc,obj){
	for(i=0;i<x.length;i++){
		if(x.charCodeAt(i)<32 || x.charCodeAt(i)>126)
			return(setErrorMessage(Error_12.replace(/~~1/,fieldDesc),obj));
	}
	return true;
}
function chkHSBCAcct(x,fieldDesc,obj){
	if(chkAccount(x,fieldDesc,obj)){
		var tmpAcct="";
		for(i=0;i<x.length;i++){
			if(x.charAt(i)!="-")
				tmpAcct+=x.charAt(i);				
		}
		if(tmpAcct.length!=10&&tmpAcct.length!=12&&tmpAcct.length!=16)
			return(setErrorMessage(Error_21.replace(/~~1/,fieldDesc),obj));
	}else
		return(false);
}
function checkIfSubmitted(){
	if(submitForm=="yes"){
		submitForm="no";
		return true;
	}else{
		return false;
	}
}
function chkEqual(x,y,fieldDescX,fieldDescY,objX,objY){
	if(x!=y){	
		ErrorMessage = Error_29.replace(/~~1/,fieldDescX);
		return(setErrorMessage(ErrorMessage.replace(/~~2/,fieldDescY),objX));
	}
	return(true);
}
function chkEqual2(x,y,fieldDescX,fieldDescY,objX,objY){
	if(x!=y){	
		ErrorMessage = Error_32.replace(/~~1/,fieldDescX);
		return(setErrorMessage(ErrorMessage.replace(/~~2/,fieldDescY),objX));
	}
	return(true);
}
function chkNotEqual(x,y,fieldDescX,fieldDescY,objX,objY){
	if(x==y){
		ErrorMessage = Error_25.replace(/~~1/,fieldDescX);
		return(setErrorMessage(ErrorMessage.replace(/~~2/,fieldDescY),objX));
	}
	return(true);
}
function chkPassword(x,fieldDesc,obj){
	for(i=0;i<x.length;i++)
		if(x.charCodeAt(i)==32){
			obj.focus();
			return(setErrorMessage(Error_26.replace(/~~1/,fieldDesc),obj));
		}	
	if(x.length<6){
		ErrorMessage=Error_27.replace(/~~1/,fieldDesc);
		obj.focus();
		return(setErrorMessage(ErrorMessage.replace(/~~2/,6),obj));
	}
	if(!chkAlphaNumeric(x,fieldDesc,obj)){
		obj.focus();
	    return(false);
	}    
	var temp = new Array(3);
	for(i=0;i<x.length-2;i++)
	{
		temp[1] = x.charCodeAt(i);
		temp[2] = x.charCodeAt(i+1);
		temp[3] = x.charCodeAt(i+2);
		if(temp[1]==temp[2] && temp[2]==temp[3]){
			obj.focus();
			return(setErrorMessage(Error_28.replace(/~~1/,fieldDesc),obj));
	}
	}
	return(true);
}
function blurBillType(List, Textbox) // argument: pulldownlist, corresponding textbox
{
	Value = List.options[List.selectedIndex].value;
	
	Start = Value.lastIndexOf("~~") + 2;
	Temp = Value.substring(Start, Value.length);
	if(Temp == "N")
	{	
		Textbox.blur();
	}
	Textbox.value = "";	
}
function blurPayeeAcctL(List, Textbox) // argument: pulldownlist, corresponding textbox
{
	Value = List.options[List.selectedIndex].value;
	
	Start = Value.lastIndexOf("~~") - 1;
	Temp = Value.substring(Start, Start+1);
	Start = Value.indexOf("~~");
	MerchNo = Value.substring(0, Start);

	if ((Temp == "N") || (MerchNo == HKEA_MERCH_NO))
	{	
		Textbox.blur();
		Textbox.value = "N/A";
	}
	else
		Textbox.value = "";
}

function blurPayeeAcctF(Flag, Textbox) // argument: String, corresponding textbox
{
	if(Flag == "N")
	{	
		Textbox.blur();
		Textbox.value = "";
	}
}

function chkChanged(x, fieldDesc,Compulsory, obj)
{
	var Err = 0;
	if(obj.type=="select-one")
	{
		for(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(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(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_24.replace(/~~1/, fieldDesc), obj));
		}
	return(true);

}

function processFormResult(form)
{
	if(TotalErrorMessage.length > 0)
		alertErrorMessage();
	else
		form.submit();
}
function alertErrorMessage()
{
	if(TotalErrorMessage.length > 0)
	{
		Temp = TotalErrorMessage;
		TotalErrorMessage = "";
		Flag = false;
		submitForm = "yes";
		alert(Temp);
	}
}
function chkFocus(x)
{
	if(!Flag)
	{
		x.focus();
		Flag = true;
	}
}
function setErrorMessage(Message, obj)
{
	TotalErrorMessage+=Message + "\n";
	chkFocus(obj);
	return(false);
}
function chkTextArea(x, col, row,fieldDesc, obj)
{	
	ErrorMessage = Error_04.replace(/~~1/, fieldDesc);
	ErrorMessage = ErrorMessage.replace(/~~2./, (row)) + " rows.";
	var temp = 0;
	var line = new Array();
	var y =0;
	var token="";
	var end=false;
	var offset = 0;
	for(count=0; count<(row+1); count++){
		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 count=0;
			while(count < x.length)
			{
				if(x.charCodeAt(count) != 10 && x.charCodeAt(count) != 13)
					return(setErrorMessage(ErrorMessage, obj));
				count++;
			}
			return true;
		}
		if(end)
			return true;
		else{
			start=offset+1;
			x=x.substring(start,x.length);
		}
	}
	return true;
}
function chkAlphaNumeric(x, fieldDesc, obj)
{
	for(y = 0; y<x.length; y++)
	{
		charCode = x.charCodeAt(y);
		if((charCode >= 48 && charCode <=57) || (charCode >= 65 && charCode <= 90) || (charCode >=97 && charCode <= 122))
			continue;
		else
			return(setErrorMessage(Error_22.replace(/~~1/, fieldDesc), obj));
	}
	return(true);
}
function chkMinLength(x,y,fieldDesc,obj){
	if(x.length<y){
		ErrorMessage=Error_27.replace(/~~1/,fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/,y),obj));}
	else
		return true;
}
function chkAccount(x, fieldDesc, obj)
{
	var Err = 0;
	var tmpx="";
	for(i=0;i<x.length;i++)
		if(x.charAt(i)!='-')
			tmpx+=x.charAt(i);
		for(y = 0; y<x.length; y++)
		{
			charCode = x.charCodeAt(y);
			if(charCode == 45)
			{
				if(isNaN(x.charAt(y-1)) || isNaN(x.charAt(y + 1)) || y==x.length-1 || y==0)
					Err = 1;
			}
			else if(charCode>=48 && charCode <=57)
				continue;
			else
				Err = 1;
		}
	if(Err == 1)
		return(setErrorMessage(Error_21.replace(/~~1/, fieldDesc), obj));
	return(true);
}
function chkAlphabetic(x, fieldDesc, obj)
{
	for(y = 0; y<x.length; y++)
	{
		charCode = x.charCodeAt(y);
		if((charCode >= 65 && charCode <= 90) || (charCode >=97 && charCode <=122))
			continue;
		else
			return(setErrorMessage(Error_20.replace(/~~1/, fieldDesc), obj));

		return(true);
	}
	return(true);
}

function chkNotSmallerThan(x,y,fieldDesc, obj)
{
	tempx = trimSeparator(x);
	tempy = trimSeparator(y);
	
	if(parseFloat(tempx) < parseFloat(tempy)){
		ErrorMessage = Error_14.replace(/~~1/, fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/, y), obj));
	}
	else
		return(true);
}

function chkNotGreaterThan(x,y,fieldDesc, obj)
{
	tempx = trimSeparator(x);
	tempy = trimSeparator(y);

	if(parseFloat(tempx) > parseFloat(tempy)){
		ErrorMessage = Error_13.replace(/~~1/, fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/, y), obj));
	}
	else
		return(true);
}
function chkBothInput(x,y,fieldDesc, fieldDesc2, obj)
{
	ErrorMessage = Error_10.replace(/~~1/, fieldDesc);

	if(x.length == 0 || y.length == 0 )
		return(setErrorMessage(ErrorMessage.replace(/~~2/, fieldDesc2),obj));

	return(true);
}
function chkBothEmpty(x,y,fieldDesc, fieldDesc2, obj)
{
	ErrorMessage = Error_11.replace(/~~1/, fieldDesc);
	if(x.length == 0 && y.length == 0 )
		return(true);
	else
		return(setErrorMessage(ErrorMessage.replace(/~~2/, fieldDesc2), obj));
}
// modified by HASE to support for dd/MM/yyyy checking
function chkDateComparsion(x, y, fieldDesc, fieldDesc2, obj)
{
	if(chkDate(x, fieldDesc, obj) & chkDate(y, fieldDesc2, obj))
	{
		var a = x.substr(6, 4) + "/" + x.substr(3, 2) + "/" + x.substr(0, 2);
		var b = y.substr(6, 4) + "/" + y.substr(3, 2) + "/" + y.substr(0, 2);
		if(Date.parse(a) >= Date.parse(b))
			return(true);
		else
		{
			ErrorMessage = Error_09.replace(/~~1/, fieldDesc);
			return(setErrorMessage(ErrorMessage.replace(/~~2/, fieldDesc2),obj));
		}
	}
	else
		return(false);
}
// modified by HASE to support for dd/MM/yyyy checking
function chkNotEarlyThanToday(x, fieldDesc, obj)
{
	var a = x.substr(6, 4) + "/" + x.substr(3, 2) + "/" + x.substr(0, 2);
	// jy 27oct00 - begins
	//var Today = new Date();
	//var Now = Today.getFullYear() + "/" + (Today.getMonth()+1) + "/" +Today.getDate();
	//var Now = "2001/01/16";
	// jy 27oct00 - ends
	
	if(chkDate(x, fieldDesc, obj))
	{
		if(Date.parse(a) >= Date.parse(Now))
			return(true);
		else
			return(setErrorMessage(Error_08.replace(/~~1/, fieldDesc), obj));
	}
	else
		return(false);
}
// modified by HASE to support for dd/MM/yyyy checking
function chkNotFutureDate(x, fieldDesc, obj)
{
	// jy 27oct00 - begins
	//var Today = new Date();
	//var Now = Today.getFullYear() + "/" + (Today.getMonth() + 1) + "/" + Today.getDate();
	//var Now = "2001/01/16";
	// jy 27oct00 - ends
		
	var d = x.substr(6, 4) + "/" + x.substr(3, 2) + "/" + x.substr(0, 2);
		
	if(Date.parse(d) <= Date.parse(Now))
	{
		return(true);
	}
	else
	{
		return(setErrorMessage(Error_07.replace(/~~1/, fieldDesc), obj));
	}
}

function chkFutureDate(x, fieldDesc, obj)
{
	// jy 27oct00 - begins
	//var Today = new Date();
	//var Now = Today.getFullYear() + "/" + (Today.getMonth() + 1) + "/" + Today.getDate();
	//var Now = "2001/01/16";
	// jy 27oct00 - ends
		
	var d = x.substr(6, 4) + "/" + x.substr(3, 2) + "/" + x.substr(0, 2);
		
	if(Date.parse(d) > Date.parse(Now))
	{
		return(true);
	}
	else
	{
		return(setErrorMessage(Error_31.replace(/~~1/, fieldDesc), obj));
	}
}

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);	
}

function chkNumeric(x, fieldDesc, obj)
{
	if(chknum(x))
		return(true);
	else
		return(setErrorMessage(Error_01.replace(/~~1/, fieldDesc), obj));
}

function chkGreaterThanZero(x, fieldDesc, obj)
{
	return(chkGreaterThan(x,"0",fieldDesc,obj));	
}
function chkGreaterThan(x,y,fieldDesc,obj){
	var tmpx=trimSeparator(x);
	var tmpy=trimSeparator(y);
	if(parseFloat(tmpx)>parseFloat(tmpy))
		return(true);
	else{
		if(y==0)y="zero";
		ErrorMessage=Error_02.replace(/~~1/,fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/,y),obj));
	}
}
function chkNotBlank(x, fieldDesc,Compulsory, obj) {
	
	if(obj.type==null){
		for(i=0;i<obj.length;i++){
			if(obj[i].checked)
			return(true);}
		if(Compulsory){
			TotalErrorMessage += Error_03.replace(/~~1/, fieldDesc) + "\n";
			chkFocus(obj[0]);
		}
		return(false);
	}
	else
	{
		if(x.length==0 || (obj.type=="checkbox" && !obj.checked)){
			if(Compulsory){
				if(obj.type == "select-one")
					TotalErrorMessage += Error_23.replace(/~~1/, fieldDesc) + "\n";
				else
					TotalErrorMessage += Error_03.replace(/~~1/, fieldDesc) + "\n";
				chkFocus(obj);}
			return(false);
		}
	}
	return(true);
}
function chkLessThan(x, y, fieldDesc, obj)
{
	if(x.length > y)
	{
		ErrorMessage = Error_04.replace(/~~1/ , fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2./, y)+ " characters.", obj));
	}
	else
		return(true);
}
function chkLongerThan(x,y,fieldDesc, obj)
{
	if(x.length < y)
	{
		ErrorMessage = Error_05.replace(/~~1/ , fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/, y - 1), obj));
	}
	else
		return(true);
}
function convertDateFormat(x){
	var space1 = x.indexOf(" ",0);
	DD = x.substring(0,space1);
	if(space1==1)DD="0"+DD;
	var space2 = x.indexOf(" ",space1+1);
	MM = (x.substring(space1+1,space2)).toUpperCase();
	YYYY = x.substring(space2+1,x.length);
	if(MM=="JAN")MM="01";
	else if(MM=="FEB")MM="02";
	else if(MM=="MAE")MM="03";
	else if(MM=="APR")MM="04";
	else if(MM=="MAY")MM="05";
	else if(MM=="JUN")MM="06";
	else if(MM=="JUL")MM="07";
	else if(MM=="AUG")MM="08";
	else if(MM=="SEP")MM="09";
	else if(MM=="OCT")MM="10";
	else if(MM=="NOV")MM="11";
	else if(MM=="DEC")MM="12";
	else MM="XX";
	return YYYY+"/"+MM+"/"+DD;
}
function chkDate(x, fieldDesc, obj){

	Err =0;
	if(x.length != 10)
		Err =1;
	else if(x.charAt(2) != "-" || x.charAt(5) != "-")
		Err =1;
	else if(!chknum(x.substring(0,2)) || !chknum(x.substring(3,5)) || !chknum(x.substring(6,10)))
		Err = 1;
		
	if(Err != 1 )
	{ 
		YYYY = x.substring(6,10);
		MM = x.substring(3,5);
		DD = x.substring(0,2);

		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_06.replace(/~~1/, fieldDesc), obj));

	return(true);	
}
function chkAmount(x, fieldDesc, obj){

	return(chkPositive(x,fieldDesc,obj));
}
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 chkAmountSeparator(x, fieldDesc, obj){
	noSeparator=trimSeparator(x);
	if(chkFloat(noSeparator,fieldDesc,obj)){
		if(chkSeparator(x,fieldDesc,obj))
			return true;
		return(setErrorMessage(Error_19.replace(/~~1/, fieldDesc), obj));
	}
	else
		return false;
}
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;
}
function chknum(x){

	for(y=0; y<x.length; y++)
	{
 		if(x.charCodeAt(y) > 57 || x.charCodeAt(y) < 48)
			return(false);
	}
	return(true);
}

function chkValidCharacters(x, fieldDesc, obj){

	for(y=0 ; y < x.length ; y++)
	{	
		charCode = x.charCodeAt(y);	

		if((charCode >= 48 && charCode <=57) || (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) || charCode == 10 || charCode == 13 || charCode == 58 || charCode==63 || charCode==46 || charCode==44 || charCode==40 || charCode==41 || charCode==32 || charCode==39 || charCode==47 || charCode==45 || charCode==43 || charCode==123 || charCode==125)
			continue;
		else
			return(setErrorMessage(Error_12.replace(/~~1/, fieldDesc), obj));
	}
	return(true);
}

function chkValidEmailCharacters(x, fieldDesc, obj){
	for(y=0 ; y < x.length ; y++)
	{	
		charCode = x.charCodeAt(y);	

		if ((charCode >= 1) && (charCode <=255))
			continue;
		else
			return(setErrorMessage(Error_12.replace(/~~1/, fieldDesc), obj));
	}
	return(true);
}

function chkInteger(x,fieldDesc, obj) {
	tmpx=trimSeparator(x);
	if(!isNaN(tmpx)&&tmpx.indexOf(".",0)==-1&&chkSeparator(x)){
		if ((tmpx-parseInt(tmpx,10))!=0){
			return(setErrorMessage(Error_16.replace(/~~1/, fieldDesc), obj));
		}
		return true;
	}
	else
		return(setErrorMessage(Error_16.replace(/~~1/, fieldDesc), obj));
}
function chkFloat(x,fieldDesc, obj){
	ErrorMessage = Error_15.replace(/~~1/, fieldDesc);
	if(chkFloatingPoint(x))
		return true;
	return(setErrorMessage(ErrorMessage, obj));
}
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(y=start; y<string_len; y++)
	{
		temp_string = x.substring(y,y+1);
		if(isNaN(parseInt(temp_string)))
		{
			if(temp_string == "." && decimal_counter == 0)
				decimal_counter++;
			else
				return(false);
		}
	}
	return (true);	
}

function chkNonNegative(x, fieldDesc, obj){

	if(chkAmountSeparator(x,fieldDesc, obj))
		x = trimSeparator(x);
	else
		return(false);

	if(chkFloat(x,fieldDesc, obj)){

		if(parseFloat(x) >= 0)
			return(true);
		else
			return(setErrorMessage(Error_17.replace(/~~1/, fieldDesc), obj));
	}
	else{
		return(false);
	}
}
function chkPositive(x, fieldDesc, obj){
	
	if(chkAmountSeparator(x,fieldDesc, obj))
		x = trimSeparator(x);
	else
		return(false);

	if(chkFloat(x,fieldDesc, obj)){
	
		if(parseFloat(x) > 0)
			return(true);
		else
			return(setErrorMessage(Error_18.replace(/~~1/, fieldDesc), obj));
	}
	else{
		return(false);
	}
}
function radioChecker(obj) {
   for (var i=0; i<obj.length; i++) {
      if (obj[i].checked) return (obj[i].value);
   }
   return(null);
}

/*
function logoff() {
	var msg ="Thank you for using Hang Seng e-Banking Services.\n\nAre you sure you want to Logoff?"

	if (confirm(msg))
		parent.location.replace("/servlet/Logoff");
}
*/

/*
// check whether target is set for the follow url
function isUrlSetTarget(url)
{
	if ((url == "/logon.htm") || (url == "/c_logon.htm") || (url == "/e_logon.htm") 
	|| (url == "LogonProfile") || (url == "Logoff") || (url.indexOf("HomeChangeLang") == 0))
		return true;
	else 
		return false;
}

function setLocation(obj, loc, waitFlag)
{
	if (waitFlag)
	{
		popupWait();
	}
	obj.href = loc;
	if (isUrlSetTarget(loc))
		obj.target = "_parent";
}		
*/
function popupWait()
{
	window.open("/PleaseWait.htm","HSBCPopUp","width=390,height=140,resizable=no,menubar=no,toolbar=no,directories=no,location=no,scrollbars=no,status=no");
}

function OpenHelp(form)
{
	window.open(form,'PopUp','width=400,height=400,resizable=yes,menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=yes');
}

function chkIntAmount(x, fieldDesc, obj)
{
	return(chkPositive(x,fieldDesc, obj) && chkInteger(x, fieldDesc, obj));
}

function chkEmail(x, fieldDesc, flag, obj, mandatory)
{
   if (mandatory)
   {
   	if (!chkNotBlank(x, fieldDesc, flag, obj))
   		return false;
   }
   if (x.length > 0)
   {
   	if ((x.indexOf("@") == -1) || (x.indexOf(".") == -1))
	{
		return (setErrorMessage(Error_16.replace(/~~1/, fieldDesc), obj));
	}
	else
	{
		for(i=0;i<x.length;i++) {
			if (!((x.charCodeAt(i)>=35 && x.charCodeAt(i)<=39) || (x.charCodeAt(i)==43) || 
			(x.charCodeAt(i)>=45 && x.charCodeAt(i)<=57) ||
			(x.charCodeAt(i)>=63 && x.charCodeAt(i)<=90) || 
			(x.charCodeAt(i)==95) || (x.charCodeAt(i)>=97 && x.charCodeAt(i)<=122)))
				return (setErrorMessage(Error_16.replace(/~~1/, fieldDesc), obj));
		}

		chkBeg = x.indexOf("@");
		chkEnd = x.lastIndexOf("@");
		if ((chkBeg != chkEnd) || (chkBeg <=0) || (chkEnd >= x.length - 3))
			return (setErrorMessage(Error_16.replace(/~~1/, fieldDesc), obj));

		chkBeg = x.indexOf(".");
		chkEnd = x.lastIndexOf(".");
		if ((chkBeg <= 0) || (chkEnd >= (x.length - 1)))
			return (setErrorMessage(Error_16.replace(/~~1/, fieldDesc), obj));
	
		if ((x.indexOf("..") != -1) || (x.indexOf(".@") != -1) || (x.indexOf("@.") != -1))
			return (setErrorMessage(Error_16.replace(/~~1/, fieldDesc), obj));

		chkBeg = x.lastIndexOf("@");
		chkEnd = x.lastIndexOf(".");
		if (chkBeg > chkEnd)
			return (setErrorMessage(Error_16.replace(/~~1/, fieldDesc), obj));
	}
		
   	return true;	
   }		
   return true;	
}

// check empty
function chkBlank(x, fieldDesc, obj)
{
	if(x == "NIL") return true;
	if(x == null) return true;
  	if(x.length == 0) return true;
	return(setErrorMessage(Error_33.replace(/~~1/,fieldDesc),obj));
}

// check whether rado buttion or checkbox is selected
function isChecked(obj)
{
	for(var i=0; i<obj.length;i++) {
		if (obj[i].checked)
			return true;
	}
	return false;
}

// check whether the radio buttion or checkbox is selected
function chkChecked(fieldDesc, obj) {
	if (!isChecked(obj))
   		return (setErrorMessage(Error_23.replace(/~~1/,fieldDesc),obj[0]));
	return true;
}

// check whether the radio buttion or check-box is not selected
function chkNotChecked(fieldDesc, obj)
{
	if (isChecked(obj))
   		return (setErrorMessage(Error_34.replace(/~~1/,fieldDesc),obj[0]));
   	return true;
}

// check whether the radio buttion selected equal to value y
function chkRadioEqual(chkSelected, y, fieldDesc, fieldDescY, obj) {
   for (var i=0; i<obj.length; i++) {
      if (obj[i].checked)
 	return (chkEqual(obj[i].value, y, fieldDesc, fieldDescY, obj[0], obj[0]));
   }
   if (chkSelected)
   	return (setErrorMessage(Error_23.replace(/~~1/,fieldDesc),obj[0]));
   else
   	return true;
}

// make group check-boxes to be select-one check-box
function selectOneCheckBox(obj, currentObj, hiddenObj)
{
	hiddenObj.value = "";
	for(i=0; i<obj.length;i++) 
	{
		if (obj[i].checked) 
		{
			if (obj[i]!=currentObj)
				obj[i].checked = false;
			else
				hiddenObj.value = obj[i].value;
		}
	}
}

// select static combo box with the previous value
function selectStaticComboBox(selectObj, prefillValue, defaultValue)
{
	if (!isNaN(parseInt(prefillValue)))
 	{
		selectObj.selectedIndex = parseInt(prefillValue);
 	}
 	else
 	{
  		selectObj.selectedIndex = defaultValue;
	}
}

function chkCheckBoxNumSelect(fieldDesc, objArray, allowOneOnly)
{
  tmpCount = 0;
  for (i = 0; i < objArray.length; i++)
  {
  	if (objArray[i].checked)
  		tmpCount +=1;
  }
  if (allowOneOnly)
  {
  	if (tmpCount < 1)
		return(setErrorMessage(Error_23.replace(/~~1/,fieldDesc),objArray[0]));  	
  	else if (tmpCount > 1)
  	{
  		tmpStr = "Only one ";
		return(setErrorMessage(tmpStr.concat(fieldDesc),objArray[0]));  			
	}
  }
  else
  {
  	if (tmpCount < 1)
		return(setErrorMessage(Error_23.replace(/~~1/,fieldDesc),objArray[0]));  	  	
  }
  return true;
}


