var statusmsg=""
 
function isRut(objrut)
{
  var objrut;
  var texto = objrut.value;
  var tmpstr = "";  
 //alert("texto="+texto);
  if (texto=="") return true;
  //if (isNaN(texto.substring(0,1))) alert('Es texto');
  //return true
   
  for ( i=0; i < texto.length ; i++ )
	if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
		tmpstr = tmpstr + texto.charAt(i);
  texto = tmpstr;
   
  largo = texto.length;
  //alert("largo="+largo);
  if ( largo < 2 )
	{
    	alert("Debe ingresar el rut completo.")
    	objrut.focus();
    	objrut.select();
    	return false;
	}
  for (i=0; i < largo ; i++ )
	{ 
    	if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" && texto.charAt(i) != "K" ) 
		{
		alert("El valor ingresado no corresponde a un R.U.T valido.");
		objrut.focus();
		objrut.select();
		return false;
		}
	}

  var invertido = "";

  for ( i=(largo-1),j=0; i>=0; i--,j++ )
  	invertido = invertido + texto.charAt(i);

  var dtexto = "";

  dtexto = dtexto + invertido.charAt(0);
  dtexto = dtexto + '-';
  cnt = 0;

  for ( i=1,j=2; i<largo; i++,j++ )
	{
    	if ( cnt == 3 )
		{
		//dtexto = dtexto + '.';
		j++;
		dtexto = dtexto + invertido.charAt(i);
		cnt = 1;
		}
    	else
		{ 
		dtexto = dtexto + invertido.charAt(i);
		cnt++;
		}
	}

  invertido = "";

  for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )
	invertido = invertido + dtexto.charAt(i);

  objrut.value = invertido;
  
  if ( checkDV(objrut, texto) )
    	return true;
  return false;
}
function checkDV( objrut, crut )
{
	largo = crut.length;
	if ( largo < 2 )
		{
	  	alert("Debe ingresar el rut completo.")
	  	objrut.focus();
	  	objrut.select();
	  	return false;
	  	}
	if ( largo > 2 )
		rut = crut.substring(0, largo - 1);
	else
	  	rut = crut.charAt(0);
	dv = crut.charAt(largo-1);
	checkCDV(objrut, dv );

	if ( rut == null || dv == null )
	    return 0;
	    
	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 (dv=='K') dv='k';
	
	if ( dvr != dv.toLowerCase() )
		{
	  	alert("EL rut es incorrecto.")
	  	objrut.focus();
	  	objrut.value="";
	  	return false;
	  	}

  return true;
}
// Valida digito verificador
function checkCDV( objrut, dvr )
{
  dv = dvr + "";
  if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')
	{
    	alert("Debe ingresar un digito verificador valido.");
    	objrut.focus();
    	objrut.select();
    	return false;
    	}
  return true;
}

function isEmail(objmail) {
  var supported = 0;
  var str=objmail.value

   if (window.RegExp) {
     var tempStr = "a";
     var tempReg = new RegExp(tempStr);
     if (tempReg.test(tempStr)) supported = 1;
   }
   if (!supported) 
     return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
   var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
   var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
   if (!r1.test(str) && r2.test(str))
	 return true;
   else
   {
	 alert('Debe ingresar una dirección de e-mail válida.')
//	 objmail.focus()
	 objmail.value=""
	 return false;		
   }
}

function isNumero(objnum)
{
	if (isNaN(objnum.value))
	{
		alert('Asegúrese de que el dato ingresado sea del tipo numérico.')
		objnum.focus()
		objnum.value=""
		return false;
	}		
	else return true;
}
function validanum(obj, lowval, hival,im){
	if (obj.value != ""){
		if ((obj.value < lowval) || (obj.value > hival)){
			alert('Must contain a number between ' + lowval + ' and ' + hival)
			obj.focus()
			obj.value=""
			return false;}
	}
}
function mOvr(src,clrOver) {
if (!src.contains(event.fromElement)) {
src.style.cursor = 'hand';
src.bgColor = clrOver;
}
}
function mOut(src,clrIn) {
if (!src.contains(event.toElement)) {
src.style.cursor = 'default';
src.bgColor = clrIn;
}
}
function mClk(src) {
if(event.srcElement.tagName=='TD'){
src.children.tags('A')[0].click();
}
}
function mClk4(src,clrOver) {
if(event.srcElement.tagName=='TD'){
src.bgColor = clrOver;
src.children.tags('A')[0].click();
}
}
function mOvr4(src,clrOver2) {
if (!src.contains(event.fromElement)) {
src.style.cursor = 'hand';
//src.bgColor = clrOver;
src.style.border = clrOver2;
}
}
function mOut4(src,clrIn2) {
if (!src.contains(event.toElement)) {
src.style.cursor = 'default';
//src.bgColor = clrIn;
src.style.border = clrIn2;
}
}
function ir(url){
	opener.document.location=url;
	window.close();
}
function irPagina(url){
	window.location=url;
}
function popup(href,target,w,h) {
	window.open(href,target,'height='+ h +',width='+ w +',scrollbars=yes,resizable=yes,menubar=no,toolbar=0,status=0,location=0,directories=0,left=50,top=50');
}
function DateFormat(strfecha)
{
	if (!isDate(strfecha)) return 0;
	var patfecha = /^(\d{1,2})\/(\d{1,2})\/(\d{4})/;
	var matchArray = strfecha.match(patfecha);
	var dia = parseFloat(matchArray[1]); dia= (dia<10) ? '0'+dia : dia;
	var mes = parseFloat(matchArray[2]); mes= (mes<10) ? '0'+mes : mes;
	var ano = parseFloat(matchArray[3]); ano= (ano<1900) ? ano+1900 : ano;
	return(parseFloat(ano.toString()+mes.toString()+dia.toString()))
}
function DateCompare(fecha1,fecha2)
{
	var val1 = DateFormat(fecha1)
	var val2 = DateFormat(fecha2)
	if (val1==0 || val2==0) return(2)
	if (val1 == val2) return(0);
	if (val1 > val2) return(1);
	if (val1 < val2) return(-1);		
}
function isDate(strfecha)
{
	var patfecha = /^(\d{1,2})\/(\d{1,2})\/(\d{4})/;
	var matchArray = strfecha.match(patfecha);
	if (matchArray == null)	return false;
	var dia = parseFloat(matchArray[1]);
	var mes = parseFloat(matchArray[2]) - 1;
	var ano = parseFloat(matchArray[3]);
	var fecha = new Date(ano,mes,dia,12)	//a mediodía por cambio de horario
	var anofull=((fecha.getYear()<1900)?fecha.getYear()+1900:fecha.getYear())
	if (anofull!=ano || fecha.getMonth()!=mes || fecha.getDate()!=dia) return false;
	else return true;
}
function log_out()
{
	ht = document.getElementsByTagName("html");
	ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
	if (confirm('¿Esta seguro de que desea cerrar la sesión?'))
	{
		return true; 	
	}
	else
	{
		ht[0].style.filter = "";
		return false;
	}
}

function BorraFicha()
{
	ht = document.getElementsByTagName("html");
	ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
	if (confirm('¿Esta seguro de que desea eliminar este proyecto?'))
	{
		return true;
	}
	else
	{
		ht[0].style.filter = "";
		return false;
	}
}
function formato(tipo,numero,extra)
{
 retorno=false;
 if (tipo=='NUM')
 {
  if (arguments.length>2) if (extra>0) numero=(Math.round(numero*Math.pow(10,extra)))/Math.pow(10,extra);
                                  else numero=Math.round(numero);
  var Texto=numero.toString()+".0";
  var Entero=Texto.split('.');
  var Decimal=Entero[1];
  if (Decimal.length<extra) for(i=0;i<(extra-Decimal.length);i++) Decimal+="0";
  Entero=Entero[0];
  Texto='';
  var cont=0;
  for (var i=(Entero.length-1);i>=0;i--)
  {
   cont++;
   Texto=Entero.charAt(i)+Texto;
   if (i!=0 && (cont%3)==0) Texto='.'+Texto;
  }
  retorno=Texto;
  if (arguments.length>2 && extra>0) retorno=retorno+","+Decimal;
 }
 else if (tipo=='%') retorno=formato('NUM',numero,extra)+"%";
 return retorno;
}
function goPaso2(id2){
	window.location.href='v_paso'+id2+'.asp?EV=UPD&SAV=False';
}
function goPaso3(id3){
	window.location.href='paso'+id3+'.asp?EV=UPD&SAV=False';
}

function goSalir(){
	window.location.href='menu_ext.asp';
}
function goSalir2(){
	window.location.href='menu_ext_v.asp';
}

function LimpiaLayers(){
var i;
	for (i=0;i<=7;i++) {
		obtenerElemento('Layer'+eval(i)).style.visibility='hidden';
	} 
}

function LimpiaAreas(){
var i, targetElement;
	for (i=0;i<=3;i++) {
		targetElement = obtenerElemento("AM"+eval(i)+ "t");
		targetElement.style.display = "none";
	} 
}

function obtenerElemento( id ) {	
	if ( document.getElementById ) {
		return document.getElementById(id);
	}
	else if ( window[id] ) {
		return window[id];
	}
	return null;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  LimpiaLayers();
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function Validar(){
	var txt_Err="";
	if (document.loginForm.usuario.value=="" || document.loginForm.clave.value=="")
			txt_Err += "Debe ingresar su RUT y/o Contraseña.\n";
	if (txt_Err!=""){
		alert(txt_Err);
		return false;
	}
	else{
		return true;
		}			
}

// main function to process the fade request //
function colorFade(id,element,start,end,steps,speed) {
  var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
  var target = document.getElementById(id);
  steps = steps || 20;
  speed = speed || 20;
  clearInterval(target.timer);
  endrgb = colorConv(end);
  er = endrgb[0];
  eg = endrgb[1];
  eb = endrgb[2];
  if(!target.r) {
    startrgb = colorConv(start);
    r = startrgb[0];
    g = startrgb[1];
    b = startrgb[2];
    target.r = r;
    target.g = g;
    target.b = b;
  }
  rint = Math.round(Math.abs(target.r-er)/steps);
  gint = Math.round(Math.abs(target.g-eg)/steps);
  bint = Math.round(Math.abs(target.b-eb)/steps);
  if(rint == 0) { rint = 1 }
  if(gint == 0) { gint = 1 }
  if(bint == 0) { bint = 1 }
  target.step = 1;
  target.timer = setInterval( function() { animateColor(id,element,steps,er,eg,eb,rint,gint,bint) }, speed);
}

// incrementally close the gap between the two colors //
function animateColor(id,element,steps,er,eg,eb,rint,gint,bint) {
  var target = document.getElementById(id);
  var color;
  if(target.step <= steps) {
    var r = target.r;
    var g = target.g;
    var b = target.b;
    if(r >= er) {
      r = r - rint;
    } else {
      r = parseInt(r) + parseInt(rint);
    }
    if(g >= eg) {
      g = g - gint;
    } else {
      g = parseInt(g) + parseInt(gint);
    }
    if(b >= eb) {
      b = b - bint;
    } else {
      b = parseInt(b) + parseInt(bint);
    }
    color = 'rgb(' + r + ',' + g + ',' + b + ')';
    if(element == 'background') {
      target.style.backgroundColor = color;
    } else if(element == 'border') {
      target.style.borderColor = color;
    } else {
      target.style.color = color;
    }
    target.r = r;
    target.g = g;
    target.b = b;
    target.step = target.step + 1;
  } else {
    clearInterval(target.timer);
    color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
    if(element == 'background') {
      target.style.backgroundColor = color;
    } else if(element == 'border') {
      target.style.borderColor = color;
    } else {
      target.style.color = color;
    }
  }
}

// convert the color to rgb from hex //
function colorConv(color) {
  var rgb = [parseInt(color.substring(0,2),16), 
    parseInt(color.substring(2,4),16), 
    parseInt(color.substring(4,6),16)];
  return rgb;
}


/* inicio ordernamiento*/
var TINY={};
function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}
TINY.table=function(){
	function sorter(n){this.n=n; this.pagesize=20; this.paginate=0}
	sorter.prototype.init=function(e,f){
		var t=ge(e), i=0; this.e=e; this.l=t.r.length; t.a=[];
		t.h=T$$('thead',T$(e))[0].rows[0]; t.w=t.h.cells.length;
		for(i;i<t.w;i++){
			var c=t.h.cells[i];
			if(c.className!='nosort'){
				c.className=this.head; c.onclick=new Function(this.n+'.wk(this.cellIndex)')
			}
		}
		for(i=0;i<this.l;i++){t.a[i]={}}
		if(f!=null){var a=new Function(this.n+'.wk('+f+')'); a()}
		if(this.paginate){this.g=1; this.pages()}
	};
	sorter.prototype.wk=function(y){
		var t=ge(this.e), x=t.h.cells[y], i=0;
		for(i;i<this.l;i++){
      t.a[i].o=i; var v=t.r[i].cells[y]; t.r[i].style.display='';
      while(v.hasChildNodes()){v=v.firstChild}
      t.a[i].v=v.nodeValue?v.nodeValue:''
    }
		for(i=0;i<t.w;i++){var c=t.h.cells[i]; if(c.className!='nosort'){c.className=this.head}}
		if(t.p==y){t.a.reverse(); x.className=t.d?this.asc:this.desc; t.d=t.d?0:1}
		else{t.p=y; t.a.sort(cp); t.d=0; x.className=this.asc}
		var n=document.createElement('tbody');
		for(i=0;i<this.l;i++){
			var r=t.r[t.a[i].o].cloneNode(true); n.appendChild(r);
			r.className=i%2==0?this.even:this.odd; var cells=T$$('td',r);
			for(var z=0;z<t.w;z++){cells[z].className=y==z?i%2==0?this.evensel:this.oddsel:''}
		}
		t.replaceChild(n,t.b); if(this.paginate){this.size(this.pagesize)}
	};
	sorter.prototype.page=function(s){
		var t=ge(this.e), i=0, l=s+parseInt(this.pagesize);
		if(this.currentid&&this.limitid){T$(this.currentid).innerHTML=this.g}
		for(i;i<this.l;i++){t.r[i].style.display=i>=s&&i<l?'':'none'}
	};
	sorter.prototype.move=function(d,m){
		var s=d==1?(m?this.d:this.g+1):(m?1:this.g-1);
		if(s<=this.d&&s>0){this.g=s; this.page((s-1)*this.pagesize)}
	};
	sorter.prototype.size=function(s){
		this.pagesize=s; this.g=1; this.pages(); this.page(0);
		if(this.currentid&&this.limitid){T$(this.limitid).innerHTML=this.d}
	};
	sorter.prototype.pages=function(){this.d=Math.ceil(this.l/this.pagesize)};
	function ge(e){var t=T$(e); t.b=T$$('tbody',t)[0]; t.r=t.b.rows; return t};
	function cp(f,c){
		var g,h; f=g=f.v.toLowerCase(), c=h=c.v.toLowerCase();
		var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,''));
		if(!isNaN(i)&&!isNaN(n)){g=i,h=n}
		i=Date.parse(f); n=Date.parse(c);
		if(!isNaN(i)&&!isNaN(n)){g=i; h=n}
		return g>h?1:(g<h?-1:0)
	};
	return{sorter:sorter}
}();
/* fin procedimiento de ordenamiento*/
/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}


/**
 * SUBMODAL v1.6
 * Used for displaying DHTML only popups instead of using buggy modal windows.
 *
 * By Subimage LLC
 * http://www.subimage.com
 *
 * Contributions by:
 * 	Eric Angel - tab index code
 * 	Scott - hiding/showing selects for IE users
 *	Todd Huss - inserting modal dynamically and anchor classes
 *
 * Up to date code can be found at http://submodal.googlecode.com
 */

// Popup code
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gReturnFunc;
var gPopupIsShown = false;
var gDefaultPage = "loading.html";
var gHideSelects = false;
var gReturnVal = null;

var gTabIndexes = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

// If using Mozilla or Firefox, use Tab-key trap.
if (!document.all) {
	document.onkeypress = keyDownHandler;
}


/* funciones de procedimiento submodal */
function  funValidaCampos()
{
    with (document.forms[0])
   {
       Usuarios =  txtusuario.value;
       	if (Usuarios == "")
       		{
      	  	alert("Debe ingresar Rut");
      	  	txtusuario.focus();
          	return;
       		}
       		Password =  txtclave.value;
       		if (Password == ""){
      	    	alert("Debe ingresar Contraseña");
     	    	txtclave.focus();
            	return;
            	}
  			else{
        	  document.forms[0].action = "urg_va.php";
		     document.forms[0].submit();
        	}  
   }
}


