// JavaScript Document

function chg_prefix(){
	$("#client").val($("#tbClientID option:selected").attr("title"));
}

function get_products(){
	var pre = $("#tbClientID option:selected").attr("title");
	$.ajax({ url: 'project_create.php',
					  type: 'post',
					  data:{act:'get',
					  		   client:pre
					          },
					  dataType:'json',
					  success: function(data)
					  {
addOptions("#tbProducName",data,"tbModel","tbProductName",0);
					  }
				   });  	
}

function save_items(){
	var act='';
	var tbClientID = $("#tbClientID").val();
	var pre = $("#tbClientID option:selected").attr("title");
	var tbProductName = $("#tbProducName :selected").text().trim();
	var tbShippingQty = $("#tbShippingQty").val();
	var tbPN = $("#tbPN").val();
							  
	if(tbClientID=="")
	{
     alert("Client must be selected!");
	 project_create.tbClientID.focus();
	}else{
		if(tbProductName=="")
		{
		   alert("Product Name must be selected!");
	       project_create.tbProductName.focus();
		}else if(tbShippingQty==""){
		   alert("Qty must be filled!");
	       project_create.tbShippingQty.focus();
		}else{
		   (tbPN=='') ? act="save" : act="saveitem";
		
		 $.ajax({ url: 'project_create.php',
					  type: 'post',
					  data:{act:act,
							   tbClientID:tbClientID,
							   client:pre,
							   tbProductName:tbProductName,
							   inspect_year:$("#inspect_year :selected").val(),
                               inspect_month:$("#inspect_month :selected").val(),
                               inspect_day:$("#inspect_day :selected").val(),
							   tbModelNO:$("#tbModelNO").val(),
							   tbClientPONo:$("#tbClientPONo").val(),
							   tbShippingQty:tbShippingQty,
							   tbSampleSize:$("#tbSampleSize").val(),
							   tbSerialNO:$("#tbSerialNO").val(),
							   tbSpec:$("#tbSpec").val(),
							   tbPiece:$("#tbPiece").val(),
							   tbRemark:$("#tbRemark").val(),
							   tbPONumber:$("#tbPONumber").val(),
							   tbPN:$("#tbPN").val()
							  },
					  dataType:'json',
					  success: function(data)
					  {
							$("#tbPONumber").val(data[0]);
							$("#show_item").html(data[1]);
							cleanitem("#tbProducName","#tbModelNO","#tbClientPONo","#tbShippingQty","#tbSampleSize","#tbSerialNO","#tbSpec","#tbRemark");
							disable("#tbProducName","#tbModelNO","#tbClientPONo","#tbShippingQty","#tbSampleSize","#tbSerialNO","#tbSpec","#tbRemark");
							//$("#show_item").append(data[1]);cleanitem();disable();//累加条目
					  }
			 	  });  	
		}
    }
}
 
function fill_model()
 {
	$("#tbModelNO").val($("#tbProducName :selected").val());
 }
 
 //编辑BOOK产品信息
 function edititem(pre,po,id)
 {
   	enable("#tbProducName","#tbModelNO","#tbClientPONo","#tbShippingQty","#tbSampleSize","#tbSerialNO","#tbRemark");
	$.ajax({ url: 'project_create.php',
					  type: 'post',
					  data:{act:'getitem',
					  		   client:pre,
							   po:po,
							   id:id
							  },
					  dataType:'json',
					  success: function(data)
					  {
						  	//开始以为赋值给tbProducName并自动选取目标Option,但不成功是因为逻辑错了，要点击后才加载产品名,妈的搞了一天才想起来是这个问题
							$("#tbPN").val(data[0]);
							$("#tbPONumber").val(data[1]);
							$("#tbProducName").append("<option value='" + data[3] + "' selected='selected'>" + data[2] + "</option>"); 
							$("#tbModelNO").val(data[3]);
							$("#tbClientPONo").val(data[4]);
							$("#tbShippingQty").val(data[5]);
							$("#tbSampleSize").val(data[6]);
							$("#tbSerialNO").val(data[7]);
							$("#tbSpec").val(data[8]);
							$("#tbPiece").val(data[9]);
							$("#tbRemark").val(data[10]);
					   }
			 	  });  	
 }
 //删除BOOK产品信息
 function delitem(pre,po,id)
 {
	 $.ajax({ url: 'project_create.php',
					  type: 'post',
					  data:{act:'delitem',
					  		   client:pre,
					  		   po:po,
							   id:id
							  },
					  dataType:'json',
					  success: function(data)
					  {
							$("#tbPONumber").val(data[0]);
							$("#show_item").html(data[1]);
					   }
			 	  });  	
 }
  
 function additem()
 {
	  enable("#tbProducName","#tbModelNO","#tbClientPONo","#tbShippingQty","#tbSampleSize","#tbSerialNO","#tbSpec","#tbRemark");
	  cleanitem("#tbPN");
 }
  
function count_sample(qty)
{
	var sample = [2,3,5,8,13,20,32,50,80,125,200,315,500];
	//eval(level.join('+'));数组元素求和
	var sum = eval(qty.replace(/&/g,"+"));
	var get_sample = (sum<=8 && sample[0]) || 
					 (sum<=15 && sample[1]) || 
					 (sum<=25 && sample[2]) || 
					 (sum<=50 && sample[4]) ||
					 (sum<=90 && sample[5]) ||
					 (sum<=280 && sample[6]) ||
					 (sum<=500 && sample[7]) ||
					 (sum<=1200 && sample[8]) ||
					 (sum<=3200 && sample[9]) ||
					 (sum<=10000 && sample[10]) ||
					 (sum<=35000 && sample[11]) ||
					 (sum<=150000 && sample[12]) || 0; 
	document.getElementById("tbSampleSize").value = get_sample;
}