﻿
// -------------------------------------------------------------------------
// Función que cargar el formulario para permitir realizar el registro de
// un nuevo afiliado
// -------------------------------------------------------------------------
function RegistroAfiliado()
{
   
    //Ponemos en Opcion Generia 1 el Valor de Registro de Afiliados
    CargarOpcionGenericaRegAfi();
  
    //Cargamos el Contenedor de la Izquierda(Información Registro Afiliados)
    cargarInfoRegAfiliado();
    
    //Cargamos el Formulario que nos permitirá el registro de afiliados
    CargarFormRegAfi('1');
}


// ---------------------------------------------------------------------------------------
// Función que carga el Formulario para el Registro de Afiliados
// ---------------------------------------------------------------------------------------
function CargarFormRegAfi(pTipoAfiliado)
{
  //Enviamos los datos del formulario
  index.CargarFormRegAfi(pTipoAfiliado,CargarFormRegAfi_CallBack);  
}

// ----------------------------------------------------------------------------------
// Función que se encarga de introducir la información del formulario donde se 
// va a realizar el registro de afiliados.
// ----------------------------------------------------------------------------------
function CargarFormRegAfi_CallBack(response)
{
    if(response.value != "NOSESS")
    {
        var resultado=response.value;    

        //Comprobamos si se ha cambiado la Opción de Menú
        if (resultado !='')
        {                         
            GeneraCatcha();  
            //Mostramos las tablas con las Apuestas
            var etiqueta="dvPrincipal";	    
	        document.getElementById(etiqueta).innerHTML = resultado;	    	                
        }
    }        
}


// ---------------------------------------------------------------------------------------
// Función que se encarga de poner en Opcion Generica 1 Registro Afiliados
// ---------------------------------------------------------------------------------------
function CargarOpcionGenericaRegAfi()
{
  //Enviamos los datos del formulario
  index.CargarOpcionGenericaRegAfi(CargarOpcionGenericaRegAfi_CallBack);  
}


// ----------------------------------------------------------------------------------
// Función que se encarga de introducir la información en la Opcion Generica 1 de
// Registro de Afiliados
// ----------------------------------------------------------------------------------
function CargarOpcionGenericaRegAfi_CallBack(response)
{
    if(response.value != "NOSESS")
    {
        var resultado=response.value;    

        //Comprobamos si se ha cambiado la Opción de Menú
        if (resultado !='')
        {                     
            //Mostramos las tablas con las Apuestas
            var etiqueta="dvcontTipoApuesta";	    
	        document.getElementById(etiqueta).innerHTML = resultado;
	        
	        var etiqueta2="dvcontTipoApuestaBombo";	    
	        document.getElementById(etiqueta2).innerHTML = resultado;	
	        	    	                
        }
    }        
}

//Variables de Usuario
var vCodUsr= "";
var vPassUsr= "";
// ----------------------------------------------------------------------------------
// Función que se encarga de realizar el registro de Afiliado
// ----------------------------------------------------------------------------------
function RegistroAfi()
{
    //Variable para guardar las etiquetas
    var etiqueta;
    
    //Variables de Usuario
    var vNomUsr= "";
    var vApeUsr1= "";    
    var vApeUsr2= "";    
    var vDiaNac= "";
    var vMesNac= "";
    var vAnoNac= "";   
    var vDirecc= "";
    var vCodPos= "";
    var vMunic= "";
    var vProv= "";
    var vCodPai= "";
    var vEmail= "";
    var vTlfno= "";
    var vTlfnoMov= "";
    var vPregunta1= "";
    var vPregunta2= "";
    var vRespuesta1= "";
    var vRespuesta2= "";
    var vTipoAfi = "";
    var vTipoAfiliacion ="";
    var vNombreEmpresa = "";
    var vUrlAfiliado = ""; 
    var vDocuAfi = "";
    var  vTipoDocuAfi = "";
    var vTipoConocer =""; 
    var catcha="";
       
    //Código de Usuario    
    etiqueta = "textoCatcha";
    catcha = document.getElementById(etiqueta).value;
          
    //Tipo de Afiliado (Empresa/Particular)
    etiqueta = "tbTipoAfiliado";
    var vTipoAfi = document.getElementById(etiqueta).value;
    
    //Tipo de Afiliación Ambos:(Click / Registro)
    etiqueta = "tbTipoAfiliacion";
    var vTipoAfiliacion = "3"; //Tipo de Afiliacion Ambos (Click/Registro)
             
    
    //Código de Usuario    
    etiqueta = "tbCodUsr";
    vCodUsr = document.getElementById(etiqueta).value;
   
    //Password de Usuario
    etiqueta = "tbPassUsr";
    vPassUsr = document.getElementById(etiqueta).value;
    
    //Nombre de Usuarios
    etiqueta = "tbNomUsr";
    vNomUsr = document.getElementById(etiqueta).value;
   
    //Apellido 1 de Usuario
    etiqueta = "tbApe1Usr";
    vApeUsr1 = document.getElementById(etiqueta).value;
    
    //Apellido 2 de Usuario
    etiqueta = "tbApe2Usr";
    vApeUsr2 = document.getElementById(etiqueta).value;
    
    if(vTipoAfi == "1")
    {
        //Dia de Nacimiento
        etiqueta = "tbDiaNacUsr";
        vDiaNac = document.getElementById(etiqueta).value;
    
        //Mes de Nacimiento
        etiqueta = "tbMesNacUsr";
        vMesNac = document.getElementById(etiqueta).value;
 
        //Año de Nacimiento
        etiqueta = "tbAnoNacUsr";
        vAnoNac = document.getElementById(etiqueta).value;
    }

    //Dirección
    etiqueta = "tbDirecUsr";
    vDirecc = document.getElementById(etiqueta).value;
    
    //Código Postal
    etiqueta = "tbCodPosUsr";
    vCodPos = document.getElementById(etiqueta).value;
    
    //Municipio
    etiqueta = "tbMunicUsr";
    vMunic = document.getElementById(etiqueta).value;

    //Provincia
    etiqueta = "tbProvUsr";
    vProv = document.getElementById(etiqueta).value;

    //Pais
    etiqueta = "tbPaisUsr";
    vCodPai = document.getElementById(etiqueta).value;

    //Email
    etiqueta = "tbEmail";
    vEmail = document.getElementById(etiqueta).value;

    //Teléfono 
    etiqueta = "tbTlfnoFijo";
    vTlfno = document.getElementById(etiqueta).value;

    //Móvil 
    etiqueta = "tbTlfnoMov";
    vTlfnoMov = document.getElementById(etiqueta).value;
    
    //Preugunta 1 
    etiqueta = "tbPregunta1";
    vPregunta1 = document.getElementById(etiqueta).value;
    
    //Preugunta 2
    etiqueta = "tbPregunta2";
    vPregunta2 = document.getElementById(etiqueta).value;
     
    //Respuesta 1
    etiqueta = "tbRespuesta1";
    vRespuesta1 = document.getElementById(etiqueta).value;

    //Respuesta 2
    etiqueta = "tbRespuesta2";
    vRespuesta2 = document.getElementById(etiqueta).value;               
    
    //Url del afiliado
    etiqueta = "tbUrlAfiliado";
    vUrlAfiliado = document.getElementById(etiqueta).value;               
    
    if(vTipoAfi == "2")
    {
        //Nombre Empresa
        etiqueta = "tbNomEmp";
        var vNombreEmpresa = document.getElementById(etiqueta).value;
    }
   
	// Numero y Tipo de documento, modo por el que nos conocio
	vDocuAfi = document.getElementById("tbDocuAfi").value;
	vTipoDocuAfi =  document.getElementById("tbTipoDocuAfi").value;
	vTipoConocer = document.getElementById("tbTipoConocer").value;
        
    // ---------------------------------------------------------
    // Comprobamos si el usuario a aceptado las condiciones
    // ---------------------------------------------------------
    etiqueta = "chkCertifico";       
    if(document.getElementById(etiqueta).checked==true)
    {
         //Enviamos los datos del formulario
        index.RegistroAfi(vCodUsr,vPassUsr,vNomUsr,vApeUsr1,vApeUsr2,vDiaNac,vMesNac,vAnoNac,vDirecc,vCodPos,vMunic,vProv,vCodPai,vEmail,vTlfno,vTlfnoMov,vPregunta1,vRespuesta1,vPregunta2,vRespuesta2,vTipoAfi,vTipoAfiliacion,vNombreEmpresa,vUrlAfiliado, vDocuAfi, vTipoDocuAfi, vTipoConocer,catcha, RegistroAfi_CallBack);      
    }
    else
    {
      
       //Ponemos el focus en el Check de certificaciones
       SetFocusSelect('chkCertifico');
      
       //Mostramos el error correspondiente
       MuestraMessage(115);
    }
   
}


// ----------------------------------------------------------------------------------
// Función que obtiene la respuesta del registro de afiliados
// ----------------------------------------------------------------------------------
function RegistroAfi_CallBack(response)
{
    if(response.value != "NOSESS")
    {
        var resultado=response.value;    
              
        // ------------------------------------------
        // TODO OK!!!
        // ------------------------------------------
        if (resultado == '0')
        {
            //location.href="index.aspx?regafi=S";                    
                       
            document.getElementById("enhorabuena").style.display = "inline";
            document.getElementById("dvRegUsr").style.display = "none";
           	    	
            CargarAfiliadoRegistrado();
            
                                                
        }
        else
        {

            //Ponemos el focus en el Código de Afiliado
            //SetFocusSelect('tbCodUsr');
        
            if(resultado.length <= 4)
            {
                //Mostramos el error correspondiente
                MuestraMessage(resultado);
            }
            else           
            {
                alert(resultado);
            }
            
        }
    }        
}

//---------------------------------------------------
// Función encargada de cargar la información del
// registro de afiliado para mostrar panel de 
// bienvenida.
// --------------------------------------------------
function CargarAfiliadoRegistrado()
{
    
    //Cargamos la información del registro de usuario.
    index.CargarAfiliadoRegistrado(vCodUsr,vPassUsr,CargarAfiliadoRegistrado_CallBack); 
    
    //Obtenemos el código de Afiliature
    index.GetCodigoAfiliature(vCodUsr,GetCodigoAfiliature_CallBack);
    
   
    //Obtenemos el código de Atevenio        
    index.GetCodigoAntenenio(vCodUsr,GetCodigoAntenenio_CallBack);  
    
}

function CargarAfiliadoRegistrado_CallBack(response)
{
    var resultado = response.value;                
    if(resultado != "")
    {
        var etiquetaRegistro = "dvDatosRegistro";
        document.getElementById(etiquetaRegistro).innerHTML = resultado;
        
    }

}

//---------------------------------------------------
// Aplicamos promoción de Antevenio
//---------------------------------------------------
function GetCodigoAntenenio_CallBack(response)
{
    var resultado = response.value;
    var etiquetaAntenio = "dvantevenio";
    var cadenaAntevenio = "<IFRAME src='http://www.netfilia.com/ventas/confirmacion_preventa.html?idventa="+resultado+"&cantidad=1&codigo=26874&programa=2162&checksum=790e8f020fd237f1e6943e20bc3f5c55' frameborder='no' width='0' height='0'></IFRAME>";
    var adjal= "<IMG SRC=\"http://www.adjal.com/i_track_sale/3488/1/"+resultado+"/AltaUsuario\">"+
               "<script src=\"http://www.adjal.com/i_sale_third/3488/1/"+resultado+"/AltaUsuario\"></script>"+
               "<noscript><IMG SRC=\"http://www.adjal.com/i_track_sale/3488/1/"+resultado+"/AltaUsuario\"></noscript>";
    
    if(document.getElementById(etiquetaAntenio).innerHTML != null)
    {
        document.getElementById(etiquetaAntenio).innerHTML = cadenaAntevenio+adjal;
    }  
}

//---------------------------------------------------
// Aplicamos promoción de Antevenio
//---------------------------------------------------
//function GetCodigoAntenenio_CallBack(response)
//{
//    var resultado = response.value;
//    var etiquetaAntenio = "dvantevenio";
//    var cadenaAntevenio = "<IFRAME src='http://www.netfilia.com/ventas/confirmacion_preventa.html?idventa="+resultado+"&cantidad=1&codigo=26874&programa=2162&checksum=790e8f020fd237f1e6943e20bc3f5c55' frameborder='no' width='0' height='0'></IFRAME>";
//    if(document.getElementById(etiquetaAntenio) != null)
//    {
//        document.getElementById(etiquetaAntenio).innerHTML = cadenaAntevenio;
//    }  
//}


function AFProcessLeadV2(merchantID, payoutcodes, offlinecode, ref)
{
var secure;
secure = (document.location.toString().indexOf("https") == 0)
var RN = new String (Math.random());
var RNS = RN.substring (2, 11);
var etiquetaAfiliature = "dvafiliature";
if ( secure == true )
	{
	document.getElementById(etiquetaAfiliature).innerHTML ="<iframe frameborder = '0' width='1' height='1' src='https://scripts.affiliatefuture.com/AFLeadV2.asp?merchant=" + merchantID + "&ref=" + ref + "&payoutcodes=" + payoutcodes + "&offlinecode=" + offlinecode + "&r="+RNS+"'></IFRAME>";
	//document.write("<script language='javascript' src='https://scripts.affiliatefuture.com/AFLeadV2.asp?merchant=" + merchantID + "&ref=" + ref + "&payoutcodes=" + payoutcodes + "&offlinecode=" + offlinecode + "&r="+RNS+"'/></SCRIPT>");
	}
else
	{
	document.getElementById(etiquetaAfiliature).innerHTML ="<iframe frameborder = '0' width='1' height='1' src='http://scripts.affiliatefuture.com/AFLeadV2.asp?merchant=" + merchantID + "&ref=" + ref + "&payoutcodes=" + payoutcodes + "&offlinecode=" + offlinecode + "&r="+RNS+"'></IFRAME>";
	//document.write("<script language='javascript' src='http://scripts.affiliatefuture.com/AFLeadV2.asp?merchant=" + merchantID + "&ref=" + ref + "&payoutcodes=" + payoutcodes + "&offlinecode=" + offlinecode + "&r="+RNS+"'/></SCRIPT>");
	}
}




//---------------------------------------------------
// Aplicamos promoción de Afiliature
//---------------------------------------------------
function GetCodigoAfiliature_CallBack(response)
{
    var resultado = response.value;    
    /*var cadenaAfiliature = "<script language=\"javascript\" src=\"http://scripts.affiliatefuture.com/AFFunctions.js\"></script> <script language=\"javascript\">  var merchantID = 4718; var orderRef= "+resultado+"; var payoutstrCodes = ''; var offlinestrCode = ''; AFProcessLeadV2(merchantID, payoutstrCodes, offlinestrCode, ref); </script>";*/    
    /*var cadenaAfiliature = "<script type=\"text/javascript\">  var merchantID = 4718; var orderRef= "+resultado+"; var payoutstrCodes = ''; var offlinestrCode = ''; AFProcessLeadV2(merchantID, payoutstrCodes, offlinestrCode, orderRef); </script>";*/
    var merchantID = 4718; 
    var orderRef=resultado; 
    var payoutstrCodes = ''; 
    var offlinestrCode = ''; 
    AFProcessLeadV2(merchantID, payoutstrCodes, offlinestrCode, orderRef);      
    /*
    if(document.getElementById(etiquetaAfiliature) != null)
    {
        document.getElementById(etiquetaAfiliature).innerHTML = cadenaAfiliature;
    } //else index.GetCodigoAfiliature(vCodUsr,GetCodigoAfiliature_CallBack);        
    */
}


//---------------------------------------------------
// Función que se encarga de limpiar los campos
// de registro de afiliados
// --------------------------------------------------
function LimpiaCamposFormRegistroAfi() 
{
  
    //Variable etiquetas
    var  etiqueta
    
    //Tipo de Afiliado (Empresa/Particular)
    etiqueta = "tbTipoAfiliado";
    var vTipoAfi = document.getElementById(etiqueta).value;
    
    //Código de Usuario    
    etiqueta = "tbCodUsr";
    document.getElementById(etiqueta).value = "";
   
    //Password de Usuario
    etiqueta = "tbPassUsr";
    document.getElementById(etiqueta).value = "";
    
    //Nombre de Usuarios
    etiqueta = "tbNomUsr";
    document.getElementById(etiqueta).value= "";
    
    //Apellido 1 de Usuario
    etiqueta = "tbApe1Usr";
    document.getElementById(etiqueta).value= "";
    
    //Apellido 2 de Usuario
    etiqueta = "tbApe2Usr";
    document.getElementById(etiqueta).value= "";
    
    if(vTipoAfi == "1")
    {
        //Dia de Nacimiento
        etiqueta = "tbDiaNacUsr";
        document.getElementById(etiqueta).value= "";
    
        //Mes de Nacimiento
        etiqueta = "tbMesNacUsr";
        document.getElementById(etiqueta).value= "";
 
        //Año de Nacimiento
        etiqueta = "tbAnoNacUsr";
        document.getElementById(etiqueta).value= "";
    }
    
    //Dirección
    etiqueta = "tbDirecUsr";
    document.getElementById(etiqueta).value= "";
    
    //Código Postal
    etiqueta = "tbCodPosUsr";
    document.getElementById(etiqueta).value= "";
    
    //Municipio
    etiqueta = "tbMunicUsr";
    document.getElementById(etiqueta).value= "";

    //Provincia
    etiqueta = "tbProvUsr";
    document.getElementById(etiqueta).value= "";

    //Pais
    etiqueta = "tbPaisUsr";
    document.getElementById(etiqueta).value= "ESP";

    //Email
    etiqueta = "tbEmail";
    document.getElementById(etiqueta).value= "";

    //Teléfono 
    etiqueta = "tbTlfnoFijo";
    document.getElementById(etiqueta).value= "";
    
    //Móvil 
    etiqueta = "tbTlfnoMov";
    document.getElementById(etiqueta).value= "";
    
    //Preugunta 1 
    etiqueta = "tbPregunta1";
    document.getElementById(etiqueta).value= "";
    
    //Preugunta 2
    etiqueta = "tbPregunta2";
    document.getElementById(etiqueta).value= "";
     
    //Respuesta 1
    etiqueta = "tbRespuesta1";
    document.getElementById(etiqueta).value= "";

    //Respuesta 2
    etiqueta = "tbRespuesta2";
    document.getElementById(etiqueta).value= "";
   
   //Documento de identificacion
   	document.getElementById("tbDocuAfi").value="";

    //Check de condiciones
    etiqueta = "chkCertifico";       
    document.getElementById(etiqueta).checked=true;         
    
    if(vTipoAfi == "2")
    {
        //Nombre Empresa
        etiqueta = "tbNomEmp";
        document.getElementById(etiqueta).value  = "";
    }
       
    //Ponemos el focus en el Código de Usuario       
   // SetFocus('tbCodUsr');
}
 
// -------------------------------------------------------------------------
// Función ques se encarga de cargar el contenedor dela izqierda del 
// registro de Afiliado
// ------------------------------------------------------------------------ 
function cargarInfoRegAfiliado() 
{   
    var tipoAfiliacion = "1";
    var tipoAfiliado = "1";
    //Cargamos la estructura de Mis Apuestas
    index.cargarInfoRegAfiliado(tipoAfiliado,tipoAfiliacion,cargarInfoRegAfiliado_CallBack);
}

// -------------------------------------------------------------------------
// Cargamos el contenedor de la derecha con la información de Registro 
// de Afiliado
// ------------------------------------------------------------------------
function cargarInfoRegAfiliado_CallBack(response)
{
    if(response.value != "NOSESS")
    {
        //Mostramos las tablas con las Apuestas
        var etiqueta="dvcontInfoTipoApuesta";
	    var contenido=response.value;

    	if (contenido != "")
	    {
    	    //Incluimos el HTML
        	document.getElementById(etiqueta).innerHTML = contenido;	    	   	
	    }
    }	    
}

// -------------------------------------------------------------------------
// Función que se encarga de tratar los casos de cambio de tipo de afiliado
// de empresa / particular
// ------------------------------------------------------------------------
function chgTipoAfiliado()
{
   
    //Obtenemos el valor del tipo de afiliado
    etiqueta = "tbTipoAfiliado";
    var cbTipoAfiliado = document.getElementById(etiqueta).value;
    
    if(cbTipoAfiliado == "1")
    {
        CargarFormRegAfi('1');
    }
    
    if(cbTipoAfiliado == "2")
    {      
        CargarFormRegAfi('2');
    }
}

// -----------------------------------------------------------
// Funcion para validar los pasos del asistente de 
// registro de usuarios, por ahora solo se comprueba
// que esten rellenos los campos, mas tarde a la 
// hora de insertar se realizan mas comprobaciones
// -----------------------------------------------------------
function SigPaso1RegAfi()
{
	// obtenemos los campos a chequear
	var usuario = document.getElementById("tbCodUsr").value;
	var pass = document.getElementById("tbPassUsr").value;
	var pass2 = document.getElementById("tbPassUsr2").value;
	var email = document.getElementById("tbEmail").value;
	
	index.SigPaso1RegAfi(usuario, pass, pass2, email, SigPaso1RegAfi_Callback);
}

function SigPaso1RegAfi_Callback(response)
{
	resultado = response.value;
	
	if(resultado == "OK")
	{
			document.getElementById("RegUsrCampoError1").style.display = "none";
			changeme("RegUsrPaso1", "hide");
			changeme("RegUsrPaso2", "show");
			changeme("RegUsrPaso3", "hide");	
	}
	else 
	{
		// Error
		document.getElementById("RegUsrCampoError1").style.display = "block";
		document.getElementById("RegUsrCampoError1").innerHTML = resultado;
	}
}

function SigPaso2RegAfi(tipo)
{
	// obtenemos los campos a chequear
	var nombre = document.getElementById("tbNomUsr").value;
	var ape1 = document.getElementById("tbApe1Usr").value;
	var ape2 = document.getElementById("tbApe2Usr").value;
	var direc = document.getElementById("tbDirecUsr").value;
	var codpos = document.getElementById("tbCodPosUsr").value;
	var munic = document.getElementById("tbMunicUsr").value;
	var provi = document.getElementById("tbProvUsr").value;
	var url = document.getElementById("tbUrlAfiliado").value;
	var DocuAfi = document.getElementById("tbDocuAfi").value;
	
	if(tipo == "1")
	{
		var dianac = document.getElementById("tbDiaNacUsr").value;
		var mesnac = document.getElementById("tbMesNacUsr").value;
		var anonac = document.getElementById("tbAnoNacUsr").value;
		var nomemp = "";
	}
	else
	{
		var nomemp = document.getElementById("tbNomEmp").value;
		var dianac = "";
		var mesnac = "";
		var anonac = "";
	}
		
	index.SigPaso2RegAfi(tipo, nombre, ape1, ape2, dianac, mesnac, anonac, direc, codpos, munic, provi, nomemp, url, DocuAfi, SigPaso2RegAfi_Callback);
}

function SigPaso2RegAfi_Callback(response)
{
	resultado = response.value;
	
	if(resultado == "OK")
	{
			document.getElementById("RegUsrCampoError2").style.display = "none";
			changeme("RegUsrPaso1", "hide");
			changeme("RegUsrPaso2", "hide");
			changeme("RegUsrPaso3", "show");	
	}
	else 
	{
		// Error
		document.getElementById("RegUsrCampoError2").style.display = "block";
		document.getElementById("RegUsrCampoError2").innerHTML = resultado;
	}
}

function AntPaso2RegAfi()
{
	changeme("RegUsrPaso1", "show");
	changeme("RegUsrPaso2", "hide");
	changeme("RegUsrPaso3", "hide");
}

function AntPaso3RegAfi()
{
	changeme("RegUsrPaso1", "hide");
	changeme("RegUsrPaso2", "show");
	changeme("RegUsrPaso3", "hide");
}

