﻿var minimumStrLen = -1;
var currentAwardType = "";
var cachedSearchTerm = "";
var maintimer =0;

var CourseObject;
 
/*----------------------------------------------------------------*/
function singleTimer() { if (maintimer==0) {		maintimer = setInterval("setSearchTimer()",600);	}		}
/*----------------------------------------------------------------*/
function loadcoursepage(eventelem,resource,stash) {	
	addAZLIs();
	$(stash).update("");
	setQT("");
	resetAZ();
	nofilter();
	$$('a.awardselect').each(	function(item) {
	parentItemId = $(item.parentNode.id);
	if  (parentItemId.hasClassName('awardselected') ){
			parentItemId.removeClassName('awardselected');
		}
	});
	currentAwardType = eventelem.id;
	eventelem.parentNode.addClassName('awardselected');
	new Ajax.Request(resource, {
		method:'get', 
		evalJS: 'false', 
		onCreate: function() {	
		loaderDIV = new Element('div',{'class':'loadingdiv'});
		loaderDIV.update("LOADING...");
		$('qthAZ').appendChild(loaderDIV);}
		,
		onSuccess: function(request) {
			$(stash).update("");
			var strf = "";
			var foo = request.responseText;
			CourseObject = eval('(' + foo + ')');			// Create the Course Object from the JSON source
			var dx = new Element('ul');
			for (i = 0; i < CourseObject.courses.length;i++) {
					 var d = new Element('li');
					// var c = new Element('a',{ 'class': 'foo', href: CourseObject.courses[i].url }).update(CourseObject.courses[i].title);
						 var c = new Element('a',{ 'class': 'foo', href: '#'}).update(CourseObject.courses[i].title);
					d.appendChild(c);
					 dx.appendChild(d);		
			}
			$('datastash').appendChild(dx);		// Store the HTML List in the #datastash element
  new Effect.Fade(loaderDIV,{duration:'1.4',onComplete:function(x){loaderDIV.remove();}});
  singleTimer(); 


		}
		
	});
	
 
 

}
/*-------nofilter--  ------------------------------------*/
function nofilter() {
	selection = '#datastash';
	$('resultslistitems').update("");
	$$(selection+ " > ul > li > a").each(function(x) {
		var cvar = x.innerHTML;
		$('resultslistitems').appendChild(  x.parentNode.cloneNode(true));
	});
	resetAZ();
}


//First Letter Only
function filterByFirstLetter(term) {
		var count = 0;
		$('resultslistitems').update("");
	for (i = 0; i < CourseObject.courses.length;i++) {
			if  (CourseObject.courses[i].title.toLowerCase().indexOf(term.toLowerCase()) ==0) {
					var d = new Element('li'); //common li
					//d.appendChild( new Element('a',{ href: CourseObject.courses[i].url }).update(CourseObject.courses[i].title));
										d.appendChild( new Element('a',{ href:'#' }).update(CourseObject.courses[i].title));
				 
					$('resultslistitems').appendChild(d);
					count++;
				}
	}
		 if (count == 0) {
			$('resultslistitems').update(""); // clear it
			var d = new Element('li'); //common li
			var resetLink = new Element('a',{ href: "#" }).update("no results found - click here to show all "+currentAwardType+" courses");
			resetLink.observe('click', function(event) {	setQT("");nofilter();resetAZ();}    );  
			d.appendChild( resetLink);
			$('resultslistitems').appendChild(d);
		 }
}


function resetAZ() {
$$(".az").each(function(x) { 
					 if (x.hasClassName('azSelected')) {
					 x.removeClassName('azSelected');
					 } 
});
}
function seekanddisplay(term) {
resetAZ();
var count = 0;
  $('resultslistitems').update("");
 
 
	for (i = 0; i < CourseObject.courses.length;i++) {
			if  (CourseObject.courses[i].title.toLowerCase().indexOf(term.toLowerCase()) ==0 || " "+ CourseObject.courses[i].title.toLowerCase().indexOf(" "+term.toLowerCase()) > -1 ) {
					var d = new Element('li'); //common li
					//d.appendChild( new Element('a',{ href: CourseObject.courses[i].url }).update(CourseObject.courses[i].title));
					d.appendChild( new Element('a',{ href: '#' }).update(CourseObject.courses[i].title));
					$('resultslistitems').appendChild(d);
					count++;
				}
	} 
 
 
 
 if (count == 0) {
		 if (count == 0) {
			$('resultslistitems').update(""); // clear it
			var d = new Element('li'); //common li
			var resetLink = new Element('a',{ href: "#" }).update("no results found - click here to show all "+currentAwardType+" courses");
			resetLink.observe('click', function(event) {	setQT("");nofilter();resetAZ();}    );  
			d.appendChild( resetLink);
			$('resultslistitems').appendChild(d);
		 }
 }
}

/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function setSearchTimer() {
// $('timerStateDiv').update($('timerStateDiv').innerHTML + "..run");
/* update the onscreen counter */
 var searchcounter = parseInt($('searchcount').innerHTML);
 searchcounter++;
 $('searchcount').update(searchcounter);
 
 /*
  * set fooVal to the current typed selection
  * update the searchterm field
 */
 
  	fooVal = trim(document.fooform.qt.value);
	
 if  (fooVal == "") {
		nofilter();
 }
  
 //cache - everytime the value changes, rerun the filter
if (cachedSearchTerm != fooVal) { cachedSearchTerm = fooVal;
	$('searchterm').update(fooVal);
	if (fooVal.length >minimumStrLen) {
			seekanddisplay(fooVal);
	}
	else if (fooVal == "") {
			nofilter();
	}
	else {

	}
}
 
	//$('timerStateDiv').update($('timerStateDiv').innerHTML + maintimer);
		$('timerStateDiv2').update(maintimer);
 
}

function setQT(varl) {
	document.fooform.qt.value = varl;
}
	//
	
	
	// Add li's for the A-Z headings
function addAZLIs() {
	$('resultslistitems').update("");
	alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
	
	for (a=0;a < alpha.length;a++) {
		 var alph = new Element('li');
		 alph.update(alpha[a]).addClassName("liheader");
		 $('resultslistitems').appendChild(alph);
	}
 

}