/************************
 * MouseOver/Out Events *
 ************************/
 
function mOver(ref) {
	if(document.getElementById(ref)){
		if(document.getElementById(ref).rev == "topMenu"){
			document.getElementById(ref).style.color = 'grey';
		}
	}
}

function mOut(ref) {
	if(document.getElementById(ref)){
		activeRef = window.location.href.slice(window.location.href.indexOf('ref=') + 4),window.location.href.indexOf('&',window.location.href.indexOf('ref='));
		if(ref == activeRef){
		}
		else{
			if(document.getElementById(ref).rev == "topMenu"){
				document.getElementById(ref).style.color = "";
			}
		}
	}
}

function ajaxRequest(id) {
	if (window.XMLHttpRequest) {
		xmlhttp2=new XMLHttpRequest();
	} else 
		if (window.ActiveXObject) {
			xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp2.onreadystatechange=function() {
		if (xmlhttp2.readyState==4 && id !='') {
			document.getElementById(id).innerHTML = xmlhttp2.responseText
		}
	}
}

function addToCart(artikelcode) {
	ajaxRequest('winkelmand');
	xmlhttp2.open("GET","getbasket.asp?artikel=" + artikelcode ,true);
	xmlhttp2.send(null);
	document.getElementById('winkelmand').style.visibility = 'visible'
	alert("Artikel toegevoegd aan winkelmandje");
}

function removeFromCart(artikelcode) {
	document.getElementById('artikel_' + artikelcode).style.backgroundColor = 'red'
	if (confirm("Gemarkeerde regel verwijderen?")) {
		document.getElementById('artikel_' + artikelcode).style.display = 'none'
		document.getElementById('amount_' +artikelcode).value = 0
		changeCart(artikelcode)
		updateCartTotals()
		if (!document.winkelmandje.verzenden.disabled) {
			frm = document.winkelmandje
			artavail = false
			for (var i=0; i < frm.length; i++) {
				form_field = frm.elements[i]
				form_id = form_field.id
				if (form_id.substring(0,7) == 'amount_') {
					if (parseInt(form_field.value) > 0) artavail = true
				}
			}	
			if (!artavail) document.winkelmandje.verzenden.disabled = true
		}
	} else {
		document.getElementById('artikel_' + artikelcode).style.backgroundColor = 'white'
	}
}


function changeCart(artikelcode) {
	field = document.getElementById('amount_' + artikelcode)
	if (field.value != '') {
		field.value = field.value.replace(/[^0-9]/g,"");
		aantal = field.value
	} else {
		aantal = 0
	}
	
		ajaxRequest('')
		xmlhttp2.open("GET","getbasket.asp?change=" + artikelcode + "&atl=" + aantal,true);
		xmlhttp2.send(null);
		updateCartTotals()	
		
		
		
//		frm = document.winkelmandje
//		var subtotal = 0
//		for (var i=0; i < frm.length; i++) {
//			form_field = frm.elements[i]
//			form_id = form_field.id;
//			
//			if (form_id.substring(0,7) == "amount_") {
//				artcode = form_id.substring(7)
//				if (form_field.value == '') {
//					item_quantity = 0
//				} else {
//					item_quantity = parseInt(form_field.value);
//				}	
//				artikelprijs = (document.getElementById('price_' + artcode).value).replace(",",".");
//				document.getElementById('total_' + artcode).innerHTML = formatBedrag(artikelprijs * item_quantity);
//				subtotal = subtotal + (artikelprijs * item_quantity);
//			}
//		}
//
//		document.getElementById('sub').innerHTML = formatBedrag(subtotal)
//		
//		verzendkosten = parseFloat(document.getElementById('verzend').innerHTML.replace(",","."))
//		eindtot = subtotal + verzendkosten
//		document.getElementById('totaal').innerHTML = formatBedrag(eindtot)
//		frm.totaal.value = formatBedrag(eindtot)
}


function updateCartTotals() {
	frm = document.winkelmandje
	var subtotal = 0
	var verzendkosten = 0
	var aantal = 0
	for (var i=0; i < frm.length; i++) {
		form_field = frm.elements[i]
		form_id = form_field.id;
		
		if (form_id.substring(0,7) == "amount_") {
			artcode = form_id.substring(7)
			if (form_field.value == '') {
				item_quantity = 0
			} else {
				item_quantity = parseInt(form_field.value);
			}	
			if (item_quantity > 0) {
				aantal = 1
			}
			artikelprijs = (document.getElementById('price_' + artcode).value).replace(",",".");
			document.getElementById('total_' + artcode).innerHTML = formatBedrag(artikelprijs * item_quantity);
			subtotal = subtotal + (artikelprijs * item_quantity);
		}
	}
	document.getElementById('sub').innerHTML = formatBedrag(subtotal)
	if (aantal == 0 || (parseFloat(frm.franco.value.replace(",",".")) <= subtotal && parseFloat(frm.franco.value.replace(",",".")) > 0 )) {
		verzendkosten = 0
		document.getElementById('verzend').innerHTML = formatBedrag(0)
	} else {	
		verzendkosten = parseFloat(frm.transport.value.replace(",","."))
		document.getElementById('verzend').innerHTML = formatBedrag(verzendkosten)
	}	
	eindtot = subtotal + verzendkosten
	document.getElementById('totaal').innerHTML = formatBedrag(eindtot)
	frm.totaal.value = formatBedrag(eindtot)
	checkFields()
}

function wijzigbetaal() {
//	if (document.getElementById('betaal1').checked) {
//		document.getElementById('banken').style.display = 'block'
//	} else {
//		document.getElementById('banken').style.display = 'none'
//	}
//	wijzigwijze()	
	updateCartTotals()
}

function wijzigwijze() {
	if (document.getElementById('lever2').checked) {
		verzendkosten = 0
	} else {
		verzendkosten = parseFloat(document.winkelmandje.transport.value.replace(",","."))
	}
	var totaal;	
	document.getElementById('verzend').innerHTML = formatBedrag(verzendkosten)
	totaal = parseFloat(document.getElementById('sub').innerHTML.replace(",",".")) + verzendkosten
	document.getElementById('totaal').innerHTML = formatBedrag(totaal)
	document.winkelmandje.totaal.value = formatBedrag(totaal)
}



function checkSpecial(){
	return true
}

function checkFields() {
	fout = false;
	if (document.winkelmandje.naam.value == '')     fout = true;
	if (document.winkelmandje.adres.value == '')    fout = true;
	if (document.winkelmandje.postcode.value == '') fout = true;
	if (document.winkelmandje.plaats.value == '')   fout = true;
	if (document.winkelmandje.telefoon.value == '') fout = true;
	if (document.winkelmandje.postcode.value == '') fout = true;
	if (document.winkelmandje.email.value == '')    fout = true;
	if (!document.winkelmandje.akkoord.checked)     fout = true;
	
	if (!fout && document.winkelmandje.banknummer[0]) {
		if (!fout) {
			fout = true
			for (i=0; i<document.winkelmandje.banknummer.length; i++) {
				 if (document.winkelmandje.banknummer[i].checked) fout = false
			}
		}
	}
	if (fout) {
		document.winkelmandje.verzenden.disabled = 'disabled'
	} else {
		if (parseFloat(document.getElementById('sub').innerHTML.replace(",",".")) > 0) {
			document.winkelmandje.verzenden.disabled = ''
		} else { 
			document.winkelmandje.verzenden.disabled = 'disabled'
		}	
	}
	
}

function checkPostcode() {
	re = /^[1-9][0-9]{3} ?[a-zA-Z]{2}$/
	invoer = document.winkelmandje.postcode.value
	if (invoer != '') {
		OK = re.exec(invoer);
		if (OK) {
			document.winkelmandje.postcode.value = invoer.substr(0,4) + " " + invoer.substr(invoer.length - 2,2).toUpperCase();
		} else {
			alert (invoer + " is geen geldige Nederlandse postcode!");
			document.winkelmandje.postcode.focus()
		}
	} 
}

function checkEmail() {
	re = /^[A-Za-z0-9_\-]+([.][A-Za-z0-9_\-]+)*[@][A-Za-z0-9_\-]+([.][A-Za-z0-9_\-]+)+$/
	if (document.winkelmandje.email.value != '') {
		if (!re.exec(document.winkelmandje.email.value)) {
			alert('Fout in emailadres')
			document.winkelmandje.email.focus()
		}
	}
}

function formatBedrag(invoer){
	invoer = Math.round(invoer * 100) / 100
	var test = invoer.toString()
	deel = test.split('.')
	if (deel[1]) {
		if (deel[1].length == 1) {
			bedrag = test + '0'
		} else {
			bedrag = test
		}
	} else {
		bedrag = test + '.00'
	}
	bedrag = bedrag.replace(",","")
	bedrag = bedrag.replace(".",",")
	return bedrag
}

function naarHome() {
	location.href = '/inge/'
}

function verder() {
//	if (document.getElementById('vooruit').checked || document.getElementById('annuleren').checked) {
//		if (document.getElementById('annuleren').checked) {
			location.href = '/inge/div/annuleer/'
//		} else {
//			location.href = '/inge/div/vooruit/' + document.getElementById('referentie').value + '/'
//		}
//	} else {
//		alert('Geen keuze gemaakt')
//	}
}


