// #######################
// ### General Globals ###
// #######################

//const loadingimage = 'images/arrows.gif';	// This is the loading image source from wherever your image root is.
//const blankimage = 'blank.gif'; 			// This is the blank image used to redirect pages on_load.

const debug = true;

var http = createXMLHttpRequest();
//var inCall = false;
//var callToArray = new Array();
//var returnToArray = new Array();
//var requests = new Array();

// #######################


// #########################
// ### General Functions ###
// #########################

function showMsg(msg, timeout) {
	document.getElementById("msg").innerHTML = msg;
	show("msg");
	if (timeout > 0) {
		setTimeout('hide("msg", false);', timeout);
	}
}

function hideMsg() {
	hide("msg", false);
}

// Easier than typing doc.getElemenbById every time...very cool


// Show a hidden element
function show(id) { 
	$(id).style.visibility="visible";
	$(id).style.display="block";
}

//	Hide a shown element
function hide(id, display_none) {
	if (display_none == true) {
		$(id).style.display="none";
	}
	$(id).style.visibility="hidden";
}

function toggleDateFormEl(id, string1, string2) {
	switch (document.getElementById(id).value) {
		case string1: document.getElementById(id).value = string2; break;
		case string2: document.getElementById(id).value = string1; break;
		default: break;
	}
}

// Change the font color of text within an element...use a span to change specific text.
function changeColor(id, color) {
	$(id).style.color=color;
}

// Change the font of the text within an element...use a span to change specific text.
function changeFont(id, font) {	
	$(id).style.font=font;	
}

// Changes background of an element. Types are "image" or "color".
// For body, apply an ID to your body tag. i.e.: <body id='bodyid'>..
function changeBackground(id, background, type) {
	if (type == "image") { //If an image is specified
		$(id).style.background="url("+background+")";
	}
	else {
		if (type == "color") { //If user specifies a color
			$(id).style.background = background;
		}
	}
} // End background function

// Replaces text with by "by" string...
function replace(string, text, by) {
	var strLength = string.length, txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0)) {return string;}
	var i = string.indexOf(text);
	if ((!i) && (text != string.substring(0,txtLength))) {return string;}
	if (i == -1) {return string;}
	var newstr = string.substring(0,i) + by;
	if (i+txtLength < strLength)
	newstr += replace(string.substring(i+txtLength,strLength),text,by);
	return newstr;
}

// Be sure you have defined a blank image at the top. have yet to find a better way
function redirect(page) {
	document.write("<img src=\"images/blank.png\"alt=\"blankimage\" onload=\"window.location.href='"+page+"'\" />");
}
 
function moveLayerToMouse(obj, e) {
	var tempX = 0;
	var tempY = 0;
	var offset = 5;


	if (document.all) {
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {
			tempX = e.pageX;
			tempY = e.pageY;
		}

	if (tempX < 0) { tempX = 0; }
 	if (tempY < 0) { tempY = 0; }
 	
	// Determine the max width and height that we have to work with
 	if (self.innerWidth) {
		var maxWidth = self.innerWidth;
		var maxHeight = self.innerHeight;
	
 	} else if (document.documentElement && document.documentElement.clientWidth) {
		var maxWidth = document.documentElement.clientWidth;
		var maxHeight = document.documentElement.clientHeight;
	
 	} else if (document.body) {
		var maxWidth = document.body.clientWidth;
		var maxHeight = document.body.clientHeight;		
	}

	// Adjust max vars based on scroll position
	if (document.body.scrollTop) { maxHeight = maxHeight + document.body.scrollTop; }
	if (document.body.scrollLeft) { maxWidth = maxWidth + document.body.scrollLeft; }	
	
	if (tempX + obj.offsetWidth + 30 > maxWidth) {
		$(obj).style.left = (tempX - obj.offsetWidth) + 'px';
	} else {
		$(obj).style.left = (tempX + offset) + 'px';
	}
	
	if (tempY + obj.offsetHeight + 30 > maxHeight) {
		$(obj).style.top = (tempY - obj.offsetHeight) + 'px';
	} else {
		$(obj).style.top = (tempY + offset) + 'px';
	}
}	

function countDown(secs) {
	display = '';
	if (secs == 0) {
		document.getElementById("msg").innerHTML = "Refreshing...";
		document.getElementById("msg").style.display = "block";
		setTimeout('document.getElementById("msg").style.display = "none"; setTimeout(null, 100); document.getElementById("msg").innerHTML="";', 3000);
		location.reload(true);
		return;
	}
	ticks = ((Math.floor(secs/60))%60).toString();
	if (ticks.length < 2) {ticks = "0" + ticks;}
	display += ticks + ':';
	ticks = ((Math.floor(secs/1))%60).toString();
	if (ticks.length < 2) {ticks = "0" + ticks;}
	display += ticks;

	document.getElementById("cntdwn").innerHTML = display;
	setTimeout("countDown(" + (secs - 1) + ")", 990);
}


// #########################


// ############
// ### AJAX ###
// ############

// Actual function for creating AJAX stuffs
function createXMLHttpRequest() {
	var xmlhttp;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
 		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

var HttpQueue = {
	methods:new Array(),
	urls:new Array(),
	postStrings:new Array(),
	handlers:new Array(),
	inCall:false,
	performRequests:function() {
		if (this.methods.length > 0 && this.urls.length > 0 && this.handlers.length > 0) {
			if (this.inCall) {
				setTimeout("HttpQueue.performRequests();", 100);
			}
			else {
				this.inCall = true;
				http.open(this.methods.shift(), this.urls.shift());
				http.onreadystatechange = this.handlers.shift();
				http.send(this.postStrings.shift());
				setTimeout("HttpQueue.performRequests();", 100);
			}
		}
	},
	prepareRequest:function(method, url, postString, handler) {
		HttpQueue.methods.push(method);
		HttpQueue.urls.push(url);
		HttpQueue.postStrings.push(postString);
		if (handler) {
			HttpQueue.handlers.push(handler);
		}
		else {
			HttpQueue.handlers.push(function () {if (http.readyState == 4){HttpQueue.inCall = false;}});
		}
	}
}

function buildGenericHandler(response,replace) {
	return function () {
		if (http.readyState == 4) {
			HttpQueue.inCall = false;
			if (replace == false) {
				$(response).innerHTML = $(response).innerHTML+ http.responseText;
			}
			else if (replace == true) {
				$(response).innerHTML = http.responseText;
			}
		}
	};
}

function appendElement(id, val) {
	$(id).innerHTML = $(id).innerHTML + val;
}

function highlight(id) {
 $(id).style.border= "3px solid yellow";
}

function unhighlight(id)
{
	$(id).style.border = 'none';
}
function disable(id){
	$(id).disabled=true;
}
function enable(id){
	$(id).disabled=false;
}
function deleteQuestion(question)
{
	if (confirm("Are you sure you wish to delete this question?"))
	{
		window.location.href='performQuizAction.php?action=deleteQuestion&deleteQuestion='+question;
	}
}

function deleteAnswer(answer)
{
	if(confirm("Are you sure you wish to delete this answer?"))
	{
		window.location.href='performQuizAction.php?action=deleteAnswer&deleteAnswer='+answer;
	}
}

function deleteQuiz(quiz)
{
	if(confirm("Are you sure you wish to delete this quiz?"))
	{
		window.location.href='performQuizAction.php?action=deleteQuiz&deleteQuiz='+quiz;
	}
}

function deleteRecord(user,quiz)
{
	if(confirm("Are you sure you want to release this user's quiz? The user will have to take the quiz again."))
	{
		window.location.href='performQuizAction.php?action=deleteRecord&deleteRecord='+quiz+'&user='+user;
	}
}
// ###############
