/**
 * @author epaul
 */

/**
 * loadContent()
 * 
 * function loads clicked content into the fields 
 * data retrieved from the server will be inserted into the selected divs
 * 
 * @param {string} type
 * @param {string} subType
 * @param {integer} id
 */
function loadContent(type, subType, id){
	arrowBack();
	blindlock = false;
	var args = "&check=2&type=" + type + "&subType=" + subType + "&id=" + id;
	new Ajax.Request('/AjaxRequests.php', {
		method: 'post',
		postBody: args,
	 	onSuccess: function(transport) {
     		var response = transport.responseText || "Er is een storing";
			$('contentOverlayContent').style.height = $('MiddlePane').clientHeight + "px";	
			$('contentOverlayContent').update(response);
  			new Effect.toggle('contentOverlay','blind', {duration: 0.5});
  		}
	});
}


/**
 * loadMenuItems()
 * 
 * loads the menu items into the div
 * function will also cause an overlay div to hide underlying information
 * 
 * @param {string} type
 */
function loadMenuItems(type){
	arrowBack();
	var args = "&check=2&type=" + type + "&subType=&id=1";
	new Ajax.Request('/AjaxRequests.php', {
		method: 'post',
		postBody: args,
	 	onSuccess: function(transport) {
     		var json = transport.responseText.evalJSON();
			$('contentOverlayContent').style.height = $('MiddlePane').clientHeight + "px";	
			$('contentOverlayContent').update(json.message);
			new Effect.toggle('contentOverlay','blind', {duration: 0.2});
  		}
	});
}


/**
 * processForm()
 * 
 * function sends contact data submitted by the user to the server and saves it
 *  
 * @param {string/ htmlelement} formId
 */
 
 var submittedForm	= '';
 
function processForm(formId){
	// headerContact || nameContact
	formId	= formId.id;
	submittedForm	= formId;
	if (checkForm(formId)){
		var jsonString = escape(Object.toJSON($(formId).serialize(true)));
		new Ajax.Request('/AjaxRequests.php', {
			method: 'post',
			postBody: "&check=1&jsonString=" + jsonString,
		 	onSuccess: function(transport) {
				$(submittedForm+'Message').update("Bedankt voor het invullen van het contactformulier!");
				submittedForm	= '';
	  		}
		});
		$(formId).reset();
	}else {
		obj = $(formId).serialize(true);
		if (obj.name == ""){
			$(submittedForm+'Message').update("Wilt u a.u.b alle velden invullen.");
		}else if(obj.message == ""){
			$(submittedForm+'Message').update("Wilt u a.u.b alle velden invullen.");
		}else{
			$(submittedForm+'Message').update("Wilt u a.u.b uw emailadres correct invullen.");
		}	
	}
} 

/**
 * processInschrijvenForm()
 * 
 * data submitted by the user after filling in the form will 
 * be submitted to the server and stored in the database
 * 
 * @param {string / htmlelement} formId
 */
function processInschrijvenForm(formId){
	if (checkForm(formId)){
		var jsonString = escape(Object.toJSON($(formId).serialize(true)));
		new Ajax.Request('/AjaxRequests.php', {
			method: 'post',
			postBody: "&check=3&jsonString=" + jsonString,
		 	onSuccess: function(transport) {
				$('formMessage').update("Bedankt voor het invullen van het inschrijfformulier!");
	  		}
		});
		$(formId).reset();	
	}else {
		$('formMessage').update("Wilt u a.u.b alle velden invullen!");
	}
} 