function creaAjax(){
  var objetoAjax=false;
  try {
   /*Para navegadores distintos a internet explorer*/
   objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
     /*Para explorer*/
     objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
     } 
     catch (E) {
     objetoAjax = false;
   }
  }

  if (!objetoAjax && typeof XMLHttpRequest!='undefined') {
   objetoAjax = new XMLHttpRequest();
  }
  return objetoAjax;
}

function PFAjax (url,capa,valores,metodo)
{
   var ajax=creaAjax();
   var capaContenedora = document.getElementById(capa);

/*Creamos y ejecutamos la instancia si el metodo elegido es POST*/
 if(metodo.toUpperCase()=='POST'){
    ajax.open ('POST', url, true);
    ajax.onreadystatechange = function() {
         if (ajax.readyState==1) {
                 capaContenedora.innerHTML="<img src='img/spinner-pq.gif' alt='Cargando' border='0' style='float:left; margin:11px 0 2px 4px;' />";
         }
         else if (ajax.readyState==4){
            if(ajax.status==200)
            {
                 document.getElementById(capa).innerHTML=ajax.responseText; 
            }
            else if(ajax.status==404)
                 {

                     capaContenedora.innerHTML = "La direccion existe";
                 }
             else
                 {
                     capaContenedora.innerHTML = "Error: ".ajax.status;
                 }
        }
    }
    ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    ajax.send(valores);
    return;
}
/*Creamos y ejecutamos la instancia si el metodo elegido es GET*/
if (metodo.toUpperCase()=='GET'){

    ajax.open ('GET', url, true);
    ajax.onreadystatechange = function() {
         if (ajax.readyState==1) {
                 capaContenedora.innerHTML="<span style='font-family:Tahoma; color:#8A8A8A; font-size:8pt;'>Procesando.......</span>";
         }
         else if (ajax.readyState==4){
            if(ajax.status==200){ 
                 document.getElementById(capa).innerHTML=ajax.responseText; 
            }
            else if(ajax.status==404)
                 {

                     capaContenedora.innerHTML = "La direccion existe";
                 }
                 else
                 {
                     capaContenedora.innerHTML = "Error: ".ajax.status;
                 }
        }
    }
    ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    ajax.send(null);
    return
}
}

function emailCheck (emailStr) {
/* Verificar si el email tiene el formato user@dominio. */
var emailPat=/^(.+)@(.+)$/; 

/* Verificar la existencia de caracteres. ( ) < > @ , ; : \ " . [ ] */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"; 

/* Verifica los caracteres que son válidos en una dirección de email */
var validChars="\[^\\s" + specialChars + "\]"; 

var quotedUser="(\"[^\"]*\")"; 

/* Verifica si la dirección de email está representada con una dirección IP Válida */ 


var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;


/* Verificar caracteres inválidos */ 

var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
/* domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");


var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
/*alert("Email address seems incorrect (check @ and .'s)");*/
return false
}
var user=matchArray[1]
var domain=matchArray[2]

// Si el user "user" es valido 
if (user.match(userPat)==null) {
// Si no
/*alert("El nombre de usuario no es válido.");*/
return false
}

/* Si la dirección IP es válida */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
/*alert("IP de destino inválida");*/
return false
}
}
return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
/*alert("El dominio parece no ser válido.");*/
return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) { 

/*alert("La dicrección debe tener 3 letras si es .'com' o 2 si en de algún pais.");*/
return false
}

if (len<2) {
var errStr="La dirección es erronea"
/*alert(errStr);*/
return false
}

// La dirección de email ingresada es Válida
return true;
}
// End -->

function Identificar(){
var mail=emailCheck(document.getElementById("email").value);
if (mail == false)
{ alert("Debe introducir un e-mail correcto"); document.getElementById("email").focus(); return false; }

if (document.getElementById("pass").value == "")
{ alert("Debe introducir su clave"); document.getElementById("pass").focus(); return false; }

document.getElementById("login").submit();
}

function Identificarb(){
var mail=emailCheck(document.getElementById("email").value);
if (mail == false)
{ alert("Debe introducir un e-mail correcto"); document.getElementById("email").focus(); return false; }

document.getElementById("login").submit();
}

function Registrar(){
var mail=emailCheck(document.getElementById("emailn").value);
if (mail == false)
{ alert("Debe introducir un e-mail correcto"); document.getElementById("emailn").focus(); return false; }

document.getElementById("formc").submit();
}

function Registrarok(){

if (document.getElementById("Nombre").value == "")
{ alert("Debe introducir su nombre"); document.getElementById("Nombre").focus(); return false; }

if (document.getElementById("apellidos").value == "")
{ alert("Debe introducir sus apellidos"); document.getElementById("apellidos").focus(); return false; }

if (document.getElementById("empresa").value == "")
{ alert("Debe introducir el nombre de la empresa"); document.getElementById("empresa").focus(); return false; }

if (document.getElementById("temp").value == "0")
{ alert("Debe seleccionar su tipo de fiscalidad aplicable"); document.getElementById("temp").focus(); return false; }

if (document.getElementById("cif").value == "")
{ alert("Debe introducir el CIF/NIF de la empresa"); document.getElementById("cif").focus(); return false; }

if (document.getElementById("direccion").value == "")
{ alert("Debe introducir su direccion"); document.getElementById("direccion").focus(); return false; }

if (document.getElementById("cp").value == "")
{ alert("Debe introducir su CP"); document.getElementById("cp").focus(); return false; }

if (document.getElementById("loc").value == "")
{ alert("Debe introducir su localidad"); document.getElementById("loc").focus(); return false; }

if (document.getElementById("prov").value == "0")
{ alert("Debe seleccionar su provincia"); document.getElementById("prov").focus(); return false; }

var mail=emailCheck(document.getElementById("emailb").value);
if (mail == false)
{ alert("Debe introducir un e-mail correcto"); document.getElementById("emailb").focus(); return false; }

if (document.getElementById("Telefono").value == "")
{ alert("Debe introducir su telefono"); document.getElementById("Telefono").focus(); return false; }

if (document.getElementById("passb").value == "")
{ alert("Debe introducir su clave"); document.getElementById("passb").focus(); return false; }

if (document.getElementById("Privacidad").checked == false)
{ alert("Debe aceptar la politica de privacidad"); document.getElementById("Privacidad").focus(); return false; }

document.getElementById("fregistro").submit();
}



