//**************************************************
// sIMPLE fORM cHECK v1.0
// written by Chetan M. Soni (11/10/2000)
//**************************************************

function addToList(theString, errorString) {
  beginSlice = 0;
  endSlice = theString.indexOf('_');
  theString = theString.slice(beginSlice, endSlice);
  if (errorString.indexOf(theString) == -1) {
    errorString = errorString + '<li>' + theString + '</li>';
  }
  return errorString;
}

function isEmailValid(theEmail) {
  emailLength = theEmail.length;
  atPos = theEmail.indexOf("@");
  dotPos = theEmail.indexOf(".", atPos);
  if (atPos > 0) {
    if (dotPos != -1) {
      if (dotPos > eval(atPos + 1)) {
        if (dotPos < eval(emailLength - 1)) {
          return true;
        }
      }
    }
  }
  return false;
}

function showErrors(errorString) {
  beginString = '<html><head><title>Form: Invalid Fields</title></head><body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" bgcolor="#FFFFFF" link="blue" alink="red" vlink="blue""><table border="0" cellpadding="5" cellspacing="0" width="100%" height="100%"><tr valign="top" bgcolor="#FFFFCC"><td height="10%" colspan="2"><span style="font-family:verdana; font-size:14px;"><b>The following fields were invalid:</b></span></td></tr><tr valign="top"><td height="80%" width="15%"><span style="font-family:verdana; font-size:12px;">&nbsp;</span></td><td height="80%" width="80%"><span style="font-family:verdana; font-size:12px;"><ul>'
  endString = '</ul></span></td></tr><tr valign="bottom" bgcolor="#FFFFCC"><td height="10%" colspan="2" align="right"><a href="javascript:top.self.close();"><span style="font-family:verdana; font-size:12px; text-decoration:none;"><b>close window</b></span></a></td></tr></table></body></html>'
  if ((window.errorsWindow) && (!window.errorsWindow.closed)) {
    window.errorsWindow.close();
  }
  errorsWindow = window.open('','errorsWindow','resizable=no,width=300,height=225');
  window.errorsWindow.top.document.open()
  window.errorsWindow.top.document.write(beginString + errorString + endString);
  window.errorsWindow.top.document.close()
}

function formChecker(theForm) {
  errorString = '';
  for (numOfField = 0; numOfField < theForm.elements.length; numOfField++) {
    if ((theForm.elements[numOfField].type == 'text') || (theForm.elements[numOfField].type == 'password')) {
      if (theForm.elements[numOfField].name.indexOf('_req') != -1) {
        if (theForm.elements[numOfField].value.length <= 0) {
          errorString = addToList(theForm.elements[numOfField].name, errorString);
        }
        else {
          if (theForm.elements[numOfField].name.indexOf('_length') != -1) {
            beginSlice = theForm.elements[numOfField].name.indexOf('_length') + 7;
            endSlice = theForm.elements[numOfField].name.indexOf('_length') + 9;
            whatLength = eval(theForm.elements[numOfField].name.slice(beginSlice, endSlice));
            if (theForm.elements[numOfField].value.length < whatLength) {
              errorString = addToList(theForm.elements[numOfField].name, errorString);
            }
          }
          if (theForm.elements[numOfField].name.indexOf('_numeric') != -1) {
            if (isNaN(theForm.elements[numOfField].value)) {
              errorString = addToList(theForm.elements[numOfField].name, errorString);
            }
          }
          if (theForm.elements[numOfField].name.indexOf('_email') != -1) {
            if (!isEmailValid(theForm.elements[numOfField].value)) {
                errorString = addToList(theForm.elements[numOfField].name, errorString);
            }
          }
        }
      }
    }
    if ((theForm.elements[numOfField].type == 'checkbox') && (theForm.elements[numOfField].name.indexOf('_req') != -1)) {
      theCheckBoxObj = eval('document.' + theForm.name + '.' + theForm.elements[numOfField].name);
      if (theCheckBoxObj.length) {  // multiple checkboxes (array)
        beginSlice = theCheckBoxObj[0].name.indexOf('_req') + 4;
        endSlice = theCheckBoxObj[0].name.indexOf('_req') + 6;
        howManyReq = eval(theCheckBoxObj[0].name.slice(beginSlice, endSlice));
        howManyChecked = 0;
        for (x = 0; x < theCheckBoxObj.length; x++) {
          if (theCheckBoxObj[x].checked) {
            howManyChecked++;
          }
        }
        if (howManyChecked < howManyReq) {
          errorString = addToList(theForm.elements[numOfField].name, errorString);
        }
        numOfField = numOfField + theCheckBoxObj.length - 1;
      }
      else {  // single checkbox (not array)
        if (!theForm.elements[numOfField].checked) {
          errorString = addToList(theForm.elements[numOfField].name, errorString);
        }
      }
    }
    if ((theForm.elements[numOfField].type == 'select-one') && (theForm.elements[numOfField].name.indexOf('_req') != -1)) {
      selectedValue = theForm.elements[numOfField].options[theForm.elements[numOfField].selectedIndex].value;
      if (selectedValue == 0) {
        errorString = addToList(theForm.elements[numOfField].name, errorString);
      }
    }
  }
  if (errorString != '') {
    showErrors(errorString);
    return false;
  }
  else {
    if ((window.errorsWindow) && (!window.errorsWindow.closed)) {
      window.errorsWindow.close();
    }
    return true
  }
}
