function validateSearch() {
    if (document.search.q.value.match(/^\s*$/) != null) {
        alert("Please enter a search phrase");
        return false;
    }
    
    return true;
}

function validateEmail(pAddress) {
    var address = new String();
    address = pAddress;

    if (pAddress.length == 0 || pAddress.indexOf("@") == -1) {
        alert("The e-mail address you entered is invalid.  Please correct " +
            "it and try again");
        return false;
    }

    var valid = /[\w]+@[\w]+\.[\w]+/;
                                                                                
    if (!valid.test( pAddress)) {
        return confirm("[" + pAddress + "] doesn't appear to be a valid e-mail address.  Are you sure you want to subscribe with it?\n\nOK to subscribe\nCancel to fix the e-mail address.");
    }

    return true;
}

function roundNum(n) {
    var dec = 2;
    return Math.round(n*Math.pow(10,dec))/Math.pow(10,dec);
}

function addCommas(n) {
    n += '';
    x = n.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1))
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
    return x1 + x2;
}

// Return number that is precise to 2 decimal points
function toFixed(value) {
    var precision = 2
    var power = Math.pow(10, precision || 0);
    var n = String(Math.round(value * power) / power)
    if (n.indexOf('.') == -1) n += ".";
    while (n.length - n.indexOf('.') <= precision) n += "0";
    return n;
}

function showhide(id) {
    obj = document.getElementById(id);
    obj.style.display = (obj.style.display == "none") ? "" : "none";
}

// Position ad relative to answer box
// TODO In IE 8 ad stil shows up when you close answer - make it go away
function showFloatingAd(id) {
    var pos = $("#" +id).offset();
    var height = $("#" +id).height();
    var width = $("#" +id).width();
    var topPos = pos.top - 65;
    var leftPos = pos.left;
    $("#floatingAd").css({"left": leftPos + "px", "top":topPos + "px"});
    $("#floatingAd").show();
}


