//-------------------------------------------------------------------------
// Declara objeto AJAX
var xhr;

//-------------------------------------------------------------------------
// Erro com AJAX
function notAjax() {
	error("Seu browser não suporta AJAX!");
}

//-------------------------------------------------------------------------
// Exige uma mensagem de erro
function error(msg) {
	document.getElementById("conteudo").innerHTML = "<div id=\"subpage\"><div id=\"dot\"><h1>Erro</h1><h2>Ocorreu um erro ao processar sua requisição</h2></div>" + msg + "</div>";
}

//-------------------------------------------------------------------------
// Obtém o objeto XmlHttpRequest
function getXHR() {
	xhr = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xhr = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xhr;
}

//-------------------------------------------------------------------------
// Função de CallBack do LoadPage
function loadPageResponse() {
	// Resposta chegou
	if (xhr.readyState  == 4) {
		if (xhr.status  == 200) {
			// Status = OK
			document.getElementById("conteudo").innerHTML = xhr.responseText;
		} else {
			// Status != OK
			error("Código de erro: " + xhr.status + " - " + xhr.statusText);
		}
	} else {
			// Resposta sendo processada
			document.getElementById("conteudo").innerHTML = "<div id=\"subpage\"><h2>Carregando...</h2></div>";
	}
}

//-------------------------------------------------------------------------
// Carrega a página
function loadPage(pagina) {

	// Objeto AJAX
	xhr = getXHR();
	if (xhr == null) {
		notAjax();
		return;
	}

	// Registra a função de CallBack
	xhr.onreadystatechange = loadPageResponse;

	// Faz a requisição da página por GET de maneira assíncrona
	xhr.open("GET", pagina,  true);
	xhr.send(null);
}

//-------------------------------------------------------------------------
// Carrega a página
function popPage(URL) {
   window.open(URL,"ACPop","Location=NO,Menubar=NO,Status=NO,Toolbar=NO,Scrollbars=YES");
} 

//-------------------------------------------------------------------------
// Valida form de contato
function ValidateContato() {
	var Check = 0;
	if (document.FormContato.nome.value == '') { Check = 1; }
	if (document.FormContato.email.value == '') { Check = 1; }
	if (document.FormContato.assunto.value == '') { Check = 1; }
	if (document.FormContato.mensagem.value == '') { Check = 1; }
	if (Check == 1) {
		alert("Por favor, preencha todos os campos antes de continuar.");
		return false;
	}
        return true;
}

//-------------------------------------------------------------------------
// Envia form de contato
function send_contato(form) {

	// Obtém os parâmetros do form
	var param = "?";
	for (i = 0; i < form.childNodes.length; i++) {
		param += form.childNodes[i].name + "=" + encodeURI(form.childNodes[i].value) + "&";
	}

	// Objeto AJAX
	xhr = getXHR();
	if (xhr == null) {
		notAjax();
		return;
	}

	// Registra a função de CallBack
	xhr.onreadystatechange = loadPageResponse;

	// Faz a requisição da página por GET de maneira assíncrona
	xhr.open("POST", "contato_send.php",  true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", param.length);
	xhr.setRequestHeader("Connection", "close");
	xhr.send(param);
}

//-------------------------------------------------------------------------
// Set DIV ID to hide
function hide_div(id) {
	var itm = document.getElementById(id);
	if ( ! itm ) return;
	itm.style.display = "none";
}

//-------------------------------------------------------------------------
// Set DIV ID to show
function show_div(id) {
	var itm = document.getElementById(id);
	if ( ! itm ) return;
	itm.style.display = "";
}

//-------------------------------------------------------------------------
// Set DIV ID to show
function toggle_div(id) {
	var itm = document.getElementById(id);
	if (itm.style.display == "") {
		hide_div(id);
	} else {
		show_div(id);
	}
}

//-------------------------------------------------------------------------
// Resizes image according to site's width
function catflap_resize_image (imageref, size_available)
  {
	size_available = 640;
    if (imageref.width > size_available)
      {
        imageref.catflap_original_width = imageref.width;
        imageref.catflap_shrunk_width = size_available;
        imageref.catflap_shrunk_status = 1;
        imageref.style.width = size_available + 'px';
        if (imageref.previousSibling.previousSibling)
          {
            imageref.previousSibling.previousSibling.style.width = size_available + 'px';
            imageref.previousSibling.previousSibling.firstChild.nodeValue = "Imagem reduzida. Clique para ampliar.";
            imageref.previousSibling.previousSibling.style.display = 'block';
          }
      }
  }

//-------------------------------------------------------------------------
// Alternates full image x resized image
function catflap_toggle_image_size (imageref)
  {
    if (imageref.nextSibling.nextSibling)
      {
        if (imageref.nextSibling.nextSibling.catflap_shrunk_status == 1)
          {
            imageref.firstChild.nodeValue="Imagem no tamanho original. Clique para reduzir.";
            imageref.style.width = imageref.nextSibling.nextSibling.catflap_original_width + 'px';
            imageref.nextSibling.nextSibling.style.width = imageref.nextSibling.nextSibling.catflap_original_width + 'px';
            imageref.nextSibling.nextSibling.catflap_shrunk_status = 0;
            imageref.style.display = 'block';
          }
         else
          {
            imageref.firstChild.nodeValue = "Imagem reduzida. Clique para ampliar.";
            imageref.style.width = imageref.nextSibling.nextSibling.catflap_shrunk_width+'px';
            imageref.nextSibling.nextSibling.style.width = imageref.nextSibling.nextSibling.catflap_shrunk_width+'px';
            imageref.nextSibling.nextSibling.catflap_shrunk_status = 1;
            imageref.style.display = 'block';
          }
      }
  }
