//Function to process an XMLHttpRequest.
function processajax (serverPage, obj, getOrPost, str){
	//Get an XMLHttpRequest object for use.
	xmlhttp = getxmlhttp ();
	if (getOrPost == "get"){
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
	}
	xmlhttp.send(null);
	} else {
	xmlhttp.open("POST", serverPage, true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
			}
		}
	xmlhttp.send(str);
	}
}

function getxmlhttp(){
	//Create a boolean variable to check for a valid Microsoft active x instance.
	var xmlhttp = false;
	//Check if we are using internet explorer.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using internet explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-internet explorer browser.
			xmlhttp = false;
		}
	}
	// If not using IE, create a
	// JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
		}
	return xmlhttp;
	}
try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
       try{
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       }catch(E){
          xmlhttp = false;
       }
    }
}

// validate form
function validateform(thevalue){
	serverPage = "inc_contato_validator.php?sstring=" + thevalue;
	objID = "messagebox";
	var obj = document.getElementById(objID);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
xmlhttp.send(null);
}

//Function to validate the addtask form.
function validatetask1 (thevalue, thename){
	var nowcont = true;
	if (thename == "cnome"){
		if (trim (thevalue) == ""){
		document.getElementById("themessage").innerHTML = "<span class=aviso>Informe seu nome</span>";
		nowcont = false;
		}
	}
	if (thename == "ctelefone"){
		if (trim (thevalue) == ""){
		document.getElementById("themessage").innerHTML = "<span class=aviso>Informe seu telefone</span>";
		nowcont = false;
		}
	}
	if (thename == "cemail"){
		if (trim (thevalue) == ""){
		document.getElementById("themessage").innerHTML = "<span class=aviso>Informe seu e-mail</span>";
		nowcont = false;
		}
	}
	if (thename == "ccidade"){
		if (trim (thevalue) == ""){
		document.getElementById("themessage").innerHTML = "<span class=aviso>Informe sua cidade</span>";
		nowcont = false;
		}
	}
	if (thename == "cmensagem"){
		if (trim (thevalue) == ""){
		document.getElementById("themessage").innerHTML = "<span class=aviso>Digite sua mensagem</span>";
		nowcont = false;
		}
	}
	if (nowcont == true){
	}
return nowcont;
var aok;
}

// FUNÇÃO PARA POSTAR O FORMULÁRIO
function getformvalues(fobj, valfunc){
	var str = "";
	aok = true;
	var val;
	//Run through a list of all objects contained within the form.
	for(var i = 0; i < fobj.elements.length; i++){
		if(valfunc) {
			if (aok == true){
				val = validatetask1(fobj.elements[i].value,fobj.elements[i].name);
				if (val == false){
					aok = false;
				}
			}
		}
	str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
	}
	if(document.frmcontato.pessoa[0].checked){
		str += "pessoa=Fisica";
	}else{
		str += "pessoa=Juridica";
	}
	//alert(str);
	return str;
}

function submitform(theform, serverPage, objID, valfunc){
	var file = serverPage;
	var str = getformvalues(theform,valfunc);
	//If the validation is ok.
	if (aok == true){
		obj = document.getElementById(objID);
		processajax(serverPage, obj, "post", str);
	}
}

function trim(inputString) {
	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces them with one space. If something besides
	// a string is passed in (null, custom object, etc.), then return the input.
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf(" ") != -1) {
		// Note there are two spaces in the string
		// Therefore look for multiple spaces in the string
		retValue = retValue.substring(0, retValue.indexOf(" ")) + 
		retValue.substring(retValue.indexOf(" ")+1, retValue.length); 
		// Again, there are two spaces in each of the strings
	}
return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function verifica_contato(){ 
document.all.divnome.innerHTML = '&nbsp;'; 
document.all.divtelefone.innerHTML = '&nbsp;'; 
document.all.divemail.innerHTML = '&nbsp;'; 
document.all.divcidade.innerHTML = '&nbsp;'; 
document.all.divmensagem.innerHTML = '&nbsp;'; 
document.all.statusnome.innerHTML = '&nbsp;'; 
document.all.statustelefone.innerHTML = '&nbsp;'; 
document.all.statusemail.innerHTML = '&nbsp;'; 
document.all.statuscidade.innerHTML = '&nbsp;'; 
document.all.statuscidade.innerHTML = '&nbsp;'; 
document.frmcontato.cnome.className = 'frmok'; 
document.frmcontato.ctelefone.className = 'frmok'; 
document.frmcontato.cemail.className = 'frmok'; 
document.frmcontato.ccidade.className = 'frmok'; 
document.frmcontato.cmensagem.className = 'frmok'; 
var valido = false;
if ((document.frmcontato.cnome.value == "") || (document.frmcontato.cnome.value== null)){
	document.all.divnome.innerHTML = '<b>Preencha seu nome</b>'; 
	document.frmcontato.cnome.focus(); 
	document.frmcontato.cnome.className = 'frmerro'; 
	var valido = false;
	return false;
}else{
	document.frmcontato.cnome.className = 'frmok'; 
	document.all.divnome.innerHTML = '&nbsp;'; 
	document.all.statusnome.innerHTML = '<img src=http://www.sevengel.com.br/site/wp-content/themes/sevengel/images/bto_OK.png>';
	var valido = true;
}
if ((document.frmcontato.ctelefone.value == "") || (document.frmcontato.ctelefone.value== null)) { 
	document.all.divtelefone.innerHTML = '<b>Preencha seu telefone</b>'; 
	document.frmcontato.ctelefone.focus(); 
	document.frmcontato.ctelefone.className = 'frmerro'; 
	var valido = false;
	return false; 
}else{
	document.frmcontato.ctelefone.className = 'frmok'; 
	document.all.divtelefone.innerHTML = '&nbsp;'; 
	document.all.statustelefone.innerHTML = '<img src=http://www.sevengel.com.br/site/wp-content/themes/sevengel/images/bto_OK.png>'; 
	var valido = true;
}
if ((document.frmcontato.cemail.value == "") || (document.frmcontato.cemail.value== null)) { 
	document.all.divemail.innerHTML = '<b>Preencha seu email</b>'; 
	document.frmcontato.cemail.focus(); 
	document.frmcontato.cemail.className = 'frmerro'; 
	var valido = false;
	return false; 
}else{
	document.frmcontato.cemail.className = 'frmok'; 
	document.all.divemail.innerHTML = '&nbsp;'; 
	document.all.statusemail.innerHTML = '<img src=http://www.sevengel.com.br/site/wp-content/themes/sevengel/images/bto_OK.png>'; 
	var valido = true;
}
if ((document.frmcontato.ccidade.value == "") || (document.frmcontato.ccidade.value== null)) { 
	document.all.divcidade.innerHTML = '<b>Preencha com o nome de sua cidade</b>'; 
	document.frmcontato.ccidade.focus(); 
	document.frmcontato.ccidade.className = 'frmerro'; 
	var valido = false;
	return false; 
}else{
	document.frmcontato.ccidade.className = 'frmok'; 
	document.all.divcidade.innerHTML = '&nbsp;'; 
	document.all.statuscidade.innerHTML = '<img src=http://www.sevengel.com.br/site/wp-content/themes/sevengel/images/bto_OK.png>'; 
	var valido = true;
}
if ((document.frmcontato.cmensagem.value == "") || (document.frmcontato.cmensagem.value== null)) { 
	document.all.divmensagem.innerHTML = '<b>Preencha seu mensagem</b>'; 
	document.frmcontato.cmensagem.focus(); 
	document.frmcontato.cmensagem.className = 'frmerro'; 
	var valido = false;
	return false; 
}else{
	document.frmcontato.cmensagem.className = 'frmok'; 
	document.all.divmensagem.innerHTML = '&nbsp;'; 
	document.all.statusmensagem.innerHTML = '<img src=http://www.sevengel.com.br/site/wp-content/themes/sevengel/images/bto_OK.png>'; 
	var valido = true;
}
if (valido == true){
	return true; 
}
}

//Carregando conteúdo
function updateStatus(){
	//document.getElementById("errordiv").innerHTML = "";
	document.getElementById("div_compra").innerHTML = "<img src=anima/carregando.gif width=500 height=30>";
}

function conteudo(conteudo){
    //Abre a conexão
	updateStatus(); 
    xmlhttp.open("GET",conteudo);
    xmlhttp.onreadystatechange=mostracont
    //Executa
    xmlhttp.send(null)
}

function mostracont() {
if (xmlhttp.readyState==4){
   //Mostra o HTML recebido
   document.getElementById('div_compra').innerHTML=xmlhttp.responseText
   }
}
///// NEWSLETTER > VERIFICA
function verificanews() { 
document.all.divemail.innerHTML = '&nbsp;';
document.all.statusemail.innerHTML = '&nbsp;'; 
document.frmnewsletter.cemail.className = 'frmok'; 
var valido = false;
if ((document.frmnewsletter.cemail.value == "") || (document.frmnewsletter.cemail.value== null)) { 
	document.all.divemail.innerHTML = '<b>Preencha seu e-mail</b>'; 
	document.frmnewsletter.cemail.focus(); 
	document.frmnewsletter.cemail.className = 'frmerro'; 
	var valido = false;
	return false; 
}else{
	document.frmnewsletter.cemail.className = 'frmok'; 
	document.all.divemail.innerHTML = '&nbsp;'; 
	document.all.statusemail.innerHTML = '<img src=http://www.sevengel.com.br/site/wp-content/themes/sevengel/images/bto_OK.png>'; 
	var valido = true;
}
if (valido == true){
	return true; 
}
}

function submitform2(theform, serverPage, objID, valfunc){
	var file = serverPage;
	var str = getformvalues(theform,valfunc);
	//If the validation is ok.
	if (aok == true){
		obj = document.getElementById(objID);
		processajax(serverPage, obj, "post", str);
	}
}

//Function to validate the addtask form.
function validatetask2(thevalue, thename){
	var nowcont = true;
	if (thename == "cemail"){
		if (trim (thevalue) == ""){
			document.getElementById("themessage").innerHTML = "<span class=aviso>Informe seu e-mail</span>";
			nowcont = false;
		}
	}
	if (nowcont == true){
	}
return nowcont;
var aok;
}
