﻿var FirstStyle;
var pSelectedListBox;
var isIE = false;

if(navigator.userAgent.indexOf("MSIE") != -1){isIE = true;}

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){
	
	var sth, stht, sthe, stmc;
	
	sth = document.createElement("div");
	sth.id = TypeId + "_ListBox_Header_"+ObjectId;
	sth.className = "ListBox_Header";
	
	stht = document.createElement("div");
	stht.id = TypeId + "_ListBox_Header_Header_Title_"+ObjectId;
	stht.className = "ListBox_Header_Title";
	stht.innerHTML = ObjectName;
	
	sthe = document.createElement("img");
	sthe.id = TypeId + "_ListBox_Header_Expand_"+ObjectId;
	sthe.className = "ListBox_Header_Expand";
	sthe.src = ROOT_DIR+"Atom/Images/expand_down.gif";
	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;
	stmc.className = "ListBox_MainContent_Empty";
	stmc.innerHTML = "There are no available documents.";
	
	ParentContainer.appendChild(sth);
	ParentContainer.appendChild(stmc);
	
	var spacer = document.createElement("div");
	spacer.className = "ListBox_MainContent_Spacer";

	ParentContainer.appendChild(spacer);

}

function AddListBoxItem(ParentObjectId, ObjectId, ObjectName, ObjectDescription, ParentContainer, DblClickCmdText, TypeId, Icon, Attributes){
	
	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(DblClickCmdText){s.ondblclick = eval("IE_" + DblClickCmdText.substring(0,DblClickCmdText.indexOf("(")));}
		s.onclick = ListBoxSelected;
	
	}else{
	
		s.setAttribute("onmouseover", "SetStyle(this,'over');");
		s.setAttribute("onmouseout", "SetStyle(this,'out');");
		s.setAttribute("ondblclick", DblClickCmdText)
		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);

}

function AddBlankListBoxItem(ParentContainer){

	try{
		s = document.createElement("div");
		s.innerHTML = "There are no items to display.";
		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 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;}
	
	ParentContainer.appendChild(sth);
	ParentContainer.appendChild(stmc);
	
	var spacer = document.createElement("div");
	spacer.className = "ListBox_MainContent_Spacer";

	ParentContainer.appendChild(spacer);

}

function GetContentFromContentTemplate(id){

	var content = document.getElementById(id+"_ContentTemplate").innerHTML;
	document.getElementById(id+"_ContentTemplate").innerHTML = "";
	return content;

}


function AddCategoryItem(catId, catName, parentContainer){

    div = document.createElement("div");
    div.className = "MMC_MyCategory";
    div.setAttribute("originalclass","MMC_MyCategory");

    if(isIE){

        div.onmouseover = SetStyle;
        div.onmouseout = SetStyle;
	
    }else{
	
        div.setAttribute("onmouseover", "SetStyle(this,'over','MMC_MyCategory');");
        div.setAttribute("onmouseout", "SetStyle(this,'out','MMC_MyCategory');");
	
    }    
    
    divIcon = document.createElement("div");
    divIcon.className = "MMC_MyCategory_Icon";
    
    img = document.createElement("img");
    img.src = "/icon/24/app/shadow/folder_red.png";
    if(isIE){img.onload = png;}
    
    divIcon.appendChild(img);
    
    divName = document.createElement("div");
    divName.className = "MMC_MyCategory_Name";
    divName.innerHTML = catName;
    		    
    div.appendChild(divIcon);
    div.appendChild(divName);
    
    parentContainer.appendChild(div);

}


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){}
			
			try{
				c = document.getElementById(prefix+"tab_"+i);
				c.className = "Tab";
			}catch(ex){}
			
			try{
				r = document.getElementById(prefix+"tab_"+i+"_r");			
			
				if(i == maxtabid){
					r.className = "Tab_Right_Last";
				}else{
					r.className = "Tab_Right";
				}
			}catch(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");
		if (l) {
			l.className = "Tab_Right";
		}
		if (c) {
			c.className = "Tab";
		}

		if (r) {
			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";		
		r.className = "Tab_Right_Next";
		
	}
	
	//*******************************************************************************	
	//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");
		if (l) {
			l.className = "Tab_Right_NextOn";
		}
		if (c) {
			c.className = "Tab_On";
		}
		if (r) {
			if (id == maxtabid) {
				r.className = "Tab_Right_LastOn";
			} else {
				r.className = "Tab_Right_On";
			}
		}
	
	}
	
	
	CURRENT_TABS[tabsetid] = id;
	
}

