function replace (texto, s1, s2) {
	return texto.split(s1).join(s2);
}
function analiza(cadena,cSpecials,dosseguidos){

    var i=0;
    var Existextrano=false;
    reservadas="ABUSE@,POSTMASTER@,NOSPAM@,@ANTISPAM.,HOSTMASTER@,ROOT@,NOC@,SECURITY@,ANTISPAM@,IPADMIN@";
    if(reservadas.indexOf(cadena)!=-1){
                return false;
    }

    for(i=0;i<cadena.length;i++){
                datoA=cadena.charAt(i);
                datoACode=cadena.charCodeAt(i);
                if(datoACode<48 || (datoACode>57 && datoACode<65) || datoACode>90){
                           if(cSpecials.indexOf(datoA)==-1){
                                       return false;
                           }
                           else{
                                       if(Existextrano){
                                                   if(!dosseguidos){
                                                               return false;
                                                   }
                                       }
                                       Existextrano=true;
                           }
                }
                else{
                           Existextrano=false;
                }
    }
    return true;
}

function longitudPartes(matrizDom){

    dominiosNo="COM,NET,ORG,INT";
    noDominios="ARPA,MIL";

    var i=0;
    var ExistDomi=false;
    var reexp=/\d/;

    for(i=0;i<matrizDom.length;i++){
                if(noDominios.indexOf(matrizDom[i])!=-1 && matrizDom[i].length>2){
                           return false;
                }

                if(dominiosNo.indexOf(matrizDom[i])!=-1 && matrizDom[i].length==3){
                           if(ExistDomi){
                                       return false;
                           }
                           ExistDomi=true;
                }

                
                if(i==matrizDom.length-1 || (i==matrizDom.length-2 && matrizDom.length>2)){
                           if(matrizDom[i].length<2){
                                       return false;
                           }
                }
                if(i==matrizDom.length-1){
                           if(matrizDom[i].match(reexp) || matrizDom[i].length>6){
                                       return false;
                           }
                }
    }            

    return true;        
}


function checkEmail (campo) {
	
	// regexp
    var mailexp = /.[^@]*@[\w]*[\w\.-][\w]*\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]/;
	
	Strmail = campo.value.toUpperCase ();
	mailDepurado = Strmail.match (mailexp);
	if (mailDepurado != Strmail) {
		return false;
	}
	partesMail=Strmail.split('@');
	if (!analiza (partesMail[0],".,-,_,!,#,$,%,&,',+,/,=,?,`,{,},|,~", true)) {
		return false;
	}
	segPartMail=partesMail[1].split ('.');
	if (!longitudPartes (segPartMail)) {
		return false;
	}
	if (!analiza("@"+segPartMail[0]+".","@,.,-",false)) {
		return false;
	}
	return true;
}

function checkSelect (campo) {
	if (campo.options.selectedIndex < 1) {
		if (campo.value==-1 || campo.value==0 || campo.value=="") {
			return false;
		}
	}
	return true;
}

function checkPhone (campo) {
	
	var txt = replace (campo.value, ' ', '');
	var txt = replace (txt, '-', '');
	campo.value = txt;
	
	longitud = campo.value.length;
	
	var regexp = /^(\+{0,1}[0-9]+)$/;
	
	if (regexp.test(campo.value) == false || longitud > 15) {
		return false;
	}
	
	return true;
}  

function verificamail(campo,mensaje){
    /*expresion regular*/
    var mailexp=/.[^@]*@[\w]*[\w\.-][\w]*\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]/;            

    Strmail=campo.value.toUpperCase();
    mailDepurado=Strmail.match(mailexp);
    if(mailDepurado!=Strmail){
                alert(mensaje);campo.focus();return false;
    }
    partesMail=Strmail.split('@');
    if(!analiza(partesMail[0],".,-,_,!,#,$,%,&,',+,/,=,?,`,{,},|,~",true)){
                alert(mensaje);campo.focus();return false;
    }
    segPartMail=partesMail[1].split('.');
    if(!longitudPartes(segPartMail)){
        alert(mensaje);campo.focus();return false;
    }
    if(!analiza("@"+segPartMail[0]+".","@,.,-",false)){
     	alert(mensaje);campo.focus();return false;
    }
    return true;
}

/*Devuelve false cuando el campo esta vacio*/
function verificatxt(campo,mensaje){
	if(!mensaje){
		alerta="El campo "+campo.name+" no puede estar vacio";
	}
	else{
		alerta=mensaje;
	}
	retorno=true;
	longitud=campo.value.length;
	if(longitud<1){alert(alerta);campo.focus();retorno=false;}
	return retorno;
}

/*Si en un formulario de checkboxs no hay ninguno marcado devuelve false*/

function verificacheck(formul){
	retorno=false;
	var i=0;
	for(i=0;i<formul.elements.length;i++){
		if(formul.elements[i].type=='checkbox'){
			if(formul.elements[i].checked==true){
				retorno=true;
			}
		}
	}
	if(retorno==false){alert("Debes marcar una casilla");}
	return retorno;
}

function verificatxtmm(campo,minimo,maximo,texto){
	retorno=true;
	longitud=campo.value.length;
	mensajemin="Debes escribir al menos "+minimo+" caracteres";
	if(texto){mensajemin=texto}
	if(longitud>maximo){alert("Has superado el maximo de caracteres permitido");campo.focus();retorno=false;}
	if(longitud<minimo){alert(mensajemin);campo.focus();retorno=false;}
	return retorno;
}

function verificaselect(campo,mensaje){
	retorno=true;
	resp="Debes seleccionar un valor del combo";
	if (mensaje){
		resp=mensaje;
	}
	if(campo.options.selectedIndex<1){		
		if(campo.value==-1 || campo.value==0 || campo.value==""){
			alert(resp);campo.focus();retorno=false;
		}
	}
	return retorno;
}

function verificatfno (pais, campo, mensaje) {
	
	retorno = true;
	
	var txt = replace (campo.value, ' ', '');
	var txt = replace (txt, '-', '');
	campo.value = txt;
	
	longitud = campo.value.length;
	
	resp = "Debes escribir un número de teléfono válido";
	if (mensaje) {
		resp = mensaje;
	}
	
	var regexp = /^(\+{0,1}[0-9]+)$/;
	
	if (regexp.test(campo.value) == false || longitud > 15) {
		alert (resp);
		campo.focus ();
		retorno = false;
	}
	
	return retorno;
}  

function verificatfno2(pais,campo,mensaje) {
	retorno=true;

	longitud=campo.value.length;

	resp="Debes escribir un número de teléfono válido";
	if(mensaje){resp=mensaje;}
	if(pais==6){
		if(longitud==9){
			if(parseInt(campo.value)!=campo.value){
				alert(resp);campo.focus();
				retorno=false;
			}
			else if(campo.value.substring(0,1)!='6' && campo.value.substring(0,1)!='9'){
				/* Comprobaremos que el numero de tfno empiece por 9 ó 6 */
				alert(resp);campo.focus();
				retorno=false;
			}
		}
		else{
			alert(resp);campo.focus();
			retorno=false;
		}
	}
	else if (pais == 71) {
	
		var regexp = /^[0-9]*$/;
		if (regexp.test(campo.value) == false){
			alert(resp);campo.focus();
			retorno=false;
		}

	}
	else if (pais == 109) {
		var regexp = /^[0-9]+$/;
		if (regexp.test(campo.value) == false){
			alert(resp);campo.focus();
			retorno=false;
		}

	}
	else{
		if(parseInt(campo.value)!=campo.value){
			alert(resp);campo.focus();
			retorno=false;
		}
	}
	return retorno;
}			