
function validate () {
  /* check for empty or unselected field */
  switch (validate.arguments[2]) {
    case ('empty'):  /* check for empty textbox, i.e. validate(0,'txtName','empty','Please enter a name') */
      if (document.forms[validate.arguments[0]].elements[validate.arguments[1]].value == '') {
        alert(validate.arguments[3]);
        document.forms[validate.arguments[0]].elements[validate.arguments[1]].focus();
        return true;
      }
      break;
    case ('numeric'):  /* check for numeric, i.e. validate('frmName','txtTel','numeric','Please enter numbers only. Avoid slashes & dashes.') */
      if (isNaN (document.forms[validate.arguments[0]].elements[validate.arguments[1]].value) == true) {
        alert(validate.arguments[3]);
        document.forms[validate.arguments[0]].elements[validate.arguments[1]].focus();
        return true;
      }
      break;
    case ('telephone'): /* check for valid telephone characters [0-9, ()-]. i.e. validate(1,'txtTel','telephone','Please enter a valid telephone number') */
      var validNum = "0123456789 ()-";
      a = new String(document.forms[validate.arguments[0]].elements[validate.arguments[1]].value);
      for(i=0; i<a.length;i++){
        if(validNum.indexOf(a.charAt(i)) < 0){
          alert(validate.arguments[3]);
          document.forms[validate.arguments[0]].elements[validate.arguments[1]].focus();
          return true;
        }
      }
      break;
    case ('comma'): /* check for valid numerals [0-9 ,&. ]. i.e. validate(0,'txtPayment','comma','Please enter a valid amount') */
      var validNum = " 0123456789,.";
      a = new String(document.forms[validate.arguments[0]].elements[validate.arguments[1]].value);
      for(i=0; i<a.length;i++){
        if(validNum.indexOf(a.charAt(i)) < 0){
          alert(validate.arguments[3]);
          document.forms[validate.arguments[0]].elements[validate.arguments[1]].focus();
          return true;
        }
      }
      break;
    case ('email'):    /* check for valid email, i.e. validate(1,'txtEMail','email','Please enter a valid email address') */
      /* assumes valid email has '@' from 2nd char onwards     */
      /* assumes valid email has '.' from 4th char onwards     */
      /* assumes valid email has at least 2 char after the '.' */
      if (validate.arguments[2] == 'email') {
        var a, i;
        a = document.forms[validate.arguments[0]].elements[validate.arguments[1]].value;
        i = a.indexOf('.') + 3;
        if(a.indexOf('@') < 1 || a.indexOf('.') < 3 || a.length < i) {
          alert(validate.arguments[3]);
          document.forms[validate.arguments[0]].elements[validate.arguments[1]].focus();
          return true;
        }
      }
      break;
    case ('unselected1'):    /* check unselected drop downs, i.e. validate(0,'selCountry','unselected1','Please select a country') */
      if (document.forms[validate.arguments[0]].elements[validate.arguments[1]].selectedIndex == 0) {
        alert(validate.arguments[3]);
        document.forms[validate.arguments[0]].elements[validate.arguments[1]].focus();
        return true;
      }
      break;
    case ('unselected2'):    /* check unselected radio buttons, i.e. validate(0,'rdoTel','unselected2','Please select type of contact number',4) where 4 is the total rdo */
      var j = false;
      for (i=0; i<validate.arguments[4]; i++) {
        if (document.forms[validate.arguments[0]].elements[validate.arguments[1]][i].checked == true){j = true;}
      }
      if (j == false){
        alert(validate.arguments[3]);
        document.forms[validate.arguments[0]].elements[validate.arguments[1]][0].focus();
        return true;
      }
      break;
    case ('unselected3'):    /* check unselected checkboxes, i.e. validate(0,'chkService','unselected3','Please enter or select type of service',12) where 12 is the total chk */
      var j = false;
      var strArgument, k;
      for (i=0; i<validate.arguments[4]; i++) {
        k = i+1;
        strArgument = validate.arguments[1] + k;
        if (document.forms[validate.arguments[0]].elements[strArgument].checked == true){j = true;}
      }
      if (j == false){
        alert(validate.arguments[3]);
        strArgument = validate.arguments[1] + 1;
        document.forms[validate.arguments[0]].elements[strArgument].focus();
        return true;
      }
      break;
    case ('range'):    /* check if value is between a range, i.e. validate('frmName','txtYear','range','Please enter valid year',1800,2100) where 1800 is the min and 2100 is max */
      /* minimum and maximum value must be present                 */
      /* minimum and maximum value = 0 if unchecked                */
      /* minimum value must preceed maximum value in the arguments */
      if ((validate.arguments[4] > 0 && document.forms[validate.arguments[0]].elements[validate.arguments[1]].value < validate.arguments[4]) ||
          (validate.arguments[5] > 0 && document.forms[validate.arguments[0]].elements[validate.arguments[1]].value > validate.arguments[5])) {
        alert(validate.arguments[3]);
        document.forms[validate.arguments[0]].elements[validate.arguments[1]].focus();
        return true;
      }
      break;
    case ('length'):  /* check the length of textbox value, i.e. validate('frmName','txtName','length','Tel number must be at least 7 digits',7) */
      var a = document.forms[validate.arguments[0]].elements[validate.arguments[1]].value;
      if(a.length < validate.arguments[4]){
        alert(validate.arguments[3]);
        document.forms[validate.arguments[0]].elements[validate.arguments[1]].focus();
        return true;
      }
      break;
    case ('maxlength'):  /* check if length of textbox value is less than max, i.e. validate('frmName','txtName','maxlength','message must be 100 characters or less',100) */
      var a = document.forms[validate.arguments[0]].elements[validate.arguments[1]].value;
      if(a.length > validate.arguments[4]){
        alert(validate.arguments[3]);
        document.forms[validate.arguments[0]].elements[validate.arguments[1]].focus();
        return true;
      }
      break;
    case ('image'):  /* check if file is JPEG or GIF, i.e. validate('frmName','txtImage','image','Please select an image file') */
      var a = new String(document.forms[validate.arguments[0]].elements[validate.arguments[1]].value);
	  var ext = new String(a.substring(a.length-3, a.length))
	  if(ext != 'jpg' && ext != 'jpe' && ext != 'gif'){
          alert(validate.arguments[3]);
          document.forms[validate.arguments[0]].elements[validate.arguments[1]].focus();
          return true;
	  }
      break;
    }
  }

  function MM_jumpMenu(targ,selObj,restore){ //v3.0
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
  }

  //format numbers from xxx.x into xx,xxx.xx
  function formatMoney(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    cents = Math.floor((num*100+0.5)%100);
    num = Math.floor((num*100+0.5)/100).toString();
    if(cents < 10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
    return (num + '.' + cents);
  }

  //format numbers from xx,xxx.xx into xxxxx.x
  function unformatMoney(str){
    re = /^\$|,/g;
    return str.replace(re, "");
  }

