﻿function ChangeCheckBoxState(id, checkState) 
{
    var cb = document.getElementById(id);
    if(cb != null)
    {
        cb.checked = checkState;
    }
}
// Adiciona a Lista de Favoritos
function AddBookMark()
{
    var bookmarkurl= "http://www.docuprint.com.br/LojaVirtual/";
    var bookmarktitle= "Loja Virtual Docuprint";
    window.external.AddFavorite(bookmarkurl,bookmarktitle); 
}



function ChangeAllCheckBoxStates(checkState)
{
    if(CheckBoxIDs != null) 
    {
        for(var i = 0; i < CheckBoxIDs.length; i++) 
            ChangeCheckBoxState(CheckBoxIDs[i], checkState);

    }
}

function ChangeHeaderAsNeeded()
{
    if(CheckBoxIDs != null)
    {
        for(var i = 1; i < CheckBoxIDs.length; i++)
        {
            var cb = document.getElementById(CheckBoxIDs[i]);
            if(!cb.checked)
            {
                ChangeCheckBoxState(CheckBoxIDs[0], false);
                return;
            }
        }
    }
    ChangeCheckBoxState(CheckBoxIDs[0], true);
}

function MascaraCEP(formato, keypress, objeto)
{
	campo = eval (objeto);
	if (formato=='CEP')
	{
		caracteres = '01234567890';
		separacoes = 1;
		separacao1 = '-';
		conjuntos = 1;
		conjunto1 = 5;
		if ((caracteres.search(String.fromCharCode (keypress))!=-1))
		     //campo.value.length < (conjunto1 + conjunto2 + 1))
		{
			if (campo.value.length == conjunto1) campo.value +=  separacao1;
		} 
		else  
			event.returnValue = false;
	}
}

function Numero(evtKeyPress)
{
	var nTecla = 0;
	if (document.all) {
		nTecla = evtKeyPress.keyCode;
	} else {
		nTecla = evtKeyPress.which;
	}
	if ((nTecla> 47 && nTecla <58)
	|| nTecla == 8 || nTecla == 127
	|| nTecla == 0 || nTecla == 9  // 0 == Tab
	|| nTecla == 13) { // 13 == Enter
		return true;
	} else {
		return false;
	}
} 			

function NumeroDecimal(campo, evtKeyPress)
{
	var nTecla = 0;
	var valor = campo.value.toString();
	
	if (document.all) 
	{
		nTecla = evtKeyPress.keyCode;
	} 
	else 
	{
		nTecla = evtKeyPress.which;
	}
	
	if ((nTecla> 47 && nTecla <58)
	    || nTecla == 8 || nTecla == 127
	    || nTecla == 0 || nTecla == 9  // 0 == Tab
	    || (nTecla == 44 && (valor.indexOf(",") < 0 && valor.length > 0))
	    || nTecla == 13) // 13 == Enter
	{ 
		return true;
	} 
	else 
	{
		return false;
	}
} 


function somenteNumeroDecimal(value, pTecla) 
{ 
	value = value.toString();
	if(pTecla == 44 && value.indexOf(",") < 0 && value != "") return true; 
	if(pTecla >= 48 && pTecla <= 57) return true; 
	return false; 
}

			  




function MascaraData(formato, keypress, objeto)
{
	campo = eval (objeto);
	if (formato=='Data')
	{
		caracteres = '01234567890';
		separacoes = 2;
		separacao1 = '/';
		separacao2 = '/';
		conjuntos = 2;
		conjunto1 = 2;
		conjunto2 = 5;
		if ((caracteres.search(String.fromCharCode (keypress))!=-1))
		     //campo.value.length < (conjunto1 + conjunto2 + 1))
		{
			if (campo.value.length == conjunto1) campo.value +=  separacao1;
			if (campo.value.length == conjunto2) campo.value +=  separacao2;
		} 
		else  
			event.returnValue = false;
	}
}

function LimpaFormato(objeto)
{
    //retira caracteres inválidos da string
    var campo = eval(objeto);
    var chars = new Array(".", "/", " ", "-", "(", ")", "R$");
    
    for(i=0; i < 7; i++)
    {
        while(campo.value.indexOf(chars[i]) >= 0)
            campo.value = campo.value.replace(chars[i], "");
    }
}

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;
}

function MascaraCPF(formato, keypress, objeto)
{
	campo = eval (objeto);
	if (formato=='CPF')
	{
		caracteres = '01234567890';
		separacoes = 3;
		separacao1 = '.';
		separacao2 = '.';
		separacao3 = '-';
		//separacao4 = '-';
		conjuntos = 4;
		conjunto1 = 3;
		conjunto2 = 7;
		conjunto3 = 11;
		//conjunto4 = 12;
		
		//123,456,789-09

		if ((caracteres.search(String.fromCharCode (keypress))!=-1))
		     //campo.value.length < (conjunto1 + conjunto2 + 1))
		{
			if (campo.value.length == conjunto1) campo.value +=  separacao1;
			if (campo.value.length == conjunto2) campo.value =  campo.value + separacao2;
			if (campo.value.length == conjunto3) campo.value =  campo.value + separacao3;
			//if (campo.value.length == conjunto4) campo.value =  campo.value + separacao4;
		} 
		else  
			event.returnValue = false;
	}
}

function MascaraCNPJ(formato, keypress, objeto)
{
	campo = eval (objeto);
	if (formato=='CNPJ')
	{
		caracteres = '01234567890';
		separacoes = 4;
		separacao1 = '.';
		separacao2 = '.';
		separacao3 = '/';
		separacao4 = '-';

		conjuntos = 4;
		conjunto1 = 2;
		conjunto2 = 6;
		conjunto3 = 10;
		conjunto4 = 15;
		
		//12.345.674/1234-54

		if ((caracteres.search(String.fromCharCode (keypress))!=-1))
		     //campo.value.length < (conjunto1 + conjunto2 + 1))
		{
			if (campo.value.length == conjunto1) campo.value +=  separacao1;
			if (campo.value.length == conjunto2) campo.value +=  separacao2;
			if (campo.value.length == conjunto3) campo.value +=  separacao3;
			if (campo.value.length == conjunto4) campo.value +=  separacao4;
		} 
		else  
			event.returnValue = false;
	}
}
						
function MascaraTEL(formato, keypress, objeto)
{
	campo = eval (objeto);
	if (formato=='TEL')
	{
		caracteres = '01234567890';
		separacoes = 3;
		separacao1 = '(';
		separacao2 = ') ';
		separacao3 = '-';
		conjuntos = 4;
		conjunto1 = 1;
		conjunto2 = 3;
		conjunto3 = 9;
		//conjunto4 = 4;

		if ((caracteres.search(String.fromCharCode (keypress))!=-1))
		     //campo.value.length < (conjunto1 + conjunto2 + 1))
		{
			if (campo.value.length == conjunto1) campo.value =  separacao1 + campo.value;
			if (campo.value.length == conjunto2) campo.value =  campo.value + separacao2;
			if (campo.value.length == conjunto3) campo.value =  campo.value + separacao3;
		} 
		else  
			event.returnValue = false;
	} 
	else if(formato=='TEL_SEM_DDD')
	{
		caracteres = '01234567890';
		separacoes = 1;
		separacao1 = '-';
		conjuntos = 1;
		conjunto1 = 4;
		
		if ((caracteres.search(String.fromCharCode (keypress))!=-1))
		{
			if (campo.value.length == conjunto1) campo.value =  campo.value + separacao1;
		} 
		else  
			event.returnValue = false;

		
	    
	}
	
}

function FormataTudo(formato, objeto) 
{
    LimpaFormato(objeto);
    var campo = eval(objeto);
    if(formato=="CEP") 
    {
        campo.value = campo.value.substr(0, 5) + "-" + campo.value.substr(5, 3);
    }
    else if(formato=="TEL")
    {
        if(campo.value.length == 8)
        {
            campo.value = campo.value.substr(0, 4) + "-" + campo.value.substr(4, 4);
        } 
        else if(campo.value.length == 10)
        {
            campo.value = "(" + campo.value.substr(0, 2) + ") " + campo.value.substr(2, 4) + "-" + campo.value.substr(6, 4);
        }
    }
    else if(formato=="CPF") 
    {
        campo.value = campo.value.substr(0, 3) + "." + campo.value.substr(3, 3) + "." +
                      campo.value.substr(6, 3) + "-" + campo.value.substr(9, 2);
    }
    else if(formato=="CNPJ") 
    {
        campo.value = campo.value.substr(0, 2) + "." + campo.value.substr(2, 3) + "." +
                      campo.value.substr(5, 3) + "/" + campo.value.substr(8, 4) + "-" + 
                      campo.value.substr(12, 2);
    }
}

function startTimer()
{
    var timer = $find("<%=Timer1.ClientID%>")
    timer._startTimer();
}

function stopTimer()
{
    var timer = $find("<%=Timer1.ClientID%>")
    timer._stopTimer();
    alert("P...");
    document.Forms[0].Submit();
}


function AvancaPagina() 
{
    window.history.forward(1);
}

function RetornaPagina()
{
    window.history.back(3);
}

function __doPostBack(eventTarget, eventArgument) 
{
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) 
    {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

function Finaliza()
{
    var xmlHttp=false;
    try 
    {
      xmlHttp = new XMLHttpRequest();
    } 
    catch (e1) 
    {
      try 
      {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e2) 
      {
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
    }

    xmlHttp.open("GET", "Finaliza.aspx", true);
    xmlHttp.send(null);
}  

function Preenche(combo, texto, valor, padrao, selecionado) 
{
	var elemento = new Option(texto, valor, padrao, selecionado)
	combo.add(elemento);
}

function AlteraSessao(key, value) 
{
	SetSessionValue(key, value);

	var cliente = GetSessionValue("Cliente");
		
	if(key == "OperacaoComercialAtual") 
	{
		PreencheFormasPagamento(cliente, value);
		var formapagamentoselecionada = GetSessionValue(cliente, "FormaPagamentoAtual");
		PreenchePrazosPagamento(cliente, formapagamentoselecionada);
	}
	else if(key == "FormaPagamentoAtual") 
	{
		PreenchePrazosPagamento(cliente, value);
	}
}

function LimpaCombo(combo) 
{
	for(var i=0; i<combo.length; i++)
		combo.remove(i);
}