﻿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;
	  }
	}
};

//gets the dimensions of the viewport (needed this b/c Prototype document.viewport.getDimensions() doesn't work in IE quirks mode)
PIE.Viewport =
{
    getDimensions: function() {
        var dimensions = {}, B = Prototype.Browser;
        $w('width height').each(function(d) {
            var D = d.capitalize();
            if (B.WebKit || B.Gecko) {
                // Safari <3.0 needs self.innerWidth/Height
                dimensions[d] = self['inner' + D];
            } else if ((B.Opera && parseFloat(window.opera.version()) < 9.5) || (B.IE)) {
                // Opera <9.5 and IE > 8.0 needs document.body.clientWidth/Height
                dimensions[d] = document.body['client' + D]
            } else {
                dimensions[d] = document.documentElement['client' + D];
            }
        });
        return dimensions;
    }
};


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: "400px", constraintoviewport: true, underlay: "shadow", visible: false, draggable: false, modal: true, fixedcenter: true, close: false, zIndex:99999 }); //set zIndex higher than Play Zone handles of 99
            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);
    }

};

PIE.Registration =
{
    policyInfo: function(poUserId, poPassword, poPassword2) {
        if (poUserId) {
            var loUserIdInfo = $('inputinfo_' + poUserId.id);
            var loUserId = $(poUserId);
        }
        if (poPassword) {
            var loPasswordInfo = $('inputinfo_' + poPassword.id);
            var loPassword = $(poPassword);
        }
        if (poPassword2) {
            var loPassword2 = $(poPassword2);
        }
        
        if (poUserId) {
            if (loUserIdInfo)
                loUserIdInfo.hide();

            loUserId.observe('focus', function() {
                loUserIdInfo.show();

                if (poPassword || poPassword2)
                    loPasswordInfo.hide();
            });
            loUserId.observe('blur', function() {
                loUserIdInfo.hide();
            });
        }
        if (poPassword || poPassword2) {
            if (loPasswordInfo)
                loPasswordInfo.hide();

            loPassword.observe('focus', function() {
                loPasswordInfo.show();
                
                if (poUserId)
                    loUserIdInfo.hide();
            });

            loPassword2.observe('focus', function() {
                loPasswordInfo.show();

                if (poUserId)
                    loUserIdInfo.hide();
            });

            loPassword.observe('blur', function() {
                loPasswordInfo.hide();
            });
            loPassword2.observe('blur', function() {
                loPasswordInfo.hide();
            });
        }
    }
};

PIE.Print =
{
    //Workaround for IPad and IPhone users so they can scroll through the pdf print reports (make reports not embedded in browser)
    targetNewWindow: function() {
        var masterframe = window.top.document.getElementById('fraMaster');
        var bottomframe = window.top.document.getElementById('fraBottom');

        masterframe.rows = "2000,*";
        bottomframe.style.display = 'none';
        $('printreporttext').style.display = 'none';
        $('pdflinkcontainer').style.display = 'block';
        $('pdfpath').href = bottomframe.src;
    }
};

function htmlEncode(value) {
    if (value)
        return jQuery('<div/>').text(value).html();
    else
        return '';
}

function htmlDecode(value) {
    if (value)
        return jQuery('<div/>').html(value).text();
    else
        return '';
}

function isEncodedHTML(str) {
    if (str.search(/&amp;/g) != -1 || str.search(/&lt;/g) != -1 || str.search(/&gt;/g) != -1)
        return true;
    else
        return false;
}


/*
* modified from http://xavisys.com/2007/03/using-prototype-javascript-to-get-the-value-of-a-radio-group/
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*/
function $FR(el, radioGroup) {
    if ($(el) && $(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    }
    else if ($(document.forms[0]).getInputs('radio', el).length != 0) {
        var radioGroup = el;
        var el = document.forms[0];
    }
    else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(function(re) { return re.checked; });

    return (checked) ? $F(checked) : null;
}

//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)
                    if (ePrev.readAttribute("onclick") != null)
                    ePrev.onclick();
            }
        });
    });
});


