//---Functions used site wide or as subroutines for other functions---//

//function for creating request object for ajax
function getXmlHttp(){
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
		}
	else if(window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
		}
	else{
		alert("AJAX Functionality is not available, please use a more current browser.");
		}
	}

//return random number between a and b
function randValue(a,b){
	//check a is actually min number, and swap if necessary
	if(a > b){
		var c = a;
		a = b;
		b = c;
	}
	return Math.floor(a + (Math.random()*(b - a)));
}

//return a random string of length ofLength. Possible TODO: make passable charSelection
function randString(ofLength){
	var charSelection = '0123456789';
	var returnString = '';
	for(i=0;i<ofLength;i++){
		var charIndex = randValue(0,charSelection.length);
		var newChar = charSelection.substring(charIndex,charIndex+1);
		returnString += newChar;
	}
	return returnString;
}

//function which returns an array containing window width[0] and height[1].
function getWindowSize(){
	var windowWidth;
	var windowHeight;
	if(typeof(window.innerWidth) == 'number' ){
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	return [windowWidth,windowHeight];
}

//show and hide functions which fade in or fade out a div. div must have initial display and opacity value set
//either via css or when built by jscript
//USAGE: pass name of div to be shown/hidden - optional maxOpacity is float 0-1
function show(divName, maxOpacity){
	
	var div = document.getElementById(divName);
	var opacityVal = 0.1;
	var alphaVal = 10;
	maxOpacity = (maxOpacity != null) ? maxOpacity : 1;
	
	function fadeIn(){
		if(opacityVal < maxOpacity){
			div.style.opacity = opacityVal;
			div.style.filter = 'alpha(opacity='+alphaVal+')';
		}
		else{
			return;
		}
		opacityVal += 0.1;
		alphaVal += 10;
		setTimeout(fadeIn,50);
	}
		
	if(div.style.display != 'block'){
		div.style.display = 'block';
		fadeIn();
	}
}

function hide(divName){
	var div = document.getElementById(divName);
	var opacityVal = 0.9;
	var alphaVal = 90;
	
	function fadeOut(){
		if(opacityVal > 0){
			div.style.opacity = opacityVal;
			div.style.filter = 'alpha(opacity='+alphaVal+')';
		}
		else{
			div.style.display = 'none';
			return;
		}
		opacityVal -= 0.1;
		alphaVal -= 10;
		setTimeout(fadeOut,50);
	}

	if(div.style.display != 'none'){	
		fadeOut();
	}
}

//creates a new message window
function messageBox(messageText){
	//check if messagebox div exists and create if needed
	if(document.getElementById('mBox') == null){
		var messageBox = document.createElement('div');
		messageBox.className = 'messageBox';
		messageBox.setAttribute('id','mBox');
		document.body.appendChild(messageBox); //this is kinda important ^_^
	}
	else{
		messageBox = document.getElementById('mBox');
		messageBox.innerHTML = '';
	}
		
	var windowSize = getWindowSize();
	
	//set messageBox content and position based on window size
	var leftPos = (windowSize[0]/2)-150;
	var topPos = (windowSize[1]/2)-80;
	var info = document.createTextNode(messageText);
	var closeLink = document.createElement('a');
	closeLink.setAttribute('href','javascript:hide("mBox"); hide("bgOverlay")');
	closeLink.innerHTML = 'OK';
	
	messageBox.appendChild(info);
	messageBox.innerHTML += '<br>';
	messageBox.innerHTML += '<br>';
	messageBox.appendChild(closeLink);
	messageBox.style.position = 'fixed';
	messageBox.style.top = Math.ceil(topPos)+'px';
	messageBox.style.left = Math.ceil(leftPos)+'px';
	
	if(document.getElementById('bgOverlay') == null) bgOverlay();
	show('mBox');
	show('bgOverlay',0.6);
}

//creates background overlay
function bgOverlay(){
	var divArray = document.getElementsByTagName('div');

	//check if div exists and create if it doesn't
	if(document.getElementById('bgOverlay') == null){
		bgOverlay = document.createElement('div');
		bgOverlay.className = 'bgOverlay';
		bgOverlay.setAttribute('id','bgOverlay');
		bgOverlay.style.height = (parseInt(divArray[0].clientHeight) + 20)+'px';
		document.body.appendChild(bgOverlay);
	}

}

