// Direct MooTools port of Ext.ux.UUID.js - http://ext.vosandhowden.com/ux/uuid/Ext.ux.UUID.js
// Original version copyright (C) 2006, Erik Giberti (AF-Design), and licensed GNU GPL

var UUID = new Class({
    initialize: function() {
        this.id = '';
        if(Browser.Engine.trident) {
            /* for IE users, we can use a built-in ActiveX object to retrive a GUID and trim it
            to 38 characters to more closely resemble a UUID. */
            try {
                var guid = (new ActiveXObject('Scriptlet.TypeLib').GUID).substr(1, 36).split('-');
                this.id = guid[0] + '-' + guid[1] + '-' + guid[2] + '-' + guid[3] + guid[4];
            }
            catch(e) {this.generateUUID();}
        }
        else
            this.generateUUID();
    },
    generateUUID: function() {
        /* Loose interpretation of the specification DCE 1.1: Remote Procedure Call
        described at http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagtcjh_37
        since JavaScript doesn't allow access to internal systems, the last 48 bits
        of the node section is made up using a series of random numbers (6 octets long). */
        var dg = new Date(1582, 10, 15, 0, 0, 0, 0).getTime();
        var dc = new Date().getTime();
        var t = (dg < 0) ? Math.abs(dg) + dc : dc - dg;
        var h = '-';
        var tl = this.getIntegerBits(t, 0, 31);
        var tm = this.getIntegerBits(t, 32, 47);
        var thv = this.getIntegerBits(t, 48, 59) + '1'; // version 1, security version is 2
        var csar = this.getIntegerBits(Math.randRange(0, 4095), 0, 7);
        var csl = this.getIntegerBits(Math.randRange(0, 4095), 0, 7);
        
        /* since detection of anything about the machine/browser is far too buggy, include
        some more random numbers here if nic or at least an IP can be obtained reliably,
        that should be put in here instead. */
        var n = this.getIntegerBits(Math.randRange(0, 8191), 0, 7) +
                this.getIntegerBits(Math.randRange(0, 8191), 8, 15) +
                this.getIntegerBits(Math.randRange(0, 8191), 0, 7) +
                this.getIntegerBits(Math.randRange(0, 8191), 8, 15) +
                this.getIntegerBits(Math.randRange(0, 8191), 0, 15); // this last number is two octets long
        this.id = tl + h + tm + h + thv + h + csar + csl + n;
    },
    getIntegerBits: function(val, start, end) {
        var quadArray = val.toString(16).toUpperCase().split('');
        var quadString = '';
        for(var i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {
            if(!quadArray[i] || quadArray[i] == '') quadString += '0';
            else quadString += quadArray[i];
        }
        return quadString;
    }
});


Math.randRange = function(min, max) { 
    return Math.max(Math.min(Math.round(Math.random() * max), max), min);
}
        