// JavaScript Document
//**********************************************
//*************Validacion Rut ******************
//**********************************************
function validarut(rut,objeto){
	//Limpia el string
	var tmpstr = "";
	var num ="";
	largo = rut.length;
	if(largo < 2){
		alert("EL rut es incorrecto.");		
		objeto.value = "";
		return false;
		objeto.focus();
	}
	for (i=0; i < largo ; i++ ){
		if ( rut.charAt(i) != "0" && rut.charAt(i) != "1" && rut.charAt(i) !="2" && rut.charAt(i) != "3" && rut.charAt(i) != "4" && rut.charAt(i) !="5" && rut.charAt(i) != "6" && rut.charAt(i) != "7" && rut.charAt(i) !="8" && rut.charAt(i) != "9" && rut.charAt(i) !="k" && rut.charAt(i) !="K" && rut.charAt(i) !="x" && rut.charAt(i) !="X" ){
		}else{
			tmpstr = tmpstr + rut.charAt(i);
		}
	}
	//Separa el digito verificador del numero	
	largo = tmpstr.length;
	dv = tmpstr.charAt(largo-1);
	dv = dv.toUpperCase();
	num = tmpstr.substring(0, largo - 1);
	if(dv!='X'){		
		//Realiza validacion
		var dvr = '0';
		suma = 0;
		mul = 2;
		for (i= num.length-1 ; i >= 0; i--){
			suma = suma + num.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 + "";
		}	
		//Compara el digito verificador con el digito verificador producto del calculo
		if ( dvr != dv ){
			alert("EL rut es incorrecto.");		
			objeto.value = "";
			return false;
			objeto.focus();
		}
	}
	//Formatea el rut
	var c = 0;
	var numf ='';
	for (i= num.length-1 ; i >= 0; i--){		
		numf = numf +  num.charAt(i);
		c = c + 1
		if (c == 3){
			c = 0; 
			numf = numf + '.';
		}
	}
	num =  numf;
	numf = '';
	for (i= num.length-1 ; i >= 0; i--){		
		numf = numf +  num.charAt(i);
	}
	objeto.value = numf + "-" + dv;
	return true;
}
function validarangofechas(fecha1,fecha2){
	//El script debe recivir las fechas en formato dd/mm/yyyy
	//Dentro de este debe transforma a mm/dd/yyyy
	//Retorna 1 cuando las fechas son iguales
	//Retorna 2 cuando fecha1 es mayor que fecha2
	//Retorna 3 cuando fecha1 es menor que fecha2
	vfecha1=fecha1.split("/");
	vfecha2=fecha2.split("/");
	nfecha1=vfecha1[1] + '/' + vfecha1[0] + '/' + vfecha1[2]; 
	nfecha2=vfecha2[1] + '/' + vfecha2[0] + '/' + vfecha2[2]; 
	f1=new Date(nfecha1); 
	f2=new Date(nfecha2); 
	if(fecha1==fecha2){
		return 1;
	}else if(f1>f2){
		return 2;
	}else if(f1<f2){
		return 3;
	}		
}
//Para hacer el llamado a esta funcion agregar al <head> En el link agragar la siguente linea
//"javascript:NewWindow('NombreArchivo','acepopup','520','350','center','front');"
var win=null;
function NewWindow(mypage,myname,w,h,pos,infocus,sbars){
	if(pos=="random"){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!='center' && pos!="random") || pos==null){myleft=0;mytop=20}
	settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars="+ sbars +",location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";
	win=window.open(mypage,myname,settings);
}
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//***********************************
//Mueve un valor de un listado a otro
//***********************************
function muevevalorlista(emisor, receptor){
	// Accedemos a los 2 selects
	emisor = document.getElementById(emisor);
	receptor = document.getElementById(receptor);
	// Obtenemos algunos datos necesarios
	posicion = receptor.options.length;
	selecionado = emisor.selectedIndex;
	if(selecionado != -1) {
		volcado = emisor.options[selecionado];
		// Volcamos la opcion al select receptor y lo eliminamos del emisor
		receptor.options[posicion] = new Option(volcado.text, volcado.value);
		emisor.options[selecionado] = null;
		emisor.selectedIndex=selecionado;
		if(selecionado>emisor.length-1){
			emisor.selectedIndex=emisor.length-1;
		}
	}
}
//***********************************
//Elimina un valor de un listado 
//***********************************
function eliminadellistado(listado){
	if (document.getElementById(listado).selectedIndex >= 0) {
		selecionado=document.getElementById(listado).selectedIndex;
		document.getElementById(listado).options[document.getElementById(listado).selectedIndex]=null;
		document.getElementById(listado).selectedIndex=selecionado;
		if(selecionado>document.getElementById(listado).length-1){
			document.getElementById(listado).selectedIndex=document.getElementById(listado).length-1;
		}		
	}
}
//****************************************
//Mueve un listado completo a otro listado
//****************************************
function muevelista(n, from, to) {
	if (document.layers) {
		fromObj = document.layers[from];
		to = document.layers[to];
	} else if (document.all) {
		fromObj = document.all(from);
		to = document.all(to);
	}
	if (n == 1 || n == 2) {
		var indTo = to.length-1;
		for (i=fromObj.length-1; i>=0; i--) {
			if (n==1 || fromObj.options[i].selected) {
				indTo++;
				to.options[indTo] = new Option(fromObj.options[i].text, fromObj.options[i].value);
				fromObj.options[i] = null;
			}
		}
	} else if (n == 3 || n == 4) {
		var indFrom = fromObj.length-1;
		for (i=to.length-1; i>=0; i--) {
			if (n==4 || to.options[i].selected) {
				indFrom++;
				fromObj.options[indFrom] = new Option(to.options[i].text, to.options[i].value);
				to.options[i] = null;
			}
		}
	}	
}
//***********************************
//Ordena los valores de un listado
//***********************************
function ordenalista(box){
	var temp_opts = new Array();
	var temp = new Object();
	for(var i=0; i<document.getElementById(box).options.length; i++){
		temp_opts[i] = document.getElementById(box).options[i];
	}
	for(var x=0; x<temp_opts.length-1; x++){
		for(var y=(x+1); y<temp_opts.length; y++){
			if(temp_opts[x].text > temp_opts[y].text){
				temp = temp_opts[x].text;
				temp_opts[x].text = temp_opts[y].text;
				temp_opts[y].text = temp;
			}
		}
	}
	for(var i=0; i<document.getElementById(box).options.length; i++){
		document.getElementById(box).options[i].value = temp_opts[i].value;
		document.getElementById(box).options[i].text = temp_opts[i].text;
	}
}
function seleccionatodoslista(lista,estado){
	for(var i=0;i<document.getElementById(lista).length;i++)
	document.getElementById(lista).options[i].selected=estado;
}
function formatofecha(objeto,valor,e){    
    var evt = e ? e : event;
    var key = window.Event ? evt.which : evt.keyCode;	
	if(key!=8 && key!=46 && key!=0){
		if(valor.length==2){
			if(isNaN(valor)){
				alert("Debe ingresar un valor");
			}else{
				if(valor>31){
					alert("El número de día no es valido");
				}else{
					objeto.value=valor + "/";
				}
			}
		}else if(valor.length==5){
			mes=valor.substring(valor.length-2)
			if(isNaN(mes)){
				alert("Debe ingresar un valor");
			}else{
				if(mes>12){
					alert("El número de mes no es valido");
				}else{
					objeto.value=valor + "/";
				}			
			}
		}else if(valor.length==10){
			anio=valor.substring(valor.length-4)
			if(isNaN(anio)){
				alert("Debe ingresar un valor");
			}		
		}
	}
}
//-->
