var exTerminate="FLiCA Exception: Terminate";

var scripterror=false;
var emsg='A script error occurred on this page.  Please notify Flightline technical support.';
var globNoAlert=false;
window.onerror=function( msg, url, line ) {
  if ( /^.*loading.script.*/.test(msg) ) {
    return true;
  }
  var noalert=globNoAlert;
  if( scripterror ) {
    return true;
  }
  try {
    modalGearboxWindow.close();
  }
  catch( ex ) {}
  try {
    window.parent.modalGearboxWindow.close();
  }
  catch( ex ) {}
  switch( msg ) { // Try to catch errors caused by popup blockers
  case exTerminate:
    scripterror=true;
    return true;
  case "Unspecified error.":
  case "Object required":
  case "Object expected":
  case "Object doesn't support this property or method":
  case "open() failed, nothing popped up":
  case "No such interface supported":
    alert( "A script error occurred on this page.\n\n"+
           "This error may be caused by a program that kills popup windows.  Please check your settings.\n"+
           "If this error persists, please call Flightline technical support with the information below.\n\n"+
           window.location.pathname+", line "+line+": "+msg );
    scripterror=true;
    return true;
  case "Syntax error": // Ignore
    if( line == 2 ) return true;
  }
  if( /^.*vent.is.*defined.*/.test(msg) ) { // Web help message
    noalert=true;
  }
//  if( /^.*bmi.load.*defined.*/.test(msg) ) { // Error in automatically inserted script - perhaps from Vodafone image compression proxy?
//    alert( "An error occurred in script inserted in this page by a third party.  Flightline \n"+
//           "cannot directly prevent this error.  You may be able to change the settings on \n"+
//           "programs that may insert JavaScript into web pages that you view.  If you are \n"+
//           "using a Vodafone UTMS connection, this error may be a bug in the browser software; \n"+
//           "you may be able to upgrade to a more current version.\n" );
//    noalert=true;
//    msg+=" (special user message)";
//  }
  var details=''+window.location;
  var e=new RegExp( "&", "g" );
  details=details.replace( e, "%26" );
  try {
    xmlhttp=new ActiveXObject( "Msxml2.XMLHTTP" );
  }
  catch( ex ) {
    try {
      xmlhttp=new ActiveXObject( "Microsoft.XMLHTTP" );
    }
    catch( ex ) {
      try {
        xmlhttp=new XMLHttpRequest();
      }
      catch( ex ) {
        return false;
      }
    }
  }
  if( line >= 0 ) {
    xmlhttp.open( "GET", '/online/jserror.cgi?&LINE='+line+'&MSG='+msg+'&DETAILS='+details+'&CGI='+window.location.pathname, true );
  }
  else {
    xmlhttp.open( "GET", '/online/jserror.cgi?NEWMSG='+msg+'&DETAILS='+details+'&CGI='+window.location.pathname, true );
  }
  xmlhttp.onreadystatechange=function() {
    if( xmlhttp.readyState == 4 && !noalert ) {
      alert( emsg );
    }
  }
  xmlhttp.send( null );
  scripterror= !globNoAlert; // Alert on next script error if this one was "silent"
  return true;
}

function reportScriptError( msg, noalert ) {
  globNoAlert= noalert != null ? noalert : false;
  window.onerror( msg, null, -1 );
  globNoAlert=false;
}
