// JavaScript Document

$(function(){
	// Dialog			
	$('#publieksreactie-a-modo').dialog({
		autoOpen: false,
		width: 450,
		modal: true,
		resizable: false,
		title: "Geef uw reactie",
		buttons: {
			"Annuleren": function() { 
				$(this).dialog("close"); 
			}, 
			"Verzenden": function() { 
				submitForm();
			} 
		}
	});
	
	// Dialog Link
	$('#publieksreactielink').click(function(){
		getProducties();
		$('#publieksreactie-a-modo').dialog('open');
		$('#naam').val('');
		$('#reactie').val('');
		return false;
	});
/*
	//hover states on the static widgets
	$('#dialog_link, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
*/	
});

function showErrors(t){
	$("#errors").empty();
	$("#errors").append(t);
}

function submitForm(){
	$("#errors").empty();
	fValid = true;
	if($("#reactie").val() == ''){
		showErrors("Er is geen reactie ingevoerd.");
		fValid = false;
	}

	if(fValid){
		postReactie();
	}
}

function getProducties(){
//	$("#loading").show();
	$.ajax(
		{	type: "GET",
			url: "http://www.bonheur.nl/scripts/getdata.php",
			cache: false,
			data: {soort:"producties"},
			dataType: "json",
			error: dataError,
			success: getProductiesResult
		}
	);
};

/*
aanhef:$("#aanhef").val()
,voorletters:$("#voorletters").val(),
,voornaam:$("#voornaam").val()
,tussenvoegsels:$("#tussenvoegsels").val()
,achternaam:$("#achternaam").val()
,email:$("#email").val()
,bedrijfsnaam:$("#bedrijfsnaam").val()
,adres:$("#adres").val()
,postcode:("#postcode").val()
,plaats:("#plaats").val()
,land:("#land").val()
,voorstelling:("#voorstelling").val()
,reactie:("#reactie").val()
*/
function postReactie(){
	var curTime = new Date();
/*
	$.ajax(
		{	type: "GET",
			url: "http://www.bonheur.nl/scripts/postdata.php",
			cache: false,
			data: $("#reactieform").serialize()+"tijd="+curTime.getTime(),
			dataType: "text",
			error: dataError,
			success: postReactieResult
		}
	);
*/
	$.ajax(
		{	type: "POST",
			url: "http://www.bonheur.nl/scripts/postdata.php",
			cache: false,
			data: {	soort:"producties",
					naam:$("#naam").val(),
					voorstelling:$("#voorstelling").val(),
					reactie:$("#reactie").val(),
					tijd:curTime.getTime()},
			dataType: "text",
			error: dataError,
			success: postReactieResult
		}
	);

};

function postReactieResult(data,text){
	$("#publieksreactie-a-modo").dialog("close");
	$("#result-a-modo").empty();
	if(data == "success"){
		$("#result-a-modo").append("<p>&nbsp;</p><p>Hartelijk dank.</p><p>Uw reactie verschijnt binnen 24 uur op onze website.</p>");
	}else{
		$("#result-a-modo").append("<p>&nbsp;</p><p>Er is een probleem opgetreden. Probeer het later nogmaals.</p>");
	}
	$('#result-a-modo').dialog('open');
}

function getProductiesResult(data,text){
	var s = "<option value=\"0\">algemeen</option>";
	
	for(var i=0;i<data.length;i++){
		s += "<option value=\""+data[i].id+"\">"+data[i].productie+"</option>";
	}
	$("#voorstelling").empty();
	$("#voorstelling").append(s);

}

function dataError(XMLHttpRequest, textStatus, errorThrown){
	$("#publieksreactie-a-modo").dialog("close");
	$("#result-a-modo").empty();
	$("#result-a-modo").append("<p>&nbsp;</p><p>Er is een probleem opgetreden. Probeer het later nogmaals.</p>");
	$('#result-a-modo').dialog('open');
}