var whitespace = " \t\n\r";
var defaultEmptyOK = false
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyzáéíóúñü"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚÑ"


function modificar(destino) {
var ventana, pagina
pagina = "bajaclientes.asp?logon=modificar&registro="+destino
ventana = window.open(pagina, "_self") 
}

function baja(destino) {
var ventana, pagina
pagina = "bajaclientes.asp?logon=baja&registro="+destino
ventana = window.open(pagina, "_self") 
}

function registrar(form) {
var envi = 1
if(form.entidad.value=='') {alert("Debe rellenar el campo NOMBRE DEL ORGANISMO O ENTIDAD");form.entidad.focus();envi = 0;}
if (envi > 0) {if(form.direccion.value=='') {alert("Debe rellenar el campo DIRECCION");form.direccion.focus();envi = 0;}}
if (envi > 0) {if(form.cp.value=='') {alert("Debe rellenar el campo CODIGO POSTAL");form.cp.focus();envi = 0;}}
if (envi > 0) {if(!(esNumero(form.cp.value))) {alert("Ingrese sólo números en el CODIGO POSTAL");form.cp.value='';form.cp.focus();envi = 0;}}
if (envi > 0) {if(form.poblacion.value=='') {alert("Debe rellenar el campo POBLACION");form.poblacion.focus();envi = 0;}}
if (envi > 0) {if(form.provincia.value<1&&form.pais.value==67) {alert("Debe indicar una PROVINCIA");form.provincia.focus();envi = 0;}}
if (envi > 0) {if(form.direccion.value=='') {alert("Debe rellenar la dirección");form.direccion.focus();envi = 0;}}
if (envi > 0) {if(form.cif.value==''&&form.pais.value==67) {alert("Debe rellenar el campo CIF");form.cif.focus();envi = 0;}}
if (envi > 0) {if(form.telefono.value=='') {alert("Debe rellenar el campo TELEFONO");form.telefono.focus();envi = 0;}}
if (envi > 0) {if(form.email.value=='') {alert("Debe rellenar el campo E-MAIL");form.email.focus();envi = 0;}}
if (envi > 0) {if(!(isEmail(form.email.value))) { alert("Ingrese una dirección de CORREO ELECTRÓNICO válida");form.email.value='';form.email.focus();envi = 0;}}
if (envi > 0) {if(form.contacto.value=='') {alert("Debe rellenar el campo PERSONA DE CONTACTO");form.contacto.focus();envi = 0;}}
if (envi > 0) {if(form.usuario.value=='') {alert("Debe rellenar el campo USUARIO");form.usuario.focus();envi = 0;}}
if (envi > 0) {if(isWhitespace(form.usuario.value)) { alert("No deje espacios en el campo USUARIO");form.usuario.value='';form.usuario.focus();envi = 0;}}
if (envi > 0) {if(!(isAlphanumeric(form.usuario.value))) { alert("Ingrese un texto que contenga solo letras y/o numeros en USUARIO");form.usuario.focus();envi = 0;}}
if (envi > 0) {if(form.contrasena.value=='') {alert("Debe rellenar el campo CONTRASEÑA");form.contrasena.focus();envi = 0;}}
if (envi > 0) {if(form.contrasena.value.length < 4) {alert("La CONTRASEÑA debe ser al menos de cuatro caracteres");form.contrasena.value='';form.contrasena2.value='';form.contrasena.focus();envi = 0;}}
if (envi > 0) {if(form.contrasena2.value=='') {alert("Debe rellenar el segundo campo CONTRASEÑA repita la misma que a puesto en el campo de arriba");form.contrasena2.focus();envi = 0;}}
if (envi > 0) {if(form.contrasena.value != form.contrasena2.value) {alert("Las CONTRASEÑAS no son iguales, por favor intentelo de nuevo");form.contrasena.value='';form.contrasena2.value='';form.contrasena.focus();envi = 0;}}
if (envi > 0) {document.alta.submit();}
}


// COMPROBAR SI ES UN CORREO ELECTRONICO

function isEmail (s)
{
    if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function isWhitespace (s)
{   var i;
	var compro = false
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        // si el caracter en que estoy no aparece en whitespace,
        // entonces retornar falso
        if (whitespace.indexOf(c) != -1) return true;
    }
    return false;
}

function isAlphanumeric (s)
{   var i;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!(isLetter(c) || isDigit(c) ) ) return false;
    }

    return true;
}

function esNumero (s)
{   var i;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!(isDigit(c))) return false;
    }

    return true;
}



function isLetter (c)
{
    return( ( uppercaseLetters.indexOf( c ) != -1 ) ||
            ( lowercaseLetters.indexOf( c ) != -1 ) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function verono() {
	if(document.getElementById('pais').value==67) {document.getElementById('provincia2').className='visible'}
	else {document.getElementById('provincia').value=0;document.getElementById('provincia2').className='oculto'}
}

function modificar(form) {
var envi = 1
if(form.entidad.value=='') {alert("Debe rellenar el campo NOMBRE DEL ORGANISMO O ENTIDAD");form.entidad.focus();envi = 0;}
if (envi > 0) {if(form.direccion.value=='') {alert("Debe rellenar el campo DIRECCION");form.direccion.focus();envi = 0;}}
if (envi > 0) {if(form.cp.value=='') {alert("Debe rellenar el campo CODIGO POSTAL");form.cp.focus();envi = 0;}}
if (envi > 0) {if(!(esNumero(form.cp.value))) {alert("Ingrese sólo números en el CODIGO POSTAL");form.cp.value='';form.cp.focus();envi = 0;}}
if (envi > 0) {if(form.poblacion.value=='') {alert("Debe rellenar el campo POBLACION");form.poblacion.focus();envi = 0;}}
if (envi > 0) {if(form.provincia.value<1&&form.pais.value==67) {alert("Debe indicar una PROVINCIA");form.provincia.focus();envi = 0;}}
if (envi > 0) {if(form.direccion.value=='') {alert("Debe rellenar la dirección");form.direccion.focus();envi = 0;}}
if (envi > 0) {if(form.cif.value==''&&form.pais.value==67) {alert("Debe rellenar el campo CIF");form.cif.focus();envi = 0;}}
if (envi > 0) {if(form.telefono.value=='') {alert("Debe rellenar el campo TELEFONO");form.telefono.focus();envi = 0;}}
if (envi > 0) {if(form.email.value=='') {alert("Debe rellenar el campo E-MAIL");form.email.focus();envi = 0;}}
if (envi > 0) {if(!(isEmail(form.email.value))) { alert("Ingrese una dirección de CORREO ELECTRÓNICO válida");form.email.value='';form.email.focus();envi = 0;}}
if (envi > 0) {if(form.contacto.value=='') {alert("Debe rellenar el campo PERSONA DE CONTACTO");form.contacto.focus();envi = 0;}}
if (envi > 0) {if(form.contrasena.value.length < 4 && form.contrasena.value!='') {alert("La CONTRASEÑA debe ser al menos de cuatro caracteres");form.contrasena.value='';form.contrasena2.value='';form.contrasena.focus();envi = 0;}}
if (envi > 0) {if(form.contrasena.value != form.contrasena2.value) {alert("Las CONTRASEÑAS no son iguales, por favor intentelo de nuevo");form.contrasena.value='';form.contrasena2.value='';form.contrasena.focus();envi = 0;}}
if (envi > 0) {document.modifica.submit();}
}
