// formstuff.js
//
// Created: 2006-06-23
// Author: Bob Barnes
//
// Description: Various javascript functions useful for forms 


// Used to remember which field has been previously highlighted, so that the
// highlight can be turned off
var PreviousInputHighlighted;
var PreviousColor = '#000000';


// Call this to highlight a field when it has a problem
// Input: theInput is the field to be highlighteds
function Highlight( theInput )
{
  theInput.focus();
  PreviousColor = theInput.style.backgroundColor;
  PreviousInputHighlighted = theInput;
  theInput.style.backgroundColor = 'yellow';
}


// If there is already a highlighted field, remove the highlight
function HighlightOff()
{

  if ( PreviousColor != '#000000' )
  {
    PreviousInputHighlighted.style.backgroundColor = PreviousColor;
  }
}


// Check a given field for presence, min and max length
// Input: theField    - the field that should be checked
//        strFriendlyName - the user-friendly name to use when refering to the field
//        blMandatory - true if the field is mandatory
//        intLengthMin - the minimum acceptable length of this field
//        intLengthMax - the maximum acceptable length of this field
 
function FieldCheck( theField, strFriendlyName, blMandatory, intLengthMin, intLengthMax )
{
  if ( ( blMandatory == true ) && (theField.value == "") )
  {
    Highlight( theField );
    alert("Please enter a value for the " + strFriendlyName + " field");
    return (false);
  }

  if ( ( theField.value.length > 0 ) && ( theField.value.length < intLengthMin ) )
  {
    Highlight( theField );
    strAlert = "Please enter at least " + intLengthMin + " characters in the " + strFriendlyName + " field";
    if ( blMandatory == false )
    {
      strAlert += ", or leave it blank";
    }
    alert( strAlert );
    return (false);
  }

  if ( theField.value.length > intLengthMax )
  {
    Highlight( theField );
    alert("Please enter at most " + intLengthMax + " characters in the " + strFriendlyName + " field");
    return (false);
  }

  return( true );
}


// Check the personal info fields on a form to make sure they are 
// good enough for the form to be submitted
// Input: theForm is the form to be validated
// Output: returns true if the form's contents are valid
//        issues an alert if there is a problem


function PersInfoValidate( theForm )
{
  if (theForm.Title.selectedIndex < 0)
  {
    alert("Please select one of the \"Title\" options.");
    Highlight( theForm.Title );
    return (false);
  }

  if ( FieldCheck( theForm.FirstName, "first name", true, 2, 50 ) == false )
  {
    return( false );
  }
  
  if ( FieldCheck( theForm.LastName, "last name", true, 2, 50 ) == false )
  {
    return( false );
  }
  
  if ( FieldCheck( theForm.Address1, "first address", true, 5, 35 ) == false )
  {
    return( false );
  }
  
  if ( FieldCheck( theForm.Address2, "second address", false, 5, 35 ) == false )
  {
    return( false );
  }
 
  PostCode = checkPostCode( theForm.Postcode.value );
  if ( PostCode == false )
  {
	alert( "Please supply a valid UK postcode" );
        Highlight(theForm.Postcode);
	return( false );	
  }
  else
  {
    theForm.Postcode.value = PostCode;
  }
  
  if ( FieldCheck( theForm.Tel, "telephone number", true, 5, 14 ) == false )
  {
    return( false );
  }
  
  if ( theForm.Email.value.length > 0 )
  {
    if ( !emailCheck( theForm.Email.value ) )
    {
    alert("The email address you have entered doesn't seem right.");
    Highlight(theForm.Email);
    return (false);
    }
  }
  
return( true );
}



