function DisablePrijs()
{
	$('#prijs').attr("disabled", ($('#prijskeuze').val() != 1));
	
	if ($('#prijskeuze').val() != 1)
		$('#prijs').val("0,00");
}

function FillSubCategorieList() {
	if(http_request.readyState == 4){
		var items=http_request.responseXML.documentElement;		
		var combo = document.getElementById('subcategorie');
		
		// Add options
		combo.options.length = 0;
		for( var i = 0; i < items.childNodes.length; i++ ) {
			combo.options[i] = new Option(items.childNodes[i].attributes[1].value, items.childNodes[i].attributes[0].value);
		}		
	}
}

function ZoekSubcategorien() {
	var categorieid = document.getElementById('categorie');
	makeRequest('ajax.php?table=subcategorie&search=', categorieid.value, FillSubCategorieList);	
}

function ReloadFotos() {
	// Stuur request naar server
	var advertentieid = document.getElementById('advertentieid').value;
	makeRequest('ajax.php?table=foto&search=', advertentieid, FillFotoTable);	
}

function DoAfterDelete() {
	if(http_request.readyState == 4){
		ReloadFotos();
	}
}

function FillFotoTable() {
	if(http_request.readyState == 4){
		var items=http_request.responseXML.documentElement;		
		var table = document.getElementById('fotos');

		// Remove cells
		table.deleteRow(0);
		var row = table.insertRow(0);		
		
		// Add rows			
		if (items == null) return;
		for( var i = items.childNodes.length-1; i>=0; i-- ) {
		//for( var i = 0; i < items.childNodes.length; i++ ) {
			if (items.childNodes[i].attributes.length > 0) {
				value = items.childNodes[i].attributes[1].value; 
				var cell = row.insertCell(0);
				cell.setAttribute('class','td_foto');
				cell.innerHTML = sprintf('<a class="thickbox" rel="fotos" href="fotos/%s.jpg?val=%s" title=""><img src="fotos/%s.jpg" alt="foto" width="100" /></a><br /><a href="javascript:DeleteFoto(\'%s\')">Verwijderen</a>', Math.floor(Math.random()*1000), value, value, value);
			}
		}

		//Re-init thickbox
		tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
		imgLoader = new Image();// preload image
		imgLoader.src = tb_pathToImage;		
	}
}

function DeleteFoto(name) {
	if (confirm('Weet u zeker dat u deze foto wilt verwijderen?') == false) return; 
	
	// Stuur request naar server
	var advertentieid = document.getElementById('advertentieid').value;
	makeRequest(sprintf('manage_fotos.php?action=delete&advertentieid=%s&name=', advertentieid), name, DoAfterDelete);
}

function DoAfterBodDelete() {
	if(http_request.readyState == 4){
		window.location.reload();
	}
}

function DeleteBod(advertentieid, biddingid) {
	if (confirm('Weet u zeker dat u dit bod wilt verwijderen?') == false) return; 
	
	// Stuur request naar server
	makeRequest(sprintf('manage_biddings.php?action=delete&advertentieid=%s&biddingid=', advertentieid), biddingid, DoAfterBodDelete);
}

// ========== --------------- AJAX code --------------- ==========
var http_request = false;
function makeRequest(url, parameters, callback) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		http_request.overrideMimeType('text/xml');
		//http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }
  http_request.onreadystatechange = callback; //showSearchResults;
  http_request.open('GET', url + parameters, true);
  http_request.send(null);
}
// ---------------------------------------------------------------------
// This code is in the public domain. Feel free to link back to http://jan.moesen.nu/
function sprintf() {
	if (!arguments || arguments.length < 1 || !RegExp)
	{
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
		else
		{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length)
			{
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
				   if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			  else if (pPad) pad = pPad;
			var justifyRight = true;
				   if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
				   if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
				   if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
				   if (pType == 'b') subst = parseInt(param).toString(2);
			  else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			  else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			  else if (pType == 'u') subst = Math.abs(param);
			  else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			  else if (pType == 'o') subst = parseInt(param).toString(8);
			  else if (pType == 's') subst = param;
			  else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			  else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}

