$.scope = function(target,func){ return function() { func.apply(target,arguments);}};

function zeroPadding(num, length) {
    var str = "0000000000"+num;
    return str.substring(str.length-length);
}

function n(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
	x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function short_id(id) {
    str = ""
    do {
	str = "abcdefghijklmnopqrstuvwxyz".charAt(id % 26) + str ;
	id = Math.floor(id / 26);
    } while (id > 0);
    return str;
}
    
var truncate = function(str, len) {
    if(!str) return "";
    if(str.length<=len) return str;
    return str.substring(0,len-3)+"...";
}

var escape_html = (function(){
  var map = {"<":"&lt;", ">":"&gt;", "&":"&amp;", "'":"&#39;", "\"":"&quot;", " ":"&nbsp;"};
  var replaceStr = function(s){ return map[s]; };
    return function(str) { return (str+"").replace(/<|>|&|\'|\"|\s/g, replaceStr); };
})();
var h = escape_html;

var escape_js = (function(){
  var replaceStr = function(s){ return "\\"+s; };
    return function(str) { return (str+"").replace(/\'|\"/g, replaceStr); };
})();

var post_link = function(a){
  var f = document.createElement("form");
  document.body.appendChild(f);
  
  // Add action and method attributes
  f.action = a.href
  f.method = "POST"

  // Call the form's submit method
  f.submit();

  return false;
};

Date.parseISO8601 = function (string) {
    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var date = new Date(d[1], 0, 1);
    
    if (d[3]) { date.setMonth(d[3] - 1); }
    if (d[5]) { date.setDate(d[5]); }
    if (d[7]) { date.setHours(d[7]); }
    if (d[8]) { date.setMinutes(d[8]); }
    if (d[10]) { date.setSeconds(d[10]); }
    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    date.setTime(Number(time));
    return date;
}

function date_format(date) {
    yy = date.getUTCFullYear() % 100;
    mm = date.getUTCMonth() + 1;
    dd = date.getUTCDate();
    HH = date.getUTCHours();
    MM = date.getUTCMinutes();

    if (yy < 10) { yy = "0" + yy; }
    if (mm < 10) { mm = "0" + mm; }
    if (dd < 10) { dd = "0" + dd; }
    if (HH < 10) { HH = "0" + HH; }
    if (MM < 10) { MM = "0" + MM; }
    
    return(mm + "-" + dd + "-" + yy + " " + HH + ":" + MM + " UTC");
}

function encodeToHex(str){
    var h, r = "", e = str.length, c = 0;
    while(c<e){
        h = "000" + str.charCodeAt(c++).toString(16);
        r += h.substring(h.length-4);
    }
    return r;
}

function decodeFromHex(str){
    var s, r = "", e = str.length;
    while(e>0){
        s = e - 4;
        r = String.fromCharCode("0x"+str.substring(s,e)) + r;
        e = s;
    }
    return r;
}
