//Funcion cursor mano
function cursorHand(){
	var item = event.srcElement;
	item.style.cursor = "pointer";
}

//Funciones teclado virtual
function doNum(num)
{
	var passField;
	passField = document.getElementById("fCamDirectoPass");

	if(passField != null)
	{
		passField.value += num.getAttribute("alt");
	}
}

function showTeclado(mostrar)
{
	var teclado;
	teclado=eval(document.getElementById("fCamDirectoTeclado"));
	if (teclado && mostrar)
	{
		teclado.style.display='block';
	}
	else
	{
		teclado.style.display='none';
	}
	
}
	
function resetPass()
{
	var passField;
	showTeclado(false);
	
	passField = document.getElementById("fCamDirectoPass");

	if(passField != null)
	{
		passField.value = "";
	}
}

//funciones validación datos de entrada

function validarDatos(userid,passwd,lang)
{
	var msg,msgValidar;
	var ok;
	var usuario, clave, idioma;
	var minN =String.fromCharCode(241);
	var minA= String.fromCharCode(225);
	var minvA= String.fromCharCode(224);
	ok=false;
	//Comprobamos si se ha introducido usuario y contraseña
	//Personalizamos los mensajes dependiendo del idioma
	//Comprobamos que no se ha introducido caracteres distintos de letras y numeros en
	//el usuario, contraseña e idioma, además de comprobar que no excede su longitud permitidda

	switch(lang)
	{
		 case "ES": 
				  	if (userid==''|| passwd=='')
					{
				  		msg="Deber rellenar el usuario y la contrase" + minN + "a";
		  				alert(msg);
						ok=false;
					}
					else
					{
						usuario="usuario: ";
						clave="contrase" + minN + "a: ";
						idioma="idioma: ";
						msgValidar= "Car" + minA + "cter no v" + minA + "lido en ";
						ok=true;
					}
					break;						
		 case "VAL":	  
					if (userid==''|| passwd=='')
					{
				  		msg="Ha d' introduir l'usuari i la contrasenya";
		  				alert(msg);
						ok=false;
					}
					else
					{
						usuario="usuari: ";
						clave="contrasenya: ";
						idioma="idioma: ";
						msgValidar= "Car" + minvA + "cter no v" + minvA + "lid en ";
						ok=true;
					}
					break;
		  case "EN":  
					if (userid==''|| passwd=='')
					{
				  		msg="Please fill in your username and password";
		  				alert(msg);
						ok=false;
					}
					else
					{
						usuario="user: ";
						clave="pasword: ";
						idioma="language: ";
						msgValidar= "Symbol not valid in ";
						ok=true;
					}
					break;
		   default: 
				  	if (userid==''|| passwd=='')
					{
				  		msg="Deber rellenar el usuario y la contrase" + minN + "a";
		  				alert(msg);
						ok=false;
					}	
					else
					{
						usuario="usuario: ";
						clave="contrase" + minN + "a: ";
						idioma="idioma: ";
						msgValidar= "Car" + minA + "cter no v" + minA + "lido en ";
						ok=true;
					}		
	}

	if(ok)
	{
		if (userid.length<=15 && passwd.length<=8 && lang.length<=3)
		{
			ok=validarCaracteres(userid, msgValidar + usuario);
			if (ok)
			{
				ok=validarCaracteres(passwd, msgValidar + clave);
				if(ok)
				{
					ok=validarCaracteres(lang, msgValidar + idioma);
				}
			}
		}
		else
		{
			ok=false;
		}				
	}

	return ok;
}


function validarCaracteres(alias,msg) 
{ 
	var a=alias;
	var i;
	var minN =String.fromCharCode(241);
	var mayN =String.fromCharCode(209);
	var minA= String.fromCharCode(225);
	
	for (i=0; i< a.length; i++) 
	{ 
       if ((a.charAt(i) < 'a' || a.charAt(i) > 'z') && 
       (a.charAt(i) < 'A' || a.charAt(i) > 'Z') && 
       (a.charAt(i) < '0' || a.charAt(i) > '9')  && 
	   (a.charAt(i) != '' + minN + '') && (a.charAt(i) != '' + mayN + '') && (a.charAt(i) != ' '))
	   { 
			alert(msg + a.charAt(i)); 
   			return false; 
		} 
	} 
    return true; 
 }
 
function validar(idioma){
	
	var userId,passwd;
	var ok;

	userId=document.getElementById("fuserid");
	passwd=document.getElementById("fCamDirectoPass");
	
	ok=validarDatos(userId.value,passwd.value,idioma);
	if (ok)
	{
		document.forms[0].action='https://seguro.cam.es/camd/SvlLoginCAM';
		document.forms[0].submit();
		userId.value = "";
		passwd.value = "";
		return false;
	}
	else
	{
		userId.value = "";
		passwd.value = "";
		return false;
	}

}

//Funcion detección enter	
function eventoEnter(e,idioma) {
	var ok;
	if(window.Event) {
		var codigo= e.which;
	}
	else {
		if(e.type=="keypress") {
			var codigo= e.keyCode;
		}
	} 
	if(e.type=="keypress" && codigo==13)    
	{
		ok=validar(idioma);
		e.returnValue=false;
		return ok;
	}
}

function rejectEnter(e)
{
	if(window.Event) {
		var codigo= e.which;
	}
	else {
		if(e.type=="keypress") {
			var codigo= e.keyCode;
		}
	} 
	if(e.type=="keypress" && codigo==13)    
	{
		e.returnValue=false;
		return false;
	}
}



//Funcion acceso por dni digital
function cargaCertificado(){

    var userAgent=navigator.userAgent.toLowerCase();

    if (userAgent.indexOf("msie") != -1) {
		try{
          	document.execCommand("ClearAuthenticationCache", false);

       	} catch (ex) {}
    }
    top.location.href = "https://seguro.cam.es/camd/SvlLoginCAM?accion=login&certificado=si";
}




 

