/*
  Name: 		   scripts.js
  Copyright:   Fundação Abrinq
  Author: 	   Marcelo Martins
  Data: 		   02/08/2006
	Atualizado:  22/08/2007
  Description: Funções em JavaScript para rodar no cliente.
*/

//------------------------------------------------------------------------------

function abre_janela(url,config){
				  var urls = '';
				  var conf = '';
				  urls = url;
				  conf = config;
				  window.open(urls,'Jal',conf);
				}

function confirma(){
				 var frase = 'Lembre-se que após validar as informações, o município somente terá acesso às opções de impressão.\nDeseja realmente validar os dados ?';
				 if (!confirm(frase)) return false;
				 }

function alerta(msg){
				 if (!alert(msg)) return false;
				 }
				 
function key(){
				 theKey = event.keyCode;
				 if((theKey < 48) || (theKey > 57))
						 event.returnValue = false;
				 }
				 
/*
	Exemplo (key): aplicar no atributo ( onKeyPress="key();" )
*/
	
function isMail(mailField){
  strMail = mailField.value;
  var re = new RegExp;
  re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var arr = re.exec(strMail);
  if (arr == null)
  		return(false);
  else
  		return(true);
			}
			
function FormataTel(fld, e){
				 		var key = '';
				 		var len = 0;
						var strCheck = '0123456789';
						var whichCode = (window.Event) ? e.which : e.keyCode;
						if (whichCode == 13) return true;
								key = String.fromCharCode(whichCode);
								len = fld.value.length;
								if (len > 8) return false;
										if (len == 4){
    										if (key == '-') {
        										return;
     										} else {
        										if (strCheck.indexOf(key) >= 0) {
           											fld.value += '-' + key;
        										}
 													return false;
 												}
 										}
										if (strCheck.indexOf(key) == -1)
								return false;
						}
						
/*
Exempllo (FormataTel): aplicar no atributo ( onKeyPress="return(FormataTel(this,event));" )
*/

function MascaraMoeda(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;
    if (whichCode == 13) 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;
}

/*
Exemplo (MascaraMoeda): aplicar no atributo ( onKeyPress="return(MascaraMoeda(this,'.',',',event));" )
*/

