function changeParcelamento( opcao ) 
{
	if ( opcao != "Parcela Unica" ) //mostra comentarios sobre parcelametno
		document.getElementById("divParcelamento_MSG").style.display = "";
	else
		document.getElementById("divParcelamento_MSG").style.display = "none";
}

function validaLicence()
{
	if ( ( !document.getElementById("opMeraK_nao_Instalado").checked ) && ( document.getElementById("CUSTOMER_REF_KEY_FILE").value == "" ) )
	{
		alert('Por favor, selecione o arquivo XML contendo a "Reference Key".');
		return false;
	}
	else
	{
		if ( validateForm() )
		{
			if ( ( document.getElementById("PAYMENTMETHOD").value.substr(0, 6) == "Cartao" ) && (! isValidCreditCard( document.getElementById("CUSTOMER_Cartao_Numero").value ) ) )
			{
				if ( document.getElementById("PAYMENTMETHOD").value == "Cartao_BNDES" ) 
					return true;
				else
				{
					alert ("Número de Cartão de Crédito Inválido.\nPor favor, tente novamente.");
					document.getElementById("CUSTOMER_Cartao_Numero").focus();
					return false;
				}
			}
			else //Se valido
			{
				if ( trim(typeOfCard( document.getElementById("CUSTOMER_Cartao_Numero").value ).toUpperCase()) == trim(document.getElementById("PAYMENTMETHOD").value.substr(7, 10).toUpperCase()) ) //Testar se o numero esta de acordo com a bandeira
					return true;
				else
				{
					alert("Este número de cartão pertence a outra bandeira: " + trim(typeOfCard( document.getElementById("CUSTOMER_Cartao_Numero").value ).toUpperCase()) );
					return false;
				}
			}
		}
		else
			return false;
	}
}

function checkMeraK_nao_Instalado()
{
	if ( document.getElementById("opMeraK_nao_Instalado").checked )
	{
		document.getElementById("CUSTOMER_REF_KEY_FILE").disabled = true;
	}
	else
	{
		document.getElementById("CUSTOMER_REF_KEY_FILE").disabled = false;
	}
}

function checkIM_Isento()
{
	if ( document.getElementById("opIM_Isento").checked )
	{
		document.getElementById("CUSTOMER_Inscricao_Municipal").value = "ISENTO";
		document.getElementById("CUSTOMER_Inscricao_Municipal").disabled = true;
	}
	else
	{
		document.getElementById("CUSTOMER_Inscricao_Municipal").value = "";
		document.getElementById("CUSTOMER_Inscricao_Municipal").disabled = false;
	}
}

function checkIE_Isento()
{
	if ( document.getElementById("opIE_Isento").checked )
	{
		document.getElementById("CUSTOMER_Inscricao_Estadual").value = "ISENTO";
		document.getElementById("CUSTOMER_Inscricao_Estadual").disabled = true;
	}
	else
	{
		document.getElementById("CUSTOMER_Inscricao_Estadual").value = "";
		document.getElementById("CUSTOMER_Inscricao_Estadual").disabled = false;
	}
}

function ChangeResponsavel()
{
	if (document.getElementById("CUSTOMER_Tipo_Documento").value == 'CPF')
	{
		document.getElementById("CUSTOMERDOCNUMBER").setAttribute('maxlength', 14);
		
		document.getElementById("labelCNPJ_Numero").style.display 			= "none";
		document.getElementById("labelCPF_Numero").style.display 			= "";

		document.getElementById("labelCNPJ").style.display 					= "none";
		document.getElementById("labelCPF").style.display 						= "";

		if ( document.getElementById("rowIM") )
			document.getElementById("rowIM").style.display 						= "none";
		
		if ( document.getElementById("rowIE") )
			document.getElementById("rowIE").style.display 						= "none";
		
		if ( document.getElementById("CUSTOMER_Inscricao_Municipal") )
			document.getElementById("CUSTOMER_Inscricao_Municipal").value 	= ".";
		
		if ( document.getElementById("CUSTOMER_Inscricao_Estadual") )
			document.getElementById("CUSTOMER_Inscricao_Estadual").value 	= ".";
		
		document.getElementById("rowContato").style.display 					= "none";
		document.getElementById("CUSTOMERNAME").value 							= ".";
	}
	else
	{
		document.getElementById("CUSTOMERDOCNUMBER").setAttribute('maxlength', 18);

		document.getElementById("labelCPF_Numero").style.display 			= "none";
		document.getElementById("labelCNPJ_Numero").style.display 			= "";

		document.getElementById("labelCPF").style.display 						= "none";
		document.getElementById("labelCNPJ").style.display 					= "";

		if ( document.getElementById("rowIM") )
			document.getElementById("rowIM").style.display 						= "";

		if ( document.getElementById("rowIE") )
			document.getElementById("rowIE").style.display 						= "";

		if ( document.getElementById("CUSTOMER_Inscricao_Municipal") )
			document.getElementById("CUSTOMER_Inscricao_Municipal").value 	= "";
		
		if ( document.getElementById("CUSTOMER_Inscricao_Estadual") )
			document.getElementById("CUSTOMER_Inscricao_Estadual").value 	= "";
		
		document.getElementById("rowContato").style.display 					= "";
		document.getElementById("CUSTOMERNAME").value 							= "";
	}
}


function ChangeCartao( opcao )
{
	document.getElementById("PARCELAMENTO").disabled = false;

	//Parcelamento - INICIO
	if ( document.getElementById("PARCELAMENTO").length > 3 )
	{
		for (i=1;i=document.getElementById("PARCELAMENTO").length-3;i++)
			document.getElementById("PARCELAMENTO").remove(document.getElementById("PARCELAMENTO").length - 1);
	}
	document.getElementById("PARCELAMENTO").options[1].value = "2x sem juros";
	document.getElementById("PARCELAMENTO").options[2].value = "3x sem juros";

	document.getElementById("PARCELAMENTO").options[1].text = "2x sem juros";
	document.getElementById("PARCELAMENTO").options[2].text = "3x sem juros";
	//Parcelamento - FIM

	if ( opcao.substr(0, 6) == "Cartao" ) //mostra campos do cartao de credito
	{
		document.getElementById("rowCartao1").style.display = "";
	
		document.getElementById("CUSTOMER_Cartao_Numero").value 		= "";

		if ( opcao == "Cartao_BNDES" )
		{
			//Parcelamento - INICIO
			document.getElementById("PARCELAMENTO").options[1].text = "2x";
			document.getElementById("PARCELAMENTO").options[2].text = "3x";

			var op;

			for (i=2;i<=32;i++)
			{
				op = document.createElement('option');
				
				op.text = i + 'x';
				op.value = i + 'x';
				
				try
				{
					document.getElementById("PARCELAMENTO").add(op); 
				}
				catch(e)
				{
					document.getElementById("PARCELAMENTO").add(op, null); 
				}
				op = null;
			}
			//Parcelamento - FIM
			
			document.getElementById("rowCartao2").style.display = "none";
			document.getElementById("rowCartao3").style.display = "none";
			document.getElementById("rowCartao4").style.display = "none";
	
			document.getElementById("CUSTOMER_Cartao_Nome").value 		= ".";
			document.getElementById("CUSTOMER_Cartao_Validade").value 	= ".";
			document.getElementById("CUSTOMER_Cod_Seguranca").value 		= ".";
		}
		else
		{
			document.getElementById("rowCartao2").style.display = "";
			document.getElementById("rowCartao3").style.display = "";
			document.getElementById("rowCartao4").style.display = "";
	
			document.getElementById("CUSTOMER_Cartao_Nome").value 		= "";
			document.getElementById("CUSTOMER_Cartao_Validade").value 	= "";
			document.getElementById("CUSTOMER_Cod_Seguranca").value 		= "";
		}

		if ( opcao != "Cartao_Visa" )
			document.getElementById("divPagSeguro").style.display = "none";
	}
	else
	{
		document.getElementById("rowCartao1").style.display = "none";
		document.getElementById("rowCartao2").style.display = "none";
		document.getElementById("rowCartao3").style.display = "none";
		document.getElementById("rowCartao4").style.display = "none";

		document.getElementById("CUSTOMER_Cartao_Numero").value 		= ".";
		document.getElementById("CUSTOMER_Cartao_Nome").value 		= ".";
		document.getElementById("CUSTOMER_Cartao_Validade").value 	= ".";
		document.getElementById("CUSTOMER_Cod_Seguranca").value 		= ".";

		document.getElementById("divPagSeguro").style.display = "none";

		if ( opcao == "Pagamento_Digital" )
		{
			document.getElementById("PARCELAMENTO").value = "Parcela Unica";
			document.getElementById("PARCELAMENTO").disabled = true;
			document.getElementById("divParcelamento_MSG").style.display = "none";
			document.getElementById("divPagSeguro").style.display = "";
		}
	}
	changeParcelamento( document.getElementById("PARCELAMENTO").value );
}

function ChangeCartaoWorkshop( opcao )
{
	//document.getElementById("PARCELAMENTO").disabled = false;

	//Parcelamento - INICIO
	if ( document.getElementById("PARCELAMENTO").length > 3 )
	{
		for (i=1;i=document.getElementById("PARCELAMENTO").length-3;i++)
			document.getElementById("PARCELAMENTO").remove(document.getElementById("PARCELAMENTO").length - 1);
	}
	document.getElementById("PARCELAMENTO").options[1].value = "2x sem juros";
	document.getElementById("PARCELAMENTO").options[2].value = "3x sem juros";

	document.getElementById("PARCELAMENTO").options[1].text = "2x sem juros";
	document.getElementById("PARCELAMENTO").options[2].text = "3x sem juros";
	//Parcelamento - FIM

	if ( opcao.substr(0, 6) == "Cartao" ) //mostra campos do cartao de credito
	{
		document.getElementById("rowCartao1").style.display = "";
	
		document.getElementById("CUSTOMER_Cartao_Numero").value 		= "";

		document.getElementById("rowCartao2").style.display = "";
		document.getElementById("rowCartao3").style.display = "";
		document.getElementById("rowCartao4").style.display = "";

		document.getElementById("CUSTOMER_Cartao_Nome").value 		= "";
		document.getElementById("CUSTOMER_Cartao_Validade").value 	= "";
		document.getElementById("CUSTOMER_Cod_Seguranca").value 		= "";

		if ( opcao != "Cartao_Visa" )
			document.getElementById("divPagSeguro").style.display = "none";
	}
	else
	{
		document.getElementById("rowCartao1").style.display = "none";
		document.getElementById("rowCartao2").style.display = "none";
		document.getElementById("rowCartao3").style.display = "none";
		document.getElementById("rowCartao4").style.display = "none";

		document.getElementById("CUSTOMER_Cartao_Numero").value 		= ".";
		document.getElementById("CUSTOMER_Cartao_Nome").value 		= ".";
		document.getElementById("CUSTOMER_Cartao_Validade").value 	= ".";
		document.getElementById("CUSTOMER_Cod_Seguranca").value 		= ".";

		document.getElementById("divPagSeguro").style.display = "none";

		if ( opcao == "Pagamento_Digital" )
		{
			//Parcelamento - INICIO
			document.getElementById("PARCELAMENTO").options[1].text = "2x";
			document.getElementById("PARCELAMENTO").options[2].text = "3x";

			var op;

			for (i=2;i<=24;i++)
			{
				op = document.createElement('option');
				
				op.text = i + 'x';
				op.value = i + 'x';
				
				try
				{
					document.getElementById("PARCELAMENTO").add(op); 
				}
				catch(e)
				{
					document.getElementById("PARCELAMENTO").add(op, null); 
				}
				op = null;
			}
			//Parcelamento - FIM

			//document.getElementById("PARCELAMENTO").value = "Parcela Unica";
			//document.getElementById("PARCELAMENTO").disabled = true;
			document.getElementById("divParcelamento_MSG").style.display = "none";
			document.getElementById("divPagSeguro").style.display = "";
		}
	}
	changeParcelamento( document.getElementById("PARCELAMENTO").value );
}


function verificaCEP( campoCEP )
{
	if ( campoCEP.length == 9 )
		buscaCEP( );
}

function buscaCEP( )
{
	if ( document.getElementById("CUSTOMER_CEP").value.length == 0 )
	{
		alert("Por favor, preencha o campo CEP");
	}
	else if ( document.getElementById("CUSTOMER_CEP").value.length <= 6 )
	{
		alert("Por favor, preencha o campo CEP corretamente");
	}
	else
	{
		//showPopup('frmAguarde', 'visible');

		//-----------------------------------------------------------------------------
		// Define and initialize an HttpRequest object for posting data.
		//-----------------------------------------------------------------------------
		var postRequest = new HttpRequest();
		postRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		//postRequest.abort();
		//postRequest.clearRequestHeaders();
		postRequest.url = "_Objetos/buscaCEP.php";
		postRequest.successCallback = eval("resultadoCEP");
		postRequest.post("CEP=" + document.getElementById("CUSTOMER_CEP").value);
	}
}

function resultadoCEP( httpRequest ) 
{ 
	try
	{
		var Resultado = decodeURIComponent(httpRequest.responseText).split("|");

		var Cod_Erro			= Resultado[0];
		var Erro					= Resultado[1].replace("cep", "CEP");
		var Tipo_Logradouro 	= Resultado[2];
		var Logradouro 		= Resultado[3];
		var Bairro 				= Resultado[4];
		var Cidade				= Resultado[5];
		var UF					= Resultado[6];
		
		if ( Cod_Erro != "1" )
			alert ( Erro );
		
		document.getElementById("CUSTOMER_Endereco").value	= Tipo_Logradouro + " " + Logradouro;
		document.getElementById("CUSTOMER_Bairro").value 	= Bairro;
		document.getElementById("CUSTOMER_Cidade").value 	= Cidade;
		document.getElementById("CUSTOMER_Estado").value 	= UF;
		
	 }
	 catch(e)
	 {
		 alert("CEP incorreto");
		 //alert(e);
	  }
}


function MM_formtCep(e,src,mask) 
{
	if(window.event) 
	{ 
		_TXT = e.keyCode; 
	}
   else if(e.which) 
	{ 
		_TXT = e.which; 
	}
   if(_TXT > 47 && _TXT < 58) 
	{
 		var i = src.value.length; 
		var saida = mask.substring(0,1); 
		var texto = mask.substring(i)
 		if (texto.substring(0,1) != saida) 
		{ 
			src.value += texto.substring(0,1); 
		}
    	return true; 
	} 
	else 
	{ 
		if (_TXT != 8) 
		{ 
			return false; 
		}
 		else 
		{ 
			return true; 
		}
	}
}

function comoFicou_Sabendo()
{
	if ( document.getElementById("OP_Cliente").checked )
		document.getElementById("rowComo_Ficou_Sabendo").style.display = "none";
	else
		document.getElementById("rowComo_Ficou_Sabendo").style.display = "";
}

function comoFicou_Sabendo_Combo()
{
	if ( document.getElementById("CUSTOMER_HOW_DID_YOU_KNOW").value == "Outra_forma" )
	{
		document.getElementById("labelCampo_Ficou_Sabendo").style.display = "";
		document.getElementById("campoOutra_Forma").value						= "";
	}
	else
	{
		document.getElementById("labelCampo_Ficou_Sabendo").style.display = "none";
		document.getElementById("campoOutra_Forma").value						= ".";
	}
}



/**
 * Formata o Campo de acordo com a mascara informada.
 * Ex de uso: onkeyup="AplicaMascara('HORA', this);".
 * @author Igor Escobar (blog@igorescobar.com)
 * @param Mascara String que possui o nome da mascara de formatação do campo.
 * @param elemento Campo que será formatado de acordo com a mascara, voce pode informar o id direto ou o próprio elemento usando o this.
 * @returns {void}
 * @methods
	- DATA:
	- HORA:
	- CEP:
	- CPF:
	- CNPJ
	- TEL
	- TELDDD
 */
function AplicaMascara(Mascara, elemento)
{
	// Seta o elemento
	var elemento = (elemento) ? elemento : document.getElementById(elemento);
	if(!elemento) return false;
	// DEFINE AS REGRAS DE FORMATAÇÃO DOS CAMPOS
	var Regexs = {
		DATA: /(([0-9]){2})(([0-9]){2})(([0-9]){4})/,
		HORA: /(([0-9]{2}))(([0-9]){2})(([0-9]){2})/,
		CEP: /(([0-9]{5}))(([0-9]){3})/,
		CPF: /(([0-9]{3}))(([0-9]){3})(([0-9]){3})(([0-9]){2})/,
		CNPJ: /(([0-9]{2}))(([0-9]){3})(([0-9]){3})(([0-9]){4})(([0-9]){2})/,
		TEL: /(([0-9]){4})(([0-9]){4})/,
		TELDDD: /(([0-9]){2})(([0-9]){4})(([0-9]){4})/
	}
	MyRegex = eval('Regexs.'+Mascara);
	// INSTANCIA A REGRA REGEX PARA FORMATACAO
	getRegexGroups = new RegExp(MyRegex);
	// INSTANCIA A REGRA QUE VERIFICA SE O CAMPO POSSUI SOMENTE NÚMEROS
	onlyNumbers = new RegExp(/^([0-9]+)$/);
	// ESTE É O CAMPO COM AS IMPURESAS (DIGITADO PELO USUÁRIO FINAL)
	var CampoSujo = elemento.value;
	// RETIRA OS EXCESSOS DOS CAMPOS PARA QUE POSSAMOS APLICAR A MASCARA EM UM CAMPO LIMPO
	var oValue = CampoSujo.replace(/([./;:,\-()]+)/g,"");
	// VERIFICA SE A REGRA QUE ELE SE ESCOLHEU SE APLICA AO TEXTO DIGITADO NO CAMPO
	if(getRegexGroups.test(oValue) == true){
		//APLICA A REGRA NO CAMPO E FORMATA
		switch(Mascara){
			case 'DATA': oNewCampo = oValue.replace(getRegexGroups,"$1/$3/$5"); break;
			case 'HORA': oNewCampo = oValue.replace(getRegexGroups,"$1:$3:$5"); break;
			case 'CEP': oNewCampo = oValue.replace(getRegexGroups,"$1$3-$5"); break;
			case 'CPF': oNewCampo = oValue.replace(getRegexGroups,"$1.$3.$5-$7"); break;
			case 'CNPJ': oNewCampo = oValue.replace(getRegexGroups,"$1.$3.$5/$7-$9"); break;
			case 'TEL': oNewCampo = oValue.replace(getRegexGroups,"$1-$3"); break;
			case 'TELDDD': oNewCampo = oValue.replace(getRegexGroups,"($1)$3-$5"); break;
		}
		// Retorna o valor do elemento com seu novo valor
		elemento.value = oNewCampo;
	}
}