//////////// MOEDAS ////////////
function formataMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    // 13=enter, 8=backspace as demais retornam 0(zero)
    // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    
	
	if (objTextBox.value.length >= 9) whichCode = 1;
	
    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
    	return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
 
 
    if (strCheck.indexOf(key) == -1) 
    	return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) 
        	break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) 
        	aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) 
    	objTextBox.value = '';
    if (len == 1) 
    	objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) 
    	objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        	objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

//////////// APENAS NUMEROS ////////////
function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function soNumeros(v){
    return v.replace(/\D/g,"")
}

//////////// POP-UP ////////////
function popup(url, nome, width, height) {
  window.open(url, nome, 'width='+width+', height='+height+', top=2, left=2, scrollbars=yes, status=yes, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}

//////////// SHOW - HIDE ////////////
function show_hide(acao, ID) {
	if (acao == ''){
		if (document.getElementById(ID).style.display == "none") {
			document.getElementById(ID).style.display = "";
		}else {
		   document.getElementById(ID).style.display = "none";
		}
	}else{
		if(acao == 's'){
			document.getElementById(ID).style.display = "";
		}else{
			document.getElementById(ID).style.display = "none";
		}
	}
}

//////////// CONTA CARACTERES ////////////
function max(txarea){ 
	total = 500; 
	tam = txarea.value.length; 
	str = ""; 
	str=str+tam; 
	Restante.innerHTML = total - str;

	if (tam > total){ 
		aux = txarea.value; 
		txarea.value = aux.substring(0,total); 
		Restante.innerHTML = 0;
	}
}

//////////// CONFIRMA REMOÇÃO ////////////
function confirma_remocao(){
	if(!confirm('Deseja remover os registros selecionados?')){
		return false;
	}
}

//////////// ACENTOS ////////////
function acentos(e){
    var acentos = new String('àâêôûãõáéíóúçüÀÂÊÔÛÃÕÁÉÍÓÚÇÜ´~,!@#$%¨&()+={[}]^?/\;:>< ');      
    var k= "";
    if (e.which){
		k = e.which;
    }else{
		if (e.keyCode){
	        k = e.keyCode;
    	}
	}
    
    var rxp = new RegExp(String.fromCharCode(k))
    var pos = acentos.search(rxp);

    if (pos > -1){
        alert('Você digitou um caractere inválido.\rPor favor, reveja o texto.');
		return false;
    }else{
		return true;
    }
}

//////////// VALIDA CAMPOS ////////////
// SINTAX: valida(campos)
// campos -> nomes dos campos a serem 
//			 validados, separados por virgulas

function tokenizer(str,captura){
    var tokens = new Array();
    indiceTokens = 0;
    fimStr = str.length;
    pos = 0;
    for (i=0; i<fimStr; i++){
	 	if(str.charAt(i) == captura){
			tokens[indiceTokens] = str.substring(pos,i);
	     	pos = i + 1;
    	 	indiceTokens++;
	 	}
	}
    return tokens;
}

function valida(campos){
	var campos = tokenizer(campos+',',',');
	for(var i in campos){
		if(document.getElementById(campos[i]).value == ""){
			document.getElementById(campos[i]).style.border = "#990000 1px solid";
			document.getElementById(campos[i]).focus();
			return false;
		}
	}
}

//////////// NOT FOUND ////////////
function notfound(imagem){
	imagem.src="http://www.progbr.com/imagens/imagem_notfound.gif";
}

//////////// FOCO ////////////
function sf(ID){
	document.getElementById(ID).focus();
}

//////////// POP IMAGEM ////////////
function PopupPic(sPicURL){
	window.open("http://www.biketoor.com.br/pop_imagem.html?"+ sPicURL, "", "resizable=1,HEIGHT=200,WIDTH=200,scrollbars=yes,status=yes");
}

var arrTemp=self.location.href.split("?");
var picUrl = (arrTemp.length>0)?arrTemp[1]:"";
var NS = (navigator.appName=="Netscape")?true:false;

function FitPic() {
	iWidth = (NS)?window.innerWidth:document.body.clientWidth;
	iHeight = (NS)?window.innerHeight:document.body.clientHeight;
	iWidth = document.images[0].width - iWidth;
	iHeight = document.images[0].height - iHeight;
	window.resizeBy(iWidth, iHeight);
	self.focus();
};
