$(document).ready(function(){
 	
	var loader = jQuery('<div id="statusBar"><img src="templates/images/loading.gif" alt="Cargando..." /></div>')
			.css({position: "absolute", top: "0px", left: "0px"})
			.hide()
			.appendTo("body");
		jQuery().ajaxStart(function() {
			loader.show();
		}).ajaxStop(function() {
			loader.hide();
		}).ajaxError(function(a, b, e) {
			throw e;
	});
		
	jQuery.validator.addMethod("password", function( value, element ) {
		var result = this.optional(element) || value.length >= 6 && /\d/.test(value) && /[a-z]/i.test(value);
		if (!result) {
			element.value = "";
			var validator = this;
			setTimeout(function() {
				validator.blockFocusCleanup = true;
				element.focus();
				validator.blockFocusCleanup = false;
			}, 1);
		}
		return result;
	}, "La contraseņa tiene que tener como minimo 6 caracteres alfanumericos sin espacios.");

	// a custom method making the default value for companyurl ("http://") invalid, without displaying the "invalid url" message
	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
		return value != element.defaultValue;
	}, "");
	
	jQuery.validator.messages.required = "";
	$("form").bind("invalid-form.validate", function(e, validator) {
		var errors = validator.numberOfInvalids();
		if (errors) {
			var message = errors == 1
				? 'Se encontro 1 campo con error. El mismo se marca en rojo mas abajo'
				: 'Se encontraron ' + errors + ' campos con errores.  Los mismos estan resaltados en rojo';
			$("div.error span").html(message);
			$("div.error").show();
		} else {
			$("div.error").hide();
		}
	}).validate({
		//focusInvalid: false,
		//focusCleanup: true,
		onkeyup: false,
		submitHandler: function() {
			$("div.error").hide();
			
			$('#form').submit(function() { 
    			// submit the form 
			    $(this).ajaxSubmit({target: "#Contenedor"}); 
			    // return false to prevent normal browser submit and page navigation 
			    return false; 
			});
			
		},
		messages: {
			password2: {
				required: " ",
				equalTo: "Please enter the same password as above"	
			},
			email: {
				required: " ",
				email: "Por favor ingresa un e-mail valido. Ej: usuario@dominio.com",
				remote: jQuery.format("{0} ya esta registrado, por favor ingresa un email diferente.")	
			},
			name: {
				required: " ",
				name: "Ingresa un nombre de usuario valido",
				remote: jQuery.format("{0} ya esta registrado por otro usuario, elige otro.")	
			},
			codigo: {
				required: " ",
				codigo: "Ingresa el codigo que aparece en la imagen",
				remote: jQuery.format("El codigo ingresado es incorrecto.")	
			}
			
		},
		debug:true
	});
});

