﻿function AddProduct(productID, product) {
    $('#hProductID').val(productID);
    $('#hProduct').val(product);
    $('#txtNumVarieties').val('1');
    $('#txtMiscInfo').val('');
    $('#dProd :checkbox').removeAttr('checked');
    if ($('#aCheckAll').text() == 'uncheck all') $('#aCheckAll').trigger('click');
    $('#btnAdd').val('Add Product');
    win1.setTitle('Add a New Product - ' + product);
    win1.setSize(550, 400);
    win1.screenCenter();
    win1.Open();
}
function EditProduct(productID) {
    var p = null;
    for (var i = 0; i < arrProducts.products.length; i++) {
        if (arrProducts.products[i].productID == productID) p = arrProducts.products[i];
    }
    if (!p) return;
    $('#btnAdd').val('Update Product');
    $('#hProductID').val(p.productID);
    $('#hProduct').val(p.product);
    $('#txtNumVarieties').val(p.NumVarieties);
    $('#txtMiscInfo').val(p.MiscInfo);
    $('#dProd :checkbox').removeAttr('checked');
    if ($('#aCheckAll').text() == 'uncheck all') $('#aCheckAll').trigger('click');
    if (p.Availability[0]) $('#chkJan').attr('checked', 'checked');
    if (p.Availability[1]) $('#chkFeb').attr('checked', 'checked');
    if (p.Availability[2]) $('#chkMar').attr('checked', 'checked');
    if (p.Availability[3]) $('#chkApr').attr('checked', 'checked');
    if (p.Availability[4]) $('#chkMay').attr('checked', 'checked');
    if (p.Availability[5]) $('#chkJun').attr('checked', 'checked');
    if (p.Availability[6]) $('#chkJul').attr('checked', 'checked');
    if (p.Availability[7]) $('#chkAug').attr('checked', 'checked');
    if (p.Availability[8]) $('#chkSep').attr('checked', 'checked');
    if (p.Availability[9]) $('#chkOct').attr('checked', 'checked');
    if (p.Availability[10]) $('#chkNov').attr('checked', 'checked');
    if (p.Availability[11]) $('#chkDec').attr('checked', 'checked');
    if (p.isOrganic) $('#chkOrganic').attr('checked', 'checked');
    win1.setTitle('Edit a Product - ' + p.product);
    win1.setSize(550, 400);
    win1.screenCenter();
    win1.Open();
}
function DeleteProduct(productID) {
    if (confirm('Are you sure that you want to remove this product from your product list?')) {
        var found = false;
        for (var i = 0; i < arrProducts.products.length; i++) {
            if (found) {
                arrProducts.products[i - 1] = arrProducts.products[i];
            } else if (arrProducts.products[i].productID == productID) {
                found = true;
            }
        }
        if (found) arrProducts.products.length--;
    }
    arrProducts.Draw();
}
function SaveProducts(ForSubmit) {
    //alert(arrProducts.products.length);
    if (ForSubmit && arrProducts.products.length == 0) {
        alert('You must select at least one product in order to submit your application.');
        return false;
    } else {
        if (arrProducts.products.length == 0) return true;
        return (arrProducts.Save());
    }
}
function Product() {
    this.productID = 0;
    this.product = '';
    this.NumVarieties = 0;
    this.isOrganic = false;
    this.MiscInfo = '';
    this.Availability = [false, false, false, false, false, false, false, false, false, false, false, false];

    this.Serialize = function() {
        var s = this.productID + '|' + this.product + '|' + this.NumVarieties + '|' + this.isOrganic + '|' + this.Availability + '|' + this.MiscInfo.replace('&','and');
        return s;
    };

    this.Draw = function() {
        var html = '<dt>';
        html += '<table><tr><td width="350">';
        html += '<strong>' + this.product + '</strong>, ' + ((this.NumVarieties == 1) ? '1 variety' : this.NumVarieties + ' varieties');
        if (this.isOrganic) html += ' (Organic)';
        html += '</td><td width="40">';
        html += '<a href="javascript:EditProduct(' + this.productID + ');"><img src="/images/edit2.png" alt="edit" title="Edit Product" width="16" height="16" /></a>';
        html += '&nbsp;&nbsp;';
        html += '<a href="javascript:DeleteProduct(' + this.productID + ');"><img src="/images/trash.png" alt="delete" title="Delete Product" width="16" height="16" /></a>';
        html += '</td></tr></table>';

        html += '</dt><dd>';
        var numMonths = 0;
        for (var i = 0; i < this.Availability.length; i++) {
            var month = '';
            switch (i) {
                case 0:
                    month = "Jan";
                    break;
                case 1:
                    month = "Feb";
                    break;
                case 2:
                    month = "Mar";
                    break;
                case 3:
                    month = "Apr";
                    break;
                case 4:
                    month = "May";
                    break;
                case 5:
                    month = "Jun";
                    break;
                case 6:
                    month = "Jul";
                    break;
                case 7:
                    month = "Aug";
                    break;
                case 8:
                    month = "Sep";
                    break;
                case 9:
                    month = "Oct";
                    break;
                case 10:
                    month = "Nov";
                    break;
                case 11:
                    month = "Dec";
                    break;
            }
            if (this.Availability[i]) {
                if (numMonths > 0) html += ', ';
                html += month;
                numMonths++;
            }
        }
        html += '</dd>';
        html += '<dd>' + this.MiscInfo + '</dd>';
        return html;
    };
}

function ProductsArray() {
    this.products = new Array();

    this.AddOrUpdateProduct = function() {
        var productID = parseInt($('#hProductID').val());
        var found = false;
        for (var i = 0; i < this.products.length; i++) {
            if (this.products[i].productID == productID) {
                this.UpdateProduct(this.products[i]);
                found = true;
            }
        }
        if (!found) this.AddProduct(productID);
        this.Draw();
    };

    this.AddProduct = function(productID) {
        var p = new Product();
        p.productID = productID;
        p.product = $('#hProduct').val();
        p.NumVarieties = $('#txtNumVarieties').val();
        p.isOrganic = $('#chkOrganic').attr('checked') ? true : false;
        p.MiscInfo = $('#txtMiscInfo').val();
        p.Availability[0] = $('#chkJan').attr('checked') ? true : false;
        p.Availability[1] = $('#chkFeb').attr('checked') ? true : false;
        p.Availability[2] = $('#chkMar').attr('checked') ? true : false;
        p.Availability[3] = $('#chkApr').attr('checked') ? true : false;
        p.Availability[4] = $('#chkMay').attr('checked') ? true : false;
        p.Availability[5] = $('#chkJun').attr('checked') ? true : false;
        p.Availability[6] = $('#chkJul').attr('checked') ? true : false;
        p.Availability[7] = $('#chkAug').attr('checked') ? true : false;
        p.Availability[8] = $('#chkSep').attr('checked') ? true : false;
        p.Availability[9] = $('#chkOct').attr('checked') ? true : false;
        p.Availability[10] = $('#chkNov').attr('checked') ? true : false;
        p.Availability[11] = $('#chkDec').attr('checked') ? true : false;
        this.products.push(p);
    };

    this.UpdateProduct = function(p) {
        p.NumVarieties = $('#txtNumVarieties').val();
        p.isOrganic = $('#chkOrganic').attr('checked') ? true : false;
        p.MiscInfo = $('#txtMiscInfo').val();
        p.Availability[0] = $('#chkJan').attr('checked') ? true : false;
        p.Availability[1] = $('#chkFeb').attr('checked') ? true : false;
        p.Availability[2] = $('#chkMar').attr('checked') ? true : false;
        p.Availability[3] = $('#chkApr').attr('checked') ? true : false;
        p.Availability[4] = $('#chkMay').attr('checked') ? true : false;
        p.Availability[5] = $('#chkJun').attr('checked') ? true : false;
        p.Availability[6] = $('#chkJul').attr('checked') ? true : false;
        p.Availability[7] = $('#chkAug').attr('checked') ? true : false;
        p.Availability[8] = $('#chkSep').attr('checked') ? true : false;
        p.Availability[9] = $('#chkOct').attr('checked') ? true : false;
        p.Availability[10] = $('#chkNov').attr('checked') ? true : false;
        p.Availability[11] = $('#chkDec').attr('checked') ? true : false;
    };

    this.Draw = function() {
        var html = '<dl>';
        for (var p in this.products) {
            html += this.products[p].Draw();
        }
        html += '</dl>';
        $('#dTheList').html(html);
    };

    this.Save = function() {
        var s = '';
        for (var p in this.products) {
            if (s.length > 0) s += '&';
            s += this.products[p].Serialize();
        }

        var ret = ob_post.post(null, 'SaveProducts', null, { s: s });
        if (ret == 'OK') return true;
        else {
            alert(ret);
            return false;
        }

    };
}

var arrProducts = new ProductsArray();