var Auction = Class.create();
Auction.prototype = {
    initialize: function(changeProductUrl){
        
        this.changeProductUrl = changeProductUrl;
       
    },
	
	changeProduct : function(radio_product_id)
	{
		var product_id = $(radio_product_id).value;
		
		var url = this.changeProductUrl;
		
		url += 'product_id/' + product_id;

		new Ajax.Updater('product_name',url,{method: 'get', onComplete: function(){updateProductName();} ,onFailure: ""}); 	
		
	},
	
	checkBidderName : function()
	{
		var bidder_name = $('bidder_name').value;
		
		var url = this.changeProductUrl;
		url += 'bidder_name/'+ bidder_name;

		new Ajax.Updater('bidder-notice',url,{method: 'get', onComplete: function(){validBidderName();} ,onFailure: ""}); 			
		
		$('biddername-please-wait').style.display = "block";
		$('bidder-notice').style.display = "none";
		$('bidder_name').removeClassName('validation-passed');
	}
}

function validBidderName()
{
	$('biddername-please-wait').style.display = "none";
	$('bidder-notice').style.display = "block";
	if($('is_valid_bidder_name') != null)
	{
		if($('is_valid_bidder_name').value == '2') //invalid
		{
			$('bidder_name').removeClassName('validation-passed');
		}
	}
}

var AuctionUpdater = Class.create();
	AuctionUpdater.prototype = {

	initialize: function(url)
	{
		this.url = url;
	},
	
	updateInfo : function(elementId)
	{
		new Ajax.Updater(elementId,this.url,{method: 'get', onFailure: ""}); 		 		
	}
}

var AuctionTimeCounter = Class.create();
AuctionTimeCounter.prototype = {
	//params now_time, end_time : seconds
	initialize: function(now_time,end_time){
		this.now_time = parseInt(now_time) * 1000;
		this.end_time = parseInt(end_time) * 1000;
		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 = " seconds ";
		var mintext = " minutes, ";
		var hrtext = " hours, ";
		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 = " hour, ";
		if (min == 1)
			mintext = " minute, ";
		if (sec == 1)
			sectext = " second ";
	*/
		var sectext = "";
		var mintext = ":";
		var hrtext = ":";
		var dytext = " dagen, ";
		var mnthtext = " maanden, ";
		var yrtext = " jaren, ";
		if (yr == 1)
			yrtext = " jaar, ";
		if (mnth == 1)
			mnthtext = " maand, ";
		if (dy == 1)
			dytext = " dag, ";
		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;
		}
			this.now_time = this.now_time + 1000; //incres 1000 miliseconds

			timerID = setTimeout("setAuctionTimeleft("+ (this.now_time / 1000) +"," + (this.end_time /1000) +",'"+ timeleft_id +"');", 1000); 	
			
			delete this;
	}
}

function setAuctionTimeleft(now_time,end_time,timeleft_id)
{
	var counter = new AuctionTimeCounter(now_time,end_time);
	counter.setTimeleft(timeleft_id);
}

function updateProductName()
{
	$('product_name').value = $('newproduct_name').value;
}

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)
{
	var auctionUpdater = new AuctionUpdater(url);
	
	auctionUpdater.updateInfo(elementId);
	
	delete auctionUpdater;
	
	setTimeout("updateAuction('"+ elementId +"','"+ url +"')",8000);
}

	function checkBidPrice()
	{
		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)
			if(price > max_next_price)
			{
				alert($('max_next_price_nonce').value);				
				return false;
			}
		} 
		return true;
	}

	function setBackground(elementId,productId)
	{
		var codeColorId = 'codecolor'+ productId;
		var codeColor = $(codeColorId).value;
		var full_elementId = elementId + productId;
		
		codeColor = parseInt(codeColor);
		if(codeColor <255)
			codeColor +=4;
		
		$(codeColorId).value = codeColor;
		$(full_elementId).style.background = 'rgb(255,'+ codeColor +','+ codeColor +')';
		
		setTimeout("setBackground('"+ elementId +"','"+ productId +"')",10);
	}

//Adminhtml	
function setAuctionCheckboxId(url)
{
	var radios = document.getElementsByClassName('radio');
			
	for( var mi=0;mi<radios.length ;mi++)
	{
		radios[mi].name = 'candidate_product_id';
		radios[mi].id = 'candidate_product_id_' + radios[mi].value;
		
		if($('is_seted') == null)
		{
			Event.observe(radios[mi], 'click', function(event){ 		
				setproduct(url);
			});
		}
	}			
	
	if($('filter_entity_id') != null)
	{
		$('filter_entity_id').innerHTML = '<input type="hidden" id="is_seted" value="1">';
	}
	
	setTimeout("setAuctionCheckboxId('"+ url +"')",1000); 
}

function setproduct(url)
{
	var radios = document.getElementsByClassName('radio');
	
	var auction = new Auction(url);
	
	for( var mi=0;mi<radios.length ;mi++)
	{
		if(radios[mi].checked == true)
		{
			auction.changeProduct(radios[mi].id);
		}
	}
}	
//End Adminhtml	

