// checar navegador do usuario
var sAgent = navigator.userAgent;
var bIsIE = sAgent.indexOf("MSIE") > -1;
var bIsNav = sAgent.indexOf("Mozilla") > -1 && !bIsIE;


// evento: evento associado ao input
// input: elemento ao qual se vai associar a fun��o
// chamada: onkeypress="return permiteNumeros(event, this);"
function permiteNumeros(evento, input){
	
	if(bIsIE){
		if((evento.keyCode >= 48 && evento.keyCode <= 57) || evento.keyCode == 13){
			return true;
		}
		else
			return false;
	}
	else{
		if((evento.which >= 48 && evento.which <= 57) || evento.which == 8 || evento.which == 0 || evento.keyCode == 13){
			return true;
		}
		else
			return false;
	}
}

// p_obj: elemento ao qual se vai associar a fun��o
// evt: evento associado ao input
function maskDate(p_obj, evt)
{
	var strtmp = '';
	var rExpr = new RegExp("[0-9]");
	var inicio = p_obj.value.length - 1;
	var text = p_obj.value;
	
	if (evt) 
		key = evt.keyCode || evt.which;
	else 
		key = window.event.keyCode;
			
	if(key != 8)
	{
		if(rExpr.test(text.substr(inicio, 1)) != true)
			strtmp = text.substr(0, inicio);
			
		if(text.length == 3 && text.substr(2, 1) != '/')
			strtmp = text.substr(0, 2) + '/' + text.substr(2, 1);
			
		if(text.length == 6 && text.substr(5, 1) != '/')
			strtmp = text.substr(0, 5) + '/' + text.substr(5, 1);
			
		if(strtmp != '')
			p_obj.value = strtmp;
	}	
}


// obj: objeto html textarea
function maxLength(obj){
	
	var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : "";
	if (obj.getAttribute && obj.value.length>mlength)
		obj.value = obj.value.substring(0,mlength)
}

// p_id: id da tabela a ter os dados apagados
function clearDataTable(p_id){
	
	if(document.getElementById(p_id))
	{
		trs = document.getElementById(p_id).childNodes;
		
		for(i = 0; i < trs.length;i++)
		{
			//alert(trs[i].tagName)
			tds = trs[i].childNodes;
			//alert(1)
			for(j = 0; j < tds.length; j++)
			{
				filhos_tds = tds[j].childNodes;
				//alert(filhos_tds.length)
				for(k = 0; k < filhos_tds.length; k ++)
				{
					//alert(2)
					if(filhos_tds[k])
					{
						if(filhos_tds[k].type)
						{	
							tipo = filhos_tds[k].type;
							if(tipo == "text" || tipo == 'hidden' || tipo == 'password' )
							{
								
								//alert(filhos_tds[k].value);
								filhos_tds[k].value = '';
							}
							else if(tipo == 'textarea'){
								filhos_tds[k].value = '';	
							}
								
							
						}
					}
				}
			}
		}
	}
	else
	{
		alert("O elemento de id "+p_id+" não existe!");
	}

}

function validaEmail(email)
{
  if ((email.length != 0) && ((email.indexOf("@") < 1) || (email.indexOf('.') < 7)))
    return false;
    
   return true;
}