﻿/* console logging for IE */


var errhnd = {
  
  pmode: false,
   
  init: function () {
    errhnd.log('errhnd.init');
  },
  
  setpmode: function (pm) {
    errhnd.pmode = pm;
  },
  
  log: function (t) {
    if (typeof console == "undefined") return;
    if (console.log) {
      console.log(t);
    }
  },
  
  logobj: function (obj, name) {
     if (typeof name == "undefined") name = "Object:";
     errhnd.log(errhnd.r_logobj(obj,name));  
  },
  
  
  loghex: function (t,t2) {
    if (typeof console == "undefined") return;
    if (typeof t2 == "undefined") t2 = "undefined";
    if (typeof t2 == null) t2 = "";
    if (console.log) {
      var s = t + ":";
      for (i=0;i<t2.length;i++) {
         s += t2.charCodeAt(i) + ":";
      }
      console.log(s);
    }
  },

  
  
  error: function (n,t) {
     errhnd.log('Error #'+n+':'+t);
     if (errhnd.pmode) {
        alert('Der er opstået en intern fejl med nummer:'+n+', kontakt venligst leverandøren.');
     } else {
        alert('Error #'+n+':'+t);
     }
  },
  
  r_logobj: function (obj, name, indent, depth) {
   
     if (typeof name == "undefined") name = "Object:";
     if (typeof indent == "undefined") indent = "";
     if (typeof depth== "undefined") depth= 0;
   
     if (depth > 10) {
       return indent + name + ": <Maximum Depth Reached>\n";
     }
     if ((obj===null) || (obj.value===null)) {
       return indent + name + ": <null>\n";
     }
     if (typeof obj == "object") {
        var child = null;
        var output = indent + name;
        var total = 0;
        if (obj instanceof Array) {
            total = obj.length;
            output += " (Array)\n";
        } else {
            for (var item in obj) {total++;}
            output += " (Object)\n";
        }
        output += indent + "Total item: " + total + "\n";
        indent += "\t";
        if (obj instanceof Array) {
            if (total>0) {
	            for (var i = 0; i < obj.length; i++) {
	               child = obj[i];
	               output += errhnd.r_logobj(child, i, indent, depth + 1);
	            }
            }
        } else {
            for (var item in obj) {
              try {
                child = obj[item];
              } catch (e) {
                child = "<Unable to Evaluate>";
              }
		      if (child ===null) {
                output += indent + item + ": <null>\n";
		      } else if (typeof child == "object") {
                output += errhnd.r_logobj(child, item, indent, depth + 1);
              } else {
                output += indent + item + ": " + child + "\n";
              }
            }
        }
        return output;
     } else {
        return obj + " is not an object.";
     }
  } // end logobj  
  
}



$(document).ajaxError(function(e, xhr, settings, exception) {

  var x =  "*** ajaxError(request=("+settings.url+")";
      x += ", status=("+xhr.status+" "+xhr.statusText+")";
      x += ", response=("+xhr.responseText+")";
      x += ")";
      
  errhnd.error(11999,x);
}); 


