/* ------------------------------------------------- */
/* ----->> AVL 3 - Modulo Funciones Globales <<----- */
/*
Fecha Inicio	: 15/10/2008
@author Carolina Casanova Garcia
*/
/* ------------------------------------------------- */

var i;

//--------------------------------------------------------------------
// Función retorna cadena numérica rellena con Ceros a la izquierda 
// (copiada de misma función hecha por mí en PHP)
function Zeros(numero, largo)
{
	var lrg = largo - numero.length;
	var val = numero;
	for (var z = 0; z < lrg; z++)
	{  val = "0" + val;  }
	return val;
	
}

//--------------------------------------------------------------------
function Ahora(tipo, sep, leng, hora)
{
	// Valores recibidos :
	//  tipo	:	Formato de Fecha
	//				0 => Martes 25 de Octubre de 1998 [, 15:25:02 ]
	//				1 => 25-10-1998 [, 15:25:02 ]
	//				2 => 1998-10-25 [ 15:25:02 ]
	//	sep		:	Tipo de Separador de Fecha (tipos 1 y 2). Ejs:
	//				"-" => 25-10-1998
	//				"/" => 25/10/1998
	//	leng	:	Idioma de la Fecha (tipo 0)
	//				"es" => español
	//				"en" => inglés
	//	hora	:	Boolean determina si se incluye Hora a continuación de la Fecha
	
	var fres;
	var fch = new Date();
	
	var fYY = fch.getYear();
	var fDD = fch.getDate();
	var fDs = fch.getDay();	
	var fMM = fch.getMonth();
	
	var mes = fMM + 1;
	
	if (mes.toString.length == 1) {  mes = "0" + mes;  }
	if (tipo > 0) {  if (fDD.toString.length == 1) {  fDD = "0" + fDD;  } }
	
	if (sep == "")
	{  sep = "-";  }
	
	// Reformateo de Año :
	if (fYY < 1000)
	{  fYY += 1900;  }
	
	switch (tipo)
	{
		//--------------------
		// DD-MM-YYYY 
		case 1:
			fres = fDD + sep + mes + sep + fYY;
			break;
			
		//--------------------
		// YYYY-MM-DD
		case 2:
			fres = fYY + sep + mes + sep + fDD;
			break;
		
		//--------------------
		// Fecha Larga
		default:
		
			if (leng == "es")
			{
				var sMM = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"); 
				var sDD = new Array("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado");
				fres = sDD[fDs] + " " + fDD + " de " + sMM[fMM] + " de " + fYY;
			}
			else
			{
				var sMM = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
				var sDD = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
				fres = sDD[fDs] + ", " + fDD + " " + sMM[fMM] + " " + fYY;
			}
	}
	
	// Muestra Hora
	if (hora)
	{
		fres = fres + " " + fch.getHours() + ":" + fch.getMinutes() + ":" + fch.getSeconds();
	}
	
	return fres;
}

//--------------------------------------------------------------------
// Avanza 1 página hacia atrás en el Historial, de haberlo
function Atras()
{
  if (history.length > 0)
  {  history.back()  }
}

//--------------------------------------------------------------------
// Abre ventana Popup
function AbrirPop(ruta, destino, parametros)
{
  window.open(ruta, destino, parametros);
}

//--------------------------------------------------------------------
// Copia el Texto seleccionado en el Portapapeles
// Basado en Código © Joe Burns en http://www.htmlgoodies.com/beyond/javascript/article.php/3458851
function CopiarPP(id, sinformato)
{
  Copied = id.createTextRange();
  if (sinformato)
  {  Copied.execCommand("RemoveFormat")  }
  Copied.execCommand("Copy");
}


//--------------------------------------------------------------------
// Muestra / Oculta 1 Elemento del Documento HTML
function Mostrar(boo, id)
{
	if (boo)
	{ document.getElementById(id).style.display = ''  }
	else
	{ document.getElementById(id).style.display = 'none'  }
}


//--------------------------------------------------------------------
// Crea nuevo elemento dentro de un combo Select
function NuevoItem(ctl, val, txt, ind)
{
 ctl.options[ind] = new Option(txt, val);
}

function LimpiarCbo(ctl)
{
  var i;
  for (i = ctl.length - 1; i > -1; i--)
  { ctl.remove(i) }
}

//--------------------------------------------------------------------
// Comprueba que la cadena sea sólo numérica :
function isNumeric(cad)
{
 var i, j;
 var ch = "";
 var ok = false;

 for (i = 0; i < cad.length; i++)
 {
  ok = false;
  for (j = 48; j < 58; j++)
  { if (cad.substr(i, 1) == String.fromCharCode(j)) { ok = TRUE; break } } 
  if (!ok) { break }
 }
 
 if (!ok) 
 { return false }
 else
 { return true }
}

//------------------------------------------------------------------------
// Obtiene Dígito Verificador de un RUT en base módulo 11
function DV(rut)
{
 var dv;
 var multiplo = 2;
 var suma     = 0;
 var resto    = 0;
 var modulo;
 
 for (i = rut.length - 1; i > -1; i--)
 {
  if (multiplo == 8)
  { multiplo = 2 }
  modulo = rut.substr(i, 1);
  modulo *= multiplo;
  suma     += modulo;
  multiplo++;
 }
 
 resto = suma % 11;

 if (resto > 1)
 { dv = 11 - resto }
 else
 { 
  if (resto == 1) { dv = 'K' }
  if (resto == 0) { dv = 0 }
 }

 return dv;
}
