var Error_02 = "~~1 should be greater than ~~2.";
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_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_34 = "~~1 should be ~~2-digit number";
var Error_35 = "~~1 should be ~~2";
var Error_36 = "~~1 duplicated.";
var Error_37 = "~~1 should input only one.";
var Error_38 = "~~1 must have ~~2 decimal points.";
var Error_39 = "~~1 value should be less than or equal to ~~2.";
var Error_40 = "~~1 must have a maximum of ~~2 decimal points.";
var Error_41 = "~~1 should not contain \".";

//customized for Cross Border Application
if (lang=="eng") {
	var Error_01 = '"~~1" should be in numbers.';
	var Error_03 = 'Please provide "~~1".';
	var Error_23 = 'Please select "~~1".';
	var Error_32 = '"~~1" must contain "@" character.';
	var Error_33 = '"~~1" must contain "." character.';
	var Error_42 = '"~~1" exceed maximum length';
	var Error_43 = 'First/Last character of "~~1" must not be "@"';
	var Error_44 = 'First/Last character of "~~1" must not be "."';
	var Error_45 = '"~~1" must have one and only one "@" character';
}
else {
	var Error_01 = '"~~1"必須是數字。';
	var Error_03 = '請提供"~~1"。';
	var Error_23 = '請選擇"~~1"。';
	var Error_32 = '"~~1"必須有"@"字元。';
	var Error_33 = '"~~1"必須有"."字元。';
	var Error_42 = '"~~1"太長。';
	var Error_43 = '"~~1"最前或最後一個字元不可是"@"。';
	var Error_44 = '"~~1"最前或最後一個字元不可是"."。';
	var Error_45 = '"~~1"不可有多過一個"@"字元。';

}

var TotalErrorMessage = "";
var Flag = false;
var submitForm = "yes";

function chkValueComp(x, fieldDesc, obj, inValue){
	if (x!=inValue){
		ErrorMessage=Error_35.replace(/~~1/,fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/,inValue),obj));}
}

function fchkLength(x, fieldDesc, obj, inLength){
	if (x.length!=inLength){
		ErrorMessage=Error_34.replace(/~~1/,fieldDesc);
		return(setErrorMessage(ErrorMessage.replace(/~~2/,inLength),obj));}
	else
		return chkNumeric(x, fieldDesc, obj);
}

function fchkSign(x, fieldDesc, obj){
	if (x.indexOf('@')==-1)
		return(setErrorMessage(Error_32.replace(/~~1/,fieldDesc),obj));
	else
		return fchkDot(x, fieldDesc, obj);
}

function fchkDot(x, fieldDesc, obj) {
	if (x.indexOf('.')==-1)
		return(setErrorMessage(Error_33.replace(/~~1/,fieldDesc),obj));
	else
		return(true);
}


// 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 checkIfSubmitted(){
	if(submitForm=="yes"){
		submitForm="no";
		return true;
	}else{
		return false;
	}
}


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 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)
{
	//alert('chkFocus');
	if(!Flag)
	{
		//alert('chkFocus - if');
		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 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);
	var Today = new Date();
	var Now = Today.getFullYear() + "/" + (Today.getMonth()+1) + "/" +Today.getDate();

	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)
{
	var Today = new Date();
	var Now = Today.getFullYear() + "/" + (Today.getMonth() + 1) + "/" + Today.getDate();

	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)
{
	var Today = new Date();
	var Now = Today.getFullYear() + "/" + (Today.getMonth() + 1) + "/" + Today.getDate();

	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);
		}


//====	trap input of double-quote, not allow prompt with error message.
//		else {
//		if (x.indexOf('"') > -1) {
//			TotalErrorMessage += Error_41.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;
	else if (x <= '01/01/1900')
		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 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()
{
	window.open("/PleaseWait.htm","HSBCPopUp","width=390,height=140,resizable=no,menubar=no,toolbar=no,directories=no,location=no,scrollbars=no,status=no");
}

function chkIntAmount(x, fieldDesc, obj)
{
	return(chkPositive(x,fieldDesc, obj) && chkInteger(x, fieldDesc, obj));
}

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 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 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);
}

