var MaxRandom=1000000;

function findPosX(obj) 
{
   var posLeft = 0;
   while (obj.offsetParent) 
   {
      posLeft += obj.offsetLeft;
      obj = obj.offsetParent;
   }
   return posLeft;
}

function findPosY(obj) 
{
   var posTop = 0;
   while (obj.offsetParent) 
   {
      posTop += obj.offsetTop;
	  obj = obj.offsetParent;
   } 
   return posTop;
}

function ReemplazarAmpersonMayorMenor(Cadena)
{
   var Cad;
    
   if (Cadena=='undefined') Cad='No definido';
   else
   {
      Cad=Cadena;	
      while (Cad.indexOf('|')!=-1)
         Cad=Cad.replace('|','&');
      while (Cad.indexOf('[')!=-1)
         Cad=Cad.replace('[','<');
      while (Cad.indexOf(']')!=-1)
         Cad=Cad.replace(']','>');
      while (Cad.indexOf('&Aacute;')!=-1)
         Cad=Cad.replace('&Aacute;','Á');	
      while (Cad.indexOf('&Eacute;')!=-1)
         Cad=Cad.replace('&Eacute;','É');	
      while (Cad.indexOf('&Iacute;')!=-1)
         Cad=Cad.replace('&Iacute;','Í');	
      while (Cad.indexOf('&Oacute;')!=-1)
         Cad=Cad.replace('&Oacute;','Ó');	
      while (Cad.indexOf('&Uacute;')!=-1)
         Cad=Cad.replace('&Uacute;','Ú');
      while (Cad.indexOf('&aacute;')!=-1)
         Cad=Cad.replace('&aacute;','á');	
      while (Cad.indexOf('&eacute;')!=-1)
         Cad=Cad.replace('&eacute;','é');	
      while (Cad.indexOf('&iacute;')!=-1)
         Cad=Cad.replace('&iacute;','í');	
      while (Cad.indexOf('&oacute;')!=-1)
         Cad=Cad.replace('&oacute;','ó');	
      while (Cad.indexOf('&uacute;')!=-1)
         Cad=Cad.replace('&uacute;','ú');	
      while (Cad.indexOf('&Ntilde;')!=-1)
         Cad=Cad.replace('&Ntilde;','Ñ');		 
      while (Cad.indexOf('&ntilde;')!=-1)
         Cad=Cad.replace('&ntilde;','ñ');		 
      while (Cad.indexOf('{')!=-1)
         Cad=Cad.replace('{','¿');
      while (Cad.indexOf('}')!=-1)
         Cad=Cad.replace('}','?');			 
      while (Cad.indexOf('~')!=-1)
         Cad=Cad.replace('~','!');			 
      while (Cad.indexOf('^')!=-1)
         Cad=Cad.replace('^','¡');		    
   }   
   return Cad;	  
}

function CambioMes(SelectDia,SelectMes,SelectYear)
{
   DiaSeleccionado=Number(SelectDia.options[SelectDia.selectedIndex].value);
   Mes=Number(SelectMes.options[SelectMes.selectedIndex].value);
   Year=Number(SelectYear.options[SelectYear.selectedIndex].value);
   
   while (SelectDia.length>0) SelectDia.options[SelectDia.length-1]=null;	
   
   flag=0;  
   
   if (Mes==1) Fin=31
   else 
   if (Mes==2) 
   {
      if ((Year % 4)==0) Fin=29;
	  else Fin=28		  
   }
   else 
   if (Mes==3) Fin=31
   else 
   if (Mes==4) Fin=30
   else 
   if (Mes==5) Fin=31	  
   else 
   if (Mes==6) Fin=30	      
   else 
   if (Mes==7) Fin=31	  
   else 
   if (Mes==8) Fin=31	  
   else 
   if (Mes==9) Fin=30	  
   else 
   if (Mes==10) Fin=31	  
   else 
   if (Mes==11) Fin=30
   else 
   if (Mes==12) Fin=31;	  
   for(i=1;i<=Fin;i++)
   {
      if (i<10) a="0"+i 
	  else a=i;		   
	  	  
      if (i==DiaSeleccionado) {
	     flag=1;
   	     seleccionado="selected";
		 pordefecto="defaultSelected";
	  }	 
	  else {
	     seleccionado="";
		 pordefecto="";
	  }	 
	  SelectDia.options[i-1]=new Option(a,a,pordefecto,seleccionado);			 
	  if (seleccionado=="selected") SelectDia.options[i-1].selected=true; 
   }
   if (flag==0) SelectMinutos.options[0].selected=true;   
}

function EnteroValido(valor)
{

      //intento convertir a entero. 
     //si era un entero no le afecta, si no lo era lo intenta convertir 
     valor = parseInt(valor) 

      //Compruebo si es un valor numérico 
      if (isNaN(valor)) { 
            //entonces (no es numero) devuelvo el valor cadena vacia 
            return false 
      }else{ 
            //En caso contrario (Si era un número) devuelvo el valor 
            return true 
      } 

/*   if (Cadena.length>0)
   {
      if ((Cadena.charAt(0)=="-") || (Cadena.charAt(0)=="0") || (Cadena.charAt(0)=="1") || (Cadena.charAt(0)=="2") || (Cadena.charAt(0)=="3") || (Cadena.charAt(0)=="4") || (Cadena.charAt(0)=="5") || (Cadena.charAt(0)=="6") || (Cadena.charAt(0)=="7") || (Cadena.charAt(0)=="8") || (Cadena.charAt(0)=="9"))   
      {
		  flag=0;
		  j=0;
		  if (Cadena.charAt(0)=="-") {
		     if (Cadena.length>=2) {
			    flag=1;
				j=1;
			 }
		  }
		  else {
		     if (Cadena.length>=2) {
			    if (Cadena.charAt(0)!="0") 
				   flag=1
			 } else flag=1;
		  }
		  
		  if (flag==1) {
		     for (i=j;i<=Cadena.length-1;i++) {
                if ((Cadena.charAt(i)!="0") && (Cadena.charAt(i)!="1") && (Cadena.charAt(i)!="2") && (Cadena.charAt(i)!="3") && (Cadena.charAt(i)!="4") && (Cadena.charAt(i)!="5") && (Cadena.charAt(i)!="6") && (Cadena.charAt(i)!="7") && (Cadena.charAt(i)!="8") && (Cadena.charAt(i)!="9"))   	  
				   flag=0;
			 }
		  }
	      if (flag==1) return true
		  else return false;		    
      }
      else return false;
   }
   else return false;*/
}

function trim(sString) 
{
   while (sString.substring(0,1) == ' ')
   {
      sString = sString.substring(1, sString.length);
   }
   while (sString.substring(sString.length-1, sString.length) == ' ')
   {
   sString = sString.substring(0,sString.length-1);
   }
   return sString;
}  
 
function NumeroMes(NombreMes)
{
   if (NombreMes=="Enero") i=1
   else if (NombreMes=="Febrero") i=2
   else if (NombreMes=="Marzo") i=3
   else if (NombreMes=="Abril") i=4
   else if (NombreMes=="Mayo") i=5
   else if (NombreMes=="Junio") i=6
   else if (NombreMes=="Julio") i=7
   else if (NombreMes=="Agosto") i=8
   else if (NombreMes=="Septiembre") i=9
   else if (NombreMes=="Octubre") i=10
   else if (NombreMes=="Noviembre") i=11   
   else if (NombreMes=="Diciembre") i=12   
   return i;
}

function VerDetalleEvento2(CodigoCurso,SiglaAsignatura,NumeroEvento,PaginaDestino)
{
    URL="DetalleEvento2.php?PaginaDestino="+PaginaDestino+"&Codigo_Curso="+CodigoCurso+"&Sigla_Asignatura="+SiglaAsignatura+"&Numero_Evento="+NumeroEvento;
	 
    Ancho=400;
    Alto=450;

	var winl = (screen.width - Ancho) / 2;
	var wint = (screen.height - Alto) / 2;
	winprops = 'height='+Alto+',width='+Ancho+',top='+wint+',left='+winl+',scrollbars=1';
	win = window.open(URL,"Detalles_Evento",winprops)
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function VerDetalleEvento(CodigoCurso,SiglaAsignatura,NumeroEvento,PaginaDestino)
{
    URL="DetalleEvento.php?PaginaDestino="+PaginaDestino+"&Codigo_Curso="+CodigoCurso+"&Sigla_Asignatura="+SiglaAsignatura+"&Numero_Evento="+NumeroEvento;
	 
    Ancho=400;
    Alto=450;

	var winl = (screen.width - Ancho) / 2;
	var wint = (screen.height - Alto) / 2;
	winprops = 'height='+Alto+',width='+Ancho+',top='+wint+',left='+winl+',scrollbars=1';
	win = window.open(URL,"Detalles_Evento",winprops)
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function SQLToJavaScript2(Cadena)
{
   while (Cadena.indexOf("<BR>",0)!=-1) Cadena=Cadena.replace("<BR>","\n");
   return Cadena;
}

function JavaScriptToHTML(Cadena)
{	 
   while (Cadena.indexOf("\n\r",0)!=-1) Cadena=Cadena.replace("\n\r","<BR>");
   while (Cadena.indexOf("\n",0)!=-1) Cadena=Cadena.replace("\n","<BR>");
   return Cadena;
}

function SQLToJavaScript(Cadena)
{
   Largo=Cadena.length-1;
   i=0;
   NuevaCadena='';
   while (i<=Largo)
   {
      if (((i+1)<=Largo) && ((i+2)<=Largo) && ((Cadena.charAt(i)+Cadena.charAt(i+1)+Cadena.charAt(i+2))=='_sl'))
      {
         NuevaCadena+='\n';
   	     i++;
	     i++;
	     i++;		 	  
      }
      else if (((i+1)<=Largo) && ((i+2)<=Largo) && ((Cadena.charAt(i)+Cadena.charAt(i+1))=='__'))
      {
	     var Cad = new String("\'");
         NuevaCadena+=Cad.charAt(0);
	     i++;
	     i++;	  
      } 
      else if (((i+1)<=Largo) && ((i+2)<=Largo) && ((Cadena.charAt(i)+Cadena.charAt(i+1)+Cadena.charAt(i+2))=='_cs'))
      {
         NuevaCadena+="\'";
	     i++;
	     i++;	
	     i++;			   
      }
      else if (((i+1)<=Largo) && ((i+2)<=Largo) && ((Cadena.charAt(i)+Cadena.charAt(i+1)+Cadena.charAt(i+2))=='_cd'))
      {
         NuevaCadena+='\"';
	     i++;
	     i++;	
	     i++;			   
      }         	  
      else
      {
         NuevaCadena+=Cadena.charAt(i);
         i++;
      }	   
   }
   
   return NuevaCadena;
}


function SQLToHTML(Cadena)
{
   Largo=Cadena.length-1;
   i=0;
   NuevaCadena='';
   while (i<=Largo)
   { 
      if (((i+1)<=Largo) && ((i+2)<=Largo) && ((Cadena.charAt(i)+Cadena.charAt(i+1)+Cadena.charAt(i+2))=='_sl'))
      {
         NuevaCadena+='<br>';
   	     i++;
	     i++;
	     i++;		 	  
      }
      else if (((i+1)<=Largo) && ((i+2)<=Largo) && ((Cadena.charAt(i)+Cadena.charAt(i+1))=='__'))
      {		  
	     var Cad = new String("\'");
         NuevaCadena+=Cad.charAt(0);
	     i++;
	     i++;	  
      } 
      else if (((i+1)<=Largo) && ((i+2)<=Largo) && ((Cadena.charAt(i)+Cadena.charAt(i+1)+Cadena.charAt(i+2))=='_cs'))
      {
         NuevaCadena+="'";
	     i++;
	     i++;	
	     i++;			   
      }
      else if (((i+1)<=Largo) && ((i+2)<=Largo) && ((Cadena.charAt(i)+Cadena.charAt(i+1)+Cadena.charAt(i+2))=='_cd'))
      {
         NuevaCadena+='"';
	     i++;
	     i++;	
	     i++;			   
      }         	  
      else
      {
         NuevaCadena+=Cadena.charAt(i);
         i++;
      }	   
   }
   
   return NuevaCadena;
}

function openPictureWindow_Fever(imageName,imageWidth,imageHeight,alt,posLeft,posTop) {
	newWindow = window.open("","newWindow","width="+imageWidth+",height="+imageHeight+",left="+posLeft+",top="+posTop);
	newWindow.document.open();
	newWindow.document.write('<html><title>'+alt+'</title><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onBlur="self.close()">'); 
	newWindow.document.write('<img src='+imageName+' width='+imageWidth+' height='+imageHeight+' alt='+alt+'>'); 
	newWindow.document.write('</body></html>');
	newWindow.document.close();
	newWindow.focus();
}

function Aleatorio(inferior,superior)
{ 
   var numPosibilidades = superior - inferior;
   var aleat = Math.random() * numPosibilidades;
   aleat = Math.round(aleat);
   return parseInt(inferior) + aleat;
} 

function getVar(name)
{
         get_string = document.location.search;         
         return_value = '';
         
         do { //This loop is made to catch all instances of any get variable.
            name_index = get_string.indexOf(name + '=');
            
            if(name_index != -1)
              {
              get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
              
              end_of_value = get_string.indexOf('&');
              if(end_of_value != -1)                
                value = get_string.substr(0, end_of_value);                
              else                
                value = get_string;                
                
              if(return_value == '' || value == '')
                 return_value += value;
              else
                 return_value += ', ' + value;
              }
            } while(name_index != -1)
            
         //Restores all the blank spaces.
         space = return_value.indexOf('+');
         while(space != -1)
              { 
              return_value = return_value.substr(0, space) + ' ' + 
              return_value.substr(space + 1, return_value.length);
							 
              space = return_value.indexOf('+');
              }
          
         return(return_value);        
}
		 
function alfanumerico(e)
{ 
   var tecla = (document.all) ? e.keyCode : e.which;
   if (65<=tecla && tecla<=90 || 97<=tecla && tecla<=122 || 48<=tecla && tecla<=57 || tecla>=0 && tecla<=31 || tecla==127)
   {
      return true;
   }
   else
   {
      return false;
   }
}

function numerico(e)
{ 
   var tecla = (document.all) ? e.keyCode : e.which;

   if (((tecla>=48) && (tecla<=57)) || (tecla>=0 && tecla<=31) || (tecla==127))
   {
	   return true;
   }
   else
   {
       return false;
   }
}

function Ramdom()
{
   return new Date().getTime();	
}




function ValidarRut(Rut,DV)
{
	/*
	var hasta;
	hasta=RutFamiliarRecivido.length;
	dv=RutFamiliarRecivido.substring(hasta-1,hasta);
	numero=RutFamiliarRecivido.substring(0,hasta-1);
	dv=dv.toUpperCase();
	sw=0;
	y=9;
	rs=0;
	suma=0;
	for (x=hasta-1; x>0; x--)
	{
		mlt=parseInt(RutFamiliarRecivido.substring(x,x-1))
		//alert(mlt);
		if (isNaN(mlt)==true)
		{
				sw=1;
				x=0;
		}
		else
		{
			rs=mlt*y;
			//alert(mlt + ' * ' + rs);
			suma=rs+suma;
			if (y==4) y=9;
			else y--;
				
		}			
	}
	//alert(suma);
	suma=suma%11;
	//alert(suma);
	//alert(dv);
	if (suma==10)
	suma='K';
	if (suma!=dv)
		sw=1;
	if (sw==1)
	{
		alert("Rut no válido");
		numero="";
	}
	else
	numero=numero+dv;
 	return numero;

//-----------------------------------------	
   Rut = document.Login.RutUsuario.value;
   DV  = document.Login.DVRutUsuario.value;
   */
   var dvr = '0';
   suma = 0;
   mul  = 2;
   for (i= Rut.length -1 ; i >= 0; i--) {
      suma = suma + Rut.charAt(i) * mul;
      if (mul == 7) mul = 2
      else mul++;
   }
   res = suma % 11;
   if (res==1) dvr = 'k'
   else if (res==0) dvr = '0'
   else {
      dvi = 11-res;
      dvr = dvi + "";
   }
   if ( dvr != DV.toLowerCase() ) {
      return false;
   }
   else {
      return true;
   }	
	
}

var CodigoSeccionesCargados=new Array();

// Revision y Carga de Codigo  Necesario para aplicaciones (js/css)
function RevisionCodigoCargado(SeccionCodigo, Tipo, After)
{
   var l=CodigoSeccionesCargados.length;
   var i=0;
   var e;
   while ((i<l) && (CodigoSeccionesCargados[i]!=SeccionCodigo)) i++;
   if (i>=l)
   {
      CodigoSeccionesCargados[CodigoSeccionesCargados.length]=SeccionCodigo;
	  e = document.createElement(Tipo);
	  e.async = false;
	  if(Tipo=='script')                          //js
      {
		e.src   = SeccionCodigo;   
		if (e.readyState) { // IE
			e.onreadystatechange = function ()
			{
			  if (e.readyState === 'loaded' || e.readyState === 'complete')
			  {
				e.onreadystatechange = null;
				if(After!=null)After();
			  }
			};
		  } else 
		  { // Others
			e.onload = function()
			{
			  if(After!=null)After();
			};
		  }
	  }
	  else                                         //css
	  {
		 e.type  = 'text/css';
		 e.rel   = 'stylesheet';
		 e.href  = SeccionCodigo;
		 e.media = 'screen';
	  }
      document.getElementsByTagName('head')[0].appendChild(e);
   }
   
}

//Carga los Scripst en Cascada
function CargarScript(ListaScripts,ScriptJS,DataInicial,CallBack)
{
	RevisionCodigoCargado(ListaScripts[ScriptJS],'script',
	function()
	{
		ScriptJS++;
		if(ScriptJS!=ListaScripts.length) CargarScript(ListaScripts,ScriptJS,DataInicial,CallBack); 
		else 
		{
			if((DataInicial!=false)&&(CallBack!=null)) CallBack();
		}
		
	});
}


//Ordemamiento de Columas tipo Fecha en los grid
function date_custom(a,b,order)
{ 
  a=a.split("/")
  b=b.split("/")
  if (a[2]==b[2])
  {
     if (a[1]==b[1])
        return (a[0]>b[0]?1:-1)*(order=="asc"?1:-1);
     else
        return (a[1]>b[1]?1:-1)*(order=="asc"?1:-1);
  } else
       return (a[2]>b[2]?1:-1)*(order=="asc"?1:-1);
}



