window.onload = onload_handler();

function onload_handler() {
	setTimeout('display_timeout_warning()', 1440000); // give a warning after 24 minutes
	setTimeout('display_timeout_error()', 1800000); // advise that a timeout has occurred after 30 minutes
	}
	
function display_timeout_warning() {
	alert('Your session has been inactive for several minutes and will soon timeout. Do something (navigate to another page or reload this page) to continue.');
	}
	
function display_timeout_error() {
	alert('Your session has expired.\nYou may need to log in again.');
	window.location = window.location.href;
	}
	
// This is used to open reports in another window
function open_artwork_report_all() {
	window.open("./resources/reports.php?report_type=artwork");
	}
	
function open_artwork_report_one(refID) {
	window.open("./resources/reports.php?report_type=artwork&refID=" + refID);
	}
	
function open_titlecards_report(arrayOfRefIDs) {
	window.open("./resources/reports.php?report_type=titlecards&refIDs=" + arrayOfRefIDs);
	}
	
function open_locations_report_all() {
	window.open("./resources/reports.php?report_type=locations");
	}
	
function open_locations_report_one(refID) {
	window.open("./resources/reports.php?report_type=locations&refID=" + refID);
	}
	
function open_lots_report_all() {
	window.open("./resources/reports.php?report_type=lots");
	}
	
function open_lots_report_one(refID) {
	window.open("./resources/reports.php?report_type=lots&refID=" + refID);
	}
	
/* The following validation form has many uses.
In some cases there are multiple submit buttons. To test for a specific submit button pressed, we assign that button object to the whichButton variable (onclick attached to the button) and then call the form_validation handler from the form (onsubmit attached to the form). */

var whichButton; // whichButton is a global variable assigned a submit button object and assigned by certain submit buttons

function form_validation(theForm) {
	if (theForm.id == 'new_registration_form' || theForm.id == 'member_profile_form') {
		// reset any red labels
		var arrayOfFieldLabels = ['name_label', 'e_mail_label', 'password_label', 'password_again_label'];
		for (var i=0; i<arrayOfFieldLabels.length; i++) {
			with (document.getElementById(arrayOfFieldLabels[i])) {
				style.color = '#000000';
				style.fontWeight = 'normal';
				}	
			}
		// check for empty fields
		var flag;
		var arrayOfRequiredFields = ['name', 'e_mail', 'password', 'password_again'];
		for (var i=0; i<arrayOfRequiredFields.length; i++) {
			if (!theForm.elements[arrayOfRequiredFields[i]].value) {
				with (document.getElementById(arrayOfRequiredFields[i] + '_label')) {
					style.color = '#FF0000';
					style.fontWeight = 'bold';
					}
				flag = 1;
				}
			}
		if (flag) {
			alert('All fields are required. Please provide complete information where indicated.');
			return false;
			}
		// check for a valid e-mail
		flag = 0;
		var e_mail = theForm.e_mail.value;
		var allowedChars = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
		for(i=0; i<e_mail.length; i++) {
			if(allowedChars.indexOf(e_mail.charAt(i)) < 0) { flag = 1; break; }	
			} 
		var regex_a = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		var regex_b = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (e_mail.match(regex_a) && !e_mail.match(regex_b)) { flag = 1; }
		if (flag) {
			with (document.getElementById('e_mail_label')) { style.color = '#FF0000'; style.fontWeight = 'bold'; }
			alert('That is not a valid UserID (e-mail address). Please provide your primary e-mail address.');
			theForm.e_mail.focus();
			return false;
			}
		// check that the password and password_again are the same
		if (theForm.password.value != theForm.password_again.value) {
			with (document.getElementById('password_label')) { style.color = '#FF0000'; style.fontWeight = 'bold'; }
			with (document.getElementById('password_again_label')) { style.color = '#FF0000'; style.fontWeight = 'bold'; }
			alert('The Password and Password (again) do not match.');
			theForm.password.focus();
			return false;
			}
		// if we made it this far.
		return true;
		}
	else if (theForm.id == 'forgotten_password_form') {
		// reset any red labels
		with (document.getElementById('e_mail_label')) {
			style.color = '#000000';
			style.fontWeight = 'normal';
			}
		// check for empty fields
		var flag;
		if (!theForm.elements['e_mail'].value) {
			with (document.getElementById('e_mail_label')) {
				style.color = '#FF0000';
				style.fontWeight = 'bold';
				}
			flag = 1;
			}
		if (flag) {
			alert('The UserID (primary password) field is required. Please provide complete information where indicated.');
			return false;
			}
		// check for a valid e-mail
		flag = 0;
		var e_mail = theForm.e_mail.value;
		var allowedChars = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
		for(i=0; i<e_mail.length; i++) {
			if(allowedChars.indexOf(e_mail.charAt(i)) < 0) { flag = 1; break; }	
			} 
		var regex_a = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		var regex_b = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (e_mail.match(regex_a) && !e_mail.match(regex_b)) { flag = 1; }
		if (flag) {
			with (document.getElementById('e_mail_label')) { style.color = '#FF0000'; style.fontWeight = 'bold'; }
			alert('That is not a valid UserID (e-mail address). Please provide your primary e-mail address.');
			theForm.e_mail.focus();
			return false;
			}
		return true;
		}
	else if (theForm.id == 'artwork_form' || theForm.id == 'new_artwork_form') {
		// on create or update buttons check for empty fields
		if (whichButton.value == 'Update' || whichButton.value == 'Update and Return' || whichButton.value == 'Create New Artwork Record' || whichButton.value == 'Create New Record and Return' ) {
			var flag;
			var arrayOfRequiredFields = ['title'];
			for (var i=0; i<arrayOfRequiredFields.length; i++) {
				if (!theForm.elements[arrayOfRequiredFields[i]].value) {
					with (document.getElementById(arrayOfRequiredFields[i] + '_label')) {
						style.color = '#FF0000';
						style.fontWeight = 'bold';
						}
					flag = 1;
					}
				}
			if (flag) {
				alert('Some fields are required. Please provide complete information where indicated.');
				return false;
				}
			}
			
		// delete confirmation  Note: whichButton is a global set by the delete submit button
		if (whichButton.value == 'Delete') {
			whichButton = null; // clear the whichButton variable
			answer = confirm('You are about to delete the database record for \'' + theForm.title.value + '\'. Are you sure?');
			return answer;
			}
		}
	else if (theForm.id == 'locations_form') {
		// on create or update buttons check for empty fields
		if (whichButton.value == 'Update' || whichButton.value == 'Update and Return' || whichButton.value == 'Create New Location Record' || whichButton.value == 'Create New Record and Return' ) {
			var flag;
			var arrayOfRequiredFields = ['location'];
			for (var i=0; i<arrayOfRequiredFields.length; i++) {
				if (!theForm.elements[arrayOfRequiredFields[i]].value) {
					with (document.getElementById(arrayOfRequiredFields[i] + '_label')) {
						style.color = '#FF0000';
						style.fontWeight = 'bold';
						}
					flag = 1;
					}
				}
			if (flag) {
				alert('Some fields are required. Please provide complete information where indicated.');
				return false;
				}
			}
			
		// delete confirmation  Note: whichButton is a global set by the delete submit button
		if (whichButton.value == 'Delete') {
			whichButton = null; // clear the whichButton variable
			answer = confirm('You are about to delete the database record for \'' + theForm.location.value + '\'. Are you sure?');
			return answer;
			}
		}
	else if (theForm.id == 'lots_form') {
		// on create or update buttons check for empty fields
		if (whichButton.value == 'Update' || whichButton.value == 'Update and Return' || whichButton.value == 'Create New Lot Record' || whichButton.value == 'Create New Record and Return' ) {
			var flag;
			var arrayOfRequiredFields = ['lot_name'];
			for (var i=0; i<arrayOfRequiredFields.length; i++) {
				if (!theForm.elements[arrayOfRequiredFields[i]].value) {
					with (document.getElementById(arrayOfRequiredFields[i] + '_label')) {
						style.color = '#FF0000';
						style.fontWeight = 'bold';
						}
					flag = 1;
					}
				}
			if (flag) {
				alert('Some fields are required. Please provide complete information where indicated.');
				return false;
				}
			}
			
		// delete confirmation  Note: whichButton is a global set by the delete submit button
		if (whichButton.value == 'Delete') {
			whichButton = null; // clear the whichButton variable
			answer = confirm('You are about to delete the database record for \'' + theForm.lot_name.value + '\'. Are you sure?');
			return answer;
			}
		}
	return;
	}