﻿/// <reference path="jquery-1.3.1-vsdoc.js" />
/**
* Copyright (c) 2010 Web Revolution s. r. o.
*/

PartnerOrder = {
    // Page init
    init: function() {
        PartnerOrder.recomputePrice();

        $(".toolTip").tooltip({
            delay: 0
        });
    },

    // Recompute total price
    recomputePrice: function() {
        var versionId = $("[name='productVersionId']:checked").val();

        if (versionId == null) {
            $("#totalPrice").html("Není vybrána verze");
            $("#totalMonthPrice").html("");
            return;
        }

        var licencePrice = $("#isLicenceChecked").is(":checked") ? parseFloat($("#lp_" + versionId).val()) : 0;
        var rentPrice = $("#isRentChecked").is(":checked") ? parseFloat($("#rp_" + versionId).val()) : 0;
        var webhostingPrice = $("#isWebhostingChecked").is(":checked") ? parseFloat($("#hp_" + versionId).val()) : 0;
        var totalPrice = 0;
        var totalMonthPrice = 0;

        $("input[name='featureChecked']:checked").each(function(i, elem) {
            var featureId = $(elem).val();
            var periodic = $("#featureCount_" + featureId).size() == 1 ? $("#featureCount_" + featureId).val() : 1;

            var fixElem = $("#fp_" + versionId + "_" + featureId);
            var perElem = $("#fp_per_" + versionId + "_" + featureId);
            var perNextElem = $("#fpn_" + versionId + "_" + featureId);

            if (fixElem.size() == 1) {
                if (perNextElem.size() == 1 && perNextElem.val() != "0") {
                    totalPrice += parseFloat(fixElem.val()) + parseFloat(perNextElem.val()) * (periodic - 1);
                }
                else {
                    totalPrice += parseFloat(fixElem.val()) * periodic;
                }
            }
            else if (perElem.size() == 1) {
                totalMonthPrice += parseFloat(perElem.val()) * periodic;
            }
        });

        $("input[id^='aofPrice_']").each(function(i, elem) {
            var price = parseInt($(elem).val().replace(" ", ""));

            if (!isNaN(price)) {
                totalPrice += price;
            }
        });

        totalPrice += licencePrice;
        totalMonthPrice += rentPrice + webhostingPrice;
        $("#totalPrice").html(Widgets.formatPrice(totalPrice));
        $("#totalMonthPrice").html(Widgets.formatPrice(totalMonthPrice));
    },

    // Add new order info
    addOrderInfo: function() {
        var newIndex = $("textarea[id^='orderInfo_']").size();
        var newRow = "<tr><td><label for=\"orderInfo_" + newIndex + "\">Požadavek:</label></td><td><textarea name=\"orderInfo[" + newIndex + "]\" id=\"orderInfo_" + newIndex + "\" rows=\"2\" cols=\"80\"></textarea></td></tr>";
        $(newRow).insertBefore("#orderInfoAddRow");
    },

    // Add new additional order feature
    addAdditionalOrderFeature: function() {
        var newIndex = $("input[id^='aofTitle_']").size();
        var newRow = "<tr><td><input id=\"aofTitle_" + newIndex + "\" name=\"aofTitle[" + newIndex + "]\" type=\"text\" value=\"\" style=\"width: 180px\" /></td>"
        + "<td><input id=\"aofDesc_" + newIndex + "\" name=\"aofDesc[" + newIndex + "]\" type=\"text\" value=\"\" style=\"width: 500px\" /></td>"
        + "<td><input id=\"aofPrice_" + newIndex + "\" name=\"aofPrice[" + newIndex + "]\" style=\"width: 90px\" onchange=\"PartnerOrder.recomputePrice()\" type=\"text\" value=\"\" /> Kč</td></tr>";
        $(newRow).insertBefore("#aofAddRow");
    }
};

var __priceFormat = "#,0 Kč";
