﻿var FirstStyle;
var pSelectedListBox;
var isIE = false;
var SessionExpired = false;
var TrackTimeoutTimer, TrackTimeoutRemaining;
var ShowTimeoutMessage = true;

//if (navigator.userAgent.indexOf("MSIE") != -1) { isIE = true; }

//Jackson - 17/08 - to differenciate between IE versions, as IE9 handles JS differently to IE 6/7/8
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
    var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
    if (ieversion <= 8) {
        isIE = true;
    }
}


function InitSessionManagement(){

	if(Request("timedout") == "true" && ShowTimeoutMessage == true){
		var timeout = document.getElementById("EventManagement_LoggedOut");
		timeout.style.display = "block";
		ShowTimeoutMessage = false;
	}else{
		StartTrackTimeout();
	}
	
}

function CheckLogin(XmlDoc){

	var nodes = XmlDoc.getElementsByTagName("cmd");
	
	if(nodes.length > 0){
		if(nodes[0].getAttribute("result") == "false" && nodes[0].getAttribute("reason") == "loggedout"){
			SetUserLoggedOut();
			return false;
		}else{
			SetUserLoggedIn();	
			return true;
		}
	}else{
		SetUserLoggedIn();	
		return true;
	}
	
}

function ClearUserTimedOutStatus(){

	var timeout = document.getElementById("EventManagement_LoggedOut");
	timeout.style.display = "none";
	
}

function SetUserLoggedOut(){

	/*
	var timeout = document.getElementById("EventManagement_LoggedOut");
	var timeoutWarning = document.getElementById("EventManagement_TimeoutWarning");
	
	timeoutWarning.style.display = "none";
	timeout.style.display = "block";
	
	clearTimeout(TrackTimeoutTimer);
	*/
	
	
	var url = window.location.toString();
	if(url.indexOf("?") > 0){
		url = url.substring(0,url.indexOf("?"));
	}
	window.location.href = url + "?timedout=true";
	
}

function SetUserTimeoutWarning(){

	var timeout = document.getElementById("EventManagement_LoggedOut");
	var timeoutWarning = document.getElementById("EventManagement_TimeoutWarning");
	
	timeout.style.display = "none";
	timeoutWarning.style.display = "block";
	
}

function SetUserLoggedIn(){

	var timeout = document.getElementById("EventManagement_LoggedOut");
	var timeoutWarning = document.getElementById("EventManagement_TimeoutWarning");
	
	timeout.style.display = "none";
	timeoutWarning.style.display = "none";

	if(!TrackTimeoutTimer){StartTrackTimeout();}
	
}

function StartTrackTimeout(){	
	TrackTimeoutRemaining = SESSION_TIMEOUT;
	TrackTimeoutTimer = setTimeout(TrackTimeout, 60000);
}

function ResetTrackTimeout(){
	clearTimeout(TrackTimeoutTimer);
	StartTrackTimeout();
}

function SessionRenewed(){

	ResetTrackTimeout();
	SetUserLoggedIn();	
	
}

function TrackTimeout(){
	TrackTimeoutRemaining = (TrackTimeoutRemaining - 1);
	if(TrackTimeoutRemaining > 0){
		if(TrackTimeoutRemaining <= 5){
			SetUserTimeoutWarning();
		}else{
			SetUserLoggedIn();
		}
		TrackTimeoutTimer = setTimeout(TrackTimeout, 60000);
	}else{
		SetUserLoggedOut();
	}
}

function RenewSession(){

	Ajax_GetXmlPage(ROOT_DIR+"Atom/Home_Cmd.aspx?cmd=101", RenewSession_Response);

}

function RenewSession_Response(){
	if(Ajax_XmlHttp_IsReady()){
		
		var XmlDoc = Ajax_XmlHttp.responseXML;
		var nodes = XmlDoc.getElementsByTagName("cmd");
	
		if(nodes[0].getAttribute("result") == "false" && nodes[0].getAttribute("reason") == "loggedout"){
			SetUserLoggedOut();
		}else{
			SessionRenewed();
		}	
	
	}	
}



function ListBoxSelected(listbox){
	if(!listbox){listbox = this;}
	if(pSelectedListBox){SetStyle(pSelectedListBox,'force-out');}
	pSelectedListBox = listbox;
	SetStyle(listbox,'click');
}

function SetStyle(obj, type, cls, clsOver){
	
	if(!obj){
		obj = this;
		cls = obj.getAttribute("cls");
		clsOver = obj.getAttribute("clsover");
	}
	
	if(!cls){cls = "ListBox";}
	if(!clsOver){clsOver = "ListBox_Over";}
	
	if(type == "click"){
	
		obj.className = clsOver;	
		obj.setAttribute("holdstyle","true");
	
	}else{	
	
		var hold = obj.getAttribute("holdstyle");
		
		if(((type == "out") && (hold == "true") && (obj.id != pSelectedListBox.id)) || type == "force-out"){
			obj.setAttribute("holdstyle","false");
			hold = "false";
		}
		
		if(obj.className == clsOver){
			if(hold != "true"){
				if(!FirstStyle){
					obj.className = cls;
				}else{
					obj.className = FirstStyle;	
				}
			}
		}else{
			FirstStyle = obj.className;
			obj.className = clsOver;	
		}
		
	}
	
}



function DoExpand(obj,container){
	var id = obj.id.substring(obj.id.lastIndexOf("_")+1);

	if(obj.src.indexOf("_down") >= 0){
		//collapse
		Effect.BlindUp(container.id,{duration:0.3});
		obj.src = ROOT_DIR+"Atom/Images/expand_up.gif";
	}else{
		//expand
		Effect.BlindDown(container.id,{duration:0.3});
		obj.src = ROOT_DIR+"Atom/Images/expand_down.gif";
	}
}

function AddListBox(ObjectId, ObjectName, ParentContainer, TypeId, ClsPrefix, WrapperCls, MainContentCls){
	
	var wrapper, sth, stht, sthe, stmc;
	
	if(!ClsPrefix){ClsPrefix = "ListBox_";}
	
	sth = document.createElement("div");
	sth.id = TypeId + "_ListBox_Header_"+ObjectId;
	sth.className = ClsPrefix + "Header";
	stht = document.createElement("div");
	stht.id = TypeId + "_ListBox_Header_Header_Title_"+ObjectId;
	stht.className = ClsPrefix + "Header_Title";
	stht.innerHTML = ObjectName;
	
	sthe = document.createElement("img");
	sthe.id = TypeId + "_ListBox_Header_Expand_"+ObjectId;
	sthe.className = ClsPrefix + "Header_Expand";
	sthe.src = ROOT_DIR + "Atom/Images/expand_down.gif";
	sthe.setAttribute('Objectid', ObjectId);
	sthe.setAttribute('TypeId', TypeId);
	if (isIE) {
	    sthe.onclick = function() { DoExpand(this, document.getElementById(this.TypeId + "_ListBox_MainContent_" + this.Objectid)); }
	}
	else {
	    sthe.setAttribute("onclick", "DoExpand(this,document.getElementById('" + TypeId + "_ListBox_MainContent_" + ObjectId + "'));");
	}
	
	sth.appendChild(stht);
	sth.appendChild(sthe);
	
	stmc = document.createElement("div");
	stmc.id = TypeId + "_ListBox_MainContent_"+ObjectId;
	
	if(MainContentCls){
		stmc.className = MainContentCls;
	}else{
		stmc.className = ClsPrefix + "MainContent_Empty";
	}
	
	stmc.innerHTML = "There are no available documents.";
	
	wrapper = document.createElement("div");
	wrapper.id = TypeId + "_ListBox_Wrapper_"+ObjectId;
	
	if(WrapperCls){wrapper.className = WrapperCls;}
	
	wrapper.appendChild(sth);
	wrapper.appendChild(stmc);
	
	var spacer = document.createElement("div");
	spacer.className = ClsPrefix + "MainContent_Spacer";

	wrapper.appendChild(spacer);
	
	ParentContainer.appendChild(wrapper);

}

function AddListBoxItem(ParentObjectId, ObjectId, ObjectName, ObjectDescription, ParentContainer, DblClickCmdText, TypeId, Icon, Attributes, DisableRowSelect, ClickCmdText){
	
	var s, si, sii, sh, shs, st, sd, sicon;
	
	s = document.createElement("div");
	s.id = TypeId + "_ListBox_"+ObjectId;
	s.className = "ListBox";
	
	if(Attributes){
		var attr = Attributes.split(";")
		for(var i = 0; i < attr.length; i++){
			try{
				s.setAttribute(attr[i].substring(0,attr[i].indexOf(":")), attr[i].substring(attr[i].indexOf(":")+1));
			}catch(ex){}
		}
	}
	
	if(isIE){
		s.onmouseover = SetStyle;
		s.onmouseout = SetStyle;
		if(ClickCmdText){s.onclick = eval("IE_" + ClickCmdText.substring(0,ClickCmdText.indexOf("(")));}
		if(DblClickCmdText){s.ondblclick = eval("IE_" + DblClickCmdText.substring(0,DblClickCmdText.indexOf("(")));}
		if(!DisableRowSelect){s.onclick = ListBoxSelected;}
	
	}else{
	
		s.setAttribute("onmouseover", "SetStyle(this,'over');");
		s.setAttribute("onmouseout", "SetStyle(this,'out');");
		s.setAttribute("onclick", ClickCmdText);
		s.setAttribute("ondblclick", DblClickCmdText)
		if(!DisableRowSelect){s.setAttribute("onclick","ListBoxSelected(this);");}
	
	}

	si = document.createElement("div");
	si.id = TypeId + "_ListBox_Icon_"+ObjectId;
	si.className = "ListBox_Icon";
	
	sii = document.createElement("img");
	sii.id = TypeId + "_ListBox_Icon_Img_"+ObjectId;
	
	if(!Icon){Icon = ICON_DOCUMENT_GENERIC;}
	
	sii.src = Icon;
	
	if(isIE){sii.onload = png;}
	
	sh = document.createElement("div");
	sh.id = TypeId + "_ListBox_ItemHeader_"+ObjectId;
	sh.className = "ListBox_ItemHeader";
	
	st = document.createElement("div");
	st.id = TypeId + "_ListBox_Title_"+ObjectId;
	st.className = "ListBox_Title";
	st.innerHTML = ObjectName;
	
	sd = document.createElement("div");
	sd.id = TypeId + "_ListBox_Description_"+ObjectId;
	sd.className = "ListBox_Description";
	
	ObjectDescription = ObjectDescription.replace(/(\r\n|\r|\n)/g, ' ');
	ObjectDescription = ObjectDescription.replace(/<br\/>/g,' ');
	ObjectDescription = ObjectDescription.replace(/<br>/g,' ');
	
	sd.innerHTML = ObjectDescription;
	
	si.appendChild(sii);
	
	sh.appendChild(st);
	
	s.appendChild(si);
	s.appendChild(sh);
	s.appendChild(sd);

	ParentContainer.appendChild(s);
	
	AddStageProgressIcon(ObjectId, "", "blank", TypeId);
	
	return(s);

}

function AddBlankListBoxItem(ParentContainer, msg){

	try{
		s = document.createElement("div");
		if(!msg){msg="There are no items to display.";}
		s.innerHTML = msg;
		ParentContainer.appendChild(s);
	}catch(ex){}
	
}

function AddStageProgressIcon(ObjectId, StageLabel, StageState, TypeId){

	var sicon;
	var sh = document.getElementById(TypeId + "_ListBox_ItemHeader_"+ObjectId);

	sicon = document.createElement("img");
		
	switch(StageState){
    
        case "blank":

        	sicon.src = "/iapplication/eventmanagement/atom/images/blank16x16.gif";
            
            break;
            
            
		case "successfull", "3":
			
			sicon.src = "/icon/16/app/shadow/bullet_triangle_green.png";
			sicon.alt = "Successfull: " + StageLabel;
			sicon.title = "Successfull: " + StageLabel;
			
			break;
	
		case "failed", "4":
			
			sicon.src = "/icon/16/app/shadow/bullet_square_red.png";
			sicon.alt = "Failed: " + StageLabel;
			sicon.title = "Failed: " + StageLabel;
			
			document.getElementById(TypeId+"_ListBox_"+ObjectId).className = "ListBox_Failed";
			
			break;
	
		default://"pending", "1":
			
			sicon.src = "/icon/16/app/shadow/bullet_triangle_grey.png";
			sicon.alt = "Pending: " + StageLabel;
			sicon.title = "Pending: " + StageLabel;
			
			break;
	
		case "inprogress", "2":
			
			sicon.src = "/icon/16/app/shadow/bullet_triangle_yellow.png";
			sicon.alt = "In Progress: " + StageLabel;
			sicon.title = "In Progress: " + StageLabel;
			
			break;
		
	}
	
	if(isIE){sicon.onload = png;}
	
	sh.appendChild(sicon);
	

}

function AddReviewStageProgressIcon(ObjectId, StageLabel, StageState, TypeId){

	var sicon;
	var sh = document.getElementById(TypeId + "_ListBox_ItemHeader_" + ObjectId);

	sicon = document.createElement("img");
		
	switch(StageState){

		case "successfull", "3":
			
			sicon.src = "/icon/16/app/shadow/bullet_triangle_green.png";
			sicon.alt = "Successfull: " + StageLabel;
			sicon.title = "Successfull: " + StageLabel;
			
			break;
	
		case "failed", "4":
			
			sicon.src = "/icon/16/app/shadow/bullet_square_red.png";
			sicon.alt = "Failed: " + StageLabel;
			sicon.title = "Failed: " + StageLabel;
			
			break;
	
		default://"pending", "1":
			
			sicon.src = "/icon/16/app/shadow/bullet_triangle_grey.png";
			sicon.alt = "Pending: " + StageLabel;
			sicon.title = "Pending: " + StageLabel;
			
			break;
	
		case "inprogress", "2":
			
			sicon.src = "/icon/16/app/shadow/bullet_triangle_yellow.png";
			sicon.alt = "In Progress: " + StageLabel;
			sicon.title = "In Progress: " + StageLabel;
			
			break;
		
	}
	
	if(isIE){sicon.onload = png;}
	
	sh.appendChild(sicon);

}

function AddControlBox(Id, Name, Content, ParentContainer, TypeId, InnerContainerHeight){

	var wrap, sth, stht, sthe, stmc;
	
	sth = document.createElement("div");
	sth.id = TypeId + "_ControlBox_Header_"+Id;
	sth.className = "ListBox_Header";
	
	stht = document.createElement("div");
	stht.id = TypeId + "_ControlBox_Header_Title_"+Id;
	stht.className = "ListBox_Header_Title";
	stht.innerHTML = Name;
	
	sthe = document.createElement("img");
	sthe.id = TypeId + "_ControlBox_Expand_"+Id;
	sthe.className = "ListBox_Expand";
	sthe.src = ROOT_DIR+"Atom/Images/expand_down.gif";
	sthe.setAttribute("onclick","DoExpand(this,document.getElementById('"+TypeId+"_ControlBox_MainContent_"+Id+"'));");
	
	sth.appendChild(stht);
	sth.appendChild(sthe);

	stmc = document.createElement("div");
	stmc.id = TypeId + "_ControlBox_MainContent_"+Id;
	stmc.className = "ControlBox_MainContent";
	stmc.innerHTML = Content;
	
	if(InnerContainerHeight){stmc.style.height = InnerContainerHeight;}
	
	wrap = document.createElement("div");
	wrap.id = TypeId + "_" + Id;
	
	wrap.appendChild(sth);
	wrap.appendChild(stmc);
	
	ParentContainer.appendChild(wrap);
	
	var spacer = document.createElement("div");
	spacer.className = "ListBox_MainContent_Spacer";

	ParentContainer.appendChild(spacer);

}

function GetContentFromContentTemplate(id){

	try{
		var content = document.getElementById(id+"_ContentTemplate").innerHTML;
		document.getElementById(id+"_ContentTemplate").innerHTML = "";
		return content;
	}catch(ex){return "";}
	

}



function SetTab(tabsetid,id,reset){

	var l, c, r;

	var maxtabid = MAX_TABS[tabsetid];
	var currenttabid = CURRENT_TABS[tabsetid];
	
	var prefix = tabsetid + "_";
	
	//*******************************************************************************
	//RESET TABS...
	//*******************************************************************************	
	if(reset){

		for(var i = 0; i <= maxtabid; i++){
			
			try{		
				l = document.getElementById(prefix+"tab_"+i+"_l");
				l.className = "Tab_Left_First";
			}catch(ex){}//alert("error0: " +ex);}
			
			try{
				c = document.getElementById(prefix+"tab_"+i);
				c.className = "Tab";
			}catch(ex){}//alert("error1: " +ex);}
			
			try{

				r = document.getElementById(prefix+"tab_"+i+"_r");			
			
				//alert("r: " + r.id + " \n\ni: " + i + " \n\nmaxtabid: " + maxtabid);
				if (r != null) {
					if (i == parseInt(maxtabid)) {
						r.className = "Tab_Right_Last";
					} else {
						r.className = "Tab_Right";
					}
				}
			}catch(ex){}//alert("error2: " +ex);}
		
		}
	
	}
	
	//*******************************************************************************
	//CURRENT TAB OFF...
	//*******************************************************************************
	if(currenttabid > 0){

		l = document.getElementById(prefix+"tab_"+(currenttabid-1)+"_r");
		c = document.getElementById(prefix+"tab_"+currenttabid);
		r = document.getElementById(prefix+"tab_"+currenttabid+"_r");
		
		l.className = "Tab_Right";
		try{c.className = "Tab";}catch(ex){}

		if (r != null) {
			if (currenttabid == maxtabid) {
				r.className = "Tab_Right_Last";
			} else {
				r.className = "Tab_Right";
			}
		}
	}else if(currenttabid == 0){
	
		l = document.getElementById(prefix+"tab_0_l");
		c = document.getElementById(prefix+"tab_0");
		r = document.getElementById(prefix+"tab_0_r");
		
		l.className = "Tab_Left_First";
		c.className = "Tab";
		if (r != null) {
			r.className = "Tab_Right";
		}
		
	}
	
	//*******************************************************************************	
	//NEW TAB ON...
	//*******************************************************************************
	if(id == 0){
	
		l = document.getElementById(prefix+"tab_0_l");
		c = document.getElementById(prefix+"tab_0");
		r = document.getElementById(prefix+"tab_0_r");
		
		l.className = "Tab_Left_FirstOn";
		c.className = "Tab_On";		
		r.className = "Tab_Right_On";
	
	}else{
			
		l = document.getElementById(prefix+"tab_"+(id-1)+"_r");
		c = document.getElementById(prefix+"tab_"+id);
		r = document.getElementById(prefix+"tab_"+id+"_r");
				
		l.className = "Tab_Right_NextOn";
		try{c.className = "Tab_On";}catch(ex){}
		if (r != null) {
			if (id == maxtabid) {
				try { r.className = "Tab_Right_LastOn"; } catch (ex) { }
			} else {
				try { r.className = "Tab_Right_On"; } catch (ex) { }
			}
		}
	
	}
	
	
	CURRENT_TABS[tabsetid] = id;
	
}
