﻿function Application(bAttachErrorHandler){
	this.Window = null;
	this.ShowErrorMessage = 1;
	this.BasePath = "";
	
	if(typeof(bAttachErrorHandler) == 'undefined')
		bAttachErrorHandler = true;
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.ErrorHandler = function(oError){
		if(oError instanceof Error){
			var sMessage = oError.number +": "+ oError.description;
			var sUrl = oError.message;
			var sLine = "";
			var sCharacter = "";
			var sCode = "";
		}else{
			var sMessage = event.errorMessage;
			var sUrl = event.errorUrl;
			var sLine = event.errorLine;
			var sCharacter = event.errorCharacter;
			var sCode = event.errorCode;
			event.cancelBubble = true;
			event.returnValue = true;
		}
		sMessage += "\n\nSrc Element: "+ event.srcElement;
		if(this.ShowErrorMessage == 1){
			alert('Application Error: '+ sMessage +'\n\nFile: '+ sUrl +'\nLine: '+ sLine +'\n\nCharacter: '+ sCharacter +'\nError Code: '+ sCode);
		}
	}
	if(bAttachErrorHandler)
		window.onerror = new Function('window.Application.ErrorHandler()');
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	this.ChangeLocation = function(sLocation, sTarget){
		var oLink = document.createElement("A");
		document.body.appendChild(oLink);
		oLink.target = (typeof sTarget == 'undefined' || sTarget == '') ? self.name : sTarget;
		oLink.display = 'none';
		var reWindowName = new RegExp("sCurrentWindowName=");
		if(sLocation.match(reWindowName) == null) sLocation = sLocation +"&sCurrentWindowName="+ self.name;
		var reDoubleAmp = new RegExp("&&");
		//alert(sLocation);
		//while(sLocation.match(reDoubleAmp) != null){
			//sLocation = sLocation.replace("&&", "&");
			//alert(sLocation +"->"+ sLocation.match(reDoubleAmp));
		//}
		oLink.href = sLocation;
		//window.open(sLocation, self.name);
		oLink.click();
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////
//	this.ReloadPage = function(){
//		currentLocation = self.location.search;
//		var WindowLocation = CWindowLocation(self.location.search);
//		var hRecordID = document.all('hRecordId');
//		if(hRecordID){
//			WindowLocation.SetVariable(hRecordId, hRecordID);
//			var currentLocation = WindowLocation.GetLocation();
//		}
//		this.ChangeLocation(currentLocation, self.name);
//	}
	this.ReloadPage = function(){
		//window.location.reload(true);
		this.ChangeLocation(self.location.search, self.name);
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	this.ClosePopup = function(bRefreshParent){
		if(typeof(bRefreshParent) == "undefined")
			bRefreshParent = false;
		if(bRefreshParent && typeof(window.dialogArguments) != "undefined"){
			window.returnValue = -2;
		}
		window.CloseWindow();
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	this.DualValueMessageBox = function(sMessage, sCaption, arParameters, Handlers, nWidth){
		var sBoxCaption = (!sCaption || sCaption == null) ? "" : sCaption;
		var sBoxMessage = (!sMessage || sMessage == null) ? "" : sMessage;
		var nBoxPopupType = 14;
		var nBoxWidth = (!nWidth) ? 300 : nWidth;
		var Arguments = new Object();
		if(Handlers){
			if(Handlers.OnOK) Arguments.OnOK = Handlers.OnOK;
			if(Handlers.OnClose) Arguments.OnClose = Handlers.OnClose;
			if(Handlers.OnCancel) Arguments.OnCancel = Handlers.OnCancel;
			if(Handlers.OnApply) Arguments.OnApply = Handlers.OnApply;
		}
		var sParameters = "";
		if(arParameters["LeftValue"]) sParameters += "&LeftValue="+ arParameters["LeftValue"];
		if(arParameters["LeftText"]) sParameters += "&LeftText="+ arParameters["LeftText"];
		if(arParameters["RightValue"]) sParameters += "&RightValue="+ arParameters["RightValue"];
		if(arParameters["RightText"]) sParameters += "&RightText="+ arParameters["RightText"];
		
		return this.ShowModalWindow("?nAction=602&hPopup=1&hPopupOptions="+ nBoxPopupType +"&sMessage="+ sBoxMessage +"&sCaption="+ sBoxCaption +"&nWidth="+ nBoxWidth + sParameters, Arguments, nBoxWidth + 30, 500);
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	this.MessageBox = function(sMessage, sCaption, nPopupType, sCheckType, Handlers, sDefaultValue, nWidth){
		var sBoxCaption = (!sCaption || sCaption == null) ? "" : sCaption;
		var sBoxMessage = (!sMessage || sMessage == null) ? "" : sMessage;
		var nBoxPopupType = (!nPopupType || nPopupType == null) ? 11 : nPopupType;
		var sBoxDefaultValue = (!sDefaultValue || sDefaultValue == null) ? "" : sDefaultValue;
		var sBoxCheckType = (!sCheckType || sCheckType == null) ? "" : sCheckType;
		var nBoxWidth = (!nWidth) ? 300 : nWidth;
		var Arguments = new Object();
		if(Handlers){
			if(Handlers.OnOK) Arguments.OnOK = Handlers.OnOK;
			if(Handlers.OnClose) Arguments.OnClose = Handlers.OnClose;
			if(Handlers.OnCancel) Arguments.OnCancel = Handlers.OnCancel;
			if(Handlers.OnApply) Arguments.OnApply = Handlers.OnApply;
		}
		return this.ShowModalWindow("?nAction=601&hPopup=1&hPopupOptions="+ nBoxPopupType +"&sMessage="+ sBoxMessage +"&sCaption="+ sBoxCaption +"&sDefaultValue="+ sBoxDefaultValue +"&nWidth="+ nBoxWidth +"&sCheckType="+ sBoxCheckType, Arguments, nBoxWidth + 30, 300);
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////// S H O W   M O D A L   W I N D O W
	// S H O W   M O D A L   W I N D O W
	this.ShowModalWindow = function(sUrl, oArguments, nWidth, nHeight, bIgnoreReturnValue){
		if(sUrl.indexOf("nNewUI") != -1){
			var arMatch = sUrl.match("sHandlerName=([a-zA-Z_]*)");
			var sHandlerName = arMatch[1];
			var arMatch = sUrl.match("sArgumentElement=([a-zA-Z_]*)");
			var sArgumentElement = arMatch[1];
			eval(sHandlerName+"(document.getElementById('"+sArgumentElement+"'), oArguments);");
			return false;
		}
		//Dump(event.srcElement);
		//var nStartTime = this.GetTime();
		
//		var LoadingWindow = new CLoadingWindow("Application", "Моля изчакайте..");
//		LoadingWindow.Show();
		
		if(typeof(bIgnoreReturnValue) == "undefined")
			bIgnoreReturnValue = false;

		if(!nWidth) nWidth = (this.Window) ? this.Window.m_nMainFrameWidth + 28 : 982;
		if(!nHeight) nHeight = 800; //505

		var sArguments;
		var oArguments;
		var oWindow = window;
		
		sUrl = this.BasePath + sUrl;
		
		//if(oWindow.parent) oWindow = oWindow.parent;
		
		if(typeof(oWindow.dialogArguments) == 'undefined'){
			sArguments = "dialogTop: px; dialogLeft: px; center: Yes;";
		}else{
			var PositionedWindow = oWindow;
			try{
				if(typeof(PositionedWindow.parent) != 'undefined'){
					PositionedWindow = PositionedWindow.parent;
				}
				if(parseFloat(PositionedWindow.dialogWidth) == nWidth && parseFloat(PositionedWindow.dialogHeight) == nHeight){
					sArguments = "dialogTop: "+ PositionedWindow.screenTop +"px; dialogLeft: "+ (PositionedWindow.screenLeft + 20) +"px; center: No;";
				}else{
					sArguments = "dialogTop: px; dialogLeft: px; center: Yes;";
				}
			}catch(e){
				sArguments = "dialogTop: px; dialogLeft: px; center: Yes;";
			}finally{
				//sArguments = "dialogTop: px; dialogLeft: px; center: Yes;";
			}
		}
		if(typeof(oArguments) == 'undefined' || oArguments == null || oArguments == ""){
			oArguments = new Object();
		}
		if(!oArguments.wndOwner) 
			oArguments.wndOwner = oWindow;
		
		var oCurrentWindow = oWindow;
		
//		while(typeof(oCurrentWindow.m_oOpenedModalDialog) != "undefined" && oCurrentWindow.m_oOpenedModalDialog != null){
//			try{
//				if(oCurrentWindow.m_oOpenedModalDialog.closed){
//					oCurrentWindow.m_oOpenedModalDialog = null;
//					break;
//				}else{
//					oCurrentWindow = oCurrentWindow.m_oOpenedModalDialog;
//				}
//			}catch(e){
//				oCurrentWindow.m_oOpenedModalDialog = null;
//			} 
//		}
		
		//Debug.Echo(sUrl);
		//var nEndTime = this.GetTime();
		//alert(nEndTime - nStartTime);
		//alert(sUrl);
		if(typeof(SaveListViewSelectedItems) != "undefined")
			SaveListViewSelectedItems();
		
		var vRetVal = oCurrentWindow.showModalDialog(sUrl, oArguments, "dialogHeight: "+ nHeight +"px; dialogWidth: "+ nWidth +"px; "+ sArguments +"edge: Raised; help: No; resizable: Yes; status: No;");
		//oCurrentWindow.m_oOpenedModalDialog = null;
		//this.HideLoadingWindow();
		//LoadingWindow.Hide();
		if(typeof(vRetVal) != "object" && vRetVal == -2 && !bIgnoreReturnValue){
			//window.setTimeout('window.Application.ChangeLocation("' + oCurrentWindow.location.search + '")', 0);
			window.Application.ChangeLocation(oCurrentWindow.location.search);
		}else{
			return vRetVal;
		}
	}
	
	this.GetTime = function(){
		var oTime = new Date();
		return oTime.getSeconds() + oTime.getMilliseconds() / 1000;
	}
	
}
