﻿// AJAX VOOR OFFERTE-AANVRAAG

function createRequestObject() {
	var req;
	
	if	(window.XMLHttpRequest)	{
		//For Firefox, Safari, Opera
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		//For IE 5+
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		//Error for an old browser
		alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
	}
	return req;
}
	
//Make the XMLHttpRequest Object
var http = createRequestObject();

function showCategories() {
	var chosenProductgroup = document.getElementById("productgroupPulldown").value;
	var myRandom = parseInt(Math.random()*99999999); // cache buster
	
	http.open("get", "offerteaanvraag/productgroupResponse.php?productgroup=" + chosenProductgroup + "&rand=" + myRandom, true);
	http.onreadystatechange = handleCategoriesResponse;
	http.send(null);
}
	
function handleCategoriesResponse() {
	if (http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		if (response) {
			document.getElementById("category").innerHTML = response;
			document.getElementById("product").innerHTML = '';
			document.getElementById("measurements").innerHTML = '';
			document.getElementById("amount").innerHTML = '';
			document.getElementById("submitbutton").innerHTML = '';
		}
	}
}

function showProducts() {
	var chosenProductgroup = document.getElementById("productgroupPulldown").value;
	var chosenCategory = document.getElementById("categoriesPulldown").value;
	var myRandom = parseInt(Math.random()*99999999); // cache buster
	
	http.open("get", "offerteaanvraag/categoryResponse.php?productgroup=" + chosenProductgroup + "&category=" + chosenCategory + "&rand=" + myRandom, true);
	http.onreadystatechange = handleProductsResponse;
	http.send(null);
}
	
function handleProductsResponse() {
	if (http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		if (response) {
			document.getElementById("product").innerHTML = response;
			document.getElementById("measurements").innerHTML = '';
			document.getElementById("amount").innerHTML = '';
			document.getElementById("submitbutton").innerHTML = '';
		}
	}
}

function showMeasurements() {
	var chosenProduct = document.getElementById("productPulldown").value;
	var myRandom = parseInt(Math.random()*99999999); // cache buster
	
	http.open("get", "offerteaanvraag/productResponse.php?product=" + chosenProduct + "&rand=" + myRandom, true);
	http.onreadystatechange = handleMeasurementsResponse;
	http.send(null);
}
	
function handleMeasurementsResponse() {
	if (http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		if (response) {
			document.getElementById("measurements").innerHTML = response;
			document.getElementById("amount").innerHTML = '';
			document.getElementById("submitbutton").innerHTML = '';
			
		}
	}
}

function showAmount() {
	var chosenMeasurements = document.getElementById("measurementsPulldown").value;
	var myRandom = parseInt(Math.random()*99999999); // cache buster
	
	http.open("get", "offerteaanvraag/measurementsResponse.php?measurements=" + chosenMeasurements + "&rand=" + myRandom, true);
	http.onreadystatechange = handleAmountResponse;
	http.send(null);
}
	
function handleAmountResponse() {
	if (http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		if (response) {
			document.getElementById("amount").innerHTML = response;
			document.getElementById("submitButton").innerHTML = '';
		}
	}
}

function showSubmitbutton() {
	var chosenAmount = document.getElementById("amount").value;
	var chosenDescription = document.getElementById("description").value;
	
	var myRandom = parseInt(Math.random()*99999999); // cache buster
	
	http.open("get", "offerteaanvraag/submitbuttonResponse.php?amount=" + chosenAmount + "&description=" + chosenDescription + "&rand=" + myRandom, true);
	http.onreadystatechange = handleSubmitbuttonResponse;
	http.send(null);
}
	
function handleSubmitbuttonResponse() {
	if (http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		if (response) {
			document.getElementById("submitbutton").innerHTML = response;
		}
	}
}

function checkForm() {
	// First the normal form validation
	if (document.form.amount.value == '') {
		alert('Vul een aantal in');
		document.form.amount.focus();
		return false;
	} else {
		var chosenProductgroup = document.getElementById("productgroupPulldown").value;
		var chosenCategory = document.getElementById("categoriesPulldown").value;
		var chosenProduct = document.getElementById("productPulldown").value;
		var chosenMeasurements = document.getElementById("measurementsPulldown").value;
		var chosenAmount = document.form.amount.value;
		var chosenDescription = document.form.description.value;
		
		var myRandom = parseInt(Math.random()*99999999); // cache buster
		
		http.open("get", "offerteaanvraag/addItem.php?productgroup=" + chosenProductgroup + "&category=" + chosenCategory + "&product=" + chosenProduct + "&measurements=" +chosenMeasurements + "&amount=" +  chosenAmount + "&description=" + chosenDescription + "&rand=" + myRandom, true);
		http.onreadystatechange = handleCheckFormResponse;
		http.send(null);
	}
}

function handleCheckFormResponse() {
	if (http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		if (response) {
			document.getElementById("offerteLijst").innerHTML = response;
		}
	}
}

function editItem(index, action) {
	var myRandom = parseInt(Math.random()*99999999); // cache buster
	
	//het product dat we tot dan toe aan het toevoegen zijn wordt verborgen
	//document.getElementById("offerteLijstItem_" + index).innerHTML = '';
	
	http.open("get", "offerteaanvraag/addItem.php?index=" + index + "&action=" + action + "&rand=" + myRandom, true);
	
	http.onreadystatechange = handleEditItemResponse;
	http.send(null);
}
	
function handleEditItemResponse() {
	if (http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		if (response) {
			var index = readCookie('index');
			
			document.getElementById("offerteLijst").innerHTML = response;
		}
	}
}

function deleteItem(index, action) {
	var myRandom = parseInt(Math.random()*99999999); // cache buster
	
	eraseCookie('offerte' + index + '');
	
	http.open("get", "offerteaanvraag/addItem.php?rand=" + myRandom, true);
	
	http.onreadystatechange = handleDeleteItemResponse;
	http.send(null);
}
	
function handleDeleteItemResponse() {
	if (http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		if (response) {
			document.getElementById("offerteLijst").innerHTML = response;
		}
	}
}

function showUnits() {
	var myRandom = parseInt(Math.random()*99999999); // cache buster
	
	var id = document.getElementById("measurable_feature").value;
	
	http.open("get", "plaatmateriaal/getUnits.php?rand=" + myRandom + "&id=" + id, true);
	
	http.onreadystatechange = handleShowUnits;
	http.send(null);
}

function handleShowUnits() {
	if (http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		if (response) {
			document.getElementById("unit").innerHTML = response;
		}
	}
}

function checkSearchForm() {
	// First the normal form validation
	if ((document.form.measurable_feature.value !== '') && (document.form.enteredValue.value == '')) {
		document.getElementById("warning").innerHTML = "U dient een waarde voor het meetbare kenmerk in te voeren";
		document.form.enteredValue.focus();
		return false;
	} else {
		document.getElementById("warning").innerHTML = "";
		document.form.submit();
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for	(var i=0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
	