function createRequestObject() {
    var ro;

  if (window.ActiveXObject){
    var ro=new ActiveXObject('Microsoft.XMLHTTP');
  }else if(window.XMLHttpRequest){
    var ro=new XMLHttpRequest();
  }

/*
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
       ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
       ro = new XMLHttpRequest();
    }
*/
    return ro;
}

var http = createRequestObject();

function sndReq(id,edita,pag,folha) {
  if (folha===undefined){
    http.open('GET', pag+'.php?id='+id+'&editar='+edita);
  } else {
    http.open('GET', pag+'.php?id='+id+'&editar='+edita+'&pagina='+folha);
  }
	http.onreadystatechange = handleResponse;
  http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();        

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            //alert("|"+update[0]+"|"+update.length);
            if (update[1]!=""){
                document.getElementById(update[0]).innerHTML = update[1];
                //alert(update[2]);
                document.getElementById("tabela").style.background="url(imgs/"+update[2]+") no-repeat";
            }            
            if (update.length>3) {
                //alert('|'+update[3]+'|');
                document.getElementById(update[3]).innerHTML = update[4];
                tinyMCEinit();
                if (update[5]==1){ displayHideBox('1') };
            }                  
            animatedcollapse.init();    
			//initLightbox();            
            }
        }
}

var ieBlink = (document.all)?true:false;
function doBlink(){
	if(ieBlink){
		obj = document.getElementsByTagName('BLINK');
		for(i=0;i<obj.length;i++){
			tag=obj[i];
			tag.style.visibility=(tag.style.visibility=='hidden')?'visible':'hidden';
		}
	}
}

function setQryString(){
   queryString="";
   var frm = document.forms[0];
   var numElements = frm.elements.length;
   for (var i=0; i<numElements-2; i++ ){
        if (i< numElements-3){
            queryString += frm.elements[i].name+"='"+
                           encodeURIComponent(frm.elements[i].value)+"',"; 
        } else {
            queryString += frm.elements[i].name+"='"+
                           encodeURIComponent(frm.elements[i].value)+"'";                
        }
   }
   return queryString; 
}
// Função para mudar a imagem lateral da página
function cellImg(idCell, imgName) {
    document.getElementById(idCell).style.background = "url(" + imgName + ")";
}

/* Superior Web Systems */
function displayHideBox(boxNumber)
{
    if(document.getElementById("LightBox"+boxNumber).style.display=="none") {
        document.getElementById("LightBox"+boxNumber).style.display="block";
        document.getElementById("grayBG").style.display="block";
    } else {
        document.getElementById("LightBox"+boxNumber).style.display="none";
        document.getElementById("grayBG").style.display="none";
    }
}

function tinyMCEinit() {
    tinyMCE.init({
        // General options
        mode    : "exact",
        elements: "texto",
        theme   : "advanced",
        skin    : "o2k7",
        plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",

        // Theme options
        theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true

        // Example content CSS (should be your site CSS)
        //content_css : "css/content.css",

        // Drop lists for link/image/media/template dialogs
        //template_external_list_url : "lists/template_list.js",
        //external_link_list_url : "lists/link_list.js",
        //external_image_list_url : "lists/image_list.js",
        //media_external_list_url : "lists/media_list.js",

        // Replace values for the template plugin
        //template_replace_values : {
        //	username : "Some User",
        //	staffid : "991234"
        //}
                   
    });    
}

function checkform(form)
{
  // see http://www.thesitewizard.com/archive/validation.shtml
  // for an explanation of this script and how to use it on your
  // own website

  // ** START **
  if (form.nome.value == "") {
    alert( "Introduza o seu Nome." );
    form.nome.focus();
    return false ;
  }
  if (form.email.value == "") {
    alert( "Introduza o seu E-mail." );
    form.email.focus();
    return false ;
  } 
if (form.assunto.value == "") {
    alert( "Introduza o assunto." );
    form.assunto.focus();
    return false ;
  }  
if (form.texto.value == "") {
    alert( "Introduza o texto." );
    form.texto.focus();
    return false ;
  }  
  // ** END **
  return true ;
}
//***********************************************************************
//***********************************************************************
//***********************************************************************
/***********************************************************************
zjax, Luis Breda, 2006 (Please, keep this header)
***********************************************************************/
window.zjax=function(m,u,d,t){
  if (window.ActiveXObject){
    var r=new ActiveXObject('Microsoft.XMLHTTP');
  }else if(window.XMLHttpRequest){
    var r=new XMLHttpRequest();
  }
  if (r){
    if (m==0){
      r.open('GET',u+'?'+d+'&r='+new Date().getTime(),false);
      r.send(null);
    }else{
      r.open('POST',u,false);
      r.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');
      r.send(d);
    }
  }
  return r[t?'responseXML':'responseText'];
}
/***********************************************************************
::Instructions
zjax is a lightweight !aJAX function for working with XMLHttpRequest in
synchronous mode only. zjax() and functions below, echo() and serf(),
are the hard core of my 'web_no_refresh_page' applications :)
Luis Breda

:Parameters
m = method (0=GET, other=POST)
u = url (ie: deepthought.php)
d = data (in form "ultimate='lu'&question='e')
t = type (if defined='responseXML', if omitted='responseText')

:Usage examples (Thanks to DA)
 Getting from 'deepthought.php?ultimate=lu&question=e'
 and show the response in element with 'id=luae'
   echo('luae',zjax(0,'deepthought.php','ultimate=lu&question=e'));

 Set variable 'luae' with the response using 'responseXML'
   var luae=zjax(0,'deepthought.php','ultimate=lu&question=e',1);

 Posting for 'scrabblemath.php' the variables
 'operation' 'six' 'nine' and 'result'.
   zjax(1,'scrabblemath.php','operation=multiply&six=6&nine=9&result=42');

 Posting for 'scrabblemath.php' the form with 'id=thgttg'.
   zjax(1,'scrabblemath.php',serf('thgttg'));

***********************************************************************/
window.echo=function(e,v){
  document.getElementById(e).innerHTML=v;
}
window.serf=function(f){
  var f=$(f);
  var l=f.length;
  var r='';
  for(var i=0;i<l;i++){
    r+='&'
    var e=f.elements[i];
    var t=e.type;
    if(t=='checkbox'){
      r+=e.name+'='+e.checked;
    }else if(t=='radio'){
      if (e.checked){
        r+=e.name+'='+e.value;
      }
    }else{
      r+=e.name+'='+escape(e.value);
    }
  }
  return r.substring(1);
}

