var timerID = null
var timerRunning = false
var message="Función Deshabilitada!"

function getsysdate(idioma)
{
    var dia;
    var mes;
    var anio;
    var dias = new Array("Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado");
    var idia = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Saturday", "Sunday");
    var meses = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
    var imeses = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var retorno;


	
	fecha = new Date();
    anio = fecha.getYear();

    if ( anio < 1900) anio += 1900;
	
	switch(idioma)
	{
	 	case 'en':
		         dia = idia[fecha.getDay()];
				 mes = imeses[fecha.getMonth()];
			     retorno = dia + ' ' + mes + ' ' + fecha.getDate() +', ' + anio;
				 break;	  
	 	case 'sp':
	             dia = dias[fecha.getDay()];
           	     mes = meses[fecha.getMonth()];
			 	 retorno = dia + ' ' + fecha.getDate() +  ' de ' + mes + ' de ' + anio;
				 break;
	}
	
    return retorno;

}

function stopTimer(){

        if(timerRunning) {
                clearTimeout(timerID);
                timerRunning = false;
        }
} 

function startTimer(){

    stopTimer();
    runClock();
}

function runClock(){

        window.status = TimeNow();

        timerID = setTimeout("runClock()",1000);
        timerRunning = true;
}

function timeNow() {
        now = new Date();
        hours = now.getHours();
        minutes = now.getMinutes();
        seconds = now.getSeconds();
        timeStr = "" + ((hours > 12) ? hours - 12 : hours);
        timeStr  += ((minutes < 10) ? ":0" : ":") + minutes;
        timeStr  += ((seconds < 10) ? ":0" : ":") + seconds;
        timeStr  += (hours >= 12) ? " PM" : " AM";
        return timeStr;
}

function writeDate(lang)
{

   var fecha
   
   fecha = getsysdate(lang) + " " + timeNow();
   document.write(fecha);
}

function mailcheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if (str.indexOf(at)==-1) return false;
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
		if (str.indexOf(at,(lat+1))!=-1) return false;
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
		if (str.indexOf(dot,(lat+2))==-1) return false;
		if (str.indexOf(" ")!=-1) return false;
 		return true;					
}

function CheckSSI()
{
		
		var Flogin=document.login;

		if (Flogin.txtUser.value=='')
		   {
			  document.getElementById('lbl_error2').innerHTML = 'Identificación Inválida';
			  Flogin.txtUser.focus();
			  return false;
		   }
		   
		 if (Flogin.txtId.value=='')
		   {
			   document.getElementById('lbl_error2').innerHTML = 'Clave Inválida';
			   Flogin.txtId.focus();
			   return false;
		   }
}
	
function SwapText(index)
{
 document.getElementById(index).style.fontWeight='bold';
}

function UnSwapText(index)
{
  document.getElementById(index).style.fontWeight='normal';
}

function client_data(info)
{
	if (info == 'width')
	{
		/*width_height_html = '<h4  class="right-bar">Current Screen Resolution</h4>';*/
		width = (screen.width) ? screen.width:'';
		height = (screen.height) ? screen.height:'';
		width_height_html =  width + " x " + height + " pixels";
		(width && height) ? document.write(width_height_html):'';
	}
	else if (info == 'js' )
	{
		document.write('Habilitado');
	}
	else if ( info == 'cookies' )
	{
		expires ='';
		Set_Cookie( 'cookie_test', 'it_worked' , expires, '', '', '' );
		if ( Get_Cookie( 'cookie_test' ) )
		{
			string = 'Habilitadas';
		}
		else {
			string = 'Deshabilitadas';
		}
		document.write( string );
	}
}


function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}

function DisableRightClick()
{
	if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=clickNS4;
	}
	else if (document.all&&!document.getElementById){
	document.onmousedown=clickIE4;
	}
	
	document.oncontextmenu=new Function("alert(message);return false")
}

function MenuSwapText(index)
{
 document.getElementById(index).style.color='#000000';
 document.getElementById(index).style.fontWeight='bold';
}

function MenuUnSwapText(index)
{
  document.getElementById(index).style.color='#FFFFFF';
  document.getElementById(index).style.fontWeight='bold';
}


function SwapTextCol(index)
{
  document.getElementById(index).style.color='#000000';
  document.getElementById(index).style.background='#FFFFFF';
  document.getElementById(index).style.fontWeight='bold';
}

function UnSwapTextCol(index)
{
  document.getElementById(index).style.color='#FFFFFF';
  document.getElementById(index).style.background='#C89C00';
  document.getElementById(index).style.fontWeight='normal';
}

function SwapTextColG(index)
{  document.getElementById(index).style.background='#FFFFF3'; }

function UnSwapTextColG(index)
{  document.getElementById(index).style.background='#E7DFCA'; }

function UnSwapTextColG2(index)
{  document.getElementById(index).style.background='#E7DFCA'; }

function SwapBgSelection(index, index2)
{
  document.getElementById(index).style.background='#E7DFCA';
  document.getElementById(index2).style.background='#E7DFCA';
}

function UnSwapBgSelection(index, index2)
{
  document.getElementById(index).style.background='#FFFFFF';
  document.getElementById(index2).style.background='#FFFFFF';
}

function SwapIndexLetter(index)
{
  document.getElementById(index).style.color='#FFFFFF';
}

function UnSwapIndexLetter(index)
{
  document.getElementById(index).style.color='#000000';
}

function setAjax()
{
	var xmlhttp=false;
	try {
		  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
	catch (e) {
		        try {
					  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
				catch (E) {
					        xmlhttp=false;
					      }
			  }
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
	   { xmlhttp = new XMLHttpRequest(); }
	return xmlhttp;
}

function setcondicion(cod_institucion, index, condi_index, index_indfinanciero, vprograma, vdecimales)
{ var institucion=null;
  var nota=null;
  var condicion=null;
  var ind_financiero=null;
  var programa=null;
  var decimales=null;

  nota = document.getElementById(index).value;
  institucion = cod_institucion;
  ind_financiero= document.getElementById(index_indfinanciero).value;
  programa=vprograma
  decimales=vdecimales;
  if (nota < 0 || nota > 100) nota=null;
  ajax = setAjax();
  ajax.open('GET', 'condicion.php?modval='+cod_institucion+','+nota+','+ind_financiero+','+programa, true);
  ajax.onreadystatechange=function()
                          {
							  if (ajax.readyState==4) 
							  { if (ajax.status==200) 
							      { condicion = ajax.responseText;
									if (isNaN(nota)==false)
									   { //if (decimales=='N') nota=parseInt(nota);
									     document.getElementById(index).value=nota;
								         document.getElementById(condi_index).value=condicion; }
								   }  }  }
  ajax.send(null);
}

function mostrarlogo(logo_idx)
{
  var tbllogo=null;
  var logo=null;
  var html=null;
  tbllogo = document.getElementById(logo_idx).value;

  ajax = setAjax();
  ajax.open('GET', 'uploadfile.php', true);
  ajax.onreadystatechange=function()
                          {
							  if (ajax.readyState==4) 
							  { if (ajax.status==200) 
							      { html = ajax.responseText;
								    document.getElementById('newlogo').innerHTML=html;
								   }  }  }
  ajax.send(null);
}

function CheckCreateMaterias()
{
	var FMaterias = document.regmaterias;
	
	if (FMaterias.sle_materia.value=='') 
	   { document.getElementById('txtErrorMsg').innerHTML='<b>Código Inválido. Debe especificar el código de la materia</b>';
		 FMaterias.sle_materia.focus();
		 return false; }
		 
	if (FMaterias.sle_nommateria.value=='')
	   { document.getElementById('txtErrorMsg').innerHTML='<b>Debe especificar el nombre de la materia</b>';
	     FMaterias.sle_nommateria.focus();
	     return false; }
	
}

function CheckCreateUser()
{
   var Fcreausuarios = document.frm_createusers;
   var tmp = '';
   var txtIdentificacionLen = 0;
   var txtNombreLen = 0;
   var txtApellidoPLen = 0;
   var txtApellidoMLen = 0;
   var txtClaveLen = 0;
   
   /*Verificamos los tamaños de los campos escritos por el usuario*/
   tmp = Fcreausuarios.txtIdentificacion.value;
   txtIdentificacionLen = tmp.length;
  
   tmp = Fcreausuarios.txtNombre.value;
   txtNombreLen = tmp.length;
   
   tmp = Fcreausuarios.txtApellidoP.value;
   txtApellidoPLen = tmp.length;
   
   tmp = Fcreausuarios.txtApellidoM.value;
   txtApellidoMLen = tmp.length;
   
   tmp = Fcreausuarios.txtClave.value;
   txtClaveLen = tmp.length;

   if (Fcreausuarios.txtIdentificacion.value=='') 
      { document.getElementById('txtErrMsg').innerHTML='<b>Usuario Inválido..</b>';
	    Fcreausuarios.txtIdentificacion.focus();
   		return false; }
		
   if (Fcreausuarios.txtNombre.value=='')
      { document.getElementById('txtErrMsg').innerHTML='<b>Debe escribir el nombre del usuario..</b>';
	    Fcreausuarios.txtNombre.focus();
	    return false; }
		
   if (Fcreausuarios.txtApellidoP.value=='')
      { document.getElementById('txtErrMsg').innerHTML='<b>Debe escribir el apellido paterno del usuario..</b>';
	    Fcreausuarios.txtApellidoP.focus();
	    return false; }
		
   if (Fcreausuarios.txtClave.value=='')
      { document.getElementById('txtErrMsg').innerHTML='<b>Debe escribir la contraseña asignada al usuario..</b>';
	    Fcreausuarios.txtClave.focus();
	    return false; }

   if (txtIdentificacionLen > 15)
      { document.getElementById('txtErrMsg').innerHTML='<b>El tamaño máximo para la identificación de usuario es de 15 caracteres.</b>';
	    Fcreausuarios.txtIdentificacion.focus();
	    return false; }
		
   if (txtNombreLen > 40) 
      { document.getElementById('txtErrMsg').innerHTML='<b>El tamaño máximo para el nombre del usuario (incluyendo segundo nombre) es de 40 caracteres).</b>';
	    Fcreausuarios.txtNombre.focus();
	    return false; }
		
   if (txtApellidoPLen > 40) 
      { document.getElementById('txtErrMsg').innerHTML='<b>El tamaño máximo para el apellido del usuario es de 40 caracteres).</b>';
	    Fcreausuarios.txtApellidoP.focus();
	    return false; }
		
   if (txtApellidoMLen > 40) 
      { document.getElementById('txtErrMsg').innerHTML='<b>El tamaño máximo para el apellido materno es de 40 caracteres).</b>';
	    Fcreausuarios.txtApellidoM.focus();
	    return false; }
		
   if (txtClaveLen > 40) 
      { document.getElementById('txtErrMsg').innerHTML='<b>El tamaño para la clave del usuario es de 40 caracteres).</b>';
	    Fcreausuarios.txtClave.focus();
	    return false; }
   
}


function AbrirVentana(url)
{
   window.open(url,"Información","toolbars,scrollbars,location=0,resizable=0,statusbars=0,top=200,width=818,height=750,screenX=300,screenY=200");
}

function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}

