﻿var xmlHttpRegistro;// global instance of XMLHttpRequest
var valido;

function createXmlHttpRequestRegistro()
{
       if(window.ActiveXObject)
       {
        xmlHttpRegistro=new ActiveXObject("Microsoft.XMLHTTP");
      }

    else if(window.XMLHttpRequest)
    {
        xmlHttpRegistro=new XMLHttpRequest();
     }

}

function validaRegistro()
{
    var usuarioM = document.getElementById('usuarioM');
    var usuarioI = document.getElementById('usuarioI');
    var usuarioU = document.getElementById('usuarioU');
	var passwordM = document.getElementById('passwordM');
	var nombreM = document.getElementById('nombreM');
	var apellidoPM = document.getElementById('apellidoPM');
	var apellidoMM = document.getElementById('apellidoMM');
	var emailM = document.getElementById('emailM');
	var emailI = document.getElementById('emailI');
    var emailU = document.getElementById('emailU');
//	var cpM = document.getElementById('cpM');
	var cpI = document.getElementById('cpI');
	var fechaNacI = document.getElementById('fechaNacI');
//	var delegacionM = document.getElementById('delegacionM');
//	var telCasaM = document.getElementById('telCasaM');
//	var telCasaI = document.getElementById('telCasaI');	
//	var ciudadM = document.getElementById('ciudadM');
//	var estadoM = document.getElementById('estadoM');
//	var terminosM = document.getElementById('terminosM');
//	var ocupacionM =  document.getElementById('ocupacionM');
//	var lugarTrabajoM =  document.getElementById('lugarTrabajoM');
//	var establecimientoM  = document.getElementById('establecimientoM');

	usuarioM.style.display = 'none';
	usuarioI.style.display = 'none';
    usuarioU.style.display = 'none';
	passwordM.style.display = 'none';
	nombreM.style.display = 'none';
    apellidoPM.style.display = 'none';
    apellidoMM.style.display = 'none';
	emailM.style.display = 'none';
	emailI.style.display = 'none';
    emailU.style.display = 'none';
//	telCasaM.style.display = 'none';
//	telCasaI.style.display = 'none';
	fechaNacI.style.display = 'none';
	cpI.style.display = 'none';
/*	delegacionM.style.display = 'none';
	ciudadM.style.display = 'none';
	estadoM.style.display = 'none';
	terminosM.style.display = 'none';		
	lugarTrabajoM.style.display = 'none';
	establecimientoM.style.display = 'none';
	ocupacionM.style.display = 'none';
	
*/

  var usuario=document.forma.usuarioR.value;
  var password=document.forma.passwordR.value;
  var nombre=document.forma.nombre.value;
  var apellidoP=document.forma.apellidoP.value;
  var apellidoM=document.forma.apellidoM.value;
  var email=document.forma.email.value;
  
  var telefonoCasa=document.forma.telefonoCasa.value;
  var telefonoOficina=document.forma.telefonoOficina.value;
  var telefonoMovil=document.forma.telefonoMovil.value;
  var telefonoFax=document.forma.telefonoFax.value;
  
  var cp=document.forma.cp.value;
  
  var calle = document.forma.calle.value;
  var colonia = document.forma.colonia.value;
  var numInt = document.forma.numInt.value;
  var numExt = document.forma.numExt.value;
  var delegacion= document.forma.delegacion.value;
  var ciudad= document.forma.ciudad.value;
  var estado= document.forma.estado.value;
  var pais= document.forma.pais.value;

  var dia= document.forma.fechaDia.value;
  var mes= document.forma.fechaMes.value;
  var anio= document.forma.fechaAnio.value;
  var fechaNac = anio + mes + dia;

  var sexo = document.forma.sexo.value;
  var escuela = document.forma.escuela.value;
  var gradoEscolar =document.forma.gradoEscolar.value;
  var favorito = document.forma.favorito.value;
  var empresa = document.forma.empresa.value;
  var giroEmpresa = document.forma.giroEmpresa.value;
  var tamanoEmpresa = document.forma.tamanoEmpresa.value;
  var departamento = document.forma.departamento.value;
  var puestoFuncion = document.forma.puestoFuncion.value;
  var segmento = document.forma.segmento.value;
  
  //var	terminos=document.forma.terminos;
  //var establecimiento = document.forma.establecimiento.value;
  //var ocupacion = 	document.forma.ocupacion.value;
  					
				
//	var nombreF = document.forma.nombre;
//	nombreF.focus();

  //alert(terminos);
  var valido = true;
	if (usuario==""){
	    valido = false;
   	    usuarioM.style.display = 'block';
	}
	else 
	{
	  if (!esUsuario(usuario)){
		    valido = false;
		    usuarioI.style.display = 'block';
	  } 
	}		

    //alert("ya paso - " + valido);
		
	if (password==""){
	   valido = false;
   	   passwordM.style.display = 'block';
	}
	else 
	{
	  if (!esUsuario(password))
	  {
		    valido = false;
			passwordM.style.display = 'block';
	  }
	}		
			
	if (nombre==""){
	   valido = false;
   	   nombreM.style.display = 'block';
	}
	
	if (apellidoP==""){
	   valido = false;
   	 apellidoPM.style.display = 'block';
	}	

	if (apellidoM==""){
	   valido = false;
   	 apellidoMM.style.display = 'block';
	}	
		
	if ((dia=="--")||(mes=="--")||(anio=="--")){
	   valido = false;
	   fechaNacI.style.display = 'block';
	}
	
	if (email==""){
	   valido = false;
   	   emailM.style.display = 'block';
	}		
    else
    {
  		var filter=/^[A-Za-z][A-Za-z0-9_.-]*@[A-Za-z0-9_.-]+\.[A-Za-z0-9_.]+[A-za-z]$/;	
		if (!filter.test(email))
        {
	   		 valido = false;
   	 		 emailI.style.display = 'block';
		} 
	}

	
	if (cp!=""){
	  if (!esNumero(cp)){
		    valido = false;
				cpI.style.display = 'block';
		}
	}	

/*	if (ciudad==""){
	   valido = false;
   	 ciudadM.style.display = 'block';
	}				
	
	if (estado==""){
	   valido = false;
   	 estadoM.style.display = 'block';
	}			
//	 alert(tipoUsuario);
	if (tipoUsuario == "1")
	{
  	if (lugarTrabajo==""){
  	   valido = false;
     	 lugarTrabajoM.style.display = 'block';
  	}				   
		
  	if (establecimiento==""){
  	   valido = false;
     	 establecimientoM.style.display = 'block';
  	}						
		 
	} else {
  	if (ocupacion=="")
		{
  	   valido = false;
     	 ocupacionM.style.display = 'block';
  	}				
	}
	
	if (!terminos.checked){
	   valido = false;
   	 terminosM.style.display = 'block';
	}	
	*/
	//alert(valido);

/*	var usrNegocio = "";
	var regPapeleria = document.getElementById('chkPapeleria');
	if (regPapeleria.checked) {
	    usrNegocio = "papeleria";
	}

	var regDistribuidor = document.getElementById('chkDistribuidor');
	if (regDistribuidor.checked) {
	    usrNegocio = "distribuidor";
	}
*/

	//alert(segmento);
	if (valido){
	    document.getElementById("mensajeRegistro").innerHTML = 'Enviando datos...';
	    Scribe.services.Registro.registra(segmento, usuario, password, nombre, apellidoP, apellidoM, sexo, fechaNac, email, escuela, gradoEscolar, calle, numExt, numInt, colonia, delegacion, ciudad, estado, pais, cp, favorito, empresa, giroEmpresa, tamanoEmpresa, departamento, puestoFuncion, telefonoOficina, telefonoCasa, telefonoFax, telefonoMovil, segmento, OnCompleteReg, OnTimeOutReg, OnErrorReg);
        //Scribe.services.Registro.registra("", usuario, password, nombre, apellidoP, apellidoM, sexo, fechaNac, email, escuela, gradoEscolar, calle, numExt, numInt, colonia, delegacion, ciudad, estado, pais, cp, favorito, empresa, giroEmpresa, tamanoEmpresa, departamento, puestoFuncion, telefonoOficina, telefonoCasa, telefonoFax, telefonoMovil, segmento, OnCompleteReg, OnTimeOutReg, OnErrorReg);
    }

}

function OnCompleteReg(args) 
{
  document.getElementById("mensajeRegistro").innerHTML = '';
  if (args=="Error1"){
      var usuarioU = document.getElementById('usuarioU');
      usuarioU.style.display = 'block';
      
  } else if (args=="Error2"){
  var emailU = document.getElementById('emailU');
      emailU.style.display = 'block';
  } else {
          var escuela = document.getElementById("escuela");
          var favorito = document.getElementById("favorito");
          var empresa = document.getElementById("empresa");
          var direccion = document.getElementById("direccion");
          var telefonos = document.getElementById("telefonos");
          var requeridos = document.getElementById("requeridos");
          var registro = document.getElementById("registro");
          var reg = document.getElementById("reg");
          var exitoso = document.getElementById("exitoso");



          var usrNegocio = "";
          var segmento = document.getElementById("segmento");

        
          if (segmento.value == "R") {
              usrNegocio = "papeleria";
          }else if (segmento.value == "D") {
              usrNegocio = "distribuidor";
          }else if (segmento.value == "E") {
              usrNegocio = "escuela";
          }else {
              usrNegocio = "otro";
          }

          if (usrNegocio == "papeleria") {
              location.href = "RegistroPapelerias.aspx";
          } else if (usrNegocio == "distribuidor") {
              location.href = "RegistroDistribuidores.aspx";
          } else if (usrNegocio == "escuela") {
              location.href = "RegistroEscuelas.aspx";
          } else {
              escuela.style.display = "none";
              favorito.style.display = "none";
              empresa.style.display = "none";
              direccion.style.display = "none";
              telefonos.style.display = "none";
              requeridos.style.display = "none";
              registro.style.display = "none";
              reg.style.display = "none";
              exitoso.style.display = "block";

              //document.getElementById("mensajeRegistro").innerHTML = 'Gracias por tu registro, Es necesario activar tu cuenta, pronto recibirás un Correo para activar tu cuenta';
          }
        
      
      //location.href="Default.aspx?segmento=" + args;  
  }
}

function OnTimeOutReg(args) {
  document.getElementById("mensajeRegistro").innerHTML="Se ha agotado el tiempo, por favor inténtalo de nuevo más tarde.";  
}

function OnErrorReg(args) {
  document.getElementById("mensajeRegistro").innerHTML="Ha ocurrido un error.";  
} 

function esNumero(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
	 

function esUsuario(sText)

{
   var ValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function validaFecha() 
{
    var anio = document.forma.fechaAnio.value;
    var hoy = new Date().getFullYear();
    if (anio != "--") {
        
        var dif = ((new Date().getFullYear()) - document.forma.fechaAnio.value)
        var escuela = document.getElementById("escuela");
        var favorito = document.getElementById("favorito");
        var empresa = document.getElementById("empresa");
        var direccion = document.getElementById("direccion");
        var telefonos = document.getElementById("telefonos");

        escuela.style.display = "none";
        favorito.style.display = "none";
        empresa.style.display = "none";
        direccion.style.display = "none";
        telefonos.style.display = "none";
        
        if (dif <= 12) {
            escuela.style.display = "block";
            document.getElementById("nombreEscuela").innerHTML = 'Nombre de tu escuela:';
            document.getElementById("grado").innerHTML = 'Año en el que estás:';

            favorito.style.display = "block";
            document.getElementById("nombreFavorito").innerHTML = 'Personaje Favorito';
            
        } else if (dif <= 15) {
            document.getElementById("nombreEscuela").innerHTML = 'Nombre de tu colegio:';
            document.getElementById("grado").innerHTML = 'Grado escolar:';
            escuela.style.display = "block";

            favorito.style.display = "block";
            document.getElementById("nombreFavorito").innerHTML = 'Personaje Favorito';
            
        } else if (dif <= 23) {
            document.getElementById("nombreEscuela").innerHTML = 'Nombre de tu Prepa o universidad:';
            document.getElementById("grado").innerHTML = 'Grado o semestre:';
            escuela.style.display = "block";

            favorito.style.display = "block";
            document.getElementById("nombreFavorito").innerHTML = 'Artista Favorito';

            direccion.style.display = "block";
            telefonos.style.display = "block";
        } else {
            favorito.style.display = "block";
            document.getElementById("nombreFavorito").innerHTML = 'Personaje a quien admiras';
            direccion.style.display = "block";
            empresa.style.display = "block";
            telefonos.style.display = "block";
        }
    }
}

function campos_segmento(mycombo) {

    var puesto = document.getElementById("puesto");
    var empresa = document.getElementById("empresa2");
    var departamento = document.getElementById("departamento");
    var giroEmpresa = document.getElementById("giroEmpresa");
    var telefonoOficina = document.getElementById("telefonoOficina");
    var tamanoEmpresa = document.getElementById("tamanoEmpresa");
    var telefonoFax = document.getElementById("telefonoFax");

    var lab_puesto = document.getElementById("lab_puesto");
    var lab_empresa = document.getElementById("lab_empresa");
    var lab_departamento = document.getElementById("lab_departamento");
    var lab_giroEmpresa = document.getElementById("lab_giroEmpresa");
    var lab_telefonoOficina = document.getElementById("lab_telefonoOficina");
    var lab_tamanoEmpresa = document.getElementById("lab_tamanoEmpresa");
    var lab_telefonoFax = document.getElementById("lab_telefonoFax");

    if (mycombo.value == "O") {
        puesto.style.display = "block";
        empresa.style.display = "block";
        departamento.style.display = "block";
        giroEmpresa.style.display = "block";
        telefonoOficina.style.display = "block";
        tamanoEmpresa.style.display = "block";
        telefonoFax.style.display = "block";

        lab_puesto.style.display = "block";
        lab_empresa.style.display = "block";
        lab_departamento.style.display = "block";
        lab_giroEmpresa.style.display = "block";
        lab_telefonoOficina.style.display = "block";
        lab_tamanoEmpresa.style.display = "block";
        lab_telefonoFax.style.display = "block";

    } else {

        puesto.style.display = "none";
        empresa.style.display = "none";
        departamento.style.display = "none";
        giroEmpresa.style.display = "none";
        telefonoOficina.style.display = "none";
        tamanoEmpresa.style.display = "none";
        telefonoFax.style.display = "none";

        lab_puesto.style.display = "none";
        lab_empresa.style.display = "none";
        lab_departamento.style.display = "none";
        lab_giroEmpresa.style.display = "none";
        lab_telefonoOficina.style.display = "none";
        lab_tamanoEmpresa.style.display = "none";
        lab_telefonoFax.style.display = "none";
    
        
    }
}

