// Quick and Dirty Validation
// Copyright 2008 Integrative Success
// Coded by: JC Miller


var FormName = 'frmClassReg';
var SubmitButton = 'btnSubmit';

function LoseMyFocus( me ) {
//Move focus to proper field 
  var szName = 'txt' + me.name.substring(3, me.name.length);
  document[FormName][szName].focus();
  return 1;
}

function ValidateField( me, szFieldCode ) {
  /* Field Code Guide
      <R,N,I><A,N><#><#>
     example: ValidateField(this,'R,N,5,5');
     
     R: Required, N: Not required but validate anyway, I: Ignore
     A: Alphanumaric characters only, N: Numaric characters only
     #: minimum field length (only checks on required)
     #: maximum field length (live truncation of invalid lengths)

     Note:
     This script assumes all of your input text boxes will be named with
     a lower-case 'txt' preceding the name. ex: txtMyBox, txtAddress, txtName.
     Another input field is required to have a 'lbl' for all normal
     fields you wish to code as (R) Required. The 'lbl' field does not
     have to be visable but must be there. ex: lblMyBox, lblAddress, lblName.

*/

  var nTestCode = 1;
  var aFieldCode = new Array(4);
  aFieldCode = szFieldCode.split(',');

  if (aFieldCode[0] == 'I') {
    CheckFormState;
    return true; 
  };

  // Set valid character set
  if (aFieldCode[1] == 'N') {
    var ValidSet = "1234567890";
    var Replacement = "";
  } else {
    var ValidSet = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,.?!@#$%^&*()-_=+";
    var Replacement = "?";
  }

  // Check max length, trunc if needed
  if (parseInt(aFieldCode[3]) < me.value.length) {
    me.value = me.value.substring(0,parseInt(aFieldCode[3]));
  }

  // Check characters in value against ValidSet
  for (var i = 0; i < me.value.length; i++) {
    if (ValidSet.indexOf(me.value.charAt(i)) < 0) {
      var szTemp = me.value
      me.value = szTemp.substring(0, i) + Replacement + szTemp.substring(i + 1, szTemp.length);
    }
  }

  if (aFieldCode[0] == 'R') {
    // Check min length
    if (me.value.length < parseInt(aFieldCode[2])) {
      var szTemp = "lbl" + me.name.substring(3,me.name.length);
      document[FormName][szTemp].value = "Required";
    } else {
      var szTemp = "lbl" + me.name.substring(3,me.name.length);
      document[FormName][szTemp].value = "Accepted";
    }
  }
  
  CheckFormState();
    
  return 1;
}

function CheckFormState() {
  var nBool = 1;
  for (var i = 0; i < document[FormName].elements.length; i++) {
    if (document[FormName].elements[i].name.substring(0,3) == "lbl") {
      if (document[FormName].elements[i].value != 'Accepted') { nBool = 0 };
    }
  }
  if (nBool) { document[FormName][SubmitButton].disabled = false;
  } else { document[FormName][SubmitButton].disabled = true;  }
  return 1;
}

function ResetForm() {
  for (var i = 0; i < document[FormName].elements.length; i++) {
    if (document[FormName].elements[i].name.substring(0,3) == "lbl") {
      document[FormName].elements[i].value = 'Required';
    }
  }
  document[FormName][SubmitButton].disabled = true;

  return 1; 
}

function SubmitForm() {
  document[FormName].submit();
  return 1;
}