//Abre la página que se le pasa como argumento en un popUp y lo centra.
function AbrirPopUp(direccion, nombre, anchura, altura){
	var centrarX = (screen.width - anchura) / 2;
	var centrarY = (screen.height - altura) / 2;
	var opciones = "fullscreen=0, toolbar=0, location=0, status=0, menubar=0, scrollbars=1, resizable=0, width="+ anchura +", height="+ altura +", left="+ centrarX +", top="+ centrarY;
	window.open(direccion, nombre, opciones, 1);
}


//Controla los enlaces del menú de todas las páginas.
function HandMenu(element, clrLink, img){
	element.src = "archivos/txtImgs/txtImgs_" + clrLink + "/txt" + img + ".png";
}


//Devuelve la altura del area de cliente.
function MedidasVntn()
{
	var medidasVntn = [];
	if(typeof(window.innerWidth) == 'number')
	{
		// No es IE
		medidasVntn = [window.innerWidth, window.innerHeight];
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		//IE 6 en modo estandar (no quirks)
		medidasVntn = [document.documentElement.clientWidth, document.documentElement.clientHeight];
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		//IE en modo quirks
		medidasVntn = [document.body.clientWidth, document.body.clientHeight];
	}
	return medidasVntn;
}


//Valida el formulario de la sección "Contacto y localización".
function ValidForm(idioma){
	var pszErrores = "";
	var nErrores = 0;
	
	//Validación del nombre.
	var element = document.getElementById("nombre"); 
	//Comprueba que se ha rellenado el campo nombre.
	if(!(/^(.){1,}$/.test(element.value))){
		if(idioma == "es")pszErrores = "\n- No has rellenado el campo obligatorio \"Nombre\".";
		if(idioma == "en")pszErrores = "\n- You've not completed the mandatory field \"Name\".";
		nErrores++;
	}

	//Validación de los apellidos.
	element = document.getElementById("apellidos"); 
	//Comprueba que se ha rellenado el campo nombre.
	if(!(/^(.){1,}$/.test(element.value))){
		if(pszErrores == 0){
			if(idioma== "es"){pszErrores = "\n- No has rellenado el campo obligatorio \"Apellidos\".";}
			if(idioma == "en")pszErrores = "\n- You've not completed the mandatory field \"Surnames\".";
		}
		else{
			if(idioma == "es")pszErrores += "\n- No has rellenado el campo obligatorio \"Apellidos\".";
			if(idioma == "en")pszErrores += "\n- You've not completed the mandatory field \"Surnames\".";
		}
		nErrores++;
	}

	//Validación del código postal (si lo hay).
	element = document.getElementById("c_postal") 
	if(element.value){
		//Comprueba que se ha rellenado el campo nombre.
		if(!(/^[0-9]{5}$/.test(element.value))){
			if(idioma == "es")pszErrores += "\n- El código postal que has introducido es incorrecto.";
			if(idioma == "en")pszErrores += "\n- The zip code you entered is incorrect.";
			nErrores++;
		}
	}
	
	//Validación del número de teléfono.
	element = document.getElementById("telefono");
	//Comprueba que se ha rellenado el campo "Teléfono".
	if(!(/^(.){1,}$/.test(element.value))){
		if(idioma == "es")pszErrores += "\n- No has rellenado el campo obligatorio \"Teléfono\".";
		if(idioma == "en")pszErrores += "\n- You've not completed the mandatory field \"Telephone\".";
		nErrores++;
	}
	else{
		//Comprueba que el teléfono introducido es correcto.
		if(!(/^[0-9]{9}$/.test(element.value))){
			if(idioma == "es")pszErrores += "\n- El número de teléfono que has introducido es incorrecto.";
			if(idioma == "en")pszErrores += "\n- The phone number you entered is incorrect.";
			nErrores++;
		}
	}
	
	//Validación del e-mail.
	element = document.getElementById("e_mail"); 
	//Comprueba que se ha rellenado el campo "E-Mail".
	if(!(/^(.){1,}$/.test(element.value))){
		if(idioma == "es")pszErrores += "\n- No has rellenado el campo obligatorio \"E-Mail\".";
		if(idioma == "en")pszErrores += "\n- You've not completed the mandatory field \"E-Mail\".";
		nErrores++;
	}
	else{
		//Comprueba que el E-Mail introducido es correcto.
		if(!(/^(.){1,}[@](.){1,}[.](.){1,5}$/.test(element.value))){
			if(idioma == "es")pszErrores += "\n- El E-Mail que has introducido es incorrecto.";
			if(idioma == "en")pszErrores += "\n- The E-Mail you entered is incorrect.";
			nErrores++;
		}
	}

	
	element = document.getElementsByName("modo_de_contacto");
	for(i=0; element[i]; i++){
		if(element[i].checked){
			element = element[i].value;
			break;	
		}
	}
	if(element == "Fax"){
		if(document.getElementById("fax").value == ""){
			if(idioma == "es")pszErrores += "\n- Has seleccionado \"Fax\" como modo de contacto, pero no has indicado tu fax en el formulario.";
			if(idioma == "en")pszErrores += "\n- You've selected \"Fax\" how contact mode, but you has not indicated your fax in the form.";
			nErrores++;
		}
	}
	if(element == "Correo"){
		var nErrAquiSinInd = 0;
		if(document.getElementById("direccion").value == "") nErrAquiSinInd++;
		if(document.getElementById("localidad").value == "") nErrAquiSinInd++;
		if(document.getElementById("provincia").value == "") nErrAquiSinInd++;
		if(document.getElementById("c_postal").value == "") nErrAquiSinInd++;
		if(nErrAquiSinInd > 0){
			if(idioma == "es"){
				if(nErrAquiSinInd > 1) pszErrores += "\n- Has seleccionado \"Correo\" como modo de contacto, pero no has rellenado los campos necesarios ";
				else if(nErrAquiSinInd == 1) pszErrores += "\n- Has seleccionado \"Correo\" como modo de contacto, pero no has rellenado el campo necesario ";
				if(document.getElementById("direccion").value == ""){
					pszErrores += "\"dirección\"";
					nErrAquiSinInd--;
					if(nErrAquiSinInd > 1) pszErrores += ", ";
					else if(nErrAquiSinInd == 1) pszErrores += " y ";
				}
				if(document.getElementById("localidad").value == ""){
					pszErrores += "\"localidad\"";
					nErrAquiSinInd--;
					if(nErrAquiSinInd > 1) pszErrores += ", ";
					else if(nErrAquiSinInd == 1) pszErrores += " y ";
				}
				if(document.getElementById("provincia").value == ""){
					pszErrores += "\"provincia\"";
					nErrAquiSinInd--;
					if(nErrAquiSinInd > 0) pszErrores += " y ";
				}
				if(document.getElementById("c_postal").value == "") pszErrores += "\"código postal\"";
				pszErrores += ".";
			}
			
			else if(idioma == "en"){
				if(nErrAquiSinInd > 1) pszErrores += "\n- You has selected \"Mail\" how contact mode, but you has not indicated the necessaries fields ";
				else if(nErrAquiSinInd == 1) pszErrores += "\n- You've selected \"Mail\" how contact mode, but you has not indicated the necessarie field ";
				if(document.getElementById("direccion").value == ""){
					pszErrores += "\"address\"";
					nErrAquiSinInd--;
					if(nErrAquiSinInd > 1) pszErrores += ", ";
					else if(nErrAquiSinInd == 1) pszErrores += " y ";
				}
				if(document.getElementById("localidad").value == ""){
					pszErrores += "\"city\"";
					nErrAquiSinInd--;
					if(nErrAquiSinInd > 1) pszErrores += ", ";
					else if(nErrAquiSinInd == 1) pszErrores += " y ";
				}
				if(document.getElementById("provincia").value == ""){
					pszErrores += "\"state\"";
					nErrAquiSinInd--;
					if(nErrAquiSinInd > 0) pszErrores += " and ";
				}
				if(document.getElementById("c_postal").value == "") pszErrores += "\"post code\"";
				pszErrores += ".";
			}
			nErrores++;
		}
	}
	
	
	//Si "nErrores" vale 0 se envía el formulario.
	if(!nErrores) return true;
	//Si no vale 0...
	else{
		//No se envía el formulario.
		//Se muestran los valores de las variables "nErrores" y "pszErrores".
		var alerta;
		if(idioma == "es")alerta = "Hemos encontrado " + nErrores + " error/es:\n";
		if(idioma == "en")alerta = "We've found " + nErrores + " error/s:\n";
		alert(alerta + pszErrores);
		return false;
	}
}