//String.prototype.superTrim = superTrim;
//Implementa o m?todo superTrim() no objeto String.
function superTrim(s){
  var re1 = /^\s+|\s+$/gi;
  var re2 = /\s{2,}/gi;
  return( s.replace(re1, "" ).replace( re2, " " ).toUpperCase());
}

String.prototype.trim = function() {
	// skip leading and trailing whitespace
	// and return everything in between
	var x=this;
	x=x.replace(/^\s*(.*)/, "$1");
	x=x.replace(/(.*?)\s*$/, "$1");
	return x;
}

//Abre uma nova janela
function openWin(theURL, winName, h, w, features) { //v2.0
  if (h == "*"){ h = (screen.availHeight - 80) };
  if (w == "*"){ w = (screen.availWidth - 30) };
  var newFeatures = "height=" + h +", innerHeight=" + h;
  newFeatures += ",width=" + w + ",innerWidth=" + w;
  if (window.screen) {
  	var ah = (screen.availHeight - 30);
  	var aw = (screen.availWidth - 10);
  	var xc = (( aw - w) / 2);
  	var yc = (( ah - h) / 2);
  	newFeatures += ",left=" + xc + ",screenX=" + xc;
  	newFeatures += ",top=" + yc + ",screenY=" + yc;
  	newFeatures += "," + features + ",resizable=yes";
  } 
  var newWin = window.open(theURL,winName,newFeatures);
  newWin.focus();
}

function clearSelect(umSelect) {
	for (var i = umSelect.length - 1; i >= 0; i--) {
		umSelect.options[i] = null;
	}
}

function clearArray(umArray) {
	for (var i = umArray.length - 1; i >= 0; i--) {
		umArray[i] = null;
	}
}

function copiaParaCampoSeCampoNulo(origem, campo) {
	var campoDestino = document.getElementById(campo);
	if (campoDestino.value == "") {
		campoDestino.value = origem.value;
	}
}

function seleciona(valorSelecionado, nomeCampo) {
	id = document.getElementById(nomeCampo);
	id.value = valorSelecionado;
}

function confirmaSalvar() {
	return window.confirm("Deseja confirmar a operação?");
}

function ajustarData(input, evento){
         var BACKSPACE=  8;          
         var DEL=  46; 
         var FRENTE=  39; 
         var TRAS=  37;
         var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
         if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)) {
             return true; 
             }
         if ( tecla == 13 )     return false; 
		if ((tecla<48)||(tecla>57)) {
			evento.returnValue = false;
			return false;
			} else {
				if ((input.value.length==2)||(input.value.length==5)) {
				   input.value=input.value + "/" ;
				   }
			}
  return true;			
}

function somenteNumeros(input, evento) {
	var BACKSPACE = 8;
	var TAB = 9;
	var ESPACO = 32;
	var END = 35;
	var HOME = 36;
	var TRAS = 37; 
	var FRENTE = 39; 
	var DEL = 46;
	var ENTER = 13;
    var tecla = (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    if (tecla == BACKSPACE || tecla == TAB || tecla == ESPACO || tecla == END || tecla == HOME 
    	|| tecla == TRAS || tecla == FRENTE || tecla == DEL || tecla == ENTER) {
         return true; 
    }
    
	if ((tecla < 48) || (tecla > 57)) {
		evento.returnValue = false; 
		return false;
	}
	return true;
}

function somenteInteiros(input, evento) {
	var BACKSPACE = 8;
	var TAB = 9;
	var ESPACO = 32;
	var END = 35;
	var HOME = 36;
	var TRAS = 37; 
	var FRENTE = 39; 
	var DEL = 46;
	var ENTER = 13;
    var tecla = (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    if (tecla == BACKSPACE || tecla == TAB || tecla == ESPACO || tecla == END || tecla == HOME 
    	|| tecla == TRAS || tecla == FRENTE || tecla == DEL || tecla == ENTER) {
         return true; 
    }
    
	if ((tecla < 48) || (tecla > 57)) {
		evento.returnValue = false; 
		return false;
	}
	return true;
}

function somenteNumerosVirgula(input, evento) {

  var BACKSPACE = 8;
  var DEL = 44; 
  var FRENTE = 39; 
  var TRAS = 37; 
  var TAB = 9;
  var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
  if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)||(tecla == TAB)) {
    return true; 
  }
  if (tecla == 13) return false; 
  if ((tecla < 48) || (tecla > 57)) {
    evento.returnValue = false; 
    return false;
  }
  return true;
}

function ajustarHora(input, evento){
	var BACKSPACE = 8; 
	var DEL = 46; 
	var FRENTE = 39; 
	var TRAS = 37; 
	var tecla = (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
	if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)) {
		return true; 
	}
	if ( tecla == 13 )
    	return false; 

	if ((tecla<48)||(tecla>57)) {
		evento.returnValue = false; 
		return false;
	} else { 
		if (input.value.length == 2) {
			input.value = input.value + ":";
		}
	}
  return true;			
}

function janela(url, nome, largura, altura) {
	w = screen.width;
	h = screen.height;
	meio_w = w/2;
	meio_h = h/2;
	altura2 = altura/2;
	largura2 = largura/2;
	meio1 = meio_h - altura2;
	meio2 = meio_w - largura2;
	var jan = window.open(url, nome, "height=" + altura + ",width=" + largura + ",top=" + meio1 + ",left=" + meio2 + ",scrollbars=yes, resizable=yes");
	jan.focus();
	return jan;
}

function checkData(input, evento) {
	if (input.value.length != 10 && input.value.length != 0) {
		input.value = "";
	} 

	dia = input.value.substring(0, 2);
	if (dia < 1 || dia > 31) {
		input.value = "";
	}
	
	mes = input.value.substring(3, 5);
	if (mes < 1 || mes > 12) {
		input.value = "";
		return;
	}
	
	ano = input.value.substring(6, 10);
	if (ano < 1990 || ano > 2060) {
		input.value = "";
		return;
	}
}

function checkDataGeral(input, evento) {
	if (input.value.length != 10 && input.value.length != 0) {
		input.value = "";
	} 

	dia = input.value.substring(0, 2);
	if (dia < 1 || dia > 31) {
		input.value = "";
	}
	
	mes = input.value.substring(3, 5);
	if (mes < 1 || mes > 12) {
		input.value = "";
		return;
	}
}

function checkHora(input, evento){
	var check = true;
	if (input.value.length != 5 && input.value.length != 0) {
		input.value = "";
		check = false;
	} 

	hora = input.value.substring(0, input.value.indexOf(":"));
	if (hora < 0 || hora > 60) {
		input.value = "";
		check = false;
	}
	
	min = input.value.substring(input.value.indexOf(":") + 1, input.value.length);
	if (min < 0 || min > 60) {
		input.value = "";
		check = false;
	}
	return check;
}

var mensagemConfirmaOperacao = "Deseja confirmar a operação?"

function maximoCaracteres(input, evento, maxCaracteres) {
	if (input.value.length < maxCaracteres) {
		return true;
	}
	
	var BACKSPACE = 8;
	var DEL = 46; 
    var FRENTE = 39; 
    var TRAS = 37; 
    var TAB = 9;
    var ENTER = 13;
    
    var tecla = (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    if ((tecla == BACKSPACE) || (tecla == DEL) || (tecla == FRENTE) || (tecla == TRAS) || (tecla == TAB) || 
    		tecla == ENTER) {
         return true; 
    }
    
	return false;
}

/**
 * Retorna true quando é uma letra ou sua letra com acentuações plausíveis.
 * Utilizar com o evento onkeypress.
 * Autor: Rafael Fontoura
 */
function somenteLetras(input, evento) {
	var BACKSPACE = 8;
	var TAB = 9;
	var ESPACO = 32;
	var END = 35;
	var HOME = 36;
	var TRAS = 37; 
	var FRENTE = 39; 
	var DEL = 46; 
	var ENTER = 13; 
	
	var tecla = (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
	if (tecla == BACKSPACE || tecla == TAB || tecla == ESPACO || tecla >= END && tecla <= TRAS || 
		tecla == FRENTE || tecla == DEL || tecla == ENTER) {
    	return true; 
	}
	if (tecla == 13) return false;
	
	if (tecla >= 65 && tecla <= 90) { //letras, MAIÚSCULAS
		return true;
	}
	
	if (tecla >= 97 && tecla <= 122) { //letras, minúsculas
		return true;
	}
	
	//acentuação do A , ou Ç
	if (tecla >= 193 && tecla <= 195 || tecla >= 225 && tecla <= 227 || tecla == 199 || tecla == 231) {
		return true;
	}
	
	if (tecla == 201 || tecla == 202 || tecla == 233 || tecla == 234) { //acentuação do E
		return true;
	}
	
	if (tecla == 205 || tecla == 237) { //acentuação da letra I
		return true;
	}
	
	if (tecla >= 211 && tecla <= 213 || tecla >= 243 && tecla <= 245) { //acentuação do O
		return true;
	}
	
	if (tecla == 218 || tecla == 250) { //acentuação da letra U
		return true;
	}
	
//	alert(tecla);
	
	evento.returnValue = false; 
	return false;
}

/*
 * Função de comparação de itens dos labels de dois Options.
 */
function compareOptionText(a, b) {
	return a.text != b.text ? (a.text < b.text ? -1 : 1) : 0;
}

/*
 * Ordena um SELECT ou lista de Options por seu label.
 */
function sortOptions(list) {
	var items = list.options != null ? list.options.length : list.length;
	
	// create array and make copies of options in list
	var tmpArray = new Array(items);
	for (i = 0; i < items; i++) {
		var item = list.options != null ? list.options[i] : list[i];
		tmpArray[i] = new Option(item.text, item.value);
	}
	
	// sort options using given function
	tmpArray.sort(compareOptionText);
	
	// make copies of sorted options back to list
	for (i = 0; i < items; i++) {
		var novoOption = new Option(tmpArray[i].text, tmpArray[i].value);
		if (list.options != null) {
			list.options[i] = novoOption;
		} else {
			list[i] = novoOption;
		}
	}
}

/*
 * Retorna true se a lista de Options ou Select passado como parâmetro contém o Option determinado.
 */
function contains(lista, option) {
 	var qtd = lista.options != null ? lista.options.length : lista.length;
 	for (var i = 0; i < qtd; i++) {
 		var item = lista.options != null ? lista.options[i] : lista[i];
 		if (item == null) {
 			continue;
 		}
 		
 		if (item.value == option.value) {
 			return true;
 		}
 	}
 	return false;
 }
 
 function somenteHexadecimal(input, evento) {
	var BACKSPACE = 8;
	var TAB = 9;
	var ESPACO = 32;
	var END = 35;
	var HOME = 36;
	var TRAS = 37; 
	var FRENTE = 39; 
	var DEL = 46;
	var ENTER = 13;
    var tecla = (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    if (tecla == BACKSPACE || tecla == TAB || tecla == ESPACO || tecla == END || tecla == HOME 
    	|| tecla == TRAS || tecla == FRENTE || tecla == DEL || tecla == ENTER) {
         return true; 
    }
    
	if (tecla < 48 || tecla > 57 && tecla < 65 || tecla > 70 && tecla < 97 || tecla > 102) {
		evento.returnValue = false; 
		return false;
	}
	return true;
}

function mostraEsconde (qual) {
	if (document.getElementById(qual).style.display == "none") {
	  document.getElementById(qual).style.display= "";
	}
	else {
	  document.getElementById(qual).style.display = "none";
	}
}
