﻿function close_ProcessingModal()
{
    $("grpProcessingOverlay").addClassName("Hidden");
    $("grpProcessingMessage").addClassName("Hidden");
    stopped = true;
    return false;
}

var stopped = false;

var QuoteController = (function () {
	function fnQuoteControllerConstructor(oNewView) {
		/****************************************************/
		/*                                                  */
		/*                                                  */
		/*                 Private Variables                */
		/*                                                  */
		/*                                                  */
		/****************************************************/
		
		var oInstance;
		
		var hEvents = $H();
		var oModal;
		
		var oViewHelper;
		
		var oQuoteDAO;
		
		var oOverlay;
		var oProcessingMessage;
		
		/****************************************************/
		/*                                                  */
		/*                                                  */
		/*                  Event Handlers                  */
		/*                                                  */
		/*                                                  */
		/****************************************************/
		
		function continue_onClick(oEvent) {
		    stopped = false;
		    var iStep = oEvent.step;
		    switch (iStep) {
		        case 2:
		            $$("html").first().setStyle({ overflow: "hidden" });
                    var iScrollTop = $$("body").first().cumulativeScrollOffset().top;
                    oOverlay.setStyle({ top: iScrollTop + "px" });
                    oProcessingMessage.setStyle({ top: iScrollTop + (document.viewport.getHeight() / 2) - (oProcessingMessage.getHeight() / 2) + "px" });
                    oOverlay.removeClassName("Hidden");
                    oProcessingMessage.removeClassName("Hidden");
		            QuoteDAO.saveQuote(oContactInformationModel, oLicenseItemModel, save_onComplete);
		            break;
		        default:
		            trackInSiteCatalyst(iStep + 1);
		            oViewHelper.showNextStep(oEvent.step);
		            break;
		    }
		}
		
		function backToFirst_onClick(oEvent)
		{
			trackInSiteCatalyst(1);
			oViewHelper.showPreviousStep(2);
		}
		
		function grpOnlineQuoteContent_onKeyPress(event) {
		    var iKeyCode = event.which ? event.which : event.keyCode;
		    if (iKeyCode == 13)
		        Event.stop(event);
		}
		
		function lstRegion_onChange(event) {
		    oInstance.dispatchEvent("Region Changed", {name: Event.element(event).value});
		}
        
        function oModal_onAddError(oEvent) {
            oModal.dispatchEvent("Add Error", {error: oEvent.error});
        }
        
        function oModal_onClearHeader(oEvent) {
            oModal.dispatchEvent("Clear Header", null);
        }
        
        function oModal_onClearList(oEvent) {
            oModal.dispatchEvent("Clear Error List", null);
        }
        
        function oModal_onSetList(oEvent) {
            oModal.dispatchEvent("Set Error List", {errors: oEvent.errors});
        }
        
        function oModal_onSetHeader(oEvent) {
            oModal.dispatchEvent("Set Header", {text: oEvent.text});
        }
        
        function oModal_onShow(oEvent) {
            oModal.show();
        }
        
        function save_onComplete(oEvent) {
            if(!stopped)
            {
                oInstance.dispatchEvent("Quote Saved", {number: oEvent.sReferenceNumber});
                oProcessingMessage.addClassName("Hidden");
                oOverlay.addClassName("Hidden");
                $$("html").first().setStyle({ overflow: "auto" });
                trackInSiteCatalyst(3);
                oViewHelper.showNextStep(2);
            }
        }
        
        /****************************************************/
		/*                                                  */
		/*                                                  */
		/*                Accessors/Mutators                */
		/*                                                  */
		/*                                                  */
		/****************************************************/
		
		this.setView = function(oNewView) {
			if (oNewView)
				oView = oNewView;
		}
		
		/****************************************************/
		/*                                                  */
		/*                                                  */
		/*            Class Level Private Methods           */
		/*                                                  */
		/*                                                  */
		/****************************************************/
		
		function attachEventListeners() {
		    oInstance.attachEventListener("Continue", continue_onClick);
			oInstance.attachEventListener("BackToFirst", backToFirst_onClick);
		    oInstance.attachEventListener("Add Error To List", oModal_onAddError);
		    oInstance.attachEventListener("Clear Error Modal Header", oModal_onClearHeader);
		    oInstance.attachEventListener("Clear Error Modal List", oModal_onClearList);
		    oInstance.attachEventListener("Set Error Modal List", oModal_onSetList);
		    oInstance.attachEventListener("Show Error Modal", oModal_onShow);
		    oInstance.attachEventListener("Set Error Modal Header", oModal_onSetHeader);
		}       
		
		function attachEvents() {
		    Event.observe($("grpOnlineQuoteContent"), "keypress", grpOnlineQuoteContent_onKeyPress);
		    Event.observe($("lstRegions"), "change", lstRegion_onChange);
		}
		
		function initializeModals() {
		    oModal = new ModalDialog("dlgOnlineQuoteError", "WE'RE SORRY",
					      {display: false, url: "/ModalDialog/implementations/downloaduserintent/html/DownloadUserIntentLeftButton.htm", arguments: null},
					      {display: true, url: "/ModalDialog/implementations/downloaduserintent/html/DownloadUserIntentRightButton.htm", arguments: null},
					      {display: true, url: "/ModalDialog/implementations/onlinequote/error/html/ErrorContent.htm", arguments: null},
					      {display: true, url: "/ModalDialog/implementations/downloaduserintent/html/DownloadUserIntentFooter.htm", arguments: null},
					      null);
		}
		
		function initializeView() {
		    oOverlay = $(document.createElement("div"));
            oOverlay.setAttribute("id", "grpProcessingOverlay");
            oOverlay.addClassName("Overlay");
            oOverlay.addClassName("Semi-Transparent");
            oOverlay.addClassName("Hidden");
            document.body.appendChild(oOverlay);
		
		    oProcessingMessage = $(document.createElement("div"));
            oProcessingMessage.setAttribute("id", "grpProcessingMessage");
            oProcessingMessage.addClassName("Hidden");
            oProcessingMessage.innerHTML = "<img src=\"/customerportal/images/loading.gif\"/><p>Saving...One Moment Please</p><br/><center><input type=\"button\" id=\"oClose_Btn\" class=\"closeBtn\" onclick=\"close_ProcessingModal();\" value=\"Close\"/></center>";
            document.body.appendChild(oProcessingMessage);
		}
		
		
		function trackInSiteCatalyst(iStep) {
		    SiteCatalyst.trackOnlineQuoteByStep(iStep.toString());
		}
		
		/****************************************************/
		/*                                                  */
		/*                                                  */
		/*            Event Dispatcher Interface            */
		/*                                                  */
		/*                                                  */
		/****************************************************/

		this.attachEventListener = function(sEventName, fnEventListener) {
			var aEventListeners = hEvents.get(sEventName);
			if (aEventListeners)
				aEventListeners.unshift(fnEventListener);
			else {
				aEventListeners = $A();
				aEventListeners.push(fnEventListener);
				hEvents.set(sEventName, aEventListeners);
			}
		}

		this.detachEventListener = function(fnEventListener) {
			var aEvents = hEvents.values();
			var aEventListeners;
			EventLoop:
			for (var iEvent = 0; iEvent < aEvents.length; iEvent++) {
				aEventListeners = aEvents[iEvent];
				for (var iEventListener = 0; iEventListener < aEventListeners.length; iEventListener++) {
					oEventListener = aEventListeners[iEventListener];
					if (oEventListener == fnEventListener) {
						aEventListeners.splice(iEventListener, 1);
						break EventLoop;
					}
				}
			}
		}

		this.dispatchEvent = function(sEventName, oEvent) {
		    var aEventListeners = hEvents.get(sEventName);
			if (aEventListeners) {
				var fnEventListener;
				for (var iEventListener = aEventListeners.length - 1; iEventListener >= 0; iEventListener--) {
					fnEventListener = aEventListeners[iEventListener];
					try {
						fnEventListener(oEvent);
					}
					catch (eException) {
						aEventListeners.splice(iEventListener, 1);
					}
				}
			}	
		}
		
		/****************************************************/
		/*                                                  */
		/*                                                  */
		/*                 Initialize Class                 */
		/*                                                  */
		/*                                                  */
		/****************************************************/
		
		oInstance = this;
		
		this.setView(oNewView);
		
		if (oView) {
		    oViewHelper = new QuoteViewHelper(oView);
		    QuoteDAO = new QuoteDAO();
		    initializeModals();
		    initializeView();
			attachEventListeners();
		    attachEvents();
		    trackInSiteCatalyst(1);
		}
		
	}
	
	return fnQuoteControllerConstructor;
})();