/**
 * moeda
 * 
 * @abstract Classe que formata de desformata valores monetários em float e formata valores 
 * de float em moeda.
 * 
 * @author anselmo
 * 
 * @example 
 * 		moeda.formatar(1000) 
 *      	>> retornar 1.000,00
 * 		moeda.desformatar(1.000,00) 
 * 			>> retornar 1000
 * 
 * @version 1.0
 **/
 var moeda = {
 	
	/**
	 * retiraFormatacao
	 * 
	 * Remove a formatação de uma string de moeda e retorna um float
	 * 
	 * @param {Object} num
	 */
	 desformatar: function(num){
	   num = num.replace(".","");
	
	   num = num.replace(",",".");
	
	   return parseFloat(num);
	},

	/**
	 * formatar
	 * 
	 * Deixar um valor float no formato monetário
	 * 
	 * @param {Object} num
	 */
	formatar: function(num){
	   x = 0;
	
	   if(num<0){
	      num = Math.abs(num);
	      x = 1;
	   }
	
	   if(isNaN(num)) num = "0";
	      cents = Math.floor((num*100+0.5)%100);

	   num = Math.floor((num*100+0.5)/100).toString();
	
	   if(cents < 10) cents = "0" + cents;
	      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	         num = num.substring(0,num.length-(4*i+3))+'.'
	               +num.substring(num.length-(4*i+3));
	
	   ret = num + ',' + cents;
	
	   if (x == 1) ret = ' - ' + ret;return ret;
	},
	
	/**
	 * arredondar
	 * 
	 * @abstract Arredonda um valor quebrado para duas casas decimais.
	 * 
	 * @param {Object} num
	 */
	arredondar: function(num){
		return Math.round(num*Math.pow(10,2))/Math.pow(10,2);
	}
 }

//AJAX

try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

//VALOR PRODUTO
function PesquisaPreco(produto){

    //limpa o select
    var c=document.getElementById("objNumpreco")
    //while(c.options.length>0)c.options[0]=null
    c.value ="Aguarde..."

    //Monta a url com a uf
    xmlhttp.open("GET", "produtos.php?produto="+produto,true);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //limpa o select
            var c=document.getElementById("objNumpreco")
            //while(c.options.length>0)c.options[0]=null
            //Transforma a lista de cidades JSON em Javascript
            var aCidades=eval((xmlhttp.responseText))
            //popula o select com a lista de cidades obtida
            for(var i=0;i<aCidades.length;i++){
                aCidades[i]=unescape(aCidades[i])
                c.value = moeda.formatar(aCidades[i])
                //c.options[c.options.length]=new Option(aCidades[i],aCidades[i]) PARA COMBO
            }
        }
    }
    xmlhttp.send(null)
}





//MÁSCARA DE VALORES

function mascara(objeto, sMask, evtKeyPress) {
     var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

     if(document.all) { // Internet Explorer
       nTecla = evtKeyPress.keyCode; }
     else if(document.layers) { // Nestcape
       nTecla = evtKeyPress.which;
     }

     sValue = objeto.value;

     // Limpa todos os caracteres de formatação que
     // já estiverem no campo.
     sValue = sValue.toString().replace( "-", "" );
     sValue = sValue.toString().replace( "-", "" );
     sValue = sValue.toString().replace( ".", "" );
     sValue = sValue.toString().replace( ".", "" );
     sValue = sValue.toString().replace( "/", "" );
     sValue = sValue.toString().replace( "/", "" );
     sValue = sValue.toString().replace( ":", "" );
     sValue = sValue.toString().replace( ":", "" );
     sValue = sValue.toString().replace( "(", "" );
     sValue = sValue.toString().replace( "(", "" );
     sValue = sValue.toString().replace( ")", "" );
     sValue = sValue.toString().replace( ")", "" );
     sValue = sValue.toString().replace( " ", "" );
     sValue = sValue.toString().replace( " ", "" );
     fldLen = sValue.length;
     mskLen = sMask.length;

     i = 0;
     nCount = 0;
     sCod = "";
     mskLen = fldLen;

     while (i <= mskLen) {
       bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
       bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

       if (bolMask) {
         sCod += sMask.charAt(i);
         mskLen++; }
       else {
         sCod += sValue.charAt(nCount);
         nCount++;
       }

       i++;
     }

     objeto.value = sCod;

     if (nTecla != 8) { // backspace
       if (sMask.charAt(i-1) == "9") { // apenas números...
         return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
       else { // qualquer caracter...
         return true;
       } }
     else {
       return true;
     }

   // Submite o formulário
   if ((window.event ? event.keyCode : event.which) == 13) { document.getElementById('$formulario').submit(); }	 
	 
   }
