/*********************************************************************************************\
*       COPYRIGHT © 2010 ENVISION INFORMATION TECHNOLOGIES, LLC.    ALL RIGHTS RESERVED.      *
*       DISTRIBUTION, UNAUTHORIZED USE AND MODIFICATION IS STRICTLY PROHIBITED                *
*       ENVISION IT, MADISON, WI    http://www.envisionitllc.com   info@envisionitllc.com     *
\*********************************************************************************************/

// This is a general Javascript class containing miscellaneous useful functions

Array.prototype.inArray = function(value)
{
  for (var key in this)
  {
    if (this[key] == value)
      return true;
  };
  
  return false;
};

String.prototype.trim = function() 
{
  return this.replace(/^\s+|\s+$/g,"");
};

String.prototype.ltrim = function() 
{
  return this.replace(/^\s+/,"");
};

String.prototype.rtrim = function() 
{
  return this.replace(/\s+$/,"");
};

String.prototype.addCommas = function()
{
  nStr = this;
  
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  
  while (rgx.test(x1)) 
  {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  };
  
  return x1 + x2;
};

String.prototype.pad = Number.prototype.pad = function (length, char)
{
  var str = this.toString();
  
  while (str.length < length)
    str = char + str;
  
  return str;
} 

String.prototype.getDBDate = function()
{
  if (this.length != 10)
  {
    alert('Could not parse the date');
    return;
  };
  
  var comp = this.split('-');
  
  // The month is zero-index based
  return new Date(comp[0], comp[1]-1, comp[2]);
};

String.prototype.stripHTMLSpecialChars = function()
{
  var search = /&#124;/gi;
  var text = this.replace(search, '|');
  
  search = /&quot;/gi;
  text = text.replace(search, '"');
  
  search = /&#039;/gi;
  text = text.replace(search, "'");
  
  search = /&lt;/gi;
  text = text.replace(search, "<");
  
  search = /&gt;/gi;
  text = text.replace(search, ">");
  
  search = /&amp;/gi;
  text = text.replace(search, '&');
  
  return text;
};

String.prototype.stripPipeEncoding = function()
{
  var search = /&#124;/gi;
  var text = this.replace(search, '|');
  
  return text;
};

var agt=navigator.userAgent.toLowerCase();
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

function getClientSize()
{
  var theWidth, theHeight;
  
  if (window.innerWidth)
    theWidth=window.innerWidth;
  else if (document.documentElement && document.documentElement.clientWidth) 
    theWidth=document.documentElement.clientWidth;
  else if (document.body) 
    theWidth=document.body.clientWidth;
  
  if (window.innerHeight) 
    theHeight=window.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight) 
    theHeight=document.documentElement.clientHeight;
  else if (document.body) 
    theHeight=document.body.clientHeight;
  
  return new Object({Width:theWidth, Height:theHeight});
};

// These are global scope variables, they should remain global.
var _dialogPromptID = null;
var _blackoutPromptID = null;

function IEprompt(innertxt,def,callbackname) 
{
  that = this;
  
  this.wrapupPrompt = function (canceled) 
  {
    val=document.getElementById('iepromptfield').value;
    _dialogPromptID.style.display='none';
    _blackoutPromptID.style.display='none';
    document.getElementById('iepromptfield').value = '';
    
    if (canceled) 
      val = null;
    
    eval(callbackname + '(val);');
    return false;
  };

  //if def wasn't actually passed, initialize it to a blank string
  if (def == undefined)
    def='';
  
  if (_dialogPromptID==null) 
  {
    var tbody = document.getElementsByTagName("body")[0];
    tnode = document.createElement('div');
    tnode.id='IEPromptBox';
    tbody.appendChild(tnode);
    _dialogPromptID=document.getElementById('IEPromptBox');
    tnode = document.createElement('div');
    tnode.id='promptBlackout';
    tbody.appendChild(tnode);
    _blackoutPromptID=document.getElementById('promptBlackout');
    _blackoutPromptID.style.opacity='.9';
    _blackoutPromptID.style.position='absolute';
    _blackoutPromptID.style.top='0px';
    _blackoutPromptID.style.left='0px';
    _blackoutPromptID.style.backgroundColor='#555555';
    _blackoutPromptID.style.filter ='alpha(opacity=90)';
    _blackoutPromptID.style.height = ((document.body.offsetHeight<screen.height) ? (screen.height + 'px') : (document.body.offsetHeight + 20 + 'px')); 
    _blackoutPromptID.style.display='block';
    _blackoutPromptID.style.zIndex='50';
    // assign the styles to the dialog box
    _dialogPromptID.style.border='2px solid blue';
    _dialogPromptID.style.backgroundColor='#DDDDDD';
    _dialogPromptID.style.position='fixed';
    _dialogPromptID.style.width='330px';
    _dialogPromptID.style.zIndex='100';
  };
  
  var tmp = '<div style="width: 100%; background-color: blue; color: white; font-family: verdana; font-size: 10pt; font-weight: bold; height: 20px">Input Required</div>';
  tmp += '<div style="padding: 10px">'+innertxt + '<BR><BR>';
  tmp += '<input id="iepromptfield" name="iepromptdata" type=text size=46 value="'+def+'">';
  tmp += '<br><br><center>';
  tmp += '<input type="button" onclick="that.wrapupPrompt(false)" value="&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;">';
  tmp += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  tmp += '<input type="button" onclick="that.wrapupPrompt(true)" value="&nbsp;Cancel&nbsp;">';
  tmp += '</div>';
  
  _blackoutPromptID.style.height = ((document.body.offsetHeight<screen.height) ? (screen.height + 'px') : (document.body.offsetHeight + 20 + 'px')); 
  _blackoutPromptID.style.width='100%';
  _blackoutPromptID.style.display='block';
  
  var clientSize = getClientSize();
  
  _dialogPromptID.innerHTML=tmp;
  _dialogPromptID.style.top=((clientSize.Height/2)-100) + 'px';
  _dialogPromptID.style.left=((clientSize.Width/2)-165) + 'px';
  _dialogPromptID.style.display='block';
  // Give the dialog box's input field the focus.
  document.getElementById('iepromptfield').focus();
};

function isMaxLength(obj)
{
  if (obj.value.length > maxTextAreaLength)
    obj.value = obj.value.substring(0,maxTextAreaLength);
};

function displayMessageBox(htmlContent)
{
  if(navigator.userAgent.toLowerCase().indexOf("msie 6") == -1)
  {
    var waitbg = document.createElement("div");
    waitbg.style.position = "fixed";
    waitbg.style.left = "0px";
    waitbg.style.top = "0px";
    waitbg.style.height = "100%";
    waitbg.style.width = "100%";
    waitbg.style.background = "black";
    waitbg.id = "waitBg";
    if (((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("opera") == -1)))
    {
      waitbg.style.filter = 'alpha(opacity=30)';
    }
    else // for mozilla
    {
      waitbg.style.opacity = 0.3;
    };
    
    document.body.appendChild(waitbg);
    
    var msgBox = document.createElement("div");
    msgBox.id = "msgBox";
    msgBox.style.position = "fixed";
    msgBox.style.width = "500px";
    msgBox.style.marginLeft = "-250px";
    var y = 100;
    msgBox.style.left = "50%";
    msgBox.style.top = y + "px";
    msgBox.style.background = "#ffffff";
    msgBox.style.border = "1px solid black";
    msgBox.style.color = "black";
    msgBox.style.font = "12px Verdana";
    msgBox.style.textAlign = "left";
    msgBox.style.padding="10px 10px 10px 10px";
    
    var content = document.createElement("div");
    content.style.width="500px";
    content.style.backgroundColor="#ffffff";
    content.style.paddingBottom='15px';
    content.innerHTML = htmlContent;
    
    msgBox.appendChild(content);
    
    var closeContainer = document.createElement("div");
    
    var closeButton = document.createElement("input");
    closeButton.type='button';
    closeButton.onclick=function() { document.body.removeChild(document.getElementById('msgBox')); document.body.removeChild(document.getElementById('waitBg')); }
    closeButton.value = 'Close';

    closeContainer.appendChild(closeButton);
    msgBox.appendChild(closeContainer);
    
    
    document.body.appendChild(msgBox);
  }
  else
  {
    var myDoc = window.open("","ie6","width=400,height=400,scrollbars=1,resizeable=1");
    myDoc.document.write(htmlContent);
  };
};
