/// Mootools Form Check implementation   MOOCHECK
window.addEvent('domready', mooCheckInit);
/// Mootools Form Check implementation   MOOCHECK

function mooCheckInit(){
	$$('form.MooCheck').each(
		function(theForm){
			theForm.addEvent('submit',mooCheckSubmit);
			theForm.getElements('.MooCheck').each(
				
				function(theFormElement){
					if(theFormElement.get('type')=='checkbox' || theFormElement.get('type')=='radio'){
						theFormElement.addEvent('change',mooFieldCheck);
					} else {
						theFormElement.addEvent('blur',mooFieldCheck);
					}
				}
				
			);
		}
	);
}

function mooFieldCheck(){
	// Only return on errors..  after passing another error could occur..
	if(this.hasClass('MooNotEmpty') && this.get('value').trim()==''){
		mooRaiseError(this);
		return;
	} else if(this.hasClass('MooNotEmpty')){
		mooRemoveError(this);
	}
	if(this.hasClass('MooValidEmail') && /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,4}$/.test(this.get('value')) ){
		mooRemoveError(this);
	} else if(this.hasClass('MooValidEmail')){
		mooRaiseError(this);
		return;
	}
	
	if(this.hasClass('MooValidDate') && isDate(this.get('value') ,'yyyy-MM-dd'  ) ){
		mooRemoveError(this);
	} else if(this.hasClass('MooValidDate')){
		mooRaiseError(this);
		return;
	}
	
	if(this.hasClass('MooMinLength') && this.get('value').length>2000){ // Selectbox may not have first option selected.
		mooRaiseError(this,'Maximum length exceeded');
		return;
	} else if(this.hasClass('MooMinLength')){
		mooRemoveError(this);
	}
	
	if(this.hasClass('MooNotFirstOption') && this.selectedIndex==0){ // Selectbox may not have first option selected.
		mooRaiseError(this);
		return;
	} else if(this.hasClass('MooNotFirstOption')){
		mooRemoveError(this);
	}
	
	if(this.hasClass('MooNumeric') && this.get('value').toInt().limit(0,2147483647).toString()!=this.get('value')){ // Selectbox may not have first option selected.
		mooRaiseError(this);
		return;
	} else if(this.hasClass('MooNumeric')){
		mooRemoveError(this);
	}

	if(this.hasClass('MooCheckboxRequired') && !this.getProperty('checked')){ // Selectbox may not have first option selected.
		mooRaiseError(this);

		return;
	} else if(this.hasClass('MooCheckboxRequired')){

		mooRemoveError(this);
	}
}
function mooRaiseError(theElement){
	// Check if there is an _error field for this..
	if(theElement.hasClass('hasError')){
		return;
	}
	var theErrorInfo;
	if(theErrorInfo=$(theElement.getProperty('id')+'_error')){
		var ErrorElement = new Element('span',{'class':'error'});
		if(typeof(arguments[1])!='undefined'){
			ErrorElement.appendText(arguments[1]);	
		} else {
			ErrorElement.appendText(theErrorInfo.get('value'));
		}
		ErrorElement.injectBefore(theErrorInfo);
		theElement.addClass('hasError');
	}
}
function mooRemoveError(theElement){
	var ErrorElement;
	if(theElement.hasClass('hasError')){
		console.log('removal');
		ErrorElement=$(theElement.getProperty('id')+'_error').getPrevious();
		console.log(ErrorElement);
		if(ErrorElement.get('tag')=='span' && ErrorElement.hasClass('error')){
			console.log('GOT HERE');
			ErrorElement.destroy();
			theElement.removeClass('hasError');
		}
	}
}
function mooCheckSubmit(event){
	var event = new Event(event);
	this.getElements('.MooCheck').each(
		function(formElement){
			if(formElement.get('type')=='checkbox' || formElement.get('type')=='radio'){
				//theFormElement.addEvent('change',mooFieldCheck);
				formElement.fireEvent('change');
			} else {
				//theFormElement.addEvent('blur',mooFieldCheck);
				formElement.fireEvent('blur');
			}
			
		}
	)
	if(this.getElements('.hasError').length>0){
		console.log(this.getElements('.hasError').length);
		console.log('Found elements with errors');
		event.stop();
		alert('Vul svp alle verplichte velden in.');
		return;
	}
	// Still here?!
	console.log('Form looks okee!');
}
