box.dom(document).ready(function() {
	box.ui('form').addPatterns({
		password: /^[a-zA-Z0-9]{6,}$/
	});
	
    box.ui('form').create({
        element: '#loginForm'
    }).addReplacement().mustValidate(function(form) {
		
		form.field('password').mustValidate(function(field) {			
            if(field.isEmpty() || !field.isMatching('password')) { 
                return l10n.errors.required.EM_SW_37;
            }
        });
        
        form.field('email').mustValidate(function(field) {
            if(field.isEmpty()) {
                return l10n.errors.required.EM_SW_06;
            } else if(!field.isMatching('email')) {
                return l10n.errors.required.EM_SW_06;
            }
        });
            
        return l10n.errors.form;
    });
    
    var errorCls = 'error';
    
    var getErrorTarget = function(field) {
        if('radio' != field.type) {
            return field.getLabel();
        } else {
            return field.getElements().parent().parent().prev();
        }
    };
	
	  var addErrorIcon = function(field, target) {
		var id = field.name + 'Error';
		var img = box.dom('#' + id);
		if(img.length) {
		  img.attr('alt', field.getError());
		} else {
		  target.append('<span id="' + id + '">- ' + field.getError() + '<br/></span>');
		}
	  };

    
    var addFieldError = function(e) {
		getErrorTarget(e.source).addClass(errorCls);
		var sourceForm = $('#'+e.id).parents('form').attr('id');
		addErrorIcon(e.source, $('#'+sourceForm+' div.alert .content p'));
    };
    
    var removeFieldError = function(e) {
        getErrorTarget(e.source).removeClass(errorCls);
		box.dom('#' + e.source.name + 'Error').remove();
    };
    
    var addFormError = function(e) {
        var id = e.source.id + 'Error';
		var msg = e.source.msg;
        var error = box.dom('#' + id);
        if(error.length) {
            error.html(msg);
        } else {
            //e.source.getElement().prepend('<div id="' + id + '" class="' + errorCls + '">' + msg + '</div>');
			//$('div.alert .content p').text(msg);
			$('#' + e.source.id + ' div.alert').show();
        }
    };
    
    var removeFormError = function(e) {
        box.dom('#' + e.source.id + 'Error').remove();
    };
	
	var urlAction;
	var getDataForm;
		
	var ajaxPostForm = function(e,originalEvent) {
		if(typeof originalEvent == 'object') {
			urlAction = $('#askForPasswordForm').attr('action');
			getDataForm = $('#askForPasswordForm').serialize();
			originalEvent.preventDefault();
				$.ajax({type: "GET",
				url: urlAction+'?'+getDataForm,
					success: function(retour) {	
						if(retour.etat=='true'){
							setGoogleAnalytics('/other/sent-password-confirm');
							var message = eval("l10n.errors.required."+retour.message);
							$('#askForPasswordForm div.alert').show();
							$('#askForPasswordForm div.alert p').text(message);
							$('#askForPasswordForm div.buttons, #askForPasswordForm div.text').hide();
						} else {
							var message = eval("l10n.errors.required."+retour.message);
							$('#askForPasswordForm div.alert').show();
							$('#askForPasswordForm div.alert p').text(message);
						}
					}, 
					dataType: "json"
				});	
			return false;
		} else {
			box.ui('generic.popin').animate({top: 'viewport:before'}, 500, 'close');
		}
	}
    
    // binding des événements
    box.bind({
        'error.form': addFormError,
        'valid.form': removeFormError,
		'valid.form.askForPasswordForm': ajaxPostForm,
        'error.field': addFieldError,
		'valid.field': removeFieldError,
		'close.generic.popin.forgottenPass' : function(e) {
			box.ui('form').destroy('askForPasswordForm');
		},
		'open.generic.popin.forgottenPass' : function(e) {
			box.ui('form').create({
				element: '#askForPasswordForm'
				}).addReplacement().mustValidate(function(form) {
					form.field('emailfp').mustValidate(function(field) {
						if(field.isEmpty()) {
							return l10n.errors.required.EM_SW_06;
						} else if(!field.isMatching('email')) {
							return l10n.errors.required.EM_SW_06;
						}
        			});	
				});
			}
    	});
	});
