
Ticketlogic.register('Popup', {
    Version: '0.1.0.0',

    initialise: function() { },
    show : function() {
        var box = arguments[0];
        var inp = (typeof box == 'string') ? box : box.id;
        if (typeof box == 'string') box = document.getElementById(box);         
        box.style.display = 'block';
    },
    hide : function() {
        var box = arguments[0];
        if (typeof box == 'string') box = document.getElementById(box);
        box.style.display = ''; 
    },
    submit : function() {
        var box = arguments[0];
        if (typeof box == 'string') box = document.getElementById(box);
        box.style.display = '';
        // submit form via AJAX?
        return false;
    }
});

Ticketlogic.register('Forms', {
    Version: '0.1.0.0',

    initialise: function() { },

    reset: function() {
        var _id = id(arguments[0]);
        _id.clear();
    },

    toggleSelect: function() {
        return this.toggleSelects(arguments);
    },

    toggleSelects: function() {
        var _name = arguments[0];
        var arr = document.getElementsByTagName("INPUT");
        for (var i = 0; i < arr.length; i++) {
            var inp = arr[i];
            var rgx = new RegExp("^" + _name + ".*$");
            if (inp.type == 'checkbox' && rgx.match(inp.id)) {
                inp.checked = !inp.checked;
            }
        }
    },

    select: function() {
        var _id = id(arguments[0]);
        if (_id.type && _id.type == 'CHECKBOX') {
            _id.checked = true;
        }
        else {
            _id.select();
        }
    },

    check: function() {
        var _id = arguments[0],
        _type = arguments[1];

    },
    
    sync: function() {
        var _one = id(arguments[0]),  _two = id(arguments[1]);
        _two.value = _one.value;
    },
    
    copySafe: function() {
        var _one = id(arguments[0]),
        _two = id(arguments[1]);
        var title = _one.value.toLowerCase().trim();
        _two.value = title.replace(/[\W]+/g, '-').replace(/[\-]+/g, '-');
    },

    disclose: function() {
        var _id = id(arguments[0]);
        _id.type = 'text';
    },

    obscure: function() {
        var _id = id(arguments[0]);
        _id.type = 'password';
    },

    checkboxRow: function() {
        var _row = id('table-row-' + arguments[0]),
        _sel = id('sel-' + arguments[0]);
        var className = _row.className + '';
        if (_sel.checked) {
            _row.className = className + ' table-row-selected';
        }
        else {
            _row.className = className.replace(/ ?table-row-selected/, '');
        }
    },

    highlightRow: function() {
        var _row = id(arguments[0]);
        var className = _row.className + '';
        if (className.indexOf('table-row-selected') == -1) {
            _row.className = className + ' table-row-selected';
        }
        else {
            _row.className = className.replace(/ ?table-row-selected/, '');
        }
    }
});

Ticketlogic.register('Elements', {
    Version: '0.1.0.0',

    initialise: function() { },

    addClass: function() {
        var _f = id(arguments[0]), _c = arguments[1];
        if (_f.className.indexOf(_c) == -1) {
            _f.className = _f.className + ' ' + _c;
        }
    },

    removeClass: function() {
        var _f = id(arguments[0]), _c = arguments[1];
        if (_f.className.indexOf(_c) > -1) {
            var rgx = eval('/' + _c + '/g');
            _f.className = _f.className.replace(rgx, '');
        }
    },

    visibility: function() {
        var _id = id(arguments[0]), vis = arguments[1];
        _id.style.visibility = vis;
    },

    show: function() {
        var _id = id(arguments[0]);
        _id.style.display = 'block';
    },

    hide: function() {
        var _id = id(arguments[0]);
        _id.style.display = '';
    }
});

Ticketlogic.register('Validator', {
    Version: '0.1.0.0',

    initialise: function() { },
    
    notEmpty: function() {
        var _one = id(arguments[0]);
        if ((_one.value != null) && (_one.value.length >= 1)) {
            Ticketlogic.Elements.removeClass(_one, 'error');
            Ticketlogic.Elements.removeClass(_one.parentNode, 'fielderror'); 
        }
        else {
            Ticketlogic.Elements.addClass(_one, 'error');
            Ticketlogic.Elements.addClass(_one.parentNode, 'fielderror'); 
        }        
    },
    
    password: function() {
        var _one = id(arguments[0]), _two = id(arguments[1]), _role = id(arguments[2]);
        if ((_one.value == _two.value) && (_one.value.length >= 8)) {
            Ticketlogic.Elements.removeClass(_one, 'error');
            Ticketlogic.Elements.removeClass(_two, 'error');
        }
        else {
            Ticketlogic.Elements.addClass(_one, 'error');
            Ticketlogic.Elements.addClass(_two, 'error');
        }
    },

    minLength: function() {
        var f = id(arguments[0]), l = arguments[1];
        if (f.value.length >= l) {
            Ticketlogic.Elements.removeClass(f, 'error');
            Ticketlogic.Elements.removeClass(f.parentNode, 'fielderror'); 
        }
        else { 
            Ticketlogic.Elements.addClass(f, 'error'); 
            Ticketlogic.Elements.addClass(f.parentNode, 'fielderror'); 
        }
    },

    telephone: function() {
        var f = id(arguments[0]);
        if (f.value.match(/ /)) { f.value = f.value.replace(/ /g, ''); }
        if (f.value.match(/^\+?\d+$/) && (f.value.length > 8)) {
            Ticketlogic.Elements.removeClass(f, 'error');
            Ticketlogic.Elements.removeClass(f.parentNode, 'fielderror');
        }
        else { 
            Ticketlogic.Elements.addClass(f, 'error'); 
            Ticketlogic.Elements.addClass(f.parentNode, 'fielderror'); 
        }
    },

    email: function() {
        var f = id(arguments[0]);
        f.value = f.value.toLowerCase();
        if (f.value.match(/^.+@[\w\-\.]{2,}\.[a-z]{2,3}$/)) {
            Ticketlogic.Elements.removeClass(f, 'error');
            Ticketlogic.Elements.removeClass(f.parentNode, 'fielderror');
        }
        else { 
            Ticketlogic.Elements.addClass(f, 'error'); 
            Ticketlogic.Elements.addClass(f.parentNode, 'fielderror'); 
        }
    },

    dateOfBirth: function() {
        var f = id(arguments[0]);
        if (f.value.match(/^\d{2}\/\d{2}\/\d{4}$/)) {
            Ticketlogic.Elements.removeClass(f, 'error');
            Ticketlogic.Elements.removeClass(f.parentNode, 'fielderror');
        }
        else { 
            Ticketlogic.Elements.addClass(f, 'error'); 
            Ticketlogic.Elements.addClass(f.parentNode, 'fielderror'); 
        }
    }
});

Ticketlogic.register('Calendar', {
    Version: '0.1.0.0',

    initialise: function() { },

    init: function() {
        if ( !! !id('ticketlogic-calendar')) {
            var cal = document.createElement('div');
            cal.id = 'ticketlogic-calendar';
            // var cntr = id('container');
            var cntr = document.body;
            cntr.appendChild(cal);
        }
        var inputs = document.getElementsByTagName("INPUT");
        for (var i = 0; i < inputs.length; i++) {
            var inp = inputs[i],
            a = document.createElement("A");
            if (inp.className.indexOf('popupcalendar') == -1) continue;
            var popup = function() {
                var _f = arguments[0];
                return function() {
                    var f = id(_f),
                    c = id('ticketlogic-calendar'),
                    current = Ticketlogic.Date.parse(f.value);
                    var xy = Ticketlogic.Coordinates.locate(f);
                    c.style.left = (xy.x) + 'px';
                    c.style.top = (xy.y + f.offsetHeight + 2) + 'px';
                    c.style.display = 'block';
                    Ticketlogic.Calendar.cal(current.getTime(), f.id);
                }
            };
            a.className = "popupcalendaranchor";
            a.href = "#";
            a.innerHTML = "<img style=\"vertical-align: middle;\" src=\"/images/app1.5/calendar-icon.gif\" />";
            a.onclick = popup(inp.id);
            inp.onclick = popup(inp.id);
            if (inp.nextSibling) {
                inp.parentNode.insertBefore(a, inp.nextSibling);
            } else {
                inp.parentNode.appendChild(a);
            }
        }
    },

    cal: function() {
        var d = new Date(arguments[0]),
        s = new Date(arguments[0]),
        f = id(arguments[1]),
        c = id('ticketlogic-calendar');
        var navl = '<span id="calendar-nav"><span id="calendar-navl"><a href="#" onclick="Ticketlogic.Calendar.clock(' + d.getTime() + ',\'' + f.id +
        '\');"><img style="vertical-align: middle;" src="/images/app1.5/clock-icon.gif" alt="[o]" /></a></span>';
        var navc = '<span id="calendar-navc"><a href="#" onclick="Ticketlogic.Calendar.cal(' + d.addMonths( - 1).getTime() +
        ',\'' + f.id + '\'); return false;">&laquo;</a>&nbsp;' +
        d.getMonthName() + ' ' + d.getFullYear() + '&nbsp;<a href="#" onclick="Ticketlogic.Calendar.cal(' + d.addMonths(1).getTime() + ',\'' + f.id + '\'); return false;">&raquo;</a></span>';
        var navr = '<span id="calendar-navr"><a href="#" onclick="Ticketlogic.Calendar.hide(); return false;"><img style="vertical-align: middle;" src="/images/app1.5/close-icon.gif" alt="[x]" /></a></span></span><span style="clear: both; display: block;"></span>';
        var first = s.addDays(1 - s.getDate()),
        rows = '';
        s = first.addDays(1 - first.getDay());
        if (s.getDate() > 1 && s.getDate() < 7) s = s.addDays( - 7);
        for (var i = 0; ((i < 6) && s.beforeByMonth(d)); i++) {
            rows += '<tr>';
            for (var n = 0; n < 7; n++) {
                var unit = (((i) * 7) + (n + 1));
                rows += '<td class="' + ((s.getMonth() == d.getMonth()) ? 'active-month': '') + ' ' + ((s.isToday()) ? 'today': '') + ' ' + (((s.getDay() == 6) || (s.getDay() == 0)) ? 'weekend': '') +
                '"><a href="#" onclick="Ticketlogic.Calendar.setDate(\'' + f.id + '\',' + (s.getYear() + 1900) + ',' + s.getMonth() +
                ',' + s.getDate() + '); return false;">' + s.getDate() + '</a></td>';
                s = s.addDays(1);
            }
            rows += '</tr>';
        }

        var body = '<tbody>' + rows + '</tbody>';
        var head = '<thead><tr><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th><th>S</th></tr></thead>';
        var table = '<table>' + head + body + '</table>'

        c.innerHTML = navl + navc + navr + table;
    },

    clock: function() {
        var d = new Date(arguments[0]),
        f = id(arguments[1]),
        c = id('ticketlogic-calendar');
        var nav = '<span id="calendar-nav"><span id="calendar-navl"><a href="#" onclick="Ticketlogic.Calendar.cal(' + d.getTime() + ',\'' + f.id +
        '\');"><img style="vertical-align: middle;" src="/images/app1.5/calendar-icon.gif" alt="[o]" /></a></span>' +
        '<span id="calendar-navc">Clock</span><span id="calendar-navr"><a href="#" onclick="Ticketlogic.Calendar.hide(); return false;">' +
        '<img style="vertical-align: middle;" src="/images/app1.5/close-icon.gif" alt="[x]" /></a></span></span><span style="clear: both; display: block;"></span>';

        var hours = '';
        for (var m = 0; m < 24; m++) {
            hours += '<a href="#" onclick="Ticketlogic.Calendar.setHours(\'' + f.id + '\',' + m + '); return false;">' + m + '</a>';
        }

        var mins = '';
        for (var m = 0; m < 60; m += 5) {
            mins += '<a href="#" onclick="Ticketlogic.Calendar.setMins(\'' + f.id + '\',' + m + '); return false;">' + m + '</a>';
        }
        mins += '<a href="#" onclick="Ticketlogic.Calendar.setMins(\'' + f.id + '\',' + 57 + '); return false;">' + 57 + '</a>';
        mins += '<a href="#" onclick="Ticketlogic.Calendar.setMins(\'' + f.id + '\',' + 58 + '); return false;">' + 58 + '</a>';
        mins += '<a href="#" onclick="Ticketlogic.Calendar.setMins(\'' + f.id + '\',' + 59 + '); return false;">' + 59 + '</a>';

        c.innerHTML = nav + '<span class="cl"><strong>Hours</strong></span><span class="cr"><strong>Mins</strong></span><span class="cl">' + hours + '</span><span class="cr">' + mins + '</span>';
    },

    popup: function() {
        var _fld = id(arguments[0]),
        _cal = id('ticketlogic-calendar');
        alert(_fld.id + " " + _cal.id);
    },

    setDate: function() {
        var f = id(arguments[0]),
        y = arguments[1],
        m = arguments[2],
        d = arguments[3];
        var dat = Ticketlogic.Date.parse(f.value);
        dat.setYear(y);
        dat.setMonth(m);
        dat.setDate(d);
        f.value = Ticketlogic.Date.format(dat);
        f.select();
    },

    setHours: function() {
        var f = id(arguments[0]),
        h = arguments[1];
        var dat = Ticketlogic.Date.parse(f.value);
        dat.setHours(h);
        f.value = Ticketlogic.Date.format(dat);
        f.select();
    },

    setMins: function() {
        var f = id(arguments[0]),
        m = arguments[1];
        var dat = Ticketlogic.Date.parse(f.value);
        dat.setMinutes(m);
        f.value = Ticketlogic.Date.format(dat);
        f.select();
    },

    hide: function() {
        var _cal = id('ticketlogic-calendar');
        _cal.style.display = '';
    }

});


