/*  =====================================  */
//  Checks against errors on all input fields

//  (c) 2006 Gaslight Media
//  http://www.gaslightmedia.com
/*  =====================================  */

var input_red_box		= 'input-warn-box';
var input_green			= 'input-ok';

//  Regular expressions to check against
var zip		= /^\d{5}(-\d{4})?$/; //99999 or 99999-9999
var nums	= /^\d+$/; //any amount of numbers(only digits)
var phone	= /^\d{3}-\d{3}-\d{4}$/; //999-999-9999
var ssn		= /^\d{3}-\d{2}-\d{4}$/; //999-99-9999
var email	= /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/;

//	Warning Messages
var sub_warn_surveillance			= "<center>Please be sure to advise how many days to conduct surveillance for (integers only).</center>";

//	Submission Error Messages
var sub_error_surveillance			= "<center>If you would like us to conduct surveillance, please advise how many days (integers only).</center>";
var sub_error_reopen_case			= "<center>Please select whether this is an existing case or not.</center>";
var sub_error_subject_name			= "<center>Please enter the subjects name</center>";
var sub_error_subject_ssn			= "<center>Please format the SSN as such <strong>999-99-9999</strong></center>";
var sub_error_subject_weight		= "<center>Weight must be only digits <strong>i.e. - 145</strong></center>";
var sub_error_injury				= "<center>Please describe the physical limitations and/or nature of the injury.</center>";
var sub_error_client_name			= "<center>Please enter your name</center>";
var sub_error_client_email			= "<center>Please enter your email address <strong>i.e. - user@domain.com</strong></center>";
var sub_error_client_email_verify	= "<center>Email address do no match</center>";
var sub_error_client_city			= "<center>Please enter your city</center>";
var sub_error_zip					= "<center>Please enter a proper zip code</center>";
var sub_error_phone					= "<center>Please format the phone number as such <strong>999-999-9999</strong></center>";
var sub_error_ssn					= "<center>Please format the ssn number as such <strong>999-99-9999</strong></center>";
var sub_error_agree					= "<center>You must agree with the terms to continue</center>";
var sub_errors						= "There are errors on your form!\n\nPlease carefully review the form for errors";

var error_style = 'block';

var sub_field_ids   = new Array();
var sub_box_ids     = new Array();
var sub_msg_ids     = new Array();

function id(target) 
{
	return document.getElementById(target);
}

function auto_format_phone( phoneNumber )
{
	var theCount = 0;
	var theString = phoneNumber
	var newString = "";
	var myString = theString;
	var theLen = myString.length;
	for ( var i = 0 ; i < theLen ; i++ )
	{
		// Character codes for ints 1 - 9 are 48 - 57
		if ( (myString.charCodeAt(i) >= 48 ) && (myString.charCodeAt(i) <= 57) )
			newString = newString + myString.charAt(i);   
	}
	// Now the validation to determine that the remaining string is 9 characters.
	if (newString.length == 10 )
	{
		// Now the string has been stripped of other chars it can be reformatted to ###-##-#### 
		var newLen = newString.length;
		var newPhone = "";
		for ( var i = 0 ; i < newLen ; i++ )
		{
			if ( ( i == 2 ) || ( i == 5 ) )
			{
				newPhone = newPhone + newString.charAt(i) + "-";
			}else{
				newPhone = newPhone + newString.charAt(i);
			}
		}
		return newPhone;
	}else{
		return phoneNumber;
	}
}

function auto_format_ssn ( ssn )
{
	var theCount = 0;
	var theString = ssn; 
	var newString = "";
	var myString = theString;
	var theLen = myString.length;
	for ( var i = 0 ; i < theLen ; i++ )
	{
		// Character codes for ints 0 - 9 are 48 - 57
		if ( (myString.charCodeAt(i) >= 48 ) && (myString.charCodeAt(i) <= 57) )
			newString = newString + myString.charAt(i);   
	}
	// Now the validation to determine that the remaining string is 9 characters.
	if (newString.length == 9 )
	{
		// Now the string has been stripped of other chars it can be reformatted to ###-##-#### 
		var newLen = newString.length;
		var newssn = "";
		for ( var i = 0 ; i < newLen ; i++ )
		{
			if ( ( i == 2 ) || ( i == 4 ) )
			{
				newssn = newssn + newString.charAt(i) + "-";
			}else{
				newssn = newssn + newString.charAt(i);
			}
		}
		return newssn;
	}else{
		return ssn;
	}
}

/*  #####################################  */
//  INIT Submission form
/*  #####################################  */
/**
 *	Initialize all fields on the page that
 *	are used to give dynamic feedback to the 
 *	end user when they enter bad data.
 *
 *	This allows for strict checking of data
 *	before I send it to the server, which 
 *	creates a higher chance of eliminating
 *	a round trip to the server for validation.
 *	It also gives the user faster feedback
 *	which is always good.
 */
function init_sub_form()
{
    /*  ------------------------------------  */
    //  Input fields
    /*  ------------------------------------  */
    sub_field_ids['contact-phone']			= id( 'contact-phone' );
    sub_field_ids['surveillance']			= id( 'surveillance' );
    sub_field_ids['surveillance-days']		= id( 'surveillance-days' );
    sub_field_ids['yes-reopen']				= id( 'yes-reopen' );
    sub_field_ids['no-reopen']				= id( 'no-reopen' );
    sub_field_ids['subject-name']			= id( 'subject-name' );
    sub_field_ids['subject-phone']			= id( 'subject-phone' );
    sub_field_ids['subject-ssn']			= id( 'subject-ssn' );
    sub_field_ids['subject-zip']			= id( 'subject-zip' );
    sub_field_ids['subject-weight']			= id( 'subject-weight' );
    sub_field_ids['injury']					= id( 'injury' );
    sub_field_ids['client-name']			= id( 'client-name' );
    sub_field_ids['client-city']			= id( 'client-city' );
    sub_field_ids['client-zip']				= id( 'client-zip' );
    sub_field_ids['client-email']			= id( 'client-email' );
    sub_field_ids['client-phone']			= id( 'client-phone' );
    sub_field_ids['client-fax']				= id( 'client-fax' );
    sub_field_ids['agree']					= id( 'agree' );

    /*  -------------------------------------  */
    //  Error Boxes
    /*  -------------------------------------  */
    sub_box_ids['contact-phone']		= id( 'box-contact-phone' );
    sub_box_ids['surveillance']			= id( 'box-surveillance' );
    sub_box_ids['reopen']				= id( 'box-reopen' );
    sub_box_ids['subject-name']			= id( 'box-subject-name' );
    sub_box_ids['subject-phone']		= id( 'box-subject-phone' );
    sub_box_ids['subject-ssn']			= id( 'box-subject-ssn' );
    sub_box_ids['subject-zip']			= id( 'box-subject-zip' );
    sub_box_ids['subject-weight']		= id( 'box-subject-weight' );
    sub_box_ids['injury']				= id( 'box-injury' );
    sub_box_ids['client-name']			= id( 'box-client-name' );
    sub_box_ids['client-city']			= id( 'box-client-city' );
    sub_box_ids['client-zip']			= id( 'box-client-zip' );
    sub_box_ids['client-email']			= id( 'box-client-email' );
    sub_box_ids['client-phone']			= id( 'box-client-phone' );
    sub_box_ids['client-fax']			= id( 'box-client-fax' );
    sub_box_ids['agree']				= id( 'box-agree' );
    sub_box_ids['errors']				= id( 'box-errors' );
    
	/*  -------------------------------------  */
    //  Error Messages
    /*  -------------------------------------  */
    sub_msg_ids['contact-phone']		= id( 'msg-contact-phone' );
    sub_msg_ids['surveillance']			= id( 'msg-surveillance' );
    sub_msg_ids['reopen']				= id( 'msg-reopen' );
    sub_msg_ids['subject-name']			= id( 'msg-subject-name' );
    sub_msg_ids['subject-phone']		= id( 'msg-subject-phone' );
    sub_msg_ids['subject-ssn']			= id( 'msg-subject-ssn' );
    sub_msg_ids['subject-zip']			= id( 'msg-subject-zip' );
    sub_msg_ids['subject-weight']		= id( 'msg-subject-weight' );
    sub_msg_ids['injury']				= id( 'msg-injury' );
    sub_msg_ids['client-name']			= id( 'msg-client-name' );
    sub_msg_ids['client-city']			= id( 'msg-client-city' );
    sub_msg_ids['client-zip']			= id( 'msg-client-zip' );
    sub_msg_ids['client-email']			= id( 'msg-client-email' );
    sub_msg_ids['client-phone']			= id( 'msg-client-phone' );
    sub_msg_ids['client-fax']			= id( 'msg-client-fax' );
    sub_msg_ids['agree']				= id( 'msg-agree' );
    sub_msg_ids['errors']				= id( 'msg-errors' );

    /*  -------------------------------------  */
    //  Set up onblur
    /*  -------------------------------------  */
    sub_field_ids['contact-phone'].onblur		= check_contact_phone; 
    sub_field_ids['surveillance'].onchange		= warn_surveillance; 
    sub_field_ids['subject-name'].onblur		= check_subject_name; 
    sub_field_ids['subject-phone'].onblur		= check_subject_phone; 
    sub_field_ids['subject-ssn'].onblur			= check_subject_ssn; 
    sub_field_ids['subject-zip'].onblur			= check_subject_zip; 
    sub_field_ids['subject-weight'].onblur		= check_subject_weight; 
    sub_field_ids['injury'].onblur				= check_injury; 
    sub_field_ids['client-name'].onblur			= check_client_name; 
    sub_field_ids['client-city'].onblur			= check_client_city; 
    sub_field_ids['client-zip'].onblur			= check_client_zip; 
    sub_field_ids['client-email'].onblur		= check_client_email; 
    sub_field_ids['client-phone'].onblur		= check_client_phone; 
    sub_field_ids['client-fax'].onblur			= check_client_fax; 
    sub_field_ids['agree'].onchange				= check_agreement; 
}

/*  #####################################  */
//  Check the contact phone 
/*  #####################################  */
function check_contact_phone( event )
{
    var error_found = '';
    sub_field_ids['contact-phone'].value = id('contact-phone').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (sub_field_ids['contact-phone'].value && sub_field_ids['contact-phone'].value != "")
    {
		//	If the phone number field has data in it.
		//	Then auto format the number and check that it matches 
		//	against the pattern I want.
		//	If not, then fill the error message
		sub_field_ids['contact-phone'].value = auto_format_phone( sub_field_ids['contact-phone'].value);
		if (!sub_field_ids['contact-phone'].value.match(phone))
		{
			error_found += sub_error_phone;
		}
	}
    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['contact-phone'].className     = input_red_box;
        sub_msg_ids['contact-phone'].innerHTML       = error_found;
        sub_box_ids['contact-phone'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['contact-phone'].className     = input_green;
        sub_box_ids['contact-phone'].style.display   = 'none';
        sub_msg_ids['contact-phone'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the surveillance for being checked and 
//	if so, check that they have input some amount
//	of days.
/*  #####################################  */
function check_surveillance( event )
{
    var error_found = '';
    sub_field_ids['surveillance'].checked = id('surveillance').checked;
    sub_field_ids['surveillance-days'].value = id('surveillance-days').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (sub_field_ids['surveillance'].checked)
    {
		if (!sub_field_ids['surveillance-days'].value ||
			sub_field_ids['surveillance-days'].value == "" ||
			!sub_field_ids['surveillance-days'].value.match(nums))
		{
			error_found += sub_error_surveillance;
		}
	}
    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_msg_ids['surveillance'].innerHTML			= error_found;
        sub_box_ids['surveillance'].className			= 'input-warn-content';
        sub_box_ids['surveillance'].style.display		= error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['surveillance-days'].className    = input_green;
        sub_box_ids['surveillance'].style.display		= 'none';
        sub_msg_ids['surveillance'].innerHTML			= '';
    }
}

/*  #####################################  */
//  If the user checked the surveillance box
//	advise them to make sure they fill in the
//	num of days box also.
/*  #####################################  */
function warn_surveillance( event )
{
    var error_found = '';
    sub_field_ids['surveillance'].checked = id('surveillance').checked;
    sub_field_ids['surveillance-days'] = id('surveillance-days');
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (sub_field_ids['surveillance'].checked)
    {
		error_found += sub_warn_surveillance;
		sub_field_ids['surveillance-days'].disabled = false;
	}
	else
	{
		sub_field_ids['surveillance-days'].value = "";
		sub_field_ids['surveillance-days'].disabled = true;
	}
    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_msg_ids['surveillance'].innerHTML       = error_found;
        sub_box_ids['surveillance'].className		= 'input-new-content';
        sub_box_ids['surveillance'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_box_ids['surveillance'].style.display   = 'none';
        sub_box_ids['surveillance'].className		= 'input-warn-content';
        sub_msg_ids['surveillance'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check whether this is a reopened case or not.
/*  #####################################  */
function check_reopen( event )
{
    var error_found = '';
    sub_field_ids['yes-reopen'].checked = id('yes-reopen').checked;
    sub_field_ids['no-reopen'].checked = id('no-reopen').checked;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (!sub_field_ids['yes-reopen'].checked && !sub_field_ids['no-reopen'].checked)
    {
		error_found += sub_error_reopen_case;
	}
    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_msg_ids['reopen'].innerHTML       = error_found;
        sub_box_ids['reopen'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_box_ids['reopen'].style.display   = 'none';
        sub_msg_ids['reopen'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the subjects name 
/*  #####################################  */
function check_subject_name( event )
{
    var error_found = '';
    sub_field_ids['subject-name'].value = id('subject-name').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (!sub_field_ids['subject-name'].value || sub_field_ids['subject-name'].value == "") 
    {
        error_found += sub_error_subject_name;
    }

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['subject-name'].className     = input_red_box;
        sub_msg_ids['subject-name'].innerHTML       = error_found;
        sub_box_ids['subject-name'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['subject-name'].className     = input_green;
        sub_box_ids['subject-name'].style.display   = 'none';
        sub_msg_ids['subject-name'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the subjects phone 
/*  #####################################  */
function check_subject_phone( event )
{
    var error_found = '';
    sub_field_ids['subject-phone'].value = id('subject-phone').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (sub_field_ids['subject-phone'].value && sub_field_ids['subject-phone'].value != "")
    {
		//	If the phone number field has data in it.
		//	Then auto format the number and check that it matches 
		//	against the pattern I want.
		//	If not, then fill the error message
		sub_field_ids['subject-phone'].value = auto_format_phone( sub_field_ids['subject-phone'].value);
		if (!sub_field_ids['subject-phone'].value.match(phone))
		{
			error_found += sub_error_phone;
		}
	}
	
    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['subject-phone'].className     = input_red_box;
        sub_msg_ids['subject-phone'].innerHTML       = error_found;
        sub_box_ids['subject-phone'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['subject-phone'].className     = input_green;
        sub_box_ids['subject-phone'].style.display   = 'none';
        sub_msg_ids['subject-phone'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the subjects social security number
/*  #####################################  */
function check_subject_ssn( event )
{
    var error_found = '';
    sub_field_ids['subject-ssn'].value = id('subject-ssn').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
	//	If there is a SSN number in the field and it doesn't
	//	match my ssn number pattern
	//	fill the error message to display
    if (sub_field_ids['subject-ssn'].value && sub_field_ids['subject-ssn'].value != "")
    {
		//	If the ssn field has data in it.
		//	Then auto format the number and check that it matches 
		//	against the pattern I want.
		//	If not, then fill the error message
		sub_field_ids['subject-ssn'].value = auto_format_ssn( sub_field_ids['subject-ssn'].value);
		if (!sub_field_ids['subject-ssn'].value.match(ssn))
		{
			error_found += sub_error_ssn;
		}
	}
    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['subject-ssn'].className     = input_red_box;
        sub_msg_ids['subject-ssn'].innerHTML       = error_found;
        sub_box_ids['subject-ssn'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['subject-ssn'].className     = input_green;
        sub_box_ids['subject-ssn'].style.display   = 'none';
        sub_msg_ids['subject-ssn'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the subjects zip code
/*  #####################################  */
function check_subject_zip( event )
{
    var error_found = '';
    sub_field_ids['subject-zip'].value = id('subject-zip').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
	//	If there is a zip code in the text box
	//	but it doesn't match what a zip is supposed
	//	to look like, then fill the error message 
	//	to display
    if (sub_field_ids['subject-zip'].value && !sub_field_ids['subject-zip'].value.match(zip)) 
    {
        error_found += sub_error_zip;
    }

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['subject-zip'].className     = input_red_box;
        sub_msg_ids['subject-zip'].innerHTML       = error_found;
        sub_box_ids['subject-zip'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['subject-zip'].className     = input_green;
        sub_box_ids['subject-zip'].style.display   = 'none';
        sub_msg_ids['subject-zip'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the subjects weight 
/*  #####################################  */
function check_subject_weight( event )
{
    var error_found = '';
    sub_field_ids['subject-weight'].value = id('subject-weight').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
	//	If there is data in the field, then it must be all numbers
    if (sub_field_ids['subject-weight'].value && !sub_field_ids['subject-weight'].value.match(nums)) 
    {
        error_found += sub_error_subject_weight;
    }

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['subject-weight'].className     = input_red_box;
        sub_msg_ids['subject-weight'].innerHTML       = error_found;
        sub_box_ids['subject-weight'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['subject-weight'].className     = input_green;
        sub_box_ids['subject-weight'].style.display   = 'none';
        sub_msg_ids['subject-weight'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the injury / limitations field
/*  #####################################  */
function check_injury( event )
{
    var error_found = '';
    sub_field_ids['injury'].value = id('injury').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (!sub_field_ids['injury'].value || sub_field_ids['injury'].value == "") 
    {
        error_found += sub_error_injury;
    }

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['injury'].className     = input_red_box;
        sub_msg_ids['injury'].innerHTML       = error_found;
        sub_box_ids['injury'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['injury'].className     = input_green;
        sub_box_ids['injury'].style.display   = 'none';
        sub_msg_ids['injury'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the clients name 
/*  #####################################  */
function check_client_name( event )
{
    var error_found = '';
    sub_field_ids['client-name'].value = id('client-name').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (!sub_field_ids['client-name'].value || sub_field_ids['client-name'].value == "") 
    {
        error_found += sub_error_client_name;
    }

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['client-name'].className     = input_red_box;
        sub_msg_ids['client-name'].innerHTML       = error_found;
        sub_box_ids['client-name'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['client-name'].className     = input_green;
        sub_box_ids['client-name'].style.display   = 'none';
        sub_msg_ids['client-name'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the clients city 
/*  #####################################  */
function check_client_city( event )
{
    var error_found = '';
    sub_field_ids['client-city'].value = id('client-city').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (!sub_field_ids['client-city'].value || sub_field_ids['client-city'].value == "") 
    {
        error_found += sub_error_client_city;
    }

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['client-city'].className     = input_red_box;
        sub_msg_ids['client-city'].innerHTML       = error_found;
        sub_box_ids['client-city'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['client-city'].className     = input_green;
        sub_box_ids['client-city'].style.display   = 'none';
        sub_msg_ids['client-city'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the clients zip 
/*  #####################################  */
function check_client_zip( event )
{
    var error_found = '';
    sub_field_ids['client-zip'].value = id('client-zip').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (!sub_field_ids['client-zip'].value || !sub_field_ids['client-zip'].value.match(zip)) 
    {
        error_found += sub_error_zip;
    }

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['client-zip'].className     = input_red_box;
        sub_msg_ids['client-zip'].innerHTML       = error_found;
        sub_box_ids['client-zip'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['client-zip'].className     = input_green;
        sub_box_ids['client-zip'].style.display   = 'none';
        sub_msg_ids['client-zip'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the clients email 
/*  #####################################  */
function check_client_email( event )
{
    var error_found = '';
    sub_field_ids['client-email'].value = id('client-email').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (!sub_field_ids['client-email'].value || !sub_field_ids['client-email'].value.match(email)) 
    {
        error_found += sub_error_client_email;
    }

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['client-email'].className     = input_red_box;
        sub_msg_ids['client-email'].innerHTML       = error_found;
        sub_box_ids['client-email'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['client-email'].className     = input_green;
        sub_box_ids['client-email'].style.display   = 'none';
        sub_msg_ids['client-email'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the clients phone 
/*  #####################################  */
function check_client_phone( event )
{
    var error_found = '';
    sub_field_ids['client-phone'].value = id('client-phone').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
	//	If the phone number field has data in it.
	//	Then auto format the number and check that it matches 
	//	against the pattern I want.
	//	If not, then fill the error message
    if (!sub_field_ids['client-phone'].value || sub_field_ids['client-phone'].value == "")
    {
		error_found += sub_error_phone;
	}
	else
	{
		sub_field_ids['client-phone'].value = auto_format_phone( sub_field_ids['client-phone'].value);
		if (!sub_field_ids['client-phone'].value.match(phone))
		{
			error_found += sub_error_phone;
		}
	}

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['client-phone'].className     = input_red_box;
        sub_msg_ids['client-phone'].innerHTML       = error_found;
        sub_box_ids['client-phone'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['client-phone'].className     = input_green;
        sub_box_ids['client-phone'].style.display   = 'none';
        sub_msg_ids['client-phone'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the clients fax 
/*  #####################################  */
function check_client_fax( event )
{
    var error_found = '';
    sub_field_ids['client-fax'].value = id('client-fax').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
	//	If the phone number field has data in it.
	//	Then auto format the number and check that it matches 
	//	against the pattern I want.
	//	If not, then fill the error message
    if (sub_field_ids['client-fax'].value && !sub_field_ids['client-fax'].value.match(phone))
    {
		sub_field_ids['client-fax'].value = auto_format_phone( sub_field_ids['client-fax'].value);
		if (!sub_field_ids['client-fax'].value.match(phone))
		{
			error_found += sub_error_phone;
		}
	}

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['client-fax'].className     = input_red_box;
        sub_msg_ids['client-fax'].innerHTML       = error_found;
        sub_box_ids['client-fax'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['client-fax'].className     = input_green;
        sub_box_ids['client-fax'].style.display   = 'none';
        sub_msg_ids['client-fax'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Check the form agreement box
/*  #####################################  */
function check_agreement( event )
{
    var error_found = '';
    sub_field_ids['agree'].value = id('agree').value;
    /*  ------------------------------------  */
    //  Fill the error message to display
    /*  ------------------------------------  */
    if (!sub_field_ids['agree'].checked)
    {
		error_found += sub_error_agree;
	}

    /*  ------------------------------------  */
    //  Show user they have ERRORS
    //  Devil boxes (Red Power!)
    /*  ------------------------------------  */
    if (error_found)
    {
        sub_field_ids['agree'].className     = input_red_box;
        sub_msg_ids['agree'].innerHTML       = error_found;
        sub_box_ids['agree'].style.display   = error_style;
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    /*  ------------------------------------  */
    //  Show user they are OKAY
    //  My precious green boxes!
    /*  ------------------------------------  */
    if (error_pname == 0)
    {
        sub_field_ids['agree'].className     = input_green;
        sub_box_ids['agree'].style.display   = 'none';
        sub_msg_ids['agree'].innerHTML       = '';
    }
}

/*  #####################################  */
//  Display that there are errors on the form
/*  #####################################  */
function announce_errors(errors)
{
    if (!errors)
    {
		alert(sub_errors);
        error_pname = 1;
    }
    else
    {
        error_pname = 0;
    }

    if (error_pname == 0)
    {
        sub_box_ids['errors'].style.display   = 'none';
        sub_msg_ids['errors'].innerHTML       = '';
    }
}

/*  #####################################  */
//  VALIDATE THE SUBMISSION FORM
/*  #####################################  */
function validate_ia_request( event )
{
    sub_oktogo = 1; //default to true, if you find any errors set to false so form wont submit.

    /*  ---------------------------------  */
    //  CHECK THE FORM
    //  
    //  Simply run through the functions
    //  to determine if there are any errors.
    /*  ---------------------------------  */
    check_contact_phone( event ); 
    check_surveillance( event ); 
    check_reopen( event ); 
    check_subject_name( event ); 
    check_subject_phone( event ); 
    check_subject_ssn( event ); 
    check_subject_zip( event ); 
    check_subject_weight( event ); 
    check_injury( event ); 
    check_client_name( event ); 
    check_client_city( event ); 
    check_client_zip( event ); 
    check_client_email( event ); 
    check_client_phone( event ); 
    check_client_fax( event ); 
    check_agreement( event ); 
    
    /*  ---------------------------------  */
    //  CHECK FOR ERROR MESSAGES
    //  
    //  if any error messages are still on
    //  the screen, the sub_oktogo variable
    //  will be set to false and the form
    //  will not submit.
    /*  ---------------------------------  */
    if (sub_msg_ids['contact-phone'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['surveillance'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['reopen'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['subject-name'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['subject-phone'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['subject-ssn'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['subject-zip'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['subject-weight'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['injury'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['client-name'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['client-city'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['client-zip'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['client-email'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['client-phone'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['client-fax'].innerHTML)
    {
        sub_oktogo = 0;
    }
    if (sub_msg_ids['agree'].innerHTML)
    {
        sub_oktogo = 0;
    }

	announce_errors(sub_oktogo);
    /*  ---------------------------------  */
    //  RETURN if we're okay to go or not.
    /*  ---------------------------------  */
	return sub_oktogo ? true : false;
}

//	Attach approtiate events
function addEvent(elm, evType, fn, useCapture)
{
	//	Firefox likes this
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	//	IE likes this
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

function init_page(e)
{
	init_sub_form(e);
	enableTooltips("iarequest");
}
//	Wait until the window loads so we
//	can initialize our jazzy javascript
addEvent(window, 'load', init_page, false);
