
//Returns URL parameter in "name"
function GetURLParameter( name )
{  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	
	if (results === null)
		return "";
	else    
		return results[1];
}

//Sorts the HTML select list
//criterion - leave blank, "a", "alpha" or "alphabetical", "alphabetically" for alphabetical
//			- "d", "difficulty" to sort by difficulty
function SortSelectList(list, criterion) {
var lb = document.getElementById(list);
arrTexts = new Array();

//Copy across values in select list into an array
for(i=0; i<lb.length; i++)  {
  arrTexts[i] = lb.options[i].text;
}

//Sort the array
if ( (criterion == "alphabetically") || (criterion == "alphabetical") || (criterion=="a") || (criterion=="alpha") || (criterion=="") )
	arrTexts.sort();
if ( (criterion == "difficulty") || (criterion=="d") )
	arrTexts.sort(SortByDifficulty);

//Copy values back
for(i=0; i<lb.length; i++)  {
  lb.options[i].text = arrTexts[i];
  lb.options[i].value = arrTexts[i];
  }

}

//Function for sorting list by difficulty
function SortByDifficulty(a, b)
{
	//If a is longer, b should come first
	if (a.length > b.length)
		return 1;
	//If a is shorter, a should come first
	if (a.length < b.length)
		return -1;
	//These are the same length, so sort alphabetically (case insensitive)
	var a_lower = a.toLowerCase();
	var b_lower = b.toLowerCase();
	if (a_lower > b_lower)
		return 1;
	if (a_lower < b_lower)
		return -1;
	return 0;		
}

//Sort function - words are randomized, but kept in sets of length
function SortRandomizeByDifficulty(a, b)
{
	//If a is longer, b should come first
	if (a.length > b.length)
		return 1;
	//If a is shorter, a should come first
	if (a.length < b.length)
		return -1;
	//These words are the same length, so sort randomly
	return (Math.round(Math.random())-0.5);
}

function SortReverseRandomizeByDifficulty(a, b)
{
	//If a is longer, it should go first
	if (a.length < b.length)
		return 1;
	//If a is shorter, it should go last
	if (a.length > b.length)
		return -1;
	//These words are the same length, so sort randomly
	return (Math.round(Math.random())-0.5);
}


//Seed random number generator
function Randomize()
{
	//Get today's date and time
	now = new Date();
	//Seed generator
	Math.random(now.getSeconds());
}

//Create a cookie for the name/value pair valid for "days" days.
//days = 0 indicates the cookie will be removed when the user closes
//the browser
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//Read the cookie for the variable "name"
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//Erase the value stored for variable "name"
function eraseCookie(name) {
	createCookie(name,"",-1);
}

function ChangeFont() {
	//Call change font function
	if (document.worksheet.doChooseFont()) {
		//The font dialog asked for the font to be stored in a cookie
		//Store font in cookie
		createCookie("font", document.applets[0].getActivityFont(), 365);	
		createCookie("font_size", document.applets[0].getActivityFontSize(), 365);
	}
}

function reformatRange(range)
{
	//Convert *'s in ranges to colons
	return_range = range.replace("*",":");
	//Convert spaces to + to ensure compliance
	return_range = return_range.replace(" ","+");
	//URL encode strings
	return_range = escape(return_range);
	//Return
	return return_range;
}

