﻿var $j = jQuery.noConflict();


$j(document).ready(function(){
	
	//$('#formulario').validate();
	
	$j('a[rel*=facebox]').facebox({
		opacity: 0.5,
		closeImage   : 'http://www.mercedesboronat.com/js/facebox/close.gif'
	}); 

	$j(document).bind('reveal.facebox', function() { 
		// Codigo a ejecutar cuando se muestra el facebox
		$j('#formulario').validate({
			rules: {
				email: {
					required: true,
					email: true,
					maxlength: 60
				},
				field1:{
					required: true,
					maxlength: 60
				},
				field2:{
					required: true,
					maxlength: 60
				},
				field4: {
					required: true,
					maxlength: 60
				},
				field5: {
					range: [1,120]
				}			
			},
			messages: {
				email: {
					required: 'Your email is required',
					email: 'Please, write a valid email address',
					maxlength: '60 characters maximum'
				},
				field1: {
					required: 'Please, write your name',
					maxlength: '60 characters maximum'
				},
				field2: {
					required: 'Please, write your second name',
					maxlength: '60 characters maximum'
				},
				field4: {
					required: 'Please, write your city or country',
					maxlength: '60 characters maximum'
				},
				field5: { 
					range:'Age must be a number between 1 and 120.'
				}
			}
		
		});
		// Ponemos el foco en el campo email
		$j('#email').focus();
	});
	
	$j(document).bind('close.facebox', function(){
		// Limpiamos el formulario
		$j('#formulario')[0].reset();
		$j('#message').html('');
	});

	$j('#submit').live("click", function(){
		if ($j('#formulario').valid()){
			$j.post("/pommo/user/process.php",
				{Email: $j('#email').val(),
				'd[1]': $j('#field1').val(),
				'd[2]': $j('#field2').val(),
				'd[4]': $j('#field4').val(),
				'd[5]': $j('#field5').val(),
				formSubmitted:	1,
				pommo_signup:	'true',
				is_ajax: 'true',
				pommo_signup:	'Subscribe'
				},
				function(data){
					if (data){
						if (data.indexOf('OK') > -1 ){
							// Limpiamos el formulario
							$j('#message').html('Now you are subscribed');
							$j('#formulario')[0].reset();							
						}else if (data.indexOf('KODATA') > -1){
							// Datos incorrectos (javascript desactivado?)
							$j('#message').html('Problem sending form, please check your data');
						}else if (data.indexOf('KODUPE') > -1){
							// El email ya esta apuntado a la newsletter
							$j('#message').html('This email address is already subscribed');
						}else if (data.indexOf('KOMAIL') > -1){
							// email incorrecto/malformado
							$j('#message').html('Invalid email address');
						}else if (data.indexOf('KOUNK') > -1) {
							// error no especificado al anyadir suscriptor
							$j('#message').html('System error. Please, try again later');
						}
					}else{
						alert('Network error. Please, try again later');
					}		
			});
			
		}
	});
	
});