	// CSS Browser Selector   v0.2.5
	// Documentation:         http://rafael.adm.br/css_browser_selector
	// License:               http://creativecommons.org/licenses/by/2.5/
	// Author:                Rafael Lima (http://rafael.adm.br)
	// Contributors:          http://rafael.adm.br/css_browser_selector#contributors
	var css_browser_selector = function() {
		var 
			ua=navigator.userAgent.toLowerCase(),
			is=function(t){ return ua.indexOf(t) != -1; },
			h=document.getElementsByTagName('html')[0],
			b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
			os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
		var c=b+os+' js';
		h.className += h.className?' '+c:c;
	}();

	
	function janela(destino, titulo, width, height) {
		addWindow = open(destino, titulo,"resizable=no,toolbar=no,status=no,menubar=no,scrollbars=yes,top=100,left=100,width=" + width + ",height=" + height);
		if(addWindow.opener == null) addWindow.opener = self;
	}

	function objetoPagina (div, botao) { 
		  this.div = div; 
		  this.botao = botao;
	}	
	
	function mudarPagina(num, Paginas) {
		for (var i = 1; i <= Paginas.div; i++) {
			if(i == num) {
				Paginas[i].div.style.display='';
				Paginas[i].botao.style.backgroundColor='#F9CFA6';
			} else {
				Paginas[i].div.style.display='none';
				Paginas[i].botao.style.backgroundColor='#CDD8E4';
			}
		} 			
	}

    function dataAtual(campo) {
        	//------pega variaveis de data atual-----------------------
    	var data = new Date();
    	var ano = data.getYear();
    	var mes = data.getMonth() + 1;
    	mes = mes.toString();
    	if(mes.length == 1)
    		mes = "0" + mes;
    	var dia = data.getDate();
    	dia = dia.toString();
    	if(dia.length == 1)
    		dia = "0" + dia;
    	//---------------------------------------------------------
    	if (!campo.value.length) {
    		campo.value = dia + "/" + mes + "/" + ano;
    	} else if (campo.value.length <= 2) {
    		if(campo.value.length == 1)
    			campo.value = "0" + campo.value;
    		campo.value = campo.value + "/" + mes + "/" + ano;
    	} else if(campo.value.length == 5) {
    		campo.value = campo.value + "/" + ano;
    	}
    }
    
	function tiraPick(str) {
		temp = '';
		for (i = 0; i < str.length; i++) {
			if((str.substring(i, i+1) >= '0') && (str.substring(i, i+1) <= '9'))
				temp = temp + str.substring(i, i+1);
		}
		return temp;
	}
    
    function formataNumeroProcessoSA(campo) {
        if(campo.value.length) {
			var num = campo.value;
			var t = campo.value.length;
			
			if(t < 4)
				for(z = 0;z < (4-t);z++) 
					num = '0'+num;			
			
			campo.value = num;
		}
    }
	
	function formataNumeroProcesso(campo) {
        if(campo.value.length) {
			var pos;
			if(campo.value.indexOf('/') == -1) 
				pos = campo.value.length;
			else
				pos = campo.value.indexOf('/');
			var strCheck = '0123456789';
			var num1 = campo.value.substring(0,pos);
			var num2 = '';
			var tag = 1;
			var t;
			for(i = pos;i >= 0;i--) {
				if((pos-i<=4) && (tag)){
					if (strCheck.indexOf(num1.charAt(i)) == -1) {
						tag = 0;
						for(z = 0;z < (5-(pos-i));z++) 
							num2 = '0'+num2;
					}
				}
				num2 = num1.charAt(i)+num2;
			}
			t = num2.length;
			if(t < 4)
				for(z = 0;z < (4-t);z++) 
					num2 = '0'+num2;			
			
			var ano2 = '';
			var dif = '';
			if(campo.value.indexOf('/') != -1) {
				var ano1 = campo.value.substring(pos+1,campo.value.length);
				for(i = 0;i < ano1.length;i++) {
					if (strCheck.indexOf(ano1.charAt(i)) == -1) 
						dif = dif+ano1.charAt(i);
					else 
						ano2 = ano2+ano1.charAt(i);
				}
				t = ano2.length;
				if(t < 4) {
					if(ano2<70 || ano2>99 ) {
						for(z = 0;z < (3-t);z++) 
							ano2 = '0'+ano2;
						ano2 = '2'+ano2;
					} else {
						ano2 = '19'+ano2;
	
					}
				}
			} else {
				DataToda = new Date(); 
				ano2 = DataToda.getUTCFullYear(); 
			}
			campo.value = num2+'/'+ano2+dif;
		}
    }

    function formataData(campo,e) {
        var whichCode = (window.Event) ? e.which : e.keyCode;
		if ((whichCode == 13) || (whichCode == 8) || (whichCode == 0)) return; // Enter ou backspace
		if((campo.value.length == 2) || (campo.value.length == 5)) {
            campo.value = campo.value + "/";
        }
    }
	
	function formataDiaMes(campo) {
        if(campo.value.length == 2) {
            campo.value = campo.value + "/";
        }
    }
	
	function formataPeriodo(campo) {
        if(campo.value.length == 2) {
            campo.value = campo.value + "/";
        }
    }
	
	function formataCEP(campo) {
		if (campo.value.length == 5) {
			campo.value = campo.value + "-";
		} 
	}
	
	function formataDataHora(campo) {
		if((campo.value.length == 2) || (campo.value.length == 5)) {
            campo.value = campo.value + "/";
        } else if (campo.value.length == 10) {
			campo.value = campo.value + " ";
		} else if ((campo.value.length == 13) || (campo.value.length == 16)) {
			campo.value = campo.value + ":";
		}
	}
	
	function formataHora(campo,e,semsegundos) {
		var whichCode = (window.Event) ? e.which : e.keyCode;
		if ((whichCode == 13) || (whichCode == 8) || (whichCode == 0)) return; // Enter ou backspace
		if((campo.value.length == 2) || ((!semsegundos) && (campo.value.length == 5)))  {
            campo.value = campo.value + ":";
        } 
	}

	function completaAnoData(campo) {
		if(campo.value.length == 8) {
			ano = campo.value.substring(6,8);
			if(ano < 30)
				campo.value = campo.value.substring(0,6) + '20' + ano;
			else
				campo.value = campo.value.substring(0,6) + '19' + ano;
		}
	}

	function formataCPF(campo) {
		if((campo.value.length == 3) || (campo.value.length == 7)) {
			campo.value = campo.value + ".";
		} else if (campo.value.length == 11) {
			campo.value = campo.value + "-";
		} 	
	}
	
	function formataCNPJ(campo) {
		if((campo.value.length == 2) || (campo.value.length == 6)) {
			campo.value = campo.value + ".";
		} else if (campo.value.length == 10) {
			campo.value = campo.value + "/";
		} else if (campo.value.length == 15) {
			campo.value = campo.value + "-";
		}
	
	}
	
	
	function completaDocumento(campo, valida) {
		campo.value = tiraPick(campo.value);
		if(campo.value.length == 14) {
			completaCNPJ(campo);
			if(valida)
				validaFuncCNPJ(campo);
		} else if(campo.value.length == 11) { 
			completaCPF(campo);
			if(valida)
				validaFuncCPF(campo);
		} else
			campo.value = "";
	}

	function completaCNPJ(campo) {
		if(campo.value.length == 14) {
			campo.value = campo.value.substring(0,2) + "." + campo.value.substring(2,5) + "." + campo.value.substring(5,8) + "/" + campo.value.substring(8,12) + "-" + campo.value.substring(12,14);
		}	
	}

	function completaCPF(campo) {
		if(campo.value.length == 11) {
			campo.value = campo.value.substring(0,3) + "." + campo.value.substring(3,6) + "." + campo.value.substring(6,9) + "-" + campo.value.substring(9,11)
		}	
	}
	
	function validaFuncCPF(campo) {
		if(!campo.value.length)
			return;
		if(!validaCPF(campo.value)) {
			campo.value = '';
			alert("CPF inválido.");
			campo.focus();
			campo.select();
		}
	}
	
	function validaFuncCNPJ(campo) {
		if(!campo.value.length)
			return;
		if(!validaCNPJ(campo.value)) {
			campo.value = '';
			alert("CNPJ inválido.");
			campo.focus();
			campo.select();
		}
	}

	function validaCNPJ(CNPJ) {
		if (CNPJ.length < 18) return false; 
		if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
			return false;
		}
		//substituir os caracteres que não são números
		if(document.layers && parseInt(navigator.appVersion) == 4){
			x = CNPJ.substring(0,2);
			x += CNPJ.substring (3,6);
			x += CNPJ.substring (7,10);
			x += CNPJ.substring (11,15);
			x += CNPJ.substring (16,18);
			CNPJ = x; 
		} else {
			CNPJ = CNPJ.replace (".","");
			CNPJ = CNPJ.replace (".","");
			CNPJ = CNPJ.replace ("-","");
			CNPJ = CNPJ.replace ("/","");
		}
		var nonNumbers = /\D/;
		if (nonNumbers.test(CNPJ)) return false; 
		var a = [];
		var b = new Number;
		var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
		for (i=0; i<12; i++){
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
		}
		if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
		b = 0;
		for (y=0; y<13; y++) {
		b += (a[y] * c[y]); 
		}
		if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
		if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
			return false;
		}
		return true;
	}



	//Função de validação de CPF
	function validaCPF(st) {
		st = tiraPick(st);
		//alert(st);
		if(!st.length || (st.length != 11))
  			return false;
		digito1 = st.substring(9, 10);
		digito2 = st.substring(10, 11);
		digito1 = parseInt(digito1, 10);
		digito2 = parseInt(digito2, 10);
		sum = 0; mul = 10;
		for (i = 0; i < 9 ; i++) {
    		digit = st.substring(i, i+1);
    		tproduct = parseInt(digit ,10) * mul;
    		sum += tproduct;
    		mul--;
		}
		dig1 = ( sum % 11 );
		if ( dig1==0 || dig1==1 )
   			dig1=0;
		else	
  			dig1 = 11 - dig1;
		if (dig1!=digito1)
  			return false;
		sum = 0; mul = 11;
		for (i = 0; i < 10 ; i++) {
    		digit = st.substring(i, i+1);
    		tproduct = parseInt(digit ,10) * mul;
    		sum += tproduct;
    		mul--;
		}
		dig2 = (sum % 11);
		if (dig2 == 0 || dig2 == 1)
  			dig2 = 0;
		else
  			dig2 = 11 - dig2;
		if (dig2 != digito2)
  			return false;
		return true;
	}

    function somaCampos(campo, campo1, campo2) {
        campo1 = parseFloat("0" + floatBR2US(campo1.value));
        campo2 = parseFloat("0" + floatBR2US(campo2.value));
        //alert(campo1+'*'+campo2);
        campo.value = floatUS2BR(campo1 * campo2);
    }
    
	function selecionaTodosCheck(status, form) {
		if(!form)
			return false;
		for (var i = 0; i < form.elements.length; i++) {
		  if (form.elements[i].type == "checkbox") {
			form.elements[i].checked = status;
		  }
		}
	}

	function ExpandCollapse(e) {
		if (e.style.display == "")
			e.style.display = "none";
		else
			e.style.display = "";
	}

	function ExpandCollapseKeyboard(e) {
		if (event.keyCode == 33) //Page Up collapses
		{
			if (e.style.display == "")
				ExpandCollapse(e);
		}
		else if (event.keyCode == 34) //Page Down Expands
		{
			if (e.style.display == "none")
				ExpandCollapse(e);		
		}
	}
		
	
	function excluirVeiculo(codigo) {
		//if(confirm('Excluir '+document.all.prefixo.value+'?'))document.all.carrega.src='carrega.php?acao=excluir'
		if(codigo.length > 0) {
			if(confirm('Excluir '+document.all.prefixo.value+'?')) document.all.carrega.src='carrega.php?acao=excluir'
		} else
			alert("ESCOLHA UM VEÍCULO VÁLIDO PRA EXCLUIR!");
	}

	function encerraContrato(codigo) {
		if (codigo > 0) {
			if (confirm('Encerrar contrato '+document.all.codigo+'?')) document.all.carrega.src='WCON201Carrega.php?acao=encerrar'
		} else
			alert("Escolha um contrato.");
	}

	function excluirContrato(codigo) {
		if (codigo > 0) {
			if (confirm('Exlcuir contrato selecionado?')) document.all.carrega.src='WCON101Carrega.php?acao=excluir'
		} else
			alert("Escolha um contrato.");
	}

	function excluirFuncionario(codigo) {
		if (codigo > 0) {
			if (confirm('Exlcuir '+document.all.nome.value+'?')) document.all.carrega.src='WCAD104Carrega.php?acao=excluir'
		} else
			alert("Escolha um Funcionário.");
	}

	function excluirFornecedor(codigo) {
		if (codigo > 0) {
			if (confirm('Exlcuir '+document.all.razao.value+'?')) document.all.carrega.src='WCAD103Carrega.php?acao=excluir'
		} else
			alert("Escolha um Fornecedor.");
	}

	function transformaData(data) {
		if (data) {
			return(data.substring(6,10) + data.substring(3,5) + data.substring(0,2));
		} else
			return 0;
	}
	
	function floatUS2BR(valor) {
		valor = valor.toString();
		pos = valor.indexOf(",", 0);
		if(pos >= 0) 
			valor = valor.substr(0, pos) + valor.substr(pos + 1, valor.length - pos);
		valor = valor.toString();
		pos = valor.indexOf(".", 0);
		if(pos >= 0) { //aredondamento
			valor = valor+"0";
			centesimos = parseFloat("0." + valor.substr(pos + 3, valor.length - pos));
			decimos = parseInt(valor.substr(pos + 1, 2));
			unidades = parseInt(valor.substr(0, pos));

			if (centesimos > 0.5) 
				decimos++; //para arrendondar
			if (decimos >= 100) { //acrescentar uma unidade
				decimos -= 100;
				unidades++;
			}
			valor = unidades + "," + (decimos < 10 ? "0" : "" ) + decimos;
		} else {
			valor = valor + ",00";
		}
		return valor;
	}

    function floatBR2US(valor) {
	valor = valor.toString();
	pos = valor.indexOf(".", 0);
	if (pos >= 0) 
	    valor = valor.substr(0, pos) + valor.substr(pos + 1, valor.length - pos);
		valor = valor.toString();
	pos = valor.indexOf(",", 0);
	if(pos >= 0)
	    valor = valor.substr(0, pos) + "." + valor.substr(pos + 1, valor.length - pos);
	return valor;
    }


    function calculoPecas(quantidade, valorUnitario, total, desconto, totalLiquido) {
	campo1 = parseFloat("0" + floatBR2US(quantidade.value));
	campo2 = parseFloat("0" + floatBR2US(valorUnitario.value));
	//alert(campo1+'*'+campo2);
	total.value = floatBR2US(floatUS2BR(campo1 * campo2));
	valorDesconto = total.value * (desconto.value / 100);
	//alert(total.value+' - '+valorDesconto);
	totalLiquido.value = total.value - valorDesconto;
    }

	function calculaTotal(quantidade, valor, campoTotal)	{
		quantidade = parseFloat("0" + floatBR2US(quantidade));
		valor = parseFloat("0" + floatBR2US(valor));	
		campoTotal.value = floatUS2BR(quantidade * valor);
	}


	function formataMoeda(fld, milSep, decSep, e) {
		var whichCode = (window.Event) ? e.which : e.keyCode;
		if ((whichCode == 13) || (whichCode == 8) || (whichCode == 0)) return true; // Enter ou backspace
		if (fld.value.length > fld.maxLength) { 
			return false;
		}
		var sep = 0;
		var key = '';
		var i = j = 0;
		var len = len2 = 0;
		var strCheck = '0123456789';
		var aux = aux2 = '';
		key = String.fromCharCode(whichCode); // Get key value from key code
		if (strCheck.indexOf(key) == -1) return false; // Not a valid key
		len = fld.value.length;
		for(i = 0; i < len; i++)
			if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
		aux = '';
		for(; i < len; i++)
			if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
		aux += key;
		len = aux.length;
		if (len == 0) fld.value = '';
		if (len == 1) fld.value = '0'+ decSep + '0' + aux;
		if (len == 2) fld.value = '0'+ decSep + aux;
		if (len > 2) {
			aux2 = '';
			for (j = 0, i = len - 3; i >= 0; i--) {
				if (j == 3) {
					aux2 += milSep;
					j = 0;
				}
				aux2 += aux.charAt(i);
				j++;
			}
			fld.value = '';
			len2 = aux2.length;
			for (i = len2 - 1; i >= 0; i--)
				fld.value += aux2.charAt(i);
			fld.value += decSep + aux.substr(len - 2, len);
		}
		return false;
	}

	function formataMoedaTres(fld, milSep, decSep, e) {
		var whichCode = (window.Event) ? e.which : e.keyCode;
		if ((whichCode == 13) || (whichCode == 8) || (whichCode == 0)) return true; // Enter ou backspace
		if (fld.value.length > 10) { 
			return false;
		}
		var sep = 0;
		var key = '';
		var i = j = 0;
		var len = len2 = 0;
		var strCheck = '0123456789';
		var aux = aux2 = '';
		key = String.fromCharCode(whichCode); // Get key value from key code
		if (strCheck.indexOf(key) == -1) return false; // Not a valid key
		len = fld.value.length;
		for(i = 0; i < len; i++)
			if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
		aux = '';
		for(; i < len; i++)
			if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
		aux += key;
		len = aux.length;
		if (len == 0) fld.value = '';
		if (len == 1) fld.value = '0'+ decSep + '00' + aux;
		if (len == 2) fld.value = '0'+ decSep + '0' + aux;
		if (len == 3) fld.value = '0'+ decSep + aux;
		if (len > 3) {
			aux2 = '';
			for (j = 0, i = len - 4; i >= 0; i--) {
				if (j == 3) {
					aux2 += milSep;
					j = 0;
				}
				aux2 += aux.charAt(i);
				j++;
			}
			fld.value = '';
			len2 = aux2.length;
			for (i = len2 - 1; i >= 0; i--)
				fld.value += aux2.charAt(i);
			fld.value += decSep + aux.substr(len - 3, len);
		}
		return false;
	}

	function criticaNumero(fld, maxLenght,e) {
		var whichCode = (window.Event) ? e.which : e.keyCode;
		if ((whichCode == 13) || (whichCode == 8) || (whichCode == 0)) return true; // Enter ou backspace
		if (fld.value.length >= maxLenght) { 
			return false; 
		}
		var key = '';
		var strCheck = '0123456789';
		key = String.fromCharCode(whichCode); // Get key value from key code
		if (strCheck.indexOf(key) == -1) return false; // Not a valid key
		fld.value += key;
		return false;
	}
	
	function criticaLetra(fld, maxLenght,e) {
		var whichCode = (window.Event) ? e.which : e.keyCode;
		if ((whichCode == 13) || (whichCode == 8) || (whichCode == 0)) return true; // Enter ou backspace ou tab
		if (fld.value.length >= maxLenght) { 
			return false;
		}
		var key = '';
		var strCheck = '.abcdefghijklmnopqrstuvxywzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		key = String.fromCharCode(whichCode); // Get key value from key code
		if (strCheck.indexOf(key) == -1) return false; // Not a valid key
		fld.value += key;
		return false;
	}
