function validateForm(theForm)
{
  var result = false;
  var reasons = new Array();
  var message = "";
  var focusItem = null;

  reasons[0] = validateContactFirstName(theForm.contactFirstName);
  reasons[1] = validateContactLastName(theForm.contactLastName);
  reasons[2] = validateDaytimePhone(theForm.daytimePhone);
  reasons[3] = validateEmail(theForm.email);
  reasons[4] = validateInjuredFirstName(theForm.injuredFirstName);
  reasons[5] = validateInjuredLastName(theForm.injuredLastName);
  reasons[6] = validateQ01(theForm.Q01);
  reasons[7] = validateQ02(theForm.Q02);
  reasons[8] = validateQ03(theForm.Q03);
  reasons[9] = validateCheckBox();
  reasons[10] = validateSecurity(theForm.security);
  
  for (var i = 0; i < reasons.length; i++)
  {
                  var reason = reasons[i];
                  if(reason[0] != true)
                  {
                                  message += reason[1];
                                  if (focusItem == null)
                                  {
                                                  focusItem = i;
                                  }
                  }
  }
 
  if (message != "")
  {
      alert("Please correct the following to continue:\n" + message);
      focusControl(focusItem);
  }
  else
  {
                result = true;
  }

  return result;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateContactFirstName(fld)
{
    var result = new Array();
  
    if (fld.value.length == 0)
	{
        fld.style.background = '#FFF568'; 
		result[0] = false;
        result[1] = " - Enter your first name.\n";
		
    }
	else
	{
        fld.style.background = '#FFFFFF';
		result[0] = true;
    }
    return result;   
}

function validateContactLastName(fld)
{
    var result = new Array();
  
    if (fld.value.length == 0)
	{
        fld.style.background = '#FFF568'; 
		result[0] = false;
        result[1] = " - Enter your last name.\n";
    }
	else
	{
        fld.style.background = '#FFFFFF';
		result[0] = true;
    }
    return result;   
}

function validateDaytimePhone(fld) {
    var result = new Array();
  
    if (fld.value.length == 0)
	{
        fld.style.background = '#FFF568'; 
		result[0] = false;
        result[1] = " - Enter the best daytime phone number to reach you.\n";
    }
	else
	{
        fld.style.background = '#FFFFFF';
		result[0] = true;
    }
    return result;   
}

function validateEmail(fld)
{
    var result = new Array();
    var tfld = trim(fld.value);                                                                                                                          // value of field with white space trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "")
	{
        fld.style.background = '#FFF568';
		result[0] = false;
        result[1] = " - Enter a valid email address.\n";                    // error for blank email address
    }
	else if (!emailFilter.test(tfld))
	{                                                                   // test email for illegal characters
        fld.style.background = '#FFF568';
		result[0] = false;
        result[1] = " - Enter a valid email address.\n";
    }
	else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFF568';
		result[0] = false;
        result[1] = " - Remove illegal characters from your email address.\n";
    }
	else
	{
        fld.style.background = '#FFFFFF';
		result[0] = true;
    }
    return result;
}

function validateInjuredFirstName(fld)
{
    var result = new Array();
  
    if (fld.value.length == 0)
	{
        fld.style.background = '#FFF568'; 
		result[0] = false;
        result[1] = " - Enter the injured party's first name.\n"
    }
	else
	{
        fld.style.background = '#FFFFFF';
		result[0] = true;
    }
    return result;   
}

function validateInjuredLastName(fld)
{
    var result = new Array();
  
    if (fld.value.length == 0)
	{
        fld.style.background = '#FFF568'; 
		result[0] = false;
        result[1] = " - Enter the injured party's last name.\n"
    }
	else
	{
        fld.style.background = '#FFFFFF';
		result[0] = true;
    }
    return result;   
}

function validateQ01(fld)
{
    var result = new Array();
  
    if (fld.value.length == 0)
	{
        fld.style.background = '#FFF568';
		result[0] = false;
        result[1] = " - Answer when the accident happened.\n"
    }
	else
	{
        fld.style.background = '#FFFFFF';
		result[0] = true;
    }
    return result;   
}

function validateQ02(fld)
{
    var result = new Array();
  
    if (fld.value.length == 0)
	{
        fld.style.background = '#FFF568'; 
		result[0] = false;
        result[1] = " - Answer where the accident occured.\n"
    }
	else
	{
        fld.style.background = '#FFFFFF';
		result[0] = true;
    }
    return result;   
}

function validateQ03(fld)
{
    var result = new Array();
  
    if (fld.value.length == 0)
	{
        fld.style.background = '#FFF568'; 
		result[0] = false;
        result[1] = " - Answer what injuries you suffered.\n"
    }
	else
	{
        fld.style.background = '#FFFFFF';
		result[0] = true;
    }
    return result;   
}

function validateCheckBox()
{
	var result = new Array();
                
	if (document.forms.theForm.agree.checked == true)
	{
		document.forms.theForm.agree.style.background = 'inherit';
		result[0] = true;
	}
	else
	{
		document.forms.theForm.agree.style.background = '#FFF568';
		result[0] = false;
		result[1] = " - Agree to disclaimer.\n";
	}
	return result;
}

function validateSecurity(fld)
{
	var result = new Array();
                
    if (fld.value == 0)
	{
        fld.style.background = '#FFF568';
		result[0] = false;
        result[1] = " - Type 'law' into the security box.\n"
    }
	else
	{
		fld.value = fld.value.toLowerCase();
		if (fld.value == "law" || fld.value == "lawyer")
		{
			fld.style.background = '#FFFFFF';
			result[0] = true;
		}
		else
		{
			fld.style.background = '#FFF568'; 
			result[0] = false;
			result[1] = " - You must enter 'law' into the security box.\n"
		}
    }
    return result;   
}

function focusControl(ctrl)
{
	switch(ctrl)
	{
		case 0:
			location.hash = 'contact';
	  		document.forms.theForm.contactFirstName.focus();
			break;
		case 1:
			location.hash = 'contact';
			document.forms.theForm.contactLastName.focus();
			break;
		case 2:
			location.hash = 'contact';
			document.forms.theForm.daytimePhone.focus();
			break;
		case 3:
			location.hash = 'contact';
			document.forms.theForm.email.focus();
			break;
		case 4:
			location.hash = 'injured';
			document.forms.theForm.injuredFirstName.focus();
			break;
		case 5:
			location.hash = 'injured';
			document.forms.theForm.injuredLastName.focus();
			break;
		case 6:
			location.hash = 'details'
			document.forms.theForm.Q01.focus();
			break;
		case 7:
			location.hash = 'details'
			document.forms.theForm.Q02.focus();
			break;
		case 8:
			location.hash = 'details'
			document.forms.theForm.Q03.focus();
			break;
		case 9:
			location.hash = 'security'
			document.forms.theForm.agree.focus();
			break;
		case 10:
			location.hash = 'security'
			document.forms.theForm.security.focus();
			break;
	}	
}