/*  Course Search Box
 
 	Used to handle the form validation of course search box. 
	
    Updated: Mon 24.11.08
   ----------------------------------------------------*/

$(document).ready(function(){
	
	//Error Messages and Links
	var error_radio = "<a href='#txtSearch' id='error-student' hidefocus='true'>Please select local student or international student</a>";
	var error_input = "<a href='#txtSearch' id='error-input' hidefocus='true'>Please enter a course name</a>";	

	//On submit, call validateHomeSearch function	
	$("form#coursesearch-form").submit(function(){
		validateHomeSearch(error_radio, error_input);
		return false;
	});
	
	//On click of Local or International Student, call showHideStudentValidation function
	$("#radLocal").click(function(){
		showHideStudentValidation(error_radio, error_input);
	});
	
	$("#radInternational").click(function(){
		showHideStudentValidation(error_radio, error_input);
	});
	
	//On click of error message, focus on keyword textfield
	$("#search_error_msg").click(function(){
		$("#txtSearch").focus();
		return false;
	});
	
});


/*  function validateHomeSearch()
 
 	This function validates the course search box. Displaying an error message fields are missing. I
	
 	parameters: error_radio - Error messagae for not selecting a student type. 
	parameters: error input - Error message for not entering a keyword
   ----------------------------------------------------*/ 
   
function validateHomeSearch(error_radio, error_input) {
	
	var error = false; //error message to be shown to user

	//Collect form values
	var local = document.getElementById("radLocal");
	var international = document.getElementById("radInternational");
	var keywordToSearchFor = document.getElementById("txtSearch").value;
	
	//Confirm user has selected local|international radio button
	var region = null;
 	if (local.checked) {
		region = "local";
	} else if (international.checked) {
	 	region = "int";
	}
	
	if(region == null) {
		error = error_radio;
	} else if (keywordToSearchFor == "") {
		error = error_input;
	}
	
	//Display error message and cancel form submission
	if (error != false) {
		var e = document.getElementById("search_error_msg");
		
		e.style.display = 'block';
		e.innerHTML = error;
		
		//check if div element has tabindex set, if not, set tabindex= -1 so the focus() function works
		var divtabindex = $('#search_error_msg').attr('tabindex');
		if (!divtabindex) {
			$("#search_error_msg").attr({ 
          	tabindex: "-1"
     		});
		}

		//Set focus on error message
		if (error == error_radio) {
			$("#error-student").focus();
		} else if (error == error_input) {
			$("#error-input").focus();
		}
	
	} else {
		if (region == "local") {
			//Track search terms through Google Analytics
			pageTracker._trackPageview('/home/course-search/local - ' + keywordToSearchFor);
	 		document.location = "http://courses.swinburne.edu.au/courses/BrowseCourse.aspx?KeywordSearch=" + keywordToSearchFor + "&Populate=true";
		} else {
			//Track search terms through Google Analytics
			pageTracker._trackPageview('/home/course-search/international - ' + keywordToSearchFor);
	 		document.location = "http://courses.swinburne.edu.au/courses/BrowseCourseIntl.aspx?KeywordSearch=" + keywordToSearchFor + "&Populate=true";
		}
	}
	return false;
}

/*  function showHideStudentValidation()
 
 	This function hides the error message if the user selects a radio button option.
	
 	parameters: error_radio - Error messagae for not selecting a student type. 
	parameters: error input - Error message for not entering a keyword
   ----------------------------------------------------*/ 

function showHideStudentValidation(error_radio, error_input) {
	
	var local = document.getElementById("radLocal");
	var international = document.getElementById("radInternational");
	
  	if (local.checked || international.checked) {
		var e = document.getElementById("search_error_msg");
		if (e.innerHTML == error_radio) {
			e.style.display = 'none';
			e.innerHTML = "";
		}
	}
}