function check_form(form, fields, descs, emails, comps, btn)
{
	if (document.getElementById('form_submit').value == 1)
	{
		document.getElementById('form_submit').value = "";
		//make button invisible
		document.getElementById(btn).style.visibility = "hidden";
		
		var bg_color = "#FFFFCC";
		//Clear the background colors of the all fields in form
		var h = 0;
		for (h; h < form.length; h++)
		{
			form.elements[h].style.backgroundColor = "";
			//document.write("<p>" + form.elements[h].name + " " + form.elements[h].value + "</p>");
		}
		
		for(i=0; i < fields.length; i++)
		{
			var field_name = fields[i];
			var field_type = form.elements[field_name].type;
			
			if (field_type == undefined)
			{
				field_type = document.getElementById(fields[i]).type;
			}
	
			if (document.getElementById(fields[i]).disabled == false)
			{
				switch (field_type)
				{
					case "text":
						if (form.elements[field_name].value == "")
						{
							form.elements[field_name].style.background = bg_color	
							alert("'" + descs[i] + "'" + " may not be left blank!")
							form.elements[btn].style.visibility = 'visible';
							form.elements[field_name].focus();
							return false
						}
						break;
					case "password":
						if (form.elements[field_name].value == "")
						{
							form.elements[field_name].style.background = bg_color	
							alert("'" + descs[i] + "'" + " may not be left blank!")
							form.elements[btn].style.visibility = 'visible';
							form.elements[field_name].focus()
							return false
						}
						break;
					case "select-one":
						var box = form.elements[fields[i]];
						if (box.options[box.selectedIndex].value == 0)
						{
							box.style.background = bg_color	
							alert("Please select a '" + descs[i] + "'!");
							form.elements[btn].style.visibility = 'visible';
							box.focus();
							return false;
						}
						break;
					case "radio":
						var radio_obj = form.elements[fields[i]];
						if (get_radio_value(radio_obj) == "")
						{
							document.getElementById(fields[i]).style.background = bg_color;
							alert ("Please select a '" + descs[i] + "'!");
							form.elements[btn].style.visibility = 'visible';
							document.getElementById(fields[i]).focus();
							return false;
						}
						break;
					case "textarea":
						if (form.elements[field_name].value == "")
						{
							form.elements[field_name].style.background = bg_color	
							alert("'" + descs[i] + "'" + " may not be left blank!")
							form.elements[btn].style.visibility = 'visible';
							form.elements[field_name].focus();
							return false
						}
						break;
				} //End swtich
			} //End If disabled
		} //End For loop
		
		//validate emails
		i = 0;
		for (i; i < emails.length; i++)
		{
			if (validate_email(document.getElementById(emails[i])) == 0 && $(emails[i]).value != "")
			{
				form.elements[emails[i]].style.background = bg_color	
				alert("Not a valid e-mail address!")
				form.elements[btn].style.visibility = 'visible';
				form.elements[emails[i]].select()
				return false
			}
		}
		
		//Compare fields in comps array with corresponding field ("confirm_")
		c = 0;
		for (c; c < comps.length; c++)
		{
			var field1 = document.getElementById(comps[c]);
			var field_name2 = "confirm_" + field1.name;
			var field2 = document.getElementById(field_name2);
			if (field1.value != field2.value)
			{
				field1.style.background = bg_color	
				field2.style.background = bg_color	
				alert("Fields do not match!")
				form.elements[btn].style.visibility = 'visible';
				form.elements[comps[c]].select()
				return false
			}
		}
		return true;
	}
	return true;
}

//Display an alert when user does not type a number, letter or underscore, and sets focus on field
function check_it(evt,field_name,form_name,desc)
{
	evt = (evt) ? evt : window.event;
	var charCode = (evt.which) ? evt.which : evt.keyCode;
	if ((charCode > 31 && charCode < 48) || (charCode > 57 && charCode < 65) || (charCode > 90 && charCode < 97) || charCode > 122 && charCode != 13)
	{
		alert("Only letters, numbers or underscores allowed in " + desc + " field.");
		set_focus(form_name,field_name);
		return false;
	}
	return true;
}

//Only allow numbers in input field
function check_num(evt, field, desc)
{
	evt = (evt) ? evt : window.event;
	var charCode = (evt.which) ? evt.which : evt.keyCode;
	if ((charCode < 48 || charCode > 57) && charCode != 9 && charCode != 13) //charCode 9 is "TAB" charCode 13 is "Enter"
	{
		alert("Only numbers allowed in " + desc + " field.");
		field.focus();
		return false;
	}
	return true;
}

function check_num_dec(evt, field, desc)
{
	evt = (evt) ? evt : window.event;
	var charCode = (evt.which) ? evt.which : evt.keyCode;
	if ((charCode < 48 || charCode > 57) && charCode != 9 && charCode != 46 && charCode != 13) //9 is "TAB", 46 is ".", 13 is "Enter"
	{
		alert("Only numbers and decimal point (.) allowed in " + desc + " field.");
		field.focus();
		return false;
	}
	return true;
}

function check_length(field, len, color, desc)
{
	if (field.value.length < len)
	{
		field.style.background = color;
		alert("'" + desc + "' must contain " + len + " characters or numbers!");
		field.focus();
	}
}

//Display an alert when user hits the space bar, and sets focus on field
function check_space(evt,field,desc)
{
	evt = (evt) ? evt : window.event;
	var charCode = (evt.which) ? evt.which : evt.keyCode;
	if (charCode == 32)
	{
		alert("No spaces allowed in " + desc + " field.");
		field.focus();
		return false;
	}
	return true;
}

function inst(option, test_value, form_name, field_name)
{
	var sel = option.options[option.selectedIndex].value;
	var instBox = document.forms[form_name].elements[field_name];
	if (sel == test_value)
	{
		instBox.disabled = false;
		instBox.style.visibility = "visible";
		instBox.focus();
	}
	else
	{
		instBox.disabled = true;
		instBox.style.visibility = "hidden";
	}
}

function reg_pop(s)
{
	window.open(s,"reg_pop","width=520,height=500,scrollbars=yes,resizable=yes");
}

function check_arroba(field,desc)
{
	if (field.value != "")
	{
		var val = field.value.indexOf("@");
		if (val == -1)
		{
			alert("Please enter a valid " + desc);
			field.select();
			return false;
		}
	}
	return true;
}

/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;

/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '') || typeof(theRow.style) == 'undefined') 
	{
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') 
	{
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') 
	{
        theCells = theRow.cells;
    }
    else 
	{
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined' && typeof(theCells[0].getAttribute) != 'undefined') 
	{
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else 
	{
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == '' || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) 
	{
        if (theAction == 'over' && thePointerColor != '') 
		{
            newColor = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') 
		{
            newColor = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase() && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) 
	{
        if (theAction == 'out') 
		{
            newColor = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') 
		{
            newColor = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) 
	{
        if (theAction == 'click') 
		{
            newColor = (thePointerColor != '') ? thePointerColor : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum]) ? true : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) 
	{
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) 
		{
            for (c = 0; c < rowCellsCnt; c++) 
			{
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else 
		{
            for (c = 0; c < rowCellsCnt; c++) 
			{
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5
	theRow.style.cursor = 'hand';

    return true;
} // end of the 'setPointer()' function

function initArray() 
{
	for(i=0;i<initArray.arguments.length; i++)
	this[i] = initArray.arguments[i];
}

function confirm_delete(entry)
{
	return confirm("Are you sure you want to delete " + entry +"?");
}

function moveUp(selBox) 
{
   if ( selBox.length == -1) // If the list is empty
   {  
      alert("There are no values which can be moved!");
   } 
   else 
   {
      var selected = selBox.selectedIndex;
      if (selected == -1) 
	  {
         alert("You must select an entry to be moved!");
      } 
	  else // Something is selected 
	  {  
         if ( selBox.length == 0 ) // If there's only one in the list
		 {  
            alert("There is only one entry!\nThe one entry will remain in place.");
         } 
		 else // There's more than one in the list, rearrange the list order
		 {  
            if ( selected == 0 ) 
			{
               alert("The first entry in the list cannot be moved up.");
            }
			else 
			{
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = selBox[selected-1].text;
               var moveText2 = selBox[selected].text;
               var moveValue1 = selBox[selected-1].value;
               var moveValue2 = selBox[selected].value;
               selBox[selected].text = moveText1;
               selBox[selected].value = moveValue1;
               selBox[selected-1].text = moveText2;
               selBox[selected-1].value = moveValue2;
               selBox.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function moveDown(selBox) 
{
   if ( selBox.length == -1) // If the list is empty
   {  
      alert("There are no values which can be moved!");
   } 
   else 
   {
      var selected = selBox.selectedIndex;
      if (selected == -1) 
	  {
         alert("You must select an entry to be moved!");
      } 
	  else // Something is selected 
	  {  
         if ( selBox.length == 0 ) // If there's only one in the list
		 {  
            alert("There is only one entry!\nThe one entry will remain in place.");
         } 
		 else // There's more than one in the list, rearrange the list order
		 {  
            if ( selected == selBox.length-1 ) 
			{
               alert("The last entry in the list cannot be moved down.");
            } 
			else 
			{
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = selBox[selected+1].text;
               var moveText2 = selBox[selected].text;
               var moveValue1 = selBox[selected+1].value;
               var moveValue2 = selBox[selected].value;
               selBox[selected].text = moveText1;
               selBox[selected].value = moveValue1;
               selBox[selected+1].text = moveText2;
               selBox[selected+1].value = moveValue2;
               selBox.selectedIndex = selected+1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function selectAll(selBox) 
{
	for (var i = 0; i < selBox.options.length; i++) 
	{
		selBox.options[i].selected = true;
	}
	selBox.name = selBox.name + '[]';
}

function clear_date(form_name, y, m, d)
{
	form = document.forms[form_name];
	document.getElementById(y).value = "";
	document.getElementById(m).value = 0;
	document.getElementById(d).value = 0;
}

function clear_bg_color(form)
{
	for (i=0; i < form.length; i++)
	{
		form.elements[i].style.backgroundColor = "";
	}
}

//Define getElementById method for use in older browsers
if(!document.getElementById) 
{
	if(document.all)
	{
		document.getElementById=function()
		{
			if(typeof document.all[arguments[0]]!="undefined")
			{
				return document.all[arguments[0]]
			}
			else
			{
				return null
			}
		} //End Function
	}
	else if(document.layers)
	{
		document.getElementById=function()
		{
			if(typeof document[arguments[0]]!="undefined")
			{
				return document[arguments[0]]
			}
			else
			{
				return null
			}
		} //End Function
	}
}

function get_radio_value(radioObj) 
{
	if (!radioObj)
	{
		return "";
	}
	var radioLength = radioObj.length;
	if (radioLength == undefined)
	{
		if(radioObj.checked)
		{
			return radioObj.value;
		}
		else
		{
			return "";
		}
	}
	for (var i = 0; i < radioLength; i++)
	{
		if (radioObj[i].checked)
		{
			return radioObj[i].value;
		}
	}
	return "";
}

function insert_audit(file, audit_code, notes)
{
	window.location = file + "?ac=" + audit_code + "&notes=" + notes;
}

function insert_audit_rc(e, audit_code, notes)
{
	var file = "file_audit.php?ac=" + audit_code + "&notes=" + notes;
	if (navigator.appName == 'Netscape' && e.which == 3) 
	{
		window.open(file,"FileAudit","directories=no, width=100, height=100, location=no, menubar=no, status=no, toolbar=no, scrollbars=no, resizable=no");
	}
	else 
	{
		if (navigator.appName == 'Microsoft Internet Explorer' && event.button == 2)
		{
			window.open(file,"FileAudit","directories=no, width=100, height=100, location=no, menubar=no, status=no, toolbar=no, scrollbars=no, resizable=no");
		}
	}
}

// generic positive number decimal formatting function
function format (expr, decplaces) 
{
	// raise incoming value by power of 10 times the
	// number of decimal places; round to an integer; convert to string
	var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
	// pad small value strings with zeros to the left of rounded number
	while (str.length <= decplaces) 
	{
		str = "0" + str
	}
	// establish location of decimal point
	var decpoint = str.length - decplaces
	// assemble final result from: (a) the string up to the position of
	// the decimal point; (b) the decimal point; and (c) the balance
	// of the string. Return finished product.
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function get_cookie_data(c_name)
{
	var label_len = c_name.length
	//read cookie property only once for speed
	var cookie_data = document.cookie;
	var c_len = cookie_data.length;
	var i = 0;
	var c_end;
	while (i < c_len)
	{
		var j = i + label_len;
		if (cookie_data.substring(i,j) == c_name)
		{
			c_end = cookie_data.indexOf(";",j);
			if (c_end == -1)
			{
				c_end = cookie_data.length;
			}
			return unescape(cookie_data.substring(j+1, c_end));
		}
		i++;
	}
	return "";
}

function set_cookie(c_name, value, expiredays)
{
	var exdate=new Date()
	exdate.setDate(expiredays)
	document.cookie=c_name + "=" + escape(value) +
	((expiredays==null) ? "" : "; expires=" + exdate)
}

function delete_cookie(c_name)
{
	set_cookie(c_name,0,0);
}

function show_layer(layer_name, selects)
{
	if (selects.length > 0)
	{
		hide_selects(selects);
	}
	var layer = document.getElementById(layer_name);
	layer.style.visibility = 'visible';
}

function hide_layer(layer_name, selects)
{
	if (selects.length > 0)
	{
		show_selects(selects);
	}
	var layer = document.getElementById(layer_name);
	layer.style.visibility = 'hidden';
}

function hide_selects(selects)
{
	var i = 0;
	for (i; i < selects.length; i++)
	{
		document.getElementById(selects[i]).style.visibility = 'hidden';
	}
}

function show_selects(selects)
{
	var i = 0;
	for (i; i < selects.length; i++)
	{
		document.getElementById(selects[i]).style.visibility = 'visible';
	}
}

function check_length(field, field_length, set_focus)
{
	var value_length = field.value.length;
	if (value_length == field_length)
	{
		document.getElementById(set_focus).focus();
	}
}

function validate_email(field)
{
	email = field.value
	at_pos = email.indexOf("@")
	stop_pos = email.lastIndexOf(".")
	
	if (at_pos == -1 || stop_pos == -1) 
	{
		return 0;
	}
	
	if (stop_pos < at_pos) 
	{
		return 0;
	}
	
	if (stop_pos - at_pos == 1) 
	{
		return 0;
	}
	
	return 1;
}	

//Message functions
function clear_messenger_classes() 
{
	var elem_p = $('messenger-span');
	Element.removeClassName(elem_p, 'hourglass');
	Element.removeClassName(elem_p, 'accept');
	Element.removeClassName(elem_p, 'exclamation');
	Element.removeClassName(elem_p, 'stop');
}                                                  

function init_message(msg, status, autokill) 
{
	var elem_p = $('messenger-span');
	var elem = $('messenger-wrap');
	if (msg != '') {    
		clear_messenger_classes();
		switch(status) 
		{
			case(1):
				Element.addClassName(elem_p, 'hourglass');
				break;    
			case(2):
				Element.addClassName(elem_p, 'accept');
				break;
			case(3):
				Element.addClassName(elem_p, 'exclamation');
				msg += '<br /><input type="button" value="Ok" onclick="kill_messenger_quick(\'\'); this.parentNode.removeChild(this);" />'
				break;
		}          
		elem_p.innerHTML = msg;
	}
	Effect.Appear(elem, { duration: 0.1 });
	if (autokill)
		window.setTimeout("kill_messenger('')", 1000);
}

function kill_messenger(name) 
{
	if (name == '')
		name = 'messenger-wrap';
	Effect.Fade(name, { duration: 0.1, queue: 'end' });
}

function kill_messenger_quick(name) 
{
	if (name == '')
		name = 'messenger-wrap';
	Element.hide(name);
}

var jsMonths=new initArray("January","February","March","April","May","June","July","August","September","October","November","December");
var jsMonthsAbb=new initArray("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
var jsDays= new initArray("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
today=new Date();
