/*  Banner Course Search Box - Future Students
 	Used to handle the form validation of the top banner course search / site search box on the future students page 
	
	Updated: Mon 07.03.11 - Use submit form lines 58 and 66
    Updated: Tue 10.02.09
    Updated: Mon 16.02.09 - lines 50 & 51 inserted by AN
	Updated: Mon 25.02.09 - updated line 51 & 52 so it validates CR
	Updated: Mon 24.08.09 - updated so if google code exists, track keywords / updated to point to coursefinder rather than rss feed results. CR
	Updated: Wed 17.03.10 - Updated to include new google code
   ----------------------------------------------------------------*/

$(document).ready(function(){
						   	
	//Switch Tabs
	var lis = $('#search-switcher li a');
	lis.each(function(i, el) {
		var id = el.hash.replace('#', '');
		$(el).click(function(e) {
			$('#search-switcher li a').removeClass('selected');	
			$(el).addClass('selected');
			
			$('#search .search-tab').css({'display':'none'});
			$('#' + id).css({'display':'block'});
			e.preventDefault();
		});
	});
	
	
	
	//Validate Course Search
	$('#hd-search-courses').submit(function(e) {
												
		var n = $('input:radio:checked','#hd-search-courses').length; 
		var error = 'noerror';
		
		if (n == 0) {
			error = "Please select local or international student"
		}
		
		var q = $('#txtSearch').val();
		var analyticscategory = $('#analytics-category').val();
		
		//If no search term
		if (q.length == 0 || q == undefined || q == null) {
			error = "Please enter a course name"
		}
		
		if (error == 'noerror') {
			var region = $('input:radio:checked','#hd-search-courses').val(); 
			e.preventDefault();
			
			//Succcess - submit form
			if (region == "local") {
				
				//Track search terms through Google Analytics, if tracking code exists
				pageTracker._trackEvent(analyticscategory, 'Course Search', 'Local - ' + q);
				//document.location = "http://courses.swinburne.edu.au/courses/BrowseCourse.aspx?KeywordSearch=" + q + "&Populate=true";
				document.getElementById("hd-search-courses").submit();

		
			} else {
				
				//Track search terms through Google Analytics, if tracking code exists
				pageTracker._trackEvent(analyticscategory, 'Course Search', 'International - ' + q);
				//document.location = "http://courses.swinburne.edu.au/courses/BrowseCourseIntl.aspx?KeywordSearch=" + q + "&Populate=true";
				document.getElementById("hd-search-courses").submit();
			}
			
		} else {
			e.preventDefault();
			alert(error);
		}	
	});
	 
	 
	//Valiate Site Search
	$('#hd-search-site').submit(function(e) {
		var n = $('input:radio:checked','#hd-search-site').length; 
		var error = '';
		if (n == 0) {
			error = "Please select which part of the website you'd like to search"
		}
		var q = $('#q').val();
		if (q.length == 0 || q == undefined || q == null) {
			error = "Please enter a search query"
		}
		
		var analyticscategory = $('#analytics-category').val();
		
		if (error == '') {
			e.preventDefault();
			
			var cx = $('input:radio:checked','#hd-search-site').val(); 
			var cof = $('#cof').val();
			var ie = $('#ie').val();
			
			if (cx == '008801079654556728799:l2vd_fittx8') {
				var searchoption = 'all-swinburne';
				var cx_site_page = "http://www.swinburne.edu.au/search/results/";
			} else {
				var searchoption = 'site-specific';
				var cx_site_page = $('#cx_site_page').val();
			}
			
			//Track search terms through Google Analytics, if tracking code exists
			
			pageTracker._trackEvent(analyticscategory, 'Site Search', searchoption + ' - ' + q);
			document.location = cx_site_page + "?cx=" + cx + "&cof=" + cof + "&ie=" + ie + "&q="+ q +"&sa.x=0&sa.y=0&sa=Search";	
			//succcess - submit form
		} else {
			e.preventDefault();
			alert(error);
		}	
	});
	
});
