﻿if (typeof PIE == "undefined" || !PIE)
{
  var PIE = {};
}

  
PIE.Bool =
{
  parse : function(value)
  {
    var value_string = value != null ? value.toString() : '';
    
    switch(value_string.toUpperCase())
    {
      case "TRUE" :
			case "Y"    : 
      case "YES"  :
      case "ON"   :
        return true;
        
      default:
        return (parseInt(value) != 0);
    } 
  }
};

PIE.Int =
{
  parse: function(value)
  {
    var value_string = value != null ? value.toString() : '';
    return parseInt(value_string.replace(/[\D]/g, ''));
  },
  val: function(value)
  {
    var r = /\d+/.exec(value != null ? value.toString() : '');
    return r ? parseInt(r[0]) : 0;
  }
};

PIE.Window =
{
  popUpSid: function(url, qs, name, width, height)
  {
    var loForm = document.frmMG;

    var sid = window.location.search.toQueryParams().sid;
    var windowHandle = window.open(url + '?sid=' + sid + '&' + qs, name, "height=" + (height ? height : 600) + ",width=" + (width ? width : 620) + ",status=no,resizable=yes,scrollbars=yes");
    if (windowHandle.opener)
      windowHandle.focus();
  },
  popUp: function(url, name, width, height)
  {
    var handle = window.open(url, name, "height=" + (height ? height : 600) + ",width=" + (width ? width : 620) + ",status=no,resizable=yes,scrollbars=yes");
    if (handle.opener)
      handle.focus();

    return handle;
  }
};

PIE.Element =
{
  getElementWidth: function(e) 
  {
	  if (typeof e.clip !== "undefined") {
		  return e.clip.width;
	  } 
	  else {
		  if (e.style.pixelWidth)
			  return e.style.pixelWidth;
		  else 
			  return e.offsetWidth;
	  }
	},
  getElementHeight: function(e) 
  {
	  if (typeof e.clip !== "undefined") {
		  return e.clip.height;
	  } 
	  else {
		  if (e.style.pixelHeight)
			  return e.style.pixelHeight;
		  else 
			  return e.offsetHeight;
	  }
	}
};


Ajax.Responders.register({
  onCreate: function(request)
  {
    request['timeoutId'] = window.setTimeout(
      function()
      {
        // If we have hit the timeout and the <acronym title="Asynchronous Javascript And XML">AJAX</acronym> request is active, abort it and let the user know  
        switch (request.transport.readyState)
        {
          case 1:
          case 2:
          case 3:
            request.transport.abort();
            // Run the onFailure method if we set one up when creating the <acronym title="Asynchronous Javascript And XML">AJAX</acronym> object  
            if (request.options['onTimeout'])
              request.options['onTimeout'](request.transport, request.json);
            break;
        }
      }, (request.options && request.options.timeout ? request.options.timeout : 5) * 1000 // Five seconds  
    );
  },
  onComplete: function(request)
  {
    // Clear the timeout, the request completed ok  
    window.clearTimeout(request['timeoutId']);
  }
});


PIE.Panels =
{
    simplePanels: new Array(),
    panel: null,
    winName: 'PleaseWaitPanel',
    timerId: 0,

    launchSimplePanel: function(pMessage, pAllowShowClose) {
        var self = this;
        var body = '';
        var message = "Please Wait While We Generate Your Report ...";
        var allowShowClose = true;

        if (pMessage)
            message = pMessage;
            
        if (pAllowShowClose == false)
            allowShowClose = pAllowShowClose;

        var htmlMessage = "<table cellspacing='0' cellpadding='0' border='0'><tr><td valign='top'><img src='Img/Indicator.gif' align='absmiddle'>&nbsp;</td><td><span class='pleasewait'>" + message + "</span></td></tr></table>";

        if (this.simplePanels[this.winName]) {
            this.panel = this.simplePanels[this.winName];
            this.panel.cfg.setProperty('close', false);
        }
        else {
            this.panel = new YAHOO.widget.Panel(this.winName, { width: "380px", constraintoviewport: true, underlay: "shadow", visible: false, draggable: false, modal: true, fixedcenter: true, close: false });
            this.simplePanels[this.winName] = this.panel;
        }

        body = htmlMessage + "<img width='100%' height='15' border='0' SRC='Img/Spacer.gif'><br>";

        this.panel.setHeader("&nbsp;");
        this.panel.setBody(body);
        this.panel.render();
        this.panel.show();

        timerId = window.setTimeout(function() { if (allowShowClose) self.showClose(true) }, 25000);

        this.panel.hideEvent.subscribe(function() {
            cancelBrowserNav();
            self.hidePanel();
        });
    },

    showClose: function(bShowClose) {
        this.panel.cfg.setProperty('close', bShowClose);
    },

    hidePanel: function() {
        this.panel.hide();
        window.clearTimeout(timerId);
    }

};


//Makes the far right part of buttons clickable
document.observe('dom:loaded', function() {
    $$('.btnEnd').each(function(item) {
        item.observe('click', function(event) {
            var eCurrent = event.element();

            if (eCurrent) {
                var ePrev = eCurrent.previous();

                if (ePrev)
                    ePrev.onclick();
            }
        });
    });
});

