
/*
// Validates form input
function validate(inputs){
  var fields = inputs.split(", ")
  for(input in fields) {
    if(!(eval("document.forms[0]." + fields[input] +".value"))){
      window.alert("You must enter a value for " + 
        fields[input].replace("_", " ") + 
        ".");
      return false
    }
  }
}
*/

/*
------------------------------------------------------------------------------
Validate user input v1.1 October 2004
------------------------------------------------------------------------------
Validates user form input.

NOTES:
1) Forms NAME attribute should be set to a plain english name that will be 
   displayed to user in alert if validation fails. Spaces should be replaced 
   with underscores ("_").
   
   e.g. "Street_Address"
   
2) inputs: takes a semi-colon delimited list of input NAME attribute and 
   expression type constant pairs. These pair need to be comma-delimited. 
   Commas and semi-colons require a trailing space.
   
   e.g. validate('Name; Date, date; Time, time; Email, email')
   
3) The following expression type constants are valid and must be entered 
   exactly as listed (i.e. all lower-case):
   
   date: Date format DD/MM/YYYY
   number: Numbers only
   nonzero: Number not equal to 0
   time: 24 hour time format HH:MM
   email: Email format
   
   If no constant is supplied then the default expression is used which will
   test that the field is not empty.
*/

function validate(inputs){
  // Declare all variables
  var inputs, input, name, value, expression, message
  
  // Get list of input elements to validate and regular expressions to 
  // validate them against and convert to array
  inputs = inputs.split("; ")
  
  // Loop through input elements
  for(input in inputs) {
    // Split into array of input element and expression
    input = inputs[input].split(", ")
    
    // Get the input element's plain english name
    name = input[0].replace(/_/g, " ")
    
    // Get value entered by user
    value = document.getElementsByName(input[0])[0].value
    
    // Select correct regular expression to validate against
    switch(input[1]){
      case "date":
        expression = /^(?:0{0,1}[1-9]|[12][0-9]|3[01])\/(?:0{0,1}[1-9]|1[012])\/(19|20)[0-9][0-9]$/ // Date
        message = "You must enter the date in the form DD/MM/YYYY for the " + name + " field."
        break
      case "monthyear":
        expression = /^(?:0{0,1}[1-9]|1[012])\/(19|20)[0-9][0-9]$/ // Date
        message = "You must enter the date in the form MM/YYYY for the " + name + " field."
        break
      case "email":
        expression = /^[A-Za-z0-9_\-.]{1,}@([A-Za-z0-9_\-.]{1,}.){0,}[A-Za-z0-9_]{1,}.[A-Za-z]{2,4}$/ // Email
        message = "You must enter valid email address for the " + name + " field."
        break
      case "number":
        expression = /^[0-9]{1,}(?:.[0-9]{0,}){0,}$/
        message = "You must enter a number for the " + name + " field."
        break
      case "nonzero":
        expression = /^[1-9]{1}|[1-9]{1}[0-9]{1,}$/
        message = "You must enter a number greater than zero for the " + name + " field."
        break
      case "time":
        expression = /^(?:0{0,1}[0-9]|1[0-9]|2[0-4]):[0-5][0-9]$/ // Time
        message = "You must enter the time in 24 hour format HH:MM for the " + name + " field."
        break
      default:
        expression = /^[A-Za-z0-9_\-.,\/\\:\(\)\n& ]{1,}$/ // Something must be entered. 
                                               // Can't be blank.
        message = "You must enter a value for " + name + "."
    }
    
    // Debugging info
    //window.alert("Name: " + name)
    //window.alert("Value: " + value)
    //window.alert("Expression: " + expression)
    //window.alert(expression.test(value))
    // Validate and return error message if false
    if(!expression.test(value)){
      // Return error message
      window.alert(message)
      
      // Place cursor in offending field
      document.getElementsByName(input[0])[0].focus()
      
      // Return false to prevent form submission from taking place
      return false
    }    
  }
}

// Set's hidden ID and submits form when a delete link is clicked
function submitDelete(value){
  // Set hidden id
  document.forms[0].hdnId.value = value
  
  // Submit form
  document.forms[0].submit()
}

// Set hidden value to same as selection
function syncValue(name){
  // Set hidden value
  eval("document.forms[0]." + name + 
      "Value.value = document.forms[0]." + name + "[document.forms[0]." +
      name + ".selectedIndex].text")
}

// Set hidden value to same as selection for radio button group
function syncRadio(name){
  // Loop through options to see which is selected
  eval("input = document.forms[0]." + name)
  for(i=0;i<input.length;i++){
    if(input[i].checked){
      index = i
    }
  }
  
  // Set hidden value
  eval("document.forms[0]." + name + "Value.value = input[index].alt")
}