var seleccionados = new Array('');
function reg_ciu(val){
	if(val==42){
		document.form.regpkcodigo.disabled=false;
		document.form.ciupkcodigo.disabled=false;
	}else{
		document.form.regpkcodigo.disabled=true;
		document.form.ciupkcodigo.disabled=true;
	}
}
function solo_texto() 
{
	if ((event.keyCode < 39 || event.keyCode > 122 || event.keyCode == 63 || event.keyCode == 64 || event.keyCode == 91 || event.keyCode == 92 || event.keyCode == 93 || event.keyCode == 94 || event.keyCode == 96) && event.keyCode != 32)
		event.returnValue=false;
}
function solo_numeros() 
{
	if ((event.keyCode < 48)||(event.keyCode > 57))
		event.returnValue=false;
}
function validacion_root(rute)
{
	var rut=rute.value;
	if(rut!=""){
		p=rut.split("-");
		dv=p[1];
		rut=p[0];
		var x = 2, acum=0;
		var i, j=rut.length -1;
		for(i=j;i>=0;i--)
		{
			if(rut.charAt(i)!="."){
				acum = acum + (parseInt(rut.charAt(i)) * x);
				if(x == 7)
					x=2;
				else
					x++;
			}
		}
		var res = acum % 11;
		if ( res == 1)
			dv2 = 'K';
		else if (res == 0)
			dv2 = '0';
		else{
			var dv2 = 11 - res;
			dv2 = dv2 + "";
			}
		if(dv.toUpperCase() == dv2 && rute.value != '0-0' && rute.value != '00.000.000-0')
			return true;
		else
			alert('Error: Rut Mal Ingresado');
		rute.value='';
		rute.focus();
		rute.select() 
	}
}
function completa_root(rut)
{
	var i, Temp="", x=0, rut2="";
	for(i = rut.length-1; i >=0; i--){
		if(rut.charAt(i)!='.' && rut.charAt(i)!='-')
			rut2=rut.charAt(i)+rut2;		
	}
	rut=rut2;
	for(i = rut.length-1; i >=0; i--){
		Temp=rut.charAt(i)+Temp;
		if(x == 0)
			Temp="-"+Temp;
		if((x % 3)==0 && x != 0)
			Temp="."+Temp;
		x++;
	}
	return Temp;
		
}

String.prototype.trim = function() {
	var i=this.length-1;
	var text="";
	while(this.charCodeAt(i)==160 || this.charAt(i)==" "){
		i--; 
	}
	while(i>=0){
		text = this.charAt(i)+text;
		i--; 
	}
	return text;
}
String.prototype.ltrim = function() {
return this.replace(/^\s+/g,"");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/g,"");
} 
function IsNumeric(valor) 
{ 
	var log=valor.length; var sw="S"; 
	for (x=0; x<log; x++) 
	{ 
		v1=valor.substr(x,1); 
		v2 = parseInt(v1); 
		//Compruebo si es un valor numérico 
		if (isNaN(v2)) { sw= "N";} 
	} 
	if (sw=="S") {return true;} else {return false; } 
} 

var primerslap=false; 
var segundoslap=false; 
function completa_fecha(fecha)
{
	var long = fecha.length; 
	var dia; 
	var mes; 
	var ano; 

	if ((long>=2) && (primerslap==false)) { 
		dia=fecha.substr(0,2); 
		if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { 
			fecha=fecha.substr(0,2)+"-"+fecha.substr(3,7); 
			primerslap=true; 
		}else { 	
			alert('Error: Ingrese un dia valido');
			fecha=""; 
			primerslap=false;
		} 
	} 
	else 
	{ 
		dia=fecha.substr(0,1); 
		if (IsNumeric(dia)==false) 
		{								
			alert('Error: Ingrese un dia valido');
			fecha="";
		} 
		if ((long<=2) && (primerslap=true)) {
			fecha=fecha.substr(0,1); 
			primerslap=false; 
		} 
	} 
	if ((long>=5) && (segundoslap==false)) 
	{ 
		mes=fecha.substr(3,2); 
		if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { 
			fecha=fecha.substr(0,5)+"-"+fecha.substr(6,4); 
			segundoslap=true; 
		} else { 		
			alert('Error: Ingrese un mes valido');
			fecha=fecha.substr(0,3);
			segundoslap=false;
		} 
	} else { 
		if ((long<=5) && (segundoslap=true)) { 
			fecha=fecha.substr(0,4); segundoslap=false; 
		} 
	} 
	if (long>=7) 
	{ 
		ano=fecha.substr(6,4); 
		if (IsNumeric(ano)==false) { 
			fecha=fecha.substr(0,6); 
		} 
		else { 
			if (long==10){ 
				if ((ano==0) || (ano<1900) || (ano>2100)) { 
					alert('Error: Ingrese un año valido');
					fecha=fecha.substr(0,6); 
				} 
			} 
		} 
	} 
	if (long>=10) 
	{ 
		fecha=fecha.substr(0,10); 
		dia=fecha.substr(0,2); 
		mes=fecha.substr(3,2); 
		ano=fecha.substr(6,4); 
		// Año no viciesto y es febrero y el dia es mayor a 28 
		if ((ano%4 != 0) && (mes ==02) && (dia > 28) ) { 
			alert('Error: Fecha mal ingresada');
			fecha=fecha.substr(0,2)+"-"; 
		} 
	} 
	return (fecha); 
}


function fecha_null(fecha){
	if(fecha.value.length < 10){
		fecha.value = '';
	}
}

function cambia_div(uno, dos){
	uno.style.display='none';
	dos.style.display='block';	
}
function popupPage(page,height,width) {
	var windowprops = "height="+height+",width="+width+",location=no,scrollbars=no,menubars=no,toolbars=no,resizable=no";
	window.open(page, "pop_up", windowprops);
}

function valida_seleccion(cam){
	var temp = new Array();
	if(cam.checked==true){
		for(i = 0 ; i < seleccionados.length;i++){
			if(cam.value == seleccionados[i]){
				cam.checked=false;
				return false;
			}
		}
		i++;
		seleccionados[i] = cam.value;
	}else{
		temp = seleccionados;
		j=0;
		for(i = 0 ; i < seleccionados.length;i++){			
			seleccionados[i] = ""
			if(temp[j] != cam.value){
				seleccionados[i] = temp[j]
				j++;
			}
		}
		
	}
}

function seleccion_meses(valor){
	var arr;
	arr = valor.split("-");
	document.form.arrmes.value = arr[1];
	document.form.arrvalor.value = arr[0];
}