function mascara_data(data){ // usar com evento OnKeyUp
  var mydata = ''; 
  mydata = mydata + data.value; 

  if (mydata.length == 2){ 
    mydata = mydata + '/'; 
    data.value = mydata; 
  } 
  
  if (mydata.length == 5){ 
    mydata = mydata + '/'; 
    data.value = mydata; 
  } 
}

function mascara_hora(hora){ // usar com evento OnKeyUp
  var myhora = ''; 
  myhora = myhora + hora.value; 
  
  if (myhora.length == 2){ 
    myhora = myhora + ':'; 
    hora.value = myhora; 
  }
}

function numerico(campo) { // usar com evento OnKeyPress
  campo.value = campo.value.toLowerCase(); 

  var caracteres = "abcdefghijklmnopqrstuvwxyzáéíóúàèìòùâêîôûäëïöüãõ@#$%^&*()_+=-~` ç.\/[]{}<>?!|:;,"; 
  var retorno  = ""; 

  for(i=0;i<caracteres.length;i++) { 
    for(j=0;j<campo.value.length;j++) { 
      retorno = campo.value.replace(caracteres.substr(i,1),""); 
      campo.value = retorno; 
    } 
  }    
}

function maxTexto(campo,tamanho) { // definir o tamanho máximo de um determinado campo - OnKeyPress
  var cmp = campo.value;
  var TamCampo = campo.length;

  if (tamanho == 0) {
    alert("Este campo é somente para leitura");
	//campo.value = cmp;
	campo.readOnly = "true";
	return false;
  } else {
    if (campo.value.length > tamanho) {
      alert("Tamanho máximo do campo é de "+tamanho);
	  campo.value = campo.value.substr(campo.value,tamanho);
    }
  }
}

function insereItem(objeto, texto, valor) {
	var o = new Option(texto,valor);
	objeto.options[objeto.options.length]=o;
}

function removeItem(objeto) {
	var i = objeto.selectedIndex;
	
	if (i >= 0) {
		objeto.options[i] = null;
	}
}

function transfereItem(origem, destino) {
	var pos = origem.selectedIndex;

	if (pos < 0) return;
	
	var texto = origem.options[pos].text;
	var valor = origem.options[pos].value;
	
	insereItem(destino, texto, valor);
	removeItem(origem);
}

function disablehref(){
	var input = document.getElementsByTagName("a");
	var count = input.length;
	
	for (var i =0; i < count; i++){
		//document.getElementsByTagName("a")[i].disabled = true;
		document.getElementsByTagName("a")[i].removeAttribute("href");
		document.getElementsByTagName("a")[i].style.cursor='pointer';
	}

	return true;
}

function validarData(campo){
	var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	var msgErro = 'Formato inválido de data.';

	if ((campo.value.match(expReg)) && (campo.value!='')){
		var dia = campo.value.substring(0,2);
		var mes = campo.value.substring(3,5);
		var ano = campo.value.substring(6,10);
		if((mes==4 || mes==6 || mes==9 || mes==11) && dia > 30){
		alert("Dia incorreto !!! O mês especificado contém no máximo 30 dias.");
		return false;
	} else{
		if(ano%4!=0 && mes==2 && dia>28){
			alert("Data incorreta!! O mês especificado contém no máximo 28 dias.");
			return false;
		} else{
			if(ano%4==0 && mes==2 && dia>29){
				alert("Data incorreta!! O mês especificado contém no máximo 29 dias.");
				return false;
			} else{
				return true;
			}
		}
	}
	} else {
		alert(msgErro);
		campo.focus();
		return false;
	}
}

function formata(src, mask)	{
	var i = src.value.length;
	var saida = '#';
	var texto = mask.substring(i)

	if (texto.substring(0,1) != saida) {
		src.value += texto.substring(0,1);
	}
}

function escondeObjeto(obj) {
	if (navigator.appName == 'Netscape') {
		document.getElementById(obj).style.visibility = 'collapse';
	} else {
		eval("document.all." + obj + ".style.display = 'none'");
	}
}

function exibeObjeto(obj) {
	if (navigator.appName == 'Netscape') {
		document.getElementById(obj).style.visibility = 'visible';
	} else {
		eval("document.all." + obj + ".style.display = 'block'");
	}
}

String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
}

function checkMail(mail){
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	
	if(typeof(mail) == "string"){
		if(er.test(mail)){ return true; }
	} else if(typeof(mail) == "object"){
		if(er.test(mail.value)){
        	return true;
		}
    } else{
		return false;
	}
}

// url_encode version 1.0 
function url_encode(str) { 
	var hex_chars = "0123456789ABCDEF"; 
	var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
	var n, strCode, hex1, hex2, strEncode = ""; 

	for(n = 0; n < str.length; n++) { 
		if (noEncode.test(str.charAt(n))) { 
			strEncode += str.charAt(n); 
		} else { 
			strCode = str.charCodeAt(n); 
			hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
			hex2 = hex_chars.charAt(strCode % 16); 
			strEncode += "%" + (hex1 + hex2); 
		} 
	} 
	return strEncode; 
} 

// url_decode version 1.0 
function url_decode(str) { 
	var n, strCode, strDecode = ""; 

	for (n = 0; n < str.length; n++) { 
		if (str.charAt(n) == "%") { 
			strCode = str.charAt(n + 1) + str.charAt(n + 2); 
			strDecode += String.fromCharCode(parseInt(strCode, 16)); 
			n += 2; 
		} else { 
			strDecode += str.charAt(n); 
		} 
	} 

	return strDecode; 
}
