function GetXMLHttp(){
  var xmlHttp
  try{
	  //Firefox, Opera 8.0+, Safari
	  xmlHttp = new XMLHttpRequest();
  }catch(e){
	  //Internet Explorer
	  try{
		  xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
	  }catch(e){
		  try{
			  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		  }catch(e){
			  alert("Seu navegador não suporta a tecnologia AJAX! Atualize-o ou utilize outro navegador!")
			  return false;
		  }
	  }
  }
  
  return xmlHttp;
}
var xmlRequest = GetXMLHttp();


function noCache(
  uri  
  // uri string to open
 ){

 return uri.concat( 
  // concat String prototype,
  // the fastest way to produce
  // a complete string using multiple values
  /\?/.test(uri) ?
  // if uri has a query string
   "&"
  // add last value using & separator char
   :
  // else
   "?",
  // add a query string to this url
  "noCache=",
  // this should be a "cool name" for generated key
  (new Date).getTime(),
  // the noCache value will be milliseconds
  // from 1970/01/01
  ".",
  // plus a dot ...
  Math.random()*1234567
  // ... and a random value using
  // a "big" integer as generator
 );
 // then this is a return example using uri: http://host.com/mypage.html
 // http://host.com/mypage.html?noCache=1166301156233.332083.6663326991
 // while this is an example using uri: http://host.com/mypage.html?v0=1&v2=a
 // http://host.com/mypage.html?v0=1&v2=a&noCache=1166301168420.631416.7190624559
};

function abrirPag(valor,destino){
    url = valor;
    div = destino;
    xmlRequest.open("GET",noCache(url),true);    
    xmlRequest.onreadystatechange = mudancaEstado;
    xmlRequest.send(null);
        if (xmlRequest.readyState == 1) {
            document.getElementById(div).innerHTML = "<div class='carregando'>Carregando...<br /><img src='http://www.ferki.com.br/scripts/loader.gif' style='padding-top:15px;'></div>";
        }
    return url;
}

function mudancaEstado(){
    if (xmlRequest.readyState == 4){
        estado = xmlRequest.readyState;
		texto=xmlRequest.responseText;
        document.getElementById(div).innerHTML =texto;
		extraiScript(texto);
		if (div == "fotos") {
		initLightbox();
		}
    }
}

function extraiScript(texto){
//desenvolvido por Skywalker.to, Micox e Pita.
//http://forum.imasters.uol.com.br/index.php?showtopic=165277
var ini, pos_src, fim, codigo;
var objScript = null;
ini = texto.indexOf('<script', 0)
while (ini!=-1){
var objScript = document.createElement("script");
//Busca se tem algum src a partir do inicio do script
pos_src = texto.indexOf(' src', ini)
ini = texto.indexOf('>', ini) + 1;

//Verifica se este e um bloco de script ou include para um arquivo de scripts
if (pos_src < ini && pos_src >=0){//Se encontrou um "src" dentro da tag script, esta e um include de um arquivo script
//Marca como sendo o inicio do nome do arquivo para depois do src
ini = pos_src + 4;
//Procura pelo ponto do nome da extencao do arquivo e marca para depois dele
fim = texto.indexOf('.', ini)+4;
//Pega o nome do arquivo
codigo = texto.substring(ini,fim);
//Elimina do nome do arquivo os caracteres que possam ter sido pegos por engano
codigo = codigo.replace("=","").replace(" ","").replace("\"","").replace("\"","").replace("\'","").replace("\'","").replace(">","");
// Adiciona o arquivo de script ao objeto que sera adicionado ao documento
objScript.src = codigo;
}else{//Se nao encontrou um "src" dentro da tag script, esta e um bloco de codigo script
// Procura o final do script
fim = texto.indexOf('</script>', ini);
// Extrai apenas o script
codigo = texto.substring(ini,fim);
// Adiciona o bloco de script ao objeto que sera adicionado ao documento
objScript.text = codigo;
}

//Adiciona o script ao documento
document.body.appendChild(objScript);
// Procura a proxima tag de <script
ini = texto.indexOf('<script', fim);

//Limpa o objeto de script
objScript = null;
}
}

//Função que envia o formulário
function enviarForm(url, campos, destino) {
    xmlRequest.open('POST', url+'?'+campos, true);   	
    xmlRequest.onreadystatechange = mudancaEstado;
	xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");     
    xmlRequest.send(campos);
        if (xmlRequest.readyState == 1) {
            document.getElementById(destino).innerHTML = "<div class='carregando'>Carregando...<br /><img src='http://www.ferki.com.br/scripts/loader.gif' style='padding-top:15px;'></div>";
        }
    return url;	
}

function setarCampos() {
campos = "usuario="+encodeURI(document.getElementById('usuario').value)+"&senha="+encodeURI(document.getElementById('senha').value);
}

function setarMensagem() {
campos = "de="+encodeURI(document.getElementById('de').value)+"&para="+encodeURI(document.getElementById('para').value)+"&email="+encodeURI(document.getElementById('email').value)+"&msg="+encodeURI(document.getElementById('msg').value);
}

function setarEmail() {
campos = "nome="+encodeURI(document.getElementById('nome').value)+"&email="+encodeURI(document.getElementById('email').value)+"&cidade="+encodeURI(document.getElementById('cidade').value)+"&uf="+encodeURI(document.getElementById('uf').value)+"&msg="+encodeURI(document.getElementById('msg').value);
}

var max=300;
var ancho=600;

function progresso_tecla(obj) {
	var progresso = document.getElementById('progresso');
	if (obj.value.length < max) {
		progresso.style.backgroundColor = '#FFFFFF';
		progresso.style.backgroundImage = 'url(../imagens/textarea.png)';
		progresso.style.color = '#000000';
		var pos = ancho-parseInt((ancho*parseInt(obj.value.length))/570);
		progresso.style.backgroundPosition = '-'+pos+'px 0px';
	}
	else {
		progresso.style.backgroundColor = '#CC0000';
		progresso.style.backgroundImage = 'url()';
		progresso.style.color = '#FFFFFF';
		if (obj.value.length > 300)  {
			obj.value = obj.value.substring(0, 300);
		}
	}
	progresso.innerHTML = '('+obj.value.length+' / '+max+')';
}

	   
function janela() {	   
window.open('mural/img/smilies.php', '', 'height=300,width=535');
}

function checkLength(form){
    if (form.msg.value.length > 300){
        alert('Mensagem muito longa! O limite é de 300 caracteres!');
        return false;
    }
    return true;
}