﻿function setDate() {
    var days = document.getElementById('day').value;
    document.getElementById('ddlDays').value = days;
    document.getElementById('ddlMonth').onchange = function() {
        var year = new Date().getUTCFullYear();
        var month = this.value;
        // 2月 天数
        var sumDaysOfMonth2 = 28;
        // 非2月天数
        var sumDays = 30;
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
            // 闰年
            sumDaysOfMonth2 = 29;
        }

        if (month == 4 || month == 6 || month == 9 || month == 11) {
            sumDays = 30;
        } else {
            sumDays = 31;
        }
        var days = document.getElementById('ddlDays');
        days.length = 0;
        if (month == 2) {
            for (i = 0; i < sumDaysOfMonth2; i++) {
                days.options.add(new Option(i + 1, i + 1));
            }
        } else {
            for (i = 0; i < sumDays; i++) {
                days.options.add(new Option(i + 1, i + 1));
            }
        }
        days.disabled = false;
    }

    document.getElementById('ddlDays').onchange = function() {
        document.getElementById('day').value = document.getElementById('ddlDays').value;
    }
}

function canSubmitForm() {
    if (jQuery('#ddlServer').val() == '-1') {
        alert('Please choose your server');
        return false;
    }
    return checkForm();
}

function tipShow() {
    if (document.getElementById('ckbTip').checked)
    { document.getElementById('tip').style.display = 'block'; }
    else
    { document.getElementById('tip').style.display = 'none'; }
}

var tblHead = '<table width="95%" border="0" cellpadding="0" cellspacing="0" class="table_pl" style=" margin-left:20px">\
                        <tr align="left" class="bule12">\
                            <th colspan="5"><b>Order info</b></th>\
                        </tr>\
                        <tr>\
                            <th width="42%">Product Name </th>\
                            <th width="22%"><b>Describe</b></th>\
                            <th width="13%">Price </th>\
                            <th width="10%">Day(s)</th>\
                            <td width="13%">&nbsp;</td>\
                        </tr>';
var tblFoot = '<tr>\
                        <td colspan="4" align="right"><div align="right"><b>Total</b></div></td>\
                        <td><span class="red">$ 0.00</span></td>\
                       </tr></table>';

function showCart() {
    jQuery('#div_waite').show();
    jQuery.ajax({
        url: '/powerleveling/plAjax.aspx',
        type: 'get',
        data: 'methodName=GetShoppingCartOrderInfo&rd=' + Math.random(),
        dataType: 'json',
        success: function(json) {
            initByJson(json);
        }
    });
}

function initByJson(json) {
    jQuery('#div_waite').hide();
    var totalPrice = 0.00;
    var totalDays = 0;
    if (json != '') {
        var tblBody = '';
        for (var i = 0; i < json.Products.length; i++) {
            var tdescription = '';
            if (json.Products[i].ProductDescribe != "" || json.Products[i].ProductDescribe.length != 0) {
                var tdescription = "<div onmouseover=\"displayToolTip('more" + i + "')\" onmouseout=\"hideToolTip('more" + i + "')\" style=\"float:left\">\
				<div id=\"more" + i + "\" class=\"s_box\"><p>" + json.Products[i].ProductDescribe + "</p></div><img src=\"/images/arrow_eee.gif\" class=\"float_l\" /></div>";
            }
            tblBody += '<tr>\
                                    <td>' + json.Products[i].ProductName + '&nbsp;</td>\
                                    <td>' + tdescription + '&nbsp;</td>\
                                    <td><span class="red">' + json.Products[i].Price + '&nbsp;</span></td>\
                                    <td>' + json.Products[i].TimeLimit + '&nbsp;</td>\
                                    <td align="center"><a href=\'javascript:removeOne("' + json.Products[i].Guid + '");\' title="Delete"><img src="/images/del.jpg" border="0" /></a>&nbsp;</td>\
                                </tr>';
       //     totalPrice += json[i].Price;
         //   totalPrice = (Math.round(totalPrice * 100) / 100);
            totalDays += parseInt(json.Products[i].TimeLimit);
        }
    } else {
        tblBody = '';
    }
totalPrice = json.TotalPrice;
totalPrice = (Math.round(totalPrice * 100) / 100);
    var vipdiscount = jQuery('#CtrolVipDiscount').val();
    if (vipdiscount != '0') {
        tblFoot = '<tr>\
                        <td colspan="4" align="right"><div align="right"><b>Total</b></div></td>\
                        <td><span class="red">$ ' + totalPrice + '&nbsp;</span></td>\
                       </tr>';
        tblFoot += '<tr>';
        //                        <td colspan="4" align="right"><div align="right"><b>VIP Discount:</b><span class="red">'+vipdiscount +'%</span></div></td>\
        //                        <td><span class="red">$ '+ Math.round((totalPrice *(1-vipdiscount/100))*Math.pow(10, 2))/Math.pow(10, 2);+'</span></td>\
        //                       </tr>';
    }
    else {
        tblFoot = '<tr>\
                        <td colspan="4" align="right"><div align="right"><b>Total</b></div></td>\
                        <td><span class="red">$ ' + totalPrice + '&nbsp;</span></td>\
                       </tr>';
    }
    tblFoot += '</table>';
    jQuery('#price').html(formatNum(totalPrice, 2));
    jQuery('#div_shoppingCart').hide();
    jQuery('#div_shoppingCart').html(tblHead + tblBody + tblFoot);
    jQuery('#div_shoppingCart').show(500);
}

function removeOne(guid) {
    jQuery('#div_waite').show();
    jQuery.ajax({
        url: '/powerleveling/plAjax.aspx',
        type: 'get',
        data: 'methodName=RemoveOneOrderInfo&guid=' + guid + '&rd=' + Math.random(),
        dataType: 'json',
        success: function(json) {
           
            if (json == null || json == '') {
                window.location = '/';
            }
            initByJson(json);
        }
    });
}

function formatNum(src, pos) {
    return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
}

function hideToolTip(id) {
    jQuery("#" + id).hide();
}

function displayToolTip(id) {
    jQuery("#" + id).show();
}
