
jl_eh_debuglevel = 2;
jl_eh_outputsepwin = false;
jl_eh_srcurl = null;


jl_eh_setoutputsepwin = function(value){
    jl_eh_outputsepwin =  value;
}

jl_eh_setdebuglevel = function(level){
    jl_eh_debuglevel = level;
    if(level === 0){
       window.onerror=null;
    }else{
       window.onerror = jl_eh_handleErr;
    }

}

jl_eh_setsrcurl = function(url){
    jl_eh_srcurl = url;
}

jl_eh_getsrcurl = function(url){
    return jl_eh_srcurl;
}

jl_eh_dumpobject = function(prefix,theobject,outwin){

      var avar = null; 
      for(avar in theobject) {
          var newprefix = prefix + "[" +  avar + "]";
 
          if(typeof(theobject[avar]) === 'function'){
              continue;
          }   
          outwin.document.write(newprefix + " = " + theobject[avar] + "<br>" );

           
     }
}

jl_eh_dispconfbox = function(msg){

     var txt = msg + "\r\n " + " click ok to continue, click cancel to terminate";

    var r=confirm(txt);
    if (r == false)
    {
        //force termination by generating an error
        addlert();
    }

 
}


jl_eh_handleErr = function(msg,url,line){

    var outwin = null;

    if(jl_eh_debuglevel >= 2){ 
   
        if(jl_eh_outputsepwin === true){
            outwin = window.open();
        }else{
            outwin = window;
        }

    }     


    function outputurl(){
        var themsg = null;
        //check the current page first before the .js files

        if(jl_eh_getsrcurl() !== null){
  
        themsg = "Error: " + msg + " url " + jl_eh_getsrcurl() + " line " + line; 

        jl_eh_displayerrhtml(jl_eh_getsrcurl(),themsg,50,Number(line),outwin);   
          

        }else{
            //this does not work with angel fire. Have to set the 
            //src properly
            themsg = "Error: " + msg + " url " + url + " line " + line; 
            jl_eh_displayerrhtml(url,themsg,50,Number(line),outwin);

        }
    }//end outputurl 

    function outputscripts(){

        var themsg = null;
        var scriptnodes = document.getElementsByTagName("script"); 
    
        var i = 0;
        for(i = 0;i<scriptnodes.length;i += 1){
 
             if(scriptnodes[i].src){
                 
                 themsg = "Error: " + msg + " script file " + scriptnodes[i].src + " line " + line; 

                 jl_eh_displayerrhtml(scriptnodes[i].src,themsg,50,Number(line),outwin);      

             }    
        }
    }//end outputscripts
    
    function displayconfirmbox(){
        
        var txt = "";
        
        txt += "Error: " + msg + "\n";
        txt += "URL: " + url + "\n";
        txt += "Line: " + line + "\n\n";
            
        if(jl_eh_debuglevel >= 2) {
            txt += "src files outputed to page\n\n";
        }
    
        txt += "Click OK to continue \n\nCANCEL to terminate.\n\n";
        var r = confirm(txt);

        if (r == false){
            //force termination by generating an error
            addlert();
        }
    }

    
    if(jl_eh_debuglevel >= 2){
          outputurl();
          outputscripts();
    } 

    displayconfirmbox();


    return true;
}

jl_eh_displayerrhtml = function(url,msg,totallines,linehighlight,outwin){

    var params = {};
    var custparams = {};


    custparams.errorcb = function(txt,theparams){

        if(txt === "Permission denied"){

             alert("jl_eh_displayerrhtml error " + txt + "\r\n" + "enable your browser to access data across domains"); 

        }else{            
            alert("jl_eh_displayerrhtml error " + txt); 
            //force termination
            addlert();  
        }
  
    }


    function parsetxt(txt){
        
        var newtxt = txt.replace(/\r\n/g,"\n");
        newtxt = newtxt.replace(/\r/g,"\n");
        
        var wrkarray = newtxt.split("\n");
        var i=0; 
        newtxt = "";
        for(i=0;i<wrkarray.length;i += 1){
            var tmptxt = String(i + 1) + " " + wrkarray[i];
            wrkarray[i] = tmptxt;

        }  
        return wrkarray;
    }   

    custparams.parsecb = function(thedata,theparams){
         
        //local variables stored in lv json object
        var lv = {};
        lv.data = parsetxt(thedata);

        function lineinrange(){

            if(!theparams.linehighlight){
                return true;
            }

            if((theparams.linehighlight - 1) >= lv.data.length){
                return false; 
            }    

        }


        function initdiv(){

            //search for unused div. 

            var i = 0; 
            var thedoc = outwin.document;   
            while(1){
                lv.thediv = thedoc.getElementById("debugdiv" + String(i));

                if(lv.thediv){ 
                    i += 1;
                    continue;
                }

                lv.thediv=thedoc.createElement("div"); 
                lv.thediv.id = "debugdiv" + String(i); 
                lv.thediv.style.backgroundColor = "black";
                lv.thediv.style.color = "silver";
                thedoc.body.appendChild(lv.thediv);
                break;
   
            }   
            lv.thediv.appendChild(thedoc.createElement("br"));
        } 
         
        function createtable(){
            
            var thedoc = outwin.document;
            var tablenode = thedoc.createElement("table");
            tablenode.style.backgroundColor = "black";
            tablenode.style.color = "silver";
            tablenode.width = "100%";
            lv.thediv.appendChild(tablenode);
            tablenode.border = "1";

            var thnode = thedoc.createElement("thead");
            tablenode.appendChild(thnode);

            lv.headerrow =  thedoc.createElement("tr");
            thnode.appendChild(lv.headerrow);
            lv.headerdata = thedoc.createElement("th");

            var tbnode = thedoc.createElement("tbody");
            tablenode.appendChild(tbnode);

            var bodyrow =  thedoc.createElement("tr");
            tbnode.appendChild(bodyrow);
            lv.bodydata = thedoc.createElement("td");
            bodyrow.appendChild(lv.bodydata);
      
        }//end createtable
        
        function filltableheader(){
             
            var thedoc = outwin.document;
            if(theparams.msg){

                lv.headerdata.appendChild(thedoc.createTextNode(theparams.msg));
                lv.headerrow.appendChild(lv.headerdata);


            }
        }//end filltableheader

        function filltabledata(){ 
            var i=0;
            var startindex = 0;
            var thedoc = outwin.document;
            var endindex = lv.data.length - 1;
              
            if(theparams.totallines){
               
                if(theparams.linehighlight){

                     startindex = theparams.linehighlight - (theparams.totallines/2) - 1;

                     if(startindex < 0){
                         startindex = 0;
                     }   

                 }   
                 endindex = (startindex + theparams.totallines) - 1; 
                 if(endindex >= lv.data.length){
                     endindex = lv.data.length - 1;    
                 } 
            }
 

            for(i=startindex;i <= endindex;i += 1){
                
                var textnode = thedoc.createTextNode(lv.data[i]);
                var brnode = thedoc.createElement("br"); 
                var fileline = i + 1;

                if(fileline === theparams.linehighlight){
                    var spnode =  thedoc.createElement("span");     
                    spnode.style.color = "black";
                    spnode.style.backgroundColor = "silver";
                    spnode.appendChild(textnode);
                    lv.bodydata.appendChild(spnode);
                    lv.bodydata.appendChild(brnode);
                    continue;
                }

                lv.bodydata.appendChild(textnode);
                lv.bodydata.appendChild(brnode);

            }

        }//end filltabledata

        function filltable(){

            filltableheader();  
            filltabledata();
        }

        if(lineinrange() === false){

            return; 

        } 
        initdiv();
        createtable();
        filltable();  

    }//end displaycb

    params[jlaurino.xmlhttp.RequestParameters.CONTENT_TYPE] = jlaurino.xmlhttp.ContentType.TEXT;

    custparams.parseparams = {};
    custparams.parseparams.linehighlight = linehighlight;
    custparams.parseparams.totallines = totallines;
    custparams.parseparams.msg = msg;

    jlaurino.xmlhttp.readfile(url,params,custparams);
    
}

window.onerror = jl_eh_handleErr;