function radioValue(campo){
	for (var i = 0; i < campo.length; i++) {
		if (campo[i].checked==true){
			value=campo[i].value;
		}
	}
	return value;
}

/**
 * Evita Letra
 * Dont let people input letters to the field
 * @param: field to be controledd
 *
 * HOW-TO: onkeyup="this.value = evitaLetra(this)"
 */

function evitaLetra(campo) {
	texto = campo.value;
	res = ""
	for (i=0; i < texto.length ; i++ ){
		if ("0123456789".indexOf(texto.charAt(i)) > -1) {
			res = res + texto.charAt(i)
		}
	}
	return res;
}
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}


function doprint() {
  //save existing user's info
  var h = factory.printing.header;
  var f = factory.printing.footer;
  //hide the button
  document.all("printbtn").style.visibility = 'hidden';
  //set header and footer to blank
  factory.printing.header = "";
  factory.printing.footer = "";
  //print page without prompt
//  factory.DoPrint(false);
  //restore user's info
  factory.printing.header = h;
  factory.printing.footer = f;
  //show the print button
  document.all("printbtn").style.visibility = 'visible';
}


/**
 * Fun??o b?sica para submeter o formul?rio
 */

function frmSubmit(vlr) {
  if (checkData()) {
    if (vlr != undefined){
      document.frmData.opt.value = vlr;
    }
    return document.frmData.submit();
  } else {
    return false;
  }
}

function iSearch(valor, obj) {
  j = valor.length;
  if(j == 0) return;
  valor = valor.toUpperCase();
  //alert(obj.options.length)
//  alert ("aa"+obj.options.length);
  for(i = 0; i < obj.options.length; i++)
  {
//    alert(obj.options[i].text.substring(0,j).toUpperCase())
    aux = obj.options[i].text.substring(0,j).toUpperCase();
    if(aux == valor)
    {
      obj.options[i].selected = true;
      break;
    }
    else if(aux > valor)
    {
//      alert("erro");
      //break;
    }
  }
}

/**
 * Valida??es
 */

function isSelected (campo){
	res = false;
	for (i=0;i<campo.length;i++){
		if (campo[i].checked==true){
			res = true
		}
	}
	return res;
}


function isEmpty(campo){
	if (campo.value == "")
		return true
	else
		return false
}

/**
 * Valida??o de Email
 *   . precisa existir uma arroba
 *   . precisa existir um ponto depois da arroba
 */

function chkEmail(campo){
	var email = campo.value;
	if (email.indexOf("@") < 3) return false
	if (email.charAt(email.indexOf("@")-1) == ".") return false;
	if (email.lastIndexOf(".") <= email.indexOf("@")) return false;
	if (email.lastIndexOf(".")  == (email.length-1)) return false;
	return true;
}
/**
 * Valida??o de datas v?lidas
 */
function chkDate (mydata) {
	tmp = mydata.split("/");

	dia = tmp[0];
	mes = tmp[1];
	ano = tmp[2];

	// verifica o dia valido para cada mes
	if ((dia < 1)||(dia < 1 || dia > 30) && (  mes == 4 || mes == 6 || mes == 9 || mes == 11 ) || dia > 31) {
		return false;
	}
	// verifica se o mes e valido
	if (mes < 1 || mes > 12 ) {
		return false;
	}
	// verifica se e ano bissexto
	if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) {
		return false;
	}
	if (mydata == "")  {
		return false;
	}
	return true;
}
function chkHora(campo){
	hrs = (campo.value.substring(0,2));
	min = (campo.value.substring(3,5));
	situacao = "";
	if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){
		situacao = "falsa";
	}
	if (campo.value == "") {
		situacao = "falsa";
	}

	if (situacao == "falsa") {
		alert("Hora inválida!");
		campo.focus();
	}
}



/**
 * M?scara de Data
 */

function mskDate(campo){
	var mydata = campo.value;
//	mydata = mydata.replace("/", "")
	letra = mydata[mydata.length-1];

	if (mydata.length == 2){
		mydata = mydata + '/';
		campo.value = mydata;
	}
	if (mydata.length == 5){
		mydata = mydata + '/';
		campo.value = mydata;
	}
	if (mydata.length == 10){
		chkDate(mydata);
	}
}



function mskHora(campo){
	var myhora = '';
	myhora = myhora + campo.value;
	if (myhora.length == 2){
		myhora = myhora + ':';
		campo.value = myhora;
	}
	if (myhora.length == 5){
		chkHora(campo);
	}
}
/*
function mascara(valor, formato){
	var myhora = '';
	myhora = myhora + campo.value;

	if (myhora.length == 2){
		myhora = myhora + ':';
		campo.value = myhora;
	}
	if (myhora.length == 5){
		verifica_hora(campo);
	}
}
*/



function addElement(elemento, id, valor){
	newOption = document.createElement("option");
	newOption.value = id;
	newOption.text=	valor;
	elemento.appendChild(newOption);
	elemento.options.selectedIndex = elemento.options.length-1
//	window.opener.document.frmData.idGrupo.appendChild(newOption);
}

function checkAll(campo, ini, fim) {
	for (var j=ini; j < fim; j++) {
//		alert(campo+'['+j+']');
		for (var i=0; i < document.frmDel.elements.length; i++) {
			var x = document.frmDel.elements[i];
			if (x.name == campo+'['+j+']') {
				if (x.checked == true)
					x.checked = false;
				else
					x.checked = true;

			}
		}
	}
}

function hideElement(idVisible, idShow ){
//	alert("Opa");
//	document.getElementById(id).style.visibility = "visible";
	if (document.getElementById(idVisible).style.display == "none"){
		document.getElementById(idVisible).style.display = "inline";
		document.getElementById(idShow).style.display = "none";
	}else   {
		document.getElementById(idVisible).style.display = "none";
		document.getElementById(idShow).style.display = "block";
	}
}
function hideElement(idVisible){
	if (document.getElementById(idVisible).style.display == "none"){
		document.getElementById(idVisible).style.display = "inline";
	}else   {
		document.getElementById(idVisible).style.display = "none";
	}
}


/**
 * Rel?gio
 */

var clockID = 0;

function UpdateClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }

   var tDate = new Date();
   var hora;

   var hrs = tDate.getHours();
   var min = tDate.getMinutes();

   if (hrs<10){
      hrs = "0"+hrs;
   }
   hora = hrs;
   if (hrs<10){
      hrs = "0"+hrs;
   } else {
      hrs = hrs;
   }

   if (min<10){
      min = "0"+min;
   } else {
      min = min;
   }

   document.theClock.theTime.value = hora + ":" + min;

   clockID = setTimeout("UpdateClock()", 1000);
}
function StartClock() {
	if (document.theClock){
		clockID = setTimeout("UpdateClock()", 500);
	}
}

function KillClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
}

/**
 * Validacao de RUT
 */


function revisarDigito( dvr ) {
	dv = dvr + ""

	if ("0123456789kK".indexOf(dv) == -1) {
		alert("Debe ingresar un digito verificador valido: "+dv);
		return false;
	}
	return true;
}

function revisarDigito2( crut )
{
	largo = crut.length;
	if ( largo < 2 )
	{
		alert("Debe ingresar el rut 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() )	{
		error = "EL rut es incorrecto"
		return error
	}

	return true
}

function Rut(texto) {
	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 < 2 ){
		error = ("Debe ingresar el rut completo");
		return error;
	}

	for (i=0; i < largo ; i++ ){
		if ("0123456789kK".indexOf(texto.charAt(i)) == -1) {
//			alert("pos("+texto.charAt(i)+"): " + "0123456789kK".indexOf(texto.charAt(i)))
			error = "El valor ingresado no corresponde a un R.U.T(" + texto + ") valido " ;
			return error;
		}
	}

	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++ ) {
		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);
	if (revisarDigito2(texto) != true)
		return revisarDigito2(texto);
	return true;
}

function toProperCase(tmp){
	return tmp.toLowerCase().replace(/\w+/g, function(s){
          return s.charAt(0).toUpperCase() + s.substr(1);
     })
}

function writInLayer(text,id) {
	if (document.getElementById) {
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	} else if (document.all) {
		x = document.all[id];
		x.innerHTML = text;
	} else if (document.layers) {
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}

