﻿if (!DT_EFFECTS) { var DT_EFFECTS = new Object(); }

DT_EFFECTS.menu = {
	slideIn 		: function(element) {
		new Effect.MoveBy( element.parentNode, 185, 0 );
		element.onclick = function(event) { 
			DT_EFFECTS.menu.slideOut(element); 
		}
		
		return false;
	},
	slideOut 		: function(element) {		
		new Effect.MoveBy( element.parentNode, -185, 0 );
		element.onclick = function(event) { 
			DT_EFFECTS.menu.slideIn(element); 
		}
		return false;
	}
}






/**
*	object FORMS
*
**/
if (!FORMS) { var FORMS = new Object(); }



FORMS.control = {
	
	is_valid 		: false,
	objForm			: null,
	sForm			: "",
	sType			: "",
	sMsgSuccess		: "<h2>Uw vraag werd bewaard!</h2><p>Een mail werd verzonden.</a>",
	
	// private methods
	call			: function(t) {
		
		alert("Global Error: "+t.status+" - "+t.statusText);
		
	}, 
	// public methods
	send			: function(p_form, p_type) {
		
		FORMS.control.objForm 		= p_form;		
		FORMS.control.sForm 		= p_type;		
		
		if(formChecker.checkForm(FORMS.control.objForm)) {
			FORMS.control.is_valid = true;
			
			var postBody = Form.serialize(FORMS.control.objForm); 
			// action is in form + "&action="+$F(p_type+'_action');
			
			new Ajax.Request(window.location,
					{
						method		: "post",
						postBody	: postBody,
						onSuccess	: function(t) {							
							FORMS.control.doExecute(t.responseText);
						},
						onFailure	: function(t) {
							alert("Error:\n"+t.status+" - "+t.statusText);
						}
					}
			);			
		} 		
	},
	
	doExecute			: function(responseText) {
		
		var json	= responseText.parseJSON();		
		
		if (json.status.code == "success") {			
			new Effect.Highlight(FORMS.control.objForm, { 
					startcolor:'#77AA37', 
					endcolor:'#1C1914', 
					duration:'2'
			});				
			new Insertion.Top(FORMS.control.objForm, "<p style=\"background-color: #77AA37; color: #ffffff; padding-left: 10px;\">Saved</p>");			
			FORMS.control.objForm.reset();
			
		} else {
			if(json.status.text != undefined && json.status.text != 'undefined') {
				alert(json.status.text);
			}	
			if(json.status.message != undefined && json.status.message != 'undefined') {							
				new Insertion.Top(FORMS.control.objForm, "<p style=\"background-color: #77AA37; color: #ffffff; padding-left: 10px;\">"+json.status.message+"</p>");	
			
			}			
		}
		
	},
	
	redirect			: function() {
			
			FORMS.control.objForm.parent.innerHTML = FORMS.control.sSuccess;
			
	}
	
}






/**
*	object BOXES
*
**/
if (!BOXES) { var BOXES = new Object(); }



BOXES = {

		open		: function(p_element, p_sType) {
			
			switch(p_sType) {
				
				case 'appear':
					new Effect.Appear($(p_element),{ duration:'1'});
					
				break;
				
				default:
					Element.show($(p_element));
			}
			
			// change onclickEvent of the anchor
			if($('anc_'+p_element)) {				
				$('anc_'+p_element).onclick = function() {BOXES.close(p_element, 'fade');};				
			}
			
		},
		close		: function(p_element, p_sType) {
			
			switch(p_sType) {
				
				case 'fold':
				case 'fade':
					new Effect.Fade($(p_element),{ duration:'1'});					
				break;
				
				default:
					Element.hide($(p_element));
			}
			// change onclickEvent of the anchor
			if($('anc_'+p_element)) {
				$('anc_'+p_element).onclick = function() {BOXES.open(p_element, 'appear');};
			}
			
			
		}
	
}




/**
*	object DB_FORMS
*
**/
if (!DB_FORMS) { var DB_FORMS = new Object(); }

DB_FORMS.form = {
	
	// members
	is_valid 		: false,
	objForm			: null,
	
	// private methods
	call			: function(t) {
		
		alert("Global Error: "+t.status+" - "+t.statusText);
		
	}, 
	// public methods
	
	retrieve_password : function() {
		
		var sEmail = prompt("Vul uw E-mailadres in bij het paswoord dat u vergeten bent.", "email");
		
		
		if(sEmail.indexOf('@') !== -1 && sEmail.indexOf('.') !== -1) {
			
			new Ajax.Request(window.location,
						{
							method		: "post",
							postBody	: 'action=AJAX_SEND_PASSWORD&email='+sEmail,
							onSuccess	: function(t) {
								var json	= t.responseText.parseJSON();		
								if (json.send.status == "success") {
									alert("En mail werd verstuurd!");
								} else {
									alert("Mail niet verzonden");
								}
							},
							onFailure	: function(t) {
								alert("Error:\n"+t.status+" - "+t.statusText);
							}
						}
			);
		} else {
			
			alert("Gelieve een geldig email adres op te geven.");
				
		}
		
		
	},
	
	
	save			: function(p_form, p_model) {
		
		DB_FORMS.form.objForm 		= p_form;
		formChecker.nokMessage = "Gelieve alle verplichte velden in te vullen!";
		
		if(p_model == 'clients') {							
			ORDER.form.send(DB_FORMS.form.objForm);
			return true;
		} 
		
		if(formChecker.checkForm(DB_FORMS.form.objForm)) {
			
			DB_FORMS.form.is_valid = true;
			
			var postBody = Form.serialize(DB_FORMS.form.objForm) + "&action=AJAX_SAVE_"+p_model;			
			
			new Ajax.Request(window.location,
					{
						method		: "post",
						postBody	: postBody,
						onSuccess	: function(t) {
							DB_FORMS.form.doExecute(t.responseText);
						},
						onFailure	: function(t) {
							alert("Fout");
						}
					}
			);
			
		} 
		
	},
	
	doExecute			: function(responseText) {
				
		var json	= responseText.parseJSON();		
		if (json.form.status == "success") {
			new Effect.Highlight(DB_FORMS.form.objForm, { 
				startcolor:'#576B00', 
				endcolor:'#DBD6BC', 
				duration:'2', 
				afterFinish: function() { DB_FORMS.form.redirect(); } 
			});	
			
		}
		
	},
	
	redirect			: function() {			
		
		Form.reset(DB_FORMS.form.objForm);
			
	},
	
	// extra function for a real refreshing submit
	submitPage			: function(p_form) {
		
		if(formChecker.checkForm(p_form)) {
			return true;
		} else {
			return false;
		}			
		
	}
	
}




/**
*	object Order
*
**/
if (!ORDER) { var ORDER = new Object(); }

ORDER.form = {
	
	// members
	is_valid 		: false,
	objForm			: null,
	/*
	sOrderSuccess	: "<div class=\"box\"><h2>Your order was successfully saved!</h2><p>a mail has been sent to you.</p></div>",
	*/
	sOrderSuccess	: "<div class=\"box\"><h2>Uw bestelling is bewaard!</h2><p>Er werd u ook een bevestigende informatieve mail verstuurd.</p></div>",
	
	
	// private methods
	call			: function(t) {
		
		alert("Global Error: "+t.status+" - "+t.statusText);
		
	}, 
	// public methods
	send			: function(p_form) {
		
		ORDER.form.objForm 		= p_form;
		
		if(formChecker.checkForm(ORDER.form.objForm)) {
			
			ORDER.form.is_valid = true;
			
			var postBody = Form.serialize(ORDER.form.objForm) + "&action=AJAX_SAVE_CLIENTS";			
			
			new Ajax.Request(window.location,
					{
						method		: "post",
						postBody	: postBody,
						onSuccess	: function(t) {
							ORDER.form.doExecute(t.responseText);
						},
						onFailure	: function(t) {
							alert("Error:\n"+t.status+" - "+t.statusText);
						}
					}
			);
			
		} 
		
	},
	
	doExecute			: function(responseText) {
			
		var json	= responseText.parseJSON();		
		if (json.form.status == "success") {
			
			new Effect.Highlight(ORDER.form.objForm, { 
				startcolor:'#576B00', 
				endcolor:'#DBD6BC', 
				duration:'2', 
				afterFinish: function() { DB_FORMS.form.redirect(); } 
			});	
			
			//new Effect.Fade( ORDER.form.objForm, {duration:1.1,afterFinish: function() { ORDER.form.redirect(); } });
			
		}
		
	},
	
	redirect			: function() {
			Form.reset(ORDER.form.objForm);
			ORDER.form.objForm.parent.innerHTML = ORDER.form.sOrderSuccess;			
			CART.empty();				
			new Effect.Fade( $('cart'), {duration:0.9, afterFinish: function() { ORDER.form.objForm.parent.show(); } });
	}
	
}


