// JavaScript Document

function abrirFormulario (tipo, email, num) {
	formActual = tipo;
	
	jQuery.facebox ({ ajax: urlbase+'/form_'+tipo+'.php?email='+email+'&num='+num });
};

function activarFormulario (str) {
	sjcap ();
	
	var expr = eval ('/['+str+']/');
	var patt = new RegExp (expr);
	
	if (formActual == "Rutas") {
		jQuery.datepicker.setDefaults(jQuery.datepicker.regional[idi]);
		
		jQuery ('.datePicker').datepicker({
			showOn: "button",
			minDate: 0,
			buttonImage: urlBase+"img/ico_calendar.png",
			buttonImageOnly: true,
			beforeShowDay: function(date) {
				var day = date.getDay().toString ();
				
				return [patt.test (day)];
			}
		});
		
		//	Configuración de idioma para el calendario	
		jQuery ('.ui-datepicker-trigger')
			.attr ('alt', 'Calendario')
			.attr ('title', 'Fecha');
	};
	
	jQuery ('#facebox')
		.find ('.right')
			.bind ('click', function () {
				if (formActual == "Contacto") {
					chequearFormularioContacto (document.getElementById('form'+formActual), jQuery ('#facebox .content'));
				}
				else if (formActual == "Rutas") {
					chequearFormularioRutas (document.getElementById('form'+formActual), jQuery ('#facebox .content'));
				}
			})
		.end()
		.find ('.con_label')
			.each (function (i) {
				jQuery (this)
					.focus (function () {
						jQuery ('#facebox label').eq(i).hide();
						
						if (jQuery (this).hasClass ('datePicker')) {
							jQuery ('.datePicker').datepicker ('show');
						}
					})
					.bind ('blur change', function () {
						if (jQuery (this).attr ('value') == "") {
							jQuery ('#facebox label').eq(i).show();
						}
						else {
							jQuery ('#facebox label').eq(i).hide();
						};
					});
			})
		.end()
		.find ('label')
			.each (function () {
				jQuery (this)
					.bind ('click', function () {
						jQuery (this)
							.hide ()
							.parent ()
								.find ('.con_label')
									.focus ();					
						
						if (jQuery (this).parent().find('con_label').hasClass ('datePicker')) {
							jQuery ('.datePicker').datepicker ('show');
						}
					});
			})
		.end()
		.find ('.elem_texto')
			.each (function (i) {
				jQuery (this)
					.keydown (function (e) {
						if (e.keyCode == 13) {
							if (formActual == "Contacto") {
								chequearFormularioContacto (document.getElementById('form'+formActual), jQuery ('#facebox .content'));
							}
							else if (formActual == "Rutas") {
								chequearFormularioRutas (document.getElementById('form'+formActual), jQuery ('#facebox .content'));
							}
						}
			});
		});
};

function chequearFormularioContacto (obj, $capa) {
	var fallo = true;
	
	fallo = noVacio(obj.nombre,"Nombre",idi, "Name");
	
	if (fallo == true) {
		fallo = noVacio (obj.telefono, "Tel\u00e9fono",idi, "Phone");
		if (fallo == true) {
			fallo = esNumero (obj.telefono, "Tel\u00e9fono",idi);
			if (fallo == true) {
				fallo = esMail(obj.email,idi);
				if (fallo == true) {
					fallo = noVacio(obj.comentarios, "Comentarios", idi, "Comments");
					if (fallo == true) {
						fallo = jcap ();
					}
				}
			}
		}
	}
	
	if (fallo) {
		var nom = obj.nombre.value;
		var tel = obj.telefono.value;
		var ema = obj.email.value;
		var com = obj.comentarios.value;

		var txt;
		if (idi == "es") {
			txt = '<div class="respuesta_formulario">Enviando formulario...</div>';
		}
		else if (idi == "en") {
			txt = '<div class="respuesta_formulario">Sending form...</div>';
		};
		
		$capa
			.html (txt)			

		jQuery.ajax ({
			type: 'GET',
			url: 'http://www.valenciaguias.com/wp-content/themes/valenciaguias/form_Contacto_env.php',
//			url: urlbase+'/form_Contacto_env.php',
			data: 'nombre='+nom+'&telefono='+tel+'&email='+ema+'&comentarios='+com,
			dataType: 'html',
			success: function (html, textStatus) {
				var tx;
				if (idi == "es") {
					tx = '<div class="respuesta_formulario">Formulario enviado correctamente.<br /> En breve nos pondremos en contacto con usted.<br /><br />Gracias.</div>'
				}
				else if (idi == "en") {
					tx = '<div class="respuesta_formulario">Your e-mail has been sent. <br />We will contact you very soon. <br /><br />Thanks.</div>'
				};

				$capa
					.html (tx)			
			}
		});
	}
};

function chequearFormularioRutas (obj, $capa) {
	var fallo = true;
	
	fallo = noVacio(obj.nombre,"Nombre",idi, "Name");
	
	if (fallo == true) {
		fallo = noVacio (obj.telefono, "Tel\u00e9fono",idi, "Phone");
		if (fallo == true) {
			if (fallo == true) {
				fallo = esMail(obj.email,idi);
				if (fallo == true) {
					fallo = noVacio(obj.fecha, "Fecha", idi, "Date");
					if (fallo == true) {
						fallo = noVacio(obj.personas, "N\xfamero de Personas", idi, "Number of people");
						if (fallo == true) {
							fallo = jcap ();
						}
					}
				}
			}
		}
	}	
	
	if (fallo) {
		var nom = obj.nombre.value;
		var tel = obj.telefono.value;
		var ema = obj.email.value;
		var rut = obj.oferta.value;
		var fec = obj.fecha.value;
		var per = obj.personas.value;
		var com = obj.comentarios.value;

		var txt;
		if (idi == "es") {
			txt = '<div class="respuesta_formulario">Enviando formulario...</div>';
		}
		else if (idi == "en") {
			txt = '<div class="respuesta_formulario">Sending form...</div>';
		};
		
		$capa
			.html (txt)			
		
		jQuery.ajax ({
			type: 'GET',
			url: 'http://www.valenciaguias.com/wp-content/themes/valenciaguias/form_Rutas_env.php',
//			url: urlbase+'/form_Rutas_env.php',
			data: 'nombre='+nom+'&telefono='+tel+'&email='+ema+'&oferta='+rut+'&fecha='+fec+'&personas='+per+'&comentarios='+com,
			dataType: 'html',
			success: function (html, textStatus) {
				var tx;
				if (idi == "es") {
					tx = '<div class="respuesta_formulario">Formulario enviado correctamente.<br /> En breve nos pondremos en contacto con usted.<br /><br />Gracias.</div>'
				}
				else if (idi == "en") {
					tx = '<div class="respuesta_formulario">Your e-mail has been sent. <br />We will contact you very soon. <br /><br />Thanks.</div>'
				};

				$capa
					.html (tx)			
			}
		});
	}
};

function noVacio(dato,campo,idi, campoEn) {
	var str = dato.value;
	if (str == null || str.length == 0 || str == campo) {
		if (idi == "es") {
			alert('Por favor, rellene el campo '+campo);
		}
		else if (idi == "val") {
			alert('Per favor, emplene el camp '+campo);
		}
		else if (idi == "en") {
			alert('Please, fill in the field '+campoEn);
		}
		dato.focus();
		return false;
	}
	else {
		return true;
	}
};

function conNum (dato, campo) {
	var num = dato.value;
	
	if (num == 0) {
		alert (campo);
	
		return (false);
	}
	else {
		return (true);
	}
};

function esNumero(dato,campo,idi) {
	var str = dato.value;
	var oneChar = 0;
	str = str.toString();
	
	for (var i=0;i<str.length;i++) {
		oneChar = str.charAt(i).charCodeAt(0);
		if (oneChar < 48 || oneChar > 57) {
			if (idi == "es") {
				alert("Introduzca s\u00f3lo n\u00fameros en el campo "+campo);
			}
			else if (idi == "val") {
				alert("Introdu\u00efsca nom\u00e9s nombres en el camp "+campo);
			}
			else if (idi == "en") {
				return (true);
//				alert("Enter only numbers in the field "+campo);
			}
			dato.focus();
			return false;
		}
	}
	return true;
};	

function esTamano(dato,tot,idi) {
	var str = dato.value;
	if (str.length != tot) {
		if (idi == "es") {
			alert("El tel\u00e9fono debe estar compuesto por 9 d\u00edgitos");
		}
		else if (idi == "val") {
			alert("El tel\u00e8fon ha d\u0027estar compost per 9 d\u00edgits");
		}
		else if (idi == "en") {
			alert("The phone must be composed of 9 digits");
		}
		dato.focus()
		return false;
	}
	return true;
};

function esMail(dato, idi) {
	var str = dato.value;
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if (!str.match(re)) {
		if (idi == "es") {
			alert("Introduzca un e-mail correcto");
		}
		else if (idi == "val") {
			alert("Introdu\u00efsca un e-mail correcte");
		}
		else if (idi == "en") {
			alert("Enter a correct e-mail");
		}
		dato.focus()
		return false;
	}
	else {
		return true;
	}
};

