function checkNumber(field, e)
{
  if(e.keyCode == 13)
  		x=0;		
  else if(e.keyCode <= 45 || e.keyCode > 57)
        e.returnValue = false;
}

function CheckOnlyDigits(field,e)
{
	var tecla = e.KeyCode;
    
    if(tecla == null || tecla == 'undefined')
        tecla = e.charCode;

    if(tecla == 13)
    {
        x=0;
    }  

	//44=,
	if(document.all)
    {
		if((e.keyCode < 48 || e.keyCode > 57) && e.keyCode != 44)
        {
            e.returnValue = false;
        }
        
        if(e.keyCode == 44 && field.value.indexOf(",") > -1)
        	e.returnValue = false;
    }
    else
    {
		if((tecla < 48 || tecla > 57)&&(tecla != 0 && tecla != 44))
        {
            e.returnValue = false;
            return false;
        }
        
        if(tecla == 44 && field.value.indexOf(",") > -1)
        {
        	e.returnValue = false;
        	return false;
		}
    } 
}

function CheckDigits(field,e)
{
   var tecla = e.KeyCode;

   if(tecla == null || tecla == 'undefined')
      tecla = e.charCode;

    if(tecla == 13)
    {
       x=0;
    }  
    //aceita a letra 'x'
     if(document.all)
     {
         if(e.keyCode != 88 && e.keyCode != 120)
         {
           if(e.keyCode <= 45 || e.keyCode > 57)
           {
             e.returnValue = false;
           }
         }  
     }
     else
     {
        if(tecla != 88 && tecla != 120)
        {
            if((tecla <= 45 || tecla > 57)&&(tecla != 0))
            {
                e.returnValue = false;
                return false;
            }
        } 
     } 
 }
 
 
 //Verifica os dígitos e verifica se o banco é HSBC
function verifyDigitCont2(field,fieldBank,e)
{
     var bank = fieldBank.options[fieldBank.selectedIndex].value;
     var tecla = e.KeyCode;

   if(tecla == null || tecla == 'undefined')

      tecla = e.charCode;
    if(tecla == 13)
    {
       x=0;
    }  
    //aceita a letra 'x'

     if(document.all)
     {
         if (bank != "399" && bank != "41")
           if (field.value.length > 0)
             e.returnValue = false;
         
         if(e.keyCode != 88 && e.keyCode != 120)
         {
   
           if(e.keyCode <= 45 || e.keyCode > 57)
           {
             e.returnValue = false;
           }

         }  
     }

     else
     {
       if (bank != "399" && bank != "41")
         if (field.value.length > 0)
          {
           if(tecla != 0)
           {
             e.returnValue = false;
             return false;
           }
          }
          
        if(tecla != 88 && tecla != 120)
        {
            if((tecla <= 45 || tecla > 57)&&(tecla != 0))
            {
              e.returnValue = false;
              return false;
            }
        } 
     } 

}
 

function CheckDecimalSeparator(field, e)
{

    var code = e.charCode;
   if ((code == null)||(code=="undefined"))
    code = e.keyCode;
   if(code  == 13)
        x=0;	
    
    else if ((code != 0) && (code!=44) && (code!=46) && (code  <= 47 || code  > 57))
    {
        e.returnValue = false; 
        return false;
    }
    return true; 
}





    function visible()
    {       
        if($('divAdd').style.display == "none")
        {
            $('divAdd').style.display = "block";
            $('box_quote_seta').src = "../im/bt_setaup.gif";
            $('box_quote_label').innerHTML = "Clique para compactar";
        }
        else
        {
            $('divAdd').style.display = "none";
            $('box_quote_seta').src = "../im/bt_setadw.gif";
            $('box_quote_label').innerHTML = "Clique para detalhes";
        }
    }
   
    function responder(opt)
    {
    var obj = 'div_resp_' + opt;
    if($(obj).style.display == "none")
    {
            $(obj).style.display = "block";
            }
            else
            {   $(obj).style.display = "none";
                   
            }
    }
    
    //Função replicada justamente para não afetar as outras páginas que utlizam o método abaixo
    function showAndHideDatasTitular(indice, trDadosTit)
    {
        var field = "ctl00_ContentMid_checkConjunta" + indice;
        var tr = trDadosTit + indice;
        var cpf = "ctl00_ContentMid_txtCPF"+ indice;
        var nome = "ctl00_ContentMid_txtNome" + indice;
        var holder = "ctl00_ContentMid_checkHolderAccount_" + indice;
        
        if(document.getElementById(field).checked)
        {
            document.getElementById(tr).style.display = "block";
        }
        else
        {
            document.getElementById(tr).style.display = "none";
            document.getElementById(cpf).value = "";
            document.getElementById(nome).value = "";
            document.getElementById(holder).checked = false;
        } 
    }
    
    //Função replicada justamente para não afetar as outras páginas que utlizam o método abaixo
    function showAndHideDigitAgencyForUpdateData(indice)
    {
        var name  = "ctl00_ContentMid_ddlBanco" + indice;
        var div = "ctl00_ContentMid_divDigitoAgencia"+ indice;
        var div2 = "ctl00_ContentMid_divDigitoConta"+ indice;
        var txtDigitAccount = "ctl00_ContentMid_txtDigito" + indice;
        var field = document.getElementById(name);
        selectField = document.getElementById(name).selectedIndex;
        
        //Só exibe o digito da agência,quando for banco do Brasil.
        if (field.options[selectField].value == "1")
        {
            document.getElementById(div).style.display = "block";
        }
        else
        {
            document.getElementById(div).style.display = "none";
            document.getElementById("ctl00_ContentMid_txtDigitoAgencia" + indice).value = "";
        }      
      
        //Verifica se deve ou não exibir do dígito da conta
        if (field.options[selectField].value == "745" || field.options[selectField].value == "752")
        {
           document.getElementById(div2).style.display = "none";
           document.getElementById(txtDigitAccount).value = "";
        }
        else
        {
          document.getElementById(div2).style.display = "block";
        }
    }
    
    function showAndHideDigitAgency(indice)
    {
        var name  = "ctl00_ContentMid_ddlBanco" + indice;
        var field = document.getElementById(name);
        var td = "tdDigitoAgencia"+ indice;
        var td2 = "divDigitoConta"+ indice;
        var txtDigitAccount = "ctl00_ContentMid_txtDigito" + indice;
        selectField = document.getElementById(name).selectedIndex;
        
        //Só exibe o digito da agência,quando for banco do Brasil.
        if (field.options[selectField].value == "1")
        {
            document.getElementById(td).style.display = "block";
        }
        else
        {
            document.getElementById(td).style.display = "none";
            document.getElementById("ctl00_ContentMid_txtDigitoAgencia" + indice).value = "";
        }      
      
        //Verifica se deve ou não exibir do dígito da conta
        if (field.options[selectField].value == "745" || field.options[selectField].value == "752")
        {
           document.getElementById(td2).style.display = "none";
           document.getElementById(txtDigitAccount).value = "";
        }
        else
        {
          document.getElementById(td2).style.display = "block";
        }
    }    
       

 function showAlert()
 {
    var obj = $('alertbox');
    obj.style.display = "";
    obj.style.height = screen.height - 180;
    obj.style.width = document.body.clientWidth + 10;
}
 function showAlert2()
 {
    var obj = $('alertbox_email');
    obj.style.display = "";    
}
 function closeBox2()
 {
    var obj = $('alertbox_login');
    obj.style.display = "none";
    
    var obj2 = $('alertbox_contato');
    obj2.style.display = "block";    
}
function closeBox()
{
    var obj = $('alertbox');
    obj.style.display = "none";
}

  function autoComplete (field, select, property, forcematch) {
	    var Ativos = new Array();
        Ativos.push("ACES3");
	    Ativos.push("ACES4");
	    Ativos.push("Acesita");
	    Ativos.push("AES Tiete");
	    Ativos.push("GETI3");
	    Ativos.push("GETI4");
	    Ativos.push("BARDELLA");
	    Ativos.push("BDLL3");
	    Ativos.push("BDLL4");
	    Ativos.push("DSUL3");
	    Ativos.push("EMBRATEL PAR");
	    Ativos.push("EBTP3");
	    Ativos.push("EBTP4");
	    Ativos.push("GLOBEX");
	    Ativos.push("GLOB3");
	    Ativos.push("GOAU4");
	    Ativos.push("ITSA4");
	    Ativos.push("PTIP4");
	    Ativos.push("TMAR5");
	    Ativos.push("USIN5");
	    Ativos.push("VALE3");
	    Ativos.push("Vale Rio Doce");
	    Ativos.push("GLOB4");
	    Ativos.push("IBOV");
	    Ativos.push("Bovespa");	    
	    Ativos.push("Indice Bovespa");	
        Ativos.push("ITAUBANCO");
        Ativos.push("CELESC");
        Ativos.push("CLSC3");
        Ativos.push("CLSC5");
        Ativos.push("ITAU3");
        Ativos.push("ITAU4");
	    Ativos.push("PETR4");
	    Ativos.push("TELB4");	    
	    Ativos.push("Petrobras ON");
	    Ativos.push("Petrobras PN");
	    var found = false;	    
	    for (var i = 0; i < Ativos.length; i++) {
	        if (Ativos[i].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
		        found=true; break;
		    }
	    }	    
	    if (field.createTextRange) {
		    if (forcematch && !found) {
			    field.value=field.value.substring(0,field.value.length-1); 
			    return;
			}
		    var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		    if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			    var r1 = field.createTextRange();
			    var oldValue = r1.text;
			    var newValue = found ? Ativos[i] : oldValue;
			    if (newValue != field.value) {
				    field.value = newValue;
				    var rNew = field.createTextRange();
				    rNew.moveStart('character', oldValue.length) ;
				    rNew.select();
				}
			}
		}
	}

//Pop-up Envie para um amigo
function envieAmigo(){
	janela=window.open("../../common/envieAmigoHelp.aspx","","width=377,height=399,scrollbars=no");
	//interceptacao de erro na abertura da janela
	text = "Desative seu bloqueador de pop-ups!";
	if(janela == null) { alert(text); return; }
	//fim
}
function envieAmigoGW(){
	janela=window.open("../../common/envieAmigo.aspx","","width=377,height=399,scrollbars=no");
	//interceptacao de erro na abertura da janela
	text = "Desative seu bloqueador de pop-ups!";
	if(janela == null) { alert(text); return; }
	//fim
}
//Janela Gráfico Apligraf
var grafico;
function OpenGraficoApligraf(symbol)
{
	try
	{
		if (grafico.closed)
			grafico = null;
	}	
	catch (err)
	{
		grafico = null;
	}

	
    if (grafico == null)
    {
        var largura = screen.width-100;
        var altura = screen.height-200;
        grafico = window.open('https://secure.apligraf.com.br/webfeed/win/graficos001.php?ativo=ibov&estudo=ifr&popup=sim&ativo=' + symbol,'grafico','width=' + largura + ',height=' + altura + ',status=1,left=50,top=100,resizable=1');
    }
    else
    {
       grafico.location = 'https://secure.apligraf.com.br/webfeed/win/graficos001.php?ativo=ibov&estudo=ifr&popup=sim&ativo=' + symbol;
       grafico.focus();
    }
}

function Trim(str)
{
   return str.replace(/^\s+|\s+$/g,"");
}

function blocksCharactersSpaceAndCommaAndSemicolon(e)
{
    var code = e.charCode;
	if(code == null || code == 'undefined')
	{
		code = e.keyCode;
	}
	if(code == 13)
    {
		x=0;
    }  
	else if ((code != 0) && (code == 32 || code == 44 || code == 59))
	{
		e.returnValue = false;
		return false;
	}
	
	return true; 
}


/*******************Validação de campos************************/

//Validação de email
function validateEmailField(field)
 {
	if (Trim(field.value) != "")
	{
		var reg = /^([\w\-]+\.)*[\w\- ]+@([\w\- ]+\.)+([\w\-]{2,3})$/;
		if (! reg.test(field.value))
		{
			return false;
		}
		return true;
	}
	else
	{
		return false;
	}
 }  
 
 //Validação do campo de telefone
function checkphone(field)
{
	if(field.value != "")
	{
		var reg = /^\d{2}-\d{4}-\d{4}$/;
		if (! reg.test(field.value))
		{
			return false;
		}
		return true;
		
	}
	else
	{
		 return false;
	}
}
 
 //Validação do campo de CEP
function checkCEP(field)
{
	if(field.value != "")
	{
		var reg = /^\d{5}\-\d{3}$/;
		if (! reg.test(field.value))
		{
			return false;
		}
		return true;
	}
	else
	{
		 return false;
	}
} 

 //Validação do campo data
function checkDate(field)
{
	if(field.value != "")
	{
		var reg = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
		if (! reg.test(field.value))
		{
			return false;
		}
		return true;
	}
	else
	{
		 return false;
	}
} 

function IsValidMonetaryValue(decimalValue)
{
	var isValid = false;
	var reg = /^[+-]?((\d+|\d{1,3}(\.\d{3})+)(\,\d*)?|\,\d+)$/;
	
	if(reg.test(decimalValue))
	{
	    decimalValue = decimalValue.replace(".","").replace(",",".");
	    if(decimalValue >= 0.01)
	    {
	        isValid = true;
	    }
	}
	
	return isValid;
}


/*******************Mascara para os campos************************/

//Formata o campo de telefone - "11-1111-1111"
function maskPhone(o,f)
{
    v_obj=o;
    v_fun=f;
    setTimeout("execMask()",1);
}

function returnPhoneMask(v)
{
    v=v.replace(/\D/g,"");
    v=v.replace(/^(\d\d)(\d)/g,"$1-$2"); 
    v=v.replace(/(\d{4})(\d)/,"$1-$2"); 
    return v;
}

//Formata o campo de CEP - "11111-111"
function maskCEP(o,f)
{
    v_obj=o;
    v_fun=f;
    setTimeout("execMask()",1);
}
function returnCEPMask(v)
{
    v=v.replace(/\D/g,"");
    v=v.replace(/(\d{5})(\d)/,"$1-$2") ;
    return v;
}


//Formata o campo de CEP - "11111-111"
function maskDate(o,f)
{
    v_obj=o;
    v_fun=f;
    setTimeout("execMask()",1);
}
function returnDateMask(v)
{
    v=v.replace(/\D/g,"");
    v=v.replace(/^(\d\d)(\d)/g,"$1/$2"); 
    v=v.replace(/(\d{2})(\d)/,"$1/$2");   
    return v;
}


//Execução das mascaras
function execMask()
{
    v_obj.value=v_fun(v_obj.value);
}

//Pega os dados de acordo com o cep
function RetrieveAddressFromZip(field)
{
	var url = '../../../Ajax/AtualizacaoCadastral_Functions.aspx';
	
	//Monta o sufixo de acordo com o tipo da informação: residencial, comercial e alternativo
	var fieldName = field.id;
	var pars = 'function=RetrieveAddressFromZip&zip=' + field.value + '&suffix=' +  fieldName.substring(20, fieldName.length - 7);
	new Ajax.Request(url,{method: 'post', parameters: pars, onComplete: LoadZip});
}

//Recebe as informações e preenche os campos de acordo com os valores do cep
function LoadZip(result)
{
	var args = result.responseText.split(';');
	for (var i = 0; i < args.length ; i++)
	{
		var content = args[i].split('=');
		
		//Se for do tipo dropdown de estados, deve selecionar o estado adequado
		var pattern = new RegExp("State");
		if (pattern.test(content[0]))
		{
			var field = document.getElementById('ctl00_ContentMid_' + content[0]);
			field.selectedIndex = content[1];
		}
		else
		{
			document.getElementById('ctl00_ContentMid_' + content[0]).value = content[1];
		}
	}
}
function LopesFilho(type, symbol, area, path)
{	    
	var vWidth = 800;
	var vHeight = 600;
	if(area == 'cotacao')
	    vWidth = 860;
	    
	window.open(path + 'ondeinvestir/LopesFilho.aspx?type=' + type + '&symbol=' + symbol + '&area=' + area, 'LopesFilho',
	'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,menubar=no,personalbar=no,width=' + vWidth + ',height=' + vHeight + ',left=2,top=10');
} 

function VerifyDropDownListItemIsSelected(drop, value)
{
	var field = document.getElementById(drop);
	var content = field.options[field.selectedIndex].value;
	var ret = false;
	
	if(content == value)
	{
	    ret = true;
	}
	return ret;
} 

function GoToLopesFilho(drop, value, type, area, TxtSymbol, path)
{
   
   var ret = VerifyDropDownListItemIsSelected(drop, value);
   var symbol = document.getElementById(TxtSymbol).value;
   
   if(ret)
    LopesFilho(type, symbol, area, path);
   
   return true;
}

 
 

