﻿//SCRIPTS VOOR SCHERMGROOTTE EN RESIZING

window.onresize = function () {
	getscreensize();
}

function getscreensize() {
	
	if (window.innerWidth) {
		document.getElementById("availWidth").innerHTML = window.innerWidth;
		myWidth = window.innerWidth - 128;
	} else if (document.documentElement.clientWidth) {
		document.getElementById("availWidth").innerHTML = document.documentElement.clientWidth;
		myWidth = document.documentElement.clientWidth - 128;
	} else {
		myWidth = 800;
	}
	
	if (window.innerHeight) {
		document.getElementById("availHeight").innerHTML = window.innerHeight;
		myHeight = window.innerHeight - 64;
	} else if (document.documentElement.clientHeight) {
		document.getElementById("availHeight").innerHTML = document.documentElement.clientHeight;
		myHeight = document.documentElement.clientHeight - 64;
	} else {
		myHeight = 528;
	}
	
	createCookie('screenWidth', '960', 1);
	createCookie('screenHeight', myHeight, 1);
}


//geheel of gedeeltelijk tonen van de content 'mnarktinformatie'

function showContent(div) {
	
	$(document).ready(function(){
	
		$("#" + div + "").css("height", "auto");
		
		var string = '<a href="#" onclick="hideContent(\'' + div + '\')" onmouseover="verberg.src=\'img/verberg_h.gif\'" onmouseout="verberg.src=\'img/verberg.gif\'">';
		string += '<img src="img/verberg.gif" width="656" height="24" border="0" alt="verbergen" name="verberg" />';
		string += '</a>';
		
		$("#wide_button").html(string);
	
	});
}

function hideContent(div) {
	
	$(document).ready(function(){
	
		$("#" + div + "").css("height", "");
		
		var string = '<a href="#" onclick="showContent(\'' + div + '\')" onmouseover="lees_verder.src=\'img/lees_verder_h.gif\'" onmouseout="lees_verder.src=\'img/lees_verder.gif\'">';
		string += '<img src="img/lees_verder.gif" width="656" height="24" border="0" alt="lees verder" name="lees_verder" />';
		string += '</a>';
		
		$("#wide_button").html(string);
	
	});
	
}

function loadImage(div, image, alt) {
	
	$(document).ready(function() {
							   
		var image_string = '<img src="' + image + '" alt="' + alt +'" border="0" id="image" style="margin: auto;" />';
		
		$("#" + div + "").fadeOut(100, function() {
		
			$("#" + div + "").html(image_string).hide().fadeIn(250);
			
	  	});
		
	});
}

function mijnVraag() {
	
	$(document).ready(function() {
		
		//formulier controleren
		var i = 0;
		
		if ($("#vraag").val() == "") {
			$("#vraag").css("border", "1px solid #e00");
			$("#vraag_asterisk").css("color", "#e00");
			i++;
		} else {
			$("#vraag").css("border", "");
			$("#vraag_asterisk").css("color", "");
		}
		
		
		if ($("#bedrijfsnaam").val() == "") {
			$("#bedrijfsnaam").css("border", "1px solid #e00");
			$("#bedrijfsnaam_asterisk").css("color", "#e00");
			i++;
		} else {
			$("#bedrijfsnaam").css("border", "");
			$("#bedrijfsnaam_asterisk").css("color", "");
		}
		
		
		if ($("#achternaam").val() == "") {
			$("#achternaam").css("border", "1px solid #e00");
			$("#achternaam_asterisk").css("color", "#e00");
			i++;
		} else {
			$("#achternaam").css("border", "");
			$("#achternaam_asterisk").css("color", "");
		}
		
		
		if ($("#email").val() == "") {
			$("#email").css("border", "1px solid #e00");
			$("#email_asterisk").css("color", "#e00");
			i++;
		} else {
			$("#email").css("border", "");
			$("#email_asterisk").css("color", "");
		}
		
		//e-mail verificatie
		var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
		
		if (i > 0) { //alle benodigde velden zijn ingevuld
			$("#alert").html("<p class='alert'>U heeft nog niet alle benodigde gegevens ingevuld</p>");
		} else if (document.form.email.value.search(emailRegEx) < 0) { //het e-mail adres is niet correct
			$("#alert").html("<p class='alert'>U heeft geen geldig e-mail adres ingevuld</p>");
			
			$("#email").css("border", "1px solid #e00");
			$("#email_asterisk").css("color", "#e00");
		} else {
			document.form.submit();
		}
		
	});
}


//OFFERTEAANVRAAG

function showCategories() {
	
	$(document).ready(function() {
		
		var myRandom = parseInt(Math.random()*99999999); // cache buster
		var chosenProductgroup = $("#productgroupPulldown").val();
		
		$.ajax({
			type: "get",
			url: "../offerteaanvraag/productgroupResponse.php",
			data: "productgroup=" + chosenProductgroup + "&rand=" + myRandom,
			success: function (response) {
				$("#category").html(response);
				$("#product").html("");
				$("#measurements").html("");
			}
		
		});
	});
}

function showProducts() {
	
	$(document).ready(function() {
		
		var myRandom = parseInt(Math.random()*99999999); // cache buster
		var chosenProductgroup = $("#productgroupPulldown").val();
		var chosenCategory = $("#categoriesPulldown").val();
		
		$.ajax({
			type: "get",
			url: "../offerteaanvraag/categoryResponse.php",
			data: "productgroup=" + chosenProductgroup + "&category=" + chosenCategory + "&rand=" + myRandom,
			success: function (response) {
				$("#product").html(response);
				$("#measurements").html("");
			}
		
		});
	});
}

function showMeasurements() {
	
	$(document).ready(function() {
		
		var myRandom = parseInt(Math.random()*99999999); // cache buster
		var chosenProduct = $("#productPulldown").val();
		
		$.ajax({
			type: "get",
			url: "../offerteaanvraag/productResponse.php",
			data: "product=" + chosenProduct + "&rand=" + myRandom,
			success: function (response) {
				$("#measurements").html(response);
				showAmount();
			}
		
		});
	});
}

function showAmount() {
	
	$(document).ready(function() {
		
		var myRandom = parseInt(Math.random()*99999999); // cache buster
		var chosenMeasurements = $("#measurementsPulldown").val();
		
		$.ajax({
			type: "get",
			url: "../offerteaanvraag/measurementsResponse.php",
			data: "measurements=" + chosenMeasurements + "&rand=" + myRandom,
			success: function (response) {
				$("#amount_description").html(response);
			}
		
		});
	});
}

function showSubmitbutton() {
	
	$(document).ready(function() {
		
		var myRandom = parseInt(Math.random()*99999999); // cache buster
		var chosenAmount = $("#amount").val();
		var chosenDescription = $("#description").val();
		
		$.ajax({
			type: "get",
			url: "../offerteaanvraag/submitbuttonResponse.php",
			data: "amount=" + chosenAmount + "&description=" + chosenDescription + "&rand=" + myRandom,
			success: function (response) {
				$("#submitbutton").html(response);
			}
		
		});
	});
}

function checkForm(index) {
	
	if ($("#amount").val() == '') {
		$("#amount").css("border", "1px solid #e00");
		$("#amount_alert").css("visibility", "visible");
		document.form.amount.focus();
		return false;
	} else {
		var chosenProductgroup = $("#productgroupPulldown").val();
		var chosenCategory = $("#categoriesPulldown").val();
		var chosenProduct = $("#productPulldown").val();
		var chosenMeasurements = $("#measurementsPulldown").val();
		var chosenAmount = $("#amount").val();
		var chosenDescription = $("#description").val();
		
		var myRandom = parseInt(Math.random()*99999999); // cache buster
		
		$.ajax({
			type: "get",
			url: "../offerteaanvraag/addItem.php",
			data: "productgroup=" + chosenProductgroup + "&category=" + chosenCategory + "&product=" + chosenProduct + "&measurements=" + chosenMeasurements + "&amount=" +  chosenAmount + "&description=" + chosenDescription + "&index=" + index + "&rand=" + myRandom,
			success: function (response) {
				$("#offerteLijst").html(response);
			}
		
		});
	}
}

function editItem(index, action) {
	
	$(document).ready(function() {
		
		var myRandom = parseInt(Math.random()*99999999); // cache buster
		
		$.ajax({
			type: "get",
			url: "../offerteaanvraag/addItem.php",
			data: "index=" + index + "&action=" + action + "&rand=" + myRandom,
			success: function (response) {
				$("#offerteLijst").html(response);
			}
		
		});
	});
}

function deleteItem(index) {
	
	$(document).ready(function() {
		
		var myRandom = parseInt(Math.random()*99999999); // cache buster
		
		$.ajax({
			type: "get",
			url: "../offerteaanvraag/addItem.php",
			data: "index=" + index + "&action=defdelete&rand=" + myRandom,
			success: function (response) {
				$("#offerteLijst").html(response);
			}
		
		});
	});
}

function sendOfferte() {
	
	$(document).ready(function() {
							   
		//aantal items in de offertelijst controleren
		var offerteNum = readCookie('offerteNum');
		offerteNum = offerteNum / 1;
		
		if (offerteNum < 1) {
			//er staan geen items in de offertelijst
			$("#alert").html("<p class='alert'>De offertelijst is nog leeg</p>");
		
		} else {
			
			$("#alert").html("");
		
			//formulier controleren
			var i = 0;
			
			if ($("#bedrijfsnaam").val() == "") {
				$("#bedrijfsnaam").css("border", "1px solid #e00");
				$("#bedrijfsnaam_asterisk").css("color", "#e00");
				i++;
			} else {
				$("#bedrijfsnaam").css("border", "");
				$("#bedrijfsnaam_asterisk").css("color", "");
			}
			
			if ($("#achternaam").val() == "") {
				$("#achternaam").css("border", "1px solid #e00");
				$("#achternaam_asterisk").css("color", "#e00");
				i++;
			} else {
				$("#achternaam").css("border", "");
				$("#achternaam_asterisk").css("color", "");
			}

			if ($("#email").val() == "") {
				$("#email").css("border", "1px solid #e00");
				$("#email_asterisk").css("color", "#e00");
				i++;
			} else {
				$("#email").css("border", "");
				$("#email_asterisk").css("color", "");
			}
			
			//e-mail verificatie
			var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
			
			if (i > 0) { //alle benodigde velden zijn ingevuld
				$("#alert_2").html("<p class='alert'>U heeft nog niet alle benodigde gegevens ingevuld</p>");
			} else if (document.form.email.value.search(emailRegEx) < 0) { //het e-mail adres is niet correct
				$("#alert_2").html("<p class='alert'>U heeft geen geldig e-mail adres ingevuld</p>");
				
				$("#email").css("border", "1px solid #e00");
				$("#email_asterisk").css("color", "#e00");
			} else {
				document.form.submit();
			}
			
		}
		
	});
}

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);
}
	
