var m_fOpeningNode=false;
var m_lngActSepCol;

 function initialize()
 {
	var File= new MqFiles;		
	mstrCurrentLocation=File.getCurrentLocation("document");

	if(parent){
		if(parent.objAdmin){
			mobjHTTPrequest=parent.objAdmin.GetHTTPRequest();
		}
	}
	
	if (objRuleMgr){
		objRuleMgr.HTTPRequest = mobjHTTPrequest;
		objRuleMgr.CurrentLocation=mstrCurrentLocation;
	}
	EnableTemplateButtons();
 }

function CallWhileWait(strFunctionEval) {
     SetDocumentCursor("wait");
     window.setTimeout (strFunctionEval, 1);
}
 
 
function SetDocumentCursor(strCursor){	
	if (strCursor=="wait"){
		document.images.imgbusy.src="../../../Theme/images/busyHourGlass.gif";	
		document.getElementById("wait").style.visibility="visible";
	}else{
		document.images.imgbusy.src="../../../Theme/images/spacer.gif";
		document.getElementById("wait").style.visibility="hidden";
	}
	
}  

function OnChangeProject()
{
	CallWhileWait('GetGroupsByProject(0,0,null,true)');
	EnableTemplateButtons();
}

function EnableTemplateButtons()
{
	if(document.getElementById("lstProjects").selectedIndex>0){
		enableImage("Group");
		enableImage("Rule");
		enableImage("Apply");
	}else{
		disableImage("Group");
		disableImage("Rule");
		disableImage("Apply");
		disableImage("up");
		disableImage("down");
	}
}

//function OnBeforeRowSelect(rowid, multi){

	
//}

function SetSavedFlag(blnCondition){

	document.frmWorkflow.txtSaveVals.value=blnCondition;
}

function GetSavedFlag(){
	return document.frmWorkflow.txtSaveVals.value;
}


function CheckIsSaved(strSavedString){
	var lngLastIndex;
	var lngFirstIndex;
	
	
	while(strSavedString.indexOf(SAVED_MSG) != -1){
		lngLastIndex = strSavedString.lastIndexOf(SAVED_MSG);
		
		if (lngLastIndex>0){
			strSavedString=strSavedString.substring(0,lngLastIndex);
		}else{
			lngFirstIndex=strSavedString.indexOf(SAVED_MSG);
			
			if(lngFirstIndex!= -1){
				strSavedString=strSavedString.substring(parseInt(lngFirstIndex)+parseInt(SAVED_MSG.length));
			}
		}
	}
	return strSavedString;
}


function ClearAll()
{
	ClearAllGroups();
	
	if(m_objCondMgr){
		m_objCondMgr.ClearAll();
	}
	
	if (objActionMgr){
		objActionMgr.ClearAll(m_objMqTreeUtils);
	}
}


function RefreshWindow(){
    window.location.replace("MqWorkflow.ASP?WCI=tmplWorkflow");
}
 
function FillCombo(strString, strSep, strSep2, objCombo, blnOrder, strSelectedValue)
{
	var arrValues;
	var i,j;
	if (objCombo){
		if (strString){
			if (strString.length > 0){
				objCombo.removeAll();
				
				var arrCombos = strString.split(strSep);
				
				for(i=0; i<arrCombos.length; i++){

					if(arrCombos[i].length>0){
						arrValues=arrCombos[i].split(strSep2);

						for(j=0; j<arrValues.length; j+=2){
							objCombo.add(unescape(arrValues[j+1]));
						}
					}
				}
				
				if (strSelectedValue){
					objCombo.select(strSelectedValue);
				}
				return true;
			}else{
				objCombo.add(strString);
				return true;
			}
		}
	}
}

function ApplyWorkflow(){

		var strCurrentProject=GetProject();
		objString.InitializeString(MSG_APPLY_RULES_TO_ALL_VIEWS);
		var strMsg=objString.FormatStringVar(unescape(strCurrentProject) + ";",";");
	
		if (window.confirm(strMsg)===true){
	
			var objRequest=mobjHTTPrequest;
			var strCurrentLocation=mstrCurrentLocation;
	
			var strAll = objRequest.Get(strCurrentLocation + "MqWorkflow.ASP?WCI=ApplyWorkflow&CurrentProject=" 
					+ escape(strCurrentProject), "ADMIN");

			if (strAll==RESULT_FALSE){
				alert(MSG_WORKFLOW_NOT_APPLIED);
			}else{
				if (strAll!=HTTP_REQ_CANCEL_PROCESS){
					alert(MSG_WORKFLOW_APPLIED);
				}
			}
		}
	SetDocumentCursor("");
}


function ResizeForm()
{
	ResizeTemplatesGrid();
	ResizeRulesGrid();
}

function WriteTemplatesGrid()
{
	//ResizeTemplatesGrid();
	
	tmplGrid = new dhtmlXGridObject('tmpl_grid');
	tmplGrid.setImagePath("../../../js/codebasegrid/imgs/");
	tmplGrid.setNoHeader(false);
	tmplGrid.setColAlign("left");
	tmplGrid.setSkin("dhx_skyblue");
				
	tmplGrid.setHeader("ID,Name,Locked,Type,Type ID,Enabled,Trigger");
	tmplGrid.setInitWidthsP("0,*,0,25,0,15,15");
	tmplGrid.setColTypes("ro,ro,ro,ro,ro,ro,ro");
	tmplGrid.setColSorting("str,str,str,str,str,str,str");
	tmplGrid.init();
	tmplGrid.attachEvent("onRowSelect", OnSelectTemplate);
}

function ResizeTemplatesGrid()
{
	var objGrid=document.getElementById("tmpl_grid");
				
	if(objGrid){
					
		var intBodyHeight;
		var intBodyWidth;
					
		if (document.all){
			if (document.documentElement && document.documentElement.offsetHeight){
				intBodyHeight=document.documentElement.offsetHeight;
			}else{
				intBodyHeight=document.body.offsetHeight;
			}
			if (document.documentElement && document.documentElement.offsetWidth){
				intBodyWidth=document.documentElement.offsetWidth;
			}else{
				intBodyWidth=document.body.offsetWidth;
			}
		}else{
			intBodyHeight=window.innerHeight;
			intBodyWidth=window.innerWidth;
		}
		if(intBodyHeight>200){
			objGrid.style.height=((intBodyHeight-200)*.40) + "px";	
		}
		if(intBodyWidth>200){
			objGrid.style.width=(intBodyWidth*.75) + "px";	
		}
	}
}

function WriteRulesGrid(arrRuleCols)
{
	//ResizeRulesGrid();
	
	rulesGrid = new dhtmlXGridObject('rules_grid');
	rulesGrid.setImagePath("../../../js/codebasegrid/imgs/");
	rulesGrid.setNoHeader(false);
	rulesGrid.setColAlign("left");
	rulesGrid.setSkin("dhx_skyblue");
	
	var strHeader="";
	var strWidths="";
	var strTypes="";
	var strSort="";
	var strAttHdr="";
	
	m_lngActSepCol=-1;
	var blnNextColHidden=false;
	var lngColHiddenCount=0;

	if(!arrRuleCols){
		strHeader="ID,<div style='color:#000000'>Conditions</div>,<div style='color:#000000'>Actions</div>";
		strWidths="0,50,*";
		strTypes="ro,ro,ro";
		strSort="str,str,str";
	}else{
		var lngNumCondCols=0;
		var lngNumActCols=0;
		for(var y=0;y<arrRuleCols.length; y++){
			if(arrRuleCols[y]=="*"){
				lngNumCondCols=y;
			}
		}
		lngNumActCols=arrRuleCols.length-lngNumCondCols;
		
		var lngPercWidth=50/((lngNumCondCols-1)/4);
		for(var x=0; x<arrRuleCols.length; x++){
			

			if(g_MqRef.MqStr.Right(arrRuleCols[x], 7)=="_Hidden"){
				strWidths+="0" + ",";
			}else{
				if(arrRuleCols[x]=="*"){
					strWidths+="0" + ",";
					m_lngActSepCol=x+1;
					lngPercWidth=49/((lngNumActCols-1)/4);
				}else{
					strWidths+=lngPercWidth + ",";
				}
			}

			if(x==0){
				strAttHdr+="#rspan" + ",";
				strHeader+="ID,";
			}else{
				if(x==1){
					strAttHdr+=arrRuleCols[x] + ",";
					strHeader+="<div style='color:#000000'>Conditions</div>,";
				}else{
					if(x==m_lngActSepCol){
						strAttHdr+=arrRuleCols[x] + ",";
						strHeader+="<div style='color:#000000'>Actions</div>,";
					}else{
						strAttHdr+=arrRuleCols[x] + ",";
						strHeader+="#cspan" + ",";
					}
				}
			}
			
				
			strTypes+="ro,";
			strSort+="str,";
		}
		strHeader=g_MqRef.MqStr.StripTrailing(strHeader, ",");
		strAttHdr=g_MqRef.MqStr.StripTrailing(strAttHdr, ",");
		strWidths=g_MqRef.MqStr.StripTrailing(strWidths, ",");
		strTypes=g_MqRef.MqStr.StripTrailing(strTypes, ",");
		strSort=g_MqRef.MqStr.StripTrailing(strSort, ",");		
	}
	
	rulesGrid.setHeader(strHeader);
	rulesGrid.setInitWidthsP(strWidths);
	rulesGrid.setColTypes(strTypes);
	rulesGrid.setColSorting(strSort);
	if(strAttHdr!=""){
		var arrAttHdr=strAttHdr.split(",");
		if(arrAttHdr){
			rulesGrid.attachHeader(arrAttHdr);
		}
	}
	rulesGrid.init();
	rulesGrid.attachEvent("onRowSelect", ClickRuleCell);
}

function ResizeRulesGrid()
{
	var objGrid=document.getElementById("rules_grid");
				
	if(objGrid){
					
		var intBodyHeight;
		var intBodyWidth;
					
		if (document.all){
			if (document.documentElement && document.documentElement.offsetHeight){
				intBodyHeight=document.documentElement.offsetHeight;
			}else{
				intBodyHeight=document.body.offsetHeight;
			}
			if (document.documentElement && document.documentElement.offsetWidth){
				intBodyWidth=document.documentElement.offsetWidth;
			}else{
				intBodyWidth=document.body.offsetWidth;
			}
		}else{
			intBodyHeight=window.innerHeight;
			intBodyWidth=window.innerWidth;
		}

		if(intBodyHeight>200){
			objGrid.style.height=((intBodyHeight-200)*.50) + "px";	
		}
		if(intBodyWidth>200){
			objGrid.style.width=(intBodyWidth*.75) + "px";	
		}
	}
}

function ResizeParams()
{
	var intBodyHeight;
					
	if (document.all){
		if (document.documentElement && document.documentElement.offsetHeight){
			intBodyHeight=document.documentElement.offsetHeight;
		}else{
			intBodyHeight=document.body.offsetHeight;
		}
	}else{
		intBodyHeight=window.innerHeight;
	}

	if(intBodyHeight>300){
		document.getElementById("Params").height=(intBodyHeight*.51) + "px";	
	}
}

function GetTextButtonHTML(strFunction, strID, strCaption, strTooltip, strBTNOffset)
{
	var strHTML='<div style="padding-right:15px; padding-bottom:10px"><div class="TextButton '+ strBTNOffset +'" style="width:75px;height:30px;cursor:hand;cursor:pointer" onclick="' + strFunction + '" id="btnAction' + strID + '"' +
			 '	onmouseover="this.className=\'TextButton TextButtonOver\'; document.getElementById(\'lnkAction' + strID + '\').className=\'TextButton TextButtonOver\'" ' +
			 '	onmouseout="this.className=\'TextButton\'; document.getElementById(\'lnkAction' + strID + '\').className=\'TextButton\'">' +
			 '	<table height="100%"><tr>' +
			 '	<td valign="middle" class="TextButton" style="padding-left:10px" id="lnkAction' + strID + '">' + strCaption + 
			 '  </td></tr></table>' +
			 '	</div></div>';
	return strHTML;
}

