function validateZip(zip) {
    if (isNaN(zip)) {
        window.alert('Please enter a valid zip code.');
        return;
    }
    zip = Math.floor(zip);
    if (zip < 1 || zip > 99999) {
        window.alert('Please enter a valid zip code.');
        return;
    }
    return zip;
}

function bookmarkSite(url, title) {
    if (window.sidebar) // Firefox
        window.sidebar.addPanel(title, url, '');
    else if (window.opera && window.print) { // Opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    } 
    else if (document.all) // Internet Explorer
        window.external.AddFavorite(url, title);
}

function createElement(type, name) {
    var element = null;

    try {
        element = document.createElement('<'+type+' name="'+name+'">');
    }
    catch (e) { }

    if (!element) {
       element = document.createElement(type);
       element.name = name;
    }
    return element;
}

function toNum(val) {
    return (val - 0);
}
function toString(val) {
    return (val + '');
}

function numDefault(num) {
    if (!num) return '-';
    return num;
}
function dollarDefault(num) {
    if (!num) return '-';
    return '$' + roundPrecise(num, 2);
}

// Rounds with fixed number of decimal places
function roundPrecise(num, precision) {
    num = Math.round(num * Math.pow(10, precision)) /
                           Math.pow(10, precision);
    return num.toFixed(precision);
}

// Pads left to specified number of digits
function zeroPad(num, padding) {
    num += '';
    while (num.length < padding) num = '0' + num;
    return num;
}

// Adds commas every 3 digits
function addCommas(num) {
    if (!toNum(num)) return '-';
    num = toString(num);

    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(num)) {
        num = num.replace(rgx, '\$1' + ',' + '\$2');
    }
    return num;
}

// Use to limit textarea size
function maxSize(e, num) {
    if (e.value.length > num)
        e.value = e.value.substring(0, num);
}

var utilsLoaded = true;
