Event.observe(window, 'load', init, false);
Event.observe(window, 'load', initAccordions, false);

function init() {
    if (document.getElementById) {
    	if (document.getElementById('colores')) {
	    	initColores();
	    }
        if (document.getElementById('search')) {
            var t_cualquiera = "";

			if (document.getElementById('idpob') && document.getElementById('idrec')) {
            	var opts1 = {Parameters: ['f=myForm'], add_empty_opt: true, add_empty_label: t_cualquiera};
            	var pobs  = new FormsHandler.DoubleCombo('idpob', 'idrec', '/webservice.php', opts1);
            }
            if (document.getElementById('idprov') && document.getElementById('idpob')) {
            	var opts2 = {Parameters: ['f=myForm'], add_empty_opt: true, add_empty_label: t_cualquiera};
				var provs = new FormsHandler.DoubleCombo('idprov', pobs, '/webservice.php', opts2);
           	}
            if (document.getElementById('idorg') && document.getElementById('idsuborg')) {
            	var opts3 = {Parameters: ['f=myForm'], add_empty_opt: true, add_empty_label: t_cualquiera};
            	var orgs  = new FormsHandler.DoubleCombo('idorg', 'idsuborg', '/webservice.php', opts3);
           	}
        }
        if (document.getElementById('recomendados')) {
            initPestanas('recomendados');
        }
        if (document.getElementById('recomendados_concursos')) {
            initPestanas('recomendados_concursos');
        }
        if (document.getElementById('recomendados_cursos')) {
            initPestanas('recomendados_cursos');
        }
        if (document.getElementById('hoy_altea_actividades')) {
            initPestanas('hoy_altea_actividades');
        }
        if (document.getElementById('hoy_altea_concursos')) {
            initPestanas('hoy_altea_concursos');
        }
        if (document.getElementById('hoy_altea_cursos')) {
            initPestanas('hoy_altea_cursos');
        }
    }
}

function update_sop(what, ruta) {
    var idsop = what.options[what.selectedIndex].value;
    location = ruta+'/?idsop='+idsop;
}

function initColores() {
	var obj = document.getElementById('colores');
	if (!obj) {
		return false;
	}

	if (document.getElementById('tcons')) {
	    var idcol = 'tcons';
	} else if (document.getElementById('tcurs')) {
	    var idcol = 'tcurs';
    } else {
	    var idcol = 'tacts';
	}

	obj.onclick = function() {
		processDiv(idcol);
	}
}

function processDiv(id) {
	if (!id) {
		return;
	}
	var obj = document.getElementById(id);
	if (!obj) {
		return false;
	}
	if (obj && obj.style && obj.style.display != 'block') {
        obj.style.display = 'block';
    } else {
	    obj.style.display = 'none';
    }
}

function initAccordions() {
    if (!document.getElementById('vertical_container')) {
        return;
    }
    var topAccordion = new accordion('vertical_container', {
		resizeSpeed : 10,
		classNames : {
			toggle : 'accordion_toggle',
			toggleActive : 'accordion_toggle_active',
			content : 'accordion_content'
		},
		direction : 'vertical',
		onEvent : 'click'
	});

	var i = 0;
	var nestedVerticalAccordion = new Array();
	while ($('nested_container_'+i)) {
	    nestedVerticalAccordion[i] = new accordion('nested_container_'+i, {
    	    resizeSpeed : 10,
    	    classNames : {
    			toggle : 'nested_accordion_toggle',
    			toggleActive : 'nested_accordion_toggle_active',
    			content : 'nested_accordion_content'
    		},
    		direction : 'vertical',
		    onEvent : 'click'
    	});
    	i += 1;
	}
	if (!menu_idpadre) {
        menu_idpadre = 0;
    }
    topAccordion.activate($$('#vertical_container .accordion_toggle')[menu_idpadre]);

	if (menu_idsubpadre) {
	    nestedVerticalAccordion[menu_idsubpadre].activate($$('#nested_container_'+menu_idsubpadre+' .nested_accordion_toggle')[0]);
    }

}

function basename(text) {
    var b=text.match(/[\/|\\]([^\\\/]+)$/);
    return b[1];
}

function isValidMail(mail) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(mail)) {
	    return true;
	}
	return false;
}

var calobj = new Object();
calobj.successHandler = function(request) {
    var obj = document.getElementById('calendario_content');
    if (!obj) {
        alert('No se ha podido detectar el calendario');
        return false;
    }
    obj.innerHTML = request.responseText;
}

function procesa_cal(fecha, type) {
    if (!type) {
        type = 'act';
    }
    if (!fecha) {
        alert('Debe especificar una fecha');
        return false;
    }
    var obj = document.getElementById('calendario_content');
    if (!obj) {
        alert('No se ha podido detectar el calendario');
        return false;
    }
    obj.innerHTML = '<div class="cal_loading"><img src="/images/loading.gif" /></div>';
    var con = new net.ContentLoader(calobj, '/webservice.php', 'POST', []);
    con.sendRequest('type='+type+'&cal=1&fecha='+fecha);

    return false;
}

var respobj = new Object();
respobj.successHandler = function(request) {
    var obj = document.getElementById('cal_multiple');
    if (!obj) {
        alert('No se ha podido detectar el calendario');
        return false;
    }
    obj.innerHTML = request.responseText;

    initCal();
}

function procesa_dia(fecha) {
    if (!fecha) {
        alert('Debe especificar una fecha');
        return false;
    }
    var obj = document.getElementById('cal_multiple');
    if (!obj) {
        alert('No se ha podido detectar el calendario');
        return false;
    }
    if (obj && obj.style && obj.style.display != 'block') {
        obj.style.display = 'block';
    }
    var con = new net.ContentLoader(respobj, '/webservice.php', 'POST', []);
    con.sendRequest('fecha='+fecha);

    return false;
}

function check_form_register() {
    var form = document.getElementById('frm_register');
    if (!form) {
        alert('No se ha podido detectar el formulario');
        return false;
    }
    if (!form.login.value) {
        alert('Debe rellenar el login');
        form.login.focus();
        return false;
    }
    if (form.login.value.length < 5) {
        alert('El login debe tener como minimo 5 caracteres');
        form.login.focus();
        return false;
    }
    if (!form.pass.value) {
        alert('Debe rellenar el password');
        form.pass.focus();
        return false;
    }
    if (form.pass.value.length < 6) {
        alert('El password debe tener como minimo 6 caracteres');
        form.pass.focus();
        return false;
    }
    if (!form.confirm_pass.value) {
        alert('Debe rellenar la confirmacion del password');
        form.confirm_pass.focus();
        return false;
    }
    if (form.pass.value != form.confirm_pass.value) {
        alert('La confirmacion del password y el password no coinciden');
        form.confirm_pass.focus();
        return false;
    }
    if (!form.codigo.value || form.codigo.value.strlen < 1) {
        alert('Debe rellenar el codigo que aparece en la imagen');
        form.codigo.focus();
        return false;
    }
    if (!form.email.value) {
        alert('Debe rellenar el email');
        form.email.focus();
        return false;
    }
    if (!isValidMail(form.email.value)) {
        alert('Debe introducir un email correcto');
        form.email.select();
        return false;
    }
    if (!form.country.options[form.country.selectedIndex].value) {
        alert('Debe rellenar pais');
        form.country.focus();
        return false;
    }
    if (!form.zip_code.value) {
        alert('Debe rellenar el codigo postal');
        form.zip_code.focus();
        return false;
    }
    if (form.gender) {
        var found = 0;
        var i = 0;
        for (i=0; i < form.gender.length; i++) {
            if (form.gender[i].checked) {
                found = 1;
                break;
            }
        }
        if (!found) {
        	alert("Debe marcar el sexo");
        	return false;
        }
    }
    if (!form.birthday_mon.options[form.birthday_mon.selectedIndex].value) {
        alert('Debe rellenar el dia de nacimiento');
        form.birthday_mon.focus();
        return false;
    }
    if (!form.birthday_day.options[form.birthday_day.selectedIndex].value) {
        alert('Debe rellenar el dia de nacimiento');
        form.birthday_day.focus();
        return false;
    }
    if (!form.birthday_yr.options[form.birthday_yr.selectedIndex].value) {
        alert('Debe rellenar el dia de nacimiento');
        form.birthday_yr.focus();
        return false;
    }
    if (!form.sector.options[form.sector.selectedIndex].value) {
        alert('Debe rellenar el sector');
        form.sector.focus();
        return false;
    }

    Modalbox.show('/registro/?ajax=1',
    {title: "Registrarse", width: 500, height: 500, closeString: 'X', overlayOpacity: 0.50, method: 'post', params:Form.serialize('frm_register') });
    return false;
}

function initPestanas(mainid) {
    if (!mainid) {
        alert('Debe especificar un ID');
        return false;
    }
    var mod = document.getElementById(mainid);
    if (!mod) {
        alert('No se ha podido encontrar el modulo');
        return false;
    }

    var objs = mod.getElementsByTagName('span');
    if (objs && objs.length) {
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].className == 'pestana') {
                objs[i].onclick = function() {
                    activaPestana(mainid, this.id);
                }
                var id  = objs[i].id.substring(1);
                var obj = document.getElementById(id);
                if (obj && obj.style) {
                    obj.style.display = 'none';
                }
            } else {
                objs[i].onclick = '';
                var id  = objs[i].id.substring(1);
                var obj = document.getElementById(id);
                if (obj && obj.style) {
                    obj.style.display = 'block';
                }
            }
        }
    }
}

function activaPestana(mainid, id) {
    if (!mainid || !id) {
        return false;
    }
	var mod = document.getElementById(mainid);
    if (!mod) {
        alert('No se ha podido encontrar el modulo');
        return false;
    }
    var objs = mod.getElementsByTagName('span');
    if (objs && objs.length) {

        // Verificamos que el DIV destino existe
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].className=='pestana' || objs[i].className=='pestana sel') {
                var idcontent = objs[i].id.substring(1);
                var obj       = document.getElementById(idcontent);
                if (objs[i].id == id && !obj) {
                    alert('No se ha encontrado el DIV del contenido');
                    return false;
                }
            }
        }

        // Todo OK
        for (var i = 0; i < objs.length; i++) {
            if (objs[i].className=='pestana' || objs[i].className=='pestana sel') {
                var idcontent = objs[i].id.substring(1);
                var obj       = document.getElementById(idcontent);
                if (objs[i].id == id) {
                    if (obj && obj.style) {
                        obj.style.display = 'block';
                    }
                    objs[i].className = 'pestana sel';
                    objs[i].onclick = null;
                } else {
                    if (obj && obj.style) {
                        obj.style.display = 'none';
                    }
                    objs[i].className = 'pestana';
                    objs[i].onclick = function() {
                        activaPestana(mainid, this.id);
                    }
                }
            }
        }
    }
}

function show_form_register() {
    if (!Modalbox) {
        alert('requiere modalbox');
        return false;
    }

    /*this.blur();*/
    Modalbox.show('/registro/?ajax=1',
    {title: "Registrarse", width: 500, height: 550, closeString: 'X', transitions: false, overlayOpacity: 0.50, onUpdate: initFormRegister, afterLoad: initFormRegister });
    return false;
}
