/******************************************************************************
 *                                                                            *
 *                    XSite PHP Content Management System                     *
 *                                                                            *
 *        Copyright (c) 2000-2002 by Robert Bala. All rights reserved         *
 *                                                                            *
 ******************************************************************************/


var lstChecks = new Array()
var lstErrors = new Array()

lstChecks['iso'] = new Dispatcher('isValid');
lstChecks['name'] = new Dispatcher('isValid');
lstChecks['email'] = new Dispatcher('isValid');
lstChecks['sid'] = new Dispatcher('isValid');
lstChecks['value'] = new Dispatcher('isValid');
lstChecks['title'] = new Dispatcher('isValid');
lstChecks['robots'] = new Dispatcher('isValid');
lstChecks['username'] = new Dispatcher('isValid');
lstChecks['password1'] = new Dispatcher('isValid');
lstChecks['password2'] = new Dispatcher('isValid');
lstChecks['first_name'] = new Dispatcher('isValid');
lstChecks['last_name'] = new Dispatcher('isValid');
lstChecks['ordering'] = new Dispatcher('isInteger');
lstChecks['description'] = new Dispatcher('isValid');
lstChecks['contents'] = new Dispatcher('isValid');
lstChecks['subject'] = new Dispatcher('isValid');
 
function Dispatcher(validate) {
  this.validate = eval(validate)
}
 
function isEmpty(inputVal) {
  if (inputVal == null || inputVal == "") {
      return true
  }
  return false
}

function isValid(inputVal) {
  if (isEmpty(inputVal)) {
    return false
  }
  return true
}

function isPosInteger(inputVal) {
  if (isEmpty(inputVal)) {
    return false
  }
  inputStr = inputVal.toString()
  for (var i = 0; i < inputStr.length; i++) {
    var oneChar = inputStr.charAt(i)
    if (oneChar < "0" || oneChar > "9") {
      return false
    }
  }
  return true
}

function isInteger(inputVal) {
  if (isEmpty(inputVal)) {
    return false
  }
  var inputStr = inputVal.toString()
  for (var i = 0; i < inputStr.length; i++) {
    var oneChar = inputStr.charAt(i)
    if (i == 0 && oneChar == "-") {
      continue
    }
    if (oneChar < "0" || oneChar > "9") {
      return false
    }
  }
  return true
}

function isNumber(inputVal) {
  var oneDecimal = false
  if (isEmpty(inputVal)) {
    return false
  }
  var inputStr = inputVal.toString()
  for (var i = 0; i < inputStr.length; i++) {
    var oneChar = inputStr.charAt(i)
    if (i == 0 && oneChar == "-") {
      continue
    }
    if (oneChar == "." && !oneDecimal) {
      oneDecimal = true
      continue
    }
    if (oneChar < "0" || oneChar > "9") {
      return false
    }
  }
  return true
}

function identName(inputVal) {
  var inputStr = inputVal.toString()
  if (!isEmpty(inputStr)) {
    var indexB = inputStr.indexOf(']')
    var indexA = inputStr.indexOf('[') + 1
    inputStr = inputStr.substring(indexA, indexB)
  }
  return inputStr
}

function checkForm(form) {
  var action = form.elements['form_action']
  if (action && ((action.value == 'cancel') || (action.value == 'delete'))) {
      return true
  }
  for (var i = 0; i < form.elements.length; i++) {
    var field = form.elements[i]
    var ident = identName(field.name)
    if (ident && lstChecks[ident]) {
      if (!lstChecks[ident].validate(field.value)) {
        if (lstErrors[ident]) {
          alert(lstErrors[ident])
        } else {
          alert("You didn't fill required field. Please try again.")
        }
        field.focus()
        return false
      }
    }
  }
  return true
}
