function checkAll(id,name,check){
	var frm = document.getElementById(id);
	var els = frm.elements; 
	for(i=0; i<els.length; i++){
		element = els[i];
		if(element.type == 'checkbox' && element.name.indexOf(name) !== -1){
			if(check === null){
				element.checked = element.checked ? false : true;
			} else {
				element.checked = check;
			}
		}
	}
}

/*#########################################################
 Verifica se existe apenas números
   @param field -> nomedoform.nomedocampo;
   @param nome -> nome visual do campo
   @param nMax -> Numer Máximo aceito
   Usado com onKeyUp
##########################################################*/	
function maxNumero(field,nMax){
	expr = field.value;
	Max = nMax;
	
	if(expr > Max){ field.value = ''; }
}

/*#########################################################
	Máscara de Cep
    onkeypress="return(mascaraCep(this,event));"
#########################################################*/
function mascaraCep(Campo, e) { 
    var key = ''; 
    var len = 0; 
    var strCheck = '0123456789'; 
    var aux = ''; 
    var whichCode = (window.Event) ? e.which : e.keyCode; 
    
    if (whichCode == 13 || whichCode == 8 || whichCode == 0){ 
        return true;  /* Enter backspace ou FN qualquer um que não seja alfa numerico */
    } 
    key = String.fromCharCode(whichCode); 
    if (strCheck.indexOf(key) == -1){ 
        return false;  /* NÃO E VALIDO */
    } 
    
    aux =  Cep_Remove_Format(Campo.value); 
    
    len = aux.length; 
    if(len>=8){ 
        return false;    /*impede de digitar um cep maior que 10  */
    } 
    aux += key; 
     
    Campo.value = Cep_Mont_Format(aux); 
    return false; 
} 

function  Cep_Mont_Format(Cep){ 
    var aux = len = ''; 
 						    
    len = Cep.length; 
    if(len<=9){ 
        tmp = 5; 
    }else{ 
        tmp = 5; 
    } 
    
    aux = ''; 
    for(i = 0; i < len; i++){ 
        aux += Cep.charAt(i); 
        if(i+1==tmp){ 
            aux += '-'; 
        } 
    } 
    return aux ; 
} 

function  Cep_Remove_Format(Cep){ 
    var strCheck = '0123456789'; 
    var len = i = aux = ''; 
    len = Cep.length; 
    for(i = 0; i < len; i++){ 
        if (strCheck.indexOf(Cep.charAt(i))!=-1){ 
            aux += Cep.charAt(i); 
        } 
    } 
    return aux; 
}

/*#########################################################
	Máscara de Moeda
    onkeypress="return(mascaraMoeda(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; 
}

/*#########################################################
	Máscara de hora
    onkeypress="return(mascaraHora(this,event));"
#########################################################*/
function mascaraHora(Campo, e) { 
    var key = ''; 
    var len = 0; 
    var strCheck = '0123456789'; 
    var aux = ''; 
    var whichCode = (window.Event) ? e.which : e.keyCode; 
    
    if (whichCode == 13 || whichCode == 8 || whichCode == 0){ 
        return true;  /* Enter backspace ou FN qualquer um que não seja alfa numerico */
    } 
    key = String.fromCharCode(whichCode); 
    if (strCheck.indexOf(key) == -1){ 
        return false;  /* NÃO E VALIDO */
    } 
    
    aux =  hora_remove_format(Campo.value); 
    
    len = aux.length; 
    if(len>=4){ 
        return false;    /* impede de digitar um telefone maior que 10 */
    } 
    aux += key; 
     
    Campo.value = hora_mont_format(aux); 
    return false; 
} 

function  hora_mont_format(Data){ 
    var aux = len = ''; 
 						    
    len = Data.length; 
    if(len<=9){ 
        tmp = 5; 
    }else{ 
        tmp = 5; 
    } 
    
    aux = ''; 
    for(i = 0; i < len; i++){ 
        if(i==0){ 
            aux = ''; 
        } 
        aux += Data.charAt(i); 
        if(i+1==2){ 
            aux += ':'; 
        } 
    } 
    return aux ; 
} 

function  hora_remove_format(Data){ 
    var strCheck = '0123456789'; 
    var len = i = aux = ''; 
    len = Data.length; 
    for(i = 0; i < len; i++){ 
        if (strCheck.indexOf(Data.charAt(i))!=-1){ 
            aux += Data.charAt(i); 
        } 
    } 
    return aux; 
}

/*#########################################################
	Aceita Campos numéricos apenas
    onkeypress="return(campoNumerico(this,event));"
#########################################################*/
function campoNumerico(Campo, e){ 
 	var key = ''; 
    var len = 0;  
    var strCheck = '0123456789'; 
    var aux = Campo; 
    var whichCode = (window.Event) ? e.which : e.keyCode; 
    
    if (whichCode == 13 || whichCode == 8 || whichCode == 0){ 
        return true;  /* Enter backspace ou FN qualquer um que não seja alfa numerico */
    } 
    key = String.fromCharCode(whichCode); 
    if (strCheck.indexOf(key) == -1){ 
        return false;  /* NÃO E VALIDO */
    } 
    return aux; 
}

/*#########################################################
 Recebe o tipo de campo
 'num' -> numero, 'cep' -> cep, 'data' -> Data, 'hora' -> Hora
 'fone' -> Telefone, 'cpfcnpj' -> Cpf/CNPJ, 'letra' -> Letras
 onkeyup="tipoCampo('num', this);
#########################################################*/
function tipoCampo(tipo,obj){
    var str;

    if(tipo == 'num') /* campos numéricos */
        str = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZçÇáéíóúÁÉÍÓÚàÀâêîôûÂÊÎÔÛãõÃÕäëïöüÄËÏÖÜ@*.,;:~!?/|\\'\\\"<>()[]{}&%#-_=+"; 
    if(tipo == 'hora') /* campos numéricos */
        str = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZçÇáéíóúÁÉÍÓÚàÀâêîôûÂÊÎÔÛãõÃÕäëïöüÄËÏÖÜ@*.,;~!?/|\\'\\\"<>()[]{}&%#-_=+"; 
    if(tipo == 'numd') /* campos com números fracionados */ 
        str = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZçÇáéíóúÁÉÍÓÚàÀâêîôûÂÊÎÔÛãõÃÕäëïöüÄËÏÖÜ@*;:~!?/|\\'\\\"<>()[]{}&%#-_=+"; 
    else if(tipo == 'data') /* campos de data  */
        str = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZçÇáéíóúÁÉÍÓÚàÀâêîôûÂÊÎÔÛãõÃÕäëïöüÄËÏÖÜ@*.,;:~!?|\\'\\\"<>()[]{}&%#-_=+"; 
    else if(tipo == 'cep') /* campos de cep */
        str = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZçÇáéíóúÁÉÍÓÚàÀâêîôûÂÊÎÔÛãõÃÕäëïöüÄËÏÖÜ@*,;:~!?/|\\'\\\"<>()[]{}&%#_=+"; 
    else if(tipo == 'cpfcnpj') /* campos de cnpj e cpf */
        str = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZçÇáéíóúÁÉÍÓÚàÀâêîôûÂÊÎÔÛãõÃÕäëïöüÄËÏÖÜ@*,;:~!?|\\'\\\"<>()[]{}&%#_=+"; 
    else if(tipo == 'fone') /* campos de telefone */
        str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZçÇáéíóúÁÉÍÓÚàÀâêîôûÂÊÎÔÛãõÃÕäëïöüÄËÏÖÜ@*.,;:~!?/|\\'\\\"<>[]{}&%#_=+"; 
    else if(tipo == 'letra') /* campos alfabéticos */
        str = "1234567890"; 

    if(obj.value != ''){ 
        tam = str.length; 
        for(x=0;x<tam;x++){ 
            if(obj.value.indexOf(str.substr(x,1)) != -1){ 
                obj.value = obj.value.substr(0,obj.value = '') 
                break; 
            } 
        } 
    } 
}

/*#########################################################
	Máscara de telefone
    onkeypress="return(TelefoneFormat(this,event));"
#########################################################*/
function mascaraTelefone(Campo, e) {
    var key = '';
    var len = 0;
    var strCheck = '0123456789';
    var aux = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    
    if (whichCode == 13 || whichCode == 8 || whichCode == 0){	
        return true;  /* Enter backspace ou FN qualquer um que não seja alfa numerico */
    } 
    key = String.fromCharCode(whichCode); 
    if (strCheck.indexOf(key) == -1){ 
        return false;  /* NÃO E VALIDO */
    } 
    
    aux =  Telefone_Remove_Format(Campo.value); 
    
    len = aux.length; 
    if(len>=10){ 
        return false;    /* impede de digitar um telefone maior que 10 */
    } 
    aux += key; 
     
    Campo.value = Telefone_Mont_Format(aux); 
    return false; 
} 

function  Telefone_Mont_Format(Telefone){ 
    var aux = len = ''; 
    len = Telefone.length; 
    
    if(len<=9){ 
        tmp = 5; 
    }else{ 
        tmp = 6; 
    } 
    
    aux = ''; 
    for(i = 0; i < len; i++){ 
        if(i==0){ 
            aux = '('; 
        } 
        aux += Telefone.charAt(i); 
        if(i+1==2){ 
            aux += ')'; 
        } 
        
        if(i+1==tmp){ 
            aux += '-'; 
        } 
    } 
    return aux ; 
} 

function  Telefone_Remove_Format(Telefone){ 
    var strCheck = '0123456789'; 
    var len = i = aux = ''; 
    len = Telefone.length; 
    for(i = 0; i < len; i++){ 
        if (strCheck.indexOf(Telefone.charAt(i))!=-1){ 
            aux += Telefone.charAt(i); 
        } 
    } 
    return aux; 
}

/*#########################################################
	Máscara de data
    onkeypress="return(mascaraData(this,event));"
#########################################################*/
function mascaraData(Campo, e) { 
    var key = ''; 
    var len = 0; 
    var strCheck = '0123456789'; 
    var aux = ''; 
    var whichCode = (window.Event) ? e.which : e.keyCode; 
    
    if (whichCode == 13 || whichCode == 8 || whichCode == 0){ 
        return true;  /* Enter backspace ou FN qualquer um que não seja alfa numerico */
    } 
    key = String.fromCharCode(whichCode); 
    if (strCheck.indexOf(key) == -1){ 
        return false;  /* NÃO E VALIDO */
    } 
    
    aux =  data_remove_format(Campo.value); 
    
    len = aux.length; 
    if(len>=8){ 
        return false;    /* impede de digitar um telefone maior que 10 */
    } 
    aux += key; 
     
    Campo.value = data_mont_format(aux); 
    return false; 
} 

function  data_mont_format(Data){ 
    var aux = len = ''; 
    len = Data.length; 
    
    if(len<=9){ 
        tmp = 5; 
    }else{ 
        tmp = 5; 
    } 
    
    aux = ''; 
    for(i = 0; i < len; i++){ 
        if(i==0){ 
            aux = ''; 
        } 
        aux += Data.charAt(i); 
        if(i+1==2){ 
            aux += '/'; 
        } 
        
        if(i+2==tmp){ 
            aux += '/'; 
        } 
    } 
    return aux ; 
} 

function  data_remove_format(Data){ 
    var strCheck = '0123456789'; 
    var len = i = aux = ''; 
    len = Data.length; 
    for(i = 0; i < len; i++){ 
        if (strCheck.indexOf(Data.charAt(i))!=-1){ 
            aux += Data.charAt(i); 
        } 
    } 
    return aux; 
} 

function verifica_data (obj) { 
    dia = (obj.value.substring(0,2)); 
    mes = (obj.value.substring(3,5)); 
    ano = (obj.value.substring(6,10)); 
	 				
    situacao = ""; 
	/ verifica o dia valido para cada mes*/ 
	if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) { 
	    situacao = "falsa"; 
	} 
	
	/* verifica se o mes e valido */ 
	if (mes < 01 || mes > 12 ) { 
	    situacao = "falsa"; 
	} 
	
	/* verifica se e ano bissexto */ 
	if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
	    situacao = "falsa"; 
	} 
   			
    if (obj.value == "") { 
    	situacao = "falsa"; 
    } 
   
    if (situacao == "falsa") { 
    	obj.value=''; 
        obj.focus(); 
    } 
}
