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 init_rollovers() {
    var objs = $$('img.rollover');
    if (objs.length) {
        var i   = 0;
        var tam = objs.length;
        for (i = 0; i < tam; i++) {
            if (objs[i].getAttribute('class') == 'rollover') {

                objs[i].addEvent('mouseover', active_rollover);
                objs[i].addEvent('mouseout', inactive_rollover);
            }
        }
    }
}

function active_rollover() {
    if (!this) {
        return;
    }
    var src   = this.getAttribute('src');
    var over  = src.replace(/.+menu[0-9]/, '');
    over      = over.replace('.png', '');
    if (over == '') {
        this.setAttribute('src', src.replace('.png', 'ON.png'));
    }
}

function inactive_rollover() {
    if (!this) {
        return;
    }
    var src   = this.getAttribute('src');
    var clase = this.getAttribute('class');
    var over  = src.replace(/.+menu[0-9]/, '');
    over      = over.replace('.png', '');
    if (over == 'ON') {
        this.setAttribute('src', src.replace('ON.png', '.png'));
    }
}

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;
}

function check_form(formid) {
    if (!formid) {
        alert('No se ha encontrado el ID');
        return false;
    }
    var form = document.getElementById(formid);
    if (!form) {
        alert('No se ha podido detectar el formulario');
        return false;
    }
    var i = 0;
    var elems = form.length;
    var valid = true;
    for (i = 0; i < elems; i++) {
        if (!form.elements[i].value) {
            if (form.elements[i].type != 'submit') {
                form.elements[i].style.border = '1px solid #844';
                form.elements[i].style.background = '#f99';
                valid = false;
            }
        } else {
            if (form.elements[i].type != 'submit') {
                form.elements[i].style.border = '1px solid #484';
                form.elements[i].style.background = '#9f9';
            }
        }
    }
    if (valid) {
        return true;
    }
    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);
                    }
                }
            }
        }
    }
}


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="/img/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 initColores() {
	var obj = document.getElementById('colores');
	if (!obj) {
		return false;
	}
	var idcol = 'tacts';

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

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 init() {
    if ($('colores')) {
        initColores();
    }
}