function CMqWorkFlowRules(objViewFldDefs,oLoadDataMgr, oMqAutoList, oDisplayElements){
	var DEFAULT_SRC_PAGE = "../ASP/GetWorkFlowValues.asp?m=1&";
	var m_varDependentFields;
	var m_rsOrigFldVals;
	var m_blnVerifyCondFldsExist;
	var m_oStaticDepFlds=new MqDependentFields();
	var m_objViewFldDefs=objViewFldDefs;
	var m_oLoadDataMgr = oLoadDataMgr;
	var m_oMqAutoList=oMqAutoList;
	var m_oDisplayElements=oDisplayElements;
	var m_arrDisabledBizRuleIDs=new Array();
	var m_arrRuleOrder=new Array();
	var m_blnBizRlsLoaded=false;

	function Initialize(objFieldCollection,strHTMLContainer,objMqAutoList, objDisplayElements, blnVerifyCondFldsExist, strExtraHTMLContainers){
		if (!m_varDependentFields){
			m_varDependentFields= new Array();
		}

		m_blnVerifyCondFldsExist = blnVerifyCondFldsExist;
		var INDEX_FIELD_ID=0;

}//End Function

	function OnLoad(){		
		WriteConsole("[Wrkflow rules] OnLoad");
		var iDepFldsID;
		if (m_varDependentFields){
			if(m_arrRuleOrder){
				for (var i=0;i<m_arrRuleOrder.length;i++){
					iDepFldsID=m_arrRuleOrder[i];
					if(m_varDependentFields[iDepFldsID]){
						if(m_varDependentFields[iDepFldsID].GetTriggerOnLoadRec()){
							WriteConsole("[Wrkflow rules] OnChange" + iDepFldsID);
							OnChange(iDepFldsID);
						}
					}
				}
			}		
		}
	}
	
	function OnChange(intID, blnOnFieldChange,strTriggerFieldID){
		if (m_varDependentFields){			
			if (m_varDependentFields[intID]){
				m_varDependentFields[intID].SetDependentField(m_rsOrigFldVals, blnOnFieldChange, m_blnVerifyCondFldsExist, strTriggerFieldID);
			}
			
		}
	}
	
	function SetOrigFldValues(rsOrigFldVals)
	{
		m_rsOrigFldVals=rsOrigFldVals;
	}
	
	function ValidateRequiredFields()
	{
		var blnReturn=true;
		
		if (m_varDependentFields){
			for (var i=0;i<m_varDependentFields.length;i++){
				if(m_varDependentFields[i]){
					blnReturn=m_varDependentFields[i].ValidateRequiredFields();
				
					if(blnReturn==false){
						break;
					}
				}
			}		
		}
		return blnReturn;
	}
	function GetDependentFields(){
		return m_varDependentFields;
	}
	
	function SetDependentFields(varDependentFields){
		m_varDependentFields=varDependentFields;
	}

	function GetOrigFldValues()
	{
		return m_rsOrigFldVals;
	}
	
	function GetVerifyCondFldsExist()
	{
		return m_blnVerifyCondFldsExist;
	}

	function LoadBizRules()
	{
		if(!m_blnBizRlsLoaded){
			WriteConsole("[WorkflowRules] Loading business rules from server. URL:" + window.location); 
			g_MqRef.MqFS.LoadScript("../asp/GetBizRules.asp?rd=" + Math.floor(Math.random()*1000001),null, document);  
			m_blnBizRlsLoaded=true;
		}
	}
		
	function AddBizRule(oBizRuleParams)
	{
	
		if (oBizRuleParams && m_objViewFldDefs && m_oLoadDataMgr && m_oMqAutoList && m_oDisplayElements)
		{
			var strRefToWkflObj="";
			
			if (!m_varDependentFields){
				m_varDependentFields= new Array();
			}
			
			if(!BizRuleExists(oBizRuleParams.GlobalID)){

				var intOrder=m_varDependentFields.length;

				WriteConsole("[WorkflowRules] Adding new rule with GlobalID:" + oBizRuleParams.GlobalID + " and current Order:" + intOrder );
							
				m_varDependentFields[intOrder]=new MqDependentFields(m_objViewFldDefs,m_oLoadDataMgr.GetDataHTMLDocument(),m_oMqAutoList);
				
				var blnAdded = false;
				if(typeof(oBizRuleParams.AddAtBeginning)!="undefined"){
					if(oBizRuleParams.AddAtBeginning==true){
						m_arrRuleOrder.unshift(intOrder);
						blnAdded=true;
					}
				}
				if(!blnAdded){
					m_arrRuleOrder[m_arrRuleOrder.length]=intOrder;
				}

				var arrCondFields=oBizRuleParams.Conditions;
			
				if (oBizRuleParams.TriggerOnChange==true){
					for (i=0;i<arrCondFields.length;i++){
						if (arrCondFields[i]){
							var strFieldID=arrCondFields[i].FieldID;
							if (strFieldID){
								m_varDependentFields[intOrder].SetOnChange(strFieldID,strRefToWkflObj+
										"m_objWorkFlow.OnChange("+intOrder+", true,"+strFieldID+");");
							}
						}
					}
					// Assumption: most of dependent fields don't display current value - workflow does
					m_varDependentFields[intOrder].DisplayCurrentValue=false;
				}else{
					m_varDependentFields[intOrder].DisplayCurrentValue=true;
				}
					
				MakeParamsBackwardsCompatible(oBizRuleParams.Conditions, oBizRuleParams.Actions);
				
				if (oBizRuleParams.URL) {
				    DEFAULT_SRC_PAGE = oBizRuleParams.URL;
				}

				m_varDependentFields[intOrder].Initialize(oBizRuleParams.Actions,oBizRuleParams.Conditions,
							DEFAULT_SRC_PAGE, oBizRuleParams.GlobalID, false, m_oDisplayElements, oBizRuleParams.LocalConditions); 
				m_varDependentFields[intOrder].SetGlobalID(oBizRuleParams.GlobalID);

				if(typeof(oBizRuleParams.SelectFirstValue)!="undefined"){
					m_varDependentFields[intOrder].SetIsSelectFirstValue(oBizRuleParams.SelectFirstValue);
				}

				if(m_varDependentFields[intOrder].GetTriggerOnLoadRec()){
					OnLoad();
				}
			}
		}else{
			WriteConsole("[WorkflowRules] Objects not initialized," + oBizRuleParams );
		}
		return true;
	}

	function BizRuleExists(strGlobalID)
	{
		var blnReturn=false;
		if(m_varDependentFields){
			for(var x=0; x<m_varDependentFields.length;x++){
				if(m_varDependentFields[x]){
					if(m_varDependentFields[x].GetGlobalID()==strGlobalID){
						blnReturn=true;
						break;
					}
				}
			}
		}
		return blnReturn;
	}
	
	function MakeParamsBackwardsCompatible(arrCondFields,arrActions)
	{
		var IDX_COND_FIELD_ID=0;
		var IDX_COND_FIELD_NAME=1;
	
		var INDEX_FIELD_ID=0;
		var INDEX_FIELD_NAME=1;
		var INDEX_ACTION_TYPE=2;
		var i;
		
		for (i=0;i<arrCondFields.length;i++){
			if (arrCondFields[i]){
				// Add backwards compatible elements
				arrCondFields[i][IDX_COND_FIELD_ID]=arrCondFields[i].FieldID;
				arrCondFields[i][IDX_COND_FIELD_NAME]=arrCondFields[i].ParamReplace;
			}
		}

		for (i=0;i<arrActions.length;i++){
			if (arrActions[i]){
				// Add backwards compatible elements
				arrActions[i][INDEX_FIELD_ID]=arrActions[i].TargetFieldID;
				arrActions[i][INDEX_FIELD_NAME]=arrActions[i].ValueSource;
				arrActions[i][INDEX_ACTION_TYPE]=arrActions[i].ActionType;
			}
		}

	}
	
	function SetDisabledBizRuleIDs(arrBizRuleIDs)
	{
		m_arrDisabledBizRuleIDs=arrBizRuleIDs;
	}
	
	this.Initialize=Initialize;	
	this.OnLoad=OnLoad;
	this.OnChange=OnChange;
	this.SetOrigFldValues=SetOrigFldValues;
	this.ValidateRequiredFields=ValidateRequiredFields;
	this.GetDependentFields=GetDependentFields;
	this.SetDependentFields=SetDependentFields;
	this.GetOrigFldValues=GetOrigFldValues;
	this.GetVerifyCondFldsExist=GetVerifyCondFldsExist;
	this.LoadBizRules=LoadBizRules;
	this.AddBizRule=AddBizRule;
	this.SetDisabledBizRuleIDs=SetDisabledBizRuleIDs;
}

function CMqLoadDataViewActions(){
	var m_objLoadDVActions;
	var m_objLoadData;
	
	function SetLoadDataMgr(objLoadDataMgr){
		m_objLoadData=objLoadDataMgr;
		
	}	

	function Add(varCondFields,varDVFields,varDataViews,objHTTPRequest,strHTTPLocation,varCondVals,objRecord){
		
		if (!m_objLoadDVActions){
			m_objLoadDVActions=new Array();			
		}		
		var lngIndex=-1;
		if (varCondFields && varDataViews){
			if ((varCondFields.length>0) && (varDataViews.length>0)){
				lngIndex=m_objLoadDVActions.length;
				m_objLoadDVActions[lngIndex]=new CMqLoadDataViews(varCondFields,varDVFields,varDataViews,m_objLoadData,objHTTPRequest,strHTTPLocation,varCondVals,objRecord);
			}
		}
		return lngIndex;
	}
	
	function Item(lngIndex){
		var objAction;
		if (m_objLoadDVActions){
			objAction= m_objLoadDVActions[lngIndex];
		}
		return objAction;
	}
	function Reset(){
		if (m_objLoadDVActions){
			for(var i=0;i<m_objLoadDVActions.length;i++){
				if (m_objLoadDVActions[i]){
					m_objLoadDVActions[i].Reset();
				}
			}
		}
	}
	this.Add=Add;
	this.Item=Item;
	this.Reset=Reset;
	this.SetLoadDataMgr=SetLoadDataMgr;

}

function CMqLoadDataViews(varCondFields,varDVFields,varDataViews,objDataMgr,objHTTPRequest,strHTTPLocation,varCondVals,objRecord){
	var m_strCondition="";
	//start constructor
	var m_varCondFields=varCondFields;
	var m_varDataViews=varDataViews;
	var m_objLoadData=objDataMgr;	
	var m_objHTTPRequest=objHTTPRequest;
	var m_strHTTPLocation=strHTTPLocation;
	var m_varDVFields =varDVFields;
	var m_varCondVals=varCondVals;
	var m_objRecord=objRecord;
	//end constructor

	function SetCondFields(varCondFields){
		m_varCondFields=varCondFields;
	}
	
	function GetCondFields(){
		return m_varCondFields;
	}
	
	function SetCondVals(varCondVals){
		m_varCondVals=varCondFields;
	}
	
	function GetCondVals(){
		return m_varCondVals;
	}
	
	function SetDataViews(varDataViews){
		m_varDataViews=varDataViews;
	}
	
	function GetDataViews(){
		return m_varDataViews;
	}
	

	function Reset(){
		if(!m_varCondVals){
			m_strCondition=BuildCond(m_varCondFields,m_objLoadData);		
		}
	}
	
	function Apply(){
		if (m_objLoadData){	
			var strNewCond=BuildCond(m_varCondFields,m_objLoadData,m_varCondVals);
			if (IsCondChanged(m_strCondition,strNewCond) || !IsDVLoaded(m_varDVFields,m_objLoadData)){
				m_strCondition=strNewCond;									
				var objRsValue= m_objLoadData.GetObjFieldValues().GetFromServer(m_varDataViews,m_strCondition,m_objHTTPRequest,m_strHTTPLocation);		
				if (objRsValue){
					m_objLoadData.Load(objRsValue);
				}
			}
		}
	}
	

	this.SetCondFields=SetCondFields;
	this.GetCondFields=GetCondFields;
	this.SetDataViews=SetDataViews;	
	this.GetDataViews=GetDataViews;
	this.Reset=Reset;
	this.Apply=Apply;
	this.SetCondVals=SetCondVals;
	this.GetCondVals=GetCondVals;
	
	//private methods
	function BuildCond(varCondFields,objDataMgr,varCondVals){
		var strCond="";
		var blnGetFromDoc=true;
		var blnNoCond=false;
		
		var strVal="";
		if (objDataMgr){
			if(varCondFields){
				if(varCondVals){
					if(varCondFields.length==varCondVals.length){
						for(x=0;x<varCondFields.length;x++){
							strVal=varCondVals[x];
							if(strVal=="%RecordNumber%"){
								if(m_objRecord){
									strVal=m_objRecord.GetRecordId();
									if(strVal<=0 || strVal==""){
										blnNoCond=true;
										break;
									}
								}
							}
							strCond+=varCondFields[x] + "=" + strVal + "&";
						}
						strCond=g_MqRef.MqStr.StripTrailing(strCond, "&");
						strCond=g_MqRef.MqStr.MqEncodeURIComponent(strCond);
						blnGetFromDoc=false;
					}
				}
				if(blnNoCond){
					strCond="";
				}else{
					if(blnGetFromDoc){
						strCond=objDataMgr.GetObjFieldValues().GetFromDocument(varCondFields);		
					}
				}
			}
		}
		return strCond;
	}
	
	function IsCondChanged(strCurrentCond,strNewCond){
		return !(strCurrentCond==strNewCond);
	}

	function IsDVLoaded(varDVFields,objDataMgr)
	{
		var blnLoaded =true;
		if (varDVFields && objDataMgr){
			for(var i=0;i<varDVFields.length;i++){
				if (!objDataMgr.GetFieldValue(varDVFields[i],false,false)){
					blnLoaded=false;
					break;
				}
			}
		}
		return blnLoaded;
	}
	
}