﻿var Ajax = false;
//
	function AjaxRequest() {
        if(window.XMLHttpRequest){
            Ajax = new XMLHttpRequest();
        }else if(window.ActiveXObject){
            try{
                Ajax = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    Ajax = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
    }
//
	function $(idDoDiv) {
		return document.getElementById(idDoDiv);
	}
//
	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){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		if(yScroll < windowHeight) pageHeight = windowHeight;
		else pageHeight = yScroll;
		if(xScroll < windowWidth) pageWidth = windowWidth;
		else pageWidth = xScroll;
		arrayPageSize = new Array();
		arrayPageSize[0] = pageWidth;
		arrayPageSize[1] = pageHeight;
		arrayPageSize[2] = windowWidth;
		arrayPageSize[3] = windowHeight;
		return arrayPageSize;
	}
//
	function centralizaJanela(nomeJanela, t_msg_w, t_msg_h){
		teste = getPageSize();
		$(nomeJanela).style.width = t_msg_w+'px';
		$(nomeJanela).style.height = t_msg_h+'px';
		
		left_msg = (teste[2]/2)-(t_msg_w/2);
		top_msg = ((teste[3]/2)-(t_msg_h/2))-30;
		
		$(nomeJanela).style.top = top_msg+'px';
		$(nomeJanela).style.left = left_msg+'px';
	}
//
	function TamanhoEveitoMsg(w, h){
		teste = getPageSize();
		$('divInteracao').style.width = teste[0]+'px';
		$('divInteracao').style.height = teste[1]+'px';
		if(w){
			$t_msg_w = w;
		}else{
			$t_msg_w = 350;
		}
		
		if(h){
			$t_msg_h = h;
		}else{
			$t_msg_h = 150;
		}
		centralizaJanela('mensagem', $t_msg_w, $t_msg_h);
	}
//
	function abilitaInteracao(status){
		teste = getPageSize();
		$('mensagem').innerHTML = "";
		centralizaJanela('mensagem', 500, 350);
		$('divInteracao').style.width = teste[0]+'px';
		$('divInteracao').style.height = teste[1]+'px';
		

		if(status == 1){
			$('divInteracao').style.display='block';
			$('mensagem').style.display='block';
		}else{
			$('divInteracao').style.display='none';
			$('mensagem').style.display='none';
		}
	}
//
	function abrirGraca(idGraca){
		if(idGraca){
			chamaDados("modulo/XMLtestemunho.php", "idGraca="+idGraca, "xml");
			abilitaInteracao(1);
			//$('mensagem').innerHTML = "Givoani";
		}else{
			alert("Entre com o código da graça para ser exibido!");	
		}
	}
//
	function chamaDados(txtPathScript, txtCampos, txtTipo, txtDestino){
		AjaxRequest();
		//AbilitaMsg(1, 1, '<br /><br />Carregando os Dados...');
		Ajax.open('POST', txtPathScript, true);
			Ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			Ajax.setRequestHeader("Cache-Control","no-store, no-cache, must-revalidate");
			Ajax.setRequestHeader("Cache-Control","post-check=0, pre-check=0");
			Ajax.setRequestHeader("Pragma", "no-cache");
		Ajax.send(txtCampos);
		Ajax.onreadystatechange = function (){
			if(Ajax.readyState == 4){
				if(Ajax.status == 200){
					if(txtTipo == "json"){
						json = eval("("+Ajax.responseText+")");
						tamanhoResp = json.resp.length;
						$(json.dest).innerHTML = "";
						for(i = 0; i < tamanhoResp; i++){
							$(json.dest).innerHTML += json.resp[i].lnk;	
						}
						$(json.dest).innerHTML += "<div style=\"margin-top: -20px; clear: both; width: 100%; height: 20px\"></div>";
					}else if(txtTipo == "text"){
						respostaText = Ajax.responseText;
						txtDestino = 'divMeio';
						$(txtDestino).innerHTML = respostaText;
						$(txtDestino).innerHTML += "<div style=\"margin-top: -20px; clear: both; width: 100%; height: 20px\"></div>";
					}else if(txtTipo == "xml"){
						xmldoc = Ajax.responseXML;
						if(xmldoc.hasChildNodes){
							var nos = xmldoc.getElementsByTagName('campos');
							for(var i=0; i<nos.length; i++){
								var no = nos[i];
								for(var r=0; r<no.childNodes.length; r++){
									var nodes = no.childNodes[r];
									if(nodes.nodeName != "#text"){
										var tipo_no = no.childNodes[r].getAttribute("type");
										if(tipo_no == 'text'){
											$(no.childNodes[r].nodeName).value = no.childNodes[r].firstChild.nodeValue;
										}else if(tipo_no == 'div'){
											$(no.childNodes[r].nodeName).innerHTML = no.childNodes[r].firstChild.nodeValue;
										}else if(tipo_no == '+div'){
											$(no.childNodes[r].nodeName).innerHTML += "<br />"+no.childNodes[r].firstChild.nodeValue;
										}else if(tipo_no == '-div'){
											$(no.childNodes[r].nodeName).innerHTML = no.childNodes[r].firstChild.nodeValue+"<br />"+$(no.childNodes[r].nodeName).innerHTML;
										}else if(tipo_no == 'alert'){
											alert(no.childNodes[r].firstChild.nodeValue);
										}else if(tipo_no == 'command'){
											ActionBtn(no.childNodes[r].firstChild.nodeValue);	
										}
									}
								}
							}
						}else{
							alert("Erro ao abrir os nós");
						}
					}
				}else{
					alert("Erro no Script! Erro na resposta do servidor: "+Ajax.status+", o erro é: "+Ajax.statusText);
					Ajax.abort();
				}
				//AbilitaMsg(0);
			}
			
		}
	}

