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, "" ).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 == null ) params = "";
  if (
    params.indexOf( "Section" ) == -1 &&
    (
      toPage == "BOP" ||
      toPage == "BOPContractor" ||
      toPage == "Building" ||
      toPage == "BuildingSupp" ||
      toPage == "BusinessInfo" ||
      toPage == "CommAuto" ||
      toPage == "CommAutoTrailer" ||
      toPage == "CommAutoVehicle" ||
      toPage == "InfoTech" ||
      toPage == "SelectCoverage" ||
      toPage == "WorkComp"
    )
  ) {
    params += "&Section=1";
  }
  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 SubmitPersApp() {
  var ok = document.getElementById( "okButton" );
  if ( ok != null ) {
    ok.disabled = true;
    appSubmitted = true;
    DoCarrierEndorsements();
  }
}

function SubmitOtherApp() {
  var ok = document.getElementById( "okButton" );
  if ( ok != null ) {
    ok.disabled = true;
    appSubmitted = true;
    DoSubmit();
  }
}

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 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" ) DoCarrierEndorsements();
      else if ( status == "true" ) TieBreaker();
      else ModalWinReturn( status + ":" + reason );
    }
  } catch ( e ) {
    ModalWinReturn( "error:response" );
  }
}

function TieBreaker() {
  var url = addTime( "nobrand.jsp?ToPage=TieBreaker&Title=Tie Breaker" );
  OpenModalWinDirect( url, "TieBreaker", null, TieBreakerResponse );
}

function TieBreakerResponse() {
  if ( winRet == "success" ) DoCarrierEndorsements();
  else ModalWinReturn( winRet );
}

function DoCarrierEndorsements() {
  try {
    var sUrl = addTime( "process.jsp?FromPage=CarrierEndorsements&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 = DoCarrierEndorsementsResponse;
    objHTTP.onreadystatechange = DoCarrierEndorsementsResponse;
    objHTTP.contentWindow.document.getElementById( "sumForm" ).submit();
  } catch ( e ) {
    ModalWinReturn( "error:request" );
  }
}

function DoCarrierEndorsementsResponse() {
  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" ) DoCarrierDispatchSelections();
      else if ( status == "true" ) CarrierEndorsements();
      else ModalWinReturn( status + ":" + reason );
    }
  } catch ( e ) {
    ModalWinReturn( "error:response" );
  }
}

function CarrierEndorsements() {
  var url = addTime( "nobrand.jsp?ToPage=CarrierEndorsements&Title=Carrier Endorsements" );
  OpenModalWinDirect( url, "CarrierEndorsements", null, CarrierEndorsementsResponse );
}

function CarrierEndorsementsResponse() {
  if ( winRet == "success" ) DoCarrierDispatchSelections();
  else ModalWinReturn( winRet );
}

function DoCarrierDispatchSelections() {
  try {
    var sUrl = addTime( "process.jsp?FromPage=CarrierDispatchSelections&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 = DoCarrierDispatchSelectionsResponse;
    objHTTP.onreadystatechange = DoCarrierDispatchSelectionsResponse;
    objHTTP.contentWindow.document.getElementById( "sumForm" ).submit();
  } catch ( e ) {
    ModalWinReturn( "error:request" );
  }
}

function DoCarrierDispatchSelectionsResponse() {
  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" ) CarrierDispatchSelections();
      else ModalWinReturn( status + ":" + reason );
    }
  } catch ( e ) {
    ModalWinReturn( "error:response" );
  }
}

function CarrierDispatchSelections() {
  var url = addTime( "nobrand.jsp?ToPage=CarrierDispatchSelections&Title=Carrier Dispatch Selections" );
  OpenModalWinDirect( url, "CarrierDispatchSelections", null, CarrierDispatchSelectionsResponse );
}

function CarrierDispatchSelectionsResponse() {
  if ( winRet == "success" ) DoSubmit();
  else ModalWinReturn( winRet );
}

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 += ">";
  obj += "  <param name=\"Folder\" value=\"" + folder + "\"/>";
  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 menu = document.getElementById( "menu" );
  if ( menu != null ) {
    var lis = 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"), "" );
      }
    }
  }
}

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 = "+";
  }
}

function IZ_POP() {
  if ( window != top ) top.location.href = window.location.href;
}
