﻿var WebEngine = new Object();

WebEngine.url = '../calculator/booklet-calculator.aspx';
WebEngine.FedexObj;
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_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.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);
    
    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";
    }
}


WebEngine.shipping_calculate = function()
{   
    var success = WebEngine.check_zipcode();
    if($("serviceCode").value=="Custom Shipping quote required")
    {
        $("serviceCode").value="FEDEX_GROUND";
    } 
    if(success)
    {
        var weightValue = $F("printWeightValue");
        
        var serviceCodeValue = $F("serviceCode");
        
        var zipCodeValue = $F("zipCode");
        
        var cityValue = $F("city");
        
         //RuanYu.Utility.clear_select_items("serviceCode");
        //RuanYu.Utility.add_select_item("serviceCode","loading...","null"); 
       
        $("shippingSubtotalContainer").innerHTML = "Subtotal: $0.00";
           $("shippingSubtotalValue").value = "0.00";  
          
             $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
    
         $("calculateResultView").innerHTML = "$" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ; 
        new Ajax.Request("../calculator/FedexCalculator.aspx", { method:'post' ,parameters: {action: "shipping_calculate" ,weight:weightValue, serviceCode:serviceCodeValue , zipCode:zipCodeValue, city:cityValue }, onSuccess:WebEngine.shipping_calculate_callback, onFailure:WebEngine.onFailure });
          
    }
    else
    {
        $("shippingSubtotalContainer").innerHTML = "Subtotal: $0.00";
        
        $("shippingSubtotalValue").value = "0";
        
        $("calculateResult").innerHTML = "Total: $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;
        
        $("calculateResultView").innerHTML = " $" + RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value)) ;

    }
}
WebEngine.OnChangeserviceCode=function()
{
    WebEngine.shipping_calculate();
}
WebEngine.shipping_calculate_callback = function(transport)
{
    var result = transport.responseText.evalJSON();
   WebEngine.FedexObj=result; 
    //alert(result);
    if(result.message!="ok")
    {
//        $("shippingSubtotalWarning").className = "window-calculator-subtotal-warning";
//        $("shippingSubtotalWarning").innerHTML = "<div>" + result.message + "</div>";
//        RuanYu.Utility.clear_select_items("serviceCode");
//        RuanYu.Utility.add_select_item("serviceCode","not found","not found"); 
//        $("shippingSubtotalContainer").innerHTML = "Subtotal: $0.00";
//        $("shippingSubtotalValue").value = "0";
         $("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)) + "<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)) ;
    
    }
    else
    {
        $("shippingSubtotalWarning").className = "hidden";
        //shippingSubtotalValue
        //         RuanYu.Utility.clear_select_items("serviceCode");
        //        for(i=0;i<result.fedex.length;i++)
        //        { 
        //             RuanYu.Utility.add_select_item("serviceCode",result.fedex[i].servicetype,result.fedex[i].servicetype);
        //        }
        //        if( result.fedex.length>0)
        //        { 
        //            $("serviceCode").value=result.fedex[0].servicetype;
        //            $("shippingSubtotalContainer").innerHTML = "Subtotal: $" + RuanYu.Utility.numberRound2(result.fedex[0].Totalcharge);
        //           $("shippingSubtotalValue").value = RuanYu.Utility.numberRound2(result.fedex[0].Totalcharge); 
        //        } 
        //        else
        //       {
        //            RuanYu.Utility.add_select_item("serviceCode","not found","not found");
        //            $("shippingSubtotalContainer").innerHTML = "Subtotal: $0.00";
        //           $("shippingSubtotalValue").value = "0.00"; 
        //       } 
      
    $("shippingSubtotalContainer").innerHTML = "Subtotal: $"+result.value;
    $("shippingSubtotalValue").value = result.value; 
    //$("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.check_address = function()
{
    if($F("address") == "")
    {
        $("addressWarning").className = "window-calculator-warning";
        
        return false;
    }
    else
    {
        $("addressWarning").className = "hidden";
        
        return true;
    }
}

WebEngine.check_city = function()
{
    if($F("city") == "")
    {
        $("cityWarning").className = "window-calculator-warning";
        
        return false;
    }
    else
    {
        $("cityWarning").className = "hidden";
        
        return true;
    }
}

WebEngine.check_zipcode = function()
{
    var text = $F("zipCode");
    
    var re = /^\d{5}-\d{4}$|^\d{5}$/g; 
    
    if( text == "")
    {
        $("zipCodeWarning").innerHTML = "<div>Zip Code is required.</div>";
        
        $("zipCodeWarning").className = "window-calculator-warning";
        
        return false;
    }
    else
    {
        if(!text.match(re))
        {
            // -*- show error message -*-
            
            $("zipCodeWarning").innerHTML = "<div>Invalid zip code.</div>";
            
            $("zipCodeWarning").className = "window-calculator-warning";
            
            return false;
        }
    }
    
    $("zipCodeWarning").className = "hidden";
    
    return true;
}
function addcar_single(bpay, pidvalue, bproof, bcolor, bpapertype, bweight, bname, bsize, bquantity, bturnaround)
{
    if(Number(bpay))
    {
        var today = new Date();
        
        var expires = new Date();
        
        expires.setTime(today.getTime() + 1000*60*60*365); //  one year;
        new Ajax.Request("../calculator/addcar.aspx", { method:'post' ,parameters: {op: 'add',bpid:pidvalue, pay:bpay,other:bproof+"|Proof@"+bcolor+"|Color@"+bpapertype+"|PaperType@"+bweight+"|Weight@"+bname+"|ProductName@"+bsize+"|Size@"+bquantity+"|Quantity@"+bturnaround+"|Turnaround@"+ bpay}, onSuccess:WebEngine.addcar_callback, 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',bpid:$("bpid").value, pay:pay,other:$("proof").options[$("proof").selectedIndex].text+"|Proof@"+$("color").options[$("color").selectedIndex].text+"|Color@"+$("paperType").options[$("paperType").selectedIndex].text+"|PaperType@"+$("printWeightValue").value+"|Weight@"+$("productName").value+"|ProductName@"+$("size").options[$("size").selectedIndex].text+"|Size@"+$("quantity").options[$("quantity").selectedIndex].text+"|Quantity@"+$("turnaround").options[$("turnaround").selectedIndex].text+"|Turnaround@"+ RuanYu.Utility.numberRound2($("printSubtotalValue").value)}, onSuccess:WebEngine.addcar_callback, onFailure:WebEngine.onFailure });   
    }
}
WebEngine.checkoutCart=function()
{
       if($("txtshoppcount").value=="0")
       {
           alert("Your Shopping Cart is empty, please purchase some products to check out.");
          return; 
       }
      location.href="https://www.angelprint.com/member/check-out.aspx";
}
WebEngine.checkout_product = function()
{
 if(!WebEngine.check_zipcode()) return;
    
   
    var pay = $("calculateResultView").innerHTML.replace("$","");
    
    var zipCodeValue = $F("zipCode");
        
    var cityValue = $F("city");
    
    if(Number(pay))
    {
        //$("txtinfo").innerHTML="<font color=\"red\">adding to shopping car</font>";
       // $("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:$("proof").options[$("proof").selectedIndex].text+"|Proof@"+ RuanYu.Utility.numberRound2(Number($("printSubtotalValue").value) + Number($("shippingSubtotalValue").value))+"|PrintPrice@"+$("serviceCode").value+"|ShippingType@"+ $F("Zipcode")+"|ZipCode@"+$("shippingSubtotalValue").value+"|ShippingPrice"}, onSuccess:WebEngine.checkoutCar, 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',bpid:$("bpid").value, pay:pay,other:$("proof").options[$("proof").selectedIndex].text+"|Proof@"+ RuanYu.Utility.numberRound2($("printSubtotalValue").value)+"|PrintPrice@"+$("serviceCode").value+"|ShippingType@"+zipCodeValue+"|ZipCode@"+$("shippingSubtotalValue").value+"|ShippingPrice"}, onSuccess:WebEngine.addcar_callback, onFailure:WebEngine.onFailure });
       
    }
} **/
WebEngine.stoptwosec=function()
{
     $("txtinfo").innerHTML="";
     $("txtinfo").style.display="none"; 
}
WebEngine.addcar_callback=function(transport)
{
     location.href="../ShoppingCart.aspx";
     var result = transport.responseText;
     if(result)
    {
        WebEngine.SetShowShoppingcart();
    } 
   else
   {
      alert("add to shopping cart fail ");
      WebEngine.SetShowShoppingcart();
   } 
}
WebEngine.opencart=function()
{
       location.href="../shoppingcar/ShoppingCartDetails.aspx?"+new Date().getMilliseconds();
//         if(RuanYu.Utility.isIE())
//        { 
//               window.showModelessDialog("../shoppingcar/ShoppingCarPage.aspx?"+new Date().getMilliseconds(),window,"dialogWidth:550px;status:no;dialogHeight:400px;scrolling:no");
//         } 
//          else
//          {
//                window.open("../shoppingcar/ShoppingCarPage.aspx?"+new Date().getMilliseconds(),"me","left="+(screen.width - 550) / 2+",top="+(screen.height - 400) / 2+",width=550,height=400,scrollbars=yes,modal=yes");
//          } 
}
WebEngine.opencart2=function()
{

       location.href="shoppingcar/ShoppingCartDetails.aspx?"+new Date().getMilliseconds();
//         if(RuanYu.Utility.isIE())
//        { 
//               window.showModelessDialog("shoppingcar/ShoppingCarPage.aspx?"+new Date().getMilliseconds(),window,"dialogWidth:550px;status:no;dialogHeight:400px;scrolling:no");
//         } 
//          else
//          {
//                window.open("shoppingcar/ShoppingCarPage.aspx?"+new Date().getMilliseconds(),"me","left="+(screen.width - 550) / 2+",top="+(screen.height - 400) / 2+",width=550,height=400,modal=yes,scrollbars=yes");
//          } 
}
WebEngine.SetShowShoppingcart = function()
{
    new Ajax.Request("../shoppingcar/RightShoppingCart.aspx", { method:'post' ,parameters: {action: 'show',t:new Date().getMilliseconds()}, onSuccess:WebEngine.SetShowShoppingcart_callback, onFailure:WebEngine.onFailure });
}
WebEngine.SetShowShoppingcart_callback = function(transport)
{
    var result = transport.responseText;
    $("shoppingcartContainer").innerHTML=result;
    
}
WebEngine.deleteGoods = function(id)
{
    if(confirm("Are you sure you want to remove this product?"))
    { 
        new Ajax.Request("../shoppingcar/RightShoppingCart.aspx", { method:'post' ,parameters: {action: 'deleteGoods',jobno:id,t:new Date().getMilliseconds()}, onSuccess:WebEngine.deleteGoods_callback, onFailure:WebEngine.onFailure });
    } 
}
WebEngine.deleteGoods_callback=function(res)
{
     var result = res.responseText.evalJSON();
    if(result.value)
    {
       $("item"+result.jobid).style.display="none";
       $("totalconst").innerHTML=result.total;
       if(result.total.replace("Total Cost: $ ","")=="0")
       {
            $("txtshoppcount").value="0";
       }
    }  
   else
   {  
       alert("Delete fail");
   } 
}
WebEngine.MyAccount = function()
{
    new Ajax.Request("../checkLogin/login.aspx", { method:'post' ,parameters: {action: 'check',t:new Date().getMilliseconds()}, onSuccess:WebEngine.MyAccount_callback, onFailure:WebEngine.onFailure });
}
WebEngine.MyAccount_callback=function(res)
{
   if(res.responseText=="false")
       location.href="https://www.angelprint.com/member/login.aspx";
   else
      location.href="https://www.angelprint.com/member/default.aspx";
}
WebEngine.MyAccount2 = function()
{
    new Ajax.Request("../checkLogin/login.aspx", { method:'post' ,parameters: {action: 'check',t:new Date().getMilliseconds()}, onSuccess:WebEngine.MyAccount_callback2, onFailure:WebEngine.onFailure });
}
WebEngine.MyAccount_callback2=function(res)
{
   if(res.responseText=="false")
       location.href="https://www.angelprint.com/member/login.aspx";
   else
      location.href="https://www.angelprint.com/member/default.aspx";
}
WebEngine.setTopTab=function()
{
       $("cal_toptab").style.paddingTop=0;
       $("cal_toptab").style.marginLeft=0;
} 
WebEngine.onFailure = function()
{
    alert(transport.responseText);
}

