var globalCallbacks = {
	onCreate: function(){$('cargando').style.display = 'block';},
	onComplete : function(){
		if (Ajax.activeRequestCount == 0){
			$('cargando').style.display = 'none';
		}
	}
}
/* Se registran los callbacks en Ajax.Responders */
Ajax.Responders.register( globalCallbacks );

function enviaCampania(id, cantidad, modo)
{
	var url 	= 'envioajax.php';
	var pars 	= 'id='+id+'&cantidad='+cantidad+'&modo='+modo;
	var div		= 'resultado';
	var myAjax 	= new Ajax.Updater(
		div, 
		url, 
		{
			method: 'post',
			onFailure: reportError,
			parameters: pars
		});
}

function reportError() 
{
	alert('Ha ocurrido un error!');
}

function cambiar_color_over(celda){ 
   celda.style.backgroundColor="#EBF2FE" 
} 

function cambiar_color_out(celda){ 
   celda.style.backgroundColor="#FFFFFF" 
}

function validaIngreso()
{
	if(document.getElementById("usuario").value=="")
	{
		alert("Ingrese un nombre de usuario");
		document.getElementById("usuario").focus();
		return false;
	}
	if(document.getElementById("email").value=="")
	{
		alert("Ingrese un E-Mail");
		document.getElementById("email").focus();
		return false;
	}else{
		if(validarEmail(document.getElementById("email"))==false)
		{
			return false;
		}
	}
	if(document.getElementById("telefono").value!="")
	{
		if(esNumero(document.getElementById("telefono"))==false)
		{
			return false;
		}
	}
	if(document.getElementById("nuevapass").value=="")
	{
		alert("Ingrese una Password");
		document.getElementById("email").focus();
		return false;
	}
	if(document.getElementById("nuevapass").value!="")
	{
		if(document.getElementById("nuevapass").value!=document.getElementById("nuevapass2").value)
		{
			alert("Password no son iguales.\nIngréselas nuevamente.");
			document.getElementById("nuevapass").focus();
			return false;
		}
	}
	return true;
}

function validaActualiza()
{
	if(document.getElementById("usuario").value=="")
	{
		alert("Ingrese un nombre de usuario");
		document.getElementById("usuario").focus();
		return false;
	}
	if((document.getElementById("nuevapass").value != document.getElementById("nuevapass2").value))
	{
		alert("Password son diferentes.\nIngréselas nuevamente.");
		document.getElementById("nuevapass").focus();
		return false;
	}
	return true;
}

function validaLogin()
{
	if(document.getElementById("usuario").value=="")
	{
		alert("Debe ingresar su Usuario");
		document.getElementById("usuario").focus();
		return false;
	}
	if(document.getElementById("password").value=="")
	{
		alert("Debe ingresar su PASSWORD");
		document.getElementById("password").focus();
		return false;
	}
	return true;
}

function verLog(id)
{
	window.open("log.php?id="+id, "LOGS", "width=400, height=250, scrollbars=yes");
}

function bajarArchivo(id)
{
	window.open("bajar.php?id="+id, "BAJAR", "width=200, height=150");
}

function agregarUsuario()
{
	location.href="index.php?pag=usuario_add";
}

function editaUsuario(id)
{
	location.href="index.php?pag=usuario_edit&id="+id;
}

function eliminaUsuario(id)
{
	if(confirm("Desea eliminar este usuario?"))
	{
		location.href="index.php?pag=usuario_del&id="+id;
	}
}

function suspendeUsuario(id, tipo)
{
	if(tipo==2)
	{
		var frase = "suspender";
	}else{
		var frase = "reactivar";
	}
	if(confirm("Desea "+frase+" a este usuario?"))
	{
		location.href="index.php?pag=usuario_susp&id="+id+"&tipo="+tipo;
	}
}

function eliminaOperacion(id)
{
	if(confirm("Desea eliminar esta operacion?"))
	{
		location.href="index.php?pag=operacion_del&id="+id;
	}
}

function bloqueaOperacion(id)
{
	if(confirm("Desea bloquear esta operacion?"))
	{
		location.href="index.php?pag=operacion_bloq&id="+id;
	}
}

// INICIO VALIDA RUT
function checkCDV( 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 dígito verificador válido.");
    return false;
  }
  return true;
}

//////////////////////////////////////////////////

function checkDV( crut )
{
  var error = false; 
  largo = crut.length;
  if ( largo < 2 )
  {
	error = true;
  }
  if ( largo > 2 )
    rut = crut.substring(0, largo-1);

  dv = crut.charAt(largo-1);
  checkCDV( 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() )
  {
	error = true;
  }
  if(error) {
	return false;
  }
  else {
	return true;
  } 
}

function validaRut(camtexto)
{
  var error = false;
  texto = camtexto.value;
  if(texto == "") {
	return true;
  }
  var tmpstr = "";
  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 > 0 && largo < 2 )
  {
    error = true;
  }
 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" ) 
    {
      error = true;
    }
  }
  var invertido = "";
  for ( i=(largo-1),j=0; i>=0; i--,j++ )
    invertido = invertido + texto.charAt(i);

  var dtexto = "";
  dtexto = dtexto + invertido.charAt(0);
  if(dtexto != "") {
  	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);


  camtexto.value = invertido;  
  if(texto.length > 7 ) {
  	if ( !checkDV(texto) ) {
		error = true;
  	}
  }
  else {
	error = true;
  }

  if(error) {
	alert("El dato ingresado no es un R.U.T válido.");
	camtexto.focus();
	camtexto.select();
	return false;
  }
  else {
	return true;
  }
}

//FIN VALIDA RUT

//VALIDA E-MAIL
function validarEmail(objeto)
{
	var s = objeto.value;
	//	var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	var filter=/^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@([_a-zA-Z0-9-]+.)*[a-zA-Z0-9-]{2,200}.[a-zA-Z]{2,6}$/;
	if (s.length == 0 ) return true;
	if (filter.test(s))
		return true;
	else
		alert("Ingrese una dirección de correo válida");
		objeto.focus();
	return false;
}
//FIN VALIDA E-MAIL

//VALIDA NUMEROS
function esNumero(objeto)
{
	var numero = objeto.value;
	if(isNaN(numero))
	{
		alert("Campo debe ser numérico");
		objeto.value="";
		objeto.focus();
		return false;
	}
	return true;
}

function imprSelec(nombre)
{
  var ficha = document.getElementById(nombre);
  var ventimp = window.open(' ', 'popimpr');
  ventimp.document.write( ficha.innerHTML );
  ventimp.document.close();
  ventimp.print( );
  ventimp.close();
} 
