/*
Classe para tratar exeptions
*/
function Exception(e, msg){
	if(e instanceof Error){
		alert(msg+"\nDescrição:\t" + e.message +
			  	  "\nLinha Número:\t\t" + e.lineNumber +
				  "\nNome do Arquivo:\t\t" + e.fileName + 
				  "\nTipo:\t\t" + e.name);
	}else{
		alert(e);
	}
	return false;
}
/*
Retorna o elemento 
*/
function $(id){
	try{
		return document.getElementById(id);
	}catch(e){
		new Exception(e, "Erro ao executar $("+id+")");
	}	
}
/**
 *
 * function mask(_mask, val)
 *
 * _mask = Mascara Exemplo: ##/##/#### ou ###.###.###-##
 * val   = Valor a ser formatado.
 *
 * Formata um valor  para a mascara definida.
 *
 * pedro.leao@ig.com.br 2003/08/16
 */
function mask(_mask, val) {
	var i, mki;
	var aux="";
	
	for(i=mki=0; i<val.length; i++, mki++) {
		if(_mask.charAt(mki)=='' || _mask.charAt(mki)=='#' || _mask.charAt(i)==val.charAt(i)) {
			aux+=val.charAt(i);
		} else {
			aux+=_mask.charAt(mki)+val.charAt(i);
			mki++;
		}
	}
	return aux;
}

/**
 * function maskEvent(field, _mask, event)
 *
 * field = Objeto que esta enviando o evendo onKeyPress()
 * _mask = Mascara Exemplo: ##/##/#### ou ###.###.###-##
 * event = Evento a ser observado.
 *
 * Formata um valor para a mascara definida conforma o valor vai sendo digitado.
 *
 */
function maskEvent(field, _mask, event) {
	var key ='';
	var aux='';
	var len=0;
	var i=0;
	var aux2='';
	var strCheck = '0123456789';
	var rcode = (window.Event) ? event.which : event.keyCode;
	
	if(rcode == 13) {
		//Enter
		return true;
	}
	
	//Get key value from key code
	key=String.fromCharCode(rcode);
	
	if(strCheck.indexOf(key)==-1) {
		//Not a valid key
		return false;
	}
	aux=field.value;
	aux2=_mask;
	if(aux.length < aux2.length) {
		//aux=field.value+key;
		//window.alert(aux);
		aux=mask(_mask,aux);
		//window.alert(aux);
		field.value=aux;
	}
	field.value=aux;
	return false;
}
/*
Executa a função se precionado enter
*/
function executeEnter( evt , _callBack) {
   var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode );
	if ( charCode == 13 ) {
		if(_callBack != null){
	   		eval(_callBack.toString()+"()");
		}
		return true;
	} else {
	   return false;
	}
}

function simpleConfirm(msg, url){
  	if(confirm(msg)) {
		go(url);
	}
}

function go(url, frame){
	try{
		 var cmd = "document.location.href";
		  if(frame != null){
			cmd = frame+"." + cmd;
		  }
		  cmd = cmd + " = \""+url+"\";";
		  //alert(cmd);
		  eval(cmd);
	}catch(e){
		new Exception(e, "Erro ao executar go("+url+", "+frame+")!")
	}
}

function showId(id){
	try{
		$(id).style.display = "block";
	}catch(e){
		new Exception(e, "Erro ao executar showId("+id+")!")
	}
}

function hideId(id){
	try{
		$(id).style.display = "none";
	}catch(e){
		new Exception(e, "Erro ao executar hideId("+id+")!")
	}
}

function setValueId(id,value){
	try{
		$(id).value = value;
	}catch(e){
		new Exception(e, "Erro ao executar setValueId("+id+", "+value+")!")
	}
}

function getValueId(id){
	try{
		return $(id).value;
	}catch(e){
		new Exception(e, "Erro ao executar getValueId("+id+")!")
	}
}

function validaEmail(email){
	try{
		if( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email) ) {
    	   	return true;
		}
		return false;
	}catch(e){
		new Exception(e, "Erro ao executar validaEmail("+email+")!")
	}
}

function getSelectedRadio(obj){
	try{
		 if(document.forms[0].elements[obj].length){
			 for(var i = 0; i< document.forms[0].elements[obj].length; i++){
				if(document.forms[0].elements[obj][i].checked == true) {
					return document.forms[0].elements[obj][i].value;
				}
			 }
		 }else{
			if(document.forms[0].elements[obj].checked == true) {
				return document.forms[0].elements[obj].value;
			}
		 }
		 return null;
	}catch(e){
		new Exception (e, "Erro ao executar getSelectedRadio()");
	}
}

//-------------------------------------------------------------------
// isNumeric(value)
//   Returns true if value contains a positive float value
//-------------------------------------------------------------------
function isNumeric(val){
	return(parseFloat(val,10)==(val*1));
}

