var time_out;

function initAuction(){
	time_out = setTimeout("show_time()",1000);
}

function show_time()
{
	var time_end_s = document.getElementById("time_end").value;
	var astate = document.getElementById("astate").value;

	if(astate == 1){
		var timer = document.getElementById("timer");
		acd.findAuctionCountDown(time_end_s,
				function(data){
					timer.innerHTML = data;
					if(data == "over"){
						clearTimeout(time_out);
						var a_button = document.getElementById("add_auction");

						if(a_button == null){
							alert("Bidding has ended on this item.");
						}else{
						}
						a_button.style.display = "none";
						
						window.location.reload();
					}else{
						time_out = setTimeout("show_time()",1000);
					}
				}
		);
	}else{
		clearTimeout(time_out);
	}
}
function canselAuction(){
	auctionMainDIV.style.display = "none";
	auctionDIV.style.display = "none";
	
}
function biddingAuction(id){
	var bidprice = document.getElementById("bidprice").value;
	var auctionId = document.getElementById("auctionId").value;
	if(bidprice == null){
		bidprice = 0;
	}
	if(auctionId == null){
		auctionId = "0";
	}
	
	aDWR.findUserLoginState(
			function(data){
				if(data == false){
					alert("Please login or register before you bid.");
					document.getElementById("loginForm").submit();				
					return;
				}else{
					var bid_now = document.getElementById("bid_now");
					var place_bid = document.getElementById("place_bid");
					bid_now.style.display = "none";
					place_bid.style.display = "block";
				}
			}
	);
	
	aDWR.findAuctionBidPrice(auctionId,
		function(data){
			if(data != null && data[1]+data[4] > bidprice){
				document.getElementById("bidprice").innerHTML = data[1].toFixed(2);
				//document.getElementById("bPrice_0").innerHTML = data[1].toFixed(2);
				//document.getElementById("b_time").innerHTML = data[2].format("yyyy-MM-dd hh:mm:ss");;
				//document.getElementById("b_email").innerHTML = data[3];
				document.getElementById("u_bprice").innerHTML = (data[1] + data[4]).toFixed(2) ;
				document.getElementById("bnum").innerHTML = data[5];
			}else{
				document.getElementById("bidprice").value = bidprice;
			}
		}
	);
	
	
}
function loginAuction(){
	
	var userEmail = document.getElementById("auserEmail").value;
	var userPassword = document.getElementById("auserPassword").value;
	var price = document.getElementById("aprice").value;
	var bprice = document.getElementById("bprice").value;
	var sprice = document.getElementById("sprice").value;
	var auctionId = document.getElementById("auctionId").value;

	if(price == null || price == ""){
		alert("Your bid is incorrect or lower than the current price, please try again.");
		return ;
	}	
	if(!isFloat(price)){
		alert("Your bid is incorrect or lower than the current price, please try again.");
		return ;
	}
	price = parseFloat(price);
	if(price >= bprice && price >= sprice){

	}else{
		alert("Your bid is incorrect or lower than the current price, please try again.");
		return ;
	}

	
	var info = document.getElementById("info");
	aDWR.findUserLoginAndAuction(userEmail,userPassword,price,auctionId,
		function(data){
			if(data == 0){
				info.innerHTML = "error";
			}else if (data == 1){
				info.innerHTML = "Email Address must not be empty!";
			}else if (data == 2){
				info.innerHTML = "Password must not be empty!";
			}else if (data == 3){
				info.innerHTML = "Price must not be empty!";
			}else if (data == 4){
				info.innerHTML = "Auction is error";
			}else if (data == 6){
				alert("Your bid is incorrect or lower than the current price, please try again.");
				window.location.reload();
			}else if (data == 5){
				info.innerHTML = "Email Address or password is error!";
			}else if (data == 99){
				alert("You have bidden successfully.");
				window.location.reload();
			}
		}
	);

}

function isFloat(f){ 
	var j=0 ; 
	if (f==""){ 
		return false; 
	} 
	if(f.charAt(0)=="0"){ 
		return false; 
	} 
	if(f.charAt(f.length-1)=="."){ 
		return false; 
	} 
	if(f<=0){ 
		return false; 
	} 
	for(var i = 0;i<f.length;i++){ 
		if(f.charAt(i)=="."){ 
			if(i==0){ 
				return false; 
			} 
			j=j+1; 
		} 
		if(j>1){ 
			return false; 
		} 
	} 
	for(var i = 0;i<f.length;i++){ 
		if(f.charAt(i)<"0"||f.charAt(i)>"9"){ 
			if(f.charAt(i)!="."){ 
				return false; 
			} 
		} 
	} 
	return true; 
} 

Date.prototype.format = function(format) {   

    var o = {   
        "M+" :this.getMonth() + 1, // month   
        "d+" :this.getDate(), // day   
        "h+" :this.getHours(), // hour   
        "m+" :this.getMinutes(), // minute   
        "s+" :this.getSeconds(), // second   
        "q+" :Math.floor((this.getMonth() + 3) / 3), // quarter   
        "S" :this.getMilliseconds()   
    }   
  
    if (/(y+)/.test(format)) {   
        format = format.replace(RegExp.$1, (this.getFullYear() + "")   
                .substr(4 - RegExp.$1.length));   
    }   
  
    for ( var k in o) {   
        if (new RegExp("(" + k + ")").test(format)) {   
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]   
                    : ("00" + o[k]).substr(("" + o[k]).length));   
        }   
    }   
    return format;   
}  

