// JavaScript Document

function abrirMeGusta () {
	jQuery.facebox({ ajax: 'meGusta.htm' });
}

function abrirAmigos () {
	jQuery.facebox({ ajax: 'amigos.html' });
}

function abrirAmigosEn () {
	jQuery.facebox({ ajax: 'amigosEn.html' });
}

function abrirAmigosVal () {
	jQuery.facebox({ ajax: 'amigosVal.html' });
}

function abrirAviso () {
	newWindow ('aviso_legal.html', 'ventana', 570, 400, 'yes', true, false, false, false, false, false);
}

function abrirAvisoVal () {
	newWindow ('avis_legal.html', 'ventana', 570, 400, 'yes', true, false, false, false, false, false);
}

function abrirPrivacidad () {
	newWindow ('privacidad.html', 'ventana', 570, 200, 'yes', true, false, false, false, false, false);
}

function abrirPrivacidadVal () {
	newWindow ('privacitat.html', 'ventana', 570, 200, 'yes', true, false, false, false, false, false);
}

function abrirCreditos () {
	newWindow ('creditos.html', 'ventana', 570, 200, 'yes', true, false, false, false, false, false);
}

function abrirCreditosVal () {
	newWindow ('creditos_val.html', 'ventana', 570, 200, 'yes', true, false, false, false, false, false);
}

function abrirCreditosEn () {
	newWindow ('creditos_en.html', 'ventana', 570, 200, 'yes', true, false, false, false, false, false);
}

function compartirFacebook () {
	newWindow ('http://www.facebook.com/sharer.php?u=http://www.chirivellasoriano.org', 'ventana', 650, 400, 'yes', true, false, false, false, false, false);
}

function newWindow(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen) {
	var int_windowLeft = (screen.width - a_int_windowWidth) / 2;
	var int_windowTop = (screen.height - a_int_windowHeight) / 2;
	
	var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
	
	var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
	
	if (parseInt(navigator.appVersion) >= 4) {
		obj_window.window.focus();
	}
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  var dimensiones = new Array();
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
   	//IE 4 compatible
	myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  dimensiones [0] = myWidth;
  dimensiones [1] = myHeight;
  
  return (dimensiones);
}


function sobreForm ($str, $txt) {
	if ($str.value == $txt) {
		$str.value = "";
	}
}

function fueraForm ($str, $txt) {
	if ($str.value == '') {
		$str.value = $txt;
	}
}

function comprobarFormulario (nom) {
	var fallo = true;
	var idi = "es";
	
fallo = noVacio(nom.nombre,"Nombre*",idi);
	
	if (fallo == true) {
		fallo = noVacio(nom.apellidos,"Apellidos*",idi);
		if (fallo == true) {
			fallo = noVacio (nom.direccion, "Direcci\u00f3n*",idi);
			if (fallo == true) {
				fallo = noVacio (nom.poblacion, "Poblaci\u00f3n*",idi);
				if (fallo == true) {
					fallo = noVacio (nom.cp, "CP*",idi);
					if (fallo == true) {
						fallo = esNumero(nom.cp, "CP*",idi);
						if (fallo == true) {
							fallo = noVacio (nom.telefono, "Tel\u00e9fono*",idi);
							if (fallo == true) {
								fallo = esNumero (nom.telefono, "Tel\u00e9fono*",idi);
								if (fallo == true) {
									fallo = esMail(nom.email,idi);
								}
							}
						}
					}
				}
			}
		}
	}

	if (fallo) {
		var nombre = nom.nombre.value + ' ' + nom.apellidos.value;
		var direccion = nom.direccion.value + ', ' + nom.cp.value + ', ' + nom.poblacion.value;
		var telefono = nom.telefono.value;
		var email = nom.email.value;
		var edad = nom.edad.value;
		var profesion = nom.profesion.value;
		var dni = nom.dni.value;
		
		var cadena = 'nombre='+nombre+'&direccion='+direccion+'&telefono='+telefono+'&email='+email+'&edad='+edad+'&profesion='+profesion+'&dni='+dni;
		
		$.ajax({
			type: "GET",
			url: "http://www.aptitudelab.com/webs/amigos_enviar.php",
			data: cadena,
			cache: false
		});
		
		alert ('Formulario enviado correctamente. En breve nos pondremos en contacto con usted.');
		$.facebox.close();
	}
	
	return (false);
}

function comprobarFormularioVal (nom) {
	var fallo = true;
	var idi = "val";
	
fallo = noVacio(nom.nombre,"Nom*",idi);
	
	if (fallo == true) {
		fallo = noVacio(nom.apellidos,"Cognoms*",idi);
		if (fallo == true) {
			fallo = noVacio (nom.direccion, "Adre\u00e7a*",idi);
			if (fallo == true) {
				fallo = noVacio (nom.poblacion, "Poblaci\u00f3*",idi);
				if (fallo == true) {
					fallo = noVacio (nom.cp, "CP*",idi);
					if (fallo == true) {
						fallo = esNumero(nom.cp, "CP*",idi);
						if (fallo == true) {
							fallo = noVacio (nom.telefono, "Tel\u00e8fon*",idi);
							if (fallo == true) {
								fallo = esNumero (nom.telefono, "Tel\u00e8fon*",idi);
								if (fallo == true) {
									fallo = esMail(nom.email,idi);
								}
							}
						}
					}
				}
			}
		}
	}

	if (fallo) {
		var nombre = nom.nombre.value + ' ' + nom.apellidos.value;
		var direccion = nom.direccion.value + ', ' + nom.cp.value + ', ' + nom.poblacion.value;
		var telefono = nom.telefono.value;
		var email = nom.email.value;
		var edad = nom.edad.value;
		var profesion = nom.profesion.value;
		var dni = nom.dni.value;
		
		var cadena = 'nombre='+nombre+'&direccion='+direccion+'&telefono='+telefono+'&email='+email+'&edad='+edad+'&profesion='+profesion+'&dni='+dni;
		
		$.ajax({
			type: "GET",
			url: "amigos_enviar.php",
			data: cadena,
			cache: false
		});
		
		alert ('Formulari enviat correctament. Ens posarem en contacte amb vost\u00e8 tan prompte com ens siga possible.');
		$.facebox.close();
	}
	
	return (false);
}

function comprobarFormularioEn (nom) {
	var fallo = true;
	var idi = "en";
	
	fallo = noVacio(nom.nombre,"Name*",idi);
	
	if (fallo == true) {
		fallo = noVacio(nom.apellidos,"Surname*",idi);
		if (fallo == true) {
			fallo = noVacio (nom.direccion, "Address*",idi);
			if (fallo == true) {
				fallo = noVacio (nom.poblacion, "City*",idi);
				if (fallo == true) {
					fallo = noVacio (nom.cp, "Post Code*",idi);
					if (fallo == true) {
						fallo = esNumero(nom.cp, "Post Code*",idi);
						if (fallo == true) {
							fallo = noVacio (nom.telefono, "Telephone*",idi);
							if (fallo == true) {
								fallo = esNumero (nom.telefono, "Telephone*",idi);
								if (fallo == true) {
									fallo = esMail(nom.email,idi);
								}
							}
						}
					}
				}
			}
		}
	}

	if (fallo) {
		var nombre = nom.nombre.value + ' ' + nom.apellidos.value;
		var direccion = nom.direccion.value + ', ' + nom.cp.value + ', ' + nom.poblacion.value;
		var telefono = nom.telefono.value;
		var email = nom.email.value;
		var edad = nom.edad.value;
		var profesion = nom.profesion.value;
		var dni = nom.dni.value;
		
		var cadena = 'nombre='+nombre+'&direccion='+direccion+'&telefono='+telefono+'&email='+email+'&edad='+edad+'&profesion='+profesion+'&dni='+dni;
		
		$.ajax({
			type: "GET",
			url: "http://www.aptitudelab.com/webs/amigos_enviar.php",
			data: cadena,
			cache: false
		});
		
		alert ('Form sent successfully. We will contact you shortly.');
		$.facebox.close();
	}
	
	return (false);
}

function noVacio(dato,campo,idi) {
	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 '+campo);
		}
		focus(dato);
		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") {
				alert("Enter only numbers in the field "+campo);
			}
			focus(dato);
			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");
		}
		focus(dato);
		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");
		}
		focus(dato);
		return false;
	}
	else {
		return true;
	}
}

