var IzOnLoadItems = new Array();
var win = null;
var winArgs = null;
var winRet = null;
var winFunc = null;
var maxHeight = screen.availHeight - 80 - 120;
var maxWidth = screen.availWidth - 80 - 180;
var minHeight = ( ( maxHeight > 775 ) ? 775 : maxHeight );
var minWidth = ( ( maxWidth > 575 ) ? 575 : maxWidth );
var winIeParams = "height=" + minWidth + ",width=" + minWidth + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no,top=120,left=180";
var winNsParams = "height=" + minWidth + ",width=" + minWidth + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no,top=120,left=180";
var winNsParamsNo = "location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=no";
var winIeModalParams = "dialogWidth:" + minWidth + "px;dialogHeight:" + minWidth + "px;scroll:yes;center:no;resizable:yes;status:yes;help:no;dialogTop:120;dialogLeft:180";
var winNsModalParams = "height=" + minWidth + ",width=" + minWidth + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no,top=120,left=180,dependent=yes";
var notPopUp = false;

function IzOnLoad() {
  for ( var i = 0; i < IzOnLoadItems.length; i++ ) {
    var item = IzOnLoadItems[ i ];
    window.setTimeout( item, 0 );
  }
}

function IzOnLoadItemsAdd( item ) {
  var next = IzOnLoadItems.length;
  IzOnLoadItems[ next ] = item;
}

function IzOnEvent( e ) {
  return false;
}
 
function IzOnFocus() {
  if ( win != null && ! win.closed ) {
    win.focus();
  } else {
    window.releaseEvents( Event.CLICK | Event.FOCUS );
    window.onclick = "";
  }
  return false;
}

function OpenModalWinDirect( url, winName, args, func ) {
  var now = new Date();
  url = addTime( url );
  if ( window.showModalDialog ) {
    winRet = window.showModalDialog( url, args, winIeModalParams );
    if ( func ) func();
  } else {
    winArgs = args;
    winFunc = func;
    window.captureEvents( Event.CLICK | Event.FOCUS );
    window.onclick = IzOnEvent;
    window.onfocus = IzOnFocus;
    win = window.open( url, winName, winNsModalParams );
    win.focus();
  }
}
 
function OpenModalWin( toPage, params, args, func ) {
  var winName = toPage;
  var url = "nobrand.jsp?ToPage=" + toPage;
  if ( params ) {
    url += params;
    winName += params.replace( /&/g, "" ).replace( /=/g, "" );
  }
  url = addTime( url );
  if ( window.showModalDialog ) {
    winRet = window.showModalDialog( url, args, winIeModalParams );
    if ( func ) func();
  } else {
    winArgs = args;
    winFunc = func;
    window.captureEvents( Event.CLICK | Event.FOCUS );
    window.onclick = IzOnEvent;
    window.onfocus = IzOnFocus;
    win = window.open( url, winName, winNsModalParams );
    win.focus();
  }
}
 
function OpenWinDirect( url, winName, noFeatures ) {
  if ( ! noFeatures ) url = addTime( url );
  if ( noFeatures ) win = window.open( url, winName, ( window.showModalDialog ? "" : winNsParamsNo ) );
  else if ( window.showModalDialog ) win = window.open( url, winName, winIeParams );
  else win = window.open( url, winName, winNsParams );
  win.focus();
}

function OpenWin( toPage, params ) {
  var winName = toPage;
  var url = "nobrand.jsp?ToPage=" + toPage;
  if ( params ) {
    url += params;
    winName += params.replace( /&/g, "" ).replace( /=/g, "" );
  }
  url = addTime( url );
  if ( window.showModalDialog ) win = window.open( url, winName, winIeParams );
  else win = window.open( url, winName, winNsParams );
  win.focus();
}

function CloseWin() {
  window.close();
}

function ResizeWin() {
  if ( ! notPopUp || notPopUp == null ) {
    var mainTable = document.getElementById( "main" );
    var width = mainTable.clientWidth + 40;
    var height = mainTable.clientHeight + 40;

    if ( width > maxWidth ) width = maxWidth;
    if ( height > maxHeight ) height = maxHeight;

    var widthDiff = width - document.body.clientWidth;
    var heightDiff = height - document.body.clientHeight;

    window.resizeBy( widthDiff, heightDiff );
    window.focus();
  }
}

function ResizeModalWin() {
  if ( ! notPopUp || notPopUp == null ) {
    if ( window.dialogWidth ) {
      var mainTable = document.getElementById( "main" );
      var width = mainTable.clientWidth + 40;
      var height = mainTable.clientHeight + 40;

      if ( width > maxWidth ) width = maxWidth;
      if ( height > maxHeight ) height = maxHeight;

      width = window.dialogWidth.replace( /px/g, '' ) * 1 + width - window.document.body.clientWidth;
      height = window.dialogHeight.replace( /px/g, '' ) * 1 + height - window.document.body.clientHeight;

      window.dialogWidth = width + "px";
      window.dialogHeight = height + "px";
    } else {
      ResizeWin();
    }
  }
}

function emailTo(emailAddress,emailSubject,emailBody) {
  var str = 
    "mailto:" +
    encodeURIComponent( emailAddress ) +
    "?subject=" + 
    encodeURIComponent( emailSubject ) +
    "&body=" +
    encodeURIComponent( emailBody );
  window.location.href=str;
}

function Goto( toPage, params ) {
  var url = "display.jsp?ToPage=" + toPage;
  if ( params ) url += params;
  window.location.href = addTime( url );
}

function GotoNoBrand( toPage, params ) {
  var url = "nobrand.jsp?ToPage=" + toPage;
  if ( params ) url += params;
  window.location.href = addTime( url );
}

function GotoProcess( fromPage, params ) {
  var url = "process.jsp?FromPage=" + fromPage;
  if ( params ) url += params;
  window.location.href = addTime( url );
}

function GotoDirect( sUrl ) {
  window.location.href = addTime( sUrl );
}

function ShowAlert( text, titleText, headerText, doSubmitText, cancel, func, moreParams ) {
  var params = "";
  if ( titleText ) params += ( "&Title=" + titleText );
  if ( headerText ) params += ( "&Header=" + headerText );
  if ( doSubmitText ) params += ( "&DoSubmit=" + doSubmitText );
  if ( cancel ) params += ( "&Cancel=" + cancel );
  if ( moreParams ) params += moreParams;
  OpenModalWin( "Alert", params, text, func );
}

var objHTTP = null;
var xmlDoc = null;
var appSubmitted = false;

function SubmitApp() {
  var ok = document.getElementById( "okButton" );
  if ( ok != null ) {
    ok.disabled = true;
    appSubmitted = true;
    DoTieBreaker();
  }
}

function TieBreaker() {
  var url = addTime( "nobrand.jsp?ToPage=TieBreaker&Title=Tie Breaker" );
  OpenModalWinDirect( url, "TieBreaker", null, TieBreakerResponse );
}

function DoTieBreaker() {
  try {
    var sUrl = addTime( "process.jsp?FromPage=TieBreaker&Action=Check" );
    objHTTP = document.getElementById( "IzRelayFrame" );
    objHTTP.contentWindow.document.body.innerHTML = "<form action=\"" + sUrl + "\" method=post id=sumForm name=sumForm><textarea name=Comments id=Comments>" + document.getElementById( "Comments" ).value + "</textarea></form>";
    objHTTP.onload = DoTieBreakerResponse;
    objHTTP.onreadystatechange = DoTieBreakerResponse;
    objHTTP.contentWindow.document.getElementById( "sumForm" ).submit();
  } catch ( e ) {
    ModalWinReturn( "error:request" );
  }
}

function DoTieBreakerResponse() {
  try {
    if ( objHTTP.readyState == "complete" || objHTTP.readyState == null ) {
      xmlDoc = objHTTP.contentWindow.document.getElementById( "XmlResult" );
      if ( xmlDoc == null ) xmlDoc = objHTTP.contentWindow.document;
      var status = valueOf( xmlDoc.getElementsByTagName( "Status" ).item( 0 ) );
      var reason = valueOf( xmlDoc.getElementsByTagName( "Reason" ).item( 0 ) );
      if ( status == "false" ) DoSubmit();
      else if ( status == "true" ) TieBreaker();
      else ModalWinReturn( status + ":" + reason );
    }
  } catch ( e ) {
    ModalWinReturn( "error:response" );
  }
}

function TieBreakerResponse() {
  if ( winRet == "success" ) DoSubmit();
  else ModalWinReturn( status + ":" + reason );
}

function DoSubmit() {
  try {
    var fromPage = "Summary";
    if ( document.getElementById( "FromPage" ) != null ) fromPage = document.getElementById( "FromPage" ).value;
    var sUrl = addTime( "process.jsp?FromPage=" + fromPage + "&Action=Submit" );
    objHTTP = document.getElementById( "IzRelayFrame" );
    objHTTP.contentWindow.document.body.innerHTML = "<form action=\"" + sUrl + "\" method=post id=sumForm name=sumForm><textarea name=Comments id=Comments>" + document.getElementById( "Comments" ).value + "</textarea></form>";
    objHTTP.onload = DoSubmitResponse;
    objHTTP.onreadystatechange = DoSubmitResponse;
    objHTTP.contentWindow.document.getElementById( "sumForm" ).submit();
  } catch ( e ) {
    ModalWinReturn( "error:request" );
  }
}

function DoSubmitResponse() {
  try {
    if ( objHTTP.readyState == "complete" || objHTTP.readyState == null ) {
      xmlDoc = objHTTP.contentWindow.document.getElementById( "XmlResult" );
      if ( xmlDoc == null ) xmlDoc = objHTTP.contentWindow.document;
      var status = valueOf( xmlDoc.getElementsByTagName( "Status" ).item( 0 ) );
      var reason = valueOf( xmlDoc.getElementsByTagName( "Reason" ).item( 0 ) );
      if ( status == "error" ) ModalWinReturn( status + ":" + reason );
      else ModalWinReturn( status );
    }
  } catch ( e ) {
    ModalWinReturn( "error:response" );
  }
}

function ModalWinReturn( ret ) {
  if ( window.showModalDialog ) {
    window.returnValue = ret;
    window.close();
  } else {
    window.opener.winRet = ret;
    window.close();
    window.opener.winFunc();
  }
}

function valueOf( node ) {
  var textNode = node.firstChild;
  if ( textNode != null ) return textNode.nodeValue;
  else return "";
}

function contains( first, second ) {
  return ( ( "" + first ).indexOf( "" + second ) != -1 );
}

function not( first ) {
  return ( ! first );
}

function safeSplit( value, delem, index ) {
  var retstr = "";
  if ( value != null ) {
    var values = value.split( delem );
    if ( index < values.length ) retstr = values[ index ];
  }
  return retstr;
}

function string( obj ) {
  return ( "" + obj );
}

function addTime( url ) {
  var now = new Date();
  if ( url.indexOf( "?" ) != -1 ) {
    url += "&Time=T" + now.valueOf();
  } else {
    url += "?Time=T" + now.valueOf();
  }
  return url;
}

function hasPopupBlocker() {
  var ret = true;
  try {
    win = window.open( "", "", "directories=no,height=100,left=" + screen.availWidth + ",location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,top=" + screen.availHeight + ",width=100" );
    if ( win != null ) {
      win.close();
      ret = false;
    }
  } catch ( e ) {
    //do nothin
  }
  return ret;
}

function getNewXmlHttp( handler ) {
  this.xmlHttp = null;
  this.response = handler;
  if ( window.XMLHttpRequest && false ) {
    xmlHttp = new XMLHttpRequest();
  } else {
    try {
      xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
    } catch( e ) {
      try {
        xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
      } catch( e ) {
        //nothin we can use
      }
    }
  }
  if ( xmlHttp != null ) {
    xmlHttp.onreadystatechange = function tryResponse() { if ( this.xmlHttp.readyState == 4 ) this.response( this.xmlHttp.responseText ); };
  }
  return xmlHttp;
}

function setCookie( sName, sValue ) {
  date = new Date();
  document.cookie = sName + "=" + escape( sValue );
}

function getCookie( sName ) {
  var aCookie = document.cookie.split( "; " );
  for ( var i = 0; i < aCookie.length; i++ ) {
    var aCrumb = aCookie[i].split( "=" );
    if ( sName == aCrumb[0] ) return unescape( aCrumb[1] );
  }
  return null;
}

function GetInnerText( node ) {
  if ( node.innerText != null ) {
    var str = node.innerText.replace( /[\f\n\r\t\v]+/g, " " ).replace( /[ ]{2,}/g, " " );
    return str.replace( /(^\s+)|(\s+$)/g, "" );
  } else {
    var str = "";
    var kids = node.childNodes;
    for ( var i = 0; i < kids.length; i++ ) {
      var kid = kids.item( i );
      if ( kid.nodeName == "#text" ) {
        var val = "" + kid.nodeValue;
        if ( val.replace( /\s+/g, "" ) != "" ) str += val.replace( /[\f\n\r\t\v]+/g, " " ).replace( /[ ]{2,}/g, " " );
      } else if ( kid.tagName != "SELECT" ) {
        str += GetInnerText( kid );
      }
    }
    return str.replace( /(^\s+)|(\s+$)/g, "" );
  }
}

function OutlookObj( id, name, folder, restrict ) {
  var elem = document.getElementById( id );
  var obj = "";
  obj += "<object";
  obj += "  classid=\"CLSID:0006F063-0000-0000-C000-000000000046\"";
  obj += "  id=\"" + name + "\"";
  obj += "  name=\"" + name + "\"";
  obj += "  width=\"100%\"";
  obj += "  height=\"300\"";
  obj += "  border=\"1\"";
  obj += "  bordercolor=\"333399\"";
  obj += "  viewastext=\"viewastext\"";
  obj += "  codebase=\"outlctlx.CAB#ver=9,0,0,3203\"";
  obj += ">";
  obj += "  <param name=\"Folder\" value=\"" + folder + "\"/>";
  //obj += "  <param name=\"View\" value=\"Day/Week/Month View With AutoPreview\"/>";
  obj += "  <param name=\"Namespace\" value=\"MAPI\"/>";
  if ( restrict ) {
    obj += "  <param name=\"Restriction\" value=\"\"/>";
  }
  obj += "  <param name=\"DeferUpdate\" value=\"0\"/>";
  obj += "</object>";
  elem.innerHTML = obj;
}

function menu() {
  var lis = document.getElementById( "menu" ).getElementsByTagName( "LI" );
  for ( i = 0; i < lis.length; i++ ) {
    var li = lis[ i ];
    li.onmouseover = function() {
      this.className += " overmenu";
    }
    li.onmouseout = function() {
      this.className = this.className.replace( new RegExp("overmenu\\b"), "" );
    }
  }
}

var BrowserDetect = {
  init: function () {
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    this.version = this.searchVersion(navigator.userAgent)
      || this.searchVersion(navigator.appVersion)
      || "an unknown version";
    this.OS = this.searchString(this.dataOS) || "an unknown OS";
  },
  searchString: function (data) {
    for (var i=0;i<data.length;i++) {
      var dataString = data[i].string;
      var dataProp = data[i].prop;
      this.versionSearchString = data[i].versionSearch || data[i].identity;
      if (dataString) {
        if (dataString.indexOf(data[i].subString) != -1)
          return data[i].identity;
      }
      else if (dataProp)
        return data[i].identity;
    }
  },
  searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index == -1) return;
    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  },
  dataBrowser: [
    {
      string: navigator.userAgent,
      subString: "Chrome",
      identity: "Chrome"
    },
    {   string: navigator.userAgent,
      subString: "OmniWeb",
      versionSearch: "OmniWeb/",
      identity: "OmniWeb"
    },
    {
      string: navigator.vendor,
      subString: "Apple",
      identity: "Safari",
      versionSearch: "Version"
    },
    {
      prop: window.opera,
      identity: "Opera"
    },
    {
      string: navigator.vendor,
      subString: "iCab",
      identity: "iCab"
    },
    {
      string: navigator.vendor,
      subString: "KDE",
      identity: "Konqueror"
    },
    {
      string: navigator.userAgent,
      subString: "Firefox",
      identity: "Firefox"
    },
    {
      string: navigator.vendor,
      subString: "Camino",
      identity: "Camino"
    },
    {   // for newer Netscapes (6+)
      string: navigator.userAgent,
      subString: "Netscape",
      identity: "Netscape"
    },
    {
      string: navigator.userAgent,
      subString: "MSIE",
      identity: "Explorer",
      versionSearch: "MSIE"
    },
    {
      string: navigator.userAgent,
      subString: "Gecko",
      identity: "Mozilla",
      versionSearch: "rv"
    },
    {     // for older Netscapes (4-)
      string: navigator.userAgent,
      subString: "Mozilla",
      identity: "Netscape",
      versionSearch: "Mozilla"
    }
  ],
  dataOS : [
    {
      string: navigator.platform,
      subString: "Win",
      identity: "Windows"
    },
    {
      string: navigator.platform,
      subString: "Mac",
      identity: "Mac"
    },
    {
         string: navigator.userAgent,
         subString: "iPhone",
         identity: "iPhone/iPod"
      },
    {
      string: navigator.platform,
      subString: "Linux",
      identity: "Linux"
    }
  ]

};

function ToggleChanges( cell ) {
  var text = cell.innerText;
  var row = cell.parentElement.nextSibling;
  if ( text == "+" ) {
    row.style.display = "";
    cell.innerText = "-";
  } else {
    row.style.display = "none";
    cell.innerText = "+";
  }
}