var minbudnu = 0;
var firstload = false;

function countdown() {
	var start		= parseInt(document.getElementById('start').firstChild.nodeValue);
	var slut		= parseInt(document.getElementById('slut').firstChild.nodeValue);
	var phptime		= parseInt(document.getElementById('phptime').firstChild.nodeValue);
	
	var seconds		= slut - phptime;
	
	// opdater sluttidspunkt hvert 20. sek
	if (seconds%20 == 0) opdaterslut(0);
	
	// afslut auktion
	if (parseInt(seconds) < 1) {
		document.getElementById('auktionsstatus').firstChild.nodeValue = text_loading_status;
		
		tjekforslut();
		
		return false;
	}
	
	// opdater status
	if (start < phptime && slut < phptime)		statustext = text_next;
	else if (phptime < slut)					statustext = text_active;
	
	document.getElementById('auktionsstatus').firstChild.nodeValue = statustext;
			
	// hvis 5 minutter tilbage, blink
	if (seconds < 300) {
		document.getElementById('countdown_dage').style['color'] = '#FF0000';
		document.getElementById('countdown_timer').style['color'] = '#FF0000';
		document.getElementById('countdown_minutter').style['color'] = '#FF0000';
		document.getElementById('countdown_sekunder').style['color'] = '#FF0000';
		
		setTimeout("document.getElementById('countdown_dage').style['color'] = '#000000'",495);
		setTimeout("document.getElementById('countdown_timer').style['color'] = '#000000'",495);
		setTimeout("document.getElementById('countdown_minutter').style['color'] = '#000000'",495);
		setTimeout("document.getElementById('countdown_sekunder').style['color'] = '#000000'",495);
	}
	
	// udregn tid tilbage
	days		= Math.floor(seconds / (60 * 60 * 24));
	seconds		%= (60 * 60 * 24);
	hours		= Math.floor(seconds / (60 * 60));
	seconds		%= (60 * 60);
	minutes		= Math.floor(seconds / 60);
	seconds		%= 60;
	
	if (document.getElementById('countdown_timer')) { // sikre at auktionen findes
		document.getElementById('countdown_dage').firstChild.nodeValue		= days;
		document.getElementById('countdown_timer').firstChild.nodeValue		= hours;
		document.getElementById('countdown_minutter').firstChild.nodeValue	= minutes;
		document.getElementById('countdown_sekunder').firstChild.nodeValue	= seconds;
	}
	
	// gentag countdown hvert sekundt
	window.setTimeout('countdown()',995);
}

function tjekforslut() {
	// opdater sluttidspunkt og tjek for slut
	opdaterslut(1);
}

function opdaterslut(mode) {
	var xmlhttp;
	
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	
	xmlhttp.open('GET','/_feed/auktionslut.php?auktion=' + document.getElementById('auktion').firstChild.nodeValue,true);
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var result = xmlhttp.responseText;
			
			var json = eval('(' + result + ')');
			
			if (json.length == 0) {
				alert('Fejl ved hentning af auktionssluttidspunkt');
			}
			
			document.getElementById('slut').firstChild.nodeValue = json;
			
			if (mode == 1) {
				if (json < document.getElementById('phptime').firstChild.nodeValue) {
					document.getElementById('auktionsstatus').firstChild.nodeValue		= text_closed;
					
					document.getElementById('countdown_dage').firstChild.nodeValue		= '0';
					document.getElementById('countdown_timer').firstChild.nodeValue		= '0';
					document.getElementById('countdown_minutter').firstChild.nodeValue	= '0';
					document.getElementById('countdown_sekunder').firstChild.nodeValue	= '0';
				}
				else countdown();
			}
		}
	}
	
	xmlhttp.send(null);
}

function afsendbud() {
	if (!document.getElementById('bud_bud')) {
		alert('Manglende budfelter');
		
		return false
	}
	
	// tjek om auktion er aktiv
	if (document.getElementById('slut').firstChild.nodeValue < document.getElementById('phptime').firstChild.nodeValue || document.getElementById('start').firstChild.nodeValue > document.getElementById('phptime').firstChild.nodeValue) {
		alert(text_not_active);
		return false
	}
	
	// tjek om brugeren er logget ind
	if (document.getElementById('indlogget').firstChild.nodeValue == 0) {
		if(confirm(text_do_you_want_to_login)) {
			location.href = '/' + document.getElementById('systemlang').firstChild.nodeValue + '-' + document.getElementById('systemcur').firstChild.nodeValue + '/konto/login.php?back=' + location.href;
		}
		
		return false
	}
	
	var auktion = document.getElementById('auktion').firstChild.nodeValue;
	var antal	= document.getElementById('bud_antal').getAttribute('selectedIndex') + 1;
	var bud		= document.getElementById('bud_bud').value;
	
	if (!/^[0-9]{1,12}$/.test(antal)) {
		alert(text_invalid_quantity);
		return false
	}
	
	if (!/^[0-9]{1,12}$/.test(bud)) {
		alert(text_only_int);
		return false
	}
	
	// tjek om over minbud
	moms = (cur_vat == 0)? 1.25 : 1;
	
	if (Math.ceil(parseInt(bud)*cur_kurs*moms) < minbudnu) {
		alert(text_bet_to_low);
		return false
	}
	
	// bekræft bud
	if (!confirm(text_confirm_bet.replace('[bud]',bud).replace('[currency]',cur_code))) return false;
	
	// afsend bud og vent på respons
	var xmlhttp;
	
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	
	xmlhttp.open('GET','/' + $('#systemlang').text() + '-' + $('#systemcur').text() + '/action/bud.php?auktion=' + auktion + '&antal=' + antal + '&bud=' + bud,true);
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var result = xmlhttp.responseText;
			
			var json = eval('(' + result + ')');
			
			// kontroller om der findes respons
			if (json.length == 0) {
				alert('Bud blev ikke registreret: Ukendt respons');
			}
			
			if (!/^[0-9]{1,12}$/.test(json)) {
				for(h=0;h<json.length;h++) {
					if (json[h] !== undefined) alert(text_bet_not_registret + json[h]);
				}
			}
			
			opdaterslut();
			hentbud();
		}
	}
	
	xmlhttp.send(null);
	
	return false;
}

function startbudhentning() {
	hentbud();
	
	setTimeout('startbudhentning()',30000);
}

function hentbud() {
	var xmlhttp;
	
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	
	xmlhttp.open('GET','/_feed/bud.php?auktion=' + document.getElementById('auktion').firstChild.nodeValue,true);
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var result = xmlhttp.responseText;
			
			var json = eval('(' + result + ')');
			
			// kontroller der ikke er fejl
			if (json[0] == undefined) {
				alert(json);
				
				return false
			}
			
			// tøm listen
			tombudliste();
			
			var vindende = false;
			
			// tjek om der er bud
			if (json[0] == '0') {
				document.getElementById('budliste').appendChild(document.createTextNode(text_no_bet_yet));
				
				return false
			}
			
			var table = document.createElement('table');
				table.setAttribute('cellPadding','0');
				table.setAttribute('cellSpacing','0');
				
				table.style['width'] = '100%';
				
				var tbody = document.createElement('tbody');
					var tr = document.createElement('tr');
						var td = document.createElement('td');
							td.setAttribute('colSpan','3');
							
							var h2_2 = document.createElement('h2');
								h2_2.appendChild(document.createTextNode(text_winner_bet));
							
							td.appendChild(h2_2);
						tr.appendChild(td);
					tbody.appendChild(tr);
					
					var tr1 = document.createElement('tr');
						tr1.style['textAlign']			= 'left';
						tr1.style['verticalAlign']		= 'middle';
						tr1.style['backgroundColor']	= '#DADADA';
						tr1.style['fontWeight']			= 'bold';
						
						var td2 = document.createElement('td');
							td2.style['padding']		= '5px';
							
							td2.appendChild(document.createTextNode(text_time));
						
						var td3 = document.createElement('td');
							td3.style['padding']		= '5px';
							
							td3.appendChild(document.createTextNode(text_username));
						
						var td4 = document.createElement('td');
							td4.style['padding']		= '5px';
							
							td4.style['textAlign']		= 'right';
							
							td4.appendChild(document.createTextNode(text_bet_price));
							
						tr1.appendChild(td2);
						tr1.appendChild(td3);
						tr1.appendChild(td4);
					tbody.appendChild(tr1);
				
			// udskriv bud
			for(h=0;h<json.length;h++) {
				var id				= json[h][0];
				var bud				= json[h][1];
				var brugernavn		= json[h][2];
				var tidspunkt		= json[h][3];
				var overbyder		= json[h][4];
				
				var antal			= document.getElementById('antalprodukter').firstChild.nodeValue;
				
				if (h < antal && brugernavn == document.getElementById('indloggetbrugernavn').firstChild.nodeValue) vindende = true;
				
				if (h == antal) {
					var tr = document.createElement('tr');
						var td = document.createElement('td');
							td.setAttribute('colSpan','3');
							
							var h2_1 = document.createElement('h2');
								h2_1.appendChild(document.createTextNode(text_recent_bets));
							
							td.appendChild(h2_1);
						tr.appendChild(td);
					tbody.appendChild(tr);
					
					var tr1 = document.createElement('tr');
						tr1.style['textAlign']			= 'left';
						tr1.style['verticalAlign']		= 'middle';
						tr1.style['backgroundColor']	= '#DADADA';
						tr1.style['fontWeight']			= 'bold';
						
						var td2 = document.createElement('td');
							td2.style['padding']		= '5px';
							
							td2.appendChild(document.createTextNode(text_time));
						
						var td3 = document.createElement('td');
							td3.style['padding']		= '5px';
							
							td3.appendChild(document.createTextNode(text_username));
						
						var td4 = document.createElement('td');
							td4.style['padding']		= '5px';
							
							td4.style['textAlign']		= 'right';
							
							td4.appendChild(document.createTextNode(text_bet_price));
							
						tr1.appendChild(td2);
						tr1.appendChild(td3);
						tr1.appendChild(td4);
					tbody.appendChild(tr1);
				}
				
				var tr = document.createElement('tr');
					tr.setAttribute('title',id);
					
					var td1 = document.createElement('td');
						td1.style['width']		= '130px';
						
						var div1 = document.createElement('div');
							div1.style['margin']			= '2px 0px';
							div1.style['backgroundColor']	= '#F3F3F3';
							div1.style['padding']			= '10px 5px';
							
							div1.appendChild(document.createTextNode(tidspunkt));
							
						td1.appendChild(div1);
						
					tr.appendChild(td1);
					
					var td2 = document.createElement('td');
						var div2 = document.createElement('div');
							div2.style['margin']			= '2px 0px';
							div2.style['backgroundColor']	= '#F3F3F3';
							div2.style['padding']			= '10px 5px';
							
							div2.appendChild(document.createTextNode(brugernavn));
						
							if (overbyder != '' && overbyder != brugernavn) {
								var autobud = document.createElement('span');
									autobud.setAttribute('title',text_max_bet_info.replace('[loser]',overbyder).replace('[winner]',brugernavn));
									
									autobud.style['color']		= '#666666';
									autobud.style['cursor']		= 'help';
									
									autobud.appendChild(document.createTextNode(text_max_bet));
									
								div2.appendChild(autobud);
							}
							
						td2.appendChild(div2);
						
					tr.appendChild(td2);
					
					var td3 = document.createElement('td');
						td3.style['width']		= '90px';
						td3.style['textAlign']	= 'right';
						
						var div3 = document.createElement('div');
							div3.style['margin']			= '2px 0px';
							div3.style['backgroundColor']	= '#F3F3F3';
							div3.style['padding']			= '10px 5px';
							
							div3.appendChild(document.createTextNode(cur_pris(bud,2,1)));
							
						td3.appendChild(div3);
						
					tr.appendChild(td3);
				tbody.appendChild(tr);
				
				if (browser() == 'IE') document.getElementById('ervinder').firstChild.nodeValue = (vindende)? text_im_winner : '';
			}
			
			table.appendChild(tbody);
			
			document.getElementById('budliste').appendChild(table);
			
			minbud();
		}
	}
	
	xmlhttp.send(null);
}

function tombudliste() {
	var position = document.getElementById('budliste');
	
	while(position.firstChild) {
		position.removeChild(position.firstChild);
	}
}

function minbud() {
	var xmlhttp;
	
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	
	xmlhttp.open('GET','/_feed/budnu.php?auktion=' + document.getElementById('auktion').firstChild.nodeValue + '&antal=' + document.getElementById('antalprodukter').firstChild.nodeValue,true);
	
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var result = xmlhttp.responseText;
			
			var json = eval('(' + result + ')');
			
			if (document.getElementById('bud_bud')) document.getElementById('bud_bud').value = cur_pris(json[1],3,0);
			
			if (json[0] !== 0) document.getElementById('td_min_bud_text').firstChild.nodeValue = text_current_bet;
			document.getElementById('td_min_bud').firstChild.nodeValue = cur_pris(json[0],2,1);
			
			if (document.getElementById('td_besparelse')) document.getElementById('td_besparelse').firstChild.nodeValue = cur_pris(document.getElementById('salgspris').firstChild.nodeValue - json[0],2,1);
			
			minbudfor = minbudnu;
			
			// opdater gemt minbud
			minbudnu = parseInt(json[1]);
			
			// fokuser budboks
			if (minbudfor != 0 && document.getElementById('bud_bud')) document.getElementById('bud_bud').select();
		}
	}
	
	xmlhttp.send(null);
}
