// JavaScript Document
function validateFrm(){
		
		var frm2Mask = document.getElementById('frm2Mask');
		var frm3Mask = document.getElementById('frm3Mask');
		
		var email = document.frm.email.value;
		var ic = document.frm.icno.value;
		//var dob = document.frm.dob.value;
		
		var list = document.frm.currency.value.split(":");
		var rate_code=list[0];
		var rate=list[1];
		document.frm.rate_code.value = rate_code;
		document.frm.rate.value = rate;
		
		
		//document.getElementById('pre_dis').className='disableBtn';
		document.getElementById('sub_dis').className='disableBtn';
	
		makeAllValid();
		
		//validate form 1
		if(!validateString(document.frm.name.value)) {makeInvalid(document.frm.name);return hideAllFrm(frm2Mask,frm3Mask)};
		if(!validateString(document.frm.nationality.value)) {makeInvalid(document.frm.nationality); return hideAllFrm(frm2Mask,frm3Mask)};
		if(!validateIC(ic)){makeInvalid(document.frm.icno); return hideAllFrm(frm2Mask,frm3Mask)};
		//if(!validateDOB(dob)){makeInvalid(document.frm.dob); return hideAllFrm(frm2Mask,frm3Mask)};
		if(!df.isValid()){ return hideAllFrm(frm2Mask,frm3Mask)};
		if(!validateString(document.frm.tel.value)) {makeInvalid(document.frm.tel); return hideAllFrm(frm2Mask,frm3Mask)};
		if(!validateEmail(email)) {makeInvalid(document.frm.email); return hideAllFrm(frm2Mask,frm3Mask)};
		if(!validateString(document.frm.address.value)) {makeInvalid(document.frm.address); return hideAllFrm(frm2Mask,frm3Mask)};
		if(!validateString(document.frm.postal.value)) {makeInvalid(document.frm.postal); return hideAllFrm(frm2Mask,frm3Mask)};
		if(!validateString(document.frm.purpose.value)) {makeInvalid(document.frm.purpose); return hideAllFrm(frm2Mask,frm3Mask)};
		
		//frm 1 valid show another frm
		showFrm(frm2Mask);
		
		if(!validateString(document.frm.r_name.value)) {makeInvalid(document.frm.r_name); return hideFrm(frm3Mask)};
		if(!validateString(document.frm.r_address.value)) {makeInvalid(document.frm.r_address); return hideFrm(frm3Mask)};
		if(!validateString(document.frm.bank.value)) {makeInvalid(document.frm.bank); return hideFrm(frm3Mask)};
		if(!validateString(document.frm.accountno.value)) {makeInvalid(document.frm.accountno); return hideFrm(frm3Mask)};
		if(!validateString(document.frm.branch.value)) {makeInvalid(document.frm.branch); return hideFrm(frm3Mask)};
		if(!validateString(document.frm.unitno.value)) {makeInvalid(document.frm.unitno); return hideFrm(frm3Mask)};
		if(!validateString(document.frm.relationship.value)) {makeInvalid(document.frm.relationship); return hideFrm(frm3Mask)};
		
		//frm 1 valid show another frm
		showFrm(frm3Mask);
		
		//if(!validateString(document.frm.amount.value) || isNaN(document.frm.amount.value)) {makeInvalid(document.frm.amount); return false};
		if(!validateString(document.frm.rate_code.value)) {makeInvalid(document.frm.rate_code); return false};
		if(!validateString(document.frm.rate.value)) {makeInvalid(document.frm.rate); return false};
		
		
		//document.getElementById('pre_dis').className='enableBtn';
		document.getElementById('sub_dis').className='enableBtn';
		
		return true;
}

function makeInvalid(obj){
	if(obj.className.indexOf('error')<0)
		obj.className= obj.className + '_error';
}

function makeAllValid(){
	document.frm.name.className = document.frm.name.className.replace('_error', '');
	document.frm.nationality.className = document.frm.nationality.className.replace('_error', '');
	document.frm.tel.className = document.frm.tel.className.replace('_error', '');
	document.frm.email.className = document.frm.email.className.replace('_error', '');;
	document.frm.icno.className = document.frm.icno.className.replace('_error', '');;
	document.frm.dob.className = document.frm.dob.className.replace('_error', '');;
	document.frm.address.className = document.frm.address.className.replace('_error', '');
	document.frm.postal.className = document.frm.postal.className.replace('_error', '');
	document.frm.purpose.className = document.frm.purpose.className.replace('_error', '');
	
	document.frm.r_name.className = document.frm.r_name.className.replace('_error', '');
	document.frm.r_address.className = document.frm.r_address.className.replace('_error', '');
	document.frm.bank.className = document.frm.bank.className.replace('_error', '');
	document.frm.accountno.className = document.frm.accountno.className.replace('_error', '');
	document.frm.branch.className = document.frm.branch.className.replace('_error', '');
	document.frm.unitno.className = document.frm.unitno.className.replace('_error', '');
	document.frm.relationship.className = document.frm.relationship.className.replace('_error', '');
	
	//document.frm.amount.className = document.frm.amount.className.replace('_error', '');
	document.frm.rate_code.className = document.frm.rate_code.className.replace('_error', '');
	document.frm.rate.className = document.frm.rate.className.replace('_error', '');
}

function submitForm(){
	if(validateFrm()){
		document.frm.submit();	
	}
}

function hideAllFrm(frm1, frm2){
	hideFrm(frm1);
	hideFrm(frm2);
}

function hideFrm(frm){
	frm.className = 'maskF';
}

function showFrm(frm){
	frm.className = 'nomaskF';	
}

function validateCFrm(){	
		//document.getElementById('pre_dis').className='disableBtn';
		document.getElementById('submitBtn').className='disableBtn';
		makeCAllValid();
		//validate form 1
		if(!validateString(document.frm.name.value)) {makeInvalid(document.frm.name); return false;};
		if(!validateEmail(document.frm.email.value)) {makeInvalid(document.frm.email); return false};
		if(!validateString(document.frm.message.value)){makeInvalid(document.frm.message); return false};
				
		//document.getElementById('pre_dis').className='enableBtn';
		document.getElementById('submitBtn').className='enableBtn';
		
		return true;
}

function makeCAllValid(){
	document.frm.name.className = document.frm.name.className.replace('_error', '');
	document.frm.email.className = document.frm.email.className.replace('_error', '');
	document.frm.message.className = document.frm.message.className.replace('_error', '');
	
}


function resetCFrm(){
	makeCAllValid();
	document.frm.name.value = "";
	document.frm.email.value = "";
	document.frm.message.value = "";
	document.getElementById('submitBtn').className='disableBtn';
}

function submitCFrm(){
	if(validateCFrm()){
		document.frm.submit();	
	}
}

function validateEmail(email){
	var RE_EMAIL = /^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/;
	
	return RE_EMAIL.test(email);
}

function validateIC(ic){
	var RE_IC = /^[A-Za-z]\d{7}[A-Za-z]$/;
	return RE_IC.test(ic);
}

function validateDOB(dob){
	var RE_DOB = /^\d{8}$/;
	return RE_DOB.test(dob);	
}

function validateString(str){
	str = str.replace(/^\s*/, "").replace(/\s*$/, "");
	if(str==""){
		return false;	
	}
	return true;
}


