// Easynews Shift-Click
// version 0.2
// Copyright (c) 2006, Mike Cao
//
// --------------------------------------------------------------------
// Disclaimer:
// This script is third party software and is not affiliated with
// Easynews in any way. Easynews has not reviewed nor endorsed this
// software. Use at your own risk.
// --------------------------------------------------------------------
//
// ==UserScript==
// @name           Easynews Shift-Click
// @description    Adds shift-click functionality to easynews.com.
// @include        http://members.easynews.com/*
// @namespace      http://www.mikecao.com/
// ==/UserScript==

var shiftClick = false;
var lastClick = 0;
var lastCheck = false;
var clickBoxes = new Array();

function keyDown(e) {
    var key = (window.event) ? event.keyCode : e.keyCode;
    if (key == 16) {
        shiftClick = true;
    }
}

function keyUp(e) {
    var key = (window.event) ? event.keyCode : e.keyCode;
    if (key == 16) {
        shiftClick = false;
    }
}

function checkClick() {

    var thisClick = parseInt(this.getAttribute('index'));

    var e = document.getElementById(this.id);
    lastCheck = e.checked;

    if (shiftClick && thisClick != lastClick) {
        var start = lastClick;
        var end = thisClick - 1;
        var allChecked = true;

        if (thisClick < lastClick) {
            start = thisClick;
            end = lastClick - 1;
        }
    
        for (var i = start; i < end; i++) {
            if (clickBoxes[i].checked == false) {
                allChecked = false;
                break;
            }
        }

        if (allChecked) {
            checkRange(start, end, false);
        }
        else {
            checkRange(start, end, true);
        }
    }

    lastClick = thisClick;
}

function checkRange(start, end, checked) {
    for (var i = start; i < end; i++) {
        clickBoxes[i].checked = checked;
    }
}

function isInt(str) {
    var i = parseInt(str);

    if (isNaN(i)) return false;

    return true;
}

function loadCheckboxes() {
    var index = 0;
    var elements = document.getElementsByTagName("input");

    for (var i = 0; i < elements.length; i++) {
        var e = elements[i];
        if (e.type == "checkbox" && isInt(e.name)) {
            index++;
            e.setAttribute('index', index);
            if (!e.id) e.id = index;
            e.addEventListener('click', checkClick, true);
            clickBoxes[clickBoxes.length] = e;
        }
    }
}

window.document.addEventListener('keydown', keyDown, false);
window.document.addEventListener('keyup', keyUp, false);

loadCheckboxes();
