var AuctionUpdater = Class.create();
	AuctionUpdater.prototype = {

	initialize: function(url)
	{
		this.url = url;
	},
	
	updateInfo : function(elementId,productId,auctionId)
	{
		var rq_url = this.url;
		new Ajax.Request(rq_url, {
			method: 'post',
			parameters:{product_id:productId,auction_id:auctionId},
			evalJSON: true,
			onSuccess: function(response) {
				var data = response.responseText.evalJSON();
				$('product-price-'+productId).update(data['data_bid']['current_customer_bid_max']['price_bid']);
				$('bidder-'+productId).update(data['data_bid']['current_customer_bid_max']['name']);
				updateListCustomerBid(data['data_bid']['list_customer_bid']);
			}
		});
	}
}

var AuctionTimeCounter = Class.create();
AuctionTimeCounter.prototype = {
	//params now_time, end_time : seconds
	initialize: function(now_time,end_time,auction_id){
		this.now_time = parseInt(now_time) * 1000;
		this.end_time = parseInt(end_time) * 1000;
		this.auction_id = auction_id;
		this.end = new Date(this.end_time);
		var endDate = this.end;
		this.second = endDate.getSeconds();
		this.minute = endDate.getMinutes();
		this.hour = endDate.getHours();
		this.day = endDate.getDate();
		this.month = endDate.getMonth();
		var yr;
		if(endDate.getYear() < 1900)
			yr = endDate.getYear() + 1900;
		else
			yr = endDate.getYear();
		this.year = yr;
	},
	
	setTimeleft : function(timeleft_id)
	{
		var now = new Date(this.now_time);
		var yr;
		
		if(now.getYear() < 1900)
			yr = now.getYear() + 1900;
		else
			yr = now.getYear();
			
		var endtext = '0';
		var timerID;
		
		var sec = this.second - now.getSeconds();
		
		var min = this.minute - now.getMinutes();
		var hr = this.hour - now.getHours();
		var dy = this.day - now.getDate();
		var mnth = this.month - now.getMonth();
		yr = this.year - yr;
		
		var daysinmnth = 32 - new Date(now.getYear(),now.getMonth(), 32).getDate();
		if(sec < 0){
			sec = (sec+60)%60;
			min--;
		}
		if(min < 0){
			min = (min+60)%60;
			hr--;	
		}
		if(hr < 0){
			hr = (hr+24)%24;
			dy--;	
		}
		if(dy < 0){
			dy = (dy+daysinmnth)%daysinmnth;
			mnth--;	
		}
		if(mnth < 0){
			mnth = (mnth+12)%12;
			yr--;
		}	
		var sectext = "";
		var mintext = ":";
		var hrtext = ":";
		var dytext = " days, ";
		var mnthtext = " months, ";
		var yrtext = " years, ";
		if (yr == 1)
			yrtext = " year, ";
		if (mnth == 1)
			mnthtext = " month, ";
		if (dy == 1)
			dytext = " day, ";
		if (hr == 1)
			hrtext = ":";
		if (min == 1)
			mintext = ":";
		if (sec == 1)
			sectext = "";
	
		if (dy <10)
			dy = '0' + dy;		
		if (hr <10)
			hr = '0' + hr;
		if (min < 10)
			min = '0' + min;
		if (sec < 10)
			sec = '0' + sec;	
	
		if(yr <=0)
			yrtext =''
		else
			yrtext = yr + yrtext;
			
		if( (mnth <=0))
			mnthtext =''
		else
			mnthtext = '<span class="timeleft-text">'+ mnth +'</span>'+ mnthtext;
			
		if(dy <=0 && mnth>0)
			dytext =''
		else
			dytext = '<span class="timeleft-text">'+ dy +'</span>'+ dytext;
			
		if(hr <=0 && dy>0)
			hrtext =''
		else
			hrtext = '<span class="timeleft-text">'+ hr + '</span>'+ hrtext;
			
		if(min < 0)
			mintext =''
		else
			mintext = '<span class="timeleft-text">'+ min +'</span>'+ mintext;
			
		if(sec < 0)
			sectext =''
		else
			sectext = '<span class="timeleft-text">'+ sec +'</span>'+ sectext;			
			
		if(now >= this.end){
			document.getElementById(timeleft_id).innerHTML = endtext;
			clearTimeout(timerID);
		}
		else{
			
			document.getElementById(timeleft_id).innerHTML = yrtext + mnthtext + dytext + hrtext +  mintext + sectext;
		}
		
		if(this.now_time == this.end_time){
			location.reload(true);
			return;
		}		
		
		this.now_time = this.now_time + 1000; //incres 1000 miliseconds

		timerID = setTimeout("setAuctionTimeleft("+ (this.now_time / 1000) +"," + (this.end_time /1000) +",'"+ timeleft_id +"','"+ this.auction_id +"');", 1000); 	
	}
}

function setAuctionTimeleft(now_time,end_time,timeleft_id,auction_id)
{
	if($('auction_end_time_'+auction_id) != null)
		end_time = parseInt($('auction_end_time_'+auction_id).value);
	if($('auction_now_time_'+auction_id) != null){
		if($('auction_now_time_'+auction_id).value != ''){
			now_time = parseInt($('auction_now_time_'+auction_id).value);
			$('auction_now_time_'+auction_id).value = '';
		}
	}
	var counter = new AuctionTimeCounter(now_time,end_time,auction_id);
	counter.setTimeleft(timeleft_id);
}
function validNumeric(inputtext)
{
	var text = inputtext.value;
	var Char;
	var newtext = '';
	var newtext1 = '';
	var newtext2 = '';
	var j =0;
	var i;
	var is_decimal = false;
	
	for (i = 0; i < text.length ; i++) 
	{ 
		Char = text.charAt(i); 
		if( (Char != '0') || (newtext.length >0) )
		{
			if((! isNaN(Char) && Char != ' ') || (Char == '.'))
			{
				newtext +=  Char;
			}
		}
    }
	
	for (i = (newtext.length -1) ; i >=0; i--) 
	{ 
		Char = newtext.charAt(i); 

		if( Char != '.' || ((Char == '.') && (is_decimal == false) && (newtext1.length > 0)) )
		{
			newtext1 =  Char + newtext1;
			if(Char == '.')
				is_decimal = true;
		}
    }	
	
	var begin = newtext1.length -1 ;
	var varend = '';
	var k = newtext1.indexOf('.');
	
	if(k != -1)
	{
		for(i = k;i < newtext.length;i++)
			varend +=  newtext1.charAt(i);
		
		begin = k-1;
	}
	
	j = 0;
	for(i = begin ;i>=0;i--)
	{
		Char = newtext1.charAt(i); 
		newtext2 = Char + newtext2;
		j++;
		if((j==3) && (i>0))
		{
			newtext2 = ',' + newtext2;
			j = 0;
		}		
	}
	
	if(newtext2.length == 0)
		newtext2 = '0';
		
	newtext2 += varend;
	
	inputtext.value = newtext2;
}

function updateAuction(elementId,url,productId,auctionId)
{
	var auctionUpdater = new AuctionUpdater(url);
	
	auctionUpdater.updateInfo(elementId,productId,auctionId);
	
	setTimeout("updateAuction('"+ elementId +"','"+ url +"','"+ productId +"','"+auctionId+"')",5000);
}

function checkBidPrice(bid_type)
{
	var bid_price = $('bid_price').value;
	var newbid_price ='';
	var Char;
	var i;

	for(i = 0 ;i < bid_price.length ;i ++)
	{
		Char = bid_price.charAt(i); 

		if(Char != ',')
		{
			newbid_price += Char;
		}		
	}	
	
	var price = parseFloat(newbid_price);
	

	if(price != 0)
	{
		var min_next_price = $('min_next_price').value;
		var max_next_price = $('max_next_price').value;
		
		max_next_price = parseFloat(max_next_price);
		min_next_price = parseFloat(min_next_price);
		
		if(price < min_next_price)
		{
			alert($('min_next_price_nonce').value);	
			return false;
		}
		if(max_next_price != 0 && parseInt(bid_type) != 0)
		if(price > max_next_price)
		{
			alert($('max_next_price_nonce').value);				
			return false;
		}
	} 
	return true;
}


function submitBid(url,productId,auctionId)
{
	if(typeof bidForm !='undefined' && bidForm.validator.validate()){
		
		var name = '';
		if($('customer_bid_name')){
			name = $('customer_bid_name').value;
		}
		
		var customer_email = '';
		if($('customer_bid_email')){
			customer_email = $('customer_bid_email').value;
		}
		
		var mobile = '';
		if($('customer_bid_mobile')){
			mobile = $('customer_bid_mobile').value;
		}
		
		var priceBid = $('price-bid').value;
		new Ajax.Request(url, {
			method: 'post',
			parameters:{product_id:productId,auction_id:auctionId,price_bid:priceBid,email:customer_email,mobile:mobile,name:name},
			evalJSON: true,
			onSuccess: function(response) {
				var data = response.responseText.evalJSON();
				if(data['error']=='0'){
					updateListCustomerBid(data['data_bid']['list_customer_bid']);
				}
				else{
					alert(data['msg']);
				}
				//console.log(data);
				//$('product-price-'+productId).update(data['current_customer_bid_max']['price_bid']);
				//$('bidder-'+productId).update(data['current_customer_bid_max']['name']);
			}
		});
	}
}	
function updateListCustomerBid(data){
	var html = '';
	for(var i = 0; i< data.length;i++){
		var customer = data[i];
		html +='<tr class="last even"><td class="label">'+customer['name']+'</td><td class="data last">'+customer['price_bid']+'</td></tr>';
	}
	$('list-customer-bid').update(html);
}
function autionBidComplete()
{	
	$('auction_bid_button').show();
	$('auction_bid_waitting').hide();	
		
	updateAuctionComplete();

	$('is_bidding').value = '0';
}
var bidForm = null;
document.observe("dom:loaded", function() {
	Validation.add(
			'validate-double', 
			'Please use numbers only in this field.', 
			function(v) {
				//console.log(/^\d*[0-9](|.\d*[0-9]|)*$/.test(v));
                return Validation.get('IsEmpty').test(v) ||  /^\d*[0-9](|.\d*[0-9]|)*$/.test(v);
            }
	);
	bidForm = new VarienForm('bid-form');
});
