//******************************************************************************************//
//																							//
//							.JS Responsável pela Tela: home.aspx							//
//																							//
//******************************************************************************************//

//Carrega a Combo de Cidades a Partir do Estado Escolhido.
function escolheCidade()
	{				
		//Verifica qual Estado foi Escolhido.
		if (document.getElementById('cboEstado').value == "")
			{
			    document.getElementById('cboCidade').disabled = true;
				return								
			}
		
		try
			{
				//Variável de Retorno.
				var objResponse;
				
				//Chama o Metodo Ajax dentro do Servidor.																	
				objResponse = home.carregaCidades(document.getElementById('cboEstado').value);
				document.getElementById('txtTarifa').innerHTML = "";
				//Caso Haja Erro Interno do Método no Servidor.
				if(objResponse.error != null) 
					{
						try
							{							
								window.location = "../erro.aspx"															
							}
						catch(ex)
							{
								alert(ex.description);
							}
					}
				else
						{
							//Limpa a Combo de Cidades.	
							var cboCidade = document.getElementById('cboCidade');
							cboCidade.options.length = 1;
							//Monta o Corpo do Combo de Cidade.
							for (var i = 0; i < objResponse.value.length; i++) {
							    cboCidade.options[cboCidade.options.length] = new Option(objResponse.value[i].split("|")[1], objResponse.value[i].split("|")[0]);
							}
							//Habilita a Combo de Cidades.
							document.getElementById('cboCidade').disabled = false;																																																																														
						}	
			}
		catch(ex)
			{
				//Captura os Erros de JavaScript.
				alert(ex.description);
			}																
	}
	
//Consulta a tarifa.
function consultaTarifa()
	{						
			//Variável de Retorno.
			var objResponse;			
			var boolrd;									
																
				//Verifica qual radio button está escolhido.
				if	(document.getElementById('rdFixo').checked)
					{						
						boolrd = false;
					}
				else
					{
						boolrd = true;	
					}		
									
				//Chama o Metodo Ajax dentro do Servidor.																	
				objResponse = home.consultaTarifa(document.getElementById('cboCidade').value,boolrd);								
				
				//Escreve no span o retorno de Tarifas.
				document.getElementById('txtTarifa').innerHTML = "R$ " + objResponse.value + "/ min.";															
	}	
	
//Consulta as Tarifas Internacionais.
function consultaTarifaInternacional()
	{						
			//Variável de Retorno.
			var objResponse;
			var intPop = document.getElementById('cboDestinoInternacional').value;	
			
			if (intPop == 0)
					{
						intPop = 1923;
					}
						
				//Chama o Metodo Ajax dentro do Servidor.																	
				objResponse = home.consultaTarifaInternacional(intPop,document.getElementById('cboDestinoInternacional').options[document.getElementById('cboDestinoInternacional').selectedIndex].text);					
				
				//Escreve no span o retorno das tarifas Internacionais.
				document.getElementById('txtTarifaInternacional').innerHTML = "R$ " + objResponse.value + "/ min.";															
	}		
	
	
//Consulta as Tarifas Internacionais.
function InsereNewsletter()
	{						
			//Variável de Retorno.
			var objResponse;
			var strEmail = document.getElementById("txtMailNew").value;
			var strNome = document.getElementById("txtNomeNew").value;
			
			
			
			
			//Valida o campo de Email.	
		if (strEmail != "")
			{
					var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
					if(!reEmail.test(strEmail)){
					
							alert("Formato de e-Mail Incorreto.");
							document.getElementById('txtMailNew').focus();
							return
					}			
		
		}else{
		
				window.alert("Preencha o campo e-mail");
				document.getElementById('txtMailNew').focus();
				return
		
		
		}
		
				
		if(strNome == "Nome"){
		
			window.alert("Preencha o campo nome");
			document.getElementById('txtNomeNew').focus();
			return
		
		}
			
					
			//Chama o Metodo Ajax dentro do Servidor.																	
			objResponse = home.insereNewsletter(strNome, strEmail);					
		
		if(objResponse.error == null){
		
			document.getElementById("txtMailNew").value = "";
			document.getElementById("txtNomeNew").value = "";		
			window.alert("Newsletter cadastrada com sucesso");
				
		}else{
		
			window.alert("Cadastro de Newsletter esta desativado temporariamente...");		
		
		}
		
	}	
	
// Limpa os parâmetros de consulta de tarifas.
function limpaConsulta()
	{
		document.getElementById('txtTarifa').innerHTML = "";
		document.getElementById('cboCidade').selectedIndex = "0";
		document.getElementById('cboEstado').selectedIndex = "AC";
		
	}
