/**Funciones Generales*/
function addOpcionCbo(valor,text,idCbo){
	var newOpt= document.createElement("OPTION");
	newOpt.text=text;
	newOpt.value=valor;
	document.getElementById(idCbo).options.add(newOpt);
	return true;
}
function seleccionaFila(id){
	if(old!=""){
 		document.getElementById(old).style.backgroundColor=colorOld;
 		document.getElementById(old).style.fontWeight="";
	}
	colorOld=document.getElementById(id).style.backgroundColor;
	document.getElementById(id).style.backgroundColor="#F7FFEE";
	document.getElementById(id).style.fontWeight="bold";
	old=id;
}
function asignaOpcion(obj){
  document.formulario.op.value=obj.value;
}
/*Valida los vacios de los textos*/
function validaVacio(obj,optional_focus,optional_msg){
	if (obj.value.length==0){
		if (optional_msg.length>0) alert(optional_msg);
		if (optional_focus) obj.focus();
		return false;
	}
	return true;
}

function Abrir_ventana (pagina,ancho,alto,y,x) {
	var opciones="toolbar=no, location=no, titlebar=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=" +ancho+ ", height=" +alto+", top=" +y+ " , left=" +x ;
	window.open(pagina,"",opciones);	
}



/*Valida el ingreso de números*/
function validaNumero(){
	if((event.keyCode == 47)||(event.keyCode < 46)||(event.keyCode > 57)){
	event.keyCode = 0;
	}
}
function validaNumeroEntero(){
	if((event.keyCode < 47)||(event.keyCode > 57)){
		event.keyCode = 0;
	}
}

/*Valida ingreso de fecha en formato dd-mm-yyyy*/
function validaFecha(obj){   
	var Fecha= new String(obj.value);
	var Ano= new String(Fecha.substring(Fecha.lastIndexOf("-")+1,Fecha.length));
	var Mes= new String(Fecha.substring(Fecha.indexOf("-")+1,Fecha.lastIndexOf("-")));
	var Dia= new String(Fecha.substring(0,Fecha.indexOf("-")));


	if (isNaN(Ano) || Ano.length<4 || parseInt(Ano)<1900 || Ano.length>4){
		alert('Fecha inválida');
		obj.focus();
		return false;
	}

	if (isNaN(Mes) || parseFloat(Mes)<1 || parseFloat(Mes)>12){
		alert('Fecha inválida');
		obj.focus();
		return false;
	}
	if (isNaN(Dia) || parseInt(Dia, 10)<1 || parseInt(Dia, 10)>31){
		alert('Fecha inválida');
		obj.focus();
		return false;
	}
	if(Fecha.length!=10){
		alert('Fecha inválida');
		obj.focus();
		return false;
	}
	if ((Ano%4!= 0)&&(Mes==2)&&(Dia>28)){
		alert('Fecha inválida');
		obj.focus();
		return false;
	}else if ((Ano%4 == 0)&&(Mes==2)&&(Dia>29)){
		alert('Fecha inválida');
		obj.focus();
		return false;
	}
	if ((Mes==4 || Mes==6 || Mes==9 || Mes==11)&&(Dia>30)){
		alert('Fecha inválida');
		obj.focus();
		return false;
	}
  return true;
}
function validaRango(obj1,obj2){
	//valida que fecha 1 sea menor que fecha2;
	var Fecha_In= new String (obj1.value);
	var Fecha_Ter=new String (obj2.value);
	
	var Ano1= parseInt(new String(Fecha_In.substring(Fecha_In.lastIndexOf("-")+1,Fecha_In.length)));
	var Mes1= parseInt(new String(Fecha_In.substring(Fecha_In.indexOf("-")+1,Fecha_In.lastIndexOf("-"))));
	var Dia1= parseInt(new String(Fecha_In.substring(0,Fecha_In.indexOf("-"))));
	
	var Ano2= parseInt(new String(Fecha_Ter.substring(Fecha_Ter.lastIndexOf("-")+1,Fecha_Ter.length)));
	var Mes2= parseInt(new String(Fecha_Ter.substring(Fecha_Ter.indexOf("-")+1,Fecha_Ter.lastIndexOf("-"))));
	var Dia2= parseInt(new String(Fecha_Ter.substring(0,Fecha_Ter.indexOf("-"))));
		
	if (Ano2<Ano1){
		alert('Error. El año de Termino no puede ser menor que el año de Inicio');
		obj1.focus();
		return false;
	}else if ((Mes2<Mes1) && (Ano2==Ano1)){
		alert('Error. Validar el Mes de Termino no puede ser menor que el Mes de Inicio');
		obj1.focus();
		return false;
	}else if ((Dia2<Dia1) && (Mes2==Mes1)){
		alert('Error. Validar el Día de Termino no puede ser menor que el Día de Inicio');
		obj1.focus();
		return false;
	}
	return true;
}
function ponerMayusculas(txt)
{
	txt.value=Trim(txt);
	txt.value=txt.value.toUpperCase();
}

/*Elimina los espacios del inicio y fin */
function Trim( txt ) {
	var resultStr = "";
	resultStr = TrimLeft(txt.value);
	resultStr = TrimRight(resultStr);
	return resultStr;
}
function TrimLeft( str ) {
	var resultStr = "";
	var i = len = 0;
	if (str+"" == "undefined" || str == null)
	return "";
	str += "";

	if (str.length == 0)
	resultStr = "";
	else {
	len = str.length;
	while ((i <= len) && (str.charAt(i) == " "))
	i++;
	resultStr = str.substring(i, len);
	}
	return resultStr;
}
function TrimRight( str ) {
	var resultStr = "";
	var i = 0;
	if (str+"" == "undefined" || str == null)
	return "";
	str += "";
	if (str.length == 0)
	resultStr = "";
	else {
	i = str.length - 1;
	while ((i >= 0) && (str.charAt(i) == " "))
	i--;
	resultStr = str.substring(0, i + 1);
	}
	return resultStr;
}


/* Valida ingreso de R.U.T */
function revisarDigito( 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");				
		return false;	
	}	
	return true;
}
function revisarDigito2( crut )
{	
	largo = crut.length;	
	if ( largo < 2 )	
	{		
		alert("Debe ingresar el R.U.T completo")			
		return false;	
	}	
	if ( largo > 2 )		
		rut = crut.substring(0, largo - 1);	
	else		
		rut = crut.charAt(0);	
	dv = crut.charAt(largo-1);	
	revisarDigito( 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 ( dvr != dv.toLowerCase() )	
	{		
		alert("R.U.T ingresado es incorrecto")			
		return false	
	}

	return true
}
function validaRut(texto_p)
{	
	var xtexto=texto_p.value;
	texto= texto_p.value;
	var tmpstr = "";	
	if(texto.charAt(0)==0){
		alert("Favor no ingresar '0' Cero al inicio del RUT");
		return false;
	}
	
	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;	
	if ( largo < 2 )	
	{
		alert("Favor ingresar el R.U.T completo");			
		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 dato ingresado no corresponde a un R.U.T válido");					
			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++ )	
	{		
		//alert("i=[" + i + "] j=[" + 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);	
	texto_p.value = invertido.toUpperCase()
	if ( revisarDigito2(texto) )
		return true;

	return false;
}
function asignaValorCombo(valor,cbo){
	var n=cbo.length;
	for(i=0;i<n;i++){
		if(cbo.options[i].value==valor){
			cbo.options[i].selected=true;	
		}
	}
}
function asignaValorRadio(valor,obj_radio){
	var n=obj_radio.length;
	for(i=0;i<n;i++){
		if(obj_radio[i].value==valor){
			obj_radio[i].checked=true;
		}
	}
}
function asignaValorComboUF(valor,fecha,cbo){
	var n=cbo.length;
	for(i=0;i<n;i++){
		if(cbo.options[i].text.substring(0,10)==fecha){
			cbo.options[i].selected=true;		
		}
	}
}
/*FUNCIONES PARA PROCESAR DE GENERAR REPORTES*/
function nextSize(i,incMethod,textLength){
	if (incMethod == 1) 
		return (22*Math.abs( Math.sin(i/(textLength/3.14))) );
	if (incMethod == 2) 
		return (205*Math.abs( Math.cos(i/(textLength/3.14))));
}
function sizeCycle(text,method,dis){
	output = "";
	for (i = 0; i < text.length; i++){
		size = parseInt(nextSize(i +dis,method,text.length));
		output += "<font style='font-size: "+ size +"pt'>" +text.substring(i,i+1)+ "</font>";
	}
	pleasewaitScreen.innerHTML = output;
}

function doWave(n){
	sizeCycle(theText,1,n);
	if (n > theText.length){
		n=0
	}
	setTimeout("doWave(" + (n+1) + ")", 250);
}

function asignaValortxt(valor,obj){
	obj.value=valor;
}

function validaFecha2(txt,separador){      
	var Fecha= new String(txt.value);
		var Ano= new String(Fecha.substring(Fecha.lastIndexOf(separador)+1,Fecha.length));
		var Mes= new String(Fecha.substring(Fecha.indexOf(separador)+1,Fecha.lastIndexOf(separador)));
		var Dia= new String(Fecha.substring(0,Fecha.indexOf(separador)));
		
		
		if (isNaN(Ano) || Ano.length<4 || parseInt(Ano)<1900 || Ano.length>4){
	        	alert('Fecha inválida');
			txt.focus();
			return false;
		}
	
		if (isNaN(Mes) || parseFloat(Mes)<1 || parseFloat(Mes)>12){
			alert('Fecha inválida');
			txt.focus();
			return false;
		}
		if (isNaN(Dia) || parseInt(Dia, 10)<1 || parseInt(Dia, 10)>31){
			alert('Fecha inválida');
			txt.focus();
			return false;
		}
		if(Fecha.length!=10){
			alert('Fecha inválida');
			txt.focus();
			return false;
		}
		if ((Ano%4!= 0)&&(Mes==2)&&(Dia>28)){
		alert('Fecha inválida');
		txt.focus();
		return false;
		
		}else 
		if ((Ano%4 == 0)&&(Mes==2)&&(Dia>29)){
		alert('Fecha inválida');
		txt.focus();
		return false;
		}
		if ((Mes==4 || Mes==6 || Mes==9 || Mes==11)&&(Dia>30))
		{
			alert('Fecha inválida');
			txt.focus();
			return false;
		}
			
	  return true;
}


