// csv.js
// created for Stanford Shakespeare Company 2009
// michael hammersley (michael.hammersley@gmail.com)

// i'm not sure why i named it csv. whatever.

var invalid_characters = new Array('/', '\\', '<', '>', ']', '[', '`', '|', '{', '}', '$');

///////////////////////////////////////////////////////////
// String.prototype.trim
///////////////////////////////////////////////////////////
// input:	none
// output: trimmed string
// usage: str.trim() will remove all leading and trailing spaces from string 'str'.
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

///////////////////////////////////////////////////////////
// function is_empty
///////////////////////////////////////////////////////////
// input:	field - field of the document
//				required - boolean
// output: boolean
// true if the field is empty and shouldn't be; false otherwise.
function is_empty(field,required) {
	var str = field.value.trim();

	if(str==null || str=="") {
		if(required) {
			document.getElementById('error_message').innerHTML = 'Please fill in all fields. ';
		}
		return true;
	} else {
		return false;
	}
}

///////////////////////////////////////////////////////////
// function is_valid_text
///////////////////////////////////////////////////////////
// input:	field - field of the document
// output: boolean
// true if does not have an invalid character; otherwise, false.
function is_valid_text(field) {
	if(is_empty(field)==false) {
		for(n=0; n < invalid_characters.length; n=n+1) {
				if(field.value.indexOf(invalid_characters[n])!= -1){
					document.getElementById('error_message').innerHTML =
					'Please don\'t use any of (' + invalid_characters + ').';
				return false;
			}
		}
	}
	return true;
}
///////////////////////////////////////////////////////////
// function is_valid_email
///////////////////////////////////////////////////////////
// input:	field - field of the document
// output: boolean
// true if it looks like an email address; otherwise, false.
// this isn't used if we're not looking for email addresses.
function is_valid_email(field) {
	with(field) {
		if(is_empty(field) || is_valid_text(field)==false) {
			return false;
		}
		else {
			var apos=value.indexOf('@');
			var dotpos=value.lastIndexOf('.');
			if (apos<1||dotpos-apos<2||value.length-dotpos<2) {
					document.getElementById('error_message').innerHTML = 'Please use a valid e-mail.';
				return false;
			}
			else return true;
		}
	}
}
///////////////////////////////////////////////////////////
// function validate_update
///////////////////////////////////////////////////////////
// input:	thisform - form element
// output: boolean
// runs the checks on the 'update reservation', returns true if passes;
// otherwise, false.
function validate_update(thisform) {
	var bad_fields = new Array();
	var message = '';
	var pass_flag = true;
	with(document){
		clear_elements();
		if(!is_valid_text(getElementById('name')) || is_empty(getElementById('name'),true)) {
			bad_fields.push('name');
			pass_flag = false;
		}
		if(!is_valid_text(getElementById('find'))) {
			bad_fields.push('find');
			pass_flag = false;
		}
		make_error(bad_fields);
	}
	return pass_flag;
}

///////////////////////////////////////////////////////////
// function clear_elements
///////////////////////////////////////////////////////////
// input:	none
// output: none
// undoes the error messages (i.e., removes the red labels and the errors).
function clear_elements() {
	with(document) {
		getElementById('name_label').className="";
		getElementById('find_label').className="";
		getElementById('showing_label').className="";
		getElementById('error_message').innerHTML="";
	}
}
///////////////////////////////////////////////////////////
// function validate_form
///////////////////////////////////////////////////////////
// input:	thisform - form element
// output: boolean
// runs checks on the make_reservation form, returns true
// if passes; otherwise, false.
function validate_form(thisform) {
	var bad_fields = new Array();
	var message = '';
	var pass_flag = true;
	with(document){
		clear_elements();
		if(!is_valid_text(getElementById('name')) || is_empty(getElementById('name'),true)) {
			bad_fields.push('name');
			pass_flag = false;
		}
//		in case need to check emails again.
/*		if(is_valid_text(getElementById('email')) == false || is_empty(getElementById('email'),true)
			|| is_valid_email(getElementById('email')) == false) {
//			alert('here too');
			bad_fields.push('email');
			pass_flag = false;
		}
	*/
		if(!is_valid_text(getElementById('find'))) {
			bad_fields.push('find');
			pass_flag = false;
		}
		make_error(bad_fields);
	}
	return pass_flag;
}

///////////////////////////////////////////////////////////
// function make_error
///////////////////////////////////////////////////////////
// input:	bad_fields - array of all the bum fields
// output: none
// changes the classname of the element to 'error' so that it
// turns red, or whatever the CSS file defines.
function make_error(bad_fields) {
	for(var n=0; n < bad_fields.length; n=n+1) {
		with(document.getElementById(bad_fields[n]+'_label')) {
			className='error';
		}
	}
}
///////////////////////////////////////////////////////////
// function validate_edit_form
///////////////////////////////////////////////////////////
// input:	none
// output: boolean
// checks the 'edit reservation' form (name + confirmation
// number) and makes sure it's up to snuff.
function validate_edit_form() {
	var bad_fields = new Array();
	with(document) {
		getElementById('name_label').className="";
		getElementById('res_num_label').className="";

		pass_flag = true;
		if(!is_valid_text(getElementById('name')) || is_empty(getElementById('name'),true)) {
				bad_fields.push('name');
				pass_flag = false;
		}

		if(!is_valid_text(getElementById('res_num')) || is_empty(getElementById('res_num'),true)) {
				bad_fields.push('res_num');
				pass_flag = false;
		}
	}

	make_error(bad_fields);
	return pass_flag;
}