/*
*   Javascript necesario para el correcto funcionamiento del registro de usuarios en IESCampus
*/

/************************************************
*       registraUsuario1.php
************************************************/

function getAnyoBisiesto(anyo)
{
    if(((anyo%400) == 0) || (((anyo%4) == 0) && ((anyo%100) != 0)))
    {
        return true;
    }
    else
    {
        return false;
    }
}

function rellenaDia(diasel)
{
    var selectdia = document.registro1.dia;
    var mes = document.registro1.mes.value;
    var anyo = document.registro1.anyo.value;
    if(selectdia.length > 1)
    {
        while(selectdia.hasChildNodes())
        {
            selectdia.removeChild(selectdia.firstChild);
        }
        var op = document.createElement("option");
        op.value="";
        var tex = document.createTextNode("Día");
        op.appendChild(tex);
        selectdia.appendChild(op);
    }
    var anyoBisiesto = getAnyoBisiesto(anyo);
    for(i=1;i<32;i++)
    {
        if((mes == 2) && (i > 28) && !anyoBisiesto)
        {
            break;
        }
        if((mes == 2) && (i > 29))
        {
            break;
        }
        var op = document.createElement("option");
        op.value=i;
        if(diasel == i)
        {
            op.selected = "selected";
        }
        var tex = document.createTextNode(i);
        op.appendChild(tex);
        selectdia.appendChild(op);
    }
}

function rellenaMes(messel)
{
    var selectmes = document.registro1.mes;
    var meses = Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
    for(i=0;i<12;i++)
    {
        var op = document.createElement("option");
        op.value=i+1;
        if(op.value == messel)
        {
            op.selected = "selected";
        }
        var tex = document.createTextNode(meses[i]);
        op.appendChild(tex);
        selectmes.appendChild(op);
    }
}

function rellenaAnyo(anyosel)
{
    var selectanyo = document.registro1.anyo;
    var d = new Date();
    var y = d.getFullYear();
    var liminf = y - 100;
    for(i=y;i>liminf;i--)
    {
        var op = document.createElement("option");
        op.value=i;
        if(op.value == anyosel)
        {
            op.selected = "selected";
        }
        var tex = document.createTextNode(i);
        op.appendChild(tex);
        selectanyo.appendChild(op);
    }
}

function compruebaDia()
{
    var mes = document.registro1.mes.value;
    var dia = document.registro1.dia.value;
    rellenaDia(dia);
}

var a = 0;
function enviaForm(form)
{
	var pass = form.passwd.value;
	a++; // por culpa de IE
	if((pass != "") && (a == 1)) // por culpa de IE
	{
		var hash = SHA1(pass);
		form.pass.value = SHA1(form.RAND_STR.value+hash);
		form.RAND_STR.value = "";
	}
	form.submit();
}

var f8 = null;
var f9 = null;
var f10 = null;
var f11 = null;
var f12 = null;
var f13 = null;

//MODIFICADO - ÁLVARO
/*
 * - Eliminé todas las referencias a 'emailpad' porque para el registro ya no
 * se necesita el email
 * - Añadí la validación de email al 'usernamepad'
 * - Añadí la ocultación
 */
function visibilityFieldset(show)
{
    var opciones = {
        insertAfterWhatNode: 'tituloRegistro',
        onlyOnSubmit: true
    };
    //if(document.getElementById("fieldsetlogin").style.display != "none") {visibilityLogin();}
    var chek = document.getElementById("habilita");
    
    if(show == undefined)
    {
        if(chek.checked)
        {
            f8 = new LiveValidation('nombrepad',opciones);
            f9 = new LiveValidation('apell1pad',opciones);
            f10 = new LiveValidation('usernamepad',opciones);
            f11 = new LiveValidation('passwdpad',opciones);
            f12 = new LiveValidation('passwd2pad',opciones);
//            f13 = new LiveValidation('emailpad',opciones);
        
            document.getElementById("fieldset").style.display = "block";
            document.getElementById("visibilitylogin").style.display = "block";
            f8.add( Validate.Presence );
            f9.add( Validate.Presence );
            f10.add( Validate.Presence );
            f10.add(Validate.Length, {minimum: 6});
            f10.add( Validate.Email );
            f11.add( Validate.Presence );
            f11.add( Validate.Length, {minimum: 8});
            f12.add( Validate.Presence );
            f12.add( Validate.Length, {minimum: 8});
            f12.add( Validate.Confirmation, {match: 'passwdpad'} );
//            f13.add( Validate.Presence );
//            f13.add( Validate.Email );
        }
        else
        {
            if(f8 != null) f8.remove( Validate.Presence );
            if(f9 != null) f9.remove( Validate.Presence );
            if(f10 != null) {
                f10.remove( Validate.Presence );
                f10.remove( Validate.Length );
                f10.remove( Validate.Email );
            }
            if(f11 != null) {
                f11.remove( Validate.Presence );
                f11.remove( Validate.Length );
            }
            if(f12 != null) {
                f12.remove( Validate.Presence );
                f12.remove( Validate.Length );
                f12.remove( Validate.Confirmation, {match: 'passwdpad'} );
            }
//            if(f13 != null) {
//                f13.remove( Validate.Presence );
//                f13.remove( Validate.Email );
//            }
            
            var fieldset = document.getElementById("fieldset");
            fieldset.style.display = "none";
            document.getElementById("visibilitylogin").style.display = "none";
            var hablog = document.getElementById("habilitaLogin");
            hablog.checked = false;
            visibilityLogin();
            var hijos = fieldset.getElementsByTagName("input");
            var len = hijos.length;
            if(dblclick)
            {
                for(i=0;i<len;i++)
                {
                    hijos[i].value = "";
                }
            }
        }
    }
    else
    {
        if(show)
        {
            document.getElementById("fieldset").style.display = "block";
            document.getElementById("visibilitylogin").style.display = "block";
            if(f8 != null) f8.add( Validate.Presence );
            if(f9 != null) f9.add( Validate.Presence );
            if(f10 != null) {
                f10.add( Validate.Presence );
                f10.add(Validate.Length, {minimum: 6});
                f10.add( Validate.Email );
            }
            if(f11 != null) {
                f11.add( Validate.Presence );
                f11.add( Validate.Length, {minimum: 8});
            }
            if(f12 != null) {
                f12.add( Validate.Presence );
                f12.add( Validate.Length, {minimum: 8});
                f12.add( Validate.Confirmation, {match: 'passwdpad'} );
            }
//            if(f13 != null) {
//                f13.add( Validate.Presence );
//                f13.add( Validate.Email );
//            }
        }
        else
        {
            var fieldset = document.getElementById("fieldset");
            fieldset.style.display = "none";
            var hijos = fieldset.getElementsByTagName("input");
            var len = hijos.length;
            for(i=0;i<len;i++)
            {
                hijos[i].value = "";
            }
            if(f8 != null) f8.remove( Validate.Presence );
            if(f9 != null) f9.remove( Validate.Presence );
            if(f10 != null) {
                f10.remove( Validate.Presence );
                f10.remove( Validate.Length );
                f10.remove( Validate.Email );
            }
            if(f11 != null) {
                f11.remove( Validate.Presence );
                f11.remove( Validate.Length );
            }
            if(f12 != null) {
                f12.remove( Validate.Presence );
                f12.remove( Validate.Length );
                f12.remove( Validate.Confirmation, {match: 'passwdpad'} );
            }
//            if(f13 != null) {
//                f13.remove( Validate.Presence );
//                f13.remove( Validate.Email );
//            }
        }
    }
}

var f14 = null;
var f15 = null;

function visibilityLogin()
{
    var opciones = {
        insertAfterWhatNode: 'tituloRegistro',
        onlyOnSubmit: true
    };
    //if(document.getElementById("fieldset").style.display != "none") {visibilityFieldset();}
    var chek = document.getElementById("habilitaLogin");
    if(chek.checked)
    {
        visibilityFieldset(false);
        document.getElementById("fieldsetlogin").style.display = "block";
        f14 = new LiveValidation('loginpad',opciones);
        f15 = new LiveValidation('passwdloginpad',opciones);
        f14.add( Validate.Presence );
        f15.add( Validate.Presence );
    }
    else
    {
        if(document.getElementById("habilita").checked)
        {
            visibilityFieldset(true);
        }
        var fieldset = document.getElementById("fieldsetlogin");
        fieldset.style.display = "none";
        var hijos = fieldset.getElementsByTagName("input");
        var len = hijos.length;
        for(i=0;i<len;i++)
        {
            hijos[i].value = "";
        }
        if(f14 != null) f14.remove( Validate.Presence );
        if(f15 != null) f15.remove( Validate.Presence );
    }
}

function ocultaFieldset()
{
    var hab = document.getElementById("habilita");
    var hablog = document.getElementById("habilitaLogin");
    if(hablog.checked)
    {
        hablog.checked = false;
        visibilityLogin();
    }
    else
    {
        hab.checked = false;
        visibilityFieldset();
    }
}

/************************************************
*       registraUsuario2.php
************************************************/
//MODIFICADO - ÁLVARO
/*
 * Quité el deshabilitaMunicipio()
 */
function deshabilitaProvincias()
{
    var select = document.getElementById("country");
    var selectProv = document.getElementById("provincia");
    var $filaCompleta = jQuery(selectProv).parent();
    $filaCompleta = $filaCompleta.add($filaCompleta.prev());
    if(select.value != "es")
    {
        $filaCompleta.hide();
        document.getElementById("laspalmas").selected = true;
//        getMunicipios(selectProv.value);
//        deshabilitaMunicipio(true);
//        document.getElementById("radioiesc").checked = true;
    }
    else
    {
        if(jQuery(selectProv).is(':visible'))
            $filaCompleta.hide();
        else
            $filaCompleta.show();
//        deshabilitaMunicipio(false);
    }
}

/*
*   Si force == true, deshabilita el select de municipios sin tener en cuenta el valor de la provincia
*    Si force == false, deshabilita el select de municipios o no, teniendo en cuenta el valor de la provincia
*/
function deshabilitaMunicipio(force,muniselected)
{
    var selectProv = document.getElementById("provincia");
    var selectMuni = document.getElementById("municipio");
    var radio = document.getElementById("radioinsti");
    var radioiesc = document.getElementById("radioiesc");
    //if(((selectProv.value != "35") && (selectProv.value != "43")) || force)
    if(force)
    {
        selectMuni.disabled = true;
        radio.disabled = true;
        radioiesc.checked = true;
        if(force)
        {
            getInstitutos(selectMuni.value);
        }
        deshabilitaInstituto(false);
    }
    else
    {
        selectMuni.disabled = false;
        radio.disabled = false;
        getMunicipios(selectProv.value,muniselected);
        deshabilitaInstituto(true);
    }
}

function getMunicipios(prov,muniselected)
{
    var url = "registroUsuarioAjax.php";
    var param = "func=muni&prov="+prov;
    var ajaxRequest = new Ajax.Request(
        url,
        {
            method: "get",
            parameters: param,
            asynchronous: false,
            onComplete: showResponseMunicipio
        }
    );
    if(muniselected != undefined)
    {
        setMunicipio(muniselected);
    }
}

function setMunicipio(muniselected)
{
    var select = document.getElementById("municipio");
    var len = select.length;
    for(var i=0;i<len;i++)
    {
        if(select[i].value == muniselected)
        {
            select[i].selected = true;
        }
    }
}

function showResponseMunicipio(response)
{
    var municipios = response.responseText.split("##");
    var len = municipios.length;
    var select = document.getElementById("municipio");
    while(select.hasChildNodes())
    {
        select.removeChild(select.firstChild);
    }
    if(response.responseText != "")
    {
        for(var i=0;i<len;i++)
        {
            var par = municipios[i].split("#");
            var option = document.createElement("option");
            option.value = par[0];
            if(par[0] == "17")
            {
                option.selected = true;
            }
            var nom = document.createTextNode(par[1]);
            option.appendChild(nom);
            select.appendChild(option);
        }
    }
    else
    {
        var option = document.createElement("option");
        option.value = "";
        var nom = document.createTextNode("No definido");
        option.appendChild(nom);
        select.appendChild(option);
    }
    return;
}

/*
*   Si cambiar == false, deshabilita el select de institutos.
*   Si cambiar == true, obtiene por Ajax los institutos del municipio
*/
function deshabilitaInstituto(cambiar,instiselected,force)
{
    var radio = document.getElementById("radioinsti");
    var radioiesc = document.getElementById("radioiesc");
    var selectInsti = document.getElementById("instituto");
    if((force === false) || (radio.checked == true))
    {
        if((radio.checked) || (radioiesc.checked))
        {
            var selectMuni = document.getElementById("municipio");
            var radio = document.getElementById("radioinsti");
            if(!cambiar)
            {
                //getInstitutos(selectMuni.value,instiselected);
                selectInsti.disabled = true;
                //selectInsti.value = "10";
            }
            else
            {
                selectInsti.disabled = false;
                getInstitutos(selectMuni.value,instiselected);
            }
        }
    }
    else
    {
        selectInsti.disabled = true;
    }
}

function getInstitutos(muni,instiselected)
{
    var url = "registroUsuarioAjax.php";
    var param = "func=insti&muni="+muni;
    var ajaxRequest = new Ajax.Request(
        url,
        {
            method: "get",
            parameters: param,
            asynchronous: false,
            onComplete: showResponseInstituto
        }
    );
    if(instiselected != undefined)
    {
        setInstituto(instiselected);
    }
}

function setInstituto(instiselected)
{
    var select = document.getElementById("instituto");
    var len = select.length;
    for(var i=0;i<len;i++)
    {
        if(select[i].value == instiselected)
        {
            select[i].selected = true;
        }
    }
}

function showResponseInstituto(response)
{
    var select = document.getElementById("instituto");
    while(select.hasChildNodes())
    {
        select.removeChild(select.firstChild);
    }
    if(response.responseText != "")
    {
        var institutos = response.responseText.split("##");
        var len = institutos.length;
        for(var i=0;i<len;i++)
        {
            var par = institutos[i].split("#");
            if(par[1] != "IESCampus")
            {
                var option = document.createElement("option");
                option.value = par[0];
                var nom = document.createTextNode(par[1]);
                option.appendChild(nom);
                select.appendChild(option);
            }
        }
    }
    else
    {
        var option = document.createElement("option");
        option.value = "";
        var nom = document.createTextNode("No existen institutos");
        option.appendChild(nom);
        select.appendChild(option);
    }
    return;
}

/****************************************************
*       registroUsuario3.php
****************************************************/

function modifyPrepago()
{
    var rad = document.getElementById("modalidad");
    if(rad.checked)
    {
        var form = document.getElementById("prepago");
        form.codefield1.disabled = false;
        form.codefield2.disabled = false;
        form.codefield3.disabled = false;
        form.codefield4.disabled = false;
        form.prepago.disabled = false;
        var rad1 = document.getElementById("radpaypal");
        rad1.checked = false;
        document.getElementById("instruccionespago").style.display = "none";
        modifyPaypal();
    }
    else
    {
        var form = document.getElementById("prepago");
        form.codefield1.disabled = true;
        form.codefield2.disabled = true;
        form.codefield3.disabled = true;
        form.codefield4.disabled = true;
        form.prepago.disabled = true;
    }
}

function modifyPaypal()
{
    var rad = document.getElementById("radpaypal");
    if(rad.checked)
    {
//        document.getElementById("botonpaypal").disabled = false;
        var rad1 = document.getElementById("modalidad");
        document.getElementById("instruccionespago").style.display = "block";
        rad1.checked = false;
        modifyPrepago();
    }
    else
    {
        var botonpaypal = document.getElementById("botonpaypal");
        botonpaypal.disabled = true;
    }
}

function setPagando(alLlegar)
{
    var url = "ajax/gestionPagos/nuevoPago.php";
    //var param = "func=insti&muni="+muni;
    new Ajax.Request(
        url,
        {
            method: "post",
            //parameters: param,
            asynchronous: false,
            onComplete: function(response){
                if(alLlegar)alLlegar(response.responseText);
            }
        }
    );
}

