// JavaScript Document
function addEvent(elemento,nomevento,funcion,captura)
{
  if (elemento.attachEvent)
  {
    elemento.attachEvent('on'+nomevento,funcion);
    return true;
  }
  else  
    if (elemento.addEventListener)
    {
      elemento.addEventListener(nomevento,funcion,captura);
      return true;
    }
    else
      return false;
}

function ajaxConexion() 
{
  var xmlHttp=null;
  if (window.ActiveXObject) 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}

function token()
{
	var hora = new Date();
	var token = hora.getFullYear().toString()+hora.getMonth().toString()+hora.getDate().toString()+hora.getHours().toString()+hora.getMinutes().toString()+hora.getSeconds().toString()+hora.getMilliseconds().toString();
	return token;
}
function reverseShow(id){
	var e = document.getElementById(id);
	if(e.style.display == "none")
		showElement(id);
	else
		hideElement(id);
}
function hideElement(id){
	var e = document.getElementById(id);
	e.style.display = "none";
}
function showElement(id){
	var e = document.getElementById(id);
	e.style.display = "";
}

function limpiarLista(obj)
{
	if(obj != null && obj != 'undefined')
	{
		var long = obj.length;
		for(i=0; i<long ; i++)
			obj.remove(0);	
	}
}

function borrarEspacios(obj){
	if(obj.value != "")
	{
		var txt = obj.value;
		var txt2 = "";
		var bln =false;
		for(i=0; i<txt.length; i++)
			if(txt.charAt(i) != " ")
				break;
				
		txt2=txt.substr(i);		
		txt ="";	
		var c =0;
		for(i=txt2.length-1; i>=0; i--)
			if(txt2.charAt(i) == " ")
				c++;
			else
				break;
				
		txt = txt2;
		txt2 = txt.substr(0, txt2.length - c);
		obj.value = txt2;
	}
}
//
function agregarLista(id, lista)
{
	var cb=document.getElementById(id);
	var cmb = document.getElementById(lista);
	var i=0;
	if(cb.checked)
		cmb.options[cmb.length] = new Option(id, id);
	else
		for(i=0; i<cmb.length; i++)
			if(cmb.options[i].value == id){
				cmb.remove(i);
				break;
			}
}
//
function seleccionarTodo(id)
{
	var cmb = document.getElementById(id);
	cmb.setAttribute("multiple", "true");
	for(i=0; i<cmb.length; i++)
		cmb.options[i].selected=true;	
}

function validarTipoArchivo(upd, extn)
{
	var str = upd.value;
	var long =  str.length;
	var ext = "";
	var i =0;
	for(i=long-4; i<long; i++)
		ext += str.charAt(i);
	allowSubmit = false;
	if (!str) return;
	while (str.indexOf("\\") != -1) str = str.slice(str.indexOf("\\") + 1);
		ext = str.slice(str.indexOf(".")).toLowerCase();
	if (ext == extn) {
		allowSubmit = true;
	}
	

	if (ext == extn) {
		return true;
	} else {
		return false;
	}
}
function validarEmail(txt)
{
	var id_bln=false;
	var dom_bln=false;
	if(txt.indexOf("@")>=5 && txt.indexOf("@")< txt.length-1)
	{
		var id = txt.substr(0, txt.indexOf("@"));
		var dom = txt.substr(txt.indexOf("@")+1);
		var i=0;
		if(id.length>4 && id.charAt(0)!=".")
		{
			id_bln=true;
			for(i=0; i<id.length; i++)
				if(!(id.charCodeAt(i)==46 || (id.charCodeAt(i)>=48 && id.charCodeAt(i)<=57) || (id.charCodeAt(i)>=65 && id.charCodeAt(i)<=90) || (id.charCodeAt(i)>=97 && id.charCodeAt(i)<=122) || id.charCodeAt(i)==45 || id.charCodeAt(i)==95))
				{
					id_bln=false;
					break;
				}
			if(id_bln && dom.charAt(0)!="." && dom.indexOf("@")==-1 && (dom.lastIndexOf(".")== dom.length -3 || dom.lastIndexOf(".")== dom.length -4))
			{
				dom_bln=true;
				for(i=0; i<dom.length; i++)
					if(!(dom.charCodeAt(i)==46 || (dom.charCodeAt(i)>=48 && dom.charCodeAt(i)<=57) || (dom.charCodeAt(i)>=65 && dom.charCodeAt(i)<=90) || (dom.charCodeAt(i)>=97 && dom.charCodeAt(i)<=122) || dom.charCodeAt(i)==45 || dom.charCodeAt(i)==95))
					{
						dom_bln=false;
						break;
					}
			}
		}
	}
	return (id_bln && dom_bln);
}
function txt_maxLength(texto, num) {
	var tecla, in_value, out_value;

	if (texto.value.length > num) {
		in_value = texto.value;
		out_value = in_value.substring(0,num);
		texto.value = out_value;
		return false;
	}
	return true;
}
function ajax(url, destino, cargador){
	var conexion = ajaxConexion();
	switch(cargador)
	{
		case 1:
			cargador="cargando...";
			break;
		case 2:
			cargador="<img src='imagenes/cargador.gif' align='absmiddle'/>";
			break;
		case 3:
			cargador="cargando...<br><img src='imagenes/cargador.gif' align='absmiddle'/>";
			break;
	}
	conexion.onreadystatechange=function(){
		var elemento = document.getElementById(destino);
		if(conexion.readyState == 4)
			elemento.innerHTML = conexion.responseText;
		else 
			elemento.innerHTML = cargador;
	}
	conexion.open("GET", url, true);
	conexion.send(null);
}

function diasEnMes(ano, mes)
{
	if(ano=="" || mes =="")
		return;
		
	if(mes==1 || mes==3 || mes==5 || mes==7 || mes==8 || mes==10 || mes==12)
		return 31;
	else if(mes==2)
	{
		if ((ano % 4 == 0) && ((ano % 100 != 0) || (ano % 400 == 0)))
			return 29;
		else
			return 28;
	}
	else
		return 30;
}
function cargarDias(ano, mes, obj)
{
	var i=0;
	var dias=30;
	
	if(ano=="" || mes =="")
		return;
	if(mes==1 || mes==3 || mes==5 || mes==7 || mes==8 || mes==10 || mes==12)
		dias=31;
	else if(mes==2)
	{
		if ((ano % 4 == 0) && ((ano % 100 != 0) || (ano % 400 == 0)))
			dias=29;
		else
			dias=28;
	}
	limpiarLista(obj);
	for(i=1; i<=dias; i++)
		obj.options[obj.length]=new Option(i, i);
}
function validarTeclaNumerica(e){
	if(!(e.keyCode==13 || e.keyCode==9 || e.keyCode==8 || e.keyCode==46 || e.keyCode== 35 || e.keyCode==16 || (e.keyCode>=48 && e.keyCode<=57) || (e.keyCode>=37 && e.keyCode<=40)))
	{
		if(window.event)
			window.event.returnValue=false;
		else
			e.preventDefault();
	}
}
function validarNumero(obj)
{
	if(isNaN(obj.value) && obj.value!='')
	{
		alert('sólo se permiten valores numéricos en este campo');
		obj.value=''; 
		obj.focus();
	}
}
function validarTeclaAlfaNumerica(e){
	if(!(e.keyCode==13 || e.keyCode==9 || e.keyCode==8 || e.keyCode==46 || e.keyCode== 35 || e.keyCode==16 || (e.keyCode>=48 && e.keyCode<=57) || (e.keyCode>=37 && e.keyCode<=40) || (e.keyCode>=65 && e.keyCode<=90)))
	{
		if(window.event)
			window.event.returnValue=false;
		else
			e.preventDefault();
	}
}
function validarAlfaNumerico(dom){
	var bln=true;
	for(var i=0; i<dom.length; i++)
		if(!((dom.charCodeAt(i)>=48 && dom.charCodeAt(i)<=57) || (dom.charCodeAt(i)>=65 && dom.charCodeAt(i)<=90) || (dom.charCodeAt(i)>=97 && dom.charCodeAt(i)<=122)))
		{
			bln=false;
			break;
		}
	return bln;
}
