$(document).ready(function(){
    $('#loadingContainer').ajaxStart(function(){
	    $(this).show();
	}).ajaxStop(function(){
	    $(this).hide();
	});
	
	setInterval(function(){
        $('div.sub_container').css('height',$(window.parent.frames.right).height()); //设置内容区域的高度
    },100);
	
	//--修改促销规则时，需要将相关的信息显示出来
	if(getQueryStringRegExp('promotionid').replace('#','') != '')
    {
        if(solution != '')
        {
            eval("var solutionjson="+solution);
            $('[@name=promotion]').each(function(){
                if($(this).val() == solutionjson.PromotionType)
                {
                    $(this).attr('checked','checked');
                }
            });
            shouldshow(solutionjson.PromotionType);
            switch(solutionjson.PromotionType)
            {
                case "1":
                    $.ajax({
                        type:"get",
                        dataType:"json",
                        url:'Promotion.aspx?NoCopyRight=1',
                        data:'type=ajax&Action=SearchPmtProduct&promotionid=' + getQueryStringRegExp('promotionid').replace('#',''),
                        cache:false,
                        async:false,
                        success:function(msg){
                            var data = msg.Product;
                            $.each(data,function(i,n)
                            {
                                var row = $('<tr><td class="delete ccenter c7"><img src="../images/x-gray.gif" alt="删除" /><input type="hidden" name="pid" value=' + n.ProductID + '><input type="hidden" name="pname" value=' + n.ProductName + '></td><td style="overflow-x:hidden;" class="c8">' + n.ProductName + '</td></tr>');
                                row.find('.delete').css('cursor','pointer').click(function(){
                                    $(this).parent().remove();
                                    $('#count').text(Number($('#count').text())-1);
                                    //--商品ID
                                    var ids = '';
                                    $('input[@name=pid]').each(function(){
                                        ids += $(this).val() + ',';
                                    });
                                    if(ids != ''){ids = ids.substring(0,ids.length-1);};
                                    $('#PIDs').val(ids);
                                    //--商品名称
                                    var pnames = '';
                                    $('input[@name=pname]').each(function(){
                                        pnames += $(this).val() + ',';
                                    });
                                    if(pnames != ''){pnames = pnames.substring(0,pnames.length-1);};
                                    $('#pnames').val(pnames);
                                    
                                }).find('img').mouseover(function(){
                                    $(this).attr('src','../images/x-red.gif');
                                }).mouseout(function(){
                                    $(this).attr('src','../images/x-gray.gif');
                                });
                                $('#listresult').append(row);
                                $('#PIDs').val($('#PIDs').val() + ($('#PIDs').val() != '' ? ',' : '') + n.ProductID);
                                $('#pnames').val($('#pnames').val() + ($('#pnames').val() != '' ? ',' : '') + n.ProductName);
                            });
                        },
                        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
                    });
                    $('#count').text($('input[@name=pid]').length);
                    $('#txtDiscount').val(solutionjson.Discount);
                    break
                case "2":
                    $.ajax({
                        type:"get",
                        dataType:"json",
                        url:'Promotion.aspx?NoCopyRight=1',
                        data:'type=ajax&Action=SearchPmtGenCoupons&promotionid=' + getQueryStringRegExp('promotionid').replace('#',''),
                        cache:false,
                        async:false,
                        success:function(msg){
							var data = msg.Coupons;
                            $.each(data,function(i,n)
                            {
                                var row = $('<tr><td class="delete ccenter c7"><img src="../images/x-gray.gif" alt="删除" /><input type="hidden" name="couponsID" value=' + n.CouponsID + '><input type="hidden" name="couponsName" value="' + n.CouponsName + '"></td><td style="overflow-x:hidden;" class="c8">' + n.CouponsName + '</td></tr>');
                                row.find('.delete').css('cursor','pointer').click(function(){
                                    $(this).parent().remove();
                                    $('#couponscount').text(Number($('#couponscount').text())-1);
                                    //--优惠券ID
									var cids = '';
									$('input[@name=couponsID]').each(function(){
										cids += $(this).val() + ',';
									});
									if(cids != ''){cids = cids.substring(0,cids.length-1);};
									$('#couponsIDs').val(cids);
									
									//--优惠券名称
									var cNames = '';
									$('input[@name=couponsName]').each(function(){
										cNames += $(this).val() + ',';
									});
									if(cNames != ''){cNames = cNames.substring(0,cNames.length-1);};
									$('#couponsNames').val(cNames);
                                }).find('img').mouseover(function(){
                                    $(this).attr('src','../images/x-red.gif');
                                }).mouseout(function(){
                                    $(this).attr('src','../images/x-gray.gif');
                                });
								$('#couponslistresult').append(row);
								$('#couponsIDs').val($('#couponsIDs').val() + ($('#couponsIDs').val() != '' ? ',' : '') + n.CouponsID);
                                $('#couponsNames').val($('#couponsNames').val() + ($('#couponsNames').val() != '' ? ',' : '') + n.CouponsName);
                            });
                        },
                        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
                    });
                    $('#couponscount').text($('input[@name=couponsID]').length);
                    break
                case "3":
                	$.ajax({
                        type:"get",
                        dataType:"json",
                        url:'Promotion.aspx?NoCopyRight=1',
                        data:'type=ajax&Action=SearchPromotionGift&giftids=' + solutionjson.Gift,
                        cache:false,
                        async:false,
                        success:function(msg){
							var data = msg.Gift;
                            $.each(data,function(i,n)
                            {
                                var row = $('<tr><td class="delete ccenter c7"><img src="../images/x-gray.gif" alt="删除" /><input type="hidden" name="giftID" value=' + n.GiftID + '><input type="hidden" name="giftName" value="' + n.GiftName + '"></td><td style="overflow-x:hidden;" class="c8">' + n.GiftName + '</td></tr>');
                                row.find('.delete').css('cursor','pointer').click(function(){
                                    $(this).parent().remove();
                                    $('#giftcount').text(Number($('#giftcount').text())-1);
                                    //--优惠券ID
									var cids = '';
									$('input[@name=giftID]').each(function(){
										cids += $(this).val() + ',';
									});
									if(cids != ''){cids = cids.substring(0,cids.length-1);};
									$('#giftIDs').val(cids);
									
									//--优惠券名称
									var cNames = '';
									$('input[@name=giftName]').each(function(){
										cNames += $(this).val() + ',';
									});
									if(cNames != ''){cNames = cNames.substring(0,cNames.length-1);};
									$('#giftNames').val(cNames);
                                }).find('img').mouseover(function(){
                                    $(this).attr('src','../images/x-red.gif');
                                }).mouseout(function(){
                                    $(this).attr('src','../images/x-gray.gif');
                                });
								$('#giftlistresult').append(row);
								$('#giftIDs').val($('#giftIDs').val() + ($('#giftIDs').val() != '' ? ',' : '') + n.GiftID);
                                $('#giftNames').val($('#giftNames').val() + ($('#giftNames').val() != '' ? ',' : '') + n.GiftName);
                            });
                        },
                        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
                    });
                    $('#giftcount').text($('input[@name=giftID]').length);
                    break
                case "4":
                	$.ajax({
                        type:"get",
                        dataType:"json",
                        url:'Promotion.aspx?NoCopyRight=1',
                        data:'type=ajax&Action=SearchPmtProduct&promotionid=' + getQueryStringRegExp('promotionid').replace('#',''),
                        cache:false,
                        async:false,
                        success:function(msg){
                            var data = msg.Product;
                            $.each(data,function(i,n)
                            {
                                var row = $('<tr><td class="delete ccenter c7"><img src="../images/x-gray.gif" alt="删除" /><input type="hidden" name="pid" value=' + n.ProductID + '><input type="hidden" name="pname" value=' + n.ProductName + '></td><td style="overflow-x:hidden;" class="c8">' + n.ProductName + '</td></tr>');
                                row.find('.delete').css('cursor','pointer').click(function(){
                                    $(this).parent().remove();
                                    $('#count').text(Number($('#count').text())-1);
                                    //--商品ID
                                    var ids = '';
                                    $('input[@name=pid]').each(function(){
                                        ids += $(this).val() + ',';
                                    });
                                    if(ids != ''){ids = ids.substring(0,ids.length-1);};
                                    $('#PIDs').val(ids);
                                    //--商品名称
                                    var pnames = '';
                                    $('input[@name=pname]').each(function(){
                                        pnames += $(this).val() + ',';
                                    });
                                    if(pnames != ''){pnames = pnames.substring(0,pnames.length-1);};
                                    $('#pnames').val(pnames);
                                    
                                }).find('img').mouseover(function(){
                                    $(this).attr('src','../images/x-red.gif');
                                }).mouseout(function(){
                                    $(this).attr('src','../images/x-gray.gif');
                                });
                                $('#listresult').append(row);
                                $('#PIDs').val($('#PIDs').val() + ($('#PIDs').val() != '' ? ',' : '') + n.ProductID);
                                $('#pnames').val($('#pnames').val() + ($('#pnames').val() != '' ? ',' : '') + n.ProductName);
                            });
                        },
                        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
                    });
                    $('#count').text($('input[@name=pid]').length);
                    break
                case "5":
                
                    break
                case "6":
                
                    break
                case "7":
                
                    break
            }
        }
	}
	else
	{
	    shouldshow("1");
	}
		
	
	//--根据不同的情况对不同的控件进行隐藏或显示
	$('input[@name=promotion]').click(function(){
	    shouldshow($(this).val());
	});
	
	//--上一步 下一步 取消
	$('#btnCancel').click(function(){location.href('PromotionActivity.aspx?Action=Manage');});
	$('#btnNext').click(function(){	
	    var step = $('#step').val();
	    var promotiontype = 1;
	    $('[@name=promotion]').each(function(i){
            if($(this).attr('checked') == true)
            {
                promotiontype = $(this).val();
            }
        });
	    if(step == '2' || step == '3'){ if(checkMeet(step,promotiontype) == false){ return false; }}
	    
	    $('#step').val(parseInt(step) + 1)
	    $('#step_' + step).hide();
	    $('#step_' + $('#step').val()).show();
	    $('#btnPrev').show();
	    if($('#step').val() == '4')
	    {
	        $('#btnNext').hide();
	        $('#btnSave').show();
	    }
	});
	
	$('#btnPrev').click(function(){
	    var step = $('#step').val();
	    $('#step').val(parseInt(step) - 1);
	    $('#step_' + step).hide();
	    $('#step_' + $('#step').val()).show();
	    $('#btnNext').show();
	    if($('#step').val() == '1')
	        $('#btnPrev').hide();
	    $('#btnSave').hide();
	});
	
	//--日历
	$("#txtTimeBegin").datepicker();
	$("#txtTimeEnd").datepicker();
	//弹出层
	$("#btnAdd").click(function(){
		$('#popupContact').popup({width:'80%',height:'80%',popupHead:'popupHead',btnClose:'popupContactClose'});
		if(status == 0){
			search_urldata = 'type=ajax&Action=SearchProductsAjax&keyword=' + escape(keyword) + '&CatID=' + catID + '&BrandIDs=' + brandID + '&SearchType=' + searchType + '&PageSize='+PageSize+'&Page='+CurrentPage;
			BindProducts('Promotion.aspx',search_urldata);
			status = 1;
		};
	});//如果是第一次弹出层,要获取商品列表,否则只需要显示层即可 
	
	if($.browser.mozilla){setInterval(setList,100);}
	
	var isIE6 = ($.browser.msie && $.browser.version=='6.0'); 
    setInterval(function(){
        if(isIE6){
            $('#datacontainer').css('height','390px');
        }else{
            $('#content').css('height',$('#popupContact').height()-$('#popupHead').height());
            $('#datacontainer').css('height',$('#content').height()-$('#pdt_listhead').height()-$('#pageDataList1_Pager').height()-$('#divsave').height()-40);
        };
    },1000);

    $('input[@name=chkAll]').click(function(){$('input[@name=chkItem]').attr('checked',$(this).attr('checked'));});//全选
	
    var showadv = 0;//高级搜索显示状态
	$('#showadvsearch').toggle(function(){
	    $('#advsearch').show();
	    $('#showadvsearch').text('简易搜索');
	    showadv = 1;
	},function(){
	    $('#advsearch').hide();
	    $('#sel_category').val('0')
        $('#sel_brand').val('0');
        $('#showadvsearch').text('高级搜索');
        showadv = 0;
	});
	
	$('#deleteall').click(function(){$('#listresult tr:gt(0)').remove();$('#PIDs').val('');$('#count').text('0');});//删除所有已选择的商品
    
    $('#sel_category option').eq(0).attr('selected','selected');
    $('#sel_category').change(function(){
        var categoryIDs = $(this).val();
        if(categoryIDs == null){return false;};
        $.ajax({
            url:'BindingAdd.aspx?type=ajax&Action=GetBrandByCatID&product_category_id='+categoryIDs + '&NoCopyRight=1',
            cache:false,
            success:function(data){
                if(!checkLoginStatus(data)){return false;}
                $('#sel_brand').html(data);
                $('#sel_brand option').eq(0).attr('selected','selected');
            },
            error:function(){alert('error');}
        });
    });
    
    //搜索
    $('#btnSearch').click(function(){
        CurrentPage = 1;
        keyword = $('#txtKeyword').val();
        catID = $('#sel_category').val();
        brandID = $('#sel_brand').val();
        if(catID == null){catID = '0'};
        if(brandID == null){brandID = '0';}
        searchType = $('#sel_searchType').val();
        search_urldata = 'type=ajax&Action=SearchProductsAjax&keyword=' + escape(keyword) + '&CatID=' + catID + '&BrandIDs=' + brandID + '&SearchType=' + searchType + '&PageSize='+PageSize+'&Page='+CurrentPage;
        BindProducts('Promotion.aspx',search_urldata);
        if(showadv == 1){
            $('#showadvsearch').click();
        }
    });
    
    $('#btnSubmit').click(function(){
        var productIDs = '';
        var pnames = '';
        var selectedPIDs = '';
        if($('#PIDs').val() != ''){
            selectedPIDs = ','+$('#PIDs').val()+',';
        };
        $('input[@name=chkItem][@checked]').each(function(){
            var currobj = $(this);
            if(selectedPIDs.indexOf(',' + currobj.val() + ',') == -1){
                var productname = currobj.parent().siblings('#Name').text();
                var row = $('<tr><td class="delete ccenter c7"><img src="../images/x-gray.gif" alt="删除" /><input type="hidden" name="pid" value=' + currobj.val() + '><input type="hidden" name="pname" value="' + productname + '"></td><td class="c8" style="overflow-x:hidden;">' + productname + '</td></tr>');
                row.find('.delete').css('cursor','pointer').click(function(){
                    $(this).parent().remove();
                    $('#count').text(Number($('#count').text())-1);
                    //--商品ID
                    var ids = '';
                    $('input[@name=pid]').each(function(){
                        ids += $(this).val() + ',';
                    });
                    if(ids != ''){ids = ids.substring(0,ids.length-1);};
                    $('#PIDs').val(ids);
                    
                    //--商品名称
                    var pnames = '';
                    $('input[@name=pname]').each(function(){
                        pnames += $(this).val() + ',';
                    });
                    if(pnames != ''){pnames = pnames.substring(0,pnames.length-1);};
                    $('#pnames').val(pnames);
                    
                }).find('img').mouseover(function(){
                    $(this).attr('src','../images/x-red.gif');
                }).mouseout(function(){
                    $(this).attr('src','../images/x-gray.gif');
                });
                $('#listresult').append(row);
                productIDs += currobj.val() + ',';
                pnames += productname.replace(',',' ') + ',';
            };
        });//end each
        if(productIDs != ""){
            productIDs = productIDs.substring(0,productIDs.length-1);
            pnames = pnames.substring(0,pnames.length-1);
            $('#PIDs').val($('#PIDs').val() + ($('#PIDs').val() != '' ? ',' : '') + productIDs);//隐藏控件记录已选择的商品ID(逗号分隔1,2,3)
            $('#pnames').val($('#pnames').val() + ($('#pnames').val() != '' ? ',' : '') + pnames);
        };
//        disablePopup();//关闭层
        $('#popupContactClose').click();
        $('#count').text($('input[@name=pid]').length);
    });
    
    //--清空
    $('#btnClear').click(function(){
        $('#count').text('0');
        $('tr:gt(0)','#listresult').remove();
        $('#PIDs').val('');
        $('#pnames').val('');
    });
    
	$('#btnSave').click(function(){
//		alert($('#giftIDs').val());
//		alert($('#giftNames').val());
//		return false;
	});
     
});

function getQueryStringRegExp(name)
{
    var reg = new RegExp("(^|\\?|&)" + name.toLowerCase() + "=([^&]*)(\\s|&|$)", "i");
    if (reg.test(location.href.toLowerCase()))
    return unescape(RegExp.$2.replace(/\+/g, " "));
    return "";
}
