var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;
var SkimValue;
var browserName=BrowserType ();
var _SECONDS;
var ResponseHandler;
	
function BrowserType () {
	if (navigator.appName.indexOf("Microsoft") >= 0) {
		browserName = "IE";
		SkimValue = 170;
	} else {
		browserName = "Other";
		SkimValue = 155;
	}
}

function InitializeTimer(responseHandler)
{
	// Set the length of the timer, in seconds
	secs = 60;
	_SECONDS = 60;
	ResponseHandler = responseHandler;
	StopTheClock();
	StartTheTimer();
}

function InitializeTimer(responseHandler, seconds)
{
	// Set the length of the timer, in seconds
	secs = seconds;	
	_SECONDS = seconds;
	ResponseHandler = responseHandler;
	StopTheClock();
	StartTheTimer();
}

function ResetTimer() {
	//reset the clock
	secs = _SECONDS;
	StartTheTimer();
}

function StopTheClock()
{
	if(timerRunning) {
		window.clearTimeout(timerID);
	}
	timerRunning = false;
}

function StartTheTimer()
{
	if (secs==0)
	{
		StopTheClock();
		ResponseHandler();
	}
	else
	{
		//self.status = secs;
		secs = secs - 1;
		timerRunning = true;
		timerID = self.setTimeout("StartTheTimer()", delay);
	}
}
	
function WindowOnload(f) {
	var prev=window.onload;
	window.onload=function(){ if(prev)prev(); f(); }
}

function SetWorkAreaHeight()	{

	var content = document.getElementById("content");
	var map = document.getElementById("map");
	var sidebar2 = document.getElementById("sidebar2");
	var sidebar3 = document.getElementById("sidebar3");		
	
	if (browserName == "IE") {
		content.style.height=GetWindowHeight()-SkimValue;
		if (map)
			map.style.height=GetWindowHeight()-SkimValue;
		if (sidebar2){
			sidebar2.style.height=GetWindowHeight()-SkimValue;
			sidebar3.style.height=sidebar2.style.height;
		}
			
	} else {
		var height = GetWindowHeight()-SkimValue;
		content.style.height=height.toString() + "px";
		if (map)
			map.style.height=(height-10).toString() + "px";				
		if (sidebar2) {
			sidebar2.style.height=height.toString() + "px";
			sidebar3.style.height=sidebar2.style.height;
		}
	}	
	ShowLoading(false);		
}

function GetWindowWidth() {
	var frameWidth;		
	if (top.innerWidth)	{
		frameWidth = top.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth){
		frameWidth = document.documentElement.clientWidth;
	}
	else if (document.body)	{
		frameWidth = document.body.clientWidth;
	}
	
	return frameWidth;

}

function GetWindowHeight()
{

	var frameHeight;
	
	if (top.innerWidth)
	{
		frameHeight = top.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		frameHeight = document.body.clientHeight;
	}
	
	return frameHeight;

}
	
function ShowHideDiv(id)
{
	var e = document.getElementById(id);	
	if (e.style.display == 'block' || e.style.display == '')
	{
		e.style.display = 'none';
	}
	else
	{
		e.style.display = 'block'; 
	}
}

function HideDiv(id)
{
	var e = document.getElementById(id);	
	e.style.display = 'none'; 
	
}

function ShowDiv(id)
{
	var e = document.getElementById(id);	
	e.style.display = 'block';	
}

//used by ajax to submit an entire form
function getFormValues(fobj, valFunc) 
{ 
	var str = ""; 
	var valueArr = null; 
	var val = ""; 
	var cmd = ""; 
	for(var i = 0;i < fobj.elements.length;i++) 
	{ 
		switch(fobj.elements[i].type) 
		{ 
			case "text": 
				if(valFunc) 
				{ 
					//use single quotes for argument so that the value of 
					//fobj.elements[i].value is treated as a string not a literal 
					cmd = valFunc + "(" + 'fobj.elements[i].value' + ")"; 
					val = eval(cmd);
				} 
				str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&"; 
				break; 

			case "select-one": 				
				str += fobj.elements[i].name + "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&"; 
				break; 
			case "hidden":
				str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&"; 
				break; 
			case "checkbox":
				if (fobj.elements[i].checked == true) {
					str += fobj.elements[i].name + "=on&"; 
				}
				break;
			default:
				//alert(fobj.elements[i].type);
				break; 
		} 
	} 
	str = str.substr(0,(str.length - 1)); 
	return str; 
}

function RandomiseUrl(url) {
	var ran_number= Math.random()*5; 
	url = url + '&rnd=' + ran_number; // doing this so the url changes and forces reload.
	
	return url;
}

function ShowLoading (show) {
	var loading = document.getElementById("loading");
	if (show) 
		loading.style.display = 'block';
	else
		loading.style.display = 'none';			
					
}

function ShowUnitEditor() {
	try
	{
		if (document.getElementById('UnitEditor')) {
			var e = document.getElementById('UnitEditor');
		
			ShowDiv('UnitEditor');
			
			e.style.postion = 'absolute';			
			e.style.left = GetPopupLeft(300);
			e.style.top = GetPopupTop(100);
		}
	}
	catch(err)
	{
		txt="There was an error on this page.\n\n"
		txt+="Error description: " + err.description + "\n\n"
		txt+="Click OK to continue.\n\n"
		alert(txt)
	}
	
	//alert(e.style.left);
}

function GetPopupLeft(popupWidth) {
	if (browserName = "IE")
		return (GetWindowWidth() / 2) - (popupWidth / 2);
	else
		return (GetWindowWidth() / 2) - (popupWidth);
}

function GetPopupTop(popupHeight) {
	return (GetWindowHeight() / 2) - (popupHeight);	
}