formArray = new Array();

function popupWindow (theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
  return false;
}

function formValidationBuild (stringTitle, stringName, stringType, stringErrorMsg) {

  formArray[stringTitle] = new Array();
  formArray[stringTitle]['stringName'] = stringName;
  formArray[stringTitle]['stringType'] = stringType;
  if (stringType == 'changedtext') {
    formArray[stringTitle]['stringInitialValue'] = document.form.elements[stringTitle].value;
  }
  formArray[stringTitle]['stringErrorMsg'] = stringErrorMsg;

}


function formValidationCheck (currentString) {

  // If called with an argument, this function is being used to validate
  // an individual field

  if (currentString) {

    var stringTitle = '';
    for (x in formArray) {
      if (x == currentString) {
        stringTitle = x;
      }
    }

    if (!stringTitle) {
      alert ('String title "' + currentString + '" not found');
    } else {
      var stringName = formArray[stringTitle]['stringName'];
      var stringType = formArray[stringTitle]['stringType'];
      var stringErrorMsg = formArray[stringTitle]['stringErrorMsg'];
      if (stringType == 'changedtext') {
        var stringInitialValue = formArray[stringTitle]['stringInitialValue'];
      } else {
        var stringInitialValue = '';
      }
    }

    var stringError = '';
    stringError = formValidateTestValue(stringTitle, stringName, stringType, stringErrorMsg, stringInitialValue);

    // Otherwise, it's being used to validate the entire form

  } else {

    var stringTitle = '';
    var stringName = '';
    var stringType = '';
    var stringErrorMsg = '';
    var stringError = '';

    checkFormValues: for (stringTitle in formArray) {

    stringName = formArray[stringTitle]['stringName'];
    stringType = formArray[stringTitle]['stringType'];
    stringErrorMsg = formArray[stringTitle]['stringErrorMsg'];
    if (stringType == 'changedtext') {
      var stringInitialValue = formArray[stringTitle]['stringInitialValue'];
    } else {
      var stringInitialValue = '';
    }

    stringError = formValidateTestValue(stringTitle, stringName, stringType, stringErrorMsg, stringInitialValue);

    if (stringError) {
      break checkFormValues;
    }

    }

    }

    var stringTitleMod = ' ' + stringTitle + ',';
    var stringTitleRegExp = new RegExp(stringTitleMod);
    if (stringError) {

    alert(stringError);

    // New formError handling here, creates a CSV inside the variable itself
    if (!(document.form.formError.value.match(stringTitleRegExp))) {
      document.form.formError.value = document.form.formError.value + stringTitleMod;
    }
    document.form.elements[stringTitle].focus();

  } else {

    // Remove the value from the CSV

    document.form.formError.value = document.form.formError.value.replace(stringTitleRegExp,'');

  }

}

function formValidateTestValue(stringTitle,stringName,stringType,stringErrorMsg,stringInitialValue) {

  var stringError;
  
  if (stringTitle == 'Phone') {
	  stringValue = document.form.elements[stringTitle].value;
	  if (stringValue == '123456') {
      stringError = 'Spam Bot Detected';
    }

  }else if (stringType == 'text') {
    stringValue = document.form.elements[stringTitle].value;
    if (!stringValue) {
      stringError = 'Field "' + stringName + '" is mandatory';
    }
  } else if (stringType == 'changedtext') {
    stringValue = document.form.elements[stringTitle].value;
    if (!stringValue || (stringValue == stringInitialValue)) {
      stringError = 'Field "' + stringName + '" is mandatory';
    }
  } else if (stringType == 'email') {
    stringValue = document.form.elements[stringTitle].value;
    if (!(stringValue.match(/^[A-Za-z0-9_\.]+@[A-Za-z0-9_\.]+\.[A-Za-z]+$/))) {
      stringError = 'Field "' + stringName + '" must contain a valid email address';
    }
  } else if (stringType == 'phone') {
    stringValue = document.form.elements[stringTitle].value;
    if (!(stringValue.match(/^[0-9][0-9- \.]*/))) {
      stringError = 'Field "' + stringName + '" must contain a valid telephone number with area code';
    }
  } else if (stringType == 'userid') {
    stringValue = document.form.elements[stringTitle].value;
    if (!(stringValue.match(/^[A-Za-z0-9_\.]*/)) ||
         (stringValue.len > 20) || (stringValue.len < 4)) {
      stringError = 'Field "' + stringName + '" must contain a valid username between 4 and 20 characters';
    }
  } else if (stringType == 'select') {
    stringValue = document.form.elements[stringTitle].selectedIndex;
    if (stringValue == 0) {
      stringError = 'Field "' + stringName + '" must have a value selected';
    }
  } else if (stringType == 'checked') {
    stringValue = document.form.elements[stringTitle].checked;
    if (stringValue == 0) {
      stringError = 'Checkbox "' + stringName + '" must be checked off';
    }
  } else {
    alert ('String type for string title "' + currentString + '" not found');
  }

  if (stringError) {
    if (stringErrorMsg) {
      return stringErrorMsg;
    } else {
      return stringError;
    }
  } else {
    return '';
  }

}

function validateSubmit() {

  // Check form values in order to proceed

  if (isArray(formArray)) {
    formValidationCheck('');
  } else {
    document.form.formError.value = '';
  }

  if (!document.form.formError.value) {
    document.form.submitType.value = 'Submit';
    document.form.submit();
  } else {
  //  alert ('formError is ' + document.form.formError.value + '!');
  }

}

function pagePrevious() {

  // Check form values in order to proceed

  if (formArray) {
    formValidationCheck('');
  } else {
    document.form.formError.value = '';
  }

  if (!document.form.formError.value) {
    document.form.step.value = Number(document.form.step.value) - Number(1);
    document.form.submit();
  }

}

function toggleFormValue(formField, text) {

  textMod = " " + text + ",";
  textRegExp = new RegExp(textMod);
  isSet = document.form.elements[formField].value.match(textRegExp);

  if (isSet) {

    document.form.elements[formField].value = document.form.elements[formField].value.replace(textRegExp,'');

  } else {

    if (!(document.form.elements[formField].value.match(textRegExp))) {
      document.form.elements[formField].value = document.form.elements[formField].value + textMod;
    }
  }
}

function setFormPassword(formField, passwordField1, passwordField2) {

  password1 = document.form.elements[passwordField1].value;
  password2 = document.form.elements[passwordField2].value;
  if ((password1) && (password1 == password2)) {
    document.form.elements[formField].value = password1;
    formValidationCheck(formField);
  } else {
    document.form.elements[formField].value = '';
  }

}

function formConfirmDelete() {
  confirm('Once you delete this file, it cannot be recovered.  Are you sure you wish to proceed?');
  document.form.actionDelete.value = 'true';
  document.form.submit();
}

function isArray(obj) {
  if (obj.constructor.toString().indexOf("Array") == -1)
    return false;
  else
    return true;
}

