function inlineCreate(objectName, url, type, size, prefix, objectHandle) {
    var value = prompt("Enter the name of the "+objectHandle+":");
    if(value) {
        new Ajax.Request( url, {
                method: 'post',
                parameters: objectName+'.name=' + value,
                onSuccess: function(response) {

                    //Check for errors
                    var json = eval('(' + response.responseText + ')');
                    if(!json || !json.id || !json.name) {
                        alert('Could not add '+objectHandle);
                    } else if(json.errors) {
                        var err = "Could not add "+objectHandle+": \n";
                        for (i = 0; i < json.errors.length; i++) {
                            err += "\n" + json.errors[i];
                        }
                        alert(err);
                    }

                    //Now do the right thing, based on the type
                    if(type == 'option') {
                        addOption(objectName, json.id, json.name);
                    } else if(type == 'checkbox') {
                        addCheckbox(objectName, json.id, json.name, size, prefix + json.id);
                    }

                }
            }
        );

    }
}
function inlineDestroy(objectName, url, prefix,objectHandle){
    var value = prompt("Enter the name of the "+objectHandle+":");
    if(value) {
        new Ajax.Request( url, {
                method: 'post',
                parameters: objectName+'.name=' + value,
                onSuccess: function(response) {

                    //Check for errors
                    var json = eval('(' + response.responseText + ')');
                    if(!json || !json.id || !json.name) {
                        alert('Could not remove '+objectHandle);
                    } else if(json.errors) {
                        var err = "Could not remove "+objectHandle+": \n";
                        for (i = 0; i < json.errors.length; i++) {
                            err += "\n" + json.errors[i];
                        }
                        alert(err);
                    }

                    //Now do the right thing, based on the type
                   $(prefix + json.id).innerHTML = 'Removed';

                }
            }
        );
}
}
function toggleForm(form, link, focusField) {
  try {
    link.hide();
    form.show();
    focusField.focus();
  } catch(e) {
    alert(e);
  }
  return false;
}

function addOption(e, id, name) {
    target = $(e+'s');
    target.options[target.options.length] = new Option(name, id);
    target.value = id;
}

function addCheckbox(e, id, name, cols, tdId) {
    target = $(e+'s');
    if(target) {
        var tbody = target.tBodies[0];

        //Create tbody if necessary
        if(!tbody) {
            tbody = document.createElement('tbody');
            $(id).appendChild(tbody);
        }

        //Create row if this table has no rows
        if(tbody.rows.length < 1) {
            var tr = document.createElement('tr');
            tbody.appendChild(tr);
        }

        var lastRow = tbody.rows[tbody.rows.length-1];

        //If the lastRow is full, then create a new row
        if(lastRow.cells.length == cols && lastRow.cells[cols-1].innerHTML != '&nbsp;') {
            var tr = document.createElement('tr');
            tbody.appendChild(tr);
            lastRow = tr;
        }

        //If the lastRow doesn't have the right amount of cells, fill it up
        if(lastRow.cells.length < cols) {
            numCells = cols-lastRow.cells.length;
            for(i = 0; i < numCells; i++) {
                td = document.createElement('td');
                td.innerHTML = '&nbsp;';
                td.width = 100/cols+'%';
                lastRow.appendChild(td);
            }
        }

        //Now, find the first cell that is empty
        for(i = 0; i < cols; i++) {
            if(lastRow.cells[i].innerHTML == '&nbsp;') {
                cell = lastRow.cells[i];
                cell.id = tdId;
                break;
            }
        }

        //Lastly, put the checkbox into the cell
        cell.innerHTML = '<label><input type="checkbox" name="'+e+'Ids" '+
            'value="'+id+'" checked="checked"/>&nbsp;'+name+'</label>';

    } else {
        console.error("Target does not exist");
    }
}

function invoke(form, event, container) {
    var params = Form.serialize(form);
    if (event != null) params = event + '&' + params;
    new Ajax.Updater(container, form.action, {method:'post', postBody:params});

}

function quickMark(form, event, paramName, paramValue, container){
    var params = event + '&' + paramName + '=' + paramValue;
    new Ajax.Updater(container, form.action, {method:'post', postBody:params});
}
