var idRetorno;
var idLinkRetorno;
var vtexto = new novoTexto();


function createXMLHttp() {
  if (typeof XMLHttpRequest != 'undefined')
    return new XMLHttpRequest();
  else if (window.ActiveXObject) {
    var avers = ["Microsoft.XmlHttp", "MSXML2.XmlHttp",
         "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.4.0",
         "MSXML2.XmlHttp.5.0"];
    for (var i = avers.length -1; i >= 0; i--) {
      try {
        httpObj = new ActiveXObject(avers[i]);
        return httpObj;
      } catch(ex) {}
    }
  }
  throw new Error('XMLHttp (AJAX) not supported');
}

function submitLink(idlink, destino){
	idLinkRetorno = document.getElementById(destino);
	submitGet(idlink);
}

function submitGet(idlink){
  var url = escape(idlink);
  ajaxObj.open("GET", url);
  ajaxObj.onreadystatechange = function(){
  	 idLinkRetorno.innerHTML = "<p>Carregando...</p>";
    if(ajaxObj.readyState == 4) {
      var texto = ajaxObj.responseText;
      texto = texto.replace(/\+/g," ");
      texto = unescape(texto);
      idLinkRetorno.innerHTML = texto;
    }
  }
  ajaxObj.send(null);
  return false;

}

function submitForm(myform, destino){
  var reqBody = "";
  for (var i=0; i<myform.elements.length-1; i++){
    if (i == 0){
      if (myform.elements[i].type == "checkbox"){
        reqBody += myform.elements[i].name + "=" + myform.elements[i].checked;
      }else{
        reqBody += myform.elements[i].name + "=" + escape(myform.elements[i].value);
      }
    }else{
      if (myform.elements[i].type == "checkbox"){
        reqBody += "&" + myform.elements[i].name + "=" + myform.elements[i].checked;
      }else{
        reqBody += "&"+myform.elements[i].name + "=" + escape(myform.elements[i].value);
      }
    }
  }
  idRetorno = document.getElementById(destino);
  subPost(myform.action, reqBody);
}

function subPost(idlink, requisicao){
  ajaxObj.open("POST", idlink, true);
  ajaxObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  ajaxObj.onreadystatechange = function(){
  	idRetorno.innerHTML = "<p>Carregando...</p>";
    if(ajaxObj.readyState == 4) {
      var texto = ajaxObj.responseText;
      texto = texto.replace(/\+/g," ");
      texto = unescape(texto);
      idRetorno.innerHTML = texto;
    }
  }
  ajaxObj.send(requisicao);
  return false;
}

function submitPost(idlink, requisicao){
  ajaxObj.open("POST", idlink, true);
  ajaxObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  ajaxObj.onreadystatechange = function(){
    if(ajaxObj.readyState == 4) {
      var texto = ajaxObj.responseText;
      texto = texto.replace(/\+/g," ");
      texto = unescape(texto);
     	idRetorno.innerHTML = texto;
    }
  }
  ajaxObj.send(requisicao);
  return false;
}

function submitLinkForm(idlink, requisicao, destino){
  idRetorno = document.getElementById(destino);
  idRetorno.innerHTML = "<p>Carregando...</p>";
	submitPost(idlink, requisicao);
}

function contar(idlink, valores, redir){
  ajaxObj.open("POST", idlink, true);
  ajaxObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  ajaxObj.onreadystatechange = function(){
    if(ajaxObj.readyState == 4) {
      var texto = ajaxObj.responseText;
      texto = texto.replace(/\+/g," ");
      texto = unescape(texto);
	  var pos = texto.search("OK");
	  if (pos != -1) {
	  	redir = redir;// + '?ed=s';
	  	window.location.href = redir;
	  }
    }
  }
  ajaxObj.send(valores);
  return false;
}

function new_freecap()
{
	// loads new freeCap image
	if(document.getElementById)
	{
		// extract image name from image source (i.e. cut off ?randomness)
		thesrc = document.getElementById("freecap").src;
		thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
		// add ?(random) to prevent browser/isp caching
		document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
	} else {
		alert("Sorry, cannot autoreload freeCap image\nSubmit the form and a new freeCap will be loaded");
	}
}

function submitDados(myform, destino){
  var reqBody = "";
  for (var i=0; i<myform.elements.length-1; i++){
    if (i == 0){
      reqBody += myform.elements[i].name + "=" + escape(myform.elements[i].value);
    }else{
      reqBody += "&"+myform.elements[i].name + "=" + escape(myform.elements[i].value);
    }
  }
  myform.reset();
  idRetorno = document.getElementById(destino);
  subPost(myform.action, reqBody);
}

function atraso(){
	window.setTimeout('tempomostra()', 1000);
}

function tempomostra(){
	mostra("corpo");
}

function mostra(destino){
	if (document.getElementById(destino).style.display == ""){
		document.getElementById(destino).style.display = "block";
	}else if (document.getElementById(destino).style.display == "none"){
		document.getElementById(destino).style.display = "block";
	}else{
		document.getElementById(destino).style.display = "none";
	}
	return false;
}

function excluirPag(referencia, mensagem, destino){
  if (confirm(mensagem+'?')){
    submitLinkForm('classes/excluirpagina.php','tt='+referencia, destino);
  }
}

function mostrartermo(texto, geral, retorno){
	submitLink(texto, retorno);
	mostra(geral);
}

function getR() {
		var nid = "id="+this.vid;
		var ntitulo = "&titulo="+this.vtitulo;
		var npalavra = "&palavra="+this.vpalavra;
		var ncomentario = "&comentario="+this.vcomentario;
		var npublicidade = "&publicidade="+this.vpublicidade;
		var nartigo = "&texto="+this.vartigo.replace(/&/g,"#321;");
		return nid + ntitulo + npalavra + ncomentario + npublicidade + nartigo;
}

function novoTexto(idlink, id, titulo, palavra, comentario, publicidade, artigo){
	this.vidlink = idlink;
	this.vid = id;
	this.vtitulo = titulo;
	this.vpalavra = palavra;
	this.vcomentario = comentario;
	this.vpublicidade = publicidade;
	this.vartigo = artigo;
	this.getRequisicao = getR;
}

function fckpreview(myform, local){
	var vl = '<p class="centro"><input type="button" value="Retornar para edi&ccedil;&atilde;o" onclick="mostra(\'preview\');return false;" /><input type="button" value="publicar" onclick="enviar(\'detalhe\');return false;" /></p>';
	var fEditor = FCKeditorAPI.GetInstance( 'FCKeditor1' ) ;
	vtexto.vidlink = document.formpublica.action;
	vtexto.vid = document.formpublica.id.value;
	vtexto.vtitulo = document.formpublica.titulo.value;
	vtexto.vpalavra = document.formpublica.palavra.value;
	vtexto.vcomentario = document.formpublica.comentario.checked;
	vtexto.vpublicidade = document.formpublica.publicidade.checked;
	vtexto.vartigo = fEditor.GetData();
	var r = document.getElementById(local);
	r.innerHTML = '<p class="titulotexto">'+vtexto.vtitulo+'</p>' + vtexto.vartigo + vl;
	mostra(local);
	return false;
}

function enviar(destino){
	submitLinkForm(vtexto.vidlink, vtexto.getRequisicao(), destino);
	//var doc = document.getElementById('FCKeditor1___Config');
	//var t = formpublica.MyTextarea.value;
	//alert(t);
	//var fEditor = FCKeditorAPI.GetInstance('FCKeditor1');//('MyTextarea') ;
	//var fEditor = window.parent.InnerDialogLoaded().FCK;
	//var texto = formpublica.FCKeditor1.value;
	//alert(fEditor.GetData());
	//formpublica.FCKeditor1.value = "Meu novo texto.";
	return false;
}

var ajaxObj = createXMLHttp();
