// JavaScript Document

//AJAX

function ajaxInit() {
 var req;
 try {
  req = new ActiveXObject("Microsoft.XMLHTTP");
 } catch(e) {
  try {
   req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch(ex) {
   try {
    req = new XMLHttpRequest();
   } catch(exc) {
    alert("Esse browser não tem recursos para uso do Ajax");
    req = null;
   }
  }
 }
 
 return req;
}

function carregar(url, id){
	
	xmlhttp = ajaxInit();
	
    var conteudo = document.getElementById(id);

	   xmlhttp.open("GET",url,true);	//aqui voce envia a requisicao por get	
	
    //Executada quando o navegador obtiver o código
    xmlhttp.onreadystatechange=function() {
		
		if (xmlhttp.readyState == 1) {
	  		document.getElementById(id).innerHTML = '<span class="v10b">carregando...</span>';    
	  	}	

        if (xmlhttp.readyState==4){	

            //Lê o texto
            var texto=xmlhttp.responseText

            //Desfaz o urlencode
            texto=texto.replace(/\+/g," ")
            texto=unescape(texto)
			
			if (xmlhttp.status == 200) {
	  			var conteudo=document.getElementById(id)
            	conteudo.innerHTML=texto  
	  		}	
        }
    }
    xmlhttp.send(null)
}

function makePOSTRequest(url, parameters) {
  http_request = ajaxInit();
  
  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents() {

  if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		result = http_request.responseText;
		//document.getElementById('textoresp').innerHTML = result; 
		alert(result);
	 } else {
		alert('There was a problem with the request.');
	 }
  }
}

function get(obj) {
  var poststr = "mytextarea1=" + encodeURI( document.getElementById("nome").value ) +
				"&mytextarea2=" + encodeURI( document.getElementById("email").value ) +
				"&mytextarea3=" + encodeURI( document.getElementById("ddd").value ) +
				"&mytextarea4=" + encodeURI( document.getElementById("tel").value ) +
				"&mytextarea5=" + encodeURI( document.getElementById("cidade").value ) +
				"&mytextarea6=" + encodeURI( document.getElementById("estado").value ) +
				"&mytextarea7=" + encodeURI( document.getElementById("mensagem").value );
  makePOSTRequest('enviarcontato.php', poststr);

document.getElementById("nome").value = "Nome";
document.getElementById("email").value = "E-mail";
document.getElementById("ddd").value = "";
document.getElementById("tel").value = "";
document.getElementById("cidade").value = "Cidade";
document.getElementById("cpf").value = "CPF";
document.getElementById("estado").selectedIndex = 'UF';
document.getElementById("mensagem").value = "Mensagem";
}

function get2(obj) {
  var poststr = "mytextarea1=" + encodeURI( document.getElementById("email2").value );
  makePOSTRequest('enviarnews.php', poststr);


document.getElementById("email2").value = "Indique seu e-mail";
}


function Verifica() {
  if (document.forms['formContato'].nome.value.length == 0 || document.forms['formContato'].nome.value == 'Nome') {
    alert('Por favor, preencha o campo Nome!');
	document.forms['formContato'].nome.focus();
    return false;
  }
  
  if (document.forms['formContato'].email.value.length == 0 || document.forms['formContato'].email.value == 'E-mail') {
    alert('Por favor, preencha o campo E-mail!');
	document.forms['formContato'].email.focus();
    return false;
  }
  
  if (document.forms['formContato'].email.value.indexOf("@")==-1 || document.forms['formContato'].email.value.indexOf(".")==-1) {
    alert('Por favor, preencha o campo com um e-mail valido!');
	document.forms['formContato'].email.focus();
    return false;
  }
  
  if (document.forms['formContato'].cpf.value.length == 0 || document.forms['formContato'].cpf.value == 'CPF') {
    alert('Por favor, preencha o campo Nome!');
	document.forms['formContato'].cpf.focus();
    return false;
  }
  
  if (document.forms['formContato'].ddd.value.length == 0) {
    alert('Por favor, preencha o campo Telefone!');
	document.forms['formContato'].ddd.focus();
    return false;
  }
  
  if (document.forms['formContato'].tel.value.length == 0) {
    alert('Por favor, preencha o campo Telefone!');
	document.forms['formContato'].tel.focus();
    return false;
  }
  
  if (document.forms['formContato'].cidade.value.length == 0 || document.forms['formContato'].cidade.value == 'Cidade') {
    alert('Por favor, preencha o campo Cidade!');
	document.forms['formContato'].cidade.focus();
    return false;
  }
  
  if (document.forms['formContato'].estado.selectedIndex == 0) {
    alert('Por favor, selecione um Estado!');
	document.forms['formContato'].estado.focus();
    return false;
  }
  
  if (document.forms['formContato'].mensagem.value.length == 0 || document.forms['formContato'].mensagem.value == 'Mensagem') {
    alert('Por favor, preencha o campo Mensagem!');
	document.forms['formContato'].mensagem.focus();
    return false;
  }
  
  return true;
}

function Verifica2() { 

  if (document.forms['frContato'].email2.value.length == 0 || document.forms['frContato'].email2.value == 'Indique seu e-mail') {
    alert('Por favor, preencha o campo E-mail!');
	document.forms['frContato'].email2.focus();
    return false;
  }
  
  if (document.forms['frContato'].email2.value.indexOf("@")==-1 || document.forms['frContato'].email2.value.indexOf(".")==-1) {
    alert('Por favor, preencha o campo com um e-mail valido!');
	document.forms['frContato'].email2.focus();
    return false;
  }
  
  return true;
}

function abrirs(pagina,largura,altura) {
	
w = screen.width;
h = screen.height;

meio_w = w/2;
meio_h = h/2;

altura2 = altura/2;
largura2 = largura/2;
meio1 = meio_h-altura2;
meio2 = meio_w-largura2;

window.open(pagina,'','height=' + altura + ', width=' + largura + ', fullscreen=no, top='+meio1+', left='+meio2+''); 
}
