var Error_01 = "~~1 should be numeric. Please re-enter.";
var Error_02 = "~~1 should be greater than ~~2. Please re-enter.";
var Error_03 = "~~1 should not be blank. Please re-enter.";
var Error_04 = "~~1 should be less than ~~2. Please re-enter.";
var Error_06 = "~~1 is not a valid date. The format should be DD/MM/YYYY, eg 10/08/2001. Please re-enter.";
var Error_09 = "~~1 should not be earlier than ~~2. Please specify again.";
var Error_12 = "~~1 contains invalid character. Please re-enter.";
var Error_13 = "~~1 should not be greater than ~~2. Please re-enter.";
var Error_14 = "~~1 should not be smaller than ~~2. Please re-enter.";
var Error_15 = "~~1 is an invalid number. Please re-enter.";
var Error_16 = "~~1 is an invalid number. Please re-enter.";
var Error_17 = "~~1 should be positive. Please re-enter.";
var Error_18 = "~~1 should be positive. Please re-enter.";
var Error_19 = "~~1 has incorrect numeric separator. Please re-enter.";
var Error_20 = "~~1 should be alphabetic. Please re-enter.";
var Error_21 = "~~1 is not a valid account. Please specify again.";
var Error_22 = "~~1 should be alphanumeric. Please re-enter.";
var Error_23 = "Please select ~~1.";
var Error_26 = "~~1 must not have any blank space. Please specify the value.";
var Error_27 = "~~1 must have at least ~~2 alphanumeric characters. Please re-enter.";
var Error_32 = "~~1 should be smaller than ~~2. Please re-enter.";
var Error_35 = "Please select at least one Account.";
var Error_37 = "~~1 should be less than ~~2 digits. Please re-enter.";
var Error_38 = "You can only delete up to ~~1 email messages at one go. Please select again.";
var Error_39 = "Since no debit account is avaliable, please select option \"Total Balance (Principal + Interest)\".";
var Error_40 = "Since no debit account is avaliable, please select option \"Don\'t have a target date to withdraw at this moment.\".";
var Error_41 = "Please select at least one prepare or authorise service for management control.";
var Error_42 = "Please select at least one company for facility enquiry by borrower.";
var Error_43 = "Please select at least Trade Services Activities or Other Activities for activity log.";
var Error_44 = "Please select at least one service for facility enquiry.";
var Error_45 = "Please select at least one Service.";
var Error_46 = "You must agree and confirm the above declaration before submitting your form.";
var Error_47 = "~~1 should not have more than ~~2 decimal places. Please re-enter.";
var Error_48 = "Please select at least one service for payments.";
var Error_49 = "Please select at least one service for trade services.";
var Error_50 = "Your connection is about to time out. Please save the payment details as an autoPay group to avoid the loss of data.";
var Error_51 = "Please enter either the ~~1 or ~~2, ~~3 and ~~4."
var Error_52 = "Please enter either the ~~1 number or the ~~2 information."
var Error_53 = "~~1 should be inputted with ~~2."
var Error_54 = "Please enter either ~~1, ~~2 and/or ~~3."
var Error_55 = "~~1 should only contains numeric characters, space or hyphen. Please re-enter.";
var Error_56 = "~~1 is not a valid email address.  Please re-enter.";
var Error_57 = "Please select the recipient(s) to whom you want us to send an email.";
var Error_58 = "Please check the \'Pay and send e-Advice\' box if you want us to send an email to the selected recipient(s).";
var Error_59 = "~~1 is incomplete.  Please re-enter.";
var Error_60 = "Exchange rate expired. Please click the 'Refresh' button to get a new rate.";
var Error_200= "No input. Please re-enter.";
var Error_201= "Invalid input. Please re-enter.";
var Error_202 = "Please input at most 5 digits.";
var Error_203 = "Invalid input. Please re-enter.";

var errHead="Your request cannot be processed due to the following error(s):\n\n";
var errFoot="\nPlease make the changes and submit the form again.\n";
var TotalErrorMessage = "";
var WaitFlag = true;
var Flag = false;
var submitForm = "yes";
var timeout_msg = "";

function updateList(TopAccountList, AccountList, selection)
{
	selectedSet = TopAccountList.options[TopAccountList.selectedIndex].value;
	AccountList.length=0;
	for(x=0;x<selection[selectedSet].length;x++)
	{
		AccountList.options[x] = new Option(selection[selectedSet][x][0],selection[selectedSet][x][1]);
	}
	AccountList.options[0].selected=true;	
}
function chkRemarks(x,fieldDesc,obj){
	return chkRemarksFocus(x,fieldDesc,obj,true);
}
function chkRemarksFocus(x,fieldDesc,obj,focus){
	for(i=0;i<x.length;i++){
		if(x.charCodeAt(i)<32 || x.charCodeAt(i)>126)
		{
			if (focus)
				chkFocus(obj);
			return(setErrorMessageFocus(Error_12.replace(/~~1/,fieldDesc),obj,focus));
		}
	}
	return true;
}
function chkCurAcctFocus(x,fieldDesc,obj,focus){
	if(chkAccount(x,fieldDesc,obj,focus)){
		var tmpAcct="";
		for(i=0;i<x.length;i++){
			if(x.charAt(i)!="-")
				tmpAcct+=x.charAt(i);				
		}
		var suffix = Number(tmpAcct.substr(9,3));
		if( (tmpAcct.length!=12) || (suffix<1) || (suffix>29) ) {
			return(setErrorMessageFocus(Error_21.replace(/~~1/,fieldDesc),obj,focus));
		}
		
	}else
		return(false);
}
function checkPopUp(){
	if(submitForm=="yes"){
		return true;
	}else{
		return false;
	}
}
function checkIfSubmitted(){
	if(submitForm=="yes"){
		submitForm="no";
		return true;
	}else{
		return false;
	}
}
function processFormResult(form)
{
	if(TotalErrorMessage.length > 0)
		alertErrorMessage();
		
	else
	{
		if (WaitFlag == true)
			popupWait();
		form.submit();
	}
}
function alertErrorMessage()
{
	if(TotalErrorMessage.length > 0)
	{
		Temp = TotalErrorMessage;
		TotalErrorMessage = "";
		Flag = false;
		submitForm = "yes";
		alert(errHead + Temp + errFoot);
		
	}
}
function chkFocus(x)
{
	if(!Flag)
	{
		x.focus();
		Flag = true;
	}
}
function setErrorMessage(Message, obj){
	return setErrorMessageFocus(Message, obj, true)
}
function setErrorMessageFocus(Message, obj, focus)
{
	TotalErrorMessage+=Message + "\n";
	if (obj.name!="OtherAccount"&&obj.name!="BillType"&&obj.name!="OrderPrice")
	if (focus)
		chkFocus(obj);
	return(false);
}

function chkTextAreaLength(x, col, row,fieldDesc, obj){
	return	chkTextAreaLengthFocus(x, col, row,fieldDesc, obj, true);
}
function chkTextAreaLengthFocus(x, col, row,fieldDesc, obj, focus)
{	
	var counter = 0;	
	var theErrMsg = Error_04;
	
	var totalchar13 = 0;
	var totalchar10 = 0;
	var tempstr="";
	for(counter=0; counter<x.length; counter++)
	{
		if(x.charCodeAt(counter)== 13) totalchar13++;
		if(x.charCodeAt(counter)== 10) totalchar10++;
	}
	if( (totalchar10>totalchar13) && (totalchar13==0) )
	{
		for(counter=0; counter<x.length;counter++)
		{
			if (x.charCodeAt(counter)==10)
				tempstr = tempstr + String.fromCharCode(13,10);
			else 
				tempstr = tempstr + x.charAt(counter);
		}
		x = tempstr;
	}

	if (x.length > ( col * row ) )
	{
		theErrMsg = theErrMsg.replace(/~~1/, fieldDesc);
		theErrMsg = theErrMsg.replace(/~~2/, ( col * row + 1) + ' characters');
		return(setErrorMessageFocus(theErrMsg, obj, focus));
	}
	return true;
}

function chkTextArea(x, col, row,fieldDesc, obj){
	return	chkTextAreaFocus(x, col, row,fieldDesc, obj, true);
}
function chkTextAreaFocus(x, col, row,fieldDesc, obj, focus)
{	
	ErrorMessage = Error_04.replace(/~~1/, fieldDesc);
	ErrorMessage = ErrorMessage.replace(/~~2./, (row+1) + " rows.");
	
	var counter = 0;
	var totalchar13 = 0;
	var totalchar10 = 0;
	var tempstr="";
	for(counter=0; counter<x.length; counter++)
	{
		if(x.charCodeAt(counter)== 13) totalchar13++;
		if(x.charCodeAt(counter)== 10) totalchar10++;
	}
	if( (totalchar10>totalchar13) && (totalchar13==0) )
	{
		for(counter=0; counter<x.length;counter++)
		{
			if (x.charCodeAt(counter)==10)
				tempstr = tempstr + String.fromCharCode(13,10);
			else 
				tempstr = tempstr + x.charAt(counter);
		}
		x = tempstr;
	}
	
	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(setErrorMessageFocus(ErrorMessage, obj, focus));
				count++;
			}
			return true;
		}
		if(end)
			return true;
		else{
			start=offset+1;
			x=x.substring(start,x.length);
		}
	}
	return true;
}
function chkAlphaNumeric(x, fieldDesc, obj){
	return	chkAlphaNumericFocus(x, fieldDesc, obj, true);
}
function chkAlphaNumericFocus(x, fieldDesc, obj, focus)
{
	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(setErrorMessageFocus(Error_22.replace(/~~1/, fieldDesc), obj, focus));
	}
	return(true);
}
function chkMinLength(x,y,fieldDesc,obj){
	return	chkMinLengthFocus(x,y,fieldDesc,obj,true);
}
function chkMinLengthFocus(x,y,fieldDesc,obj,focus){
	if(x.length<y){
		ErrorMessage=Error_27.replace(/~~1/,fieldDesc);
		return(setErrorMessageFocus(ErrorMessage.replace(/~~2/,y),obj,focus));}
	else
		return true;
}
function chkAccount(x, fieldDesc, obj){
	return chkAccountFocus(x, fieldDesc, obj, true);
}
function chkAccountFocus(x, fieldDesc, obj, focus)
{
	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(setErrorMessageFocus(Error_21.replace(/~~1/, fieldDesc), obj, focus));
	return(true);
}
function chkAlphabetic(x, fieldDesc, obj){
	return	chkAlphabeticFocus(x, fieldDesc, obj, true);
}
function chkAlphabeticFocus(x, fieldDesc, obj, focus)
{
	for(y = 0; y<x.length; y++)
	{
		charCode = x.charCodeAt(y);
		if((charCode >= 65 && charCode <= 90) || (charCode >=97 && charCode <=122))
			continue;
		else
			return(setErrorMessageFocus(Error_20.replace(/~~1/, fieldDesc), obj, focus));

		return(true);
	}
	return(true);
}
function chkSmallerThan(x, y, fieldDesc, obj){
	return	chkSmallerThanFocus(x, y, fieldDesc, obj,true);
}
function chkSmallerThanFocus(x, y, fieldDesc, obj, focus)
{
	tempx = trimSeparator(x);
	tempy = trimSeparator(y);

	if(parseFloat(tempx) >= parseFloat(tempy)){
		ErrorMessage = Error_32.replace(/~~1/, fieldDesc);
		return(setErrorMessageFocus(ErrorMessage.replace(/~~2/, y), obj, focus));
	}
	else
		return(true);
}
function chkNotGreaterThan(x,y,fieldDesc, obj){
	return	chkNotGreaterThanFocus(x,y,fieldDesc, obj, true);
}
function chkNotGreaterThanFocus(x,y,fieldDesc, obj, focus)
{
	tempx = trimSeparator(x);
	tempy = trimSeparator(y);

	if(parseFloat(tempx) > parseFloat(tempy)){
		ErrorMessage = Error_13.replace(/~~1/, fieldDesc);
		return(setErrorMessageFocus(ErrorMessage.replace(/~~2/, y), obj,focus));
	}
	else
		return(true);
}
function chkNotSmallerThan(x,y,fieldDesc, obj){
	return	chkNotSmallerThanFocus(x,y,fieldDesc, obj, true);
}
function chkNotSmallerThanFocus(x,y,fieldDesc, obj, focus)
{
	tempx = trimSeparator(x);
	tempy = trimSeparator(y);

	if(parseFloat(tempx) < parseFloat(tempy)){
		ErrorMessage = Error_14.replace(/~~1/, fieldDesc);
		return(setErrorMessageFocus(ErrorMessage.replace(/~~2/, y), obj,focus));
	}
	else
		return(true);
}
function chkDeciamlPlaceFocus(x, dp, fieldDesc, obj, focus){
	ErrorMessage = Error_47.replace(/~~1/, fieldDesc);
	ErrorMessage = ErrorMessage.replace(/~~2/, dp);
	if(chkDecimalPlace(trimSeparator(x), dp))
		return true;
	return(setErrorMessageFocus(ErrorMessage, obj, focus));
}
function chkDecimalPlace(x, dp) {
	// assume: valid number value input
	var pos = x.indexOf(".");
	if (pos == -1)
		return (true);
	else {
		if ((x.length - pos - 1) > dp)
			return (false);
		else
			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);	
}
function chkNumeric(x, fieldDesc, obj){
	return	chkNumericFocus(x, fieldDesc, obj, true);
}
function chkNumericFocus(x, fieldDesc, obj, focus)
{
	if(chknum(x))
		return(true);
	else
		return(setErrorMessageFocus(Error_01.replace(/~~1/, fieldDesc), obj, focus));

}
function chkNotBlank(x, fieldDesc,Compulsory, obj) {
	return	chkNotBlankFocus(x, fieldDesc,Compulsory, obj, true) ;
}
function chkNotBlankFocus(x, fieldDesc,Compulsory, obj, focus) {
	
	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";
				if (focus)
					chkFocus(obj);}
			return(false);
		}

		if ((obj.type =="textarea") && (x.length > 0) && Compulsory)
		{	
			var onlySpaceEnterChar = true;
			var y=0;
			for(y=0 ; y < x.length ; y++)
			{
				charCode = x.charCodeAt(y);
				if (	(charCode != 13) && (charCode != 10) && (charCode != 32))
						onlySpaceEnterChar=false;
			}
			if (onlySpaceEnterChar)
			{
				TotalErrorMessage += Error_03.replace(/~~1/, fieldDesc) + "\n";
				return(false);
			}			
		}

	}
	return(true);
}
function chkLessThan(x, y, fieldDesc, obj){
	return	chkLessThanFocus(x, y, fieldDesc, obj,true);
}
function chkLessThanFocus(x, y, fieldDesc, obj,focus)
{
	if(x.length > y)
	{
		ErrorMessage = Error_04.replace(/~~1/ , fieldDesc);
		return(setErrorMessageFocus(ErrorMessage.replace(/~~2./, (y + 1)+ " characters."), obj, focus));
	}
	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);
	if (space2==1)MM="0"+MM;
	YYYY = x.substring(space2+1,x.length);
	return YYYY+"/"+MM+"/"+DD;
}
function chkDate(x, fieldDesc, obj){
	return	chkDateFocus(x, fieldDesc, obj, true);
}
function chkDateFocus(x, fieldDesc, obj, focus){

	var tmpx=convertDateFormat(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(setErrorMessageFocus(Error_06.replace(/~~1/, fieldDesc), obj, focus));

	return(true);	
}
function chkAmount(x, fieldDesc, obj){
	return	chkAmountFocus(x, fieldDesc, obj, true)
}
function chkAmountFocus(x, fieldDesc, obj, focus){
	return(chkPositiveFocus(x,fieldDesc,obj, focus));
}
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){
	return	chkAmountSeparatorFocus(x, fieldDesc, obj, true);
}
function chkAmountSeparatorFocus(x, fieldDesc, obj, focus){
	noSeparator=trimSeparator(x);
	if(chkFloatFocus(noSeparator,fieldDesc,obj,focus)){
		if(chkSeparator(x,fieldDesc,obj))
			return true;
		return(setErrorMessageFocus(Error_19.replace(/~~1/, fieldDesc), obj, focus));
	}
	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 chkSearchStockName(x, fieldDesc, obj){
	return	chkSearchStockNameFocus(x, fieldDesc, obj, true);
}
function chkSearchStockNameFocus(x, fieldDesc, obj, focus){
	
	for(i=0;i<x.length;i++)
		if(x.charCodeAt(i)==32)
			return(setErrorMessage(Error_26.replace(/~~1/,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==39)
			continue;
		else
			return(setErrorMessageFocus(Error_12.replace(/~~1/, fieldDesc), obj, focus));
	}
	return(true);
}
function chkValidCharacters(x, fieldDesc, obj){
	return	chkValidCharactersFocus(x, fieldDesc, obj, true);
}
function chkValidCharactersFocus(x, fieldDesc, obj, focus){

	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)
			continue;
		else
			return(setErrorMessageFocus(Error_12.replace(/~~1/, fieldDesc), obj, focus));
	}
	return(true);
}
function chkAPSValidCharacters(x, fieldDesc, obj){
	return	chkAPSValidCharactersFocus(x, fieldDesc, obj, true);
}
function chkAPSValidCharactersFocus(x, fieldDesc, obj, focus){

	for(y=0 ; y < x.length ; y++)
	{	
		cc = x.charCodeAt(y);	

		if((cc >= 48 && cc <=57) || (cc >= 65 && cc <= 90) || (cc >= 97 && cc <= 122) || cc == 32 || cc == 46 || cc == 43 || cc == 45 || cc == 44 || cc== 42 || cc == 35 || cc == 40 || cc == 41 || cc == 38 || cc == 47 || cc == 36 || cc == 39)
			continue;
		else
			return(setErrorMessageFocus(Error_12.replace(/~~1/, fieldDesc), obj, focus));
	}
	return(true);
}
function chkInteger(x,fieldDesc, obj) {
	return	chkIntegerFocus(x,fieldDesc, obj, true);
}
function chkIntegerFocus(x,fieldDesc, obj, focus) {
	tmpx=trimSeparator(x);
	if(!isNaN(tmpx)&&tmpx.indexOf(".",0)==-1&&chkSeparator(x)){
		if ((tmpx-parseInt(tmpx,10))!=0){
			return(setErrorMessageFocus(Error_16.replace(/~~1/, fieldDesc), obj, focus));
		}
		return true;
	}
	else
		return(setErrorMessageFocus(Error_16.replace(/~~1/, fieldDesc), obj, focus));
}
function chkFloat(x,fieldDesc, obj){
	return	chkFloatFocus(x,fieldDesc, obj, true);
}
function chkFloatFocus(x,fieldDesc, obj, focus){
	ErrorMessage = Error_15.replace(/~~1/, fieldDesc);
	if(chkFloatingPoint(x))
		return true;
	return(setErrorMessageFocus(ErrorMessage, obj, focus));
}
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){
	return	chkNonNegativeFocus(x, fieldDesc, obj, true);
}
function chkNonNegativeFocus(x, fieldDesc, obj, focus){
	if(chkAmountSeparatorFocus(x,fieldDesc, obj,focus))
		x = trimSeparator(x);
	else
		return(false);

	if(chkFloatFocus(x,fieldDesc, obj, focus)){

		if(parseFloat(x) >= 0)
			return(true);
		else
			return(setErrorMessageFocus(Error_17.replace(/~~1/, fieldDesc), obj, focus));
	}
	else{
		return(false);
	}
}
function chkPositive(x, fieldDesc, obj){
	return	chkPositiveFocus(x, fieldDesc, obj, true);
}
function chkPositiveFocus(x, fieldDesc, obj, focus){
	
	if(chkAmountSeparatorFocus(x,fieldDesc, obj,focus))
		x = trimSeparator(x);
	else
		return(false);

	if(chkFloatFocus(x,fieldDesc, obj, focus)){
	
		if(parseFloat(x) > 0)
			return(true);
		else
			return(setErrorMessageFocus(Error_18.replace(/~~1/, fieldDesc), obj, focus));
	}
	else{
		return(false);
	}
}
function chkPositiveFloat(x, fieldDesc, obj){
	return	chkPositiveFloatFocus(x, fieldDesc, obj, true);
}
function chkPositiveFloatFocus(x, fieldDesc, obj, focus){
	
	if(chkFloatFocus(x,fieldDesc, obj, focus)){
	
		if(parseFloat(x) > 0)
			return(true);
		else
			return(setErrorMessageFocus(Error_18.replace(/~~1/, fieldDesc), obj, focus));
	}
	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 setLocation(obj, loc, waitFlag)
{
	if (waitFlag)
	{
		popupWait();
	}
	obj.href = loc;
}		
function popupWait()
{
	
}
function helpopen(name)
{
	var width = 500;
	var height = 500;
	var popup_x  = (screen.width  - width) / 2;
	var popup_y  = (screen.height - height) / 2;
	window.open(name,'HelpPage','width='+width+',height='+height+',resizable=yes,menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=yes,left='+popup_x+',top='+popup_y);
}
function openTimetable()
{
	window.open('/misc/en_US/timetable.htm','TimeTable', 'width=575,height=430,resizable=yes,menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=yes');
}
function openCutoffTimetable()
{
	window.open('/misc/en_US/timetable_cutoff.htm','CutOff_TimeTable', 'width=520,height=500,resizable=yes,menubar=no,toolbar=no,directories=no,location=no,scrollbars=no,status=no,top=0,left=0');
}
function owin(ooo)
{
	window.open(ooo, '', 'menubar=no,resizable=yes,toolbar=no,location=no,directories=no,scrollbars=no,center=yes,status=yes,width=400,height=400,screenX=150,screenY=200,top=200,left=150');
}
function OpenWindow(url)
{
	window.open(url, '', 'width=390,height=400,resizable=yes,menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=yes');
}
function chkIntAmount(x, fieldDesc, obj)
{
	return chkIntAmountFocus(x, fieldDesc, obj, true);
}
function chkIntAmountFocus(x, fieldDesc, obj, focus)
{
	return(chkPositiveFocus(x,fieldDesc, obj, focus) && chkIntegerFocus(x, fieldDesc, obj, focus));
}
function chkIntAmountIncZero(x, fieldDesc, obj)
{
	return	chkIntAmountIncZeroFocus(x, fieldDesc, obj, focus, true)
}
function chkIntAmountIncZeroFocus(x, fieldDesc, obj, focus)
{
	return(chkNonNegativeFocus(x, fieldDesc, obj, focus) && chkIntegerFocus(x, fieldDesc, obj ,focus));
}
function popMerchantList()
{
	var width = 500;
	var height = 250;
	var popup_x  = (screen.width  - width) / 2;
	var popup_y  = (screen.height - height) / 2;
	var listwin = window.open('http://www.hsbc.com.hk/hk/commercial/tool/bib/tl02005g.htm','HKBillPay','width='+width+',height='+height+',menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=yes,resizable=yes,left='+popup_x+',top='+popup_y);
}
function toDtl(acctType)
{
	if ((acctType.indexOf("BVMST") != -1))
		return "AcIPSSummary";		
	else if ((acctType.indexOf("CUA") != -1))
		return "AcCurrentDtl";
	else if ((acctType.indexOf("SAV") != -1))
		return "AcSavingDtl";
	else if ((acctType.indexOf("SSA") != -1))
		return "AcSavingDtl";
	else if ((acctType.indexOf("TMS") != -1))
		return "TdMiList";
	else if ((acctType.indexOf("MC") != -1))
		return "AcCombiSummary";
	else if ((acctType.indexOf("GLD") != -1))
		return "AcGoldDtl";
	else if ((acctType.indexOf("INV") != -1))
		return "IvPortfolioOverview";
	else if ((acctType.indexOf("SEE") != -1))
		return "AcSavingDtl";
	else if ((acctType.indexOf("USC") != -1))
		return "AcSavingHist";
	else if ((acctType.indexOf("CAC") != -1))
		return "AcSavingDtl";
	else if ((acctType.indexOf("SEC") != -1))
		return "IvPortfolioOverview";
	else if ((acctType.indexOf("0") != -1))
		return "";

}

function toHist(acctType)
{
	if ((acctType.indexOf("BVMST") != -1))
		return "AcIPSSummary";		
	else if ((acctType.indexOf("CUA") != -1))
		return "AcCurrentHist";
	else if ((acctType.indexOf("SAV") != -1))
		return "AcSavingHist";
	else if ((acctType.indexOf("SSA") != -1))
		return "AcSavingHist";
	else if ((acctType.indexOf("TMS") != -1))
		return "TdMiList";
	else if ((acctType.indexOf("MC") != -1))
		return "AcCombiSummary";
	else if ((acctType.indexOf("GLD") != -1))
		return "AcGoldHist";
	else if ((acctType.indexOf("INV") != -1))
		return "IvPortfolioOverview";
	else if ((acctType.indexOf("SEE") != -1))
		return "AcSavingHist";
	else if ((acctType.indexOf("USC") != -1))
		return "AcSavingHist";
	else if ((acctType.indexOf("CAC") != -1))
		return "AcSavingHist";
	else if ((acctType.indexOf("SEC") != -1))
		return "IvPortfolioOverview";
	else if ((acctType.indexOf("0") != -1))
		return "";
}

function printScreen()
{
	if (navigator.userAgent.toLowerCase().indexOf("mac")!=-1)
	{
		alert("To print this screen, press 'Cntl' + 'P'");
	}
	else 
		window.print();
}
function popUpTS()
{
	var width = 680;
	var height = 440;
	var offset_x = (screen.width - width)/2;
	var offset_y = (screen.height - height)/2;
	var otherpoplink=window.open('http://www.hsbc.com.hk/hk/commercial/intbiz/default.htm', 'service', 'width='+width+', height='+height+',top='+offset_y+',left='+offset_x+'menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=no,resizable=yes');
}
function popUpMI()
{	
	var width = 725;
	var height = 475;
	var offset_x = (screen.width - width)/2;
	var offset_y = (screen.height - height)/2;
	var otherpoplink=window.open('http://www.hsbc.com.hk/hk/commercial/cash/invest/default.htm', 'service', 'width='+width+', height='+height+',top='+offset_y+',left='+offset_x+'menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=no,resizable=yes');
}
function popUpDemo()
{
	var width = 725;
	var height = 475;
	var offset_x = (screen.width - width)/2;
	var offset_y = (screen.height - height)/2;
	var otherpoplink=window.open('http://www.hsbc.com.hk/hk/commercial/tool/bib/tngdemo/start.htm', 'service', 'width='+width+', height='+height+',top='+offset_y+',left='+offset_x+'menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=no,resizable=yes');
}
function popup(url, name, width, height, other) {
	var popup_x  = (screen.width  - width) / 2;
	var popup_y  = (screen.height - height) / 2;
	var bottomlink = window.open(url, "bottomlink", "width=" + width + ",height=" + height + ",left=" + popup_x + ",top=" + popup_y + other);
}
//for pull down list of date
function chkDateCompareFromTo(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);
}
function changeLang(form, lang, country)
{
	form.submit();
}
//end of pull down list of date
// For BIB-ITS switching points
function linkToITS(submitForm, queryStringKey, menuReqName, url)
{
	submitForm.ITSQSKey.value = queryStringKey;
	submitForm.MenuReqName.value = menuReqName;
	submitForm.action = url;
	javascript:WaitFlag=true;processFormResult(submitForm);	
}
function popupIndExRate()
{
	var width = 350;
	var height = 300;
	var popup_x  = (screen.width  - width) / 2;
	var popup_y  = (screen.height - height) / 2;
	var ratewin = window.open('/servlet/GetIndExRate','IndExRate','width='+width+',height='+height+',menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=yes,resizable=yes,left='+popup_x+',top='+popup_y);
}

function openPopupPrint(params)
{
	window.open('FnPopupPrint?'+params,'AckPrint', 'width=700,height=600,resizable=yes,menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=yes');
}
function openPopupSave(params)
{
	if (navigator["appName"].indexOf("Microsoft Internet Explorer")!=-1)
		window.open('FnPopupSave?'+params,'AckSave', 'width=700,height=600,resizable=yes,menubar=no,toolbar=no,directories=no,location=no,scrollbars=yes,status=yes');
	else 
		window.open('FnPopupSave?'+params,'AckSave', 'width=700,height=600,resizable=yes,menubar=yes,toolbar=no,directories=no,location=no,scrollbars=yes,status=yes');
}

function startAlertTimer(msg){
	timeout_msg = msg;
	return setTimeout("alert(timeout_msg)", 15*60*1000);
}

function trimString(x)
{
	while (x.substring(0,1) == " " )
	{	templength=x.length;
		x = x.substring(1,templength);
	}

	while (x.substring(x.length-1,x.length) == " " )
	{	x=x.substring(0,x.length-1)
	}
	return x
}

function focusNext(from, to, len){
	if (from.value.length==len) {
		to.focus();
	}
}

function handleSecControl(form){
	if (document.secctrlForm != null)
	{	   
		chkSecControl(document.secctrlForm);
		copyFormToHiddenField(document.secctrlForm, form);
	}
}

function copyFormToHiddenField(srcForm, trgForm){

	var i;

	// init target form hidden field to empty, remove it if not required.
	for (i=0; i<srcForm.length; i++) {
		var element = srcForm.elements[i];
		trgForm[element.name].value = "";	   
	}

	for (i=0; i<srcForm.length; i++) {
		var element = srcForm.elements[i];

		switch (element.type) {
			case "text":
			case "textarea":
			case "password":
			case "hidden":
					trgForm[element.name].value = element.value;
				break;
			case "radio":
				if (element.checked)
					trgForm[element.name].value = element.value;
				break;
			case "checkbox":
				if (element.checked)
					trgForm[element.name].value = element.value;
				break;
			case "select-one":
					if (element.selectedIndex != -1)
						trgForm[element.name].value = element.options[element.selectedIndex].value;
				break;
			default:
				// ignore other form element type
		}
	}
}
function chkMessageBodyFocus(msg, fieldDesc, obj, focus) {
	var i;
	var validMsg = true;
	var firstNewLine = false;//new line char compose of 13AND10
	if (msg!="")
	{
		for(i=0; i<msg.length; i++)
		{
			var c = msg.charAt(i);
			// error occurs when the mail content contains "{", "|" , "}" , "<" , ">"
			if(c == unescape('%7B') || c == unescape('%7C') || c == unescape('%7D') || c==unescape('%3C') || c==unescape('%3E')){
				TotalErrorMessage += Error_12.replace(/~~1/, fieldDesc) + "\n";
				if (focus)
					chkFocus(obj);
				return false;
			}
				
			if((c>=unescape('%20')&&c<=unescape('%7E'))|| c==unescape('%0D')){
				if(c==unescape('%0D')){
					firstNewLine = true;
				}
				continue;
			}
			else {
				if(firstNewLine && c==unescape('%0A')){
					firstNewLine = false;
					continue;
				}
				TotalErrorMessage += Error_12.replace(/~~1/, fieldDesc) + "\n";
				if (focus)
					chkFocus(obj);
				return false;
			}
		}
	}
	return validMsg;
}

function chkEmailFocus(x, fieldDesc, obj, focus)
{

	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 == 39 || charCode == 45 || charCode == 95 || charCode == 47 || charCode == 63 || charCode == 46 || charCode == 64)
			continue;
		else
			return(setErrorMessage(Error_56.replace(/~~1/, fieldDesc), obj, focus));
	}

	if (	(x.charCodeAt(0)==46) || (x.charCodeAt(0)==64) || 
			(x.charCodeAt(x.length-1)==46) || (x.charCodeAt(x.length-1)==64)	)
	{
		return(setErrorMessage(Error_56.replace(/~~1/, fieldDesc), obj, focus));	
	}

	var s1 = x.indexOf("@");
	var s2 = x.lastIndexOf("@");

	if (s1 != s2 || s1 <= 0) 
	{
		return(setErrorMessage(Error_56.replace(/~~1/, fieldDesc),obj, focus));
	}

	if (x.indexOf(".") < 0)
		return(setErrorMessage(Error_56.replace(/~~1/, fieldDesc),obj, focus));

   	if ((x.indexOf("@@") >= 0) ||
		(x.indexOf(".@") >= 0) ||
		(x.indexOf("@.") >= 0) ||
		(x.indexOf("..") >= 0)	)
		{
			return(setErrorMessage(Error_56.replace(/~~1/, fieldDesc),obj, focus));
		}

	return(true);
}

function validstocknum(x) {
	if (x.value == "" || x.value == null) {
		alert(Error_200);
		x.focus();
		return (false);
	} else {
		var nochar = 0;
		var fchar = -1;
		var lchar = -1;
		for (count = 0; x.value.length > count ; count++) {
			var c = x.value.charCodeAt(count);
			if (! ( (c <= 57 && c >= 48) || c == 10 || c == 13 || c == 32 ) ) {
				alert(Error_201);
				x.focus();
				return (false);
			}
			if (c <= 57 && c >= 48) {
				nochar = nochar + 1;
				lchar = count;
			}
			if ((c <= 57 && c >= 48) && fchar == -1) fchar = count;
		}
		if (nochar > 5) {
			alert(Error_202);
			x.focus();
			return (false);
		}
		if (lchar - fchar + 1 != nochar) {
	 		alert(Error_203);
			x.focus();
			return (false);
		}
	}
	return (true);
}
function externalPopup(url) {
	window.open(url, '', 'menubar=no,resizable=yes,toolbar=no,location=no,directories=no,scrollbars=yes,center=yes,status=yes,width=800,height=600,screenX=150,screenY=200,top=200,left=150');
}