/**
 * The old validation code. I've added some jQuery to the bottom to bind the
 * validation code to the form.
 */

function validate_RequiredField (Value) {
	if (Value=="" | Value==null) {
		return false;
	} else {
		return true;
	}
}

function validate_RequiredNumber(Value) {
	if (Value == "" | isNaN (Value) | !(Value.indexOf(" ", 0) == -1)) {
		return false;
	} else {
		return true;
	}
}

function validate_form() {

	var error_found = false;
	var error_msg = "";
	
	if(!validate_RequiredField(document.texts.unit.value)){ //Check that unit number has been entered
	  document.texts.unit.focus();
	  error_msg += "Error: Unit code not entered" + '\n';
	  error_found = true;
	}
	if(!validate_RequiredField(document.texts.unitTitle.value)){ //Check that unti title has been entered
	  document.texts.unitTitle.focus();
	  error_msg += "Error: Unit title not entered" + '\n';
	  error_found = true;
	}
	if(!validate_RequiredField(document.texts.extension.value)){ //check that extension phone number has ben entered
	  document.texts.extension.focus();
	  error_msg += "Error: Extension not entered" + '\n';
	  error_found = true;
	}
	var email = document.texts.email.value;
	if(!validate_RequiredField(email)){ //Check that email address has been entered
	  document.texts.email.focus();
	  error_msg += "Error: Email not entered" + '\n';
	  error_found = true;
	}
	
	if (!email.match(/deakin\.edu\.au$/)) {
	  document.texts.email.focus();
	  error_msg += "Error: Email must be a valid Deakin email address ending in @deakin.edu.au." + '\n';
	  error_found = true;
	}
	
	if (document.texts.campusM.checked && !(validate_RequiredField(document.texts.enrolM.value))){
		document.texts.enrolM.focus();
		error_msg += "Error: You have indicated that the unit runs at Melbourne but have not indicated enrolments" + '\n';
		error_found = true;
	}
	if (document.texts.campusG.checked && !(validate_RequiredField(document.texts.enrolG.value))){
	    document.texts.enrolG.focus();
	    error_msg += "Error: You have indicated that the unit runs at Waurn Ponds but have not indicated enrolments" + '\n';
	    error_found = true;
    }
	if (document.texts.campusF.checked && !(validate_RequiredField(document.texts.enrolF.value))){
		document.texts.enrolF.focus();
		error_msg += "Error: You have indicated that the unit runs at Waterfront but have not indicated enrolments" + '\n';
		error_found = true;
    }
	if (document.texts.campusW.checked && !(validate_RequiredField(document.texts.enrolW.value))){
		document.texts.enrolW.focus();
		error_msg += "Error: You have indicated that the unit runs at Warrnambool but have not indicated enrolments" + '\n';
		error_found = true;
    }
	if (document.texts.campusX.checked && !(validate_RequiredField(document.texts.enrolX.value))){
		document.texts.enrolX.focus();
		error_msg += "Error: You have indicated that the unit runs Off Campus but have not indicated enrolments" + '\n';
		error_found = true;
    }
	if(!document.texts.campusM.checked && !document.texts.campusG.checked && !document.texts.campusF.checked && !document.texts.campusW.checked && !document.texts.campusX.checked && !document.texts.noBooks.checked){ //Check that a campus has been selected
		document.texts.campusM.focus();
		error_msg += "Error: No campus is selected" + '\n';
		error_found = true;
	}
	
	//if noBooks checkbox hasn't been ticked
	if(!document.texts.noBooks.checked) {
		var numSelectedTexts = 0;
		//loop through all prescribed select lists and if something is selected check if coreesponding author & title are entered
		for (i=1; i <= 15; i++) {			
			obj = eval('document.texts.prescribed' + i);
			textType = obj[obj.selectedIndex].value;
			//grab corresponding values for text type from author & title fields
			author = eval('document.texts.author' + i + '.value');
			title = eval('document.texts.title' + i + '.value');

			//if user has selected an option from the text type drop down list
			if(textType != 'No selection made') {
				numSelectedTexts++;
				//if either the author or title are empty.
				if( author == '' ){
					error_msg += 'Error: No author entered for ' + textType + ' Text ' + i + '\n';
					error_found = true;
				} 
				if ( title == '' ){
					error_msg += 'Error: No title entered for ' + textType + ' Text ' + i + '\n';
					error_found = true;
				}
			}
			//if no selection has been made check that there are no corresponding author & title entries as user may have unselected a text type
			else if(textType == 'No selection made') {
				if( author != '' ){
					error_msg += 'Error: You have entered an author for text number ' + i + ' but no Text Type is selected\n';
					error_found = true;
				} 
				else if ( title != '' ){
					error_msg += 'Error: You have entered a title for text number ' + i + ' but no Text Type is selected\n';
					error_found = true;
				}				
			}			
		}
		if (numSelectedTexts == 0) {
			error_msg += 'Error: You must enter the details for at least one text if the \'No books required\' checkbox is not ticked\n';
			error_found = true;			
		}
	}
    
	if(error_found){
		error_msg = "The Textbooks info cannot be submitted because of the following errors:" + '\n\n' + error_msg;
		alert(error_msg);
		return false;
	}
	else {//All fields are good
		return true;
	}
}

// Use jQuery to bind the above function to the form -- yuck!
$(document).ready(function () {
	$('a[href=#submit]').click(function (e) {
		e.preventDefault();
		
		if (validate_form()) {
			$('#adoption-list-form').submit();
		}
	});
});
