
var pnewsId;//news的id值
var inputId;//全局节点id
var thread;//用于停止setInterval
var pasttime;//判断拍卖是否过期

/**
	初始化
1、绑定显示价格按钮。showpriceimg
2、得到newsid号
3、newsid赋值给全局变量inputId（或许是问题所在）
4、停止向后台发送数据
5、如果节点属性是显示并且拍卖没有过期，向后台发送数据linkUrl

6、绑定shownote节点，停止向后台发送数据
7、绑定p_upinfo收起节点，停止向后台发送数据
*/
$(document).ready(function(){
 var i = 1
 while($("#showpriceimg-"+i).length > 0){
 
 	  $("#showpriceimg-"+i).click(function(){
 	  
	  var newsid = $(this).attr("id");//计数法0
	  newsid =newsid.split('-');//计数法-1
	  newsid =newsid[1];//计数法-2
	  inputId =newsid;//全局节点id  0
	  newsid = $("#newsid"+newsid).attr("value");//计数法-3
	  pnewsId = newsid;//计数法0
	  clearInterval(thread);
	  var time = $("#time"+inputId).val();
	  pasttime =time.charAt(0)=='-';//拍卖过期
	  ListenerPrice(newsid);
	  //闭包
	  function getNewsid(){
	    var newsidclosure = newsid;
	     function returnNewsid(){
	     	return newsidclosure;
	     }
	      return returnNewsid;
	  }
	  if($("#info"+inputId).is(":visible")&&(!pasttime)){
		  thread = window.setInterval(function(){
		  //如果拍卖结束，停止异步,这里不能精确判断前台的倒计时件事，所以要多读一次
		  if($("#info"+inputId).is(":hidden") || pasttime){
		  	clearInterval(thread);	
		  }
		  var tempnewsid = getNewsid();
		  ListenerPrice(tempnewsid);
		  },15000);//设置刷新时间
	  }
 });
	  
	  
	  i++;
 }
 

}); //end ready


//把拍卖提交的方法移到这里，为了拍卖成功之后马上刷新新的数据，不再等待10秒
/*
1、note得到form的id
2、formNote得到form的obj
3、obj得到我的出价的input
4、判断是否登陆
5、price我的出价
6、currPrice当前出价
7、向后台发送数据
8、得到返回数据之后调用linkUrl
*/
function checkAuctionTop(id){
	var note ="userAuctionTop"+id; //form id
	var formNote =document.getElementById(note);//form obj
	var obj = formNote.myPayTop; //my price input
	var loginMark = formNote.isLogin;
	if(loginMark.value == "false"){
		alert("对不起！请您先登录系统才可以进行拍卖.");
		return false;
	}
	var price =obj.value;//my price value
	if(price == ""){
		alert("出价不能为空，请以整数填写。(例:100，500)");
		obj.focus();
		return false;
	}
	if(checkNumber(obj.value)){
		alert("出价只能为数字类型，请输入数字(例:100，500)");
		obj.focus();
		return false;
	}
	var currPrice = parseInt(formNote.currPric.value);//当前出价
	if(parseInt(price)<= currPrice){
		alert("对不起！您的出价不能低于或等于当前价，请重新填写.");
		obj.value = "";
		return false;
	}
	var hiddenPrice = formNote.myPric.value;
	
	var saleid =formNote.saleid.value;
	Dialog.confirm('警告：您确定要出价吗？',function(){
	Dialog.alertNoneButton("系统正在处理，请稍候...");
		//formNote.action="/bespa/BusinessManager.shtml?action=auction";
		$.get("/bespa/BusinessManager.shtml",{action:"auction",myPayTop:price,myPric:hiddenPrice,saleid:saleid},function(data){
		Dialog.close();
		var tempLink = linkUrl();
		alert(data);
		});
	});
}

//lang =7
function checkAuctionTopEn(id){
	var note ="userAuctionTop"+id
	var formNote =document.getElementById(note);
	var obj = formNote.myPayTop;
	var loginMark = formNote.isLogin;
	//var code = document.userAuctionTop.myPayTopCode;
	if(loginMark.value == "false"){
		alert("Sorry! Please first sign-on system can be auctioned.");
		return false;
	}
	var price =obj.value;
	if(price == ""){
		alert("Bidding can not be empty, Please fill in.");
		obj.focus();
		return false;
	}
	if(checkNumber(obj.value)){
		alert("Bidding can only numeric type, enter the number.");
		obj.focus();
		return false;
	}
	var currPrice = parseInt(formNote.currPric.value);
	
	if(parseInt(price)<=currPrice){
		alert("Sorry! Your bid can not be less than or equal to the current prices, please re-fill.");
		obj.value = "";
		return false;
	}
	var hiddenPrice = formNote.myPric.value;
	//var myPayBottom = formNote.myPayBottom.value;//
	var saleid =formNote.saleid.value;
	Dialog.confirm('Are you sure want to bid ?',function(){
	Dialog.alertNoneButton(" The system is processing, please wait a minute...");
		$.get("/bespa/BusinessManager.shtml",{action:"auction",myPayTop:price,myPric:hiddenPrice,saleid:saleid},function(data){
		Dialog.close();
		alert(data);
		var tempLink = linkUrl();
		});
	});
}

//lang =8
function checkAuctionTopNl(id){
	var note ="userAuctionTop"+id;
	var formNote =document.getElementById(note);
	var obj = formNote.myPayTop; 
	var loginMark = formNote.isLogin;
	//var code = document.userAuctionTop.myPayTopCode;
	if(loginMark.value == "false"){
		alert("Ledsen! Du forst logga in pa systemet kan auktioneras.");
		return false;
	}
	var price =obj.value;
	if(price == ""){
		alert("Erbjudandet kan inte vara tom, fyll i ett heltal.");
		obj.focus();
		return false;
	}
	if(checkNumber(obj.value)){
		alert("Budgivning kan endast numeriska.");
		obj.focus();
		return false;
	}
	var currPrice = parseInt(formNote.currPric.value);
	if(parseInt(price)<=currPrice){
		alert("Ledsen! Budet kan inte vara mindre an eller lika med lopande priser, vanligen pafyllning.");
		obj.value = "";
		return false;
	}
	var hiddenPrice = formNote.myPric.value;
	//var myPayBottom = formNote.myPayBottom.value;
	var saleid =formNote.saleid.value;
	Dialog.confirm('Weet u zeker dat u wilt bieden ?',function(){
	Dialog.alertNoneButton("Het systeem is verwerkt, even geduld aub...");
		$.get("/bespa/BusinessManager.shtml",{action:"auction",myPayTop:price,myPric:hiddenPrice,saleid:saleid},function(data){
			Dialog.close();
			alert(data);
			var tempLink = linkUrl();
		});
	});
}

function checkAuctionTopTw(id){
	var note ="userAuctionTop"+id; //form id
	var formNote =document.getElementById(note);//form obj
	var obj = formNote.myPayTop; //my price input
	var loginMark = formNote.isLogin;
	if(loginMark.value == "false"){
		alert("對不起！請您先登錄系統再進行竟拍.");
		return false;
	}
	var price =obj.value;//my price value
	if(price == ""){
		alert("出價不能為空，請以整數填寫。(例:100，500)");
		obj.focus();
		return false;
	}
	if(checkNumber(obj.value)){
		alert("出價只能為數字類型，請輸入數字(例:100，500)");
		obj.focus();
		return false;
	}
	var currPrice = parseInt(formNote.currPric.value)
	if(parseInt(price)<=currPrice){
		alert("對不起！您的出價不能低于或等于當前價，請重新填寫.");
		obj.value = "";
		return false;
	}
	var hiddenPrice = formNote.myPric.value;
	//var myPayBottom = formNote.myPayBottom.value;//
	var saleid =formNote.saleid.value;
	Dialog.confirm('警告：您確定要出價嗎？',function(){
	Dialog.alertNoneButton("系統正在處理，請稍候...");
		$.get("/bespa/BusinessManager.shtml?action=auction",{myPayTop:price,myPric:hiddenPrice,saleid:saleid},function(data){
			Dialog.close();
			alert(data);
			var tempLink = linkUrl();
		});
	});

}

function linkUrl()
{
 $.post("PriceAndIpManager.shtml",{action:"ShowPrices",newsid:pnewsId},function(data){
 //alert(data);
 var tempdata = data.split("|");
 var price = tempdata[0];
 var myprice = tempdata[1];
 var time = tempdata[2];
 var user = tempdata[3];
 //alert(price);
 var ip = tempdata[4];
  //alert(ip);
 $("#time"+inputId).val(time);
 $("#nowprice"+pnewsId).empty();
 $("#nowprice"+pnewsId).append(price);
 $("#myprice"+pnewsId).val(myprice);
 $("#tbid"+pnewsId).nextAll().remove();
 $("#tbid"+pnewsId).after(ip);
 $("#higherPrice"+inputId).empty();
 $("#higherPrice"+inputId).append(subPrice(price));
 $("#priceForuser"+inputId).empty();
 $("#priceForuser"+inputId).append(user);
 });

}

//取得最高价格，我应该的出价，开始和结束时间差，最高出价者、出价者列表
function ListenerPrice(newsid){
	if(!newsid){
	 return;
	}
	 $.post("PriceAndIpManager.shtml",{action:"ShowPrices",newsid:newsid},function(data){
	 var tempdata = data.split("|");
	 var price = tempdata[0];
	 var myprice = tempdata[1];
	 var time = tempdata[2];
	 var user = tempdata[3];
	 var ip = tempdata[4];
	 pasttime =time.charAt(0)=='-';//拍卖过期
	 $("#time"+inputId).val(time);
	 $("#nowprice"+pnewsId).empty();
	 $("#nowprice"+pnewsId).append(price);
	 $("#myprice"+pnewsId).val(myprice);
	 $("#tbid"+pnewsId).nextAll().remove();
	 $("#tbid"+pnewsId).after(ip);
	 if(subPrice(price) != 50){
 	 $("#higherPrice"+inputId).empty();
	 $("#higherPrice"+inputId).append(subPrice(price)); 
 	}
	 $("#priceForuser"+inputId).empty();
	 $("#priceForuser"+inputId).append(user);
	 });

}

//得到价格单位前面的数值
function subPrice(soure){
var length = soure.length;
var returnVal ="";
	for(var i = 0 ; i< length ; i++){
	   if(isNaN(parseInt(soure.charAt(i)))){
	    return returnVal;
      }else{
        returnVal = returnVal +soure.charAt(i)
      }
	}
}

//错误提示
/*
onerror = handleErr;
function handleErr(msg,url,l)
{ 
var txt="糟糕哦，出错了。联系管理员\n\n" ;
txt+="Error: " + msg + "\n";
txt+="URL: " + url + "\n" ;
txt+="Line: " + l + "\n\n";
txt+="点击关闭.\n\n";
alert(txt);
return true ;} 
*/
