var appRequest;
function MqControlsServerSide(){
	var strCurrentLocation;
	var blnIsNormalUnload=false;
	var SUCCESS_SAVE=-3;
	var SAVE_CONFLICT=-1;
	
	var INVALID_ISSUE_ID=0;
	
	var exitPageLoadTarget;
	
	strCurrentLocation=location.protocol+"//"+location.hostname + ":" + location.port+getPath(location.pathname);
	
	function AddNew()
	{
		if (objCustomCode.addNewBefore()){
			var strURL="wcCmdBar.asp?WCI=wciCommand&Command=AddNew";
			
			var strReturn=AddNewRecord(strURL,CONTROL_ADDNEW);
			
			if (strReturn!=objRequest.CANCEL_PROCESS){
				var lngReturn=Number(strReturn);
			
				if(lngReturn>INVALID_ISSUE_ID){
					objCustomCode.addNewAfter();
				}else{
					if (Number(lngReturn)==INVALID_ISSUE_ID) {
						alert(MSG_ERROR_ADDNEW);
					}else{
						alert(strReturn);
					}
				}		
			}
			// return the current record id
			return strReturn;
		}else{
			return false;
		}
	}

	function Copy(lngBaseRecId,bSilent,blnParChd)
	{
		if (objCustomCode.copyBefore(bSilent,lngBaseRecId)){		
			var strParChd="0";
			if(blnParChd){
				strParChd="1";
			}	
			var strURL="wcCmdBar.asp?WCI=wciCommand&Command=Copy&nID="+lngBaseRecId+"&PC="+strParChd;

			var strReturn=AddNewRecord(strURL, CONTROL_COPY);
			if (strReturn!=objRequest.CANCEL_PROCESS){
				var lngReturn=Number(strReturn);
			
				if(lngReturn>INVALID_ISSUE_ID){
					objCustomCode.copyAfter();
				}else{
					if (lngReturn==INVALID_ISSUE_ID) {
						alert(MSG_ERROR_COPY);
					}else{
						alert(strReturn);
					}
				}
				
			}
			// return the current record id
			return strReturn;

		}else{
			return false;
		}
	}
	
	function AddNewRecord(strURL, strControl, strAddLoadDataQS)
	{
		var lngReturn;
		var strUrlPath;	
		
		objRecord.DisableRecord(true);
		strUrlPath=strCurrentLocation+strURL;			
		lngReturn=objRequest.Get(strUrlPath);
						
		if (lngReturn!=objRequest.CANCEL_PROCESS){		
			if (Number(lngReturn)>0){
				objControlsUi.disableControl(strControl);					
				
				SetLoadingStatus();
				
				//set the caption to the current record id
				objRecord.SetRecordId(Number(lngReturn));
				if (strControl!=CONTROL_COPY){
					m_objDefVals.SetApplyDefVals(true);
					
					if(m_objCustomSetVals){
						m_objCustomSetVals.SetApplyDefVals(true);
					}
				}
				loadData(lngReturn,true,strAddLoadDataQS);
				objControlsUi.disableControlButtons();
				//moveTabFrame(0);
			}
		}
		return lngReturn;
	}
	
	function Cancel(lngId,bSilent,bNotLoadPreviousRecord,blnExistingRecord)
	{			
		if (objCustomCode.cancelBefore(bSilent,blnExistingRecord)){
	
			var blnCancel=true;		
			
			if (!(blnExistingRecord)){
				var lngPreviousRecordID=objControls.getWorkingId();
				var strUrlPath=strCurrentLocation+"wcCmdBar.asp?WCI=wciCommand&Command=Cancel&nId="+lngId+"&nValidId="+lngPreviousRecordID;		
				blnCancel=objRequest.Get(strUrlPath);
			}
			
			if (blnCancel!=objRequest.CANCEL_PROCESS){
				if (blnCancel){
					if (!(blnExistingRecord)){
						// set the current record to be the previous one
						objRecord.SetRecordId(lngPreviousRecordID);	
					}
					//if we cancel when we close the browser or refresh the page we do not load the current data  
					if (!bNotLoadPreviousRecord){			
						objControls.View("&ac=can");	
					}		
					objCustomCode.cancelAfter();
					objRecord.DisableRecord(false);
					return blnCancel;
				}
			}
		}
		return false;
	}

	function Save(lngRecordId,lngRevisionNumber,blnAcceptSave,objLoadData, lngSaveOnConflict){
		var blnRun;
		
		if (!blnAcceptSave)	{
			blnRun=objCustomCode.saveBefore(lngRecordId);
		}else {
			blnRun=blnAcceptSave;
		}
		if (blnRun){
			if(m_objWorkFlow){
				m_objWorkFlow.OnSave();
			}
			if(ValidateRequiredFields()){
				var strQueryString=objCustomCode.CommitTimestamp();	
				var strSaveURL=strCurrentLocation+"wcCmdBar.asp?wci=wciCommand&Command=Save";
				
				var strResult=g_VWS.RecordMgr().Save(lngRecordId, lngRevisionNumber, false, strSaveURL, strQueryString, null, null, true);

				if(strResult!=""){
					if (Number(strResult)==SAVE_CONFLICT){
						if (objCustomCode.onConflict(lngRecordId)){
							strResult=g_VWS.RecordMgr().Save(lngRecordId, lngRevisionNumber, false, strSaveURL, strQueryString, 1, null, true);
						}else{
							if(objCustomCode.reloadAfterConflitCancel(lngRecordId)) {
								loadData(lngRecordId,false);
							}
						}
					}
					//in case it changed in the if above
					if(Number(strResult)!=SAVE_CONFLICT){
						if (Number(strResult)!=SUCCESS_SAVE) {
							g_MqRef.MsgHlpr.DisplayMessage(strResult);
								
						} else{
							
							loadData(lngRecordId,false);
							objRecord.DisableRecord(false);
							SetState(COMMANDBAR_FRAME_ID, IDLE_STATE);
							SetIdleStatus();
							objCustomCode.saveAfter();					
							objControlsUi.enableControlButtons();
						}
					}
					return strResult;		
				}else{
					return false;
				}
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	function Submit(objLoadData, fnAfterCall)
	{	
		if(g_MqRef.MqStr.GetQueryStringValue(location.search,"BS")=="1"){
			return BatchSubmit(objLoadData, fnAfterCall);
		}else{
		
			if (objCustomCode.submitBefore()){
				var strExtraQS="";
				var objExtraQS=Record.document.getElementById("txtExtraSubmitArgs");
				if(objExtraQS){
					strExtraQS=objExtraQS.value;
					if(strExtraQS!=""){
						strExtraQS="&" + strExtraQS;
					}
				}
					
				if(ValidateRequiredFields()){
					var strQueryString=objCustomCode.CommitTimestamp();
					strQueryString += objLoadData.GetDataForSave(false, true);
				
					if (strQueryString !== "") {
								
						var strReturn="";
						var strUrlPath;
						var strPostData;
				
						strUrlPath=strCurrentLocation+"wcCmdBar.asp?wci=wciCommand&Command=Submit";
						strPostData ="bQueryString="+g_MqRef.MqStr.MqEncodeURIComponent(strQueryString);
						strPostData+="nId="+Number(objRecord.GetRecordId());
						strPostData+="&txtUploadDir="+ escape(CommandBar.document.CodeForm.txtUploadDir.value);
						strPostData+="&nRevisionNumber=" + g_VWS.RecordMgr().CurrentRecord().GetRevisionNumber();
						strPostData+="&BF=" + g_MqRef.MqStr.MqEncodeURIComponent(objLoadData.GetBatchFieldIDs()) +
									  "&BR=" + objLoadData.GetNumBatchRows() + strExtraQS;
						
						//strReturn=appRequest.Post(strUrlPath,strPostData);
						strReturn=objRequest.Post(strUrlPath,strPostData);
						if (strReturn!=objRequest.CANCEL_PROCESS){
							
							if(fnAfterCall){
								eval(fnAfterCall);
							}else{
								objCustomCode.submitAfter(strReturn);
							}		
							return strReturn;
						}else{
							return false;
						}
					}else{
						return false;
					}
				}else{
					return false;
				}
			}else{
				return false;
			}
		}
	}
	function BatchSubmit(objLoadData, fnAfterCall)
	{
		if (objCustomCode.BatchSubmitBefore()){
			var strReturn="";
			if(ValidateRequiredFields()){
				var strDataForSave=objCustomCode.CommitTimestamp();	
				var strIssueIDs=g_MqRef.MqStr.GetQueryStringValue(location.search,"IDs")
				strDataForSave+=objLoadData.GetDataForSave(true, true);				

				if (strDataForSave !== "") {	
					document.body.style.cursor = "wait";
					var strUrlPath=strCurrentLocation+"wcCmdBar.asp?wci=wciCommand&Command=Batch";
						strPostData ="nId="+strIssueIDs;
						strPostData+="&bQueryString="+g_MqRef.MqStr.MqEncodeURIComponent(strDataForSave);
	
					var strReturn=objRequest.Post(strUrlPath,strPostData);
						
					if (strReturn!=objRequest.CANCEL_PROCESS){					
						if(fnAfterCall){
							eval(fnAfterCall);
						}else{
							objCustomCode.BatchSubmitAfter(strReturn);
						}		
						return strReturn;	
					}else{
						return false;
					}	
				}else{
					return false;
				}			
			}else{
				return false;
			}
		}else{
			return false;
		}
	}  

	function onWindowUnload(){
	
		// If the user did not exit with the 'Exit' button, then call the 
		// timeout process on the server.
		if (!blnIsNormalUnload){		
			cleanUp();		
			var strUrlPath=strCurrentLocation+"wcTopToolbar.asp?WCI=wciLogoff";
			objRequest.Get(strUrlPath);

			//close all the child windows
			OnBeforeNavigate(CLOSE_SCENARIO_USER_LOGS_OFF);
		}
	
	}

	function BeforeExit(){
		if (!blnIsNormalUnload){
			if (objStatus.IsInEditMode()){
				if (window.event){
					event.returnValue = MSG_EXIT_WITHOUT_SAVE;
				}else{
					return MSG_EXIT_WITHOUT_SAVE;
				}
			}
			OnBeforeNavigate(CLOSE_SCENARIO_USER_LOGS_OFF);
		}
	}
	function cleanUp(bSysMsg){
		if (!bSysMsg){
			cleanUpNewRecord();
		}	
		
	}
	
	function cleanUpNewRecord(){
		if (objStatus.IsInEditMode()){
			//delete new record
			var lngCurrentRecordID = objRecord.GetRecordId();
			var lngRevisionNumber = g_VWS.RecordMgr().CurrentRecord().GetRevisionNumber();
			//check if the current record is valid
			if (lngCurrentRecordID>0){			
				//check if it is new record
				if (lngRevisionNumber==0){
						
					//force to cancel the new record
					objControlsSs.Cancel(lngCurrentRecordID,true,true,false);		
					
				}
			}
		}
		
	}
	
	function exit(bSysMsg){	
		return ExitView(bSysMsg, false);
	}

	function SwitchView(){	
		ExitView(false, true);		
	}
	
	function SwitchToDiffView(strViewQS)
	{
		ExitView(false, true, strViewQS);
	}
	
	function ExitView(bSysMsg, blnIsSwitch, strViewQS)
	{
		var strReturn;
		var strUrlPath;
		var blnExit;
		var lngDesiredCloseScenarios = CLOSE_SCENARIO_USER_LOGS_OFF;
		
		var MAX_URL_LEN=1625;
		try{
			if (bSysMsg){
				blnExit=true;
			}else{
				if (IsCompletelyIdle()) {
					blnExit=true;
				}else{	
					var strMessage;
					if(objStatus.IsInEditMode()){
						strMessage=MSG_EXIT_WITHOUT_SAVE;
					}else{			
						strMessage=MSG_EXIT_WITHOUT_SUMMLIST;			
					}

					if (window.confirm(strMessage)) {
						blnExit=true;
						
					}else{
						blnExit=false;
					}
				}
			}
		}catch(errorObject){
			blnExit=true;
		}		
		
		if (blnExit){
			cleanUp(bSysMsg);
			
			if(objVNViewLoader){
				objVNViewLoader.LogOffViews();
			}
			
			if(blnIsSwitch){
				lngDesiredCloseScenarios = CLOSE_SCENARIO_USER_SWITCHES_VIEW;
			}
			
			OnBeforeNavigate(lngDesiredCloseScenarios);	

			strUrlPath=mstrCurrentLocation+"wcTopToolbar.asp?WCI=wciLogoff&";
						
			if(blnIsSwitch){
				strUrlPath+="Redir=1&Swch=1&varCookie=" +
					g_MqRef.MqStr.MqEncodeURIComponent(objCookie.getCookie(objCookie.GetMainCookieName())) + "&";
					
				if(strViewQS){
					if(strViewQS!=""){
						strUrlPath+=strViewQS + "&";
					}
				}
			}
			strUrlPath+=GetExitQueryString();
			blnIsNormalUnload=true;
			if(strUrlPath.length>MAX_URL_LEN){
				strUrlPath=strUrlPath.substring(0, MAX_URL_LEN);
			}
			
			if(exitPageLoadTarget){
				exitPageLoadTarget.document.location.replace(strUrlPath);
			}else{
				document.location.replace(strUrlPath);
			}
		}
		return blnExit;
	}
	
	function UpdateSLA(lngRecordId,bSilent)
	{
		var blnReturn;
		Record.document.Record.style.cursor="wait";
		
		var strURL=strCurrentLocation+"wcCmdBar.asp?WCI=wciCommand&Command=UpdateSLA&nID="+lngRecordId;

		objRecord.DisableRecord(true);
		
		var strReturn=objRequest.Get(strURL);
						
		if (strReturn!=objRequest.CANCEL_PROCESS){		
			if (Number(strReturn)!=SUCCESS_SAVE) {
				alert(strReturn);
			} else{
				loadData(lngRecordId,false);
				objControlsUi.enableControlButtons();
				alert(MSG_SLA_APPLIED_SUCCESSFULLY);
			}
			blnReturn = true;
		}else{
			blnReturn = false;
		}
		
		Record.document.Record.style.cursor = "auto";
		
		return blnReturn;
	}
	
	function Delete(lngBaseRecId, blnNotModal)
	{
		if(blnNotModal){
			OpenChildWindow("TmplConfirmDeleteIssue.htm?nID="+lngBaseRecId,"DelIssue","scrollbars=auto,resizable=yes,width=400,height=150", true, 400, 150);
		}else{
			OpenModalWindow("TmplConfirmDeleteIssue.htm?nID="+lngBaseRecId, 400, 170, false);
		}
	}
	
	function SetExitTarget(objElement){
		exitPageLoadTarget = objElement;
	}
	
	function AddChildIssueRecord(lngBaseRecId,bSilent, lngParentFieldID)
	{
		var blnAdd;
		
		if (IsCompletelyIdle()) {
			blnAdd=true;
		}else{	
			if(objStatus.IsInEditMode()){
				alert(MSG_SAVE_BEFORE_ADD_CHILD_REC);
				blnAdd=false;
			}else{
				blnAdd=true;
			}
		}
		
		if(blnAdd){
			if (objCustomCode.addNewChildBefore()){
				var strURL="wcCmdBar.asp?WCI=wciCommand&Command=AddChildIssueRec&nID="+lngBaseRecId;
				var strAddLoadDataQS="PID="+lngBaseRecId+"&PFID=" + lngParentFieldID;
				var strReturn=AddNewRecord(strURL, CONTROL_ADDCHILDISSUE, strAddLoadDataQS);
				if (strReturn!=objRequest.CANCEL_PROCESS){
					var lngReturn=Number(strReturn);
			
					if(lngReturn<=INVALID_ISSUE_ID){
						if (lngReturn==INVALID_ISSUE_ID) {
							alert(MSG_ERROR_ADDCHILDREC);
						}else{
							alert(strReturn);
						}
					}else{
						objCustomCode.addNewChildAfter();
					}
				}
				// return the current record id
				return strReturn;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	
	this.BeforeExit=BeforeExit;
	this.AddNew=AddNew;
	this.Cancel=Cancel;
	this.Save=Save;
	this.Submit=Submit;
	this.Copy=Copy;
	this.onWindowUnload=onWindowUnload;
	this.exit=exit;
	this.UpdateSLA=UpdateSLA;
	this.SwitchView=SwitchView;
	this.Delete=Delete;
	this.SwitchToDiffView=SwitchToDiffView;
	this.SetExitTarget = SetExitTarget;
	this.AddChildIssueRecord=AddChildIssueRecord;
	
	// Private functions
	function loadData(lngRecordId,blnNewRecord,strAddLoadDataQS){
		
		objRecord.OnBeforeLoad();
		
		if (blnNewRecord){	
			objRecord.Show();
			SetState(LOADDATA_FRAME_ID,BUSY_STATE);
			var strURL="../html/wcCmdBar.asp?WCI=wciLoadData&NewRecord=true&nId="+lngRecordId;
			if(strAddLoadDataQS){
				strURL+="&"+strAddLoadDataQS;
			}
			LoadData.location.replace(strURL);
		}
		else {			
			SetState(COMMANDBAR_FRAME_ID, BUSY_STATE);
			objRecord.SetRecordId(lngRecordId);		
			objControls.View();		
			objRecord.DisableRecord(false);
			SetState(COMMANDBAR_FRAME_ID, IDLE_STATE);
		}
	}
	

	function searchForChar(strSearch,strChar,blnRev){
		var intCount;
		var intLength;
		var intPosition;
				
		intLength=strSearch.length;
		intPosition=-1;
				
		if (!blnRev){
			for (intCount=0;intCount<intLength;intCount++){
				if (strSearch.substring(intCount,intCount+1)==strChar && intPosition==-1) {
					intPosition=intCount;
				}
			}
		}else{
			for (intCount=intLength-1;intCount>=0;intCount--) {
				if (strSearch.substring(intCount,intCount+1)==strChar && intPosition==-1) {
					intPosition=intCount;
				}
			}
		}

		return intPosition;
	}

	function getPath(strPath){
		var strReturn;
		var objString;

		strReturn=strPath.substring(0,searchForChar(strPath,"/",true)+1);
		return strReturn;
	}
	
	function GetExitQueryString(){
		var strExitQueryString="Exit=1";
		var QRY_STR_LOGOFF_TIME_OUT="logofftimeout=";
		
		var strPath = document.URL;
		strPath=strPath.toLowerCase();
		var intLocation = strPath.indexOf(QRY_STR_LOGOFF_TIME_OUT);
		
		if (intLocation>0){
			strExitQueryString+= "&" + strPath.substring(intLocation);
		}
		return strExitQueryString; 
	}
	
	function ValidateRequiredFields()
	{
		if(m_objWorkFlow){
			return m_objWorkFlow.ValidateRequiredFields();
		}else{
			return true;
		}
	}
}
