function hasClass(obj, className) {
    return ((" "+obj.className+" ").indexOf(" "+className+" ") >= 0);
}


function simpleConcat(arr1 ,arr2) {
    var arr = [];
    for(var i=0;i<arr1.length;i++) {
        arr[i] = arr1[i];
    }
    for(var i=0;i<arr2.length;i++) {
        arr[arr.length] = arr2[i];
    }
    return arr;
}


function findPrevSibling(el, withClass) {
    if(!el || !el.parentNode || !withClass) { return null; }
    var parentNode = el.parentNode;
    // Go forwards to us.
    for(var i=0;i<parentNode.childNodes.length;i++) {
        if(parentNode.childNodes[i] == el) {
            break;
        }
    }
    // Go backwards for our sibling.
    for(i=i-1; i>=0; i--) {
        if((" "+parentNode.childNodes[i].className+" ").indexOf(" "+withClass+" ") >= 0) {
            return parentNode.childNodes[i];
        }
    }
    return null;
}


function findDescendent(el, withClass) {
    if(!el || !el.childNodes || !withClass) { return null; }
    var nodesToCheck = simpleConcat([], el.childNodes);
    while(nodesToCheck.length > 0) {
        var nodeToCheck = nodesToCheck[nodesToCheck.length-1];
        nodesToCheck.length = nodesToCheck.length - 1;
        if(hasClass(nodeToCheck, withClass)) {
            return nodeToCheck;
        }
        if(nodeToCheck.childNodes) {
            nodesToCheck = simpleConcat(nodesToCheck, nodeToCheck.childNodes);
        }
    }   
    return null;     
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function stringToInt(value) {
    var validChars = "0123456789";
    value = trim(value);
    // The string shouldn't begin with zero unless its "0".
    if(value.length == 0 || 
         validChars.indexOf(value.charAt(0)) == -1 || 
         (value.length > 1 && parseInt(value.charAt(0), 10) == 0)) {
        return NaN;
    }
    for(var i=1;i<value.length;i++) {
        if(validChars.indexOf(value.charAt(i)) == -1) {
            return NaN;
        }
    }
    return parseInt(value, 10);
}


function checkInventory(formEl, quantityLimit) {
    var errorEl = findPrevSibling(formEl, 'item_error');
    var quantityEl = findDescendent(formEl, 'item_qty');
    
    var quantity = stringToInt(quantityEl.value);
    if(isNaN(quantity) || quantity <= 0) {
        errorEl.innerHTML = "<b>Could not add item to cart:</b>"+
        " Please enter a quantity(whole number) to add to your cart.";
        return false;
    } else if(quantity > quantityLimit) {
        errorEl.innerHTML = "<b>Could not add item to cart:</b>"+
        "Sorry, we wish we had that many plants but they grow sooo slow."+
        "Please enter a quantity(whole number) that is smaller than or equal to "+quantityLimit+".";
        return false;
    }
    errorEl.innerHTML = '';
    return true;
}


/*
var min = (function(x, y) { if(x < y) { return x; } else { return y; } });
var max = (function(x, y) { if(x > y) { return x; } else { return y; } });


function calculate_scale(w, h, max_w, max_h) {
    return min(min(max_w/w, max_h/h), 1);
}    


function get_switcher(thumb_id, image_id) {
    var switcher = function() {
        // Switch out the main image with the thumbnail's image.
        var image_el = document.getElementById(image_id);
        var thumb_el = document.getElementById(thumb_id);
        var max_w = 480;
        var max_h = 480;

        if(image_el && thumb_el) {
            var image = new Image();
            image.src = image_name_to_image_path[thumb_el.name];
            var scale = calculate_scale(image.width, image.height, max_w, max_h);
            var w = Math.floor(scale * image.width);
            var h = Math.floor(scale * image.height);
            var w_pad = Math.ceil((max_w - min(w, max_w)) / 2);
            var h_pad = Math.ceil((max_h - min(h, max_h)) / 2);
            image_el.width = w;
            image_el.height = h;
            image_el.src = image.src;
            image_el.title = image_name_to_image_caption[thumb_el.name];
            image_el.alt = image_name_to_image_caption[thumb_el.name];
            image_el.style.margin = h_pad + "px" + " " + w_pad+"px" + " "+ h_pad + "px" + " " + w_pad + "px";
        }
        return false;
    };
    return switcher;
}


function init_thumbs(image_id, thumb_class) {
    var images = document.getElementsByTagName('img');
    for(var i=0;i<images.length;i++) {
        if(images[i].className == thumb_class) {
            var thumb_id = images[i].id;
            // connect the event to the parent link
            images[i].parentNode.onclick = get_switcher(thumb_id, image_id);
        }
    }
}
*/
