﻿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']);
  }
});  