function CheckAll() {
    var check_form = document.getElementById('check_form');
    for (var i = 0; i < check_form.elements.length; i++) {
	var e = check_form.elements[i];
	if ((e.name != 'check_all') && (e.type=='checkbox')) {
	    e.checked = check_form.check_all.checked;
	}
    }
}

function ToggleVisibility(event, normal_state, element_id, show_kind) {
    var element = document.getElementById(element_id);
    var show;
    if (event.target == null) {
	if (show_kind == "table-row") {
	    show_kind = "block";
        }
	show = event.srcElement.checked;
    } else {
	show = event.target.checked;
    }

    if (show == normal_state) {
	element.style.display = show_kind;
    } else {
	element.style.display = "none";
    }
}

/*
 * Used by the Sell Shares Page.
 */

function getTotalShares(ignore_me)
{
    var sum = 0;
    var table = document.getElementById("sell_shares_table");

    for (var i = 0; i < table.rows.length; i++) {
	var row = table.rows[i];

	if (i == 0) continue;

        for (var j = 0; j < row.cells.length; j++) {
	    var cell = row.cells[j];

	    if (cell.firstChild.type == "text") {
		if (cell.firstChild.id != ignore_me) {
	            sum = sum + new Number(cell.firstChild.value);
		}
            }
        }
    }

    return sum;
}

function isDigit(c)
{
    return ((c >= "0") && (c <= "9"))
}

function SellSharesValidate(text_id, max_shares) {
    var replace = false;
    var rep_value = "";
    var text = document.getElementById(text_id);
    var value = text.value;

    for (var i = 0; i < value.length; i++) {
	if (!isDigit(value[i])) {
	    replace = true;
	} else {
	    rep_value += value[i];
	}
    }

    if (replace) {
	text.value = rep_value;

        var label = document.getElementById("num_shares");

        label.className = "warning";
        label.innerHTML = "Only Enter Decimal Digits in text fields.";
    } else {
	SellSharesComputeAll(max_shares);
    }
}

function SellSharesComputeAll(max_shares) {
    var sum = getTotalShares();
    
    var label = document.getElementById("num_shares");

    var nf = new NumberFormat(sum);
    nf.setPlaces(0);

    if (sum > max_shares) {
        label.className = "warning";
        label.innerHTML = nf.toFormatted() + " -- TOO MANY SHARES";
    } else if (sum == max_shares) {
        label.className = "warning";
        label.innerHTML = nf.toFormatted() + " -- Maximum Allowable";
    } else {
        label.className = "normal";
        label.innerHTML = nf.toFormatted();
    }
}

function SellSharesSet(max_shares, num, text_id) {
    var current = getTotalShares(text_id);

    var text = document.getElementById(text_id); 
    
    if (max_shares < (num + current)) {
	num = max_shares - current;
    }

    if (num < 0) num = 0;

    text.value = num;

    SellSharesComputeAll(max_shares);
}

