/*----------------------------------------------------------------------------#
#                                                                             #
# Author:  Casey Bienvenu                                                     #
#          casey@fireflydigital.com                                           #
#                                                                             #
# Date:    February 7, 2006                                                   #
# Version: 1.2                                                                #
#                                                                             #
# Description:                                                                #
# This javascript library will loop through an array of form fields provided  #
# and check for any empty fields. An alert is created based on the titles of  #
# the form fields and the table row the field is in is highlighted. The first #
# empty field will be focused to after closing the alert box.                 #
#                                                                             #
#-----------------------------------------------------------------------------*/

/* TO USE:
    1. Include this library file
	
	    -------- EXAMPLE --------
	    
		<script language="JavaScript" src="/formcheck.js"></script>
		
	    -------------------------
    
	2. Add the function checkForm() below to your file with an array of field names to check
	
	    -------- EXAMPLE --------
	    
		function checkForm(objForm)
		{
			var required = new Array('SocialNum',
									 'FirstName',
									 'LastName',
									 'areacode',
									 'phone1',
									 'phone2',
									 'USLegal',
									 'Felony',
									 'investigate',
									 'SpecJob_1',
									 'WorkTime',
									 'CanWork[]',
									 'PresentEmploy',
									 'agreebox');
									 
			for(i=0;i<required.length;i++)
			{
				checkField(objForm.elements[required[i]]);
			}
			
			if(errors != "")
			{
				alert("Please provide us with the following before submitting the form:\n" + errors);
				
				focusTo.focus();
				
				errors = "";
				focusTo = false;
				
				return false;
			}
			else
			{
				return true;	
			}
		}
		
	    -------------------------
	
	3. Include the following parameter on your form tag
	
	    -------- EXAMPLE --------
	    
		<form onsubmit="return checkForm(this)">
		
	    -------------------------
	
	4. Make sure each required field has the following parameters:
		  - Name
		  - Title (appears in alert box)
		  - Id    (matches with name of table row/object field is in)
	  
	    -------- EXAMPLE --------
	    
		<input name="FirstName" id="FirstName" value="" title="First Name">
		
	    -------------------------
	
	5. Give the row the form field is in an id matching the field id with 'row_' preceding it
	
	    -------- EXAMPLE --------
	    
		<tr id="row_FirstName">
		
	    -------------------------
*/

	var errors = "";
	var focusTo = false;
	
	/* Color field to error style */
	function errorField(obj)
	{
		objRow = document.getElementById("row_" + obj.id);
		
		objRow.style.background = '#F17C54';
		objRow.style.color  = '#FFFFFF';
	}
	
	/* Set field back to normal look */
	function clearField(obj)
	{
		objRow = document.getElementById("row_" + obj.id);
		
		objRow.style.background = 'none';
		objRow.style.color  = '#000000';
	}
	
	/* Append error to alert, and color field */
	function appendError(obj)
	{
		errors += "\n- " + obj.title;
		
		errorField(obj);

		if(focusTo == false) focusTo = obj;
	}
	
	/* Check to determine if a form field is empty */
	function checkField(s)
	{
		// If radio button
		if(!s.name)
		{
			switch(s[0].type)
			{
				case "text":
				case "textarea":
				case "password":
				
					if(s.length > 1)
					{
						for(o=0;o<s.length;o++)
						{
							if(s[o].value == null && s[o].value == "" || s[o].value.length == 0 || s[o].value == "$")
							{
								appendError(s[o]);
							}
							else
							{
								clearField(s[o]);
							}
						}
					}
					
				break;
				
				default:
				
					if(s.length > 1)
					{
						found = 0;
						for(o=0;o<s.length;o++)
						{
							if(s[o].checked == true)
							{
								found++;
							}
						}
						
						if(found == 0)
						{
							appendError(s[0]);
						}
						else
						{
							clearField(s[0]);
						}
					}
				
				break;
			}
		}
		// All other form field object types
		else
		{		
			switch(s.type)
			{
				case "text":
				case "textarea":
				case "password":
				case undefined:
					if(s.length > 1)
					{
						for(o=0;o<s.length;o++)
						{
							if(s[o].value == null && s[o].value == "" || s[o].value.length == 0 || s[o].value == "$")
							{
								appendError(s[o]);
							}
							else
							{
								clearField(s[o]);
							}
						}
					}
					else
					{
						if(s.value == null && s.value == "" || s.value.length == 0 || s.value == "$")
						{
							appendError(s);
						}
						else
						{
							clearField(s);
						}
					}
				break;
				
				case "select-one":
				case "select-multiple":
					if(s.value == null && s.value == "" || s.value.length == 0 || s.value == "-1")
					{
						appendError(s);
					}
					else
					{
						clearField(s);
					}
				break;
	
				default:
					
					if(s.length > 1)
					{
						found = 0;
						for(o=0;o<s.length;o++)
						{
							if(s[o].checked == true)
							{
								found++;
							}
						}
						
						if(found == 0)
						{
							appendError(s[0]);
						}
						else
						{
							clearField(s[0]);
						}
					}
					else
					{
						if(s.checked != true)
						{
							appendError(s);
						}
						else
						{
							clearField(s);
						}
					}
				break;
			}
		}
	}
	
	/* Jump to next field once max limit is reached. */
	function jumpField(objField, length, nextField)
	{
		if(objField.value.length >= length)
		{
			nextField.focus();
		}
	}
	
	/* Jump to previous field when field is empty and backspace is hit */
	function checkback(objField, prevField)
	{
		if((objField.value == '') && (window.event.keyCode == 8))
		{
			tmp = prevField.value;
			prevField.value = '';
			prevField.focus();
			prevField.value = tmp;
		}	
	}
