// JavaScript Document
startday = new Date();
clockStart = startday.getTime();
function initStopwatch() { 
var myTime = new Date(); 
return((myTime.getTime() - clockStart)/1000); 
}
function getSecs() { 
var tSecs = Math.round(initStopwatch()); 
var iSecs = tSecs % 60;
var iMins = Math.round((tSecs-30)/60);   
var sSecs ="" + ((iSecs > 9) ? iSecs : "0" + iSecs);
var sMins ="" + ((iMins > 9) ? iMins : "0" + iMins);
document.getElementById("timespent").innerHTML = sMins+":"+sSecs;
window.setTimeout('getSecs()',1000); 
}
//funcion que manda llamar la funcion que muestra el bloque
function verifica(objeto,campo)
{
	var theObj;
	theObj = eval(document.all[objeto]);
	if (theObj.checked)
		muestraBloque(campo,'block');
	else 
		muestraBloque(campo,'none') ;
}

//funcion para habilitar una seccion
//cuando se selecciona un checkbox
function habilitarBloque(chkbx, idDiv)
{
	var div = document.getElementById(idDiv);
	if(chkbx.checked)
	{
		div.style.display = "block";
	}
	else
	{
		div.style.display = "none";
	}
}

//funcion para mostrar uno u otro boloque
function cambiarSubform(form1, form2,desabilitar)
{
	desabilitar = false;
	var f1=document.getElementById(form1);
	var f2=document.getElementById(form2);
	f1.style.display="block";
	f2.style.display="none";
	//muestraBloque(form1, 'block');
	//muestraBloque(form2, 'none');
}

//funcion para mostrar el bloque
function muestraBloque(objeto,accion)
{
	var theObj;
	theObj = document.getElementById(objeto);
	if (theObj)
	{
		if(accion == 'auto')
		{
			if (theObj.style.display == 'none' )
				accion='block';
			else
				accion='none';			
		}
		
		theObj.style.display = accion;
	}		
}



//esta funcion unicamente sera llamada cuando sea alta de usuario y primero tiene
//que ser utilizada la de chk_usuario para que exista el campo de control
//<input type=hidden name=validado value=0 id=validado> para verificar si es correcto
// o no el usuario
function chkusr(x,usr,msg)
{
	var validado = x.value;

	if (validado == "1")
	{
		alert(msg);
		usr.focus();
		return true;
	}
}

//esta funcion se conecta a la base de datos y verifica la existencia del
//usuario y para esto necesita el archivo comprueba.php
function chk_usuario(archivo,x,y){
	var pos_url = archivo;
	var nombre = document.getElementById(x).value;
	var req = nuevoAjax();
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
				document.getElementById(y).innerHTML = req.responseText;
			}
		}
		req.open('GET', pos_url +'?nombre='+nombre,true);
		req.send(null);
	}
}
//Quita los espacios sobrantes a izquierda y derecha en un input
function trim(x)
{
	while(x.value.substring(0,1)==" "){x.value = x.value.substring(1, x.value.length);}
	while(x.value.substring(x.value.length-1,x.value.length)==" "){x.value = x.value.substring(0, x.value.length-1);}
}
//valida que no deje un input vacio
function vacio(x, msg)
{
	trim(x);
	if(x.value == ""){Action(x, msg); return true}
}

//Valida que se capture un Email con @ y .
function wrongEmail(x, msg)
{
	if(x.value.indexOf("@") == -1){Action(x, msg); return true}
	if(x.value.indexOf(".") == -1){Action(x, msg); return true}
}
//Envia un mensaje y enfoca al elemento x
function Action(x, msg){alert(msg); x.focus(); /*x.select();*/}
//Valida que el input contenga sólo números enteros
function noEntero(x, msg){ if(isNaN(x.value) || x.value.indexOf(".")!=-1 || x.value.indexOf("-")!=-1){Action(x, msg); return true}}

//Valida que el input contenga sólo números (enteros o flotantes) no negativos
function noNumero(x, msg){ 
if(isNaN(x.value) || x.value.indexOf("-")!=-1){
	Action(x, msg); return true;
	}
}

//Valida que el input contenga menos de i caracteres
function ValLenMax(x, i, msg) { if(x.value.length > i){Action(x, msg); return true} }

//Valida que el input contenga al menos i caracteres
function ValLenMin(x, i, msg) { if(x.value.length < i){Action(x, msg); return true} }

//Valida que el input contenga exactamente i caracteres
function ValLenExact(x, i, msg) { if(x.value.length != i){Action(x, msg); return true} }

//Valida que se seleccione una opción en una lista o combo
function noSel(x, msg){ if(x.selectedIndex==0){Action(x, msg); return (true);}}

//Valida que se cheque un checkbox y optionbox
function noCheck(x, msg)
{
	var i;
	for(i=0; i<x.length; i++)
	{
		if(x[i].checked)
		{
			return false;
		}
	}
	Action(x[0], msg);
	return true;
}

//Valida que no se capturen acentos en los inputs
function noAccents(x, msg)
{
	if( x.value.indexOf("á")!=-1 || x.value.indexOf("é")!=-1 || x.value.indexOf("í")!=-1 || x.value.indexOf("ó")!=-1 || x.value.indexOf("ú")!=-1 ){Action(x, msg); return true}
	if( x.value.indexOf("Á")!=-1 || x.value.indexOf("É")!=-1 || x.value.indexOf("Í")!=-1 || x.value.indexOf("Ó")!=-1 || x.value.indexOf("Ú")!=-1 ){Action(x, msg); return true}
}

//Valida que no se capturen espacios en los inputs
function noSpaces(x, msg)
{
	if( x.value.indexOf(" ")!= -1 ){Action(x, msg); return true}
}

//Valida que no se capturen caracteres especiales que
//corrompan las consultas SQL
function noSpecial(x, msg)
{
	if( x.value.indexOf('"')!=-1 || x.value.indexOf("'")!=-1 || x.value.indexOf("%")!=-1 || x.value.indexOf("#")!=-1  || x.value.indexOf("*")!=-1 ){Action(x, msg); return true}
}

//verifica que un campo no se haya llenado unicamente con espacios en blanco

function checarVacio(q) {
	   for ( i = 0; i < q.length; i++ ) {
                if ( q.charAt(i) != " " ) {
                        return true
                }
        }
        return false
}
// funcion para contar los caracteres de un comentario
function textCounter(field, countfield, maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	else 
	countfield.value = maxlimit - field.value.length;
}
//ventana
function NewWin(filename,winname, w, h) {
	winOptions = eval("'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,top=10,left=250,width=" + w + ",height=" + h + "'");
	obj = window.open(filename,winname, winOptions); 
	obj.focus();
	return;
}

