/*
 form_validation.js - version 2.0
*/

var inputboxes        = Array('firstname', 'lastname', 'company', 'address1', 'town', 'county', 'country', 'email_address');
var radioboxes        = Array();
var checkboxes        = Array('privacy');
var default_message   = 'Please fill out all of the highlighted fields'; 
var validation_colour = '#dcdcdc'; // The 'required' colour.
var form_id_name      = 'form1'; // The name and id of the form in question.

/* Global defaults */
var continue_submit   = true;
var alerted           = false;
var message;

/*
 This is the method called via onClick in the submit button;
*/
function requiredValidation()
{
 // Reset global vars for each click (submit).
 alerted         = false; 
 continue_submit = true;  
 message         = default_message;

 // Check radiobox, inputbox and checkbox arrays respectively.
 radioboxes.each(function(e){return check_radiobox(e)});
 inputboxes.each(function(e){return check_inputbox(e)});
 checkboxes.each(function(e){return check_checkboxes(e)});

 if( !continue_submit ) alert(message);

 return continue_submit;
}


/* 
 Checks the inputboxes array to see if they have a length at all (not empty).
*/
function check_inputbox(input_id)
{
 var inputbox = $(input_id);
 if( inputbox ){
  if( inputbox.value && inputbox.value.length != 0 ){
   inputbox.style.backgroundColor = '';
   return true;
  }
  else {
   inputbox.style.backgroundColor = validation_colour;
   continue_submit = false;
   return false;
  }
 }
 alert('Could not find input field with id = \'' + input_id + '\'');
 continue_submit = false;
 return false;
}


/* 
 Checks the radioboxes array to see if one of them is ticked/checked.
*/
function check_radiobox(radio_name)
{
 var checked = Form.getInputs(form_id_name,'radio',radio_name).find(function(e) { return e.checked; });
 if( checked ) return true;
 message         = 'Please select from the \'' + radio_name + '\' list';
 continue_submit = false;
 Form.getInputs(form_id_name,'radio',radio_name).each(function(e){ message = get_message(e, message) });
 return false;
}


/* 
 Checks the checkboxes array to see if it has been ticked.
 Works only for single chekbox items (like privacy stements).
 Will need something different to check other stuff (e.g. how many have been checked).
*/
function check_checkboxes(checkbox_id)
{
 var checkbox = $(checkbox_id);
 if( checkbox.checked )
  return true;

 message = 'Please select at least one of \'' + checkbox_id + '\'';
 message = get_message(checkbox, message);

 continue_submit = false;
 return false;
}


/*
 Retrieves the 'message' attribute from an HTMLDOM object.
 This is non standard, and supported differently on different browsers.
*/
function get_message(fieldob, default_mess)
{
 var bespoke_message;
 bespoke_message = fieldob.message               ? fieldob.message                     : default_mess;
 bespoke_message = fieldob.attributes['message'] ? fieldob.attributes['message'].value : bespoke_message;
 return bespoke_message;
}

