$(document).ready(function() {
	// the send_form.php script will write all form values to a csv file on the server if we specify which columns the fields should go in
	// there are about 900 fields over this entire site, so writing them out by hand is a pain and it would be tedious to update if a field changed. So...
	
	// Start with the following hidden field to specify the columns to write if javascript is not working:

	// <input type="hidden" name="csvcolumns" value="formURL,email,realname,phone" />

	// the csv file will get at least these values if javascript is turned off.  If it is on, then have javascript replace this value attribute
	// with all of the field values minus the ones we don't care about.
	
	// Make sure to read notes-send_form.txt for some special settings (truncated csv file, date & time, if special newlines are required)
		
		
	var $csvColumnsValue = '';						// this will be the string that replaces the value attribute in the hidden csvcolumns input
	                                               
	                                               
	var $fields = $(':input')						// this gets every field including input, textarea, selects, buttons and hidden fields
	.not( $('input[name*="env_report"]') )			// exclude the following fields from the list because we don't want to output them in the csv file
	.not( $('input[name*="required"]') )
	.not( $('input[name*="subject"]') )
	.not( $('input[name*="good_url"]') )
	.not( $('input[name*="derive_fields"]') )
	.not( $('input[name*="csvcolumns"]') )			// exclude this so it is added separately to the first column
	.not( $(':image') );
	
	// Finds the hidden csvcolumns field before we replace the value attribute with the new one. This is what is used if javascript is turned off
	var $csvColumnsDefault = $('input[name*="csvcolumns"]');
	
	// make sure the csvcolumns field comes first for parsing file in Filemaker. This will give a list of all of the form fieldnames
	$csvColumnsValue += "csvcolumns,";
	
	// cycle through each field in the list
	$($fields).each(function(i) {
		var $field = $(this);						// make explicit what "this" is
		var $fieldName = $($field).attr("name");	// get the value of the attribute "name". This is what needs to be put in the csvcolumns attribute

		// indexing starts at 0. if there are 212 fields, then the last field is 211 ($fields.length-1).  We want to add a comma after each field name
		// except the last one.  So stop at 210 ($fields.length-2). 
		if ( i <= $fields.length-2 ) {
			$csvColumnsValue += $fieldName+',';		// adding a comma after each field name in the csvcolumns list
		} else if ( i == $fields.length-1 ){
			$csvColumnsValue += $fieldName;			// the last one added to the list does not have a comma
			$csvColumnsDefault.attr("value",$csvColumnsValue);	// replace the default value attribute in csvcolumns with one that contains all of the fields
		}
		
	});
	
		
		
});
