﻿WebEngine.url = '../san_diego_printing_specials/book_printing.aspx';

WebEngine.providerId = 1;

WebEngine.bind_pages = function()
{
    var sizeValue = $F("size");
    
    new Ajax.Request(WebEngine.url, { method:'post' ,parameters: {action: 'bind_pages',size:sizeValue}, 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_quantity();
    }
    catch(e)
    {
        alert(e.message);
        //alert(transport.responseText);
    }
}
WebEngine.bind_Proofs = function()
{
    var key=$("proof").value;
    if(key=="0" )
   { 
//        if(confirm("one party might, by means of a waiver, relinquish its right to pursue any further legal action once the settlement is finalized. A waiver carried out by an action, for example, might be based on whether a party in an agreement acts on a right, such as the right to terminate the deal in the first year of the contract. If it does not terminate the deal before the first year, that party waives its right to do so in the future."))
//        {
//            WebEngine.calculate();
//        } 
//        else
//       {
           //$("proof").value=25;
//       }  
   } 
   else
   {
      //WebEngine.calculate();
   }
   WebEngine.calculate();
}

WebEngine.bind_quantity = function()
{
    var sizeValue = $F("size");
    
    var pagesValue = $F("pages");
    
    new Ajax.Request(WebEngine.url, { method:'post' ,parameters: {action: 'bind_quantity',size:sizeValue, pages:pagesValue}, 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 += "<color>" + $F("color") + "</color>";
    outString += "<turnaround>" + $F("turnaround") + "</turnaround>";
    outString += "<paperType>" + $F("paperType") + "</paperType>";
    outString += "<proof>" + $F("proof") + "</proof>";
    outString += "<quantity>" + $F("quantity") + "</quantity>";
    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 });
}
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 = $("calculateResultView").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:"Book|Product Name@"+$("size").value+"|Size@"+$("paperType").value+"|Cover Paper Type@"+$("cpt").options[$("cpt").selectedIndex].text+"|Inside Paper Type@"+$("color").options[$("color").selectedIndex].text+"|Color@"+$("turnaround").options[$("turnaround").selectedIndex].text+"|Turnaround@"+$("proof").options[$("proof").selectedIndex].text+"|Proof@"+$("quantity").value+"|Quantity@"+ RuanYu.Utility.numberRound2($("printSubtotalValue").value)+"|PrintPrice@"+$("serviceCode").value+"|ShippingType@"+zipCodeValue+"|ZipCode@"+$("shippingSubtotalValue").value+"|ShippingPrice"}, 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 = RuanYu.Utility.numberRound2(result.totalPrice.replace("$","")) ;
           
           $("unitpriceContainer").innerHTML="Unit Price: "+result.eachPrice; 
           $("unitpriceValue").value=result.eachPrice;  
           
           $("calculateResultNoShipping").innerHTML = "Total:" + result.totalPrice ;
            $("calculateResultViewNoShipping").innerHTML = result.totalPrice ;
            
            //$("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)) ;
            
            $("printWeightContainer").innerHTML = "Weight:0lb";
            $("printWeightValue").value = "0";
           
    
//         $("shippingCalculator").className = "";
        
//        if($F("zipCode") == "" )    
//            $("zipCode").value = RuanYu.Cookies.prototype.get_cookie("zipcode");
        
//        if($F("city") == "")
//            $("city").value = RuanYu.Cookies.prototype.get_cookie("city");
    
       // if($F("zipCode") != "")
 //           WebEngine.shipping_calculate();
          WebEngine.get_weight();
        }
      }
    catch(e)
    {
        alert(e.message);
    }
}
WebEngine.shipping_calculate = function()
{
    var key=0;
    if($("serviceCode").value=="Custom Shipping quote required")
    {
        $("serviceCode").value="FEDEX_GROUND";
    } 
    if( $("quantity").value=="1500")
   { 
         
        //$("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)) ;
    
        if($("cpt").value=="95# Reincarnation Matte Cover(13pt) 100% recycled"|| $("cpt").value=="111# Primavera Gloss Cover 80% recycled")
        { 
            $("shippingSubtotalWarning").className = "hidden";
             key=RuanYu.Utility.numberRound2($("quantity").value*0.28);
            $("printSubtotalContainer").innerHTML = "Subtotal:" +RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value) + $("quantity").value*0.25);
            $("printSubtotalValue").value = RuanYu.Utility.numberRound2( parseFloat($("printSubtotalValue").value) + $("quantity").value*0.25);
           
           $("unitpriceContainer").innerHTML="Unit Price: "+RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value)/$("quantity").value); 
           $("unitpriceValue").value=RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value)/$("quantity").value); 
           
            //$("shippingSubtotalContainer").innerHTML = "Subtotal: $"+key;
           // $("shippingSubtotalValue").value = key; 
          $("shippingSubtotalWarning").className = "hidden";
          $("shippingSubtotalContainer").innerHTML = "Subtotal: $0.00";
          $("shippingSubtotalValue").value = "0"; 
          RuanYu.Utility.add_select_item("serviceCode","Custom Shipping quote required","Custom Shipping quote required"); 
          $("serviceCode").value="Custom Shipping quote required"; 
         
            $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value));
            $("calculateResultView").innerHTML = "$" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
        }  
       else
       {
            $("shippingSubtotalWarning").className = "hidden";
             //key=RuanYu.Utility.numberRound2($("quantity").value*0.28);
           // $("shippingSubtotalContainer").innerHTML = "Subtotal: $"+key;
           // $("shippingSubtotalValue").value = key; 
          $("shippingSubtotalWarning").className = "hidden";
          $("shippingSubtotalContainer").innerHTML = "Subtotal: $0.00";
          $("shippingSubtotalValue").value = "0"; 
          RuanYu.Utility.add_select_item("serviceCode","Custom Shipping quote required","Custom Shipping quote required"); 
          $("serviceCode").value="Custom Shipping quote required";  
            $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value));
            $("calculateResultView").innerHTML = "$" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
       }    
   }
   else
   { 
    if($F("size")=="3")
   { 
        if($("cpt").value=="95# Reincarnation Matte Cover(13pt) 100% recycled"|| $("cpt").value=="111# Primavera Gloss Cover 80% recycled")
        { 
            $("shippingSubtotalWarning").className = "hidden";
             key=RuanYu.Utility.numberRound2($("quantity").value*0.28);
            $("printSubtotalContainer").innerHTML = "Subtotal:" +RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value) + $("quantity").value*0.25);
            $("printSubtotalValue").value = RuanYu.Utility.numberRound2( parseFloat($("printSubtotalValue").value) + $("quantity").value*0.25);
           
           $("unitpriceContainer").innerHTML="Unit Price: "+RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value)/$("quantity").value); 
           $("unitpriceValue").value=RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value)/$("quantity").value); 
           
            $("shippingSubtotalContainer").innerHTML = "Subtotal: $"+key;
            $("shippingSubtotalValue").value = key; 
            $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value));
            $("calculateResultView").innerHTML = "$" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
        }  
       else
       {
            $("shippingSubtotalWarning").className = "hidden";
             key=RuanYu.Utility.numberRound2($("quantity").value*0.28);
            $("shippingSubtotalContainer").innerHTML = "Subtotal: $"+key;
            $("shippingSubtotalValue").value = key; 
            $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value));
            $("calculateResultView").innerHTML = "$" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
       }   
   } 
   else if($F("size")=="85")
   {
        if($("cpt").value=="95# Reincarnation Matte Cover(13pt) 100% recycled"|| $("cpt").value=="111# Primavera Gloss Cover 80% recycled")
        { 
            $("shippingSubtotalWarning").className = "hidden";
            key=RuanYu.Utility.numberRound2($("quantity").value*0.35);
              $("printSubtotalContainer").innerHTML = "Subtotal:" +RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value) + $("quantity").value*0.25);
            $("printSubtotalValue").value = RuanYu.Utility.numberRound2( parseFloat($("printSubtotalValue").value) + $("quantity").value*0.25);
           
           $("unitpriceContainer").innerHTML="Unit Price: "+RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value)/$("quantity").value); 
           $("unitpriceValue").value=RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value)/$("quantity").value); 
           
            $("shippingSubtotalContainer").innerHTML = "Subtotal: $"+key;
            $("shippingSubtotalValue").value = key; 
            $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value));
            $("calculateResultView").innerHTML = "$" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
        }  
       else
       {
            $("shippingSubtotalWarning").className = "hidden";
             key=RuanYu.Utility.numberRound2($("quantity").value*0.35);
            $("shippingSubtotalContainer").innerHTML = "Subtotal: $"+key;
            $("shippingSubtotalValue").value = key; 
            $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value));
            $("calculateResultView").innerHTML = "$" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
       }   
   }
   else if($F("size")=="5")
   {
        if($("cpt").value=="95# Reincarnation Matte Cover(13pt) 100% recycled"|| $("cpt").value=="111# Primavera Gloss Cover 80% recycled")
        { 
            $("shippingSubtotalWarning").className = "hidden";
             key=RuanYu.Utility.numberRound2($("quantity").value*0.45);
             
                $("printSubtotalContainer").innerHTML = "Subtotal:" +RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value) + $("quantity").value*0.25);
            $("printSubtotalValue").value = RuanYu.Utility.numberRound2( parseFloat($("printSubtotalValue").value) + $("quantity").value*0.25);
           
           $("unitpriceContainer").innerHTML="Unit Price: "+RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value)/$("quantity").value); 
           $("unitpriceValue").value=RuanYu.Utility.numberRound2(parseFloat($("printSubtotalValue").value)/$("quantity").value); 
           
            $("shippingSubtotalContainer").innerHTML = "Subtotal: $"+key;
            $("shippingSubtotalValue").value = key; 
            $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value));
            $("calculateResultView").innerHTML = "$" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
        }  
       else
       {
            $("shippingSubtotalWarning").className = "hidden";
             key=RuanYu.Utility.numberRound2($("quantity").value*0.45);
            $("shippingSubtotalContainer").innerHTML = "Subtotal: $"+key;
            $("shippingSubtotalValue").value = key; 
            $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value));
            $("calculateResultView").innerHTML = "$" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
       }   
   }
   }
}

    
WebEngine.addcar_product = function()
{
    //if(!(WebEngine.check_zipcode() && WebEngine.check_city())) return;
   
//    if(!WebEngine.check_zipcode()) 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',bpid:$F("bpid"), pay:pay,other:$("cpt").value+"|CoverPaperType@"+$("paperType").options[$("paperType").selectedIndex].text+"|PaperType@"+$("pages").options[$("pages").selectedIndex].text+"|Pages@"+$("proof").options[$("proof").selectedIndex].text+"|Proof@"+$("color").options[$("color").selectedIndex].text+"|Color@"+$("productName").value+"|ProductName@"+$("printWeightValue").value+"|Weight@"+$("size").options[$("size").selectedIndex].text+"|Size@"+$("quantity").options[$("quantity").selectedIndex].text+"|Quantity@"+$("turnaround").options[$("turnaround").selectedIndex].text+"|Turnaround@"+ RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value))}, onSuccess:WebEngine.addcar_callback, onFailure:WebEngine.onFailure });
       
    }
}
WebEngine.get_url = function(value)
{
    if(value == "one color")
        location.href = "manual-60lb-calculator.aspx";
}

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);
    
    if(Number(result) > 0)
    {
        $("shippingCalculator").className = "";
        
        if($F("zipCode") == "" )    
            $("zipCode").value = RuanYu.Cookies.prototype.get_cookie("zipcode");
        
        if($F("city") == "")
            $("city").value = RuanYu.Cookies.prototype.get_cookie("city");
    
        if($F("zipCode") != "")
            WebEngine.shipping_calculate();
        
    }
    else
    {
        $("shippingCalculator").className = "hidden";
    }
}

window.onload = function()
{
    try
    {
        $("size").onchange = WebEngine.bind_pages;
        $("pages").onchange = WebEngine.bind_quantity; 
        
        $("quantity").onchange = WebEngine.calculate; 
        
        $("color").onchange = WebEngine.calculate; 
        
        $("turnaround").onchange = WebEngine.calculate; 
        $("proof").onchange =WebEngine.bind_Proofs; 
        $("paperType").onchange = WebEngine.calculate; 
        WebEngine.SetShowShoppingcart();  
        WebEngine.bind_pages();
         OnLoadMenu(); 
    }
    catch(e)
    {
        alert(e.message);
    }
}
