function showhide(arg) {
    var x=document.getElementById(arg).style;
    if (x.display=="none") {
        x.display="block" ;
    } else {
        x.display="none" ;
    }
}


function changeSelectionForInputs(base, max, toselect) {
    for (var i=0; i < max; i++) {
        el = document.getElementById(base + i);
        el.checked = toselect;
    }
}


function ajaxRequest(url, params, callback) {
    new Ajax.Request(url, {
        onComplete: function(transport) {
            var dictionary;
            try {
                dictionary = eval( "(" + transport.responseText + ")" );
            } catch (error) {
                alert("Failed to parse: " + transport.responseText + " error=" + error);
                $('messages') = "Failed to parse: " + transport.responseText + " error=" + error;
                return;
            }
            var code = dictionary['code'];

            if (code == 302 || code == '302') {
                var loc = dictionary['location'];
                if (loc) {
                    try {
                        location.replace(loc);
                    } catch (error) {
                        alert("failed to redirect: " + error);
                    }
                    return;
                } else {
                    alert("no location to redirect to");
                }
            }
            callback(dictionary);
        },
        onFailure: function(){ alert('Something went wrong...') },
        on404: function() {alert("cannot find that function");},
        on500: function() {alert("permission denied");},
        on302: function() {alert("redirect");},
        parameters: params,
    });
}


function ensureSet(anArray) {
    var newArray = [];
    for (var i=0; i<anArray.length; i++) {
        if (indexOf(newArray, anArray[i]) == -1) {
            newArray.push(anArray[i]);
        }
    }
    return newArray;
}

function arrayToClassString(anArray) {
    return ensureSet(anArray).join(" ");
}

function classStringToArray(aString) {
    return ensureSet(aString.split(" "));
}

// perform a set minus operation... a1 - a2 = a3
// returns a new array, does not modify a1 or a2
function minus(a1, a2) {
    var retval = a1.slice(0); // copy the old array
    for (var i = 0; i<a2.length; i++) {
        for (var j=0; j<retval.length; j++) {
            if (retval[j] == a2[i]) {
                retval.splice(j, 1); // take a bite out of crime
            }
        }
    }
    return retval;
}

function plus(a1, a2) {
    return a1.concat(a2);
}

function indexOf(anArray, item) {
    for (var i=0; i<anArray.length; i++) {
        if (anArray[i] == item) {
            return i;
        }
    }
    return -1;
}


