window.onload = function() 
{ 
	try
	{
		document.getElementById('ResolucionX').value = window.screen.width;
		document.getElementById('ResolucionY').value = window.screen.height;
	}
	catch(err){}	
}


function popup(url,windowname,width,height,features) { 

width=(width)?width:screen.width/2; 
height=(height)?height:screen.height/2; 
var screenX = (screen.width/2 - width/2); 
var screenY = (screen.height/2 - height/2); 
var features= "width=" + width + ",height=" + height +",scrollbars=no,status=no"; 
features += ",screenX=" + screenX + ",left=" + screenX; 
features += ",screenY=" + screenY + ",top=" + screenY; 

var mywin=window.open(url, windowname, features); 
if (mywin) 
mywin.focus(); 
return mywin; 
}

function abrirAviso()
{
	window.open('aviso.asp','nuevodis','resizable=1,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,width=617,height=460');
}

function validar_email(email){
	var str = email.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(str)){
		return true;
	} else {
		return false;
	}
}

var es_telefono1=/^(6|8|9)[0-9]{8}$/;
var es_telefono2=/0{4,}|1{4,}|2{4,}|3{4,}|4{4,}|5{4,}|6{4,}|7{4,}|8{4,}|9{4,}/;
var es_cp=/([0-4][0-9]{4})|(5[0-2][0-9]{3})/;

function enviardatos()
{
	// CODIGOS ACENTOS
	//var chars = "áéíóú"; 
	//var chars = "\xe1\xe9\xed\xf3\xfa"
	
	var form = document.forms.frmAdmin;

	alerta = "";
	
	if (form.nombre.value == "") 
	{
		alerta += "- Debes introducir tu nombre.\n";
	}
	else
	{
		MiNombre = trim(String(form.nombre.value));
		alerta += CheckTextoSinCifras( MiNombre, "Nombre" );
		alerta += CheckTextoSinCaracRepetidos( MiNombre, "Nombre" );
	}
	
	if (form.apellidos.value == "") 
	{
		alerta += "- Debes introducir tus apellidos.\n";
	}
	else
	{
		MiApellido = trim(String(form.apellidos.value));
		alerta += CheckTextoSinCifras( MiApellido, "Apellidos" );
		alerta += CheckTextoSinCaracRepetidos( MiApellido, "Apellidos" );
	}
	
	if (form.dia.value == "") 
	{
		alerta += "- Debes introducir el d\xeda de tu fecha de nacimiento.\n";
	}
	
	if (form.mes.value == "") 
	{
		alerta += "- Debes introducir el mes de tu fecha de nacimiento.\n";
	}
	
	if (form.ano.value == "") 
	{
		alerta += "- Debes introducir el a\xF1o de tu fecha de nacimiento.\n";
	}
	
	if (!form.sexo[0].checked && !form.sexo[1].checked) {
		alerta += "- Debes seleccionar el sexo.\n";
	}
		

	//alert(String(form.pais.options[form.pais.selectedIndex].value).substr(0,4));

	if ( String(form.pais.options[form.pais.selectedIndex].value).substr(0,4) == "Espa" ) 
	{

		// *****************************************************
		// **** TELEFONO 1 *************************************
		// *****************************************************
		MiTelefono = trim(String(form.telefono.value));
		
		if ( MiTelefono == "" ) 
		{
			alerta += "- El Tel\xe9fono1 es obligatorio (Fijo o M\xf3vil).\n";
		}
		else
		{
			alerta += CheckTelefono( MiTelefono, "Tel\xe9fono1" );
		}
		
		// *****************************************************
		// **** TELEFONO 2 *************************************
		// *****************************************************
		MiTelefono = trim(String(form.telefono2.value));
		if ( MiTelefono != "" ) 
		{
			alerta += CheckTelefono( MiTelefono, "Tel\xe9fono2" );
		}
		
		/*
		hayTelefono1 = false;
		hayTelefono2 = false;
		if (form.telefono.value) {hayTelefono1 = true;}
		if (form.telefono2.value) {hayTelefono2 = true;}
		
		if ((!hayTelefono1) && (!hayTelefono2)) 
		{
			alerta += "- Introduce tu Tel\xe9fono. Uno de los dos Tel\xe9fonos es obligatorio (Fijo o M\xf3vil).\n";
		}

		if (hayTelefono1) 
		{
			valorTelefono1 = form.telefono.value;
			form.telefono.value = valorTelefono1.replace(/[^0-9]/g,"");
			if(!es_telefono1.test(form.telefono.value))   //Comprueba Telefono Fijo
			{ 
				alerta += "- Debe de ser un n\xfamero de nueve cifras que empiece por 8 \xf3 9.\n";
			} 
		}

		// campo telefono2
		if (hayTelefono2) 
		{
			valorTelefono2 = form.telefono2.value;
			form.telefono2.value = valorTelefono2.replace(/[^0-9]/g,"");
			if(!es_telefono1.test(form.telefono2.value))   //Comprueba Telefono Móvil
			{ 
			    //alerta += "- El n\xfamero de Tel\xe9fono Móvil debe tener nueve cifras.\n";
				alerta += "- El n\xfamero de Tel\xe9fono M\xf3vil debe tener nueve cifras.\n";
			} 
		}
		*/
		
		if (!validar_email(form.email)) 
		{
			alerta += "- Debes introducir una direcci\xf3n de e-mail correcta.\n";
		}

	
	}   //si es extranjero	
	else 
	{
		if ( (form.telefono.value == "") ) 
		{
			alerta += "- El Tel\xe9fono1 es obligatorio (Fijo o M\xf3vil).\n";
		}
	}
	
	if (form.horario.value == "") {
		alerta += "- Debes seleccionar el horario de contacto.\n";
	}
	
	if (!form.chkleido.checked) {
		alerta += "- Debes leer el aviso legal y aceptarlo.\n";
	}
	if (alerta) {
		alert("Se han encontrado errores en el formulario:\n\n" + alerta);
	} else {
		form.submit();
	}
}

function trim(str)
{
		var i,j;
		for(var i=0;i<str.length;i++)if(str.charAt(i)!=' ')break;	
		for(var j=str.length-1;j>=0;j--)if(str.charAt(j)!=' ')break;
		if(i>j)return '';
		return str.substr(i,j-i+1);
}

function CheckTelefono( sParameter, sName )
{
	sMessage = ""

	if( sParameter.length != 9 )
	{
			sMessage = "- El " + sName + " debe ser un n\xfamero de nueve cifras.\n";
	}
	else
	{
			var Correcto = true;
			var i;
			for(i=0; i<sParameter.length; i++)
			{
				if( (sParameter.charAt(i) != '0') && (sParameter.charAt(i) != '1') && (sParameter.charAt(i) != '2') && (sParameter.charAt(i) != '3') && (sParameter.charAt(i) != '4') && ( sParameter.charAt(i) != '5') &&(sParameter.charAt(i) != '6') &&(sParameter.charAt(i) != '7') &&(sParameter.charAt(i) != '8') && (sParameter.charAt(i) != '9') )
				{
					sMessage = "- El " + sName + " debe ser un n\xfamero de nueve cifras.\n";
					Correcto = false;
					break;	
				}	
			}	
			
			if ( Correcto )
			{
				MiUltimoCaracter = sParameter.charAt(0);
				Contador = 1;
				for(i=1; i<sParameter.length; i++)
				{
					if ( sParameter.charAt(i) == MiUltimoCaracter )
					{
						Contador++;
						if ( Contador >= 4 )
						{
							sMessage = "- El " + sName + " no puede contener cuatro cifras iguales seguidas.\n";
							break;
						}
					}
					else
					{
						Contador = 1;
						MiUltimoCaracter = sParameter.charAt(i);
					}
				}
			}
	}
	
	
	return (sMessage);
		
}

function CheckTextoSinCifras( sParameter, sName )
{
	sMessage = ""

	var i;
	for(i=0; i<sParameter.length; i++)
	{
		if( (sParameter.charAt(i) == '0') || (sParameter.charAt(i) == '1') || (sParameter.charAt(i) == '2') || (sParameter.charAt(i) == '3') || (sParameter.charAt(i) == '4') || ( sParameter.charAt(i) == '5') || (sParameter.charAt(i) == '6') || (sParameter.charAt(i) == '7') || (sParameter.charAt(i) == '8') || (sParameter.charAt(i) == '9') )
		{
			sMessage = "- El " + sName + " no debe contener cifras.\n";
			break;	
		}	
	}
	return (sMessage);
}


function CheckTextoSinCaracRepetidos( sParameter, sName )
{
	sMessage = ""
	
	MiUltimoCaracter = sParameter.charAt(0);
	Contador = 1;
	for(i=1; i<sParameter.length; i++)
	{
			if ( sParameter.charAt(i) == MiUltimoCaracter )
			{
				Contador++;
				if ( Contador >= 4 )
				{
					sMessage = "- El " + sName + " no puede contener cuatro car\xe1cteres iguales seguidos.\n";
					break;
				}
			}
			else
			{
				Contador = 1;
				MiUltimoCaracter = sParameter.charAt(i);
			}

	}
	return (sMessage);
}

