﻿

function VariationProduct(_productDetailsId, _quantity) {

    this.productDetailsId = _productDetailsId;
    this.quantity = _quantity;
    this.parameters = new Object();
    this.key = "vcfp_" + this.productDetailsId;
    this.title = '';

    this.addValue = function(parameterId, valueId) {
        this.parameters[parameterId] = valueId;
    }

    this.clearValues = function() {
        this.parameters = new Object();
    }

    this.valuesToStr = function() {
        var res = '';
        for (parameter in this.parameters) {
            res += '_' + new String(parameter) + '|' + new String(this.parameters[parameter]);
        }
        if(res != '')
            res = res.substring(1, res.length);

        return res;
    }
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function VariationChooserForProducts() {

    this.products = new Array();
    this.controlId = randomString();
    this.formId = "variationChooserForm_" + this.controlId;
    this.chooserDivId = "variationChooserDiv_" + this.controlId;
    this.htmlContainer = "";
    this.renderStyle = "ManyProducts";
    this.onChange = function() { };

    this.addProduct = function(productDetailsId, quantity, title) {
        var product = new VariationProduct(productDetailsId, quantity);
        if(title != null && title != undefined) product.title = title;
        this.products[this.products.length] = product;
    };

    this.getProducts = function() {
        this.fillProducts();
        return this.products;
    }

    this.getAvailableQuantityByKey = function(key) {
    return $('#' + key + '_varparam_available_quantity', '#' + this.formId).val();
    }

    this.getAvailableQuantity = function() {
        return this.getAvailableQuantityByKey(this.getProducts()[0].key);
    }

    this.getPriceDifferenceAmountByKey = function(key) {
    return $('#' + key + '_varparam_price_diff_amount', '#' + this.formId).val();
    }

    this.getPriceDifferenceAmount = function() {
        return this.getPriceDifferenceAmountByKey(this.getProducts()[0].key);
    }

    this.getPriceDifferenceCurrencyByKey = function(key) {
        return $('#' + key + '_varparam_price_diff_currency', '#' + this.formId).val();
    }

    this.getPriceDifferenceCurrency = function() {
        return this.getPriceDifferenceCurrencyByKey(this.getProducts()[0].key);
    }

    this.isAllSelected = function() {
        this.fillProducts();
        for (var i = 0; i < this.products.length; i++) {
            var product = this.products[i];
            for (parameter in product.parameters) {
                if (parseInt(product.parameters[parameter]) == 0)
                    return false;
            }
        }

        return true;
    } 

    this.fillProducts = function() {
        for (var i = 0; i < this.products.length; i++) {
            var product = this.products[i];
            var dropDowns = $('*[id^=' + product.key + '_var_]', '#' + this.formId);

            product.clearValues();

            for(var j = 0; j < dropDowns.length; j++)
            {
                var dropDown = dropDowns[j];
                var ssid = dropDown.id.split('_var_');
                var variationParameterId = parseInt(ssid[1]);
                var valueId = parseInt(dropDown.value);
                
                product.addValue(variationParameterId, valueId);
            }
        }
    }

    this.render = function() {
        if (this.htmlContainer == "") {
            alert("Specify html container for variation chooser!");
            return;
        }

        var vcfp_keys = "";
        var quantity_inputs = "";
        var title_inputs = "";
        for (var i = 0; i < this.products.length; i++) {
            if (i > 0) vcfp_keys += "|";
            vcfp_keys += this.products[i].key;
            quantity_inputs += "<input type='hidden' name='" + this.products[i].key + "_quantity' value='" + this.products[i].quantity + "' />";

            if (this.products[i].title != '')
                title_inputs += "<input type='hidden' name='" + this.products[i].key + "_title' value='" + this.products[i].title + "' />";
        }


        var chooserHtml =
            "<div class='vcfp_controls_container' id='" + this.formId + "' action='/Storefront/VariationChooserForProducts.ashx' method='post' type='ajax' replaceElement='" + this.chooserDivId + "' >" +
               "<input type='hidden' name='vcfp_render_style' value='" + this.renderStyle + "' />" +
               "<input type='hidden' name='vcfp_keys' value='" + vcfp_keys + "' />" +
               "<input type='hidden' name='form_id' value='" + this.formId + "' />" +
               quantity_inputs + title_inputs +
               "<fieldset id='" + this.chooserDivId + "' >" +
               "   Content is loading now. Please wait..." +
               "</fieldset>" +
               "</form>";
        var htmlContainer = this.htmlContainer;
        var formId = this.formId;
        var onChange = this.onChange;
        //   alert(chooserHtml);
        $(document).ready(function() { $('#' + htmlContainer).html(chooserHtml); $('#' + formId).data("onLoadSuccess", onChange); AjaxForm('#' + formId); });
    }
}