
/**
 * This is a generic function that pops up a confirm() dialog -- mostly
 * for delete links in the admin.
 * 
 * 
 */
function confirmRelocate(url, msg) {
    if (confirm(msg)) {
        window.location = url;
    }
}

var blurbCount = 0;

/**
 * Writes a new row in the books table for blurbs.
 * 
 * 
 * 
 */
function addBlurbRow() {
    blurbCount++;
    
    var tbl = document.getElementById('bookTable');
    var lastRow = tbl.rows.length;
    var labelRow = tbl.insertRow(lastRow);
    
    var labelCell = labelRow.insertCell(0);
    labelCell.colSpan = 2;
    labelCell.className = 'labelHuge';
    
    var labelCellLabel = document.createElement('label');
    var labelCellLabelText = document.createTextNode('Blurb ' +
                                                     blurbCount +
                                                     ': ');
    labelCellLabel.appendChild(labelCellLabelText);
    labelCell.appendChild(labelCellLabel);
    
    var lastRow = tbl.rows.length;
    var textareaRow = tbl.insertRow(lastRow);
    
    var textareaCell = textareaRow.insertCell(0);
    textareaCell.colSpan = 2;
    textareaCell.className = 'inputHuge';
    
    var textarea = document.createElement('textarea');
    textarea.className = 'huge';
    textarea.style.height = '170px';
    textarea.name = 'blurb' + blurbCount;
    
    textareaCell.appendChild(textarea);
}

/**
 * Hopefully keeps bad things from happening when very large, non-image
 * files are uploaded.
 * 
 * 
 */
function validateImageFilename(obj) {
    if (obj.value.length) {
        s = obj.value.search(/[^\.]\.(jpg|jpeg|png|gif)$/gi);
        if (s == -1) {
            alert('The file you are trying to upload is not allowed on this system.\n' +
                  'Please choose a jpeg, gif, or png image.');
            obj.focus();
            return false;
        }
    }
    return true;
}

/**
 * Validates user form before submitting.
 * 
 * 
 * 
 */
function checkUserForm(obj) {
    if (!checkTextField(obj.name, 'Please enter a user name.')) {
        return false;
    }
    if (!checkTextField(obj.password, 'Please enter a password.')) {
        return false;
    }
    
    return true;
}


/**
 * Validates book form before submitting.
 * 
 * 
 * 
 */
function checkBookForm(obj) {
    if (!checkTextField(obj.title, 'Please enter a title.')) {
        return false;
    }
    if (!checkTextField(obj.author, 'Please enter an author.')) {
        return false;
    }
    if (!checkTextField(obj.format, 'Please enter a format.')) {
        return false;
    }
    if (!checkTextField(obj.price, 'Please enter a price.')) {
        return false;
    }
    if (!checkTextField(obj.genre, 'Please enter the genre.')) {
        return false;
    }
    if (!checkTextField(obj.ISBN, 'Please enter the ISBN.')) {
        return false;
    }
    if (!validateImageFilename(obj.imageFilename)) {
        return false;
    }
    if (!validateImageFilename(obj.excerptImageFilename)) {
        return false;
    }
    return true;
}


/**
 * Validates what's new form before submitting.
 * 
 * 
 * 
 */
function checkWhatsNewForm(obj) {
    if (!checkTextField(obj.title, 'Please enter a title.')) {
        return false;
    }
    if (!checkTextField(obj.blurb, 'Please enter a blurb.')) {
        return false;
    }
    return true;
}

/**
 * Validates events author form before submitting.
 * 
 * 
 * 
 */
function checkAuthorForm(obj) {
    if (!checkTextField(obj.author, 'Please enter an author.')) {
        return false;
    }
    return true;
}

/**
 * Validates event form before submitting.
 * 
 * 
 * 
 */
function checkEventForm(obj) {
    if (!checkTextField(obj.location, 'Please enter a location.')) {
        return false;
    }
    return true;
}
