var numbers="0123456789";
var dialogo1=null;
var tiempoLista=null;

function encodeEscape(v)
{
return escape(v).replace(/\+/ig, '%2B').replace(/\//ig, '%2F');
}


function isNumeric(x)
{
	// is x a String or a character?
	if(x.length>1)
	{
		// remove negative sign
		x=Math.abs(x)+"";
		for(j=0;j<x.length;j++)
		{
			// call isNumeric recursively for each character
			number=isNumeric(x.substring(j,j+1));
			if(!number) return number;
		}	
		return number;
	}
	else
	{
		// if x is number return true
		if(numbers.indexOf(x)>=0) return true;
		return false;
	}
}
	
function isDate(x)
{
	if (x.length==0) return true;
	
	i=0;
	j=i;
	while((x.substring(i,i+1)!="/" && x.substring(i,i+1)!="-") && (i+1)<x.length)
		i++;
	a=x.substring(j,i);
	if (!(isNumeric(a)==true && a>1900 && a<3000)) return false;
	i++;
	j=i;
	while((x.substring(i,i+1)!="/" && x.substring(i,i+1)!="-") && i<x.length)
		i++;
	m=x.substring(j,i);
	if (!(isNumeric(m)==true && m>0 && m<13)) return false;
	i++;
	j=i;
	while(i<x.length)
		i++;
	d=x.substring(j,i);
	if (!(isNumeric(d)==true && d>0 && d<32)) return false;

	if ((m==4 || m==6 || m==9 || m==11) && d==31) return false;

	if (m == 2)
	{
		var isleap = (a % 4 == 0 && (a % 100 != 0 || a % 400 == 0));
		if (d > 29 || (d==29 && !isleap)) return false;
	}
	
	return true;
}

function compareDate(a,b)

{
	var aa,am,ad,ba,bm,bd,va,vb;
	if (isDate(a)==false || isDate(b)==false || a=="" || b=="") return 0;
	i=0;
	j=0;
	
	while (a.substring(i,i+1)!="/" && a.substring(i,i+1)!="-")
		i++;
	ad=(a.substring(j,i)-0);
	i++;
	j=i;
	
	while (a.substring(i,i+1)!="/" && a.substring(i,i+1)!="-")
		i++;
	am=(a.substring(j,i)-0);
	i++;
	j=i;
	while (a.length>i)
		i++;
	aa=(a.substring(j,i)-0);

	i=0;
	j=0;
	while (b.substring(i,i+1)!="/" && b.substring(i,i+1)!="-")
		i++;
	bd=(b.substring(j,i)-0);
	i++;
	j=i;
	
	while (b.substring(i,i+1)!="/" && b.substring(i,i+1)!="-")
		i++;
	bm=(b.substring(j,i)-0);
	i++;
	j=i;
	while (b.length>i)
		i++;
	ba=(b.substring(j,i)-0);

	va=(ad-0)*10000+(am-0)*100+(aa-0);
	vb=(bd-0)*10000+(bm-0)*100+(ba-0);

	if (va>vb) return 1;
	if (va<vb) return -1;
	if (va=vb) return 0;
}

function isTime(a)
{
	var i,x,h,m;
	
	if (a.length==0) return -1;
	if (reemplazaString(a,":","").length!=(a.length-1)) return -1;
	i=0;
	while (a.substring(i,i+1)!=":")
		i++;
	if (i==(a.length-1) || i==0) return -1;
	
	h=a.substring(0,i);
	m=a.substring(i+1,a.length);
	
	if (isNumeric(h)==false || isNumeric(m)==false) return -1;
	if ((h-0)<0 || (h-0)>23 || (m-0)<0 || (m-0)>59) return -1;
	x=(h-0)*100 + (m-0);
	return x;
}

function reemplazaString(x,y,z)
{
	if (y=="") return x;
	var i=0;
	
	for(i=0;i<x.length;i++)
	{
		if (i==(x.length-y.lentgh))
		{
			if (x.substring(i,i+y.length)==y)
			{
				x=x.substring(0,i) + z;
				i--;
			}
		}
		else
			if (x.substring(i,i+y.length)==y)
			{
				x=x.substring(0,i) + z + x.substring(i+y.length,x.length);
				i--;
			}
	}
	
	return x;
}

function rutFormateado(x)
{
	var letra;
	var y=x;
	letra = '';
	varrut = '';
	dv = '';
	if (x != "")
	{
		x = reemplazaString(x ,".","");
		x = reemplazaString(x ,"-","");
		x = reemplazaString(x ,"+","");
		x = reemplazaString(x ," ","");
		j=1;
		dv = x.charAt(x.length - 1);
		for (i=x.length-2; i >= 0;i--)
		{
			letra = x.charAt(i)
			if (j == 3)
			{
				letra = '.' + letra;
				j = 1;
			}
			else
			{
				j = j + 1;
			}
			varrut = letra + varrut;
		}
	
		x = varrut + '-' + dv;
		x=reemplazaString(x,"k","K");
		if (x.substring(0,1)==".") x=x.substring(1,x.length);
	}
	return x;
}

function validaRutFormateado(x)
{
	formato="23456723";
	
	var sum=0;
	
	if (x.length>10) return false;
	
	for(i=0;i<(x.length-2);i++)
	{
		sum= sum + (x.charAt(x.length-3-i)*1)*(formato.charAt(i)*1);
	}
	
	if (sum==0) return false;
	
	resto=sum%11;
	if (resto==1 && (x.substring(x.length-1,x.length)=="k" || x.substring(x.length-1,x.length)=="K")) return true;
	if (resto==0 && x.substring(x.length-1,x.length)=="0") return true;
	if ((11-resto)==((x.substring(x.length-1,x.length))*1)) return true;
	
	return false;
}

function isRut(x)
{
	if (x != "")
	{
		x=rutFormateado(x);
		if (!validaRutFormateado(reemplazaString(x,".",""))) return false;
	}
	return true;
}

function transMoneda(texto)
{
	texto=texto + "";
	if (texto=="") return "";
	
	texto=eliminaMoneda(texto);
	if (texto.substring(0,1)=="-") signo="-"; else signo="";
	texto=reemplazaString(texto,"-","");

	largo=texto.length-1;
	resto=largo%3;

	lugares=(largo - resto) / 3;
//		alert("LUGARES=" + lugares);
	moneda="";
//		alert("MONEDA=" + moneda + "\nTEXTO=" + texto);
	for(i=0;i<lugares;i++)
	{
		moneda="." + texto.substring(largo-2,largo+1) + moneda;
		texto=texto.substring(0,largo-2);
		largo=texto.length-1;
//			alert("MONEDA=" + moneda + "\nTEXTO=" + texto);
	}
	moneda= signo + texto + moneda;
//		alert("MONEDA=" + moneda);
	return moneda;
}

function transDecimal(texto, decimal, miles)
{
	texto=texto + "";
	if (texto=="") return "";
	var arreglo=texto.split(decimal);
	texto=transMoneda(arreglo[0]);
	texto=texto.replace(/\./g,miles);
	texto=texto.replace(/,/g,miles);
	if (arreglo[1]) texto+=decimal+arreglo[1];

return texto;
}

function eliminaMoneda(texto)
{
	texto=texto + "";
	texto=reemplazaString(texto,".","");
	return texto;
}

function isEmail(texto)
{
	if (texto=="") return true;
	
	if (texto.length>reemplazaString(texto," ","").length) return false;
	
	valor1=reemplazaString(texto,"@","");
	valor2=reemplazaString(valor1,".","");
	if (valor2.length==0) return false;
	
	if ((texto.length -1)==valor1.length)
		if (texto.length>valor1.length) return true;
	
	return false;
}

function cola()
{
	dialogo1=window.open("cola_generacion.asp", "Ventana","status=no, directories=no, toolbar=no, left=50, top=50, height=500, width=620, scrollbars=yes");
	dialogo1.focus();
}

function isUrl(texto)
{
		if (texto!="")
		{
			j=0;
			if (texto.length<8)
				return false;
			if (texto.substring(0,7)!="http://")
				return false;
			for(i=7;i<texto.length;i++)
				if (texto.substring(i,i+1)=="." && j==0) j=1;
			if (j<1)
				return false;
		}
		
		return true;
}

function isReal(x)
{
	var a;
	if (x=="") return true;
	
	a=reemplazaString(x,".","");
	
	if (a.length<(x.length-1)) return false;
	if (isNumeric(a)==false) return false;
	
	return true;
}

function busquedaLista(texto, lista)
{
	if (tiempoLista==null) tiempoLista=Date();
}

function s_numero(e)
{
    var tecla =(document.all)?e.keyCode: e.which;
	if (tecla==0 || tecla==8) return true;
	if ((tecla<45)||(tecla>57)) return false;
	return true;
}

function es_entero(e)
{
    var tecla =(document.all)?e.keyCode: e.which;
	if (tecla==0 || tecla==8) return true;
	if ((tecla<45)||(tecla>57)) return false;
	return true;
}

function sin_enter(e)
{
    var tecla =(document.all)?e.keyCode: e.which;
	if (tecla==10 || tecla==13)
		return false;
	else
		return true;
}

function mover(e, formulario, patron, actual_fila, actual_col, max_fila, max_col){
    var tecla =(document.all)?e.keyCode: e.which;
	var sw=false;
	
	if (tecla==38) {actual_fila--;sw=true;}
	if (tecla==40 || tecla==13 || tecla==10) {actual_fila++;sw=true;}
	if (tecla==37 && max_col>1) {actual_col--;sw=true;}
	if ((tecla==39 || tecla==9) && max_col>1) {actual_col++;sw=true;}
	
	if (sw==true){
		if (actual_fila<1 && actual_col>1) {actual_fila=max_fila;actual_col--;}
		if (actual_fila>max_fila && actual_col<max_col) {actual_fila=1;actual_col++;}
		if (actual_col<1 && actual_fila>1) {actual_col=max_col;actual_fila--;}
		if (actual_col>max_col && actual_fila<max_fila) {actual_col=1;actual_fila++;}
		
		if (actual_fila<1) actual_fila=max_fila;
		if (actual_fila>max_fila) actual_fila=1;
		if (actual_col<1) actual_col=max_col;
		if (actual_col>max_col) actual_col=1;
		
		if (max_col==1 && max_fila>1){
			if (formulario.elements[patron + actual_fila]){
				formulario.elements[patron + actual_fila].focus();
				formulario.elements[patron + actual_fila].select();
			}
		}
		if (max_col>1 && max_fila==1){
			if (formulario.elements[patron + actual_col]){
				formulario.elements[patron + actual_col].focus();
				formulario.elements[patron + actual_col].select();
			}
		}
		if (max_col>1 && max_fila>1){
			if (formulario.elements[patron + actual_fila + "-" + actual_col]){
				formulario.elements[patron + actual_fila + "-" + actual_col].focus();
				formulario.elements[patron + actual_fila + "-" + actual_col].select();
			}
		}
		return false;
	}
	return true;
}

function destacar(tipo, origen, id, txtId){
	if (tipo==1){
		if (origen==1) id.style.backgroundColor="#ffffcc";
		if (origen==2) document.getElementById(txtId).style.backgroundColor="#ffffcc";
	}
	else{
		if (origen==1) id.style.backgroundColor="";
		if (origen==2) document.getElementById(txtId).style.backgroundColor="";
	}
}

function disableBody(){
		var elemento=document.getElementById("divContenidoMain");
		elemento.style.opacity="0.3";
		elemento.style.filter="alpha(opacity=30)";
		elemento.style.display="none";
}

function bloqueaTextArea(letras,maximo, evento){
    var tecla =(document.all)?evento.keyCode: evento.which;
	if (tecla==8 || tecla==46) return true;
	if (letras.length>=maximo) return false;
	return true;
}

function verificaTamanoTextArea(letras,divTam, total){
	if (letras.length>total) return false;
	document.getElementById(divTam).innerHTML=letras.length + " de " + total + " caracteres.";
	return true;
}

function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return {w:myWidth,h:myHeight};
}

var TimeToFade = 500.0;

function fade(eid){

  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '0.8')
    {
      element.FadeState = -2;
    }
    else
    {
      element.FadeState = 2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1){
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    element.style.display = element.FadeState == 2
        ? 'none' : 'block';
    setTimeout("animateFade(" + new Date().getTime()
        + ",'" + eid + "')", 33);
  }
}

function animateFade(lastTick, eid)
{
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.display = element.FadeState == 1
        ? 'block' : 'none';
    element.style.opacity = element.FadeState == 1
        ? '.85' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '85' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  newOpVal = newOpVal*0.85;
  element.style.opacity = newOpVal;
  element.style.filter =
      'alpha(opacity = ' + (newOpVal*100) + ')';
 
  setTimeout("animateFade(" + curTick
      + ",'" + eid + "')", 33);
}


