var actiefMenuItem = null

function viewhide(id)
{
	var e = null;
	
	// actief item sluiten
	if ( (actiefMenuItem != null) && (actiefMenuItem != id) )
	{
		e = getE(actiefMenuItem);
		e.style.display = "none";
	}
	
	e = getE(id);
	if (e)
	{
		if (e.style.display != "block")
		{
			e.style.display = "block";
		}
		else
		{
			e.style.display = "none";
		}
		
		actiefMenuItem = id;
		
		return true;
	}
	
	return false;
}

function getE(id)
{
	var e = null;
	if (document.getElementById)
	{
		e = document.getElementById(id);
	}
	else if (document.all)
	{
		e = document.all[e];
	}
	return e;
}

function is_int(v) {
	return !isNaN(v);
}

function popup_voorverkoopactie(gebruiker_id) {
	document.open("popup.php?p=voorverkoopactie&gebruiker_id=" + gebruiker_id + PARAM, "voorverkoopactie_popup", "width=640,height=600,resizable=yes,scrollbars=yes,toolbar=no");
}

/* ARTIKELEN */

function aantal_plus(artikelcode, update) {
	var e = getE("aantal_" + artikelcode);
	if (e) {
		var aantal = e.value * 1;
		if (!is_int(aantal)) {
			aantal = 0;
		}
		aantal++;
		e.value = aantal;
		if (update) {
			set_totaalprijs(artikelcode);
		}
	}
}

function aantal_min(artikelcode, update) {
	var e = getE("aantal_" + artikelcode);
	if (e) {
		if (!is_int(e.value)) {
			e.value = '0';
		}
		else {
			var aantal = e.value * 1;
			if (aantal > 0) {
				aantal--;
				e.value = aantal;
			}
		}
		if (update) {
			set_totaalprijs(artikelcode);
		}
	}
}

function check_aantal(id) {
	var e = getE(id);
	if (e) {
		if (e.value == "0") {
			e.value = "1";
		}
	}
}

function popup_artikel(artikel_code, wat) {
	document.open("popup.php?p=artikel_info&artikel_code=" + artikel_code + "&wat=" + wat + PARAM, "artikel_popup", "width=940,height=690,resizable=yes,scrollbars=yes,toolbar=no");
}

function popup_video(artikel_code, wat) {
	popup_artikel(artikel_code, wat);
}

/* BESTELLIJST */

function popup_bestellijst() {
	document.open("popup.php?p=bestellijst_afdruk" + PARAM, "bestellijst_popup", "width=740,height=600,resizable=yes,scrollbars=yes,toolbar=no,menubar=yes");
}

/* BESTELLING */

function popup_bestelling_html() {
	return document.open("popup.php?p=bestelling_html_afdruk" + PARAM, "bestelling_html_popup", "width=740,height=600,resizable=yes,scrollbars=yes,toolbar=no,menubar=yes");
}

/* ARTIKELEN BESTELLIJST */

function set_totaalprijs(artikel_code) {
	var a = getE("aantal_" + artikel_code);
	var p = getE("verkoopprijs_" + artikel_code);
	var t = getE("totaalprijs_" + artikel_code);
	if (a && p && t) {
		var tp = 0;
		if (a.value != "") {
			if (!isNaN(a.value)) {
				if (a.value.indexOf(".") == -1) {
					tp = a.value * p.value.replace(",", ".");
					tp = tp + "";
					tp = tp.replace(".", ",");
					pos = tp.indexOf(",");
				//	alert(pos + " ~ " + tp.length);
					if (pos == -1) {
						tp += ",00";
					}
					else if (pos == tp.length - 2) {
						tp += "0";
					}
					else if (pos < tp.length - 2) {
						tp = tp.substr(0, pos + 3);
					}
				}
			}
		}
		
		if (t.value) {
			t.value = tp;
		}
		else {
			t.innerHTML = "";
			t.innerHTML += "Totaalprijs: ";
			t.innerHTML += "&euro; ";
			t.innerHTML += tp;
		}
	}
}

/* KLEUR KIEZEN FUCNTIES */

function kleur_kiezen(veld, form)
{
	vwb_kleur_popup = window.open("kleur_kiezen.php?form=" + form + "&veld=" + veld, "vwb_kleur_kiezen", "width=500,height=400,status=yes,resizable=yes,scrollbars=yes");
	vwb_kleur_popup.focus();
}

function chg_color(veld, form)
{
	td = getE("td_" + veld);
	if (td != null)
	{
		fm = document.forms[form];
		td.bgColor = fm[veld].value;
	}
}

/* <TR> HOVER FUNCTIES */

function hoover(e, a)
{
	if (a == "in")
	{
		e.style.backgroundColor = "#d0d0d0"; // #fcf0c0
	}
	else
	{
		e.style.backgroundColor = "";
	}
}

/* <SELECT> VERPLAATSEN/POSITIE FUNCTIES */

function verplaatsen(r) // richting
{
	var fm = document.forms.f;
	var ps = fm.posities;
	var po = fm.pos;
	var si = ps.selectedIndex;
	
	if (si != -1)
	{
		var ni = -1; // nieuwe index
		if ( (si == 0) && (r == 'o') ) // omhoog
		{
			// ni = ps.options.length - 1;
		}
		else if ( (si == ps.options.length - 1) && (r == 'b') ) // omlaag
		{
			// ni = 0;
		}
		else if (r == 'o')
		{
			ni = si - 1;
		}
		else if (r == 'b')
		{
			ni = si + 1;
		}
		if (ni != -1)
		{
			var hv = ps.options[si].value;
			var ht = ps.options[si].text;
			ps.options[si].value = ps.options[ni].value;
			ps.options[si].text = ps.options[ni].text;
			ps.options[ni].value = hv;
			ps.options[ni].text = ht;
			ps.selectedIndex = ni;
			
			posities();
		}
	}
}

function posities()
{
	var fm = document.forms.f;
	var ps = fm.posities;
	var po = fm.pos;
	var p = '';
	for (i = 0; i < ps.options.length; i++)
	{
		p = p + ps.options[i].value + ','
	}
	po.value = p;
}