﻿
WebEngine.url = '../san_diego_printing_specials/business_card_digital_printing.aspx';

WebEngine.providerId = 25;

WebEngine.bind_paperType = function()
{
    var sizeValue = $F("size");

    new Ajax.Request(WebEngine.url, { method: 'post', parameters: { action: 'bind_paperType', size: sizeValue }, onSuccess: WebEngine.bind_paperType_callback, onFailure: WebEngine.onFailure });

}

WebEngine.bind_paperType_callback = function(transport)
{
    try
    {
        var result = transport.responseText.evalJSON();

        var list = result.paperType;

        RuanYu.Utility.convert_select_items("paperType", list);

        WebEngine.bind_color();
    }
    catch (e)
    {
        alert(e.message);
        //alert(transport.responseText);
    }
}

WebEngine.bind_color = function()
{
    var sizeValue = $F("size");

    var paperTypeValue = $F("paperType");

    new Ajax.Request(WebEngine.url, { method: 'post', parameters: { action: 'bind_color', size: sizeValue, paperType: paperTypeValue }, onSuccess: WebEngine.bind_color_callback, onFailure: WebEngine.onFailure });

}

WebEngine.bind_color_callback = function(transport)
{
    try
    {
        var result = transport.responseText.evalJSON();

        var list = result.color;

        RuanYu.Utility.convert_select_items("color", list);

        WebEngine.bind_quantity();
    }
    catch (e)
    {
        alert(e.message);
        //alert(transport.responseText);
    }
}

WebEngine.bind_turnaround = function()
{
    var sizeValue = $F("size");

    var paperTypeValue = $F("paperType");

    var colorValue = $F("color");

    new Ajax.Request(WebEngine.url, { method: 'post', parameters: { action: 'bind_turnaround', size: sizeValue, paperType: paperTypeValue, color: colorValue }, onSuccess: WebEngine.bind_turnaround_callback, onFailure: WebEngine.onFailure });

}

WebEngine.bind_turnaround_callback = function(transport)
{
    try
    {
        var result = transport.responseText.evalJSON();

        var list = result.turnaround;

        RuanYu.Utility.convert_select_items("turnaround", list);

        WebEngine.bind_quantity();
    }
    catch (e)
    {
        alert(e.message);
        alert(transport.responseText);
    }
}

WebEngine.bind_quantity = function()
{
    var sizeValue = $F("size");

    var paperTypeValue = $F("paperType");

    var colorValue = $F("color");

    var turnaroundValue = $F("turnaround");

    new Ajax.Request(WebEngine.url, { method: 'post', parameters: { action: 'bind_quantity', size: sizeValue, paperType: paperTypeValue, color: colorValue, turnaround: turnaroundValue }, onSuccess: WebEngine.bind_quantity_callback, onFailure: WebEngine.onFailure });

}

WebEngine.bind_quantity_callback = function(transport)
{
    try
    {
        var result = transport.responseText.evalJSON();

        var list = result.quantity;

        RuanYu.Utility.convert_select_items("quantity", list);

        WebEngine.calculate();
    }
    catch (e)
    {
        alert(e.message);
        //alert(transport.responseText);
    }
}

WebEngine.calculate = function()
{
    var outString = "";

    outString += "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
    outString += "<calculator>";
    outString += "<size>" + $F("size") + "</size>";
    outString += "<pages>" + $F("pages") + "</pages>";
    outString += "<quantity>" + $F("quantity") + "</quantity>";
    outString += "<color>" + $F("color") + "</color>";
    outString += "<turnaround>" + $F("turnaround") + "</turnaround>";
    outString += "<proof>" + $F("proof") + "</proof>";
    outString += "<paperType>" + $F("paperType") + "</paperType>";
    outString += "</calculator>";

    quantityValue = $("quantity").options[$("quantity").selectedIndex].text;

    new Ajax.Request(WebEngine.url, { method: 'post', parameters: { action: 'calculate', quantity: quantityValue, xml: outString }, onSuccess: WebEngine.onSuccess, onFailure: WebEngine.onFailure });
}

window.onload = function()
{
    try
    {
        $("size").onchange = WebEngine.bind_paperType;
        $("paperType").onchange = WebEngine.bind_color;
        $("color").onchange = WebEngine.calculate;
        $("turnaround").onchange = WebEngine.bind_quantity;
        $("quantity").onchange = WebEngine.calculate;
        $("proof").onchange = WebEngine.calculate;
        WebEngine.SetShowShoppingcart();
        WebEngine.bind_paperType();
        WebEngine.setTopTab();
        OnLoadMenu();
    }
    catch (e)
    {
        alert(e.message);
    }
}
