$(document).ready(function(){

	  var optionsFormRegistro = { 
	        beforeSubmit:  showRequest,  // pre-submit callback 
	        success:       showResponse,  // post-submit callback 
	        url:       'index.php?ohaction=ajaxFrmRegistro',         // override for form's 'action' attribute 
	        type:      'post',        // 'get' or 'post', override for form's 'method' attribute 
	        dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type) 
	        clearForm: false,        // clear all form fields after successful submit 
	        resetForm: false        // reset the form after successful submit ,
	       
	    }; 

	  var optionsFormLogin = { 
	        beforeSubmit:  showRequestLogin, 
	        success:       showResponselogin, 
	        url:       'index.php?ohaction=ajaxFrmLogin', 
	        type:      'post', 
	        dataType:  'json', 
	        clearForm: false, 
	        resetForm: false
	    }; 

		var optionsFormRecupero = {
	        beforeSubmit:  function(){
									sending.sendingOn("#recuperoPassword");
	        					}, 
	        success:       function(response){
	        					sending.sendingOff("#recuperoPassword");
	        					if(response.error!=0){
	        						$(".errorContent","#recuperoPassword").addClass("error");
	        						$(".errorContent","#recuperoPassword").html(response.html);
	        						$(".errorContent","#recuperoPassword").css("display","block");
	        					} else {
									$("form","#recuperoPassword").replaceWith('<div style="">'+response.html+'</div>');
								}
	        				},  
	        url:       'index.php?ohaction=ajaxRecuperoPass', 
	        type:      'post', 
	        dataType:  'json', 
	        clearForm: true, 
	        resetForm: false 
		}
		var container = $('div.errores-reg-reg');
	
	    $("#frmRegistro").validate({
		submitHandler: function(form) {
		   	$(form).ajaxSubmit(optionsFormRegistro);
		    return false;
		},
		errorElement: "li",
		errorContainer: container,
		errorLabelContainer: $("ul", container),	
		rules: {
			nombre: { 
				required: true
			},
			mail: {
				required: true,
				email: true
			},
			password: "required",
			passConf: {
				equalTo: '#frmRegistro input[name="password"]'
			},
			localidad:"required",
			terminos:"required"
		},
		
		messages: {
			nombre: { 
				required: "El nombre es requerido"
			},
			mail: {
				required: "El e-mail es requerido",
				email: "Debe escribir el mail correctamente"
			},
			password: { 
				required: "El password es requerido"
			},
			passConf: "No coinciden ambos passwords",
			localidad:"Ingrese una Localidad",
			terminos:"Debe aceptar términos y condiciones"
		}
	    });

		$("#submitLogin").click(function(){
			$("#frmLogin").submit();
		})
		
		var container = $('div.errores-log-reg');
		$("#frmLogin").validate({
			submitHandler: function(form) {
	    		$(form).ajaxSubmit(optionsFormLogin);
	    		return false;
			},
			errorElement: "li",
			errorContainer: container,
			errorLabelContainer: $("ul", container),	
			rules: {
				mail: {
					required: true,
					email: true
				},
				password:{
					required: true
				}
			},
		
			messages: {
				mail: {
					required: "El e-mail es requerido",
					email: "Debe escribir el mail correctamente"
				},
				password: { 
					required: "El password es requerido"
				}
			}
		});
		
		$("#olvidoPass").openPopUp("#recuperoPassword",{width: "500px", height: "220"});	

		$("#recuperoPassword form").validate({
			submitHandler: function(form) {
			   	$(form).ajaxSubmit(optionsFormRecupero);
			    return false;
			},
			errorElement: "span",
			errorContainer: $("#recuperoPassword .errorContent"),
			errorLabelContainer: $("span", "#recuperoPassword .errorContent"),
			rules: {
				mail: {
					required: true,
					email: true
				}			
			},
		
			messages: {
				mail: {
					required: "Este campo es requerido",
					email: "Debe escribir el mail correctamente"

				}			
			}
	    });

	});


	function showRequestLogin(formData, jqForm, options){
	
		sending.sendingOn("#sendingLogin");
	    return true; 
	}
	function showResponselogin(response, statusText){
		sending.sendingOff("#sendingLogin");

		if(response.error!=0){
//			$('input[type="submit"]', '#frmLogin')
//				.before($('<div class="error"></div>').html(response.html));
			//$('#loginErrorContent').html(response.html).addClass("error").css("display","block");
			$('div.errores-log-reg ul').html('<li class="error">'+response.html+'</li>');
			$('div.errores-log-reg').show();
			$('div.errores-log-reg ul').show();
			$('input', '#frmLogin').css("border","solid 1px red");
			 
		} else {
			$('#frmLogin').prev().html();
			$('#frmLogin').parent().append($('<div style="margin: 0 30px; height: 175px" class="texto"></div>').html(response.html));
			$('#frmLogin').remove();
			$(".submitTbl").css("display","none");
			window.location.href = ursitio+"/sistema/index.php";
			//window.open( ursitio+"/sistema/index.php");
		}
	}


	function showRequest(formData, jqForm, options) {
		sending.sendingOn("#sendingRegistro");
	    return true; 
	} 
	 
	function showResponse(response, statusText)  { 
		sending.sendingOff("#sendingRegistro");
		if(response.error!=0){
		//	$('input[type="submit"]', '#frmRegistro').before($('<div class="error"></div>').html(response.html));
		//	$('.errorContent', '#frmRegistro').html($('<div class="error"></div>').html(response.html));
		//	$('img.antispam', '#frmRegistro').attr("src","img/imagenAntispam.php?action=registro&p="+Math.random());
			$('div.errores-reg-reg ul').html('<li class="error">'+response.html+'</li>');
			$('div.errores-reg-reg').show();
			$('div.errores-reg-reg ul').show();
	
			$('input[name="antispam"]','#frmRegistro').attr("value","");
		} else {
			var mailregistro = $('input[name="mail"]','#frmRegistro').attr("value");

			$('#frmRegistro').parent().before($('<div style="margin: 0 30px; height: 195px" class="texto"></div>').html(response.html));
			//$('#formulario_registro').remove();
			//$('#frmRegistro').hide();
			//$('#frmRegistro').parent().before($('<div style="margin: 0 30px; height: 175px" class="texto"></div>').html(response.html));
			$('#frmRegistro').fadeOut('low');
			//$('#msgRegistro').css("display","block");
			
			$('#msgRegistro .msgMailDeRegistro').text(mailregistro);
			var mensaje = $("#mensajeRegistro").html();
			//$('#frmRegistro').parent().before($('<div style="margin: 0 30px; height: 175px" class="texto"></div>').mensaje);
			//$('#formulario_registro').remove();
			$('#formulario_registro .texto').text('');
			$('#formulario_registro .texto').append(mensaje);
		}
	}

