var gid = null;
function checkVoorraad(id, productcode, displaymode) {
	var now = new Date();
	var callback =
	{
	  success: onCheckVoorraad,
	  argument: {id: id, productcode: productcode, displaymode: displaymode}
	}
	YAHOO.util.Connect.asyncRequest('GET', "/wobjects/intranet/voorraadinfo?HttpProxy_displaymode="+displaymode+"&HttpProxy_fn=updatevoorraad&HttpProxy_productcode="+myescape(productcode)+"&id="+id+"&noCache="+now.getTime(), callback, null); 
}

function onCheckVoorraad(o) {
	if (o.argument.displaymode == "totaalJSON") {
		var data = YAHOO.lang.JSON.parse(o.responseText);
		document.getElementById('artikelenPrijs'+o.argument.id).innerHTML = data.prijs;
		if (data.voorraadLocatie == "intern" || data.voorraadLocatie == "extern") {
			document.getElementById('artikelenVoorraad' + o.argument.id).innerHTML = data.voorraad;
		} else {
			document.getElementById('artikelenVoorraad' + o.argument.id).innerHTML = "";
		}
		document.getElementById('artikelenVoorraad'+o.argument.id).className = data.voorraadLocatie;
		if(data.voorraadLocatie == "ruim-op-voorraad-ext") {
			document.getElementById('artikelenVoorraad'+o.argument.id).title = "Voorraad leverancier: "+data.voorraad;			
		}
		if(typeof(window[ 'momentDialog'])!="undefined") {
			momentDialog.hide();
		}			
	}
	else {
		document.getElementById('voorraad' + o.argument.id).innerHTML = o.responseText;
	}
	if(o.argument.displaymode=="artikelinfo") {
		if(document.getElementById('leveranciers'+o.argument.id)!=null) {
			showVoorraad(o.argument.id,o.argument.productcode,'artikelinfoleveranciers');
		}
	} else {
		if(o.argument.displaymode=="totaal") {
			if(typeof(window[ 'momentDialog'])!="undefined") {
				momentDialog.hide();
			}			
		}
		if(o.argument.id==gid) {
			showVoorraad(o.argument.id,o.argument.productcode,'leveranciers');
		}
	}
}

function hideVoorraad(id) {
	gid = null;
	document.getElementById(id).style.display='none';
}

function showVoorraad(id, productcode, displaymode) {
	gid = id;
	var now = new Date();
	var callback =
	{
	  success: onShowVoorraad,
	  argument: {id: id}
	}
	YAHOO.util.Connect.asyncRequest('GET', "/wobjects/intranet/voorraadinfo?HttpProxy_displaymode="+displaymode+"&HttpProxy_productcode="+myescape(productcode)+"&noCache="+now.getTime(), callback, null); 
}

function onShowVoorraad(o) {
	if(o.argument.id==gid) {
		document.getElementById('leveranciers'+o.argument.id).innerHTML = o.responseText;
		document.getElementById('leveranciers'+o.argument.id).style.display='block';
	}
}

function myescape(text) {
	text=escape(text);
	// escape doet niks met +
	while(text.indexOf('+')!=-1) {
		text = text.replace('+','%2B');
	}
	return text;
}