//Functies voor de shop, in deze volgorde worden ze ook aangeroepen
//als je gewoon de shop volgt. Onder deze lijst staan andere functies die nog
//nodig zijn.
function startShop() {
	showLoading();
	$.ajax({
		type: "GET",
		url: "ajax/shop.php?module=customer&action=edit",
		cache: false,
		success: function(data) {
			data += "<div id=\"poweredby\"><a target=\"_blank\" href=\"http://www.gmweb.nl\"><img src=\"images/powered.png\" /></a></div>";
			$("#shop").html(data);
		},
		complete: function() {
			hideLoading();
		}
	});
}

function saveCustomer(form) {
	if (!checkFormValues(form)) {
		showMessage("Niet alle velden zijn correct ingevuld. Check alstublieft de waarde van de velden met een rode rand.");
		return;
	}
	var postVars = makePostString(form);
	showLoading();
	$.ajax({
		type: "POST",
		url: "ajax/shop.php?module=customer&action=save",
		cache: false,
		data: postVars,
		success: function(data) {
			if (data == 1) {
				//getProducts();
				getOrdertype();
			} else {
				showMessage(data);
			}
		},
		complete: function() {
			hideLoading();
		}
	});
}

function getOrdertype() {
	showLoading();
	$.ajax({
		url: "ajax/shop.php?module=shop&action=getOrderType",
		cache: false,
		success: function(data) {
			data += "<div id=\"poweredby\"><a target=\"_blank\" href=\"http://www.gmweb.nl\"><img src=\"images/powered.png\" /></a></div>";
			$("#shop").html(data);
			//getCart();
		},
		complete: function() {
			hideLoading();
		}
	});
}

function saveOrdertype() {
	var category = "";
	if(document.getElementById("huur_ordertype").checked){
		category = "baan";
	} else if(document.getElementById("invite_ordertype").checked){
		category = "uitgenodigd";
	} else if(document.getElementById("lm_ordertype").checked){
		category = "lidmaatschap";
	} else if(document.getElementById("prive_ordertype").checked){
		category = "prive";
	} else if(document.getElementById("indelen_ordertype").checked){
		category = "indelen";
	} else {
		showMessage("Selecteer een van de opties voordat u verder gaat.");
		return;
	}

	var vars = "category="+category;
	showLoading();
	$.ajax({
		url: "ajax/shop.php?module=shop&action=saveOrdertype",
		cache: false,
		type: "POST",
		data: vars,
		success: function(data) {
			if(data =="1"){
				getOrderdetails();
			} else {
				showMessage(data);
			}
		},
		complete: function() {
			hideLoading();
		}
	});
}

function getOrderdetails(){
	showLoading();
	$.ajax({
		url: "ajax/shop.php?module=shop&action=getOrderdetails",
		cache: false,
		success: function(data) {
			data += "<div id=\"poweredby\"><a target=\"_blank\" href=\"http://www.gmweb.nl\"><img src=\"images/powered.png\" /></a></div>";
			$("#shop").html(data);
		},
		complete: function() {
			hideLoading();
		}
	});
}

function saveOrderdetails(form){
	var postVars = makePostString(form);
	showLoading();
	$.ajax({
		type: "POST",
		url: "ajax/shop.php?module=shop&action=saveOrderdetails",
		cache: false,
		data: postVars,
		success: function(data) {
			if (data == 1) {
				getCart();
				//als de cart eenmaal is weergegeven moeten alle inputvelden
				//gedisabled worden, pas nadat de klant dan weer op de knop
				//"NEE" heeft geklikt mag ie weer dingen doen
				disableInputs();
			} else {
				showMessage(data);
			}
		},
		complete: function() {
			hideLoading();
		}
	});
}

function getCart(){
	showLoading();
	$.ajax({
		url: "ajax/shop.php?module=cart&action=getCart",
		cache: false,
		success: function(data) {
			$("#cart").html(data);
		},
		complete: function() {
			hideLoading();
		}
	});
}

function getPayment(){
	showLoading();
	$.ajax({
		url: "ajax/shop.php?module=shop&action=getPayment",
		cache: false,
		success: function(data) {
			data += "<div id=\"poweredby\"><a target=\"_blank\" href=\"http://www.gmweb.nl\"><img src=\"images/powered.png\" /></a></div>";
			$("#shop").html(data);
		},
		complete: function() {
			hideLoading();
		}
	});
}

function savePayment(form){
	if (!checkFormValues(form)) {
		showMessage("Niet alle velden zijn correct ingevuld. Check alstublieft de waarde van de velden met een rode rand.");
		return;
	}
	var postVars = makePostString(form);
	showLoading();
	$.ajax({
		type: "POST",
		url: "ajax/shop.php?module=shop&action=savePayment",
		cache: false,
		data: postVars,
		success: function(data) {
			if (data.substring(0, 9) == "idealurl:") {
				document.location = data.substring(9);
			} else {
				showMessage(data);
			}
		},
		complete: function() {
			hideLoading();
		}
	});
}

//deze functie wordt aangeroepen als de totaalprijs van de mand 0 euro
//is. Er wordt dan evengoed savePayment aangeroepen
function skipPayment(){
	showLoading();
	$.ajax({
		type: "POST",
		url: "ajax/shop.php?module=shop&action=savePayment",
		cache: false,
		data: postVars,
		success: function(data) {
			if (data.substring(0, 6) == "error:") {
				showMessage(data.substring(6));
			} else {
				//het is goed gegaan, we hebben een getal,
				//geeft het overview weer (finish)
				finish('', data);
			}
		},
		complete: function() {
			hideLoading();
		}
	});
}

function finish(trxid, orderid){
	var url = "ajax/shop.php?module=finish&";
	var doIdealReport = false;
	var idealReportUrl = "";
	if(trxid != ''){
		url += "transaction_id=" + trxid;
	} else {
		url += "order_id=" + orderid;
		idealReportUrl = "ideal/report.php?order_id=" + orderid;
		doIdealReport = true;
	}

	$.ajax({
		type: "GET",
		url: url,
		cache: false,
		success: function(data) {
			data += "<div id=\"poweredby\"><a target=\"_blank\" href=\"http://www.gmweb.nl\"><img src=\"images/powered.png\" /></a></div>";
			$("#shop").html(data);
		},
		complete: function() {
			hideLoading();
		}
	});

	if(doIdealReport){
		//dit betekent dat er niet met iDEAL betaald is. Dus dan wordt report.php
		//ook niet aangeroepen. Dat doen we dan gewoon maar nu met AJAX!
		$.ajax({
			type: "GET",
			url: idealReportUrl,
			cache: false,
			success: function(data) {
				///hoeft geen actie
			}
		});
	}
}

function logout(){
	showLoading();
	$.ajax({
		type: "GET",
		url: "ajax/shop.php?module=logout",
		cache: false,
		success: function(data) {
			window.location = 'http://www.casparfagellaan.nl';
		},
		complete: function() {
			hideLoading();
		}
	});
}

//Verdere functies voor spelergegevens
function toggleLevel(el){
	if(el.checked){
		document.getElementById("levelselect").style.display = "block";
	} else {
		document.getElementById("levelselect").style.display = "none";
	}
}

//functie die na het selecteren van een club eventueel opties uit de
//dropdown van lestypes (on-)zichtbaar maakt
function updateClub(el){
	var selClub = el[el.selectedIndex].value;
	
	if(selClub == 166){
		if(!$(".kappeLes").is(":visible")){
			document.getElementById("les_type").selectedIndex = 0;
		}
		$(".normaalLes").hide();
		$(".kappeLes").show();
	} else {
		if($(".kappeLes").is(":visible")){
			document.getElementById("les_type").selectedIndex = 0;
		}
		$(".normaalLes").show();
		$(".kappeLes").hide();
	}
}

//functie die een popup laat zien met bericht over wat een club precies inhoudt
var msg;
function showClubInfo(el){
	var selClub = el[el.selectedIndex].value;
	msg = "";
	if(selClub == 1){
		//junior club
		msg = "Noodzakelijk voor tennisles. Vrij spelen op zaterdagmiddag van 14:00 - 17:00. ";
	} else if(selClub == 3){
		//small club == weekend club
		msg = "Speelt in het weekeinde van 9.00 uur 's morgens tot donker.";
	} else if(selClub == 94){
		//dag
		msg = "Speelt 's ochtends op maandag, dinsdag, donderdag en vrijdag.";
	}  else if(selClub == 165){
		//caspar club
		msg = "Caspar Club is de combinatie van Dag Club + Weekend Club.";
	}  else if(selClub == 166){
		//Kappe
		msg = "Kappe training is een tenniscursus op maandag-, dinsdag-, donderdag- en vrijdagavond.";
	}

	if(msg != ""){
		setTimeout("showMessage(msg)", 100);
		setTimeout("hideMessage()", 2000);
	}
	return false;
}
//Verdere functies voor baanhuur
function updateHuurDag(){
	$("#huur_tijd_div").html("");
	var huurDag = escape(document.getElementById("huur_dag")[document.getElementById("huur_dag").selectedIndex].value);
	if(huurDag != -1) {
		showLoading();
		$.ajax({
			url: "ajax/shop.php?module=gethuurtijd&huurdag="+huurDag,
			cache: false,
			success: function(data) {
				$("#huur_tijd_div").html(data);
			},
			complete: function() {
				hideLoading();
			}
		});
	}
}

function updateHuurTijd(){
	var el = document.getElementById('huur_productid');
	var productid = el[el.selectedIndex].value;
	if(productid != -1){
		showConfirmButton();
	} else {
		hideConfirmButton();
	}
}

//Verder functies voor uitgenodig voor lesgroep
function updateLesgroepSelect(){
	var el = document.getElementById('lesgroep');
	var productid = el[el.selectedIndex].value;
	if(productid != -1){
		showConfirmButton();
	} else {
		hideConfirmButton();
	}
}

//Verdere functie voor louter lidmaatschap
function updateLidmaatschap(){
	var el = document.getElementById('club');
	var lidmid = el[el.selectedIndex].value;
	if(lidmid != -1){
		showConfirmButton();
	} else {
		hideConfirmButton();
	}
}

//verdere functies voor privegroep opzetten
function updateLesDag(){
	showLoading();
	var lesDag = escape(document.getElementById("les_dag")[document.getElementById("les_dag").selectedIndex].value);
	if(lesDag != -1) {
		showLoading();
		$.ajax({
			url: "ajax/shop.php?module=getlestijd&lesdag="+lesDag,
			cache: false,
			success: function(data) {
				$("#les_tijd_div").html(data);
			},
			complete: function() {
				hideLoading();
			}
		});
	} else {
		$("#les_tijd_div").html("");
	}
}

function updateLesTijd(){
	showLoading();
	var productId = escape(document.getElementById("les_productid")[document.getElementById("les_productid").selectedIndex].value);
	if(productId != -1){
		showLoading();
		$.ajax({
			url: "ajax/shop.php?module=updatelestijd&productid="+productId,
			cache: false,
			dataType: 'json',
			success: function(data) {
				$("#les_extra_div").html(data[0]);
				showConfirmButton();
			},
			complete: function() {
				hideLoading();
			}
		});
	} else {
		$("#les_extra_div").html('');
		hideConfirmButton();
	}
}

function toggleTeacherName(){
	if(!document.getElementById("ownteacher").checked){
		document.getElementById("teachernamespan").style.display = "none";
	} else {
		document.getElementById("teachernamespan").style.display = "block";
	}
}

//verdere functies voor ingedeeld worden
function updateLesType(){
	var el = document.getElementById('les_type');
	var productid = el[el.selectedIndex].value;
	if(productid != -1){
		showConfirmButton();
	} else {
		hideConfirmButton();
	}
}

function updateComment(){
	$.ajax({
		url: "ajax/shop.php?module=getcomment",
		cache: false,
		success: function(data) {
			$("#comment_area").html(data);
		},
		complete: function() { }
	});
}

//Basisfuncties voor de shop
function showShop() {
	$("#shop").height(($(window).height() - 130) + "px");
	$("#overlay").fadeIn("fast", function() {
		$("#overlay").css("opacity", "0.4");
		$("#overlay").css("filter:", "alpha(opacity=40);");
	});
	$("#shop").fadeIn("fast");
	$("#close").fadeIn("fast");
}

function hideShop() {
	$("#overlay").fadeOut("fast");
	$("#shop").fadeOut("fast");
	$("#loader").fadeOut("fast");
	$("#message").fadeOut("fast");
	$("#close").fadeOut("fast");
}

function showMessage(message) {
	$("#message").html(message);
	$("#message").fadeIn("fast");
}

function hideMessage() {
	if ($("#message").is(":visible")) {
		$("#message").html("");
		$("#message").fadeOut("fast");
	}
}

function showLoading() {
	$("#loader").show();
}

function hideLoading() {
	$("#loader").hide();
}

//Deze functies laat de bevestigingsknop zien of niet!.
function showConfirmButton(){
	$("#confirm_div").html('<input type="submit" name="actie" value="Zijn bovenstaande gegevens correct?" />');
}
function hideConfirmButton(){
	$("#confirm_div").html('');
}

//Functies om alle inputs op de website te disabelen en weer te enabelen
function disableInputs(){
	$(':input, select').each(function(){
		if(this.type == 'submit' || this.type == 'button'){
			this.style.display = 'none';
		} else {
			this.disabled = true;
		}
	});
}

function enableInputs(){
	//De cart wordt hier verborgen
	$("#cart").html("");
	$(':input, select').each(function(){
		if(this.type == 'submit' || this.type == 'button'){
			this.style.display = 'block';
		} else {
			this.disabled = false;
		}
	});
}
//EINDE basisfuncties

