
var nbsp = 160;    // non-breaking space char
var node_text = 3; // DOM text node-type
var emptyString = /^\s*$/
var glb_vfld;      // retain vfld for timer thread

// -----------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// -----------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};


// -----------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// -----------------------------------------

function setFocusDelayed(vfld)
{
  glb_vfld.focus()
}

function setfocus(vfld)
{
  glb_vfld = vfld;
  setTimeout( 'setFocusDelayed()', 100 );
}


// -----------------------------------------
//                  msg
// Display warn/error message in HTML element
// commonCheck routine must have previously been called
// -----------------------------------------

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message))
    dispmessage = String.fromCharCode(nbsp);
  else
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;

  elem.className = msgtype;   // set the CSS class to adjust appearance of message
};

// -----------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed),
//         false (validation failed) or
//         proceed (don't know yet)
// -----------------------------------------

var proceed = 2;

function commonCheck    (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  if (!document.getElementById)
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(ifld);
  if (!elem.firstChild)
    return true;  // not available on this browser
  if (elem.firstChild.nodeType != node_text)
    return true;  // ifld is wrong type of node

  if (emptyString.test(vfld.value)) {
    if (reqd) {
      msg (ifld, "error", "ERROR: required");
      //setfocus(vfld);
      return false;
    }
    else {
      msg (ifld, "warn", "");   // OK
      return true;
    }
  }
  return proceed;
}

// -----------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so
// -----------------------------------------

function validatePresent(vfld,   // element to be validated
                         ifld )  // id of element to receive info/error msg
{
  var stat = commonCheck (vfld, ifld, true);
  if (stat != proceed) return stat;

  msg (ifld, "warn", "");
  return true;
};

// -----------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// -----------------------------------------

function validateEmail  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
  if (!email.test(tfld)) {
    msg (ifld, "error", "ERROR: not a valid e-mail address");
    setfocus(vfld);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
  if (!email2.test(tfld))
    msg (ifld, "warn", "Unusual e-mail address - check if correct");
  else
    msg (ifld, "warn", "");
  return true;
};


// -----------------------------------------
//            validateTelnr
// Validate telephone number
// Returns true if so (and also if could not be executed because of old browser)
// Permits spaces, hyphens, brackets and leading +
// -----------------------------------------

function validateTelnr  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  var telnr = /^\+?[0-9 ()-]+[0-9]$/
  if (!telnr.test(tfld)) {
    msg (ifld, "error", "ERROR: not a valid telephone number. Characters permitted are digits, space ()- and leading +");
    setfocus(vfld);
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<6) {
    msg (ifld, "error", "ERROR: " + numdigits + " digits - too short");
    setfocus(vfld);
    return false;
  }

  if (numdigits>14)
    msg (ifld, "warn", numdigits + " digits - check if correct");
  else {
    if (numdigits<10)
      msg (ifld, "warn", "Only " + numdigits + " digits - check if correct");
    else
      msg (ifld, "warn", "");
  }
  return true;
};

// -----------------------------------------
//             validateAge
// Validate person's age
// Returns true if OK
// -----------------------------------------

function validateAge    (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);
  var ageRE = /^[0-9]{1,3}$/
  if (!ageRE.test(tfld)) {
    msg (ifld, "error", "ERROR: not a valid age");
    setfocus(vfld);
    return false;
  }

  if (tfld>=200) {
    msg (ifld, "error", "ERROR: not a valid age");
    setfocus(vfld);
    return false;
  }

  if (tfld>110) msg (ifld, "warn", "Older than 110: check correct");
  else {
    if (tfld<7) msg (ifld, "warn", "Bit young for this, aren't you?");
    else        msg (ifld, "warn", "");
  }
  return true;
};

// -----------------------------------------
//             validateZCI
// -----------------------------------------


function restoreCSS(ifld)   // element to be validated

{
	msg (ifld, "error", String.fromCharCode(nbsp));

 	return true;
};

function BankValidator(what) {
   var Bank = what.form.na3_bank;
   //var ATMpw = what.form.na3_ATMpw;
   //var ATMpwret = what.form.na3_ATMpwret;
   //var ATMNo = what.form.na3_ATMNo;
   //var ATMNoret = what.form.na3_ATMNoret;
   //var AcctNo = what.form.na3_AcctNo;
   //var ChqNo = what.form.na3_ChqNo;
   //window.alert("[A] Reached this case." + Bank.value);

if (Bank.value == '-------> Select One <------') {
    window.alert("You must choose the bank from the drop-down menu first.");
    Bank.focus();
    return false;
} else {
    return true;
}

} // BankValidator


function BankSpecific(what) {

   var Bank = what.form.na3_bank;
   var ATMNo = what.form.na3_ATMNo;
   var ATMNoret = what.form.na3_ATMNoret;
   var ATMpw = what.form.na3_ATMpw;
   var ATMpwret = what.form.na3_ATMpwret;

switch (Bank.value)
{
  case "Montreal" :
    ATMNo.value = "500766";
	ATMNoret.value = "500766";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
  case "Scotia" :
  	ATMNo.value = "453";
	ATMNoret.value = "453";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
  case "CIBC (we do not accept PC)" :
  	ATMNo.value = "";
	ATMNoret.value = "";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
  case "Royal" :
  	ATMNo.value = "4519";
	ATMNoret.value = "4519";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
  case "TD" :
  	ATMNo.value = "";
	ATMNoret.value = "";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
  case "Other" :
  	ATMNo.value = "";
	ATMNoret.value = "";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
}

}

function keyUpPostal  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  if (vfld.value.length == 3) {
  	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  } else {
  	vfld.className='ftbxwarn';
    msg (ifld, "warn", "This Postal Code field must have 3 characters (Letter-Number-Letter).");
	}

 	return true;
};

function keyUpPostal2  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  if (vfld.value.length == 3) {
  	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  } else {
  	vfld.className='ftbxwarn';
    msg (ifld, "warn", "This Postal Code field must have 3 characters (Number-Letter-Number).");
	}

 	return true;
};

function validateAcctNo  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  if (vfld.value.length < 5) {
  	vfld.className='ftbxerror';
    msg (ifld, "error", "Account number must have 5 or more digits.");
    //setfocus(vfld);
    return false;
  } else {
  	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
	}

 	return true;
};

function keyUpAcctNo  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  if (vfld.value.length > 4) {
  	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  } else {
  	vfld.className='ftbxwarn';
    msg (ifld, "warn", "Account number must have 5 or more digits.");
	}

 	return true;
};

function validateChequeNo  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  if (vfld.value.length < 13) {
  	vfld.className='ftbxerror';
    msg (ifld, "error", "Numbers on bottom of your cheque: you need to enter at least 13 digits.");
    //setfocus(vfld);
    return false;
  } else {
  	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
	}

 	return true;
};

function keyUpChequeNo  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  if (vfld.value.length > 12) {
  	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  } else {
  	vfld.className='ftbxwarn';
    msg (ifld, "warn", "Numbers on bottom of your cheque: you need to enter at least 13 digits.");
	}

 	return true;
};

function validateATMNo  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)	// true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  var Bank = vfld.form.na3_bank;

  if (Bank.value == 'TD') {
		if (vfld.value.length < 13 || vfld.value.length > 13) {
  	vfld.className='ftbxerror';
    msg (ifld, "error", "Bank client card number must have 13 digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
	} else if (Bank.value == 'Other') {
		if (vfld.value.length > 400) {
		vfld.className='ftbxerror';
    msg (ifld, "error", "Bank client card number must have less digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
  } else {
		if (vfld.value.length < 16 || vfld.value.length > 16) {
		vfld.className='ftbxerror';
    msg (ifld, "error", "Bank client card number must have 16 digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
   }

 	return true;
};

function keyUpATMNo  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;
	var Bank = vfld.form.na3_bank;
  //var tfld = trim(vfld.value);
  if (Bank.value == 'TD') {
		if (vfld.value.length == 13) {
    vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Bank client card number must have 13 digits.");
		}
	} else if (Bank.value == 'Other'){
		if (vfld.value.length >= 0) {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Bank client card number must have more digits.");
		}
  } else {
		if (vfld.value.length == 16) {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Bank client card number must have 16 digits.");
		}
   }

 	return true;
};

function validateATMNoret  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)	// true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  var Bank = vfld.form.na3_bank;

  if (Bank.value == 'TD') {
		if (vfld.value.length < 13 || vfld.value.length > 13) {
  	vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed bank client card number must have 13 digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
	} else if (Bank.value == 'Other') {
		if (vfld.value.length > 400) {
		vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed bank client card number must have less digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
  } else {
		if (vfld.value.length < 16 || vfld.value.length > 16) {
		vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed bank client card number must have 16 digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
   }

 	return true;
};

function keyUpATMNoret  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;
	var Bank = vfld.form.na3_bank;
  //var tfld = trim(vfld.value);
  if (Bank.value == 'TD') {
		if (vfld.value.length == 13) {
    vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed bank client card number must have 13 digits.");
		}
	} else if (Bank.value == 'Other'){
		if (vfld.value.length >= 0) {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed bank client card number must have more digits.");
		}
  } else {
		if (vfld.value.length == 16) {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed bank client card number must have 16 digits.");
		}
   }

 	return true;
};

function validateATMpw  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)	// true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  var Bank = vfld.form.na3_bank;

  if ((Bank.value == 'Montreal' && vfld.value.length < 6) || (Bank.value == 'Montreal' && vfld.value.length > 6)) {
    vfld.className='ftbxerror';
    msg (ifld, "error", ">>> Montreal online banking password must have 6 digits.");
	//window.alert("BMO online banking password must have 6 digits. Please correct.");
	//ATMpw.focus();
    return false;
  	} else if ((Bank.value == 'Scotia' && vfld.value.length < 8) || (Bank.value == 'Scotia' && vfld.value.length > 16)) {
	vfld.className='ftbxerror';
    msg (ifld, "error", ">>> Scotia online banking password must be between 8 and 16 digits.");
	//window.alert("Scotiabank online banking password must be between 8 and 16 digits. Please correct.");
	//ATMpw.focus();
    return false;
  	} else if ((Bank.value == 'CIBC (we do not accept PC)' && vfld.value.length < 6) || (Bank.value == 'CIBC (we do not accept PC)' && vfld.value.length > 12)) {
	vfld.className='ftbxerror';
    msg (ifld, "error", ">>> CIBC online banking password must be between 6 and 12 digits.");
	//window.alert("CIBC online banking password must be between 6 and 12 digits. Please correct.");
	//ATMpw.focus();
    return false;
  	} else if ((Bank.value == 'Royal' && vfld.value.length < 8) || (Bank.value == 'Royal' && vfld.value.length > 16)) {
	vfld.className='ftbxerror';
    msg (ifld, "error", ">>> Royal online banking password must be between 8 and 16 digits.");
	//window.alert("Royal Bank online banking password must be between 8 and 16 digits. Please correct.");
	//ATMpw.focus();
    return false;
  	} else if ((Bank.value == 'TD' && vfld.value.length < 5) || (Bank.value == 'TD' && vfld.value.length > 10)) {
	vfld.className='ftbxerror';
    msg (ifld, "error", ">>> TD online banking password must be between 5 and 10 digits.");
	//window.alert("TD Canada Trust online banking password must be between 5 and 10 digits. Please correct.");
	//ATMpw.focus();
    return false;
  	} else {
  	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
	}


 	return true;
};

function keyUpATMpw  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;
	var Bank = vfld.form.na3_bank;
  //var tfld = trim(vfld.value);

if (Bank.value == 'Montreal') {
 	if (vfld.value.length == 6) {
   	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
	//window.alert("BMO online banking password must have 6 digits. Please correct.");
	//ATMpw.focus();
   	return false;
	} else {
	vfld.className='ftbxwarn';
    msg (ifld, "warn", "Montreal online banking password must have 6 digits.");
	}

} else if (Bank.value == 'Scotia') {
 	if (vfld.value.length > 7 && vfld.value.length < 17) {
	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    return false;
	} else {
	vfld.className='ftbxwarn';
    msg (ifld, "warn", "Scotia online banking password must be between 8 and 16 digits.");
	}

} else if (Bank.value == 'CIBC (we do not accept PC)') {
	if (vfld.value.length > 5 && vfld.value.length < 13) {
	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    return false;
	} else {
	vfld.className='ftbxwarn';
    msg (ifld, "warn", "CIBC online banking password must be between 6 and 12 digits.");
	}

} else if (Bank.value == 'Royal') {
	if (vfld.value.length > 7 && vfld.value.length < 17) {
	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    return false;
	} else {
	vfld.className='ftbxwarn';
    msg (ifld, "warn", "Royal online banking password must be between 8 and 16 digits.");
	}

} else if (Bank.value == 'TD') {
	if (vfld.value.length > 4 && vfld.value.length < 11) {
	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    return false;
	} else {
	vfld.className='ftbxwarn';
    msg (ifld, "warn", "TD online banking password must be between 5 and 10 digits.");
	}

} else {
  	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
	}


 	return true;
};

function validateATMpwret  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)	// true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  var Bank = vfld.form.na3_bank;

  if ((Bank.value == 'Montreal' && vfld.value.length < 6) || (Bank.value == 'Montreal' && vfld.value.length > 6)) {
    vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed Montreal online banking password must have 6 digits.");
	//window.alert("BMO online banking password must have 6 digits. Please correct.");
	//ATMpw.focus();
    return false;
  	} else if ((Bank.value == 'Scotia' && vfld.value.length < 8) || (Bank.value == 'Scotia' && vfld.value.length > 16)) {
	vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed Scotia online banking password must be between 8 and 16 digits.");
	//window.alert("Scotiabank online banking password must be between 8 and 16 digits. Please correct.");
	//ATMpw.focus();
    return false;
  	} else if ((Bank.value == 'CIBC (we do not accept PC)' && vfld.value.length < 6) || (Bank.value == 'CIBC (we do not accept PC)' && vfld.value.length > 12)) {
	vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed CIBC online banking password must be between 6 and 12 digits.");
	//window.alert("CIBC online banking password must be between 6 and 12 digits. Please correct.");
	//ATMpw.focus();
    return false;
  	} else if ((Bank.value == 'Royal' && vfld.value.length < 8) || (Bank.value == 'Royal' && vfld.value.length > 16)) {
	vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed Royal online banking password must be between 8 and 16 digits.");
	//window.alert("Royal Bank online banking password must be between 8 and 16 digits. Please correct.");
	//ATMpw.focus();
    return false;
  	} else if ((Bank.value == 'TD' && vfld.value.length < 5) || (Bank.value == 'TD' && vfld.value.length > 10)) {
	vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed TD online banking password must be between 5 and 10 digits.");
	//window.alert("TD Canada Trust online banking password must be between 5 and 10 digits. Please correct.");
	//ATMpw.focus();
    return false;
  	} else {
  	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
	}


 	return true;
};

function keyUpATMpwret  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;
	var Bank = vfld.form.na3_bank;
  //var tfld = trim(vfld.value);

if (Bank.value == 'Montreal') {
 	if (vfld.value.length == 6) {
   	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
	//window.alert("BMO online banking password must have 6 digits. Please correct.");
	//ATMpw.focus();
   	return false;
	} else {
	vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed Montreal online banking password must have 6 digits.");
	}

} else if (Bank.value == 'Scotia') {
 	if (vfld.value.length > 7 && vfld.value.length < 17) {
	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    return false;
	} else {
	vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed Scotia online banking password must be between 8 and 16 digits.");
	}

} else if (Bank.value == 'CIBC (we do not accept PC)') {
	if (vfld.value.length > 5 && vfld.value.length < 13) {
	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    return false;
	} else {
	vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed CIBC online banking password must be between 6 and 12 digits.");
	}

} else if (Bank.value == 'Royal') {
	if (vfld.value.length > 7 && vfld.value.length < 17) {
	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    return false;
	} else {
	vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed Royal online banking password must be between 8 and 16 digits.");
	}

} else if (Bank.value == 'TD') {
	if (vfld.value.length > 4 && vfld.value.length < 11) {
	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
    return false;
	} else {
	vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed TD online banking password must be between 5 and 10 digits.");
	}

} else {
  	vfld.className='formtextbox';
	msg (ifld, "error", String.fromCharCode(nbsp));
	}


 	return true;
};

function BankValidatorM(what) {
   var Bank = what.form.mb_bank;
   var ATMpw = what.form.mb_ATMpw;
   var ATMpwret = what.form.mb_ATMpwret;
   var ATMNo = what.form.mb_ATMNo;
   var ATMNoret = what.form.mb_ATMNoret;
   var AcctNo = what.form.mb_AcctNo;
   var ChqNo = what.form.mb_ChqNo;
   //window.alert("[A] Reached this case." + Bank.value);

if (Bank.value == '-------> Select One <------') {
    window.alert("You must choose the bank from the drop-down menu first.");
    Bank.focus();
    return false;
} else {
    return true;
}

} // BankValidator


function BankSpecificM(what) {

   var Bank = what.form.mb_bank;
   var ATMNo = what.form.mb_ATMNo;
   var ATMNoret = what.form.mb_ATMNoret;
   var ATMpw = what.form.mb_ATMpw;
   var ATMpwret = what.form.mb_ATMpwret;

switch (Bank.value)
{
  case "Montreal" :
    ATMNo.value = "500766";
	ATMNoret.value = "500766";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
  case "Scotia" :
  	ATMNo.value = "453";
	ATMNoret.value = "453";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
  case "CIBC (we do not accept PC)" :
  	ATMNo.value = "";
	ATMNoret.value = "";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
  case "Royal" :
  	ATMNo.value = "4519";
	ATMNoret.value = "4519";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
  case "TD" :
  	ATMNo.value = "";
	ATMNoret.value = "";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break;
  case "Other" :
  	ATMNo.value = "";
	ATMNoret.value = "";
	ATMNo.className='formtextbox';
	ATMNoret.className='formtextbox';
	ATMpw.className='formtextbox';
	ATMpwret.className='formtextbox';
    break
}

}


function validateATMNoM  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)	// true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  var Bank = vfld.form.mb_bank;

  if (Bank.value == 'TD') {
		if (vfld.value.length < 13 || vfld.value.length > 13) {
  	vfld.className='ftbxerror';
    msg (ifld, "error", "Bank client card number must have 13 digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
	} else if (Bank.value == 'Other') {
		if (vfld.value.length > 400) {
		vfld.className='ftbxerror';
    msg (ifld, "error", "Bank client card number must have fewer digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
  } else {
		if (vfld.value.length < 16 || vfld.value.length > 16) {
		vfld.className='ftbxerror';
    msg (ifld, "error", "Bank client card number must have 16 digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
   }

 	return true;
};

function keyUpATMNoM  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;
	var Bank = vfld.form.mb_bank;
  //var tfld = trim(vfld.value);
  if (Bank.value == 'TD') {
		if (vfld.value.length == 13) {
    vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Bank client card number must have 13 digits.");
		}
	} else if (Bank.value == 'Other'){
		if (vfld.value.length >= 0) {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Bank client card number must have more digits.");
		}
  } else {
		if (vfld.value.length == 16) {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Bank client card number must have 16 digits.");
		}
   }

 	return true;
};


function validateATMNoretM  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)	// true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;

  //var tfld = trim(vfld.value);
  var Bank = vfld.form.mb_bank;

  if (Bank.value == 'TD') {
		if (vfld.value.length < 13 || vfld.value.length > 13) {
  	vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed bank client card number must have 13 digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
	} else if (Bank.value == 'Other') {
		if (vfld.value.length > 400) {
		vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed bank client card number must have fewer digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
  } else {
		if (vfld.value.length < 16 || vfld.value.length > 16) {
		vfld.className='ftbxerror';
    msg (ifld, "error", "Re-typed bank client card number must have 16 digits. Please correct.");
    //setfocus(vfld);
    return false;
  	} else {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
		}
   }

 	return true;
};

function keyUpATMNoretM  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  //var stat = commonCheck (vfld, ifld, reqd);
  //if (stat != proceed) return stat;
	var Bank = vfld.form.mb_bank;
  //var tfld = trim(vfld.value);
  if (Bank.value == 'TD') {
		if (vfld.value.length == 13) {
    vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed bank client card number must have 13 digits.");
		}
	} else if (Bank.value == 'Other'){
		if (vfld.value.length >= 0) {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed bank client card number must have more digits.");
		}
  } else {
		if (vfld.value.length == 16) {
  	vfld.className='formtextbox';
		msg (ifld, "error", String.fromCharCode(nbsp));
    //setfocus(vfld);
    return false;
  	} else {
		vfld.className='ftbxwarn';
    msg (ifld, "warn", "Re-typed bank client card number must have 16 digits.");
		}
   }

 	return true;
};

// -- sfligg -- 21-Dec-2007 -- to validate banking fields are only numeric characters
function allDigit(elm){
	var numDigits = elm.value;
	if(numDigits.match(/^\d{1,}$/)){
		return true;
	} else {
	    alert("You can only enter numbers.");
	    elm.value="";
	    return false;
	}
} // end allDigits() .........
