﻿function showHide(id)
{
  var div = document.getElementById(id);
  
  if (div != null)
  {
    if (div.style.display == "none")
        div.style.display = "block"
    else
        div.style.display = "none";
  }
}


function IsNumeric()
{
    //onkeydown="event.returnValue = IsNumeric();"
    //alert(event.keyCode);
	
    if ("[8][9][13][16][17][18][20][27][33][34][35][36][37][38][39][40][45][46]".indexOf("[" + event.keyCode + "]") != -1)
        return true;
    
	if (
        (event.keyCode > 47 && event.keyCode < 58)
         ||
       (event.keyCode >= 96 && event.keyCode < 106)
    ) return true;
	return false;
}
function IsCurrency()
{

    if ("[8][9][13][16][17][18][20][27][33][34][35][36][37][38][39][40][45][46][188]".indexOf("[" + event.keyCode + "]") != -1)
        return true;
    
	if (
        (event.keyCode > 47 && event.keyCode < 58)
         ||
       (event.keyCode >= 96 && event.keyCode < 106)
    ) return true;
	return false;
}
function currencyFormat(fld, milSep, decSep, 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;
	
	if (whichCode == 13) 
		return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	
	if (whichCode == 0 && fld.value == '') 
	{
		fld.value = '0,00'
		return true;  // tab with null value
	}
	
	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 = '0'+ decSep + '0' + aux;
	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 moneda(cur,len)
{
   n='__0123456789';
   d=cur.value;
   l=d.length;
   r='';
   
   if (l > 0)
   {
    z=d.substr(0,l-1);
    s='';
    a=2;
    for (i=0; i < l; i++)
    {
        c=d.charAt(i);
        if (n.indexOf(c) > a)
        {
            a=1;
            s+=c;
    }
    }
    l=s.length;
    t=len-1;
    if (l > t)
    {
        l=t;
        s=s.substr(0,t);
    }
    if (l > 2)
    {
        r=s.substr(0,l-2)+','+s.substr(l-2,2);
    }
    else
    {
        if (l == 2)
        {
            r='0,'+s;
        }
        else
        {
            if (l == 1)
            {
                r='0,0'+s;
            }
        }
    }
    if (r == '')
    {
        r='0,00';
    }
    else
    {
        l=r.length;
        if (l > 6)
        {
            j=l%3;
            w=r.substr(0,j);
            wa=r.substr(j,l-j-6);
            wb=r.substr(l-6,6);
            if (j > 0)
            {
                w+='.';
            }
            k=(l-j)/3-2;
            for (i=0; i < k; i++)
            {
                w+=wa.substr(i*3,3)+'.';
            }
            r=w+wb;
        }
    }
   }
   if (l == 0)
   {
    cur.value='0,00';
   } 
   else
   {
   if (r.length <= len)
   {
    cur.value=r;
    
   }
   else
   {
    cur.value=z;
   }
   }
   return 'bom';
}



function clear_input(element){
 if(element.value == '0,00'){
        element.value = "";
        //element.onfocus = null;
     }
    
}		

var nav4 = window.Event ? true : false;
function acceptNum(evt){ 
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
var key = nav4 ? evt.which : evt.keyCode; 
return (key <= 13 || (key >= 48 && key <= 57));
}


function Formatotelefone(F)
{
var PH = F.value;
PH = PH.replace("(","");
PH = PH.replace(")","");
PH = PH.replace("-","");
PH = PH.replace(" ","");

      if(PH.length < 2)
      {
            F.value = "(" + PH;
      }
      if(PH.length > 2 && PH.length <= 5)
      {
            F.value = "(" + PH.substring(0,2) + ") " + PH.substring(2,6);
      }
      if(PH.length > 5)
      {
            F.value = "(" + PH.substring(0,2) + ") " + PH.substring(2,6) + "-" + PH.substring(6,10);
      }
}