//Task Class
Task = function ()
{
	this.type = "";
	this.parameters = "";
}

Tasks = function ()
{
	this.id = -1;
	this.taskList = new Array();
}
Tasks.prototype.clear = function()
{
	this.id++;
	this.taskList.length = 0;
}  
Tasks.prototype.pushTask = function(taskType, taskParameters)
{
	var theTask = new Task();
	theTask.type = taskType;
	theTask.parameters = taskParameters;
	this.taskList.push(theTask);
}
Tasks.prototype.fetchTask = function()
{
	return this.taskList.shift();
}

// The main codes
var shortkeysReady = false;
var refreshContentAfterLogin = false;
var firstLoginLoad = true;
var shortkeys=new Array(100)
var ExitFlag = true;
var loadedScripts = new Array();
var loadedStyleSheets = new Array();
var tasks = new Tasks();
var lastCallBackId = 0; 

function reset_shortkeys_array()
{
	for (i=0; i <100; i++)
	{
		shortkeys[i]=new Array(2)				
		shortkeys[i][0] = '';
		shortkeys[i][1] = '';
	}
}

function init_shortkeys()
{
	var theOptions = {
		'type':'keydown',
		'propagate':false,
		'target':document
	}; 
	for (i=0; i <100; i++)
	{
		if (shortkeys[i][0] != '')
		{
			shortcut.add(shortkeys[i][0],shortkeys[i][1],theOptions);
		}
	}
}

function remove_shortkeys()
{
	var theOptions = {
		'type':'keydown',
		'propagate':true,
		'target':document
	}; 
	for (i=0; i <100; i++)
	{
		if (shortkeys[i][0] != '')
		{
			shortcut.remove(shortkeys[i][0]);
		}
	}
}

reset_shortkeys_array();

function apply_child_shortkeys()
{
	remove_shortkeys();
	try{
		init_shortkeys();
	}
	catch(e)
	{
		setTimeout("apply_child_shortkeys();",500);
	}
}
		
		
function changeActiveMenu(miId) 
{	
	return;
	allLinks = document.getElementsByTagName("a");
	for (var i = 0; i < allLinks.length; i++)
	{
		linkId = allLinks[i].id;
		pos = linkId.indexOf('mi_link_');
		if (pos == 0)
		{
			menuItemId = linkId.substr(8,10);
			menuItemPadId = 'mi_pad_' + menuItemId;
			menuItemLinkId = 'mi_link_' + menuItemId;
			menuItemPad = document.getElementById(menuItemPadId);
			menuItemLink = document.getElementById(menuItemLinkId);
			menuItemPad.className = 'menu_item';
			menuItemLinkId.className = 'menu_item_text';
		}
	}
	menuItemPadId = 'mi_pad_' + miId;
	menuItemLinkId = 'mi_link_' + miId;
	menuItemPad = document.getElementById(menuItemPadId);
	menuItemLink = document.getElementById(menuItemLinkId);
	menuItemPad.className = 'active_menu_item';
	menuItemLinkId.className = 'active_menu_item_text';
}


function changeMenu(newContent)
{
	document.getElementById('menu_module').innerHTML =  newContent;
}

function changeMainView()
{
	document.getElementById('main_module').style.display = 'block';
	document.getElementById('waitDiv').style.display = 'none';	
	try
	{	
		var childFrame = window.frames['loadIframe'];
		document.getElementById('main_module').innerHTML =  
		childFrame.document.getElementById('main_module').innerHTML;
		refreshContentAfterLogin = childFrame.refreshContentAfterLogin;
		miId = childFrame.miId;
		changeActiveMenu(miId);
	}
	catch (e)
	{
		alert(javascriptTexts[0])
	}
	try
	{	
		var childFrame = window.frames['loadIframe'];
		childFrame.parentCall();
	}
	catch (e)
	{
	}	
}

function onChildLoad()
{
	//setTimeout("changeMainView();",20);	
	document.getElementById('main_module').style.display = 'block';
	document.getElementById('waitDiv').style.display = 'none';	
}

function onChildUnload()
{	
	document.getElementById('main_module').style.display = 'none';
	document.getElementById('waitDiv').style.display = 'block';			
}


function show_content_detail(textId)
{
	fullTextDivId = 'fullText_'+textId;
	fullTextDiv = document.getElementById(fullTextDivId);
	fullTextDiv.style.display = 'block';
	showLinkDivId = 'showLink_'+textId;
	showLinkDiv = document.getElementById(showLinkDivId);
	showLinkDiv.style.display = 'none';
}

function hide_content_detail(textId)
{
	fullTextDivId = 'fullText_'+textId;
	fullTextDiv = document.getElementById(fullTextDivId);
	fullTextDiv.style.display = 'none';
	showLinkDivId = 'showLink_'+textId;
	showLinkDiv = document.getElementById(showLinkDivId);
	showLinkDiv.style.display = 'block';
}

function change_content_visible(textId)
{
	textDivId = 'text_'+textId;
	textDiv = document.getElementById(textDivId);
	if (textDiv.style.display == 'none')
	{
		textDiv.style.display = 'block';
	}
	else
	{
		textDiv.style.display = 'none';
	}
}

function onLoginLoad()
{
	shortkeysReady = true;
	loginFrame = window.frames['loginIframe'];
	username = loginFrame.GetUserName();	
	if (username=='guest')
	{
		document.getElementById('loginFeilds').style.display = 'block';
		document.getElementById('waitForLogin').style.display = 'none';
		document.getElementById('logedinUser').style.display = 'none';
		document.getElementById('loginform_username').focus();
	}
	else
	{
		document.getElementById('loginFeilds').style.display = 'none';
		document.getElementById('waitForLogin').style.display = 'none';
		document.getElementById('logedinUser').style.display = 'block';				
		document.getElementById('currentUser').innerHTML = username;
	}
	document.onkeydown = "";
	apply_child_shortkeys();
	if (! firstLoginLoad)
	{
		if (refreshContentAfterLogin)
		{
			childFrame = window.frames['loadIframe'];
			childFrame.location.reload(false); ;				
		}
	}
	else
	{
		firstLoginLoad = false;
	}
	
}

function onLoginUnload()
{	
	shortkeysReady = false;
	remove_shortkeys();
	document.getElementById('loginFeilds').style.display = 'none';
	document.getElementById('waitForLogin').style.display = 'block';
	document.getElementById('logedinUser').style.display = 'none';
}

function do_for_login()
{
	onLoginUnload();
	lUsername = document.getElementById('loginform_username').value;
	lPassword = document.getElementById('loginform_password').value;
	loginFrame = window.frames['loginIframe'];
	loginFrame.Login(lUsername,lPassword);
	document.getElementById('loginform_username').value = '';
	document.getElementById('loginform_password').value = '';
}

function do_for_logout()
{
	onLoginUnload();
	loginFrame = window.frames['loginIframe'];
	loginFrame.LogOff();
}				

function prevent_logoff()
{
	onLoginUnload();
	loginFrame = window.frames['loginIframe'];
	loginFrame.ExitFlag = false;
	ExitFlag = false;
}

function change_loadframe_location(location)
{
	var loadFrame = window.frames['loadIframe'];
	loadFrame.location = location;
}

function SetCookie(cookieName,cookieValue, nMinute) {
 var today = new Date();
 var expire = new Date();
 if (nMinute==null || nMinute==0) nMinute=1;
 expire.setTime(today.getTime() + 60000*nMinute);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function page_unload()
{
	if (ExitFlag)	
	{
		loginFrame = window.frames['loginIframe'];
		loginFrame.LogOff();
		if (BrowserDetect.browser != 'Firefox')
		{
			alert(javascriptTexts[2]);
		}
	}
}

function LoadFrame_Loaded()
{	
	tasks.clear();
	var loadFrame = window.frames['loadIframe'];
	var moduleNames = loadFrame.moduleNames;
	var jsFiles = loadFrame.jsFiles;
	var cssFiles = loadFrame.cssFiles;
	var i=0; 
	for (i=0;i<jsFiles.length;i++)
	{
		tasks.pushTask("loadJsFile",jsFiles[i]);
	}
	for (i=0;i<cssFiles.length;i++)
	{
		tasks.pushTask("loadCssFile",cssFiles[i]);
	}
	for (i=0;i<moduleNames.length;i++)
	{
		tasks.pushTask("loadModule",moduleNames[i]);
	}
	tasks.pushTask("loadJavaScript","");
	Fetch_A_Task(tasks.id);
}

function Fetch_A_Task(tasksId)
{
	if (tasks.id != tasksId) return;
	if (tasks.taskList.length < 1)	 return;
	
	theTask = tasks.fetchTask();

	switch(theTask.type)
	{
		case "loadJsFile":
			Load_Js_File(theTask.parameters, tasksId);
			break;
		case "loadCssFile":
			Load_Css_File(theTask.parameters, tasksId);
			break;
		case "loadModule":
			Load_Module(theTask.parameters, tasksId);
			break;
		case "loadJavaScript":
			Load_JavaScript();
			break;
	}
}


function Js_File_Is_Loaded(jsFile, tasksId)
{
	for(var i=0;i<loadedScripts.length;i++)
	{
		if (loadedScripts[i] == jsFile)
		{
			return;
		}
	}
	loadedScripts.push(jsFile);
	Fetch_A_Task(tasksId);
}

function Load_Js_File(jsFile, tasksId)
{
	for(var i=0;i<loadedScripts.length;i++)
	{
		if (loadedScripts[i] == jsFile)
		{
			Fetch_A_Task(tasksId);
			return;
		}
	}
    callBackId = lastCallBackId;
    lastCallBackId++;
    var theHead = document.getElementsByTagName("head")[0];
    var theScript = document.createElement("script");
    theScript.type = "text/javascript";
    theScript.src = jsFile;
    tempScript = "function Js_File_Is_Loaded_"+callBackId+"()\n"+
                "{\n"+
                "Js_File_Is_Loaded('"+jsFile+"',"+tasksId+");\n"+
                "}\n";
    tempScript += "if (navigator.appName != 'Microsoft Internet Explorer')\n"+
                 "{\n"+
                 "    theScript.onload = Js_File_Is_Loaded_"+callBackId+";\n"+
                 "}\n"+
                 "else\n"+
                 "{\n"+
                 "    theScript.onreadystatechange = function()\n"+
                 "    {\n"+
                 "        if ((this.readyState == 'complete') || (this.readyState == 'loaded')) Js_File_Is_Loaded_"+callBackId+"();\n"+
                 "    }\n"+
                 "}";
    eval(tempScript);
    theHead.appendChild(theScript);
}

function Load_Css_File(cssFile, tasksId)
{
	for(var i=0;i<loadedStyleSheets.length;i++)
	{
		if (loadedStyleSheets[i] == cssFile)
		{
			Fetch_A_Task(tasksId);
			return;
		}
	}
    var theHead = document.getElementsByTagName("head")[0];
    var theLink = document.createElement("link");
    theLink.type = "text/css";
    theLink.rel = "stylesheet";
    theLink.href = cssFile;
    theHead.appendChild(theScript);
    loadedStyleSheets.push(cssFile);
    Fetch_A_Task(tasksId);
}

function Load_Module(moduleName, tasksId)
{
	try
	{
		var loadFrame = window.frames['loadIframe'];
	}
	catch(e)	
	{
		return;
	}
	document.getElementById(moduleName+"_module").innerHTML = 
	loadFrame.document.getElementById(moduleName+"_module").innerHTML;
	Fetch_A_Task(tasksId);
}

function Load_JavaScript(tasksId)
{
	try
	{
		var loadFrame = window.frames['loadIframe'];
	}
	catch(e)	
	{
		return;
	}
	loadFrame.Add_Page_Scripts();
	scroll(0,0);
	Fetch_A_Task(tasksId);
}

var contentRefreshUrl = "";
function Refresh_Content()
{
	if (contentRefreshUrl == "")
		return;
	var loadFrame = window.frames['loadIframe'];
	loadFrame.location = contentRefreshUrl;
}
function Change_Content(location)
{
	var loadFrame = window.frames['loadIframe'];
	loadFrame.location = location;
}