// JavaScript Document
var _agentPriceBack=function(ret){
			$("#addagentbutton").val("提交");
			$("#addagentbutton").prop("disabled",false); 	
	if(ret.indexOf("[ok]")!=-1||ret.indexOf("[err]")!=-1){
		alert(ret);
//top.location.href="updateMyPrice.asp";
		}else{
			alert(ret);
			//alert("未知错误，请重新提交");
			}
	}
var ii=0;var postStr="";var isRun=true;var idx="";
var addperform=function(){
	var iCount=$("#agentprice input[name='id']").size();
	var maxCount=(ii+1)*10;
	var isGet=true;
	if(maxCount>=iCount) {maxCount=iCount;isGet=false;}
	var doadd=parseInt($("#doadd").val())||"";
	if(doadd=="") return false;
	for(i=ii*10;i<maxCount;i++){
		var id=$("#agentprice input[name=id]:eq("+i+")").val();
		for(ji=1;ji<6;ji++){
		var agentPrice=parseInt($("#agentprice #agentPrice"+ji+"_"+id).val())||0;
		$("#agentprice #sellPrice"+ji+"_"+id).val(parseInt(agentPrice*(100+doadd)/100));
		}

		}
		if(isGet){
			ii++;
			setTimeout(function(){
					addperform();			
					},200)
			}else{
			alert("设置成功，请提交"+iCount+"|"+maxCount);				
				}
	}
var checkSubmit=function(){
	
	var iCount=$("#agentprice input[name='id']").size();
	var maxCount=(ii+1)*10;
	var isGet=true;
	if(maxCount>=iCount) {maxCount=iCount;isGet=false;}
	//alert(iCount);
	for(i=ii*10;i<maxCount;i++){
		var agentPriceArr=Array();sellPriceArr=Array();var isShowArr=Array();
		var id=$("#agentprice input[name=id]:eq("+i+")").val();
		for(ji=1;ji<6;ji++){
			agentPriceArr[ji]=parseInt($("#agentprice #agentPrice"+ji+"_"+id).val())||0;
			sellPriceArr[ji]=parseInt($("#agentprice #sellPrice"+ji+"_"+id).val())||0;
			if(agentPriceArr[ji]>0&&sellPriceArr[ji]<agentPriceArr[ji]){
				$("#agentprice #sellPrice"+ji+"_"+id).focus();
				alert("请输入价格，不能为空或者低于代理价");
			$("#addagentbutton").val("提交");
			$("#addagentbutton").prop("disabled",false); 				
				return false;
				
				}
			isShowArr[ji]=-1;
			if($("#agentprice #sellPrice"+ji+"_"+id).size()>0) {postStr+="&sellPrice"+ji+"_"+id+"="+sellPriceArr[ji];isShowArr[ji]=agentPriceArr[ji]-sellPriceArr[ji];}
			}
		idx+=","+id;
		
		if(isShowArr[1]>=0||isShowArr[2]>=0||isShowArr[3]>=0||isShowArr[4]>=0||isShowArr[5]>=0){
			isRun=false;
			$("div#agentPriceList"+id).addClass("s");
			$("div#agentPriceList"+id).removeClass("e");
			}else{
			$("div#agentPriceList"+id).addClass("e");
			$("div#agentPriceList"+id).removeClass("s");
			}
		}
		if(isGet){
			ii++;
			setTimeout(function(){
					checkSubmit();			
					},200)
			}else{
	if(!isRun){
		if( confirm("红色边框的代理价高于或等于销售价，是否依然提交？\n\n确定=继续\n取消=重新修改") )
			{
			$("#addagentbutton").val("提交中");
			postStr="idx=0"+idx+postStr;
			getPost("../api/llsapi.asp?apiAction=EditMyPrice",postStr,"_agentPriceBack");
			}
			else
			{
			$("#addagentbutton").val("提交");
			$("#addagentbutton").prop("disabled",false);  
			}
		}
		else
		{
		$("#addagentbutton").val("提交中");
		postStr="idx=0"+idx+postStr;
		getPost("../api/llsapi.asp?apiAction=EditMyPrice",postStr,"_agentPriceBack");
	}
				}
	}

$(function(){
		   
		$("input[name=addagentbutton]").prop("disabled",false);  
		$("#agentprice").unbind("submit");
		$("#agentprice").submit(function(){
			idx="";
			postStr="";
			isRun=true;
			ii=0;
			$("#addagentbutton").val("检查中，请稍候");
			$("#addagentbutton").prop("disabled",true);  
			checkSubmit();
					
										 return false;
										 })

		   })