$(document).ready(function(){
    //loading
    $('#loadingContainer').ajaxStart(function(){
	    $(this).show();
	    $('#loadingbackground').show().css({"opacity": "0.5"});
	}).ajaxStop(function(){
	    $(this).hide();
	});
	$('.datepicker').datepicker();
	Start();
	
	$('#ddlCategory').change(function(){
		Start();
	});
	
	$('#btnSearch').click(function(){
		Start();
	});
	
});

function Start()
{
	var categoryTotal = SearchOrderAjax('CategoryStatistical.aspx','type=ajax&Action=SearchOrderAjax&from=' + $.trim($('#txtDateTimeFrom').val()) + '&to=' + $.trim($('#txtDateTimeTo').val()) + '&cid=' + $('#ddlCategory').val());
	$('#categoryTotal').html(categoryTotal);
	var categoryTopTotal = SearchOrderTopAjax('CategoryStatistical.aspx','type=ajax&Action=SearchOrderTopAjax&from=' + $.trim($('#txtDateTimeFrom').val()) + '&to=' + $.trim($('#txtDateTimeTo').val()) + '&cid=' + $('#ddlCategory').val());
	//$('#categoryTopTotal').html(categoryTopTotal);
}

function SearchOrderAjax(weburl,urldata){
	var shtml = '';
	$.ajax({
        type:"get",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
		async:false,
        success:function(msg){
			var mmsg = msg.split(',');
			shtml += '<td>' + mmsg[0] + '</td>';
			shtml += '<td>' + mmsg[1] + '</td>';
			shtml += '<td>' + mmsg[2] + '%</td>';
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
	return shtml;
}

function SearchOrderTopAjax(weburl,urldata){
	$.ajax({
        type:"get",
		dataType:"json",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
		async:false,
        success:function(msg){
			var data = msg.OrderTop;
			var row = '';
			$.each(data,function(i,n){
				row += '<tr><td align="center">' + (i + 1) + '</td>';
				row += '<td>' + n.ProductName + '</td>';
				row += '<td align="center">' + n.Hits + '</td>';
				row += '<td align="center">' + n.Quantity + '</td>';
				row += '<td align="center">' + n.SubPrice + '</td></tr>';
			});
			$('#categoryTopTotal').nextAll().remove();
			$('#categoryTopTotal').after(row);
			$('#categoryTopTotal').hide();
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
}