//===========================================================
// Funções de interação entre elementos
//===========================================================
function fnCopiaConteudo(fldCampo1, fldCampo2)
{
	// copia o conteúdo do campo de texto fldCampo1 para fldCampo2
	campo1 = document.getElementById(fldCampo1);
	campo2 = document.getElementById(fldCampo2);
	if (campo2!=null){
		campo2.value = campo1.value;		
	}
}

function testaHidden(campo, nomeCampoHidden){
	campoHidden = document.getElementsByName(nomeCampoHidden);
	if (campoHidden!=null){
		//corrigida para funcionar no FireFox também
		if(campo.defaultValue && campo.defaultValue.toUpperCase() != campo.value.toUpperCase()){
			campoHidden[0].value="";
		}
	}
}

// Utilizando o Display invés do Visibility
function showObjectD(object)
{
  if (document.getElementById && null!=document.getElementById(object))
  {
	document.getElementById(object).style.display = 'inline';
  }
  else if (document.layers && document.layers[object])
  {
    document.layers[object].display = 'inline';
  }
  else if (document.all && null!=document.all[object])
  {
    document.all[object].style.display = 'inline';
  }
}

function hideObjectD(object)
{
  if (document.getElementById && null!=document.getElementById(object))
  {
    document.getElementById(object).style.display = 'none';
  }
  else if (document.layers && document.layers[object])
  {
    document.layers[object].display = 'none';
  }
  else if (document.all && null!=document.all[object])
  {
    document.all[object].style.display = 'none';
  }
}

function showLinkAeroporto(obj, divTxt, divLnk)
{
	var txtOrigem = obj;
	if (txtOrigem.value == "")
	{
		hideObjectD(divLnk);
		showObjectD(divTxt);
	}
	else
	{
		showObjectD(divLnk);
		hideObjectD(divTxt);
	}
}

function TrocaDropDownList(obj)
{
	var selects = document.getElementsByTagName("select");
		
	var objID = obj.id;
	var compare = objID.substring(0,objID.lastIndexOf("__")+2);
	
	var selIndex = obj.selectedIndex;
	
	for(var i=0; i<selects.length;i++)
	{
		if(selects[i].id.indexOf(compare)==0)
		{
			selects[i].selectedIndex = 	selIndex;
		}
	}
}

function fnAtivaCampo(radio, campo){
	objRadio = document.getElementById(radio);
	objCampo = document.getElementById(campo);
	objCampo.disabled = !objRadio.checked;
	if(!objCampo.disabled) objCampo.focus();
}

function fnValidaRadios(source, args){
	sMsg = document.getElementById("msgErro");
	oRadios = document.frmSearchVoos.grpAeroporto;
	for(i=0; i<oRadios.length; i++){
		oRadio = oRadios[i];
		if(oRadio.checked){
			return true;
		}
	}
	sMsg.innerText = "Seleccione um dos itens para prosseguir.";
	args.IsValid=false;
}

function fnExibeCalendario(campo, campoPartida, isPartida,nro_noites){
	//Alterado para somente exibir o calendário se o campo da data estiver habilitado e funcionar no FireFox
	if(!document.getElementById(campo).disabled)
	{
		data = document.getElementById(campo).value;
		strURL = "EscolherData.aspx?campo="+campo+"&data="+data;
		if(!isPartida){
			strURL+="&data_minima="+document.getElementById(campoPartida).value;
		}
		if (nro_noites) {
			strURL+="&nro_noites="+nro_noites;
		}
		calendar_window=window.open(strURL,"calendar_window","left=20,top=20,width=310,height=200,dialog=yes,status=no,menubar=no");
		calendar_window.focus();
	}
}

function fnExibeCalendarioSemRegresso(campo, campoPartida, isPartida, preencheRegresso, tipo, id, numeroNoites){
	if(!campo.disabled)
	{
		data = document.getElementById(campo).value;
		strURL = "EscolherData.aspx?campo="+campo+"&data="+data + "&preencheRegresso=" + preencheRegresso + "&tipo=" + tipo + "&id=" + id +"&nro_noites=" + numeroNoites;
		if(!isPartida){
			strURL+="&data_minima="+document.getElementById(campoPartida).value;
		}
		calendar_window=window.open(strURL,"calendar_window","left=20,top=20,width=310,height=200,dialog=yes,status=no,menubar=no");
		calendar_window.focus();
	}
}

/*
Função para redimensionar a janela do calendário depois de aberto
*/
function fnDefineTamanho(){
	var NS = (navigator.appName=="Netscape")?true:false; 
	
	largura = (NS)?window.innerWidth:document.body.clientWidth; 
	altura = (NS)?window.innerHeight:document.body.clientHeight;
	 
	primeiroCalendario = document.getElementById("PrimeiroMes");
	segundoCalendario = document.getElementById("SegundoMes");

	largura = (primeiroCalendario.clientWidth+segundoCalendario.clientWidth)-largura;
	
	diferenca = primeiroCalendario.clientHeight-segundoCalendario.clientHeight;
	
	if(diferenca>0){
		altura = primeiroCalendario.clientHeight-altura;
		document.getElementById("imgSegundo").height+=diferenca;
	}else{
		altura = segundoCalendario.clientHeight-altura;
		document.getElementById("imgPrimeiro").height+=Math.abs(diferenca);
	}
	window.resizeBy(largura,altura);
}

function fnEscreveData(stControle, stData){
	window.opener.document.getElementById(stControle).value = stData;
	window.close();
}

function fnEscreveDatas(stData1, stData2){
	var camposTexto=window.opener.document.getElementsByTagName("input");
	for(var i=0; i < camposTexto.length; i++)
	{
		if(camposTexto[i].type=="text")
		{
			if(camposTexto[i].id.lastIndexOf("_txtDataEntrada")==camposTexto[i].id.length-"_txtDataEntrada".length || camposTexto[i].id.lastIndexOf("_txtDtPartida")==camposTexto[i].id.length-"_txtDtPartida".length)
			{
				camposTexto[i].value=stData1;
			}else if(camposTexto[i].id.lastIndexOf("_txtDataSaida")==camposTexto[i].id.length-"_txtDataSaida".length || camposTexto[i].id.lastIndexOf("_txtDtRegresso")==camposTexto[i].id.length-"_txtDtRegresso".length){
				camposTexto[i].value=stData2;
			}
		}
	}
	
	window.close();
}


/* função para exibir as propriedades de um objeto */
function showProperty(obj) { 
   var propertyInfo=''; 
   for (var propertyName in obj) { 
	  propertyInfo = propertyName + " = " + obj[propertyName]; 
      alert(propertyInfo+"<br>"); 
   } 
} 

/* Formatação */
/////////////////////////////////////////////////////////////////////////////////////////
//Trecho de formatação de campos
function SelecionaCaixaTexto(campo)
{
	campo.select();
}

function ChangeChar(s, pos, c)
{
	return s.substr(0, pos) + c + s.substr(pos + 1);
}

function FormataData(campo, eventStack) 
{			
	var firefox = false;
	
	if(!SomenteNumeros(eventStack)) return;
		
	firefox = (typeof(eventStack.which)!="undefined") ? true : false;

	if(firefox)
	{
		var key = eventStack.which;
		if(key==8)
			return;		
	}
	
	tam = campo.value.length;
					
	if ((tam == 2) || (tam == 5))
	{
		campo.value = campo.value + "/";
	}	
	
	if (tam >= 3 && campo.value.charAt(2) != "/") 
	   campo.value = ChangeChar(campo.value, 2, "/");
	   
	if (tam >= 5 && campo.value.charAt(5) != "/")
	   campo.value = ChangeChar(campo.value, 5, "/");   
}

function FormataDataMMAA(campo, eventStack)
{
	var firefox=false;
	
	if(!SomenteNumeros(eventStack)) return;
		
	firefox = (typeof(eventStack.which)!="undefined") ? true : false;
	
	if(firefox)
	{
		var key = eventStack.which;
		if(key==8)
			return;		
	}
		
	if (campo.value.length==2)
	{
		campo.value = campo.value + "/";
	}	
	
	var tam=campo.value.length;
	if (tam >= 3 && campo.value.charAt(2) != "/") 
	   campo.value = ChangeChar(campo.value, 2, "/");
}

function FormataCodigoPostal(campo,eventStack)
{	
	var firefox=false;
	
	if(!SomenteNumeros(eventStack)) return;
		
	firefox = (typeof(eventStack.which)!="undefined") ? true : false;
	
	if(firefox)
	{
		var key = eventStack.which;			
		if(key==8)return;		
	}
		
	if(campo.value.length==4)
	{
		campo.value = campo.value + "-";
	}	
}
//Verifica se a tecla control está pressionada
var control = false;
function DetectaControl(eventStack)
{
	if(eventStack.keyCode==17)
		control = true;		
}
//Seta o atributo da tecla control para falso após a tecla parar de ser pressionada
function SetaControl()
{
	control = false;
}

function SomenteNumeros(eventStack)
{	
	var s=null;	
	var key=0;
	var ie=false;
	var firefox=false;
	
	s= typeof(eventStack.which);
	
	ie = (window.event) ? true : false;
	firefox = (typeof(eventStack.which)!="undefined") ? true : false;
			
	if(firefox)
	{
		key = eventStack.which;
		//s = String.fromCharCode(key);
	}
	else if(ie)
	{
		key = eventStack.keyCode;
		//s = String.fromCharCode(key);
	}
	
	//alert(key);
	
	if ( (key >= 48 && key <= 57) || (key >= 96 && key <= 105) )
		return true
	else
	{
		if(firefox)
		{	
			//Adicionada a verificação da tecla control para os casos de ctrl c/v/x
			if((key!=0) && (key!=8) && (!control))
			{				
				eventStack.preventDefault();
				return false;
			}
		}
		else if(ie)
		{
			eventStack.keyCode = 0;
			return false;
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////

//Funcao que ativa ou desativa(valEnabled) todos validadores para um controle(param control)
function HabilitaValidatorsClientSide(control, valEnabled)
{
	if(typeof(Page_Validators)!="undefined")
	{
		for(var i=0; i < Page_Validators.length; i++)
		{			
			if(Page_Validators[i].controltovalidate==control)
			{	
				ValidatorEnable(Page_Validators[i], valEnabled);
				Page_Validators[i].style.display ="none";
			}
		}
	}
	
}
function habilitaDataRegresso(habilita)
{	
	var inputs = document.getElementsByTagName("input");
	for(var i=0; i < inputs.length; i++)
	{
		if(inputs[i].type=="text")
		{
			if(inputs[i].id.lastIndexOf("_txtDtRegresso")==inputs[i].id.length-"_txtDtRegresso".length)
			{
				inputs[i].disabled = !habilita;									
				if(habilita)
				{
					if(inputs[i].value.length==0)inputs[i].value = "DD/MM/AAAA";					
					HabilitaValidatorsClientSide(inputs[i].id, true);
				}
				else
				{
					inputs[i].value = "";					
					HabilitaValidatorsClientSide(inputs[i].id, false);
				}	
			}
		}
	}		
	if(habilita)
	{
		ColocaAsteriscoDataRegresso("*");
	}
	else
	{
		ColocaAsteriscoDataRegresso("");
	}
	
	// habilita/desabilita a imagem do calendário
	habilitaImagemRegresso(habilita);
}

global = "null";
function habilitaImagemRegresso(habilita){
	// habilita/desabilita a imagem do calendário
	var oImgs = document.getElementsByTagName("a");
	for(var i=0; i < oImgs.length; i++)
	{
		if(oImgs[i].id.lastIndexOf("_hlkCalendarBack")==oImgs[i].id.length-"_hlkCalendarBack".length)
		{
			if(habilita){
				if("null"!=global){
					oImgs[i].onclick=global;
				}
			}else{
				global = oImgs[i].onclick;
				oImgs[i].onclick = new Function("return false");
			}
		}
	}		
}

function ColocaAsteriscoDataRegresso(asterisco)
{
	var spans = document.getElementsByTagName("span");
	for(var i=0; i < spans.length; i++)
	{
		if(spans[i].id.lastIndexOf("_lblRegresso")==spans[i].id.length-"_lblRegresso".length)
		{			
			spans[i].childNodes[0].nodeValue = "Regresso " + asterisco;
		}	
	}
}

function fnTrocaImg(campo){
	oImg = document.getElementById(campo);
	oImg.src = "images/global/px_transparent.gif";
	oImg.style.height = "1px";
	oImg.style.width = "1px";
	if(document.getElementById("spacer1")!=null) document.getElementById("spacer1").style.width = "1px";
	if(document.getElementById("spacer2")!=null) document.getElementById("spacer2").style.width = "1px";
}

function fnAtivaCampo(campo){
	oLink = document.getElementById(campo);
	oLink.disabled = false;
	oLink.href="javascript:{if (typeof(Page_ClientValidate) != 'function' ||  Page_ClientValidate()) __doPostBack('_ctl4$lbVerificar','')}";
}

function showLink(divTxt, divLnk)
{
	showObjectD(divLnk);
	hideObjectD(divTxt);
}

function LiberaLinkVerificar(e)
{
	var ie = (window.event) ? true : false;
	var obj = (ie) ? e.srcElement : e.target;
	
	if (obj.value!=obj.defaultValue)
	{
		var divHide = document.getElementById('divHideLink');
		divHide.style.display='none';
		var divShow = document.getElementById('divShowLink');
		divShow.style.display='inline';
	}
}

function fnTrocaCampos(campoTexto, campoPassword, campo)
{
	showObjectD(campoPassword);
	hideObjectD(campoTexto);
	oCampo = document.getElementById(campo);
	if(null!=oCampo){
		oCampo.focus();
	}
}

function fnMontaSaudacao(lyrSaudacao, nome){
	oLayer = document.getElementById(lyrSaudacao);
	if(null!=oLayer){
		var data = new Date();
		var hora = data.getHours();
		var saudacao;
		if(6<hora && hora<=12){
			saudacao="Bom dia, ";
		}else if(12<hora && hora<=20){
			saudacao="Boa tarde, ";
		}else{
			saudacao="Boa noite, ";
		}
		oLayer.innerHTML = saudacao+nome;
	}
}

function fnConfirmarLogout(mensagem, idSair){
	if(confirm(mensagem))
	{
		if( null != document.all[idSair] )
		{
			document.all[idSair].click();
		}
		else
		{
			document.forms[0].submit();	
		}
		return true;
	}
	else
	{
		location.href="MinhaViagem.ashx"
		return false;
	}
}

function fnConfirmaEnvioSenha(mensagem, url){
	alert(mensagem);
	location.href=url;
}

function fnExibeDestaques(interna, externa, oLink){
	if (document.getElementById && null!=document.getElementById(interna) && null!=document.getElementById(externa))
	{
		if("destaquesLastMinute"==interna){
			if(document.getElementById(externa).style.overflow == "visible"){
				oLink.innerHTML = "Mais Ofertas Last Minute";
				document.getElementById(externa).style.overflow = "hidden";
				document.getElementById(externa).style.height = "100px";
			}else{
				oLink.innerHTML = "Fechar";
				document.getElementById(externa).style.overflow = "visible";
				document.getElementById(externa).style.height = document.getElementById(interna).clientHeight;
			}
		}else{
			if(document.getElementById(externa).style.overflow=="visible"){
				oLink.innerHTML = "Mais Destaques";
				document.getElementById(externa).style.overflow = "hidden";
				document.getElementById(externa).style.height = "81px";
			}else{
				oLink.innerHTML = "Fechar";
				document.getElementById(externa).style.overflow = "visible";
				document.getElementById(externa).style.height = document.getElementById(interna).clientHeight;
				var altura = document.getElementById(externa).style.height;
				// correção para caso tenha ficado menor do que a altura minima
				if(altura.substr(0,altura.indexOf("p"))<81){
					document.getElementById(externa).style.height = "81px";
				}
			}
		}
	}
}

function fnSelecionaRadioButton(nomeRb){
	if (document.getElementsByName && null!=document.getElementsByName(nomeRb)){
		radios = document.getElementsByName(nomeRb);
		for(i=0; i<radios.length; i++){
			if(radios[i].value=="novo"){
				radios[i].checked=true;
			}
		}
	}
}

function fnSetaHidden(nomeCampo, valor)
{
	if(null!=document.getElementById(nomeCampo)){
		document.getElementById(nomeCampo).value = valor;
	}
}

function fnTestaDisponibilidade(object, id, mensagem, isReserva, idEstrutura){
	var blnAdiciona = false;
	var estruturaParametro;
	
	if (idEstrutura!=null){
		estruturaParametro = "&estrutura="+idEstrutura;
	}
	else {
		estruturaParametro = "";
	}
	
	if (document.getElementById && null!=document.getElementById(object))
	{
		if(document.getElementById(object).style.display == 'none'){
			blnAdiciona = true;
		}
	}
	else if (document.layers && document.layers[object])
	{
		if(document.layers[object].display == 'none'){
			blnAdiciona = true;
		}
	}
	else if (document.all && null!=document.all[object])
	{
		if(document.all[object].style.display == 'none'){
			blnAdiciona = true;
		}
	}
	
	if(blnAdiciona){
		if(isReserva){
			location.href="AdicionarProduto.ashx?id="+id+"&Reservar=1"+estruturaParametro;
		}else{
			location.href="AdicionarProduto.ashx?id="+id+estruturaParametro;
		}
	}else{
		alert(mensagem);
	}
}

function fnIgualaDrops(ddlPartida, ddlRegresso){
	oPartida = document.getElementById(ddlPartida);
	oRegresso = document.getElementById(ddlRegresso);
	
	oRegresso.selectedIndex = oPartida.selectedIndex;
}

// esconde o elemento informado
function hideElement(id)
{
	if (document.getElementById) // DOM3 = IE5, NS6
	{
		document.getElementById(id).style.display = 'none';
	}
	else
	{
		if (document.layers)
		{
			document.id.display = 'none';
		}
		else
		{
			document.all.id.style.display = 'none';
		}
	}
}

// exibe o elemento informado
function showElement(id)
{
	if (document.getElementById) // DOM3 = IE5, NS6
	{
		document.getElementById(id).style.display = 'block';
	}
	else
	{
		if (document.layers)
		{
			document.id.display = 'block';
		}
		else
		{
			document.all.id.style.display = 'block';
		}
	}
}
