/*
-----------------------------------------------------------------------------
File............: vrivas.js
Author..........: Victor Manuel Rivas Santos (vrivas@ujaen.es)
Date............: 17-Nov-2000
Description.....: Functions to generate a scheduling time-table
MOdificactions..:
=========== 1 ===========
Author..........: Víctor Manuel Rivas Santos (vrivas@ujaen.es)
Date............: 30-Aug-2004
Description.....: Adding instance variables to HORARIO so that behavior can be changed.
		  This version is fully compatible with previous one.
=========== 2 ===========
Author..........:
Date............:
Description.....:

----------------------------------------------------------------------------
*/

function edln( _cad ) {
  document.writeln( "<FONT SIZE='-1' FACE='arial'>"+
		    _cad+
		    "</FONT>" );
}

var DIAS_SEMANA=new Array("LUNES","MARTES","MIERCOLES","JUEVES",
			 "VIERNES","SABADO","DOMINGO");
var HORAS=new Array("00:00","00:30", "01:00", "01:30", "02:00", "02:30",
		    "03:00","03:30", "04:00", "04:30", "05:00", "05:30",
		    "06:00","06:30", "07:00", "07:30", 
		    "08:00","08:30","09:00","09:30",
		    "10:00","10:30","11:00","11:30","12:00","12:30",
		    "13:00","13:30","14:00","14:30","15:00","15:30",
		    "16:00","16:30","17:00","17:30","18:00","18:30",
		    "19:00","19:30","20:00","20:30","21:00","21:30", 
		    "22:00", "22:30", "23:00", "23:30" );

var HORARIO_COLOR_PRACTICAS="#FFA0A0";
var HORARIO_COLOR_TEORIA="#A0A0FF";
var HORARIO_COLOR_TUTORIAS="#F0F0A0";
var HORARIO_COLOR_FONDO="#a0a0a0";
var HORARIO_COLOR_FONDO2="#d0d0d0";



function localiza_dia( _dia ) {
  var i=0; 
  if ( _dia=="MIÉRCOLES" ) {
  	_dia="MIERCOLES";
  }
  while( _dia!=DIAS_SEMANA[i] && i<DIAS_SEMANA.length ) {
    ++i;
  }
  if ( i>=DIAS_SEMANA.length ) {
    /* alert( _dia+": Día de semana no encontrado" ); */
    return -1;
  } else {
    return i;
  }
}

function localiza_hora( _hora ) {
  var i=0;
  while( _hora!=HORAS[i] && i<HORAS.length ) {
    ++i;
  }
  if ( i>=HORAS.length ) {
    /* alert( _hora+": Periodo de tiempo no encontrado" ); */
    return -1;
  } else {
    return i;
  }
}

function show_cita_lista( _dia, _hora, _incremento ) {
	edln( "<P><LI>"+DIAS_SEMANA[_dia]+", "+HORAS[_hora]+"-"+HORAS[_hora+this.duracion*_incremento]+":"+this.texto+"</P>");
}

function show_cita( col_width, incremento_medias_horas) {
  edln( "<TD ALIGN='center' ROWSPAN='"+
       this.duracion*(2/incremento_medias_horas)+"' "+
       " BGCOLOR='"+this.color+"' "+
	" WIDTH='"+col_width+"%'"+
	" STYLE='border: 1pt solid "+HORARIO_COLOR_FONDO+"' "+
       "'>" );
    edln( this.texto );
  edln( "</TD>" );
}

function Cita( _dia, _hora, _texto, _duracion, _color ) {
  this.dia=localiza_dia( _dia );
  this.hora=localiza_hora( _hora );
  this.texto=_texto ;
  this.duracion=(_duracion)?_duracion:1;
  this.color=(_color)?_color:"#ffffff";

  this.show=show_cita;
  this.show_lista=show_cita_lista;
}


function add_cita( _dia, _hora, _texto, _duracion, _color ) {
  var d=localiza_dia( _dia );
  var h=localiza_hora( _hora );
  this.citas[h*DIAS_SEMANA.length+d]=new Cita( _dia, _hora, _texto, _duracion, 
					       _color );
  ++this.num_citas;
}


function show_horario() {
  var row=this.hora_fin-this.hora_inicio+2;
  var col=this.dia_fin-this.dia_inicio+2;
  // La columna de las horas mide un 10%, las demás, 90/(col-1)
  var col_width=Math.round( 90/(col-1) );
  var i,j,pos;
  var acarreos=new Array( 0,0,0,0,0,0,0 );
  var control_num_citas=0;

  //edln( "<TABLE WIDTH='100%' ROW='1' COL='1' BORDER='0' "+
  //     "CELLSPACING='0' CELLPADDING='0'>" );
  //edln( "<TR><TD BGCOLOR='"+HORARIO_COLOR_FONDO+"'>" );

  edln( "<TABLE WIDTH='100%' ROW='"+row+"' COL='"+col+"' BORDER='0' "+
	"CELLPADDING='0' CELLSPACING='0' "+
	" STYLE='border: 0px solid "+HORARIO_COLOR_FONDO+"' "+
	">" );

  // Dias de la semana
  edln( "<TR BGCOLOR='#ffffff'>" );
  edln( "<TH BGCOLOR='#ffffff' WIDTH='10%' "+" STYLE='border: 0px solid "+HORARIO_COLOR_FONDO+"' "+">&nbsp;</TH>" );
  for (i=this.dia_inicio; i<=this.dia_fin; ++i ) {
    edln( "<TH BGCOLOR='"+HORARIO_COLOR_FONDO2+"' WIDTH='"+col_width+"%' "+
		" STYLE='border: 1px solid "+HORARIO_COLOR_FONDO+"' "+
		"'>" );
    edln( DIAS_SEMANA[i] );
    edln( "</TH>" );
  }
  edln( "</TR>" );

  // Filas con las citas
  for( i=this.hora_inicio; i<=this.hora_fin; i+=this.INCREMENTO_MEDIAS_HORAS ) {
    edln( "<TR BGCOLOR='#ffffff'>" );
    edln ("<TD VALIGN='center' ALIGN='center' "+
		" STYLE='border: 1px solid "+HORARIO_COLOR_FONDO+"' "+
	 " BGCOLOR='"+
	 HORARIO_COLOR_FONDO2+"' WIDTH='10%' HEIGHT='"+this.ALTURA_MINIMA+"'><B>") ;
    //if( !(i%2) ) {edln( HORAS[i] );}
    edln( HORAS[i]+"-"+HORAS[i+this.INCREMENTO_MEDIAS_HORAS] );
    edln( "</B></TD>" );
    for( j=this.dia_inicio; j<=this.dia_fin; ++j ) {
      if ( !acarreos[j] ) {
	if ( this.citas[i*DIAS_SEMANA.length+j]  ) {
	  this.citas[i*DIAS_SEMANA.length+j].show( col_width,this.INCREMENTO_MEDIAS_HORAS );
	  acarreos[j]+=this.citas[i*DIAS_SEMANA.length+j].duracion*(2/this.INCREMENTO_MEDIAS_HORAS)-1;
	  ++control_num_citas;
	} else {
	  edln( "<TD "+" STYLE='border: 1px solid "+HORARIO_COLOR_FONDO+"' "+">" );
	  edln( "&nbsp;" );
	  edln( "</TD>" );
	}
	  
      } else {
	--acarreos[j];
      } // if acarreps
    } // for j
    edln( "</TR>" );
  }

  edln( "</TABLE>" ); // tabla interior

  //edln( "</TD></TR></TABLE>" ); //tabla exterior
  
  if (control_num_citas!=this.num_citas ) {
    edln( "<P><h3>ATENCIÓN</h3> Se han escrito "+control_num_citas+
	 " citas y se habían dado de alta "+this.num_citas+".<BR>"+
	 " Esto puede ser debido a:<UL>"+
	 "<LI>Solapamientos en sus citas</LI> "+
	 "<LI>Días/horas no impresos en este calendario</LI>"+
	 "<LI>Incrementos de 30 mins. descoordinados con horas de inicio</LI>"+
	 "<LI>No escribió bien los nombres de los días (deben estar en mayúsculas)</LI>"+
	 "<LI>No escribió bien las horas de inicio (HH:MM, donde MM es 00 ó 30)</LI>"+
	 "</UL></P>" );
  }
}

function show_horario_lista() {
  var tmpMat=new Array;
  tmpMat=this.citas;
	//tmpMat=burbuja4horario( "tmpMat", "dia", "hora" );
	var control_num_citas=0;
	edln( "<UL>" );
	for( j=this.dia_inicio; j<=this.dia_fin; ++j ) {
 	  for( i=this.hora_inicio; i<=this.hora_fin; i+=this.INCREMENTO_MEDIAS_HORAS ) {

			if ( this.citas[i*DIAS_SEMANA.length+j]  ) {
				this.citas[i*DIAS_SEMANA.length+j].show_lista( j,i, this.INCREMENTO_MEDIAS_HORAS );
				control_num_citas++;
			}
		}
	}
	edln( "</UL>" );
	if (control_num_citas!=this.num_citas ) {
    edln( "<P><h3>ATENCIÓN</h3> Se han escrito "+control_num_citas+
	 " citas y se habían dado de alta "+this.num_citas+".<BR>"+
	 " Esto puede ser debido a:<UL>"+
	 "<LI>Solapamientos en sus citas</LI> "+
	 "<LI>Días/horas no impresos en este calendario</LI>"+
	 "<LI>Incrementos de 30 mins. descoordinados con horas de inicio</LI>"+
	 "<LI>No escribió bien los nombres de los días (deben estar en mayúsculas)</LI>"+
	 "<LI>No escribió bien las horas de inicio (HH:MM, donde MM es 00 ó 30)</LI>"+
	 "</UL></P>" );
  }
}

function Horario( _dia_inicio, _dia_fin, _hora_inicio, _hora_fin ) {
  var i=0;
  var j=0;
  this.dia_inicio=localiza_dia( _dia_inicio );
  this.dia_fin=localiza_dia( _dia_fin );
  this.hora_inicio=localiza_hora( _hora_inicio );
  this.hora_fin=localiza_hora( _hora_fin );
  this.citas=new Array;
  this.num_citas=0;

  // Variables that should be modified by directly assigning a value.
  this.INCREMENTO_MEDIAS_HORAS=1;
  this.ALTURA_MINIMA=0;  
  // Object's Methods
  this.muestra=show_horario;
  this.show=show_horario;
  this.show_lista=show_horario_lista;
  this.cita=add_cita;
}

function Horario_Laboral( _hora_inicio, _hora_fin ) {
  h=new Horario( "LUNES", "VIERNES", _hora_inicio, _hora_fin );
  return h;
}

function Horario_Semanal( _hora_inicio, _hora_fin ) {
  h=new Horario( "LUNES", "DOMINGO" , _hora_inicio, _hora_fin );
  return h;
}

function Horario_Maniana( ) {
  h=new Horario_Laboral( "08:00", "14:30" ) ;
  return h;
}

function Horario_Tarde( ) {
  h=new Horario_Laboral( "15:00", "21:30" ) ;
  return h;
}



/*
---------------------------------------------------------------
Algoritmo de ordenacion de matrices por el metodo de la burbuja.
@param _matriz  Matriz a ordenar.
@param _campo1   Nombre del 1er campo por el que ordenar.
@param _campo2   Nombre del 2º campo por el que ordenar.
@return         La matriz ordenada.
---------------------------------------------------------------
*/
function burbuja4horario( _matriz, _campo1, _campo2 ) {
  var tmpPos;
  for( var i=0; i<_matriz.length; ++i ) {
    for( var j=i+1; j<_matriz.length; ++j ) {
       if ( eval( "_matriz[i]."+_campo1 )>eval( "_matriz[j]."+_campo1 ) ) {
         tmpPos=_matriz[i];
         _matriz[i]=_matriz[j];
         _matriz[j]=tmpPos;
       } else {
	  if ( 
		(eval( "_matriz[i]."+_campo1 )==eval( "_matriz[j]."+_campo1 )) 
		&& 
		(eval( "_matriz[i]."+_campo2 )>eval( "_matriz[j]."+_campo2 )) 
	     ) {
            tmpPos=_matriz[i];
            _matriz[i]=_matriz[j];
            _matriz[j]=tmpPos;
          } 
      }
    }
  }
  return _matriz;
};
