/****************************************************************************************************************************/
/*                                Desarrollado por La Difusora - Comunicación Digital - 2011                                */
/****************************************************************************************************************************/

var formulario = function(){
	var instancia = this;
	var f = '';
	var env = 'Enviando su mensaje. Espere por favor';
	var url = 'contacto_process.php';
	
	var fcamp = '';
	var fbien = '';
	var fmal = '';
	var session = '';
	
	this.Iniciar = function(formulario,u,s){
		this.f = formulario;
		this.url = u;
		this.session = s;
		this.reset_formulario();
	};
		
	this.Ventanas = function(a,b,c){
		this.fcamp = a;
		this.fbien = b;
		this.fmal = c;
	};

	this.Respuesta = function(data){
		$(instancia.f+' #btn_rec').removeAttr('disabled');
		if(data != null){
			if(data.error == 1){
				$('#mensaje_error').html('Debe completar los datos requeridos.')
				$.each(data, function(k,v){
					if(v == 1)
						$("input#"+k).addClass('fondo_error');
					else
						$("input#"+k).removeClass('fondo_error');
				});
			}
			
			if(data.error == 2){
				$('#mensaje_error').html('El c&oacute;digo de verificaci&oacute;n es incorrecto.');
				$(instancia.f+' input#cap').addClass('fondo_error');
			}
		
			if(data.error == 3){
				$('#mensaje_error').html('La direcci&oacute;n de mail es incorrecta.');
				$(instancia.f+' input#mail').addClass('fondo_error');
			}
			
			if(data.error == 10){
				$(instancia.f).hide();
				$(instancia.fmal).fadeIn('normal');
			}
			
			if(data.error != 0){
				document.getElementById('captcha').src='../php/captcha.php?'+Math.random()+'&id='+instancia.session;
				$(instancia.f+' input#cap').val('');
				$('#mensaje_error').fadeIn('normal');
				formf = true;
			}else{
				$(instancia.f).hide();
				if(data.enviado == 1)
					$(instancia.fbien).fadeIn('normal');
				else
					$(instancia.fmal).fadeIn('normal');
			}
		}
	};
	
	//Muestra el error y oculta el formulario
	this.Error = function(){
		$(this.fcamp).hide();
		$(this.fbien).hide();
		$(this.fmal).show();
	};
	
	
	//Reinicia el formulario para volver a ingresar los datos
	this.Reset = function(){
		$(instancia.f).show();
		$(instancia.fmal).hide();
		$(instancia.fbien).hide();
		$('#mensaje_error').html('').hide();
		$(this.f+' input:text').each(function(index) {
		    $(this).css('color','#AAA');
			$(this).removeClass('fondo_error');
			$(this).val($(this).attr('alt'));
		});
		$(this.f+' textarea').val('');
		document.getElementById('captcha').src='../php/captcha.php?'+Math.random()+'&id='+instancia.session;
	};
		
	//Resetea el formulario 
	this.reset_formulario = function (){
		$(this.f+' #btn_rec').click(function(){
			var dataString = $(instancia.f).serialize();
			$('#mensaje_error').html(instancia.env);
			$('#mensaje_error').fadeIn('normal');
			$(this).attr('disabled','disabled');
			$.ajax({
				type:"POST", 
				url:instancia.url,
				dataType:"json",
				error: function (XMLHttpRequest, textStatus, errorThrown){instancia.Error()},
				data:dataString,
				timeout:5000,
				success: instancia.Respuesta
			});  
		}).removeAttr('disabled');
	
		$(this.f+' input:text').removeAttr('disabled');
		$(this.f+' textarea').removeAttr('disabled');
	
		$(this.f+' input:text').css('color','#AAA');
		$(this.f+' input:text').focus(function(){
			this.valuedefault = this.valuedefault || this.value;
			if (this.value == this.valuedefault)
				this.value = '';
			$(this).css('color','#000');
			$(this).removeClass('error');
		});
	
		$(this.f+' input:text').blur(function(){
			if (this.value.length == 0 || this.value == this.valuedefault)
				$(this).css('color','#AAA');
			if (this.valuedefault && this.value.length==0)
				this.value = this.valuedefault;
		});
	};
};

