/* FORM-VALIDATION FUNCTIONS */
/* -------------------------------- */
	
	//specifies the background-color of erroneous input fields
	var alert_color = '#f1bcc9';
	
	//trims whitespace around a string
	//RETURNS: string
	function trim(str) {
	return str.replace(/^\s+|\s+$/g, '')
	}

	//simply checks if a string is empty or not
	//RETURN: boolean
	function mValidateEmpty(vString) {
		if(trim(vString.value) == '') {
			vString.style.backgroundColor = alert_color;
			return false;
		} else {
			vString.style.backgroundColor = '#ffffff';
			return true;
		}
	}
	
	
	//checks if given email address is in correct name@domain.com format
	//RETURN: boolean
	function mValidateEmail(vString) {
		var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
		if (!email.test(trim(vString.value))) {
			vString.style.backgroundColor = alert_color;
			return false;
		} else {
			vString.style.backgroundColor = '#ffffff';
			return true;
		}
	}
	
	//returns false if user selected first item (default empty select field)
	//RETURN: boolean
	function mValidateSelect(vString) {
		if(vString.selectedIndex == 0) {
			vString.style.backgroundColor = alert_color;
			return false;
		} else {
			vString.style.backgroundColor = '#ffffff';
			return true;
		}
	}
	
	//checks equality of two strings (for email address, passwords, etc.)
	//RETURN: boolean
	function mValidateRepeat(vString1, vString2) {
		if(trim(vString1.value) != trim(vString2.value)) {
			vString1.style.backgroundColor = alert_color;
			vString2.style.backgroundColor = alert_color;
			return false;
		} else {
			return true;
		}
	}
	
	//returns false if no selection was made for a set of radio buttons
	//RETURN: boolean
	function mValidateRadio(vString) {
		for(var i=0;i<vString.length;i++) {
			if(vString[i].checked == true) {
				return true;
			}
		}
		return false;
	}
	
	
	//checks if a selection has been made
	//RETURN: boolean
	function checkSelect(field, what) {
		if(document.getElementById(field).selectedIndex == 0) {
			alert('Please choose '+what+".");
			return false;
		} else {
			return true;
		}
	}
	
	function confirmDelete() {
		var agree=confirm("Are you sure you want to delete this client? This removes the client from the database and deletes all files associated with them. This is NOT undoable.");
		if (agree)
			return true ;
		else
			return false ;
	}
	
	//confirms whether or not the user wants to submit the form
	//RETURN: boolean
	function confirmSubmit(what, who) {
		var agree=confirm("Are you sure you want to "+what+" "+who+"?");
		if (agree)
			return true ;
		else
			return false ;
	}

/* PAGE-SPECIFIC CODE */
/* -------------------------------- */

	function mValidateForm(form) {
		var errs = 0;
		if(!mValidateRadio(form.q1)) errs += 1;
		if(!mValidateRadio(form.q2)) errs += 1;
		if(!mValidateRadio(form.q3)) errs += 1;
		if(!mValidateRadio(form.q4)) errs += 1;
		if(!mValidateRadio(form.q5)) errs += 1;
		if(!mValidateRadio(form.q6)) errs += 1;
		if(!mValidateRadio(form.q7)) errs += 1;
		if(!mValidateRadio(form.q8)) errs += 1;
		if(!mValidateRadio(form.q9)) errs += 1;
		if(!mValidateRadio(form.q10)) errs += 1;
		if(!mValidateRadio(form.q11)) errs += 1;
		if(errs == 0) {
			return true;	
		} else {
			alert("Please enter all the required information before sending.");
			return false;
		}
	}
	
	function mValidateFileUpload(form) {
		if(!mValidateEmpty(form.file)) {
			alert("Please select a file to upload.")
			return false;
		}
		if(!checkSelect('client', 'a client to associate this file with')) {
			return false;
		}
		return true;
	}
	
	function mValidateClientAdd(form) {
		if(!mValidateEmpty(form.name)) {
			alert("Please select a name for this client.")
			return false;
		}
		if(!mValidateEmpty(form.shortname)) {
			alert("Please select a shortname for this client.")
			return false;
		}
		if(!mValidateEmpty(form.username)) {
			alert("Please select a username for this client.")
			return false;
		}
		if(!mValidateEmpty(form.password)) {
			alert("Please select a password for this client.")
			return false;
		}
		return true;
	}
	








	function validateContactFormFields(_f)
	{
		if(_f.name.value == "")
		{
			alert("Please enter a Name.");
			_f.name.focus();
			return(false);
		}
		if(_f.subject.value == "")
		{
			alert("Please enter a Subject.");
			_f.subject.focus();
			return(false);
		}
		
		if(_f.email.value == "")
		{
			alert("Please enter your Email Address.");
			_f.email.focus();
			return(false);
		}
	   if(!mValidateEmail(_f.email))
		{
			alert("Please enter a Valid Email Address.");
			_f.email.focus();
			return(false);
		}
		else if(_f.message.value == "")
		{
			alert("Please enter Message.");
			_f.message.focus();
			return(false);
		}
		
		
			return(true);
	}
