/* -------------------------------------------------------------------------------------------------
							      Global Variables definition
   ------------------------------------------------------------------------------------------------- */
var divMainNavTable = null,divNavigationList = null,imgExpandCollapseList = null;
var activeNavId = -1, isMainNavAniInProcess = false, gcommonNavId = '', garrowImageId = '';
var pi = Math.PI;
var isOnlyOneMenuActive = true;
var gblnGotGlobalData = true, gblnGotSubData = true;
var initialArrowSrc='',rtToDnArrowSrc='',dnToRtArrowSrc='';
var align = '';
var numLayers = 0;
/* -------------------------------------------------------------------------------------------------
							     Function to Calculate the filter's length
   ------------------------------------------------------------------------------------------------- */
function calculate(){ 
    if(filterNum && filterNum > 1){
        var tbList = getArrayById('tbFilter','0',filterNum-1);
        var tdFtList = getArrayById('tdFt','0',filterNum-1);    
        var selectList = getArrayById('ft','0',filterNum-1);
        
        var maxLength;
        if(tbList.length < 2) return;
        maxLength = tbList[0].clientWidth;
        for(var i in tbList){
            if(maxLength < tbList[i].offsetWidth){
                maxLength = tbList[i].offsetWidth;
            }
        }
        
        for(var i in tbList){
            if(tbList[i].offsetWidth < maxLength){
                tbList[i].style['width'] = maxLength + 'px';                
                tdFtList[i].style['width'] = tdFtList[i].offsetWidth + (maxLength - tbList[i].offsetWidth) + 'px';
                selectList[i].style['width'] = tdFtList[i].offsetWidth + (maxLength - tbList[i].offsetWidth) + 'px';
            }
        }
    }
}
/* -------------------------------------------------------------------------------------------------
							     Function to Hide Image
   ------------------------------------------------------------------------------------------------- */
function hideImg(){
			var imgExpandCollapseList = getArrayById("aDiv",'1',numLayers);
			
			if(isIE()){
				var headArrowList = getArrayById("headArrow",'1',numLayers);
				if(headArrowList.length > 0){
					for(i=0; i<headArrowList.length; i++){
						headArrowList[i].style.display = "none";
					}
				}
				
				if(imgExpandCollapseList.length > 0){
					for(i=0; i<imgExpandCollapseList.length; i++){
						imgExpandCollapseList[i].style.display = "block";
					}
				}
			}
			else{
				if(imgExpandCollapseList.length > 0){
					for(i=0; i<imgExpandCollapseList.length; i++){
						imgExpandCollapseList[i].style.display = "none";
					}
				}
			}
}
/* -------------------------------------------------------------------------------------------------
							     Function to convert Degree to radian and Vice-versa
   ------------------------------------------------------------------------------------------------- */
function degToRad(x){return (x/(360/(2*pi)));}
function radToDeg(x){return (x*(360/(2*pi)));}
/* -------------------------------------------------------------------------------------------------
							   Function for syncronization of main navigation calls
   ------------------------------------------------------------------------------------------------- */
function syncMainNavigationCalls(dir, nLayer){
	align=dir;
    numLayers=nLayer;
	if(dir == 'rtl'){
		initialArrowSrc = 'initial_globalheadarrow_left';
		rtToDnArrowSrc = 'rt2dwn_ani_left';
		dnToRtArrowSrc = 'dwn2rt_ani_left';	
	}
	else{
		initialArrowSrc = 'initial_globalheadarrow_right';
		rtToDnArrowSrc = 'rt2dwn_ani_right';
		dnToRtArrowSrc = 'dwn2rt_ani_right';	
	}
		
	if(gblnGotGlobalData && gblnGotSubData){
		var divMainNavigationTable = document.getElementById('workfiletable');
					
		collapseAllNav('mainnavigationlist','aDiv');
	}
	else{
		window.setTimeout("syncMainNavigationCalls()",1000);
	}
}
/* -------------------------------------------------------------------------------------------------
					Function to collapse the Main Navigation on click on Arrow image
   ------------------------------------------------------------------------------------------------- */
function collapseAllNav(commonNavId, arrowImageId){
	gcommonNavId = commonNavId;
	garrowImageId = arrowImageId;

	divNavigationList = getArrayById(commonNavId,'1',numLayers);
	for(var j=0;j<divNavigationList.length;j++){			
	    divNavigationList[j].tempOffsetHeight = divNavigationList[j].offsetHeight;
	    attachStyle(divNavigationList[j],'height','0');
	    attachStyle(divNavigationList[j],'overflow','hidden');
	    if(isIE())
	    {	
		    if(align == 'rtl')
			    rotateArrowByIndex(j, 90);
		    else
			    rotateArrowByIndex(j, -90);
	    }	
	}
	
	if(divNavigationList.length > 0){
	    //start: Previous code for animating main navgation menu
		    toggleNav((divNavigationList.length > 1)?2:1,'false');
		//end: Previous code for animating main navgation menu
	}
}

/* -------------------------------------------------------------------------------------------------
					         Functions for rotating the arrow image.
   ------------------------------------------------------------------------------------------------- */
function rotateArrowByIndex(index, angle){
	if(imgExpandCollapseList == null){
	    imgExpandCollapseList = getArrayById("aDiv",'1',numLayers);;
	}
	if(index < imgExpandCollapseList.length){
		var imgToRotate = imgExpandCollapseList[index];
		rotateArrow(imgToRotate, angle);
	}
}

function rotateArrow(el, angle){
	var rad = degToRad(angle);
	var costheta = Math.cos(rad);
	var sintheta = Math.sin(rad);
	try{	
	el.style.filter = "progid:DXImageTransform.Microsoft.Matrix()";
	el.filters.item("DXImageTransform.Microsoft.Matrix").SizingMethod = "auto expand";
	el.filters.item("DXImageTransform.Microsoft.Matrix").FilterType = "bilinear";
	el.filters.item("DXImageTransform.Microsoft.Matrix").M11 = costheta;
	el.filters.item("DXImageTransform.Microsoft.Matrix").M12 = -sintheta;
	el.filters.item("DXImageTransform.Microsoft.Matrix").M21 = sintheta;
	el.filters.item("DXImageTransform.Microsoft.Matrix").M22 = costheta;
	}catch(e){}
}

var headImg ="";

function rotateArrow_onLoad(imgIndex){
	var imgName = "";
	headImg = document.getElementById("headArrow" + imgIndex);
	
	if(headImg.src.indexOf(initialArrowSrc) != -1)
		imgName = initialArrowSrc;
	
	if(headImg.src.indexOf(rtToDnArrowSrc) != -1)
		imgName = rtToDnArrowSrc;	
	
	if(headImg.src.indexOf(dnToRtArrowSrc) != -1)
		imgName = dnToRtArrowSrc;	
			
	if(headImg){
		headImg.src = headImg.src.replace(imgName,rtToDnArrowSrc);	
	}
}

function rotateArrow_dnToRt(imgIndex)
{
	var imgName = "";
	headImg = document.getElementById("headArrow" + imgIndex);
	
	if(headImg.src.indexOf(initialArrowSrc) != -1)
		imgName = initialArrowSrc;
	
	if(headImg.src.indexOf(rtToDnArrowSrc) != -1)
		imgName = rtToDnArrowSrc;	
	
	if(headImg.src.indexOf(dnToRtArrowSrc) != -1)
		imgName = dnToRtArrowSrc;	
			
	if(headImg){
		headImg.src = headImg.src.replace(imgName,dnToRtArrowSrc);	
	}
}

/* -------------------------------------------------------------------------------------------------
					         Function for toggling the menu list of main navigation
   ------------------------------------------------------------------------------------------------- */
function toggleNav(indexP,isClicked){
    var index = indexP - 1; 
	if(isMainNavAniInProcess)
		return;
	if(activeNavId == index){
				
		if(divNavigationList.length > 1){
			activeNavId = (index == 0)?1:0;
			highlightExpandedMenu(activeNavId, true); 
			
			if(!isIE()){
				if(isClicked == "true"){
					rotateArrow_onLoad(activeNavId + 1);
					rotateArrow_dnToRt(index + 1);					
				}
			}
		}
		else{
			rotateArrow_dnToRt(index + 1);
			activeNavId = -1;
		}
		highlightExpandedMenu(index, false);
		openCloseNavigationListOneThread(index, activeNavId, false);
	}
	else{
			
		if(!isIE()){
			if(isClicked == "true")
			{
				if(divNavigationList.length >1)
				{
					rotateArrow_onLoad(index + 1);
					rotateArrow_dnToRt(activeNavId + 1);
				}
				else
				{
					rotateArrow_onLoad(index + 1);
				}				
			}
			else
				rotateArrow_onLoad(index + 1,'true');
		}
		
		highlightExpandedMenu(index, true);
		if(activeNavId != -1)
			highlightExpandedMenu(activeNavId, false); 
		
		openCloseNavigationListOneThread(index, activeNavId, true);
		activeNavId = index;
	}
}
/* -------------------------------------------------------------------------------------------------
					    Function for displaying Main Navigation Menu without animation.
   ------------------------------------------------------------------------------------------------- */
function mainNavMenuNoAnimation(index){
	var currentNavigationList = divNavigationList[index];
	currentNavigationList.style.height = currentNavigationList.tempOffsetHeight;
	activeNavId = index;
}
/* -------------------------------------------------------------------------------------------------
					    Function for highlighting the main navigation menu when expanded
   ------------------------------------------------------------------------------------------------- */
function highlightExpandedMenu(index, action){
	var indexP = index * 1 + 1;
	attachClass('mainnavmenuheader' + indexP,action?"activemenuheader":"inactivemenuheader");
}
/* -------------------------------------------------------------------------------------------------
					    Function for open and close the Navigation as one thread
   ------------------------------------------------------------------------------------------------- */
function openCloseNavigationListOneThread(activeIndex, previousIndex, direction){
	var lActive = true, lPrevious = true;
	if(activeIndex != -1)
		lActive = openCloseNavigationList(activeIndex, direction);
	if(previousIndex != -1)
		lPrevious = openCloseNavigationList(previousIndex, !direction);
	if(lPrevious && lActive)
		isMainNavAniInProcess = false;
	else
		window.setTimeout("openCloseNavigationListOneThread(" + (lActive?-1:activeIndex) + "," + (lPrevious?-1:previousIndex) + "," + direction + ")",1);
} 
/* -------------------------------------------------------------------------------------------------
					    Function for open and close the Navigation list
   ------------------------------------------------------------------------------------------------- */
function openCloseNavigationList(index, direction){
	var newHeight = 0, limitReached = false;
	isMainNavAniInProcess = true;
	var currentNavigationList = divNavigationList[index];
    
    var isHidden = (currentNavigationList.style.display == "none");
	if(isHidden)
		attachStyle(currentNavigationList,'display','block');
		
	if(currentNavigationList){
		newHeight = currentNavigationList.offsetHeight + (direction?10:-10);
		if(newHeight <= 0 && !direction){
			newHeight = 0;
			limitReached = true;
			attachStyle(currentNavigationList,'display','none');
		}
		else if(newHeight >= currentNavigationList.tempOffsetHeight && direction){
			newHeight = currentNavigationList.tempOffsetHeight;
			limitReached = true;
			
			if(divNavigationList.length > 1){
			    var hiddenNavigationList = divNavigationList[index=='1'?'0':'1'];
			    attachStyle(hiddenNavigationList,'display','none');
			}
		}
		attachStyle(currentNavigationList,'height',newHeight + "px");
		
		if(isIE())
		{	
			if(align == 'rtl')
				rotateArrowByIndex(index, (1- newHeight/currentNavigationList.tempOffsetHeight) * 90);
			else
				rotateArrowByIndex(index, (1- newHeight/currentNavigationList.tempOffsetHeight) * -90);
		}
	}		
	return limitReached;
}
/* -------------------------------------------------------------------------------------------------
					    Function to handle toggling of main navigation
   ------------------------------------------------------------------------------------------------- */
function toggleNavTable(table_id,expandarrow){
	if(isMainNavAniInProcess)
		return;
	isMainNavAniInProcess = true;
	divMainNavTable = document.getElementById(table_id);
	if(divMainNavTable == null)
		return;
	
	imgExpandArrow = document.getElementById(expandarrow);
	if(imgExpandArrow == null)
		return;
	var isHidden = (divMainNavTable.style.display == "none");
	if(isHidden){
		attachStyle(divMainNavTable,'display','block');
		imgExpandArrow.src = imgExpandArrow.src.replace('uparrowAni','downarrowAni');
	}else{
		divMainNavTable.tempOffsetHeight = divMainNavTable.offsetHeight;
		imgExpandArrow.src = imgExpandArrow.src.replace('downarrowAni','uparrowAni');
	}
	attachClass(expandarrow, isHidden?"expand":"close");
	divMainNavTable.style.height = isHidden?0:divMainNavTable.offsetHeight;
	attachStyle(divMainNavTable,'overflow','hidden');
	openCloseMainNavigationTable(isHidden);
}
/* -------------------------------------------------------------------------------------------------
					    Function for open and close the Navigation Table
   ------------------------------------------------------------------------------------------------- */
function openCloseMainNavigationTable(direction){
	if(divMainNavTable == null)
		return;
	var newHeight = 0, limitReached = false;
	isMainNavAniInProcess = true;
	newHeight = divMainNavTable.offsetHeight + (direction?10:-10);
	if(newHeight < 0 && !direction){
		newHeight = 0;
		limitReached = true;
	}
	else if(newHeight > divMainNavTable.tempOffsetHeight && direction){
		newHeight = divMainNavTable.tempOffsetHeight;
		limitReached = true;
	}
	attachStyle(divMainNavTable,'height',newHeight + "px");
	if(!limitReached){
		window.setTimeout("openCloseMainNavigationTable(" + direction + ")",1);
	}
	else{
		attachStyle(divMainNavTable,'overflow','visible');
		if(!direction){
			attachStyle(divMainNavTable,'display','none');
		}
		else{
			if(divMainNavTable.style.removeAttribute){
				divMainNavTable.style.removeAttribute("height");
			}
			else{
				divMainNavTable.setAttribute("style","display: block;overflow: visible;");
			}
		}
		isMainNavAniInProcess = false;
	}
}

/* -------------------------------------------------------------------------------------------------
							     Function to attach the class name for a HTML control
   ------------------------------------------------------------------------------------------------- */
function attachClass(id, cName){
	var docElement = "";
	if(id.tagName != null && id.tagName.length >0)
		docElement = id;
	else
		docElement = document.getElementById(id);
		
	if(docElement != null)docElement.className = cName;
}

/* -------------------------------------------------------------------------------------------------
							     Function to dettach the class name for a HTML Element
   ------------------------------------------------------------------------------------------------- */
function detachClass(id){attachClass(id,"")}


/* -------------------------------------------------------------------------------------------------
							Function to attach style to the HTML element
   ------------------------------------------------------------------------------------------------- */
function attachStyle(id,type,value){
	var docElement = "";
	if(id.id != null && id.id.length > 0)
		docElement = id;
	else
		docElement = document.getElementById(id);
		
	if(docElement != null)
	{
		switch(type.toLowerCase())
		{
			case "height" :
				docElement.style.height = value;
				break;
			case "width" :
				docElement.style.width = value;
				break;
			case "display" :
				docElement.style.display = value;
				break;
			case "left" :
				docElement.style.left = value;
				break;
			case "top" :
				docElement.style.top = value;
				break;
			case "margintop" :
				docElement.style.marginTop = value;
				break;
			case "overflow" :
				docElement.style.overflow = value;
				break;
            case "visibility" :
				docElement.style.visibility = value;
				break;
		}
	}
}
/* -------------------------------------------------------------------------------------------------
							Function to set type of heights to the given HTML element.
   ------------------------------------------------------------------------------------------------- */
function setHeight(id,type,value){
	var docElement = "";
	if(id.id != null && id.id.length > 0)
		docElement = id;
	else
		docElement = document.getElementById(id);
		
	if(docElement != null)
	{
		switch(type.toLowerCase())
		{
			case "offsetheight" :
				docElement.offsetHeight = value;
				break;
			case "tempoffsetheight" :
				docElement.tempOffsetHeight = value;
				break;
			case "currentheight" :
				docElement.currentHeight = value;
				break;
			case "clientheight" :
				docElement.clientHeight = value;
				break;
		}
	}
}
/* -------------------------------------------------------------------------------------------------
							Function to get an array according to the given HTML element id prefix and index total number.
------------------------------------------------------------------------------------------------- */
function getArrayById(id,indexBegin, indexEnd){
    var oList = new Array();
    for(var i=indexBegin;i<=indexEnd;i++){
        oList.push(document.getElementById(id+i));
    }
    return oList;
}
/* -------------------------------------------------------------------------------------------------
							Function to judge whether the browse is IE or not
------------------------------------------------------------------------------------------------- */
function isIE(){
    return navigator.appName.indexOf("Microsoft Internet Explorer") != -1;
}
/* -------------------------------------------------------------------------------------------------
							Function to add an event
------------------------------------------------------------------------------------------------- */
function addEvent(elm, evType, fn, useCapture){
    if(elm.addEventListener){
        elm.addEventListener(evType, fn, useCapture);
        return true;
    } 
    else if(elm.attachEvent){ 
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    }
    else{
        elm['on' + evType] = fn;
    }
}