$(document).ready(function(){
	$('#btnPop').click(function(){
	    $('#popupContact').popup({width:'80%',height:'80%',popupHead:'popupHead',btnClose:'popupContactClose'});
	});
	
	$('#btnSelPdts').click(function(){
	    CreateList();
	    $('#specs').show();
	    $('#popupContactClose').click();
	});
	
	$('#Form1').submit(function(){
	    var pass = false;
	    $.each($('[id^=pro_table_]'),function(i,n){
	        var pid = $(this).attr('pid');
	        $.each($('.table_rec_'+pid),function(j,m){
	            var posIntReg = new RegExp(/^[1-9]*[1-9][0-9]*$/g);//正整数
	            pass = false;
	            var objname = $(this).find('[@name^=buyername_'+pid+'_]');
	            var objnum = $(this).find('[@name^=buynum_'+pid+'_]');
	            var objphone = $(this).find('[@name^=buyerphone_'+pid+'_]');
	            var objtime = $(this).find('[@name^=buytime_'+pid+'_]');
	            var objaddr = $(this).find('[@name^=buyeraddr_'+pid+'_]');
	            if($.trim(objname.val()) == ''){alert('购买人姓名必填！');objname.focus();return false;};
	            if($.trim(objnum.val()) == ''){alert('购买数量必填！');objnum.focus();return false;}else if(!posIntReg.test($.trim(objnum.val()))){alert('num:'+objnum.val());alert('购买数量有误！');objnum.focus();return false;};
	            if($.trim(objphone.val()) == ''){alert('购买人电话号码必填！');objphone.focus();return false;};
	            if($.trim(objtime.val()) == ''){alert('购买时间必填！');objtime.focus();return false;};
	            if($.trim(objaddr.val()) == ''){alert('购买地址必填！');objaddr.focus();return false;};
	            pass = true;
	            
	            objname.attr('name','buyername_' + pid + '_' + j);
	            objnum.attr('name','buynum_' + pid + '_' + j);
	            objphone.attr('name','buyerphone_' + pid + '_' + j);
	            objtime.attr('name','buytime_' + pid + '_' + j);
	            objaddr.attr('name','buyeraddr_' + pid + '_' + j);
	        });//end each
	        $('[@name=rec_num_' + pid + ']').val($('[@name^=buyername_'+pid+'_]').length);
	        return pass;
	    });//end each
	    return pass;
	});//end submit
});//end ready

function CreateList(){
    $('[@name=chkItem_search][@checked]').each(function(){
        var pid = $(this).val();
        var selectedIDs = ',' + $('#selectedPIDs').val() + ',';
        if(selectedIDs.indexOf(',' + pid + ',') == -1){
            var pno = $('#h_pno_'+pid).val();
            var pname = $('#h_pname_' + pid).val();
            CreateTable(pid,pno,pname);
        };//end if;
    });//end each
    CountPIDs();
};//end function

function CreateTable(pid,pno,pname,specdesc){
    var pro_table = $('#pro_temp').clone();
    pro_table.attr('id','pro_table_'+pid).attr('pid',pid);
    var cap = $('<caption style="text-align:left;white-space:nowrap;"><div style="float:left">商品名称：' + unescape(pname) + '<input type="hidden" name="pname_' + pid + '" value="'+ pname +'" /><br/>商品编码：' + pno + '</div><div style="float:right;margin-top:2px;cursor:pointer;"><img alt="删除" src="images/x-red.gif" /></div><input type="hidden" name="PID" value="' + pid + '" /></caption>');
    cap.find('img').click(function(){pro_table.remove();CountPIDs();});//删除
    pro_table.append(cap);
    
    var row_addrec = $("<tr class='row_addrec'></tr>");
    var td_addrec = $("<td></td>").append("<input type='hidden' name='rec_num_" + pid + "' />").appendTo(row_addrec);
    var btn_addrec = $("<input type='button' value='继续增加' class='inputbtn' />");
    btn_addrec.appendTo(td_addrec);
    row_addrec.appendTo(pro_table);
    $('#pro_list').append(pro_table);
    btn_addrec.click(function(){
        CreateRecoredTable(pid,row_addrec);
    }).click();
};

function CreateRecoredTable(pid,row_addrec){
    var rec_item = $("<tr class='row_rec'></tr>");
    var rec_item_td = $("<td></td>");
    var rec_table = $("<table class='sptable table_rec_" + pid + "'cellspacing='0' cellpadding='0' style='margin-top:5px;'></table>");
    $("<tr><th class='tl'>购买人：</th><td class='tr'><input type='text' name='buyername_" + pid + "_' /></td><th class='tl'>购买数量：</th><td class='tr'><input type='text' name='buynum_" + pid + "_' value='1' /></td></tr>").appendTo(rec_table);
    $("<tr><th class='tl'>电话号码：</th><td class='tr'><input type='text' name='buyerphone_" + pid + "_' /></td><th class='tl'>购买时间：</th><td class='tr'><input type='text' name='buytime_" + pid + "_' /></td></tr>").appendTo(rec_table);
    $("<tr><th class='tl'>地址：</th><td class='tr' colspan='2'><input type='text' name='buyeraddr_" + pid + "_' size='60' /></td><td class='tr'><div class='del_rec_" + pid + "' style='float:right; color:Red; top:0px;cursor:pointer;'>删除</div></td></tr>").appendTo(rec_table);
    rec_table.find('[@name^=buynum_'+pid+'_]').keyup(function(){
        $(this).val($(this).val().replace(/\D/g,""));
    });
    rec_table.find('[@name=buytime_'+pid+'_]').click(function(){setDayHM(this);$(this).blur()});
    rec_table.find('.del_rec_'+pid).click(function(){
        if($('[@name^=buyername_'+pid+'_]').length > 1){
            if(confirm("确定要删除此项记录吗？")){
                rec_item.remove();
            };
        }else{
            alert('已是最后一项！');
        };
    });
    rec_table.appendTo(rec_item_td);
    rec_item_td.appendTo(rec_item);
    row_addrec.before(rec_item);
    
}

//记录已选择的商品ID
function CountPIDs(){
    var selectedIDs = '';
    $('[@name=PID]').each(function(){
        selectedIDs += $(this).val() + ',';
    });
    if(selectedIDs != ''){
        selectedIDs = selectedIDs.substring(0,selectedIDs.length-1);
    };
    $('#selectedPIDs').val(selectedIDs);
    if(selectedIDs != ''){
        $('#save').show();
    }else{
        $('#save').hide();
    }
}