/*
TERRADON Form Field Validation Script
Copyright 2000 TERRADON Corporation
Original: June 15, 2000 by JAS, JAT
Last Modified for AutoWizard: December, 2001 by JAS
*/

//=================================================================================================================================
function Verify() {
  Errors = "";
  InValid = 0;

  regPARAM = new RegExp("\\(");

  NumFields = Form.length;

  for (var i = 0; i < NumFields; i++) {
    if (Form[i].REQUIRED) {
        eval("Require('" + Form[i].name + "')");
	}

    if (Form[i].VALIDATE){
      if ((Form[i].value.length != 0) || (Form[i].value != '') || (Form[i].value != 0)) {
        if (regPARAM.test(Form[i].VALIDATE)) {
          PARAMETER = ',' + Form[i].VALIDATE.substring(Form[i].VALIDATE.indexOf('(')+1, Form[i].VALIDATE.indexOf(')'))
          eval('Validate' + Form[i].VALIDATE.substring(0,Form[i].VALIDATE.indexOf('(')) + "('" + Form[i].name + "'" + PARAMETER + ")");
        } else {
          PARAMETER = "";
          eval('Validate' + Form[i].VALIDATE + "('" + Form[i].name + "')");
        }
      }
    }
  }

  if (InValid != 0) {
    if (InValid == 1)
      alert('The Form Validation has returned the following error in the field data:\n____________________________________\n' + Errors + '____________________________________\n\nPlease check this field and try again.');
    else
      alert('The Form Validation has returned the following errors in the field data:\n____________________________________\n' + Errors + '____________________________________\n\nPlease check these fields and try again.');

    return false;
  }

  else
    return true;
}

//=================================================================================================================================
function ValidateFail(field) {
  InValid++;

  if (Form[field].MESSAGE)
    Errors = Errors + '\n' + InValid + '. ' + Form[field].MESSAGE + "\n";
  else
    Errors = Errors + '\n' + InValid + '. ' + Form[field].name + ' is a required field or has been improperly completed.' + "\n";
}

//=================================================================================================================================
function Require(field) {
  if ((Form[field].selectedIndex) && (parseInt(Form[field].selectedIndex) == -1))
    ValidateFail(field);

  else if ((!Form[field].selectedIndex) && (Form[field].value.length == 0))
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateSelect(field) {
  if ((Form[field].selectedIndex < 0) || (Form[field].options(Form[field].selectedIndex).value.length == 0))
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateSingle(field) {
  if (!Form[field].checked)
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateUSPhone(field, HasArea) {
  if (HasArea == 'Yes')
    AcceptNumbers=10;
  else
    AcceptNumbers=7;

  //Check numbers
  End = Form[field].value.length;
  Numbers=0;

  for (var i = 0; i < End; i++) {
    if (parseInt(Form[field].value.charAt(i)) == Form[field].value.charAt(i))
      Numbers++;

    else if ((Form[field].value.charAt(i) != '.') && (Form[field].value.charAt(i) != ' ') && (Form[field].value.charAt(i) != '(') && (Form[field].value.charAt(i) != ')') && (Form[field].value.charAt(i) != '-'))
      ValidateFail(field);
    }

  if (Numbers != AcceptNumbers)
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateInteger(field) {
  if (parseInt(Form[field].value) != Form[field].value)
    ValidateFail(field);
}

//=================================================================================================================================
function ValidatePositiveInteger(field) {
  if (isNaN(parseInt(Form[field].value)) || parseInt(Form[field].value) < 0)
      ValidateFail(field);
}

//=================================================================================================================================
function ValidateNumber(field) {
  if (isNaN(Form[field].value))
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateNumberByLength(field, Length)
{
  if (Length) {
    AcceptLength = parseInt(Length);
  }
  if (Form[field].value.length > 0) {
      if (isNaN(Form[field].value) || Form[field].value.length < AcceptLength)
        ValidateFail(field);
  }
}
//=================================================================================================================================
function ValidateRange(field, Min, Max) {
  if (isNaN(Form[field].value))
    ValidateFail(field);
  else {
    if ((Form[field].value < Min) || (Form[field].value > Max))
      ValidateFail(field);
  }
}

//=================================================================================================================================
function ValidateUSZip(field, Type) {
  if (Type == "Long") 
    AcceptNumbers=9;

  else if (Type == "Short") 
    AcceptNumbers=5;

  else {
      if (Form[field].value.indexOf('-') >= 0) 
        AcceptNumbers=9;
      else
        AcceptNumbers=5;
    }

  //Check numbers
  End = Form[field].value.length;
  Numbers=0;

  for (var i = 0; i < End; i++) {
      if (parseInt(Form[field].value.charAt(i)) == Form[field].value.charAt(i))
        Numbers++;

      else if (Form[field].value.charAt(i) != '-')
         ValidateFail(field);
    }

  if (Numbers != AcceptNumbers)
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateIncludes(field, String) {
  var includePattern = new RegExp(String);
  
  if (!includePattern.test(Form[field].value)) 
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateExcludes(field, String) {
  var excludePattern = new RegExp(String);
  
  if (excludePattern.test(Form[field].value)) 
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateURL(field) {
  var addressPattern = new RegExp("ftp://|http://|javascript:|file://|gopher://|https://|mailto:|rlogin://|shttp://|snews://|telnet://|tn3270://|wais://");

  if (!addressPattern.test(Form[field].value))
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateEmail(field) {
  var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;

  if (!addressPattern.test(Form[field].value))
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateImage(field) {  
  if (Form[field].value.length != 0) {
    if ((Form[field].value.toLowercase().indexOf('.jpg') < 0) && (Form[field].value.toLowercase().indexOf('.gif') < 0))
      ValidateFail(field);
  }
}

//=================================================================================================================================
function ValidateLength(field, Min, Max) {  
  if ((Form[field].value.length < Min) || (Form[field].value.length > Max))
    ValidateFail(field);
}

//=================================================================================================================================
function ValidateComboFields(field1, field2) {  
  if ((Form[field1].value.length == 0) && (Form[field2].value.length == 0))
    ValidateFail(field1);
}

//=================================================================================================================================
function ValidateSequence(field1, field2) {  
  if ((Form[field2].value.length == 0) && (Form[field1].value.length != 0))
    ValidateFail(field1);
}
//=================================================================================================================================
function ValidateMatch(field1, field2) {  
  if (Form[field2].value != Form[field1].value)
    ValidateFail(field1);
}

//=================================================================================================================================
//Left in for Existing Apps which use this
function ValidateCondition(field, Expression) {
  if (eval(Expression))
    ValidateFail(field);
}

//=================================================================================================================================
//Left in for Existing Apps which use this
function ValidateDateCombo(field) {

  jsDate = new Date(Form[field].value)

  eval("fldMonth = parseInt(Form.system_month_" + field + ".value)-1");
  eval("fldDay = parseInt(Form.system_day_" + field + ".value)");

  if ((fldMonth != jsDate.getMonth()) || (fldDay != jsDate.getDate()))
    ValidateFail(field);
}

