﻿function iluminaPainel(obj)
{
    obj.style.backgroundColor = "#abcdef";
}

function EscurecePainel(obj)
{
    obj.style.backgroundColor = "#fff";
}

function setacampo(campo) 
{ 
    campo.selectionStart = campo.value.length; 
    campo.selectionEnd = campo.value.length; 
} 

function isSelected(campo) {
	var selectedText = document.selection;
	if (selectedText.type == 'Text') {
		var newRange = selectedText.createRange();		
		if (campo.value.length = newRange.text.length)
		   return true;
		else
		   return false;		
	} else 
	  return false;
}

function FormataNTC(oCampo,teclapres){
    var
    vr = oCampo.value;
    tam = vr.length;
   
	if(navigator.appName.indexOf('Internet Explorer')>0){ // Internet Explorer
	    setacampo(oCampo);
		nTecla = teclapres.keyCode;
		if (isSelected(oCampo)) {
			teclapres.returnValue = true;
			return;    
		}  
	} else {
		nTecla = teclapres.which;
	}   
    
    if (tam == 13){
       teclapres.returnValue = false;
       return;
    }

    if ((nTecla < 48) || (nTecla > 57))
        teclapres.returnValue = false;
    else {
	   if (tam == 0)
		   oCampo.value = vr + '(';
	   else
       if (tam == 3)
           oCampo.value = vr + ')';
       else
       if (tam == 8)
           oCampo.value = vr + '-';
    }
}

function FormataData(oCampo,teclapres){
    var
    vr = oCampo.value;
    tam = vr.length;
    
    if (vr.length >= 10)
    {
        teclapres.returnValue = false;
        return;
    }
        
	if(navigator.appName.indexOf('Internet Explorer')>0){ // Internet Explorer
		nTecla = teclapres.keyCode;
		setacampo(oCampo);
		if (isSelected(oCampo)) {
			teclapres.returnValue = true;
			return;    
		}  
	} else {
		nTecla = teclapres.which;
	}

    if ((nTecla < 48) || (nTecla > 57))
        teclapres.returnValue = false;
    else
       if ( (tam == 2) || (tam == 5) )
           oCampo.value = vr + '/';
}

function FormataNumero(oCampo,teclapres){
   if ((teclapres.keyCode < 48) || (teclapres.keyCode > 57))
        teclapres.returnValue = false;
}


function FormataDecimal(oCampo,teclapres){
    var tam = oCampo.value.length;
    var virg = oCampo.value.indexOf(',');
    var ie = (typeof window.ActiveXObject != 'undefined');
    if (ie)
      code = event.keyCode;
    else   
      code = event.which;

    if ((code == 44) && (tam > 0) && virg < 1) {
        teclapres.returnValue = true;
        return;
    }
    if (((tam - virg) == 3) && (virg > 0)) {
        teclapres.returnValue = false;
        return;
    }
    if ((code < 48) || (code > 57))
        teclapres.returnValue = false;

}

function FormataMesAno(oCampo,teclapres)
{
    var
    vr = oCampo.value;
    tam = vr.length;
    
    if (vr.length >= 10)
    {
        teclapres.returnValue = false;
        return;
    }
        
	if(navigator.appName.indexOf('Internet Explorer')>0){ // Internet Explorer
		nTecla = teclapres.keyCode;
		setacampo(oCampo);
		if (isSelected(oCampo)) {
			teclapres.returnValue = true;
			return;    
		}  
	} else {
		nTecla = teclapres.which;
	}

    if ((nTecla < 48) || (nTecla > 57))
        teclapres.returnValue = false;
    else
       if (tam == 2)
           oCampo.value = vr + '/';
}


/// Formata o campo valor
//
    function formataValor(campo) 
    {
        campo.value = filtraCampo(campo);
        vr = campo.value;
        tam = vr.length;

    if ( tam <= 2 ){ 
             campo.value = vr ; }
         if ( (tam > 2) && (tam <= 5) ){
             campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
         if ( (tam >= 6) && (tam <= 8) ){
             campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
         if ( (tam >= 9) && (tam <= 11) ){
             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) ){
             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 <= 18) ){
             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 ) ;}         
             
    }
    // limpa todos os caracteres especiais do campo solicitado
    function filtraCampo(campo)
    {
        var s = "";
        var cp = "";
        vr = campo.value;
        tam = vr.length;
        for (i = 0; i < tam ; i++) {  
            if (vr.substring(i,i + 1) != "/" && vr.substring(i,i + 1) != "-" && vr.substring(i,i + 1) != "."  && vr.substring(i,i + 1) != "," ){
                 s = s + vr.substring(i,i + 1);}
        }
        s=s.replace(/\D/g,"");
        
        campo.value = s;
        return cp = campo.value
    }
//   
///

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
