﻿function CheckInventory(productid, divid, outmsg, inmsg, f)
{
    var color = document.getElementById('product-color');
    var size = document.getElementById('product-size');
    
    var vars = new Array()
    var colorid = '0';
    var sizeid = '0';
    
    if(color != null) colorid = color.options[color.selectedIndex].value;
    if(size != null) sizeid = size.options[size.selectedIndex].value;
    
    vars[0] = 'color=' + colorid;
    vars[1] = 'size=' + sizeid;
    vars[2] = 'product-id=' + productid;
    
    var a = new AjaxCall('get.product.inventory', vars);
    var holder = document.getElementById(divid);
    
    a.resulttype = 'text';
    a.sessionid = 'xx-xx';

    a.Send(function(result) {
        if (typeof f == 'undefined') f = null;
        if (f == null && size != null) f = size.form;
        if (f == null && color != null) f = color.form;
        if (f == null) f = document.forms[0];

        var count = parseInt(result)
        if (count == 0) {
            if (typeof outmsg == 'undefined') outmsg = 'Out of stock';
            if (holder == null) alert(outmsg);
            else holder.innerHTML = outmsg;

            f.onsubmit = function() {
                alert('This item is out of stock. Change your size and/or color.');
                return false;
            }
        }
        else {
            if (typeof inmsg == 'undefined') inmsg = '';

            if (holder == null) {
                if (inmsg.length > 0) alert(inmsg);
            }
            else holder.innerHTML = inmsg;

            f.onsubmit = function() {
                return true;
            }
        }
    });
}

