

/***********Process Form *************
**************************************
*************************************/


/************************************
       NOTES OF THIS SCRIPT
************************************
A few things to start off: 
  -This script assumes that all text feilds are required. If a field is not required then a * should be added the the prefix of the name feild. For example, a input box that was not required would look like this: 
	
		<input type=text name="*the_name">
		
  - There are three hiddnen elements that should concern as well. The hidden elements are important for telling the process_form() function which elements in the form are "special." For the type being, these elements must be added to the form even if the values are set too "". 
	The three hidden feids are; numbers, checkboxes, and radio_buttons. Look below for a breif description of each
	
	- Numbers: Set the number var below

    	- Checklists: This is the way we know how many chekclists there are. If you want to checkbox groups has at least one element checked. Set the variables below

 		-Radio_Buttons: If it is required that one of the radio buttons be checked, add it to this list. For the most part people use radio buttons if they are required, so this may not stay in here for long. 

More on checklists: 
   For a lot of checklists, the value that people are looking for isn't shown in the list, so  the script allows for and addition of an other textbox, that is only required if no checkboxes have been checked. However, the name of the textbox must be the name of the checkbox group + _other and it must be the first element after the last element of the checkbox. 
For Example: 

		<input type=checkbox name=box1 value=Checkbox1>
		<input type=checkbox name=box1 value=Checkbox2>
		<input type=checkbox name=box1 value=Checkbox3>
		<input type=text name="box1_other"> 

Select-one Elements: 
   Any option of a select element that is not a valid options,  like the option that may say "Please select One"  must have a value of "blank" 
For Example: 
    <select name="SelectBox" size=1>
	  <option value=blank>Please select one...
	  <option value=blank>-----------------
	  <option value=State1>State1
	</select>

	The select option has the same capabilites as the checkboxes, with the same contraints. 
	
	
************************************/

/******** Global variables ***********
*************************************/

 // check_list
 var temp = ""; 
 var check_list = new Array();
 check_list = temp.split(","); 
 
 // set Num_list 
 temp = "Phone,Zip"
 var num_list = new Array();
 num_list = temp.split(","); 
 
 // set radio list 
 temp = ""
 var radio_list = new Array();
 radio_list = temp.split(","); 

 var element;       // The current element
 var type;          // The type of the current element 
 var name;          // The name of the current element
 var value;         // The value of the current element
 var errors = "";   // A list of the current errors
 var inx_of_name; 
 var cook = new Array(); 
 var inx_of_cook = 0; 
 var test = ""; 
/***************************************
**************String Functions *********
****************************************/
function is_blank(str) 
{ 
  
  var i; 
  for(i = 0; i < str.length; i++) 
  {
    var c = str.charAt(i); 
    if((c != ' ') && (c != '\t') && (c != '\n'))
	  return false;
  } 

 return true; 
} 

function is_num(str) {
	var i; 
	if(is_blank(str)) 
	  return false; 
	else  
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if ((mychar < "0" || mychar > "9") && (mychar != "-"))
			return false
	}
	return true
}

/***********************************
********** Array Functions *********
************************************/

function in_list(val, the_list)
{ 
for(n = 0; n < the_list.length; n++) 
  if(val == the_list[n])
   { 
    inx_of_name = n; 
	return  true; 
   } 
  return false;

}

function remove_item(inx, the_list)
{
   var i = inx; 
   if(i == the_list.length - 1)
     the_list[i] = "\0"; 
   else
    {
     for(i = inx; i < the_list.length; i++)
	     the_list[i] = the_list[i + 1]; 
	 }
	 the_list.length = the_list.length - 1; 
}
/******************************************
************** Fill in the form ***********
******************************************/
function fill_in()
{ 
var str;
var val;
var inx; 
var values = new Array();
var i; 

str = document.location.toString(); 
inx = str.search(/.html?/); 
inx += 6; 
str = str.slice(inx, str.length); 

values = str.split('&'); 

for(i=3; i  < values.length; i++)
  { 
  inx = values[i].indexOf("="); 
 // str = values[i].slice(0, inx)
  str = values[i].slice(inx + 1)
  document.forms[0].elements[i].value = str; 
  }
 
}

/******************************************
**********  Form Functions ****************
******************************************/

function check_text()
{
 if(is_blank(value) && (!in_list(name, num_list))) 
  errors = errors + name + "\t <-- You must fill in this feild \n"; 
 else if(!is_blank(value) && (!in_list(name, num_list)))
   cook[inx_of_cook++] = escape(name) + "=" + escape(value); 
   
   
  if(in_list(name, num_list) && is_num(value))
	 remove_item(inx_of_name, num_list); 
} 

function check_box(elm)
  { 
   if(elm && in_list(name, check_list)) 
      remove_item(inx_of_name, check_list);
	    
  }
  
function check_other(inx)
 { 
   var temp; 
   temp = name.substr(0, inx); 
 
      
 }
 
 function check_select(inx)
 { 
   if(inx < 2) 
     errors = errors + name + "\t <-- You must select one. \n"; 
         
 }
 
function check_radio(elm)
{ 
   if(elm && in_list(name, radio_list)) 
      remove_item(inx_of_name, radio_list);
}


/*********************************
******** Error Functions *********
*********************************/

function add_errors()
{
var i;
for(i = 0; i < num_list.length; i++)
  if(num_list[i] != "") 
    errors = errors + num_list[i] + "\t <- This value may only contain Numbers and Hyphens" +"\n"; 

for(i = 0; i < check_list.length; i++)
  if(radio_list[i] != "") 
    errors = errors + check_list[i] + "\t <- You must check at least one of the boxes in this list" +"\n"; 

for(i = 0; i < radio_list.length; i++)
  if(radio_list[i] != "") 
    errors = errors + radio_list[i] + "\t <- You must select on of options." +"\n"; 
}

function show_errors()
{
document.write("<b>Before you click the submit button please make sure you have filled in all the following areas:</b><br>" + errors + ""); 

}

/******************************************
***** The function that proccesses the form
*******************************************/ 

function process_form(the_form)
{ 
 var i; 
 var out = "";
 var temp; 
 inx_of_cook = 0;
 cook.length = 0; 
 errors = "";  
 var n; 


// The processing Functions are below. These check the make sure that the certain critera
// are met for each element type. 

 for(i = 0; i < the_form.length; i++) 
 	{ 
   type = the_form.elements[i].type; 
   value = the_form.elements[i].value; 
   name = the_form.elements[i].name; 
if(name.charAt(0) != "_")
 {
  if((type == "text") && (name.search(/_other/) != -1))
  {
   out = the_form.elements[i-1].options.selectedIndex;
   temp = temp + the_form.elements[i-1].options[out].value;  
   if(temp == "blank")
      check_text(); 
  }
  else if(type == "checkbox") 
   check_box(the_form.elements[i].checked); 
  else if((type == "text") && (name.search(/_other/) == -1)) 
   check_text();
  else if(type == "radio")
   check_radio(the_form.elements[i].checked); 
  else if(type == "select-one")
   {
  out = the_form.elements[i].options.selectedIndex; 
  temp = the_form.elements[i].options[out].value; 
  if(temp == "blank") 
     {
	 if(i == the_form.length - 1) 
	   errors = errors + name + "\t <-- You must select one \n" 
     else if(the_form.elements[i+1].type == "text") 
       {
   		if((the_form.elements[i+1].name.search(/_other/) != -1) && (is_blank(the_form.elements[i+1].value)))
      		errors = errors + the_form.elements[i+1].name + "\t <-- You must either pick from the drop down list, or enter something here. \n";
      	}
	  else
	  	errors = errors + name + "\t <-- You must select one \n" 
     }  
   }
  else if(type == "textarea")
    check_text(); 
  }
 } 

 add_errors();   // Adds what is remaining the lists to the error message 	

if(errors == "")
  the_form.submit(); 
else 
  alert("You have not completely fill out this form. Please correct the following areas: \n" + errors); 
}

