﻿var ___bc = null;
var ___puc = null;
var ___cpc = null;

var ___OnLoadEvents = [];
var ___ImageCache = [];

CacheImage("LoadingImage", "Skins/Common/images/loading-gray.gif");
window.onload = ___HandleOnLoadEvent;

function ___HandleOnLoadEvent()
{
    for(var i = 0; i < ___OnLoadEvents.length; i++)
    {
        try
        {
            eval("___OnLoadEvents[i]()");
        }
        catch(err)
        {
        }
    }
}

function AddOnloadEventFunction(pOnloadFunction)
{
    ___OnLoadEvents[___OnLoadEvents.length] = pOnloadFunction;
}

function CacheImage(pName, pURL)
{
    ___ImageCache[pName] = new Image();
    ___ImageCache[pName].src = pURL;
}

function GetCached(pName)
{
    return ___ImageCache[pName];
}



//Gets the singleton instance of the browser controller
function GetBC()
{
    if(___bc == null)
    {
       ___bc = new BrowserController(); 
    }
    return ___bc;
}

//gets the singleton instance of the popitup controller
function GetPUC()
{
    if(___puc == null)
    {
        ___puc = new PopItUpController();
    }
    return ___puc;
}

//gets the singleton instance of the catalogue preview controller
function GetCPC() {
	if (___cpc == null) {
		___cpc = new CataloguePreviewController();
	}
	return ___cpc;
}

var ___dicc = null;
function GetDICC() {
	if (___dicc == null) {
		___dicc = new DateImageChangeController();
	}
	return ___dicc;
}

DateImageChangeController = function() {
	this._Items = new Array();
}

DateImageChangeController.prototype =
{
	AddItem: function(pStartDate, pEndDate, pImageId, pTargetSource) {
		var item = new DateImageChangeHelper(pStartDate, pEndDate, pImageId, pTargetSource);
		this._Items.push(item);
	},
	Execute: function() {
		var d = new Date();
		for (k1 in this._Items) {
			var item = this._Items[k1];
			if (d >= item.StartDate && d < item.EndDate) {
				var img = document.getElementById(item.ImageId);
				if (img) {
					img.src = item.TargetSource;
				}
			}
		}
	}
}


DateImageChangeHelper = function(pStartDate, pEndDate, pImageId, pTargetSource) {
	this.StartDate = pStartDate;
	this.EndDate = pEndDate;
	this.ImageId = pImageId;
	this.TargetSource = pTargetSource;
}

BrowserController = function()
{
    if(___bc != null)
    {
        throw("An instance of the Browser Controller already exists. Use GetBC() instead.");
    }
    this._BrowserName = "";
    this._BrowserVersion = "";
    
    this._IsFireFox = false;
    this._IsIE = false;
    this._IsOpera = false;
    this._IsSafari = false;
    this._IsOther = false; 
    
    this._KeyEventDelegates = [];   
    
    
    if(navigator.userAgent.indexOf("Firefox")!=-1)
    {
        this._IsFireFox = true;
        this._BrowserName = "Firefox";
        var versionindex=navigator.userAgent.indexOf("Firefox")+8;
        this._BrowserVersion = parseFloat(navigator.userAgent.charAt(versionindex));
    }
    else if (navigator.appVersion.indexOf("MSIE")!=-1)
    {
        this._IsIE = true;
        this._BrowserName = "MSIE";
        var temp=navigator.appVersion.split("MSIE");
        this._BrowserVersion=parseFloat(temp[1]);
    }
    else if (window.opera)
    {
        this._IsOpera = true;
        this._BrowserName = navigator.appName;
        this._BrowserVersion = parseFloat(navigator.appVersion);        
    }
    else if(navigator.userAgent.indexOf("Safari")!=-1)
    {
        this._IsSafari = true;
        this._BrowserName = "Safari";
        var versionindex=navigator.userAgent.indexOf("Safari")-4;
        this._BrowserVersion = parseFloat(navigator.userAgent.charAt(versionindex));        
    }  
    else
    {
        this._IsOther = true;
        this._BrowserName = navigator.appName;
        this._BrowserVersion = parseFloat(navigator.appVersion);
    }    
    
}

BrowserController.prototype =
{
	IsIE: function() {
		return this._IsIE ? this._BrowserVersion : 0;
	},

	IsFireFox: function() {
		return this._IsFireFox ? this._BrowserVersion : 0;
	},

	IsOpera: function() {
		return this._IsOpera ? this._BrowserVersion : 0;
	},

	IsSafari: function() {
		return this._IsSafari ? this._BrowserVersion : 0;
	},

	IsOtherBrowser: function() {
		return this._IsOther ? this._BrowserVersion : 0;
	},

	GetBrowserVersion: function() {
		return this._BrowserVersion;
	},

	//not 100% accurate
	GetBrowserDimensions: function() {
		var myWidth = 0, myHeight = 0, myScrollTop = 0, myScrollOffset = 0;
		if (typeof (window.innerWidth) == 'number') {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		}
		else if (document.documentElement &&
                    (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		}
		else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}

		if (this.IsIE()) {
			if (document.documentElement) {
				// to resolve the Doc-Type error in IE6. If the the response is 0, the also check body.scrollTop,
				// if also response 0, that means the page is really on top of the scroll,
				// otherwise we should get some value for the scroll top.
				//
				myScrollTop = document.documentElement.scrollTop ? document.documentElement.scrollTop :document.body.scrollTop;
			} else {
				myScrollTop = document.body.scrollTop;
				myScrollOffset = document.body.clientHeight - myHeight;
				alert('body' + myScrollTop);
			}
		}
		else {
			myScrollTop = window.pageYOffset;
			myScrollOffset = window.scrollMaxY;
		}

		return { 'Width': myWidth, 'Height': myHeight, 'ScrollTop': myScrollTop, 'ScrollOffset': myScrollOffset };
	},

	RegisterFunctionForKeyPress: function(pKeyCodeNo, pParameterlessDelegate) {
		if (document.onkeypress !== this.HandleKeyPress) {
			document.onkeypress = this.HandleKeyPress;
		}

		var keyCodeNoString = pKeyCodeNo.toString();
		if (!this._KeyEventDelegates[keyCodeNoString]) {
			this._KeyEventDelegates[keyCodeNoString] = [];
		}
		this._KeyEventDelegates[keyCodeNoString].push(pParameterlessDelegate);
	},

	HandleKeyPress: function(pKeyStroke) {
		var bc = GetBC();
		var keyCode = bc.IsIE() ? event.keyCode : pKeyStroke.which;
		if (pKeyStroke && keyCode == 0) {
			keyCode = pKeyStroke.keyCode;
		}
		keyCode = keyCode.toString();
		if (!bc._KeyEventDelegates[keyCode] || !(bc._KeyEventDelegates[keyCode] instanceof Array)) {
			return;
		}
		for (var i = 0; i < bc._KeyEventDelegates[keyCode].length; i++) {
			try {
				eval("bc._KeyEventDelegates[keyCode][i]()");
			}
			catch (err) {
			}
		}

	}
}

PopItUpController = function()
{    
    if(___puc != null)
    {
        throw("An instance of the Pop It Up Controller already exists. Use GetPUC() instead.");
    }    
    this._CommonWinStats = 'toolbar=0,location=0,directories=0,status=0,menubar=0';
    this._PopupContainer = null;
    this._PopupHeader = null;
    this._PopupTxtUrl = null;
    this._PopupIframe = null;
    this._BrowserLinkAcceptedDelegate = null;
    this._IESelects = null//if using an iframe and ie 6 then hide selects else selects will go overtop
    this._NowLoadingMessage = null;
    
    this._BC = GetBC();
    this._InOutBoard = document.getElementById('IOBoard');                
    this._IsIE = this._BC.IsIE(); 
            
    this._ModalCanvas = document.createElement("div");
    this._ModalCanvas.style.position = "absolute";
    this._ModalCanvas.style.zIndex = 999;
    this._ModalCanvas.style.top = 0;
    this._ModalCanvas.style.left = 0;
    this._ModalCanvas.style.opacity = "0.2";
    this._ModalCanvas.style.filter = "alpha(opacity=20)";
    this._ModalCanvas.style.background = "#000000";
    this._ModalCanvas.style.display = "none";
    document.body.appendChild(this._ModalCanvas);    
    
      
    
    if(this._InOutBoard)
    {
        this._InOutBoard.style.display = "none";
        this._InOutBoard.style.visibility = "visible";
    }
    else
    {
        this._InOutBoard = null;
    }    
    
    if(this._IsIE && this._IsIE < 7)
    {
        this._WindowPosition = "left=250,top=250"; 
        var allSelects = document.getElementsByTagName('select');          
        this._IESelects = [];
        for(var i = 0, j = 0; i < allSelects.length; i++)
        {
            if(!allSelects[i].getAttribute('ignorehide'))
            {
                this._IESelects[j++] = allSelects[i];                
            }
        }
    }
    else
    {
        this._WindowPosition = "screenX=250,screenY=250";
    }
}

PopItUpController.prototype =
{
	PopItUp: function(pUrl, pWidth, pHeight) {
		var winStats = this._CommonWinStats +
	                    ',scrollbars=,0resizeable=0,width=' +
	                    pWidth + ',height=' + pHeight + ',' +
	                    this._WindowPosition;
		var floater = window.open(pUrl, "businessCard", winStats);
	},

	PopItUpSizeable: function(pUrl, pWidth, pHeight) {
		var winStats = this._CommonWinStats +
	                    ',scrollbars,resizable,width=' +
	                    pWidth + ',height=' + pHeight + ',' +
	                    this._WindowPosition;
		var floater = window.open(pUrl, "businessCard", winStats);
	},

	PopItUpSizeableWithMenu: function(pUrl, pWidth, pHeight) {
		var winStats = this._CommonWinStats +
	                    ',scrollbars,resizable,menubar,toolbar,status,width=' +
	                    pWidth + ',height=' + pHeight + ',' +
	                    this._WindowPosition;
		var floater = window.open(pUrl, "businessCard", winStats);
	},

	_PopItUpIframeInit: function() {
		this._PopupContainer = document.createElement("div");
		this._PopupContainer.setAttribute("id", "PopupIFrameContainer");

		var popupClose = document.createElement("div");
		popupClose.setAttribute("id", "popupIframeClose");

		//popup browser header
		this._PopupHeader = document.createElement("div");
		this._PopupHeader.setAttribute("id", "popupHeader");
		this._PopupHeader.style.display = "none";
		this._PopupTxtUrl = document.createElement("input");
		this._PopupTxtUrl.setAttribute("id", "txtPopupBrowseUrl");
		this._PopupTxtUrl.setAttribute("type", "text");
		var lblUrl = document.createElement("label");
		lblUrl.setAttribute("for", "txtPopupBrowseUrl");
		lblUrl.innerHTML = "URL:";
		var btnPopupBrowseUrl = document.createElement("button");
		btnPopupBrowseUrl.innerHTML = "Preview Link";
		btnPopupBrowseUrl.onclick = function() {
			GetPUC()._PopupBrowse();
		};

		var btnPopupBrowseAccept = document.createElement("button");
		btnPopupBrowseAccept.innerHTML = "Accept";
		btnPopupBrowseAccept.onclick = function() {
			GetPUC()._CloseBrowser();
		};

		this._PopupHeader.appendChild(lblUrl);
		this._PopupHeader.appendChild(this._PopupTxtUrl);
		this._PopupHeader.appendChild(btnPopupBrowseUrl);
		this._PopupHeader.appendChild(btnPopupBrowseAccept);
		this._PopupHeader.appendChild(document.createElement("hr"));

		var popupCloseImg = document.createElement("div");
		popupCloseImg.setAttribute("id", "popupIframeImageDiv");

		//cant add onload dynamically in ie 
		if (this._IsIE) {
			popupCloseImg.attachEvent("onclick", this.PopItUpCloseIframe);
			this._PopupIframe = document.createElement("<iframe onload='GetPUC()._IFrameLoaded();'/>");
		}
		else {
			popupCloseImg.setAttribute("onclick", "javascript:GetPUC().PopItUpCloseIframe();");
			this._PopupIframe = document.createElement("iframe");
			this._PopupIframe.setAttribute('onload', 'GetPUC()._IFrameLoaded();');
		}

		var center = document.createElement("center");


		this._PopupIframe.setAttribute("id", "PopupIFrame");
		this._PopupIframe.setAttribute("name", "PopupIFrame");
		this._PopupIframe.setAttribute("frameborder", "0");
		this._PopupIframe.setAttribute("scrolling", "no");
		this._PopupIframe.setAttribute("frameBorder", "0");
		this._PopupIframe.setAttribute("marginWidth", "0px");
		this._PopupIframe.style.margin = "0px 0px 0px 0px";
		this._PopupIframe.style.display = 'none';

		//this._PopupContainer.style.border='1px solid #0C285A';

		this._CreateIFrameLoadingMessage();

		popupClose.appendChild(popupCloseImg);

		center.appendChild(popupClose);
		center.appendChild(this._PopupHeader);
		center.appendChild(this._PopupIframe);

		this._PopupContainer.appendChild(center);

		document.body.appendChild(this._PopupContainer);

	},

	_CreateIFrameLoadingMessage: function() {
		this._NowLoadingMessage = document.createElement("div");
		this._NowLoadingMessage.className = "popupIframeContainerNowLoading";
		this._NowLoadingMessage.style.display = "none";
		var loadingText = document.createElement("div");
		loadingText.className = "popupIframeContainerNowLoadingText";
		//loadingText.innerHTML = "Loading...";
		loadingText.appendChild(document.createTextNode("Loading..."));

		var loadingDiv = document.createElement("div");
		loadingDiv.className = "popupIframeContainerNowLoadingImg";
		loadingDiv.appendChild(___ImageCache['LoadingImage']);
		var loadingDivCenter = document.createElement("center");
		loadingDivCenter.appendChild(loadingDiv);

		this._NowLoadingMessage.appendChild(loadingText);
		this._NowLoadingMessage.appendChild(loadingDivCenter);
		document.body.appendChild(this._NowLoadingMessage);
	},


	PopItUpWebBrowser: function(pBrowserLinkAcceptedDelegate, pSrc, pSrcWidth, pSrcHeight, pShowModal, pClass) {
		this.ClosePopups();
		this._BrowserLinkAcceptedDelegate = pBrowserLinkAcceptedDelegate;
		if (pShowModal) {
			this._ToggleDimBackground(true);
		}
		var browserDimensions = this._BC.GetBrowserDimensions();

		if (this._PopupContainer === null) {
			this._PopItUpIframeInit();
			//this._PopupHeader.innerHTML = null;
			//this._PopupHeader.appendChild(document.createTextNode("hi thererer"));
		}
		if (!pClass) {
			this._PopupContainer.className = "popupIframeContainer";
		}
		else {
			this._PopupContainer.className = pClass;
		}

		/*     
		if(pSrc !== null)
		{               
		//this._ShowNowLoading(browserDimensions);  
		this._NowLoadingMessage.style.display = 'none';
		this._PopupIframe.style.display = '';
            
		this._PopupContainer.style.display="block";
		this._PopupContainer.style.visibility = "visible";                         
		}
		else
		{
		pSrc = "about:blank";
		this._NowLoadingMessage.style.display = 'none';
		this._PopupIframe.style.display = '';
            
		this._PopupContainer.style.display="block";
		this._PopupContainer.style.visibility = "visible";            
		}*/
		this._PopupHeader.style.display = "block";

		this._NowLoadingMessage.style.display = 'none';
		this._PopupIframe.style.display = '';

		this._PopupContainer.style.display = "block";
		this._PopupContainer.style.visibility = "visible";
		if (pSrc === null) {
			pSrc = "about:blank";
			this._PopupTxtUrl.value = "";
		}
		else {
			this._PopupTxtUrl.value = pSrc;
		}


		this._PopupIframe.style.width = pSrcWidth + 'px';
		this._PopupIframe.style.height = pSrcHeight + 'px';
		this._PopupIframe.src = pSrc;
		this._PopupContainer.style.width = pSrcWidth + 8 + 'px';
		this._PopupContainer.style.height = pSrcHeight + 20 + 'px'; //allow for margin
		this._PopupContainer.style.left = (browserDimensions.Width - pSrcWidth) / 2 + 'px';
		this._PopupContainer.style.top = browserDimensions.ScrollTop + 150 + 'px';
		this._ToggleSelects(false);

	},

	PopItUpIframe: function(pSrc, pSrcWidth, pSrcHeight, pShowModal, pClass) {
		this.ClosePopups();

		if (pShowModal) {
			this._ToggleDimBackground(true);
		}
		var browserDimensions = this._BC.GetBrowserDimensions();

		if (this._PopupContainer === null) {
			this._PopItUpIframeInit();
		}

		/*
		//dont need header
		this._PopupHeader.style.display = "none";        
                
		this._PopupContainer.style.display = "none"; 
                
		this._NowLoadingMessage.style.width = 400 + 8 + 'px';
		this._NowLoadingMessage.style.height = 200 + 20 + 'px'; //allow for margin
		this._NowLoadingMessage.style.left = (browserDimensions.Width - 400) / 2 + 'px';
		this._NowLoadingMessage.style.top = browserDimensions.ScrollTop + 150 + 'px';
		this._NowLoadingMessage.style.display="block";  */
		this._ShowNowLoading(browserDimensions);

		if (!pClass) {
			this._PopupContainer.className = "popupIframeContainer";
		}
		else {
			this._PopupContainer.className = pClass;
		}

		this._PopupIframe.style.width = pSrcWidth + 'px';
		this._PopupIframe.style.height = pSrcHeight + 'px';
		this._PopupIframe.src = pSrc;
		this._PopupContainer.style.width = pSrcWidth + 8 + 'px';
		this._PopupContainer.style.height = pSrcHeight + 20 + 'px'; //allow for margin
		this._PopupContainer.style.left = (browserDimensions.Width - pSrcWidth) / 2 + 'px';
		this._PopupContainer.style.top = browserDimensions.ScrollTop + 150 + 'px';
		//this._PopupContainer.style.display="block";
		//this._PopupContainer.style.visibility = "visible";
		//alert('document.documentElement.scrollTop=' + document.documentElement.scrollTop);
		//alert(browserDimensions.ScrollTop);
		this._ToggleSelects(false);

	},

	_IFrameLoaded: function() {
		var puc = GetPUC();

		//fix for opera and safari
		if (puc._NowLoadingMessage.style.display !== "block") {
			return;
		}

		puc._NowLoadingMessage.style.display = 'none';
		puc._PopupIframe.style.display = '';

		puc._PopupContainer.style.display = "block";
		puc._PopupContainer.style.visibility = "visible";
	},

	PopItUpCloseIframe: function() {
		//ie's attachEvent causes a uninitialized instance to be created, so members will be null
		var puc = GetPUC();
		puc.ClosePopups();
	},

	//Only needed for ie
	_ToggleSelects: function(pTurnOn) {
		if (!this._IsIE || this._IsIE >= 7) {
			return false;
		}

		var visibility = pTurnOn ? "visible" : "hidden";
		for (i = 0; i < this._IESelects.length; i++) {
			this._IESelects[i].style.visibility = visibility;
		}

	},

	ToggleInOutBoard: function(pShowModal) {
		if (this._InOutBoard === null) {
			return;
		}
		if (this._InOutBoard.style.display === "none") {
			this.ClosePopups();

			if (pShowModal) {
				this._ToggleDimBackground(true);
			}
			this._ToggleSelects(false);
			var browserDimensions = this._BC.GetBrowserDimensions();
			this._InOutBoard.style.left = (browserDimensions.Width - 356) / 2 + 'px';
			this._InOutBoard.style.top = browserDimensions.ScrollTop + 150 + 'px';
			this._InOutBoard.style.display = "block";
			this._InOutBoard.style.zIndex = 1000;
		}
		else {
			this.ClosePopups();
		}
	},

	ClosePopups: function() {
		this._ToggleDimBackground(false);
		this._ToggleSelects(true);

		if (this._InOutBoard !== null) {
			this._InOutBoard.style.display = "none";
		}

		if (this._PopupContainer !== null) {
			this._PopupContainer.style.display = "none";
			this._PopupContainer.style.visibility = "hidden";
		}
	},

	_ToggleDimBackground: function(pMakeDim) {
		if (pMakeDim) {
			var browserDimensions = this._BC.GetBrowserDimensions();
			this._ModalCanvas.style.width = "100%";
			this._ModalCanvas.style.height = browserDimensions.Height + browserDimensions.ScrollOffset + "px";
			this._ModalCanvas.style.display = "block";
		}
		else {
			this._ModalCanvas.style.display = "none";
		}
	},

	_ShowNowLoading: function(pBrowserDimensions) {
		this._PopupHeader.style.display = "none";
		this._PopupContainer.style.display = "none";
		this._NowLoadingMessage.style.width = 400 + 8 + 'px';
		this._NowLoadingMessage.style.height = 200 + 20 + 'px'; //allow for margin
		this._NowLoadingMessage.style.left = (pBrowserDimensions.Width - 400) / 2 + 'px';
		this._NowLoadingMessage.style.top = pBrowserDimensions.ScrollTop + 150 + 'px';
		this._NowLoadingMessage.style.display = "block";
	},

	_PopupBrowse: function() {
		var browserDimensions = this._BC.GetBrowserDimensions();
		this._PopupIframe.src = this._PopupTxtUrl.value;
	},

	_CloseBrowser: function() {
		this.ClosePopups();
		this._BrowserLinkAcceptedDelegate(this._PopupTxtUrl.value);
		this._PopupTxtUrl.value = "";
		this._BrowserLinkAcceptedDelegate = null;
	}

}

function checkAllCheckBoxes(value){
	var elementNo = 0;
	var formElements = document.mrf.elements;
	while( elementNo < formElements.length ){
		if( formElements[elementNo].type == "checkbox" ){
			formElements[elementNo].checked = value;
			
		}
		elementNo++;
	}
}

function checkboxes(){
	var elementNo = 0;
	var formElements = document.mrf.elements;
	while( elementNo < formElements.length){
		if( formElements[elementNo].type == "checkbox" ){
			var checkbox = formElements[elementNo];
			if( formElements[elementNo+1].name == checkbox.name ){
				if( formElements[elementNo+1].type == "hidden" ){
					formElements[elementNo+1].value = checkbox.checked;
				}
			}
		}
		elementNo++;
	}
	return true;
}

function printPage(){
	if( window.print) {
		window.print();
	} else {
		alert( "Your browser does not support this function. Please use the Print command in the File menu to print this page." );
	}
}

function querySave( control, newWindow ){
    /*
	if (document.mrf.dataChanged.value == 'true'){
		if (confirm('Do you want to save changes? \nClick OK to save, or Cancel to continue without saving.')) {
			document.mrf.saveAction.value='true';
		}
		document.mrf.Execute.value = control;
		if (newWindow) {
			document.mrf.target = '_blank';
		} else {
			document.mrf.target = '_self';
		}
		
		checkboxes();document.mrf.submit();
	} else {
		document.mrf.Execute.value = control;
		if (newWindow) {
			document.mrf.target = '_blank';
		} else {
			document.mrf.target = '_self';
		}
		checkboxes();document.mrf.submit();
	} */
	/* jcl changed to call _basicFormAction() instead */
    var dataChanged = document.getElementById("dataChanged");
    if (dataChanged && dataChanged.value == 'true') {
        if (confirm('Do you want to save changes? \nClick OK to save, or Cancel to continue without saving.')) {
            if (document.getElementById('saveAction')) {
                document.getElementById('saveAction').value = 'true';
            }
        }
    }
    else {
        _basicFormAction(control, newWindow);
    } 
}

function querySaveMsg(control, newWindow, message) {
	if (confirm(message)) {
		if (document.getElementById('saveAction')) {
			document.getElementById('saveAction').value = 'true';
		}
		_basicFormAction(control, newWindow);
	}
}

function TurnOnDirtyCheck()
{
    if (document.getElementById("dataChanged"))
    {
        window.onbeforeunload = CheckDirty;
    }
}

function TurnOffDirtyCheck()
{
    window.onbeforeunload = null;
}

function CheckDirty(evt)
{
    if (typeof evt == 'undefined') {
    evt = window.event;
}
    var dataChanged = document.getElementById("dataChanged");
    if (dataChanged) {
        if (dataChanged.value === 'true') {
            return 'Data will not be saved if you continue to navigate to other pages.'; // Do you want to save changes? \nClick OK to save, or Cancel to continue without saving.';
        }
    }   
    return;
}


function doFormAction(doConfirm, control, newWindow, currentNetAppView, checkDirty)
{
    if (doConfirm)
    {
        if(!confirm( 'Are you sure you want to delete this item?') ){return false;}
    }
    
    if (currentNetAppView)
    {
        document.mrf.currentNetAppView.value=currentNetAppView;	
    }
    
    if (checkDirty === undefined)
    {
        checkDirty = true;
    }

    _basicFormAction( control, newWindow, checkDirty);
    return true;
}

function _basicFormAction( control, newWindow, checkDirty)
{
    // before submit. remove the check for dirty
    
    if (!checkDirty)
    {
        TurnOffDirtyCheck();
    }    
	document.mrf.Execute.value = control;
	if (newWindow) {
		document.mrf.target = '_blank';
	} else {
		document.mrf.target = '_self';
	}

	checkboxes();
	document.mrf.submit();
}

function changeOccurred() {
    var dataChanged = document.getElementById('dataChanged');
    if (dataChanged) {
        dataChanged.value = 'true';
    }
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var uniqueNumber = 1;
function Reload(pRefreshInterval, pName, pURL) {
 interval = pRefreshInterval * 1000;
 timerID = setTimeout("Reload(" + pRefreshInterval + ",'" + pName + "','" + pURL + "')", interval );
 uniqueNumber++;
 var newSrc = pURL + "?" + uniqueNumber ;
 document.images[pName].src= newSrc;
}

function currency( field ){
	var enteredValue = new String(field.value);
	if( enteredValue.charAt(0) == '$' ){
		enteredValue = enteredValue.substr(1);
	}
	var enteredNumber = parseFloat( enteredValue );
	if(! isNaN(enteredNumber)){
		enteredValue = enteredNumber.toString();
		var strEnteredValue = new String( enteredValue );
		if( enteredValue.indexOf('.') == -1 ){
			enteredValue = enteredValue + '.00';
		} else if( enteredValue.indexOf('.') == strEnteredValue.length-2) {
			enteredValue = enteredValue + '0';
		} else if( enteredValue.indexOf('.') < strEnteredValue.length - 3){
			var theEnd = strEnteredValue.indexOf('.')+3;
			enteredValue = strEnteredValue.substring(0,theEnd);
		}
		field.value = enteredValue;
	}
}
function CatalogueConfirmDelete(control) {
    if (confirm('Permanently delete this catalogue item?')) {
        document.mrf.Execute.value = control;
        document.mrf.target = '_self';
        document.mrf.submit();
        return true;
    }
    return false;
}


function __ShowContainer(containerId) {
	var box = document.getElementById(containerId);
	if (box) {
		box.style.display = "block";
	}
}

function __HideContainer(containerId) {
	var box = document.getElementById(containerId);
	if (box) {
		box.style.display = "none";
	}
}


function SetClass(element, pClass) {
	if (element) {
		element.setAttribute("className", pClass);
		element.setAttribute("class", pClass);
	}
}

// jcl 20090710
// the catalogue preview model
//		foreach preview catalogue, we create a catalogue preview helper, and it holds a collection of cataloguePreviewItemHelper
//		all of this would be

CataloguePreviewItemHelper = function(pId, pItemID, pDetailItemID, pItemNormalClass, pItemActiveClass) {
	this.id = pId;
	this.itemId = pItemID;
	this.containerID = pDetailItemID;
	this.item = document.getElementById(pItemID);
	this.detailItem = document.getElementById(pDetailItemID);
	this.normalClass = pItemNormalClass;
	this.activeClass = pItemActiveClass;
}

CataloguePreviewItemHelper.prototype =
{
	Show: function() {
		this.detailItem.style.display = "block";
		SetClass(this.item, this.activeClass);
	},

	Hide: function() {
		this.detailItem.style.display = "none";
		SetClass(this.item, this.normalClass);
	}
}


CataloguePreviewHelper = function(pId,  pTimerInterval) {
	this.id = pId;
	this.currentPosition = 0; // default from start
	this._currentItem = null;
	this.timerInterval = 5000;
	this.isTimerMove = true;
	this.isTimerStarted = false;

	if (pTimerInterval != null) {
		this.timerInterval = pTimerInterval;
	}
	this._Items = Array();
}


CataloguePreviewHelper.prototype =
{
	AddItem: function(pId, pItemID, pDetailItemID, pItemNormalClass, pItemActiveClass) {
		var helper = new CataloguePreviewItemHelper(pId, pItemID, pDetailItemID, pItemNormalClass, pItemActiveClass);
		this._Items.push(helper);
		helper.Hide();
		return helper;
	},
	MoveTo: function(pId) {
		var i = 0;
		var pos = 0;
		var foundItem = false;
		for (i = 0; i < this._Items.length; i++) {
			var item = this._Items[i];
			if (item.id == pId) {
				foundItem = true;
				pos = i;
				break;
			}
		}
		if (foundItem) {
			this._MoveToIndex(pos);
		}
		this.StopTimerMove();
	},
	StopTimerMove: function() {
		this.isTimerMove = false;
	},
	ResumeTimerMove: function() {
		this.isTimerMove = true;
	},
	_MoveToIndex: function(pPos) {
		var item = this._Items[pPos];
		if (this._currentItem) {
			this._currentItem.Hide();
		}
		if (item != null) {
			this._currentItem = item;
			this._currentItem.Show();
			this.currentPosition = pPos;
		}
	},
	GetItemByID: function(pID) {
		for (k1 in this._Items) {
			var item = this._Items[k1];
			if (item.Id === pID) {
				return item;
			}
		}
	},
	MoveNext: function() {
		var nextPos = this.currentPosition + 1;
		if (nextPos >= this._Items.length) {
			nextPos = 0;
		}
		this._MoveToIndex(nextPos);
	},
	StartTimer: function() {
		this.isTimerStarted = true;
		this.isTimerMove = true;
		setTimeout("GetCPC().GetPreviewHelper('" + this.id + "')._TimerCallBack()", this.timerInterval);
	},
	StopTimer: function() {
		this.isTimerStarted = false;
	},
	_TimerCallBack: function() {
		if (this.isTimerStarted == true) {
			if (this.isTimerMove == true) {
				this.MoveNext();

			}
			setTimeout("GetCPC().GetPreviewHelper('" + this.id + "')._TimerCallBack()", this.timerInterval);
		}
	}
}

CataloguePreviewController = function() {
	if (___cpc != null) {
		throw ("An instance of the Catalogue Preview Controller already exists. Use GetCPC() instead.");
	}
	this._previews = Array();
}

CataloguePreviewController.prototype =
{
	AddPreviewCatalogue: function(pId, pTimerInterval) {
		var helper = new CataloguePreviewHelper(pId, pTimerInterval);
		this._previews.push(helper);
		return helper;
	},
	GetPreviewHelper: function(pId) {
		for (k1 in this._previews) {
			var item = this._previews[k1];
			if (item.id == pId) {
				return item;
			}
		}
	}
}


//<!--
// new set of codes for hovertext, using the general model of getting browser information in common JS

function mouse_getPosition(e) {
	e = (e != null) ? e : window.event;
	var cursor = { x: 0, y: 0 };
	if (e.pageX || e.pageY) {
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	}
	else {
		var de = document.documentElement;
		var b = document.body;
		cursor.x = e.clientX +
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
		cursor.y = e.clientY +
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
	}
	window.status = cursor.x + ',' + cursor.y;
	return cursor;
}

function _showHoverText(hoverTextID) {
	var o = document.getElementById(hoverTextID);
	if (o) {
		o.style.display = 'block';
		o.style.visibility = 'visible';
	}
}

function _moveHoverText(e, hoverTextID) {
	var o = document.getElementById(hoverTextID);
	if (o) {
		var mousePos = mouse_getPosition(e);
		mx = mousePos.x + 10;
		my = mousePos.y + 10;
		o.style.top = my + 'px';
		o.style.left = mx + 'px';
		o.style.display = 'block';
	}
}

function _hideHoverText(hoverTextID) {
	var o = document.getElementById(hoverTextID);
	if (o) {
		o.style.display = 'none';
		o.style.visibility = 'visible';
	}
}

function PopItUpSizeableWithMenu(pUrl, pWidth, pHeight) {
	GetPUC().PopItUpIframe(pUrl, pWidth, pHeight, true);
}


function _toggleExpandCloseContainer(pContainerId, pControlId, expandActionText, closeActionText) {
	var box = document.getElementById(pContainerId);
	var ctrl = document.getElementById(pControlId);

	if (box && ctrl) {
		if (box.style.display != "none") {
			// treat it is  opened
			__HideContainer(pContainerId);
			ctrl.innerText = expandActionText;
		}else{
		__ShowContainer(pContainerId);
		ctrl.innerText = closeActionText;
		}
	}
} 