﻿$(document).ready(function(){
    if($.browser.msie ||  $.browser.mozilla){
        
    }else{
        $("#CountryFlagTd").html("");
    }
});

function ProcessCalculateCost(){
    if($("#txtWeight").val() == ""){
        alert("Weight is required field!");
        $("#txtWeight").focus();
        return;
    }
    if(!$("#chkEMS").attr("checked") && !$("#chkDHL").attr("checked") && !$("#chkUPS").attr("checked") && !$("#chkHKSP").attr("checked")){
        alert("Please select shipping method!");
        return;
    }
    if($("#ddlCountry").val() == "select"){
        alert("Please select country!");
        return;
    }
    var Method = "";
    if($("#chkEMS").attr("checked")){
        Method += "EMS,"
    }
    if($("#chkDHL").attr("checked")){
        Method += "DHL,"
    }
    if($("#chkUPS").attr("checked")){
        Method += "UPS,"
    }
    if($("#chkHKSP").attr("checked")){
        Method += "HKSP,"
    }
    Method = Method.substr(0,Method.length-1);
    ShowBackground();
    DisableOtherSubmit();
    $.ajax({
        url: $("#HomeLink").val() + '/Ajax/ProcessCalculateCost.ashx',
        type: 'GET',
        dataType: 'xml',
        data: { Method: Method, Weight: escape($("#txtWeight").val()), Country: $("#ddlCountry").val(), time: (new Date()).getTime() },
        error: function(){
            location.href = $("#HomeLink").val() + "/Error/GenericErrorpage.aspx";
        },
        success: function(data){
            RemoveBackground();
            EnableOtherSubmit();
            if($(data).find("ErrorMessage").attr("message")){
                location.href = $("#HomeLink").val() + "/Error/GenericErrorpage.aspx";
            }else{
                var str = "<div style=\"text-align:center;color:red; margin:15px auto 15px auto; font-size:14px; font-weight:bold;\">Compare Result</div>\r\n<table class=\"ResultTable\">\r\n<tr>\r\n<td style=\"width:40%;font-weight:bold;\">Shipping Method</td>\r\n<td style=\"width:30%;font-weight:bold;\">Packages</td>\r\n<td style=\"width:30%;font-weight:bold;\">Result Cost </td>\r\n</tr>\r\n</table>\r\n";
                $(data).find("ShippingCostInfo").each(function(){
                    var Price = $(this).attr("Price");
                    var DiscountPrice = $(this).attr("DiscountPrice");
                    var Discount = $(this).attr("Discount");
                    var tempstr = "";
                    if(Price == "Contactus"){
                        tempstr = "<a href=\"mailto:sales@gbeads.cn\">Contact Us</a>";
                    }else{
                        if(Discount != "0")
                        {
                            tempstr = "<div style=\"color:#ccc; text-decoration:line-through;\">$"+Price+"</div>\r\n<div style=\"color:red;\">$"+DiscountPrice+"</div>"
                        }
                        else                        
                        {
                            tempstr = "<div style=\"color:red;\">$"+DiscountPrice+"</div>"
                        }
                        
                    }                    
                    str += "<table class=\"ResultTableItems\">\r\n<tr>\r\n<td style=\"width:40%;\">"+$(this).attr("Name")+"</td>\r\n<td style=\"width:30%;\">"+$(this).attr("Packages")+"</td>\r\n<td style=\"width:30%;\">"+tempstr+"</td>\r\n</tr>\r\n</table>\r\n"
                });
                $("#ResultDIV").html(str)
            }
        }
    });
}

function ChangeCountry(){
    var array = $("#ddlCountry").val().split("!");
    var CountryFlag = $("#CountryFlag");
    if($.browser.msie ||  $.browser.mozilla){
        CountryFlag.attr("src",$("#HomeLink").val() + "/Ajax/GetCountryFlag.ashx?code=" + array[2]);
        CountryFlag.attr("alt",array[1]);
    }
}