﻿$(document).ready(function(){

if((document.title.indexOf('<') !=-1))
	document.title =document.title.substr(0,document.title.indexOf('<'));
  
});

function doCourseSrch()
{
	var srchterm = jQuery.trim( $('#coursefind').val());
	var srchtype = jQuery.trim($('input[name=ctype]:checked').val());
	if(srchterm == '')
	{
		//$.prompt('Please provide a search term for Course Search');
		return false;
	}	
	if (srchtype == '')
	{
		//$.prompt('Please select your search type, Local or International');
		return false;
	}
	
/* SP - SharePoin custom search implemented no need to route to old website search
	var res=location.protocol+ '//'+ location.host + '/search/results.aspx?k='+srchterm +'&s='+srchtype;
*/	
	
/*	if (srchtype.toLowerCase() != 'international')
	{
	 	res +=  '&s=Short Course';
	}
*/
	
/* SP - SharePoin custom search implemented no need to route to old website search
	if (srchtype.toLowerCase() != 'international')
	{
	 	res =  location.protocol+ '//'+ 'enrolment.bhtafe.edu.au/dynamic/dynamic.cfm?fuseaction=dynamic.searchStudyAreas&studyareaID=0&SearchString=' + srchterm + '&OnlyIntl=0&SearchPageType=course information,short course information';  
	}
	else
	{
	 	res =  location.protocol+ '//'+ 'enrolment.bhtafe.edu.au/dynamic/dynamic.cfm?fuseaction=dynamic.searchStudyAreas&studyareaID=0&SearchString=' + srchterm + '&OnlyIntl=1&SearchPageType=course information';  
	}
*/	
	
	if (srchtype.toLowerCase() != 'international')
	{
	 	srchtype = 'All';
	}
	else
	{
		// Set the international scope, the idxst=2 url parameter is to enable selection of the Advanced Search dropdown
		srchtype = 'International&idxst=2';
	}
	
	
	
	var res=location.protocol+ '//'+ location.host + '/courses/pages/coursesearchresults.aspx?wd='+srchterm +'&st='+srchtype;

	window.location = res;
	 
}


function dotreeviewpre(node)
{
//	$(node).treeview({collapsed: true, unique: true, prerendered: true});
	$(node).treeview({collapsed: true, unique: true});
	$(node).show();
}


function doGlobalSrch()
{
	var srchstr = jQuery.trim( $("#gsearchboxtxt").val());
	var srchtype = jQuery.trim($('input[name=rb_CourseSearch]:checked').val());

	if(srchstr == '')
	{
		//$.prompt('Please provide a search term for Global Search');
		return false;
	}
	
//	if (srchtype == 'Courses')
//	{
		//Custom Seacrh built on SharePoint, no need to route to old site
		//window.location=location.protocol+ '//'+ 'enrolment.bhtafe.edu.au/dynamic/dynamic.cfm?fuseaction=dynamic.searchStudyAreas&studyareaID=0&SearchString=' + srchstr + '&SearchPageType=course information,short course information';    
//		window.location=location.protocol+ '//'+ location.host + '/courses/pages/coursesearchresults.aspx?wd='+srchstr;		
//	}
//	else
//	{
		window.location=location.protocol+ '//'+ location.host + '/search/results.aspx?k='+srchstr + '&wd=' + srchstr;
//	}
	
	return false;
}


function handleEnter(e, buttonid)
{
	if (e.which || e.keyCode) {
		if ((e.which == 13) || (e.keyCode == 13)) {
			document.getElementById(buttonid).click();
			return false;
		}
	} 
}

function convertToPageName(textToConvert) {
	// replace spaces with dash
	var converted = textToConvert.replace(/ /g, '-');
	// strip commas
	converted = converted.replace(/,/g, '');
	
	return converted.toLowerCase();		
}

function updateCourseCentreLink(container) {
	var text = container.html();
	var link = '/visitors/pages/' + convertToPageName(text) + '.aspx';
	
	container.html('<a href="' + link + '">' + text  + '</a>');
}

function updateCourseLocationLink(container) {
	var text = container.html();

	if (text.match(/,$/)) {
		// trim last comma
		text = text.substring(0, text.length - 1);
	}
	var locations = text.split(',');
	var item, link, result = '';
	
	try {
		for (var i=0; i<locations.length; i++) {
			item = locations[i];
			pagename =  convertToPageName(item);
			
			if (pagename == 'elgar-campus' || 
				pagename == 'nelson-campus' ||
				pagename == 'whitehorse-campus') {
				
				link = '/visitors/pages/' + pagename + '.aspx';
				result += '<a href="' + link + '">' + item + '</a>';
			}
			else {
				result += item;
			}					

			if (i < (locations.length - 1)) {
				result += ', ';
			} 					
		}
		
		container.html(result);
	}
	catch (err) {
	}

}
