//funcion para que solo introduzca numeros
var nav4 = window.Event ? true : false;
function detectBrowser() {
    var ie = document.all != undefined;
    var opera = window.opera != undefined;
   
    if (opera) return "opera";
    if (ie) return "ie";
    if ((window)&&(window.netscape)&&(window.netscape.security)) {
      if (window.XML) {
        return "firefox15";
      }
      else return "firefox10";
    }
    return "ie";      // Si no sabemos que navegador es, devolvemos ie.
 }
function acceptNum(evt){
	if(detectBrowser() == "ie"){return;}
    // NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
    var key = nav4 ? evt.which : evt.keyCode;
    return (key <= 13 || (key >= 48 && key <= 57));
}

function validarNac(valor){
	var dia = valor.substring(0,2);
	var dialim=31;
	var mes = valor.substring(3,5);
	var meslim=12;
	var anio = valor.substring(6,10);
	var anioini=1900;
	var today = new Date();
	var aniolim = today.getFullYear();
	if(!(parseInt(dia) >= 01 && parseInt(dia) <= parseInt(dialim))){
		return false;
	}else{
		if(!(parseInt(mes) >= 01 && parseInt(mes) <= parseInt(meslim))){
			return false;
		}else{
			if(!(parseInt(anio) >= parseInt(anioini) && parseInt(anio) <= parseInt(aniolim-18))){
				return false;
			}		
		}
	}
	return true;
}

function validarCP(valor){
	//alert("asdfa: "+valor);
	if(valor.length != 5){return false;}else{return true}
	/*
	var dia = valor.substring(0,1);
	var mes = valor.substring(2,3);
	*/
}

function ValidateMail(valor) {
	if (valor != "")
	{	
		//var exp = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*@[a-zA-Z0-9\-]{2,200}\.[a-zA-Z]{2,6}$/;
        var exp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        
		if (exp.test(valor)){
			return true;
		} else {
			return false;
		}
	}
	else
		return true;
}

function ValidateMovilPhone(valor){
	if (valor != ""){	
		if (/[0-9]/.test(valor)){
			//alert('validate phone OK TRUE');
			if(valor.length != 9 ){return false;}
			if(valor.substring(0,1) == 6){return true;}
			return false;
		} else {
			return false;
		}
	}else
		return true;
}

function ValidatePhone(valor){
	if (valor != ""){	
		if (/[0-9]/.test(valor)){
			//alert('validate phone OK TRUE');
			if(valor.length != 9 ){return false;}
			if(valor.substring(0,1) == 9 || valor.substring(0,1) == 8){return true;}
			return false;
		} else {
			return false;
		}
	}else
		return true;
}

function valida_nif_cif_nie(a) 
{
	var temp=a.toUpperCase();
	var cadenadni="TRWAGMYFPDXBNJZSQVHLCKE";
 
	if (temp!==''){
		//si no tiene un formato valido devuelve error
		if ((!/^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$/.test(temp) && !/^[T]{1}[A-Z0-9]{8}$/.test(temp)) && !/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			return 0;
		}
 
		//comprobacion de NIFs estandar
		if (/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			posicion = a.substring(8,0) % 23;
			letra = cadenadni.charAt(posicion);
			var letradni=temp.charAt(8);
			if (letra == letradni)
			{
			   	return 1;
			}
			else
			{
				return -1;
			}
		}
 
		//algoritmo para comprobacion de codigos tipo CIF
		suma = parseInt(a[2])+parseInt(a[4])+parseInt(a[6]);
		for (i = 1; i < 8; i += 2)
		{
			temp1 = 2 * parseInt(a[i]);
			temp1 += '';
			temp1 = temp1.substring(0,1);
			temp2 = 2 * parseInt(a[i]);
			temp2 += '';
			temp2 = temp2.substring(1,2);
			if (temp2 == '')
			{
				temp2 = '0';
			}
 
			suma += (parseInt(temp1) + parseInt(temp2));
		}
		suma += '';
		n = 10 - parseInt(suma.substring(suma.length-1, suma.length));
 
		//comprobacion de NIFs especiales (se calculan como CIFs)
		if (/^[KLM]{1}/.test(temp))
		{
			if (a[8] == String.fromCharCode(64 + n))
			{
				return 1;
			}
			else
			{
				return -1;
			}
		}
 
		//comprobacion de CIFs
		if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(temp))
		{
			temp = n + '';
			if (a[8] == String.fromCharCode(64 + n) || a[8] == parseInt(temp.substring(temp.length-1, temp.length)))
			{
				return 2;
			}
			else
			{
				return -2;
			}
		}
 
		//comprobacion de NIEs
		//T
		if (/^[T]{1}/.test(temp))
		{
			if (a[8] == /^[T]{1}[A-Z0-9]{8}$/.test(temp))
			{
				return 3;
			}
			else
			{
				return -3;
			}
		}
 
		//XYZ
		if (/^[XYZ]{1}/.test(temp))
		{
			pos = str_replace(['X', 'Y', 'Z'], ['0','1','2'], temp).substring(0, 8) % 23;
			if (a[8] == cadenadni.substring(pos, pos + 1))
			{
				return 3;
			}
			else
			{
				return -3;
			}
		}
	}
 
	return 0;
}
function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
 
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
 
    return sa ? s : s[0];
}
