window.addEvent('domready', function()
{
    init();
});

function init()
{
    //$("from_product").addEvent("change", fillToProducts());
    fillToProducts();
}

function _showUpdatePrice(price)
{
    var ups = $("updateform").getElements('.updateprice');
    ups.set('html', price);
}

function createAjaxLoader()
{
    var elLoader = new Element("img", {
        'class': 'ajaxloader',
        'alt': 'Loading...',
        'src': 'res/pics/ajax-loader2.gif',
        'width': '16',
        'height':'16',
        'border':'0'
    });
    //return elLoader;
    return "<img src=\"res/pics/ajax-loader2.gif\" alt=\"Loading...\" width=\"16\" height=\"16\" border=\"0\">";
}

function _setUpdatePrice(html)
{
    $("updateform").getElements('.updateprice').set('html', html);
}

function _setCouponCode(html)
{
    $("updateform").getElements('.couponcode').set('html', html);
}

function showUpdatePrice()
{
    var loader = createAjaxLoader();

    $("updateform").getElements('.couponcode').set('html', "");
    if ($('to_product').value == "no")
    {
        _setUpdatePrice("");
        $("step2").setStyle("visibility", "hidden");
        $("step3").setStyle("visibility", "hidden");
        return;
    }
    _setUpdatePrice(loader);

    var request = new Request.JSON(
    {
        'url': "purchase/ultramixer/update.php",
        'data': { 'cmd': 'getprice', 'fp':$('from_product').value,'tp':$('to_product').value },
        'method': 'get',
        'onComplete': function(result)
        {
            //alert("onComplete: " + result);
        },
        'onSuccess': function(responseJSON, responseText)
        {
            //alert("onSuccess: " + responseJSON);
            //alert("onSuccess: " + responseText);

            var ret = JSON.decode(responseText);
            //alert(ret.result);

            var ups = $("updateform").getElements('.updateprice');
            if (ret.result == "update")
            {
                _setUpdatePrice(ret.price);
                $("step2").setStyle("visibility", "visible");
                $("step3").setStyle("visibility", "hidden");
            }
            else if (ret.result = "noupdate")
            {
                showInfoBox($("to_product"), "test");
            }
            else
            {
                //alert("Fehler");
            }

        },
        'onFailure': function(error)
        {
            alert("Leider ist ein Fehler (" + error.statusText + ") aufgetreten. Bitte wenden Sie sich an unseren Support.");
        }
    });
    request.send();
}

function getLocale()
{
    return $("select_lang").getElements("select")[0].value;
}

function fillToProducts()
{
    var toProducts = $("to_product");
    var prevValue = toProducts.options[toProducts.selectedIndex].value;
    toProducts.selectedIndex = 0;
    _showUpdatePrice("");

    var request = new Request.JSON(
    {
        'url': "purchase/ultramixer/update.php",
        'data': { 'cmd': 'gettoproducts', 'fp':$('from_product').value,'locale':getLocale() },
        'method': 'get',
        'onSuccess': function(responseJSON, responseText)
        {
            //alert("onSuccess: " + responseJSON);
            //alert("onSuccess: " + responseText);

            var ret = JSON.decode(responseText);
            //var ret = JSON.decode("{items:[{value:'home',name:'UM Home'}]}");
            //alert(ret.result);

            toProducts.options.length = 0;
            for (var i = 0; i < ret.items.length; i++)
            {
                var item = ret.items[i];
                toProducts.options[i] = new Option(item.name, item.value);
                if (item.value == prevValue) toProducts.options[i].selected = true;
            }
            if (ret.items.length > 1)
            {
                //toProducts.selectedIndex = 1;
            }
            showUpdatePrice();
        },
        'onFailure': function(error)
        {
            alert("Leider ist ein Fehler (" + error.statusText + ") aufgetreten. Bitte wenden Sie sich an unseren Support.");
        }
    });
   request.send();
}

function getUpdatePrice()
{
    return $("updateform").getElement('.updateprice').value;
}
function getSelectedProduct()
{
    var s = $('to_product');
    return s.selectedIndex >= s.options.length ? "" : s.options[s.selectedIndex].text;
}

function getCouponCode()
{
    var nr = $('ordernumber').value;
    if (!nr)
    {
        alert("Bitte geben Sie Ihre Bestellnummer ein.");
        return false;
    }

    var loader = createAjaxLoader();
    _setCouponCode(loader);

    var request = new Request.JSON(
    {
        'url': "purchase/ultramixer/update.php",
        'data': { 'cmd': 'getcouponcode', 'ordernumber': nr, 'fp':$('from_product').value, 'tp': $('to_product').value, 'locale': getLocale() },
        'method': 'get',
        'onComplete': function(result)
        {
            //alert("onComplete: " + result);
        },
        'onSuccess': function(responseJSON, responseText)
        {
            //alert("onSuccess: " + responseJSON);
            //alert("onSuccess: " + responseText);

            var ret = JSON.decode(responseText);
            if (ret.result == "coupon")
            {
                _setCouponCode(ret.code);
                $("step3").getElements(".toproduct").set('html', getSelectedProduct());
                $("step3").setStyle("visibility", "visible");
            }
            else if (ret.result == "error")
            {
                _setCouponCode("");
                showError(ret.message.headline, ret.message.text);
            }
        },
        'onFailure': function(error)
        {
            _setCouponCode("");
            showError("Error", error.statusText);
        }
    }).send();
    return false;
}

function showError(headline, message)
{
    $("messagebox").getElements(".headline").set('html', headline);
    $("messagebox").getElements(".message").set('html', message);
    showInfoBox($("step2").getElements("button")[0], "messagebox");
}
