/*
	util.js
*/

var BrowserSize;
var DocumentSize;
var ClientArea_TopLeft;
var ClientArea_Size;

function RunSchedule()
{
	this.count = 0;
	this.list = {};
	this.contentHeight = 0;
	
	this.add = function (func)
	{
		this.list[this.count] = func;
		this.count++;
	}
	
	this.reset = function ()
	{
		this.list = {};
		this.count = 0;
	}
	
	this.run = function ()
	{
		for (i in this.list)
		{
			this.list[i]();
		}
	}
}
var Run = new RunSchedule();

function refreshPage(more)
{
	if (typeof(more) == "undefined") more = '';
	
	if (typeof(READYONETADMIN) != "undefined" && READYONETADMIN == 1)
	{
		if (typeof(location.replace) != "undefined")
			location.replace('main.php?menu=' + menu_id + more);
		else
			location.href = 'main.php?menu=' + menu_id + more;
	}
	else
	{
		if (typeof(location.replace) != "undefined")
			location.replace('main.php?refresh=1' + more);
		else
			location.href = 'main.php?refresh=1' + more;
	}
}

function replacePage(url)
{
	if (typeof(location.replace) != "undefined")
		location.replace(url);
	else
		location.href = url;
}

function dummy()
{
}

function getBrowserSize()
{
	size = new Object();
	
  	if (typeof(window.innerWidth) == 'number')
  	{
	    //Non-IE
	    size.w = window.innerWidth;
	    size.h = window.innerHeight;
  	}
  	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
	    //IE 6+ in 'standards compliant mode'
	    size.w = document.documentElement.clientWidth;
	    size.h = document.documentElement.clientHeight;
  	}
  	else if (document.body && (document.body.clientWidth || document.body.clientHeight))
  	{
	    //IE 4 compatible
	    size.w = document.body.clientWidth;
	    size.h = document.body.clientHeight;
  	}
	else
	{
		size.w = 0;
		size.h = 0;
	}
  	
	return size;
}

function calculateClientArea()
{
	ClientArea_TopLeft = getAnchorPosition('TopLeft');
	
	ClientArea_Size = getElementSize('OuterTable');
	ClientArea_Size.w -= ClientArea_TopLeft.x * 2;
	ClientArea_Size.h -= ClientArea_TopLeft.y;
}

function getElementSize(id)
{
	var size = {'w': 0, 'h': 0};
	var e = document.getElementById(id);
	if (e)
	{
		size.w = e.offsetWidth;
		size.h = e.offsetHeight;
	}
	return size;
}

function dlgCenter(e)
{
	var size = getElementSize(e.id);
	
	var x = (BrowserSize.w - size.w) / 2;
	var y = (BrowserSize.h - size.h) / 2;
	var scroll_x = document.body.scrollLeft;
	var scroll_y = document.body.scrollTop;
	
	e.style.left = (parseInt(scroll_x) + parseInt(x)) + 'px';
	e.style.top = (parseInt(scroll_y) + parseInt(y)) + 'px';
}

function dlgPosition(e, x, y)
{
	e.style.left = x + 'px';
	e.style.top = y + 'px';
}

function dlgRelativePosition(e, x, y)
{
	var scroll_x = document.body.scrollLeft;
	var scroll_y = document.body.scrollTop;
	
	e.style.left = (parseInt(scroll_x) + x) + 'px';
	e.style.top = (parseInt(scroll_y) + y) + 'px';
}

function dlgActivate(e)
{
	e.style.visibility = 'hidden';
	e.style.left = '-2000px';
	e.style.top = '0px';
	e.style.display = 'block';
}

function dlgDisplay(e)
{
	setTimeout("document.getElementById('" + e.id + "').style.visibility = 'visible';", 10);
	//e.style.visibility = 'visible';
}

function dlgHide(e)
{
	e.style.visibility = 'hidden';
	e.style.display = 'none';
}

function dlgDestroy(e)
{
	e.style.visibility = 'hidden';
	e.innerHTML = '';
	e.style.display = 'none';
}

function dlgDelayedDestroy(e)
{
	setTimeout("dlgDestroy(document.getElementById('" + e.id + "'));", 100);
}

function onReturnKey(action, event)
{
	if (!event) event = window.event;
	
	if (event.keyCode == 13)
	{
		eval(action);
	}
}

function getSel()
{
	var s = '';
	if (window.getSelection) s = window.getSelection();
	else if (document.getSelection) s = document.getSelection();
	else if (document.selection) s = document.selection.createRange().text;
	
	return s;
}

function setSelRange(inputEl, selStart, selEnd)
{
	if (inputEl.setSelectionRange)
	{ 
		inputEl.focus(); 
		inputEl.setSelectionRange(selStart, selEnd); 
	}
	else if (inputEl.createTextRange)
	{ 
		var range = inputEl.createTextRange(); 
		range.collapse(true); 
		range.moveEnd('character', selEnd); 
		range.moveStart('character', selStart); 
		range.select(); 
	}
}

function insertAtCursor(e, s)
{
	//IE/Opera support
	if (document.selection)
	{
		//in effect we are creating a text range with zero
		//length at the cursor location and replacing it
		//with myValue
		
		sel = document.selection.createRange();
		sel.text = s;
	}
	//Mozilla/Firefox/Netscape 7+ support 
	else if (e.selectionStart || e.selectionStart == '0')
	{
		//Here we get the start and end points of the 
		//selection. Then we create substrings up to the 
		//start of the selection and from the end point 
		//of the selection to the end of the field value. 
		//Then we concatenate the first substring, myValue, 
		//and the second substring to get the new value. 
		
		var startPos = e.selectionStart;
		var endPos = e.selectionEnd;
		var newPos = startPos + s.length;
		var scroll = e.scrollTop;
		e.value = e.value.substring(0, startPos) + s + e.value.substring(endPos, e.value.length); 
		e.selectionStart = newPos;
		e.selectionEnd = newPos;
		e.scrollTop = scroll;
	}
	else
	{
		e.value += s;
	}
}

function fieldsetAction(id)
{
	var e = document.getElementById(id);
	if (e)
	{
		if (!e.style.display || e.style.display == 'none')
			fieldsetOpen(id);
		else
			fieldsetClose(id);
	}
}

function fieldsetInit(id, caption)
{
	var legend = document.getElementById(id + 'Legend');
	if (legend)
		legend.caption = caption;
	
	var e = document.getElementById(id);
	if (e)
	{
		if (!e.style.display || e.style.display == 'none')
			legend.innerHTML = '<img src="images/fieldset_closed.gif"\>' + legend.caption;
		else
			legend.innerHTML = '<img src="images/fieldset_opened.gif"\>' + legend.caption;
	}
}

function fieldsetOpen(id)
{
	var legend = document.getElementById(id + 'Legend');
	
	var e = document.getElementById(id);
	if (e)
	{
		e.style.display = 'block';
		legend.innerHTML = '<img src="images/fieldset_opened.gif"\>' + legend.caption;
	}
}

function fieldsetClose(id)
{
	var legend = document.getElementById(id + 'Legend');
	
	var e = document.getElementById(id);
	if (e)
	{
		e.style.display = 'none';
		legend.innerHTML = '<img src="images/fieldset_closed.gif"\>' + legend.caption;
	}
}

function OpenNewWindow(url, title, w, h)
{
	newwin = window.open(url, title, "height=" + h +", width=" + w + ", scrollbars, menubar, resizable");
	newwin.document.close();
	return newwin;
}

function C_AutoLogout()
{
	this.start = function ()
	{
		setTimeout('AutoLogout.check();', 300000);	// 5 minutes ping
	};
	
	this.check = function ()
	{
		CSC_Request('auto_logout.csc.php', 'check', {});
	};
	
}

window.AutoLogout = new C_AutoLogout();

function C_Ping()
{
	this.start = function ()
	{
		setTimeout('window.Ping.ping();', 300000);	// 5 minutes ping
	};
	
	this.ping = function ()
	{
		CSC_Request('ping.csc.php', '', {});
	};
	
}

window.Ping = new C_Ping();


// Copyright (C) Raditha Dissanyake 2003
function ProgressBar_popUP(mypage, myname, w, h, scroll, titlebar)
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4)
	{
		win.window.focus();
	}
}

// Copyright (C) Raditha Dissanyake 2003
function ProgressBar_postIt(formname)
{
	baseUrl = 'main.php?execute[executeFile]=pgbar.php';
	sid = document.forms[formname].sessionId.value;
	iTotal = escape("-1");
	baseUrl += "&iTotal=" + iTotal;
	baseUrl += "&iRead=0";
	baseUrl += "&iStatus=1";
	baseUrl += "&sessionid=" + sid;
	
	ProgressBar_popUP(baseUrl, "Uploader", 460, 150, false, false);
	document.forms[formname].submit();
}
