﻿WebEngine.url = '../san_diego_printing_specials/Brochure_digital-printing.aspx';

WebEngine.providerId = 31;

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_turnaround();
    }
    catch(e)
    {
        alert(e.message);
        //alert(transport.responseText);
    }
}

WebEngine.bind_pages = function()
{
    var sizeValue = $F("size");
    
    var paperTypeValue = $F("paperType");
    
    var colorValue = $F("color");
    
    new Ajax.Request(WebEngine.url, { method:'post' ,parameters: {action: 'bind_pages',size:sizeValue, paperType:paperTypeValue, color:colorValue}, onSuccess:WebEngine.bind_pages_callback, onFailure:WebEngine.onFailure });

}

WebEngine.bind_pages_callback = function(transport)
{
    try
    {
        var result = transport.responseText.evalJSON();
        
        var list = result.pages;
        
        RuanYu.Utility.convert_select_items("pages",list);
        
        WebEngine.bind_turnaround();
    }
    catch(e)
    {
        alert(e.message);
        //alert(transport.responseText);
    }
}

WebEngine.bind_turnaround = function()
{
    var sizeValue = $F("size");
    
    var paperTypeValue = $F("paperType");
    
    var pagesValue = $F("pages");
     
    var colorValue = $F("color");
    
    new Ajax.Request(WebEngine.url, { method:'post' ,parameters: {action: 'bind_turnaround',size:sizeValue, paperType:paperTypeValue , color:colorValue, pages:pagesValue}, 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>" + $("quantity").value + "</quantity>";
    outString += "<color>" + $F("color") + "</color>";
    outString += "<turnaround>" + $F("turnaround") + "</turnaround>";
    outString += "<proof>" + $F("proof") + "</proof>";
    outString += "<paperType>" + $F("paperType") + "</paperType>";
   outString += "<folding>" + $F("folding") + "</folding>"; 
    outString += "</calculator>";
    
    quantityValue = $("quantity").options[$("quantity").selectedIndex].value;

    new Ajax.Request(WebEngine.url, { method:'post' ,parameters: {action: 'calculate',quantity:quantityValue, xml: outString}, onSuccess:WebEngine.onSuccess, onFailure:WebEngine.onFailure });
}
WebEngine.addcar_product = function()
{
    //if(!(WebEngine.check_zipcode() && WebEngine.check_city())) return;
//    if(!WebEngine.check_zipcode()) return;
    
//   if($("shippingSubtotalValue").value=="0.00")
//   {return;}
    var pay = $("calculateResultViewNoShipping").innerHTML.replace("$","");
    
//    var zipCodeValue = $F("zipCode");
        
//    var cityValue = $F("city");
    
    if(Number(pay))
    {
//        $("shoppingcartContainer").innerHTML="<div style=\"background-color:White\"><img src=\"../shoppingcar/images/52.gif\" /> Loading...</div>";
       // $("txtinfo").style.display="block";
        var today = new Date();
        
        var expires = new Date();
        
        expires.setTime(today.getTime() + 1000*60*60*365); //  one year;
        
//        RuanYu.Cookies.prototype.add_cookie("zipcode", zipCodeValue, expires);
        
//        RuanYu.Cookies.prototype.add_cookie("city", cityValue, expires);
        new Ajax.Request("../calculator/addcar.aspx", { method:'post' ,parameters: {op: 'add', pay:pay,other:"Brochure Digital|ProductName@"+$("size").options[$("size").selectedIndex].text+"|Size@"+$("paperType").options[$("paperType").selectedIndex].text+"|PaperType@"+$("color").options[$("color").selectedIndex].text+"|Color@"+$("printWeightValue").value+"|Weight@"+$("folding").options[$("folding").selectedIndex].text+"|Folding@"+$("turnaround").options[$("turnaround").selectedIndex].text+"|Turnaround@"+$("proof").options[$("proof").selectedIndex].text+"|Proof@"+$("quantity").value+"|Quantity@"+ RuanYu.Utility.numberRound2($("printSubtotalValue").value)}, onSuccess:WebEngine.addcar_callback, onFailure:WebEngine.onFailure });
        
    }
} 
WebEngine.onSuccess = function(transport, json)
{
    try
    {
        var result = transport.responseText.evalJSON();
        var bpid=result.bpid;
       if(bpid)
       { 
          $("bpid").value=result.bpid;
        }
         
        if(result.totalPrice == "$0.00")
        {   
  //          $("printSubtotalContainer").innerHTML = "Subtotal: $0.00";
            $("printSubtotalValue").value = "0" ;
            
            $("calculateResultNoShipping").innerHTML = "Total: $0.00" ;
            $("calculateResultViewNoShipping").innerHTML = "$0.00" ;
            
            $("printWeightContainer").innerHTML = "Weight:0lb";
            $("printWeightValue").value = "0";
            
           $("unitpriceContainer").innerHTML="Subtotal: $0.00"; 
           $("unitpriceValue").value="0";
           
            $("calculateResult").innerHTML = Resources.zero_show_message;
            $("calculateResultView").innerHTML = "" ;
            
            $("shippingCalculator").className = "hidden";
        }
        else
        {
            // result.eachPrice
           // alert(result.totalPrice);
 //           $("printSubtotalContainer").innerHTML = "Subtotal:" + result.totalPrice ;
            $("printSubtotalValue").value = result.totalPrice.replace("$","") ;
           
            $("unitpriceContainer").innerHTML="Unit Price: "+result.eachPrice; 
            $("unitpriceValue").value="0";  
            
            $("calculateResultNoShipping").innerHTML = "Total:" + result.totalPrice ;
            $("calculateResultViewNoShipping").innerHTML = result.totalPrice ;
            $("printSubtotalValue").value = result.totalPrice.replace("$","") ;
            
            //$("calculateResult").innerHTML = "Total:" + result.totalPrice + "<br /><span class='small-text gray-text'>(Each:" + result.eachPrice + ")</span>";
            //$("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) + "<br /><span class='small-text gray-text'>(Print:$" + $("printSubtotalValue").value + " | Shipping:$" + RuanYu.Utility.numberRound2($("shippingSubtotalValue").value) + ")</span>";
 //           $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value));
 //           $("calculateResultView").innerHTML = "$" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
            
            WebEngine.get_weight();
        }
    }
    catch(e)
    {
        alert(e.message);
    }
}

WebEngine.get_weight = function()
{
    var sizeValue = $F("size");
    
    var paperTypeValue = $F("paperType");
    
    var pagesValue = $F("pages");
    
    var quantityValue = $F("quantity");
    
    new Ajax.Request("../calculator/WeightCalculator.aspx", { method:'post' ,parameters: {calculatorId: WebEngine.providerId ,sizeId:sizeValue, paperTypeId:paperTypeValue , pageId:pagesValue, quantity:quantityValue }, onSuccess:WebEngine.get_weight_callback, onFailure:WebEngine.onFailure });
}

WebEngine.get_weight_callback = function(transport)
{
    var result = transport.responseText;
    
    $("printWeightContainer").innerHTML = "Weight:" + RuanYu.Utility.numberRound2(result) + "lb";
    $("printWeightValue").value = RuanYu.Utility.numberRound2(result);
}
window.onload = function()
{
    try
    {
        $("size").onchange = WebEngine.bind_paperType;
        $("paperType").onchange = WebEngine.bind_color; 
        $("turnaround").onchange = WebEngine.bind_quantity; 
        $("color").onchange= WebEngine.bind_turnaround; 
        $("quantity").onchange = WebEngine.calculate; 
        $("folding").onchange=WebEngine.calculate; 
        $("proof").onchange = WebEngine.calculate; 
        WebEngine.SetShowShoppingcart();  
        WebEngine.bind_paperType();
       WebEngine.setTopTab();
       OnLoadMenu(); 
    }
    catch(e)
    {
        alert(e.message);
    }
}
