// ------------------------------------------------------------------------------------------------------------
// Formatação de campos
// ------------------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------------------
// Janela pedidos
// ------------------------------------------------------------------------------------------------------------
function janelaPedidos(URL){
   window.open(URL,"Pedidos","width=420,height=380,scrollbars=NO,status=NO")
}

// ------------------------------------------------------------------------------------------------------------
// Janela mostra veiculo
// ------------------------------------------------------------------------------------------------------------
function janelaVeiculo(URL){
   window.open(URL,"Veiculo","width=650,height=360,scrollbars=NO,status=NO")
}

// ------------------------------------------------------------------------------------------------------------
// Janela 
// ------------------------------------------------------------------------------------------------------------
function janela(URL){
   window.open(URL,"Janela","width=800,height=500,scrollbars=NO,status=NO")
}

// ------------------------------------------------------------------------------------------------------------
// Janela Mail Vendedor
// ------------------------------------------------------------------------------------------------------------
function janelaMailVendedor(URL){
   window.open(URL,"MailVendedor","width=380,height=250,scrollbars=NO","LOCATION=NO")
}

// ------------------------------------------------------------------------------------------------------------
// Valida E-mail.....
// ------------------------------------------------------------------------------------------------------------
// Formata Valores.....
function FormataValor(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
	 		document.form[campo].value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		document.form[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		document.form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		document.form[campo].value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 12) && (tam <= 14) ){
	 		document.form[campo].value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		document.form[campo].value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
	}
		
	for (var ct = 0; ct < document.form.elements.length; ct++) {
		if (document.form.elements[ct].name == document.form.elements[campo].name) {
			if ( !teclapres.shiftKey && tecla == 9 && document.form.elements[ct+1] && document.form.elements[ct+1].name == "senhaConta" && document.applets['tclJava'] ){
				document.applets['tclJava'].setFocus();
			}	
		}
	}
}

// ------------------------------------------------------------------------------------------------------------
// Permite somente numeros
function soNums(e,args)
    {        
    // Função que permite apenas teclas numéricas e 
    // todos os caracteres que estiverem na lista
    // de argumentos.
    // Deve ser chamada no evento onKeyPress desta forma
    //  onKeyPress ="return (soNums(event,'(/){,}.'));"
    // caso queira apenas permitir caracters

        if (document.all){var evt=event.keyCode;} // caso seja IE
        else{var evt = e.charCode;}    // do contrário deve ser Mozilla
        var chr= String.fromCharCode(evt);    // pegando a tecla digitada
        // Se o código for menor que 20 é porque deve ser caracteres de controle
        // ex.: <ENTER>, <TAB>, <BACKSPACE> portanto devemos permitir
        // as teclas numéricas vão de 48 a 57
        if (evt <20 || (evt >47 && evt<58) || (args.indexOf(chr)>-1 ) ){return true;}
        return false;
    }
	
//----------------------------------------------------------------------------------------------------------------
// Valida formulario
function validate() {
  var str = "";
  var elements = document.getElementsByTagName('*'); // "pega" todos os elementos da página
  err_focus=0; // zera variavel que verifica o primeiro erro que ocorre
  
 for(i=0; i<elements.length; i++) {
 // Valida os elementos input, select e textarea
   if(elements.item(i).tagName.toLowerCase()=='input' || elements.item(i).tagName.toLowerCase()=='select' || elements.item(i).tagName.toLowerCase()=='textarea'){
        if(elements.item(i).getAttribute('errorMsg')){     
            if(elements.item(i).value.length == 0 || elements.item(i).value == "" || elements.item(i).selectedIndex == 0) { 
                str += elements.item(i).getAttribute('errorMsg')+ "\n";
                err_focus++;
                if (err_focus==1){field_focus = i;} // serve para colocar o foco no primeiro erro que ocorrer
           }else{
               if(elements.item(i).type.toLowerCase()=='text' || elements.item(i).type.toLowerCase()=='select-one' || elements.item(i).type.toLowerCase()=='textarea'){     //->Este if serve para não deixar o submit branco
                    elements.item(i).style.background = "white";
                }     
          }
      }
   } 
 }

  if (str != "") {
     // Não submete o form
     alert(str); 
     elements.item(field_focus).focus();
     return false;
  }
  
}

//-> Desabilita o Botão de Envio para evitar duplicidade
function disableSubmit(theform){
 if (document.all || document.getElementById) {
   for (i = 0; i < theform.length; i++) {
       var tempobj = theform.elements[i];
       if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset"){
          tempobj.style.cssText = 'font-size:10px; color:#666666;';
          tempobj.value='Aguarde...';
          tempobj.disabled = true;
       }    
   }
 }
} 
//------------------------------------------------------------------------------------------
// Formata mascaras em geral
function formatar(mascara, documento){
var i = documento.value.length;
var saida = mascara.substring(0,1);
var texto = mascara.substring(i)
  
  if (texto.substring(0,1) != saida){
	documento.value += texto.substring(0,1);
  }
  
}

/* -------------------------------------------------------------------------------------------
Funcao para passar var por post*/
function doPost(fields)
{
	frm = document.createElement("FORM");
	frm.method = 'POST';
	if (arguments[1] != undefined)
		frm.action = arguments[1];
	for(i=0; i < fields.length; i++)
	{
		arrAttr = fields[i].split(':');
		fldName = arrAttr[0];
		fldValue = arrAttr[1];
		inputHidden = document.createElement("INPUT");
		inputHidden.type = 'hidden';
		inputHidden.name = fldName;
		inputHidden.value = fldValue;
		frm.appendChild(inputHidden);
	}
	document.body.appendChild(frm);
	//document.location(frm);
	frm.submit();
}

// ----------------------------------------------------------------------------------------
// Usada na exclusao dos registros
function excluir(url)
{
   frm = document.form;
	cont = 0;
	if (frm['fldId[]'].length == undefined)
	{
		if (frm['fldId[]'].checked)
			cont++;
	}
	else
	{
		for (i=0; i < frm['fldId[]'].length; i++)
		{
			if (frm['fldId[]'][i].checked)
				cont++;
		}
	}
	if (cont > 0)
	{
		if (confirm('Deseja realmente excluir os itens selecionados?'))
		{
			frm.action = url;
			frm.submit();
		}
	}
	else
	{
		alert('Selecione algum registro para exclusão!!!');
	}
}

