// Javascript from Moodle modules
<?php
require_once($CFG->dirroot."/lib/javascript.php");
global $destiny1;
?>


<SCRIPT language="JavaScript">

function CierraVentana()
{
    window.close();
    
    return false;
}

function MoverPosImg(pos, sentido)
{
	elementos = document.images;

    // Nos posicionamos en la imagen origen
	for(i = 0; i < elementos.length && elementos[i].name != 'imagenOrigen'; i++); 
        i += 1;
    
	// Quitamos un elemento a la posicion la multiplicamos por cuatro, ya que mostramos por pantalla 4 imagenes
	// 1.Imagen 2.Flecha Izq 3.Flecha Der 4.Eliminar y le sumamos el desplazamiento hasta la imagenOrigen
	pos = (pos) * 4 + i;
	
	if (sentido == 'izq')
	{
	    newpos = pos - 4;
	}
	else
	{
	    newpos = pos + 4;
	}
	
	// Las imagenes en la secuencia llevan la siguiente secuencia:
	// 0->imagen 1->izq 2->der 3->borrar
	// 4->imagen 5->izq 6->der 7->borrar
	// ....
	name = elementos[newpos].name;
	src = elementos[newpos].src;
	width = elementos[newpos].width;
	height = elementos[newpos].height;
	elementos[newpos].name = elementos[pos].name;
	elementos[newpos].src = elementos[pos].src;
	elementos[newpos].width = elementos[pos].width;
	elementos[newpos].height = elementos[pos].height;
	elementos[pos].name = name;
	elementos[pos].src = src;
	elementos[pos].width = width;
	elementos[pos].height = height;

	return false;
}

function BorrarImgSec(pos)
{
	elementos = document.images;
	
    // Nos posicionamos en la imagen origen
	for(i = 0; i < elementos.length && elementos[i].name != 'imagenOrigen'; i++); 
    i += 1;
    
	// Quitamos un elemento a la posicion la multiplicamos por cuatro, ya que mostramos por pantalla 4 imagenes
	// 1.Imagen 2.Flecha Izq 3.Flecha Der 4.Eliminar y le sumamos el desplazamiento hasta la imagenOrigen
	pos = (pos) * 4 + i;
	
	for (i = pos; i < elementos.length - 4 && elementos[i + 4].width != 0; i += 4)
	{
    	elementos[i].name = elementos[i + 4].name;
	    elementos[i].src = elementos[i + 4].src;
	    elementos[i].width = elementos[i + 4].width;
	    elementos[i].height = elementos[i + 4].height;
	}
	    
	// Al desplazar las imagenes una posicion a la izquierda desaparece el ultimo elemento
	for (; i < elementos.length; i++)
	{
	    elementos[i].width = 0;
	    elementos[i].height = 0;
    }	

	return false;
}

function ChequeoColImg(url)
{
	
	chTeacher = document.forms[4].chTeacher;

    lenChTeacher = chTeacher.length;

	valorTeacher = "";
    for(i = 0; i < lenChTeacher; i++)	
    {
    	if (chTeacher[i].checked == true)
    	{
    		valorTeacher += "1";
    	}
    	else
    	{
    		valorTeacher += "0";
    	}
    }

 	chStudent = document.forms[4].chStudent;

    lenchStudent = chStudent.length;

	valorStudent = "";
    for(i = 0; i < lenchStudent; i++)	
    {
    	if (chStudent[i].checked == true)
    	{
    		valorStudent += "1";
    	}
    	else
    	{
    		valorStudent += "0";
    	}
    }

	window.document.location = url+"&teacherCheck="+valorTeacher+"&studentCheck="+valorStudent;
	
	return false;
}

function ChequeoLista(url, id1, arg1, id2, arg2)
{
    var urlFinal = './'+url+'.php?'+id1+'='+arg1
    
    if (id2!=undefined)
    {
    	urlFinal += "&"+id2+'='+arg2;
    }
    
    window.document.location = urlFinal;
        
    return false;
}

function ChequeoImagen(boton, url, id1, arg1)
{
    elementos = document.forms[0];
    
    switch (boton)
    {
        case 'secuencia':
            elementos.haySec.value = elementos.listaSecuencia[elementos.listaSecuencia.selectedIndex].value;
        case 'guardar':
            // El INPUT 'ficheroUso' debe estar definido siempre antes que 'nombrefichero'
            for (i = 0; i < elementos.length; i++)
            {
                if (elementos[i].name == "ficherouso")
                    var nombreAux = elementos[i].value;
                    
                if (elementos[i].name == "nombreFichero")
                    elementos[i].value = nombreAux;
            }
            break;
        case 'cancelar':
            urlFinal = './'+url+'.php?'+id1+'='+arg1;
            window.document.location = urlFinal;        
            return false;
            break;
        case 'secuencia':
            return false;
            break;
    }
}

function ChequeoSecuencia(boton, url, id1, arg1, id2, arg2)
{
	imagenes = document.images;
    elementos = document.forms[0];
    cad = "";
    
    switch (boton)
    {
        case 'guardar':
        	for(i = 0; i < imagenes.length; i++)
	        {
		        if (imagenes[i].name != 0 && imagenes[i].name != 'imagenOrigen' && imagenes[i].width != 0)
		        {
			        cad += (imagenes[i].name)+",";
		        }
	        }      
            elementos.secImagenes.value = cad;
            break;
        case 'cancelar':
            urlFinal = './'+url+'.php?'+id1+'='+arg1;
            window.document.location = urlFinal;        
            return false;
            break;
        case 'anadir':
            urlFinal = './'+url+'.php?'+id1+'='+arg1+'&'+id2+'='+arg2;
            window.document.location = urlFinal;        
            return false;
            break;
    }
}

function ChequeoAddImgSec(boton, url, id1, arg1, id2, arg2)
{   
    switch (boton)
    {
        case 'anadir':
            imagenes = '';
	        
	        chImagen = document.forms[0].chImagen;

            lenChImagen = chImagen.length;

            for(i = 0; i < lenChImagen; i++)	
            {
    	        if (chImagen[i].checked == true)
    	        {
    		        imagenes += chImagen[i].value+',';
    		    }
            }
            
            if (imagenes != '')
            {                
                imagenes = '&img='+imagenes.substr(0, imagenes.length - 1);
            }
                        
            urlFinal = './secuencia.php?'+id1+'='+arg1+'&'+id2+'='+arg2+imagenes+'&accion=editar';
            window.document.location = urlFinal;        
            return false;
            break;
        case 'cancelar':
            urlFinal = './'+url+'.php?'+id1+'='+arg1+'&'+id2+'='+arg2+'&accion=editar';
            window.document.location = urlFinal;        
            return false;
            break;
    }
}

</SCRIPT>
/**
 * JavaScript for checking or unchecking 
 * all the students or all students in a group.
 *
 * @param toggle Check All/None
 * @param start the first checkbox to be changed
 * @param end the last checkbox to be changed
 * return boolean
 **/
function block_quickmail_toggle(toggle, start, end) {
    // Element ID
    var id = 'mailto'+start;

    // iterate through all of the appropriate checkboxes and change their state
    while(document.getElementById(id) && start != end) {
        document.getElementById(id).checked = toggle;
        start++;
        id = 'mailto'+start;
    }

    return false;
}