var strBrowser
if (navigator.appName == 'Netscape') {
    if (document.layers)
        strBrowser = "N";
    else if (document.getElementById)
        strBrowser = "NS6";
}
else
    strBrowser = "IE";

// Retrieves the objElement's upper left corner position.
function findPos(objElement) {
    var posX = posY = 0;

    while (objElement != null) {
        posX += objElement.offsetLeft;
        posY += objElement.offsetTop;
        objElement = objElement.offsetParent;
    }

    return { x: posX, y: posY };
}

// Retrieves the browser window's dimensions.
function windowDims() {
    var elementWidth = 0;
    var elementHeight = 0;

    if (window.innerWidth || window.innerHeight) {
        // Netscape/Mozilla.
        elementWidth = window.innerWidth;
        elementHeight = window.innerHeight;
    }
    else if (document.documentElement.clientWidth || document.documentElement.clientHeight) {
        // IE 6+.
        elementWidth = document.documentElement.clientWidth;
        elementHeight = document.documentElement.clientHeight;
    }
    else if (document.body.clientHeight) {
        // IE quirks mode.
        elementWidth = document.body.clientWidth;
        elementHeight = document.body.clientHeight;
    }

    return { width: elementWidth, height: elementHeight };
}

function openNewBrowser(address, width, height, resizable, scrollbars) {
    if (typeof (width) == 'undefined')
        width = 400;

    if (typeof (height) == 'undefined')
        height = 400;

    if (typeof (resizable) == 'undefined')
        resizable = "yes";

    if (typeof (scrollbars) == 'undefined')
        scrollbars = "yes";

    var newWindow = window.open(address, 'Popup_Window', 'left=20,top=20,width=' + width + ',height=' + height + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + scrollbars + ',resizable=' + resizable);
    newWindow.focus();
}

var restoreImageSrc;

function swapImage(orgImageId, swapImageId) {
    var orgImage = $("#" + orgImageId);
    var swapImage = $("#" + swapImageId);

    restoreImageSrc = orgImage.get(0).getAttribute("src");
    var orgImageSrc = restoreImageSrc;
    var swapImageSrc = swapImage.get(0).getAttribute("src");

    var results;

    results = splitSrc(orgImageSrc);
    orgImageSrc = results.imageSrc;
    var orgImageScene7Params = results.scene7Params;

    results = splitSrc(swapImageSrc);
    swapImageSrc = results.imageSrc;
    var swapImageScene7Params = results.scene7Params;

    if (orgImageSrc != swapImageSrc) {
        // Swap 'em!
        orgImage.attr("src", swapImageSrc + orgImageScene7Params);
        //swapImage.attr("src", orgImageSrc + swapImageScene7Params);
    }
}

function restoreImage(orgImageId) {
    $("#" + orgImageId).attr("src", restoreImageSrc);
}

// If a Scene7 image, get the parameters.
function splitSrc(imageSrc) {
    var scene7Params;
    var position = imageSrc.indexOf("?");
    if (position > 0) {
        scene7Params = imageSrc.substring(position);
        scene7Params = scene7Params.replace("&amp;", "&");
        imageSrc = imageSrc.substring(0, position);
    }

    return { imageSrc: imageSrc, scene7Params: scene7Params };
}

function trimTerm(term) {

    returnString = "";

    for (idx = 0; idx < term.length; idx++) {
        if ((term.charAt(idx) != " ") && (term.charAt(idx) != null)) {
            returnString = returnString + term.charAt(idx);
        }
    }

    return returnString;
}

function checkQuantities() {
    var isValid = true;
    var hasQuantity = false;

    // Loop over all the input tags that have an id that start with "prd".
    $("input[id*='prd']").each(function() {
        // Ensure all the fields have valid data.
        if (this.value.search(/^\s*$/) == 0 || this.value.search(/^\s*\d+\s*$/) == 0) {
            // Check that at least one item has a quantity.
            if (parseInt(this.value) > 0)
                hasQuantity = true;
        }
        else {
            isValid = false;
            return;
        }
    });

    if (isValid) {
        if (hasQuantity)
            return (true);
        else {
            window.alert("You must enter a quantity");
            return (false);
        }
    }
    else {
        window.alert("You must enter only valid quantities");
        return (false);
    }
}

function formatCurrency(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) {
        i = 0.00;
    }
    var minus = '';
    if (i < 0) {
        minus = '-';
    }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) {
        s += '.00';
    }
    if (s.indexOf('.') == (s.length - 2)) {
        s += '0';
    }
    s = minus + s;
    return s;
}
