// JavaScript Document



// Radio Button Validation
// copyright Stephen Chapman, 15th Nov 2004,14th Sep 2005 
// you may copy this function but please keep the copyright notice with it 

function valButton(btn) {
	var cnt = -1; 
	for (var i=btn.length-1; i > -1; i--) {
		if (btn[i].checked) {cnt = i; i = -1;}
	}
	if (cnt > -1) return btn[cnt].value;
	else return null;
}

function valForm(formArray) {
	//alert(document.pick_winner.section.type);
	if (document.pick_winner.section.type != 'hidden') {
		var btn = valButton(document.pick_winner.section);
		if (btn == null) {
			alert('Please select a course/section.'); 
			return false;
		} else {
			//alert('Button value ' + btn + ' selected'); 
			return true;
		}
	}
}


//--------------------------------------------------//
//////////   function dumpArray()    /////////////////
//--------------------------------------------------//

function dumpArray(yourarray) {
	var msg = "array contents\n";
	for(i=0;i<yourarray.length;i++) { msg += yourarray[i] + "\n"; }
	
	alert(msg); 
}



//--------------------------------------------------//
//////////////// function isValidEmail()  ////////////////
//--------------------------------------------------//

function isValidEmail($address) {
	$pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/
	if ( $pattern.test($address) ) {
		// alert("email OK");
		return true;
	} else {
		// alert("You email address appears to be invalid.");
		return false;
	}
}

//--------------------------------------------------//
/////////// function validWinnerPickForm()  //////////
//--------------------------------------------------//
function validWinnerPickForm() {

var $errors = new Array(); // num. array, where values are problem fld labels
var $counter = 0;

// single radio buttons leave length 'undefined', so set to 1
if (isNaN(document.pick_winner.section.length)) {
	document.pick_winner.section.length = 1;
}
//alert(document.pick_winner.section.length); // debug
// check for rating of use of examples and illustrations
	var $section = -1;
	for (i=1; i<document.pick_winner.section.length; i++) {
		var radioSelect = document.getElementById('section' + i);
		if (radioSelect.checked) {
			$section = i;
			break; // selection determined; exit loop.
		}
	}
alert(radioSelect.checked); // debug
	if ($section == -1) { // if no selection was found
		$errors[$counter++] = "Select a course/section";
	}
	
	// if the array $errors is not empty, then print error message
	// and loop through array to print the labels of field where problems occurred.
	if ($errors.length > 0) {
		var $errMssg = "The form is incomplete and was not submitted. \n";
		$errMssg += "Please complete the following questions:  \n\n";
		for (i=0; i<$errors.length; i++) {
			$errMssg += (i+1) + ".\t" + $errors[i] + "\n";
		}
		
		alert ($errMssg);
		return false;
	} else {
		return true;
	}

}






//--------------------------------------------------//
///////////// function validateMailForm()  //////////////
//--------------------------------------------------//
function validMailForm() {
	
	var $errors = new Array(); // num. array, where values are problem fld labels
	var $counter = 0;

	// Text field 'Name'
	if (document.forms['mailform'].Name.value.length > 0 ) {
		var $Name = true;
	} else {
		var $Name = false;
		$errors[$counter++] = "Name";
	}

	// Text field 'Email'
	if (document.forms['mailform'].Email.value.length > 0 && isValidEmail(document.forms['mailform'].Email.value)) {
		var $email = true;
	} else {
		var $email = false;
		$errors[$counter++] = "Email Address";
	}

	
	// Text field 'Subject'
	if (document.forms['mailform'].Subject.value.length > 0 ) {
		var $Subject = true;
	} else {
		var $Subject = false;
		$errors[$counter++] = "Subject";
	}
	
	// Text field 'Comments'
	if (document.forms['mailform'].Comments.value.length > 0 ) {
		var $Comments = true;
	} else {
		var $Comments = false;
		$errors[$counter++] = "Comments";
	}
	
	//alert($errors.toString()); // debug
	// if the array $errors is not empty, then print error message
	// and loop through array to print the labels of field where problems occurred.
	if ($errors.length > 0) {
		var $errMssg = "Please check the following for errors or omissions:  \n\n";
		for (i=0; i<$errors.length; i++) {
			$errMssg += (i+1) + ".  " + $errors[i] + "\n";
		}
		
		alert ($errMssg);
		return false;
	} else {
		return true;
	}
}


//--------------------------------------------------//
///////////// function validateForm1()  //////////////
//--------------------------------------------------//
function validateEvalForm() {

	// find the selection for contact Yes/No
	var $contact
	for ($i=0; $i<document.feedback.contact.length; $i++) {
		if (document.feedback.contact[$i].checked == true) {
			$contact = document.feedback.contact[$i].value;
			break; // selection determined; exit loop.
		}
	}
	
	// alert($contact); // debug
	// if user agrees to be contacted, check email address for validity

	// The following boolean first checks to see if contact is "yes," then validates the email address
	if ($contact == "Yes") {
		// if email address field is empty or address is no valid, set error var
		if (document.feedback.email.value.length == 0 || !isValidEmail(document.feedback.email.value)) {
			var $error = "invalid";
		}
	}



	// if $error, then print error message and halt form submission
	if ($error == "invalid") {
		var $errMssg = "Sorry, the email address appears to be invalid. ";
		$errMssg += "Please check the address and resubmit.";
		alert ($errMssg);
		return false;
	} else {
		return true; // allow form to be submitted 
	}

}


//--------------------------------------------------//
//////////// function disableField()  //////////////
//--------------------------------------------------//

function disableField(calling_elem, gray_value, target) {
	// alert('calling element = ' + calling_elem +', current value = ' + calling_elem.value +', gray_value = ' + gray_value +', target = ' + target);
	// Note that var 'calling_elem' is already an obj reference, created using 'this' in function call
	// var 'target,' on the other hand, is only a text string that requires conversion to obj reference.
	var targetID = document.getElementById(target);
	
	// check whether radio button or checkbox
	if (calling_elem.type == "radio") {
	
		if (calling_elem.value != gray_value) {
				targetID.disabled = true;
		} else {
				targetID.disabled = false;
				targetID.focus();
		}
				
	} else if (calling_elem.type == "checkbox") { // element is a checkbox
		
		if (calling_elem.checked != true) {
				targetID.disabled = true;
		} else {
				targetID.disabled = false;
				targetID.focus();
		}
	
	}
	
}


//--------------------------------------------------//
////////////// function emptyField()  ////////////////
//--------------------------------------------------//

function emptyField(elementID) {
	var x = elementID;
	//alert(x);
	document.feedback.comments52.value = "";
}


//--------------------------------------------------//
///////////// function validateForm2()  //////////////
//--------------------------------------------------//

function validateForm2() {
	var $errors = new Array(); // num. array, where values are problem fld labels
	var $counter = 0;
	
	// Text field 'session_name'
	if (document.feedback.session_date.value.length > 0) {
		var $session_date = true;
	} else {
		var $session_date = false;
		$errors[$counter++] = "Session Date";
	}

// check for rating of Overall Session
	var $session_quality = -1;
	for ($i=0; $i<document.feedback.session_quality.length; $i++) {
		if (document.feedback.session_quality[$i].checked) {
			$session_quality = $i;
			break; // selection determined; exit loop.
		}
	}
	if ($session_quality == -1) { // if no selection was found
		$errors[$counter++] = "Quality of overall session";
	}
	
// check for rating of Presentation Quality
	var $present_quality = -1;
	for ($i=0; $i<document.feedback.present_quality.length; $i++) {
		if (document.feedback.present_quality[$i].checked) {
			$present_quality = $i;
			break; // selection determined; exit loop.
		}
	}
	if ($present_quality == -1) { // if no selection was found
		$errors[$counter++] = "Quality of content presented";
	}
	
// check for rating of Presenter's Knowledge
	var $knowledge = -1;
	for ($i=0; $i<document.feedback.knowledge.length; $i++) {
		if (document.feedback.knowledge[$i].checked) {
			$knowledge = $i;
			break; // selection determined; exit loop.
		}
	}
	if ($knowledge == -1) { // if no selection was found
		$errors[$counter++] = "Presenter's knowledge of the topic";
	}
	
// check for rating of Presenter's Delivery
	var $delivery = -1;
	for ($i=0; $i<document.feedback.delivery.length; $i++) {
		if (document.feedback.delivery[$i].checked) {
			$delivery = $i;
			break; // selection determined; exit loop.
		}
	}
	if ($delivery == -1) { // if no selection was found
		$errors[$counter++] = "Presenter's delivery of content";
	}
	
// check for rating of Response to questions/comments
	var $questions = -1;
	for ($i=0; $i<document.feedback.questions.length; $i++) {
		if (document.feedback.questions[$i].checked) {
			$questions = $i;
			break; // selection determined; exit loop.
		}
	}
	if ($questions == -1) { // if no selection was found
		$errors[$counter++] = "Response to questions/comments";
	}
	
// check for rating of use of examples and illustrations
	var $examples = -1;
	for ($i=0; $i<document.feedback.examples.length; $i++) {
		if (document.feedback.examples[$i].checked) {
			$examples = $i;
			break; // selection determined; exit loop.
		}
	}
	if ($examples == -1) { // if no selection was found
		$errors[$counter++] = "Use of examples and illustrations";
	}
	
// check for rating of use of how session met goals
	var $met_goals = -1;
	for ($i=0; $i<document.feedback.met_goals.length; $i++) {
		if (document.feedback.met_goals[$i].checked) {
			$met_goals = $i;
			break; // selection determined; exit loop.
		}
	}
	if ($met_goals == -1) { // if no selection was found
		$errors[$counter++] = "Met goals";
	}
	
// check for rating of interest in follow-up sessions
	var $followup_sessions = -1;
	for ($i=0; $i<document.feedback.followup_sessions.length; $i++) {
		if (document.feedback.followup_sessions[$i].checked) {
			$followup_sessions = $i;
			break; // selection determined; exit loop.
		}
	}
	if ($followup_sessions == -1) { // if no selection was found
		$errors[$counter++] = "Interest in follow-up session(s)";
	}
	
// check for rating of interest in related topics
	var $related_topics = -1;
	for ($i=0; $i<document.feedback.related_topics.length; $i++) {
		if (document.feedback.related_topics[$i].checked) {
			$related_topics = $i;
			break; // selection determined; exit loop.
		}
	}
	if ($related_topics == -1) { // if no selection was found
		$errors[$counter++] = "Interest in related topics";
	}
        
		
//alert($errors.toString()); // debug
	// if the array $errors is not empty, then print error message
	// and loop through array to print the labels of field where problems occurred.
	if ($errors.length > 0) {
		var $errMssg = "The form is incomplete and was not submitted. \n";
		$errMssg += "Please complete the following questions:  \n\n";
		for (i=0; i<$errors.length; i++) {
			$errMssg += (i+1) + ".\t" + $errors[i] + "\n";
		}
		
		alert ($errMssg);
		return false;
	} else {
		return true;
	}

}

