// Dependencies
// 		window.js
// 		VNWindow
//		CMqURIUtils

//--------------------------------------------------------------------------------------------------------------
//
// Parameters
// 	objMainDocRef: reference to censusmain (main doc with constants.js, etc. referenced.
// 	objControlsSs:	reference to a MqControlsServerSide object
// 	objFrames: reference to a MqFrames object
// 	objLoadDataMgr:  reference to a CMqLoadDataMgr object
//
//--------------------------------------------------------------------------------------------------------------

function CMqRecord(objMainDocRef, objControlsSs, objFrames, objLoadDataMgr){
	
	var m_objRecordId;
	var m_objRecordDisplay;
	var m_objFormElement=new objMainDocRef.CMqFormElement();
	
	var m_objMainDocRef=objMainDocRef;
	var m_objControlsSs=objControlsSs;
	var m_objFrames=objFrames;
	var m_objLoadDataMgr=objLoadDataMgr;
	var m_oTabs=new VNTabs();
	
	var LOAD_REC_CTNR="loadRec";
	var LOAD_REC_OVERLAY_CTNR = "loadRecOverlay";
	
	function Initialize(oCtrlRecordID,oCtrlRecordDisplay)
	{			
		m_objRecordId=oCtrlRecordID;
		m_objRecordDisplay=oCtrlRecordDisplay;	
	}
	
	function SetRecordId(lngRecordId)
	{		
		if ((m_objRecordDisplay) && (m_objRecordId)){
			if (lngRecordId=="") {
				lngRecordId=m_objMainDocRef.NO_CURRENT_RECORD;
			}
			m_objRecordId.value=lngRecordId;
			if (lngRecordId>0) {
				m_objRecordDisplay.value=lngRecordId;
			}else{
				m_objRecordDisplay.value="";
			}
		}	
	}
	
	function GetRecordId()
	{		
		return _GetRecordId(m_objMainDocRef, m_objRecordDisplay, m_objRecordId);
	}
		
	function GetVisibleRecordId()
	{
		if ((m_objRecordDisplay) && (m_objRecordId)){
			return Number(m_objRecordDisplay.value);
		}else{
			return m_objMainDocRef.NO_CURRENT_RECORD;		
		}
	}
		
	
	function DisableRecord(blnDisable)
	{		
		if(m_objRecordDisplay){
			m_objFormElement.Disable(blnDisable, m_objRecordDisplay);				
		}
	}

	function SynchronizeWithIntern()
	{
		var varRecordId;
		//get internally record ID
		varRecordId=this.GetRecordId();
		//set the internal record ID to the visible one
		this.SetRecordId(varRecordId);
		
	}
	function SynchronizeWithVisible()
	{
		var varRecordId;
		//get the visisible record ID
		varRecordId=this.GetVisibleRecordId();
		//set the visble record ID to the internal one
		this.SetRecordId(varRecordId);
	}
	
	function Resize()
	{
		var objLoadRec=objMainDocRef.document.getElementById(LOAD_REC_CTNR);
		if(objLoadRec){
			if(objLoadRec.style.display===""){
				this.Show();
			}
		}
	}
	
	function OnBeforeLoad()
	{
		this.ShowLoadingUI(true);
	}
	
	function OnAfterLoad()
	{
		_WriteTitleInfo(m_objMainDocRef, m_objControlsSs, m_objRecordDisplay, m_objRecordId);
		
		_HandleHistoryButton(m_objMainDocRef, m_objControlsSs, m_objFrames);
	}
	
	function Show()
	{
		if(m_objMainDocRef){
			var objLoadRec=m_objMainDocRef.document.getElementById(LOAD_REC_CTNR);
			
			if(objLoadRec){			
				var arrWinSize=m_objMainDocRef.GetWindowSize(m_objMainDocRef.WIN_MODE_MAXIMIZED, null, null, true);
				var lngWidth=arrWinSize[0];
				var lngHeight=arrWinSize[1];
				var intBodyWidth=arrWinSize[2];
				var intBodyHeight=arrWinSize[3];
				var intBrowserWidth=arrWinSize[4];
				var intBrowserHeight=arrWinSize[5];
				var BORDER_WIDTH=4;
				
				if(parent.document.location.href.toLowerCase()==document.location.href.toLowerCase()){
					//open same URL
					objLoadRec.style.marginTop=(((((intBrowserHeight-lngHeight)/2)-((intBrowserHeight-intBodyHeight)/2))/2)+BORDER_WIDTH)+"px";
					objLoadRec.style.marginLeft=(((((intBrowserWidth-lngWidth)/2)-((intBrowserWidth-intBodyWidth)/2))/2)+BORDER_WIDTH)+"px";
				}else{
					//opened a new view
					lngHeight=intBodyHeight;
					lngWidth=intBodyWidth;
				}
			}
			objLoadRec.style.height=lngHeight+"px";
			objLoadRec.style.width=lngWidth+"px";
			
			_SetRecordTabHeight(lngHeight, m_objMainDocRef);
			
			objLoadRec.style.display="";
		}
	}
	
	function Hide()
	{
		this.ShowLoadingUI(false);
		
		var objLoadRec=m_objMainDocRef.document.getElementById(LOAD_REC_CTNR);
		if(objLoadRec){
			objLoadRec.style.display="none";
		}
	}

	function ShowLoadingUI(blnVisible, blnForceToFront)
	{
		var objLoadRecOverlay=m_objMainDocRef.document.getElementById(LOAD_REC_OVERLAY_CTNR);
		if(objLoadRecOverlay){
			if(blnForceToFront){
				objLoadRecOverlay.style.zIndex=9999;
			}else{
				objLoadRecOverlay.style.zIndex=1001;
			}
			if(blnVisible){
				objLoadRecOverlay.style.display="";
			}else{
				objLoadRecOverlay.style.display="none";
				
			}
		}
	}
	
	function Tabs()
	{
		return m_oTabs;
	}
	

	

	this.Initialize=Initialize;
	this.OnBeforeLoad=OnBeforeLoad;
	this.OnAfterLoad=OnAfterLoad;
	this.Show=Show;
	this.Hide=Hide;
	this.SetRecordId=SetRecordId;
	this.GetRecordId=GetRecordId;
	this.DisableRecord=DisableRecord;
	this.GetVisibleRecordId=GetVisibleRecordId;
	this.SynchronizeWithVisible=SynchronizeWithVisible;
	this.SynchronizeWithIntern=SynchronizeWithIntern;
	this.Resize=Resize;
	this.ShowLoadingUI=ShowLoadingUI;
	this.Tabs=Tabs;
	
	//private static functions

	function _ResolveIssueID(strString, objMainDocRef, objRecordDisplay, objRecordId)
	{
		if(strString){
			var regExp=new RegExp("<ValOfField>1</ValOfField>","g");
			return strString.replace(regExp,_GetRecordId(objMainDocRef, objRecordDisplay, objRecordId));
		}
	}

	//private and static version of GetRecordId
	function _GetRecordId(objMainDocRef, objRecordDisplay, objRecordId)
	{
		if ((objRecordDisplay) && (objRecordId)){
			if (objRecordId.value===""){
				objRecordId.value=objMainDocRef.NO_CURRENT_RECORD;				
			}
			return Number(objRecordId.value);
		}else{
			return objMainDocRef.NO_CURRENT_RECORD;
		}
	}
	
	function _WriteTitleInfo(objMainDocRef, objControlsSs, objRecordDisplay, objRecordId)
	{
		var objTitle=objMainDocRef.document.getElementById("wizTitle");
		var objDesc=objMainDocRef.document.getElementById("wizDesc");
		var objMacros=new objMainDocRef.CMqMacros();
		var strTitleTemplate;
		var strDescTemplate;
		var objExistingIssueBtns=objMainDocRef.document.getElementById("ExistingIssueButtons");
		
		if(!objMainDocRef.g_VWS.RecordMgr().CurrentRecord().IsNewRecord()){
			strTitleTemplate=objMainDocRef.WIZARD_TITLE_EDIT;
			strDescTemplate=objMainDocRef.WIZARD_DESCRIPTION_EDIT;
			if(objExistingIssueBtns){
				objExistingIssueBtns.style.display="";
			}
		}else{
			strTitleTemplate=objMainDocRef.WIZARD_TITLE;
			strDescTemplate=objMainDocRef.WIZARD_DESCRIPTION;
			if(objExistingIssueBtns){
				objExistingIssueBtns.style.display="none";
			}
		}
		
		strTitleTemplate=_ResolveIssueID(strTitleTemplate, objMainDocRef, objRecordDisplay, objRecordId);
		strDescTemplate=_ResolveIssueID(strDescTemplate, objMainDocRef, objRecordDisplay, objRecordId);

		if(objMacros){	
			if(objTitle){			
				objTitle.innerHTML=objMacros.ResolveFieldMacros(strTitleTemplate,m_objLoadDataMgr,false, true);
			}
			if(objDesc){			
				objDesc.innerHTML=objMacros.ResolveFieldMacros(strDescTemplate,m_objLoadDataMgr,false, true);
			}
		}
	}

	function _HandleHistoryButton(objMainDocRef, objControlsSs, objFrames)
	{
		var HISTORY_BTN_NAME="cmdViewHistory";
		var objTabsFrame=objFrames.GetFrameWindow(objMainDocRef.TABS_FRAME_ID);
		if(objTabsFrame){
			var objHistButton=objTabsFrame.document.getElementById(HISTORY_BTN_NAME);
			if(objHistButton){
				if(!objMainDocRef.g_VWS.RecordMgr().CurrentRecord().IsNewRecord()){
					objHistButton.style.display="";
				}else{
					objHistButton.style.display="none";
				}
			}
		}
	}


	function _SetRecordTabHeight(lngWinHeight, objMainDocRef)
	{
		var iRecHeaderHeight=125; // same as in objMainDocRef.document.getElementById("RecHeader").offsetHeight;

		var intRecHeight=lngWinHeight-iRecHeaderHeight-objMainDocRef.DEFAULT_TABS-objMainDocRef.DEFAULT_COMMANDBAR-objMainDocRef.DEFAULT_LOADDATA;
		var objRecFrame=objMainDocRef.document.getElementById(objMainDocRef.RECORD_FRAME_NAME);
					
		if(objRecFrame){
			objRecFrame.style.height=intRecHeight+"px";
			
			objRecFrame=m_objFrames.GetFrameWindow(objMainDocRef.RECORD_FRAME_ID);
			if(objRecFrame){
				var objRecBody;
				
				if(objRecFrame.document){
					objRecBody=objRecFrame.document.body;
					if(objRecBody){
						objRecBody.style.height=intRecHeight+"px";
					}
				}
			}
		}
	}
}

// Represents a VN Record 
function VNRecord(oVecWebSession)
{
	var _me=this;
	var m_oLoadDataMgr;	
	var m_oViewFlds;
	// Array of VNFormFields, one per field
	var m_arrFrmFlds = new Array();
	var m_lngRevisionNumber=-1;
	var m_oVecWebSession=oVecWebSession;
	
	if (oVecWebSession)
	{
		m_oLoadDataMgr=oVecWebSession.LoadDataMgr();	
		m_oViewFlds=oVecWebSession.ViewFields();
	}
	
	function GetFormField(iFldID)
	{

		if (iFldID)
		{
			
			var oFrmFld=m_arrFrmFlds[iFldID];
			
			if (!oFrmFld)
			{
				// create a vn form field from the field definition
				var oFld=m_oViewFlds.Field(iFldID);
				
				if (oFld)
				{
					// Add to array
					oFrmFld= new VNFormField(oFld,m_oLoadDataMgr.GetDataHTMLDocument());
					m_arrFrmFlds[iFldID]=oFrmFld;

				}	
					
			}
			
			return oFrmFld;
								
		}
	}

	// Shortcut to returns the value form element of a dts field
	function FldVals(iFldID)
	{

		if (iFldID)
		{
			// Get the vn form field
			var oVNFrmFld=_me.GetFormField(iFldID);
			
			return oVNFrmFld.GetValueControl();
								
		}
	}
	
	//g_VWS.RecordMgr().CurrentRecord().IsNewRecord()
	function IsNewRecord() 
	{
		if (this.GetRevisionNumber() > 0) 
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	//g_VWS.RecordMgr().CurrentRecord().SetRevisionNumber(x);
	function SetRevisionNumber(lngRevisionNum)
	{
		m_lngRevisionNumber=lngRevisionNum;
	}
	
	//g_VWS.RecordMgr().CurrentRecord().GetRevisionNumber();
	function GetRevisionNumber()
	{
		return m_lngRevisionNumber;
	}
	
	// TODO: pass LoadDataMgr in constructor instead
	function ResolveMacros(sStr,oLoadDataMgr,bResolveChoiceIDsToText)
	{
		if (sStr && oLoadDataMgr && m_oVecWebSession.Macros){
			sStr=m_oVecWebSession.Macros.ResolveFieldMacros(sStr,oLoadDataMgr,
													true,bResolveChoiceIDsToText);
			        
		}
		return sStr;
	
	}
	
	
	this.IsNewRecord = IsNewRecord;
	this.FldVals=FldVals;
	this.GetFormField=GetFormField;
	this.SetRevisionNumber=SetRevisionNumber;
	this.GetRevisionNumber=GetRevisionNumber;
	this.ResolveMacros=ResolveMacros;
}

// Represents a VN Record Form
// TODO: consider initializing VNRec inside
function VNRecForm(oVNRec, oLoadDataMgr, oVecWebSession)
{
	var _me=this;
	var m_oVecWebSession=oVecWebSession;
	var m_oLoadDataMgr=oLoadDataMgr;	
	var m_oVNRec=oVNRec;
	
	
	function GetRecord()
	{
		return m_oVNRec;
	}

	
	function OpenURL(strURL,sWinName, oWinProp, blnAsChildWin)
	{               
		var MAX_URL_LEN=2020;
		if (!sWinName){
			sWinName="_blank";
		}

		// Resolve field macros to the current values if needed 
		m_oVNRec.ResolveMacros(strURL,m_oLoadDataMgr,_me.ResolveChoiceIDsToText);
		
		var objURIUtils=new CMqURIUtils();
		if(objURIUtils){
			strURL=objURIUtils.AddURIProtocol(strURL);
		}

	
		if (strURL.length>MAX_URL_LEN){
			strURL=strURL.substr(0,MAX_URL_LEN);
		}

		if(blnAsChildWin)
		{
			var oWin = new VNWindow(oWinProp.Modal);
			oWin.Open(strURL, sWinName, oWinProp.Width, oWinProp.Height, null, null, oWinProp.SizeMode);
			
		}else{
			window.open(strURL,sWinName);
		}
    }
	
	this.ResolveChoiceIDsToText=false;
	this.GetRecord=GetRecord;
	this.OpenURL=OpenURL;
}

function VNTabs()
{

	this.Visible=true;

}

