/////////////////////////////////////////////////////////////////
//	::	Desenvolvido por <?% Marcelo Porto %?>
// Página Ajax.js
// Funcionlidade:
//  Essa página possui a função que faz o xml request e povoa Combos e campos
/////////////////////////////////////////////////////////////////
fila=[]
ifila=0
window.defaultStatus="- AutoSalvador - As melhores ofertas estão aqui!";

/////////////////////////////////////////////////
/////////////////////////////////////////////////
/* Funções da página de pesquisa */

try{
	xmlhttp = new XMLHttpRequest();
}catch(ee){
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
		}
	}
}

/* Funções da página de pesquisa */
/////////////////////////////////////////////////
/////////////////////////////////////////////////

function preencheUmCampo(objform,getpage){
	var campo = document.getElementById(objform);
	var results = getpage.split("|");
	campo.value = results[0];
}

function funcaoalerta(retorno){
	
	a = retorno.split(";");

if(a[0] < 0){
		alert(a[1]);
	}
}


/////////////////////////////////////////////////
/////////////////////////////////////////////////
/* Funções da página de pesquisa */

function preencheComboSelecao(objform,getpage){

	varIndex=1;
	//alert(objform);
	combo = document.getElementById(objform)
	combo.options.length = 0;
	results = getpage.split("|");
	combo.selectedIndex = 0;
	for( i = 0; i < results.length; i++ ){
		string = results[i].split( ";" );
		if (string[0]!=''){
			combo.length = i + 1;
			combo.options[i].value = string[0];
			combo.options[i].text = string[1];

			if(string[2]=="S"){
				combo.options[i].selected = true;
				//combo.options[i].style.background="#73cfef";
			}else{
				varIndex=varIndex+1;
			}
			
		}
	}
}


function preencheCombo(objform,getpage){
	
	var combo = document.getElementById(objform);
	combo.options.length = 0;
	var results = getpage.split("|");
	
	for( i = 0; i < results.length; i++ ){
		string = results[i].split( ";" );
		if (string[0]!=''){
			combo.options[i] = new Option();
			combo.options[i].value = string[0];
			combo.options[i].text = string[1];
			
		}
	}
}

function preencheComboOptGruop(objform,getpage){
	
	var combo = document.getElementById(objform);
	dom.empty(combo);
	
	var results = getpage.split("|");
	var temp = '';
	
	
	//var oOption = document.createElement('option');
	//oOption.value = "0";
	//oOption.innerText = "-- Selecione --";
	//combo.appendChild(oOption);
	
	
	for( i = 0; i < results.length; i++ ){
		arrLine = results[i].split( ";" );
		if (i == 0){
			var oOption = document.createElement('option');
			oOption.value = arrLine[0];
			oOption.innerText = arrLine[1];
			combo.appendChild(oOption);
		}else{
			arrLine = results[i].split( ";" );
			if (arrLine[0] != '') {
				if (arrLine[2] != temp){
					if (temp != ''){combo.appendChild(oGroup);}
					temp = arrLine[2];
					var oGroup = document.createElement('optgroup');
					oGroup.value = arrLine[2];
					oGroup.label = arrLine[3];
				}
				var oOption = document.createElement('option');
				oOption.value = arrLine[0];;
				oOption.innerText = arrLine[1];
				oGroup.appendChild(oOption);
			}
		}
	}
	//combo.selectedIndex = 0;
	if (temp != ''){combo.appendChild(oGroup);}
	
}

function ajax(){
	
	xmlhttp.open("GET",fila[ifila][1],true);
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState == 4) {
			switch(fila[ifila][2]){
				case "preencheCombo":
					preencheCombo(fila[ifila][0],xmlhttp.responseText);
					funcaoalerta(xmlhttp.responseText);
				break;
				case "preencheComboSelecao":
					preencheComboSelecao(fila[ifila][0],xmlhttp.responseText);
				break;
				/*
				case "preencheUmCampo":
					document.getElementById(fila[ifila][0]).value = xmlhttp.responseText;
					//alert(document.getElementById(fila[ifila][0]).value);
					//preencheUmCampo(fila[ifila][0],xmlhttp.responseText);
				break;
				*/
				case "preencheComboOptGruop":
					preencheComboOptGruop(fila[ifila][0],xmlhttp.responseText);
				break;
				
				case "carregadiv":
					document.getElementById(fila[ifila][0]).innerHTML = xmlhttp.responseText;
				break;
				case "carregadivResultado":
					if (xmlhttp.responseText == 'nothing') {
						document.getElementById('load').style.display = 'none';
						alert("Nenhuma Hospedagem Para Esta Busca!");
						document.getElementById('pesquisa').style.display = 'block';
					}else{
						document.getElementById('load').style.display = 'none';
						document.getElementById('resultados').style.display = 'block';
						document.getElementById(fila[ifila][0]).innerHTML = xmlhttp.responseText;
					}
				break;
			}
			ifila++;
			if(ifila < fila.length){
				setTimeout("ajax()",1);
			}
		}
	}
	xmlhttp.send(null);

}

function loading(opt) {
	
	if (opt == true) {
	
		var tagBody = document.getElementsByTagName('body').item(0);
		// Pega os tamanhos atuais da página, como largura, altura, ...
		var sizesPage = getPageSize();
		// Vamos criar uma tag div
		var bgBody = document.createElement('div');
		// Setar o atributo ID a div criada
		bgBody.setAttribute('id','bgBody');
		// Essa div terá o tamanho exato da página
		bgBody.style.height = arrayPageSize[1] + 'px';
		// Essa div terá a largura exata da página
		bgBody.style.width = arrayPageSize[0] + 'px';
		// Evita criar a div novamente
		if (!document.getElementById('bgBody')) {
			tagBody.insertBefore(bgBody, tagBody.firstChild);
		}	
		
		// A tag que receberá a img de loading
		var refer = document.getElementById('bgBody');
		// O tamanho da referida tag
		var referHeight = refer.offsetHeight;
		// Dizemos que os elementos dentro dela será alinhado ao centro
		refer.style.textAlign = 'center';
		// Criamos uma imagem, img.
		var img = document.createElement('img');
		// Informamos o caminho da img
		img.setAttribute('src','../tema01/port/imgLoading.gif'); 
		// Setamos um atributo ID na img criada
		img.setAttribute('id','loading');
		// Definimos seu tamanho
		img.setAttribute('width','126');
		// Dizemos que o margin-top será a metada do tamanho da div
		img.style.marginTop = (referHeight /3) + 'px';
		// Evita que seja criada duas ou mais img de loading
		if (!document.getElementById('loading')) {
			// Insere a img na tag informada na variável refer
			refer.insertBefore(img, refer.firstChild);
		}
		
	} else if (opt == false) {
		// Referenciamos a img de login através de seu ID
		var bgBody = document.getElementById('bgBody');
		bgBody.parentNode.removeChild(bgBody);
		var imgLoading = document.getElementById('loading');
		// Removemos a img de loading
		if (imgLoading) {
			imgLoading.parentNode.removeChild(imgLoading);
		}
	}
	
}

function boxCad() {
	// esconde os selects da pagina de baixo
	mostraSelects('hidden');
	// Cria um 'container' que comportará o formulário de cadastro.
	var objBody = document.getElementsByTagName('body').item(0);
	var sizesPage = getPageSize();
	var boxCad = document.createElement('div');
	boxCad.setAttribute('id','boxCad');
	var wPage = arrayPageSize[0]; // Largura total da página
	var hPage = arrayPageSize[1]; // tamanho total da página
	/*boxCad.style.width = (wPage / 2) + 'px'; // metade da largura da página*/
	boxCad.style.height = (wPage / 2) + 'px'; // metada da altura da página
	boxCad.style.marginTop = -(wPage / 4) + 'px'; // 1 quarto da largura
	//boxCad.style.marginLeft = -(wPage / 4) + 'px'; // 1 quarto da altura
	objBody.insertBefore(boxCad, objBody.lastChild);
}

function FeichaboxCad(){
	// mostra os selects da pagina de baixo
	mostraSelects('visible');
	
	var boxCad = document.getElementById('boxCad');
	if (boxCad) { // Por que ao clicar X (para deletar um registro) cria-se somente o encobridor e não o boxCad	
		boxCad.parentNode.removeChild(boxCad);
	}
	
	loading(false);
}


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 

}

function mostraSelects(op) {
		var d = document;
		var i = 0; // contador para quantidade de formulários
		var j = 0; // contador para quantidade de elementos

		// checa se o navegador é Internet Explorer
		if (navigator.appName == 'Microsoft Internet Explorer') {
			for (i=0; i < d.forms.length; i++) {
				for (j=0; j < d.forms[i].elements.length; j++) {
					if (d.forms[i].elements[j].type == 'select-one' || d.forms[i].elements[j].type == 'select-multiple')
						d.forms[i].elements[j].style.visibility = op;
				}
			}
		}
	}
