/**
 *		Bootstrap
 **/
$( document ).ready( function(){
	// Mailing //
	$("#idInput_Mailing_Naam").bind( "keypress", function(e) {
		// Enter pressed?
	  	if (e.keyCode == 13) {
	  		click_Mailing_Submit();
	  	}
	});

	$("#idInput_Mailing_Emailadres").bind( "keypress", function(e) {
		// Enter pressed?
	  	if (e.keyCode == 13) {
	  		click_Mailing_Submit();
	  	}
	});

	$( "#idButton_Mailing_Submit" ).click( click_Mailing_Submit );
});


/**
 *		Shows mailing form.
 */
function showMailingForm()
{
	$( "#idA_Mailing" ).hide(); 
	$( '#idDiv_Mailing' ).show( 250 ); 
	return false;
}


/**
 *		Gebruiker verstuurt verzoek tot inschrijven mailing list
 **/
function click_Mailing_Submit()
{
	// Wis bericht en verberg
	$( "#idDiv_Mailing_Message" ).hide();
	

	// Validatie
	if ( ! validate_Mailing_Submit() ) {
		// Stop
		return false;
	}
	
	
	// Alle waarden uit het formulier ophalen
	var strNaam 			= $( "#idInput_Mailing_Naam" ).val();
	var strEmailadres 		= $( "#idInput_Mailing_Emailadres" ).val();

	// Create AJAX message
	var strAjaxMessage = "Form=visitor.mailing.subscribe";
	strAjaxMessage += "&Naam=" + strNaam;
	strAjaxMessage += "&Emailadres=" + strEmailadres;
	
	
	// Verberg formulier
	$( "#idDiv_Mailing_Form" ).hide();
	
	// Weergeef loader
	$( "#idDiv_Mailing_Loader" ).show();
	
	
	// Send AJAX request
	dispatchAjaxRequest( g_Ajax_ServicesLocation, strAjaxMessage, "xml", callback_Mailing_Submit );	
}



/**
 *		Gebruiker verstuurt verzoek tot inschrijven mailing list (CALLBACK)
 **/
function callback_Mailing_Submit( ajaxMessage )
{
	// Verberg loader
	$( "#idDiv_Mailing_Loader" ).hide();
	
	
	// Fetch response values
	var strResult 	= getXmlChildElementValue( ajaxMessage, "response", "result" );
	var strMessage	= getXmlChildElementValue( ajaxMessage, "response", "message" );	

	
	// Check
	if ( parseStringToBoolean( strResult ) )
	{
		// Positieve response
		$( "#idDiv_Mailing_Message" ).removeClass( "error" ).html( getHtmlIcon( true )  + strMessage );
	}
	else
	{	
		// Negatieve response
		$( "#idDiv_Mailing_Message" ).addClass( "error" ).html( getHtmlIcon( false ) + strMessage );	
		
		// Weergeef formulier
		$( "#idDiv_Mailing_Form" ).show();
	}
	
	// Weergeef bericht
	$( "#idDiv_Mailing_Message" ).show();
}



/**
 *		Gebruiker verstuurt verzoek tot inschrijven mailing list (VALIDATIE)
 **/
function validate_Mailing_Submit()
{
	// Ophalen verplichte waarden
	var strNaam 			= $( "#idInput_Mailing_Naam" ).val();
	var strEmailadres 		= $( "#idInput_Mailing_Emailadres" ).val();

	// Error flag & message
	var fError = false;
	var strErrorMessage = "";
	
	// Naam
	if ( strNaam.length == 0 )
	{
		strErrorMessage += "- Uw naam ontbreekt.\n";
		fError = true;
	}
	
	// Emailadres
	if ( strNaam.length == 0 )
	{
		strErrorMessage += "- Uw e-mailadres ontbreekt.\n";
		fError = true;
	}

	// Fout?
	if ( fError )
	{
		// Bericht naar gebruiker
		alert( strErrorMessage );
		
		// Annuleer
		return false;
	}

	// Klaar
	return true;	
}
