/*readyState:
0 = não inicializado
1 = carregando
2 = carregado
3 = modo interativo
4 = completado
*/

var tempHTML2;

var req;
var req2;
var parametros = new Array();

var primeiraVez = true;

var htmlCarregando = "<table width=\"154\" height=\"36\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"divCarregando\"><tr><td width=\"34\" height=\"36\" align=\"right\"><img alt=\"\" src=\"../images/carregando.gif\" width=\"18\" height=\"18\"></td><td width=\"10\" align=\"center\" class=\"fonte21\">&nbsp;</td><td width=\"110\" align=\"left\" class=\"fonte21\" style=\"font-size:15px;\">cargando...</td></tr></table>";

//carregaPagina = onnavigate("carregaPagina", function(url) {
function carregaPagina(url) {
	makeHistory(url);
	try {
		if(url.indexOf("{")!=-1) {			
			funcao = url.substring(url.indexOf("{")+1, url.length);
			if(funcao.indexOf("!")!=-1) {				
				par = funcao.substring(funcao.indexOf("!")+1, funcao.length);
				par = par.split("&");
				funcao = funcao.substring(0, funcao.indexOf("!"));
				temParametro = true;
			} else {
				temParametro = false;
			}
			url = url.substring(0, url.indexOf("{"));			
			temFuncao = true;
		} else {
			temFuncao = false;			
		}
				
		if(url == "") {
			alert("Try to open again http://www.ctsinformatica.com.br/english/");
			return;
		}
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			// Internet Explorer Windows
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if(req) {
			req.onreadystatechange = function() {			
				lugarACarregar = document.getElementById("corpo");
				if(req.readyState == 4) {					
					tempHTML2 = req.responseText;
					if(lugarACarregar!=null) {
						lugarACarregar.innerHTML = req.responseText;
						
						if(temFuncao) {
							if(temParametro) {								
								funcao = funcao + "(";
								for(i=0;i<par.length-1;i++) {
									funcao = funcao + "'" + par[i] + "', ";
								}
								funcao = funcao + "'" + par[i] + "'";							
								funcao = funcao + ");";
							} else {
								funcao = funcao + "();";
							}							
							eval(funcao);
						}					
						switch(url) {							
							case "home.html": {
								desmarcarMenu(true);
								break;
							}							
							case "empresa.html": {
								itemMenuId = 1;
								marcarMenu(1);
								break;
							}
							case "produtos.html": {
								itemMenuId = 2;
								marcarMenu(2);
								break;
							}							
							case "cadastro.html": {
								if(!temFuncao) {									
									interesseId = 0;
								}								
								primeiroClique = true;
								itemMenuId = 3;
								marcarMenu(3);	
								break;
							}							
							case "representantes.html": {
								itemMenuId = 4;
								marcarMenu(4);
								break;
							}												
							case "contato.html": {
								marcarMenu(5);
								itemMenuId = 5;
								break;
							}
							
							case "fonoView.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(1);
								break;	
							}							
							case "voxGames.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(7);
								break;	
							}							
							case "voxMetria.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(9);
								break;	
							}
							case "earMix.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(9);
								break;	
							}
							case "fisiMetrix.html": {
								marcarMenu(2);
								itemMenuId = 2;
								//carregaPagProd(9);
								break;	
							}
						}						
						scrollTo(0,0);
						
					}
				} else if(req.readyState > 4) {
					alert("erro: " + req.readyState);
				} else if(req.readyState==1 && lugarACarregar!=null) {
					lugarACarregar.innerHTML = htmlCarregando;	
				} 				
			}
			req.open("GET", url, true);
			req.send(null);
		}
	} catch(e) {
		alert("Erro ao carregar a pagina!\n\nMotivo: " + e);		
	}
}
//});

function carregaPaginaAncora(url, ancora) {
  try {
	  if(url == "") {
    	return;
	  }
	  if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	  // Internet Explorer Windows
	  } else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	
	  if(req) {
		req.onreadystatechange = function() {
			var lugarACarregar = document.getElementById("corpo");
			if(req.readyState == 4) {
				lugarACarregar.innerHTML = req.responseText;				
				//alert(">> " + ancora);
				gotoAncora(ancora);
				
			} else if(req.readyState > 4) {
				alert("erro: " + req.readyState);
			} else {
				if(req.readyState == 1) {
					lugarACarregar.innerHTML = htmlCarregando;
				}
			}			
		}
		req.open("GET", url, true);
		req.send(null);
	  }
	} catch(e) {
		alert("Erro ao carregar a pagina!\n\nMotivo: " + e);		
	}
}

function carregarPaginaEm(url, idLugar) {
  	try {	
		if(url.indexOf("{")!=-1) {			
			funcao = url.substring(url.indexOf("{")+1, url.length);
			if(funcao.indexOf("!")!=-1) {
				par = funcao.substring(funcao.indexOf("!")+1, funcao.length);
				par = par.split("&");
				funcao = funcao.substring(0, funcao.indexOf("!"));
				temParametro = true;
			} else {
				temParametro = false;
			}
			url = url.substring(0, url.indexOf("{"));
			temFuncao = true;
		} else {
			temFuncao = false;
		}
		
		if(url == "") {
			alert("?????");
			return;
		}
	
		if(idLugar=="") {
			return;
		} else {
			var lugarACarregar = document.getElementById(idLugar);	
		}		
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			// Internet Explorer Windows
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if(req) {	
			req.onreadystatechange = function() {
				if(req.readyState == 4) {					
					lugarACarregar.innerHTML = req.responseText;	
					
					if(temFuncao) {	
						if(temParametro) {
							funcao = funcao + "(";
							for(i=0;i<par.length-1;i++) {
								funcao = funcao + "'" + par[i] + "', ";
							}
							funcao = funcao + "'" + par[i] + "'";							
							funcao = funcao + ");";
						} else {
							funcao = funcao + "();";
						}						
						eval(funcao);						
					}
					
				} else if(req.readyState > 4) {
					alert("erro: " + req.readyState);
				} else {
					if(req.readyState == 1) {
						lugarACarregar.innerHTML = htmlCarregando;
					}
				}
			}
			req.open("GET", url, true);
			req.send(null);			
		}

	} catch(e) {
		alert("Erro ao carregar a pagina!\n\nMotivo: " + e);		
	}
}

function carregarPaginaDadosEm(url, idLugar) {
  	try {
		
		urlPag = url.substring(0, url.indexOf("*"));
		
		if(idLugar=="") {
			return;
		} else {
			var lugarACarregar = document.getElementById(idLugar);	
		}
		
		if (url == "") {
			return;
		}
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			// Internet Explorer Windows
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if(req) {	
			req.onreadystatechange = function() {	
				if(req.readyState == 4) {					
					
					novoHTML = req.responseText;
					
					tempParam = url.substring(url.indexOf("*")+1, url.length);					
					cont = 0;
					parametros = new Array();
					
					while(tempParam.indexOf("&")!=-1) {
						parametros[cont] = tempParam.substring(0, tempParam.indexOf("&"));
						tempParam = tempParam.substring(tempParam.indexOf("&")+1, tempParam.length);
						cont++;					
					}
					parametros[cont] = tempParam;
					
					for(i=0;i<parametros.length;i++) {
						novoHTML = novoHTML.replace("!!"+(i+1)+"!!", parametros[i]);
					}
							
					lugarACarregar.innerHTML = novoHTML;					
										
					
				} else if(req.readyState > 4) {
					alert("erro: " + req.readyState);
				} else {
					if(req.readyState==1) {
						lugarACarregar.innerHTML = htmlCarregando;
					}
				}
			}
			req.open("GET", urlPag, true);
			req.send(null);
		}

	} catch(e) {
		alert("Erro ao carregar a pagina!\n\nMotivo: " + e);		
	}
}

var http_request = false;

function makePOSTRequest(url, idLocal, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			   http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}  
  	http_request.onreadystatechange = function() {
		if(http_request.readyState==4) {
			if(http_request.status==200) {
				result = http_request.responseText;	
				document.getElementById(idLocal).innerHTML = result;            	 
			} else {
				alert('There was a problem with the request.\n' + http_request.statusText);
			}
		}
	}	
	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 getFormDownload(obj, idLocal) {
  var poststr = "nome=" + encodeURI(document.getElementById("campoNome").value);
  poststr += "&profissao=" + encodeURI(document.getElementById("campoProfissao").value);
  poststr += "&endereco=" + encodeURI(document.getElementById("campoEndereco").value);
  poststr += "&cidade=" + encodeURI(document.getElementById("campoCidade").value);
  poststr += "&estado=" + encodeURI(document.getElementById("campoEstado").value);
  poststr += "&cep=" + encodeURI(document.getElementById("campoCEP").value);
  poststr += "&pais=" + encodeURI(document.getElementById("campoPais").value);
  poststr += "&telefone=" + encodeURI(document.getElementById("campoTelefone").value);
  poststr += "&email=" + encodeURI(document.getElementById("campoEmail").value);
  poststr += "&nomeDownload=" + encodeURI(document.getElementById("campoDownload").value);
  poststr += "&downloadId=" + encodeURI(document.getElementById("campoDownloadId").value);

  makePOSTRequest('enviaEmailDownload.php', idLocal, poststr);
}

function getFormCadastro(obj, idLocal) {
  var poststr = "nome=" + encodeURI(document.getElementById("campoNome").value);
  poststr += "&endereco=" + encodeURI(document.getElementById("campoEndereco").value);
  poststr += "&bairro=" + encodeURI(document.getElementById("campoBairro").value);
  poststr += "&cidade=" + encodeURI(document.getElementById("campoCidade").value);
  poststr += "&estado=" + encodeURI(document.getElementById("campoEstado").value);
  poststr += "&cep=" + encodeURI(document.getElementById("campoCEP").value);
  poststr += "&pais=" + encodeURI(document.getElementById("campoPais").value);
  //poststr += "&telefoneR=" + encodeURI(document.getElementById("campoTelefoneR").value);
  poststr += "&telefoneC=" + encodeURI(document.getElementById("campoTelefoneC").value);
  poststr += "&fax=" + encodeURI(document.getElementById("campoFax").value);
  poststr += "&email=" + encodeURI(document.getElementById("campoEmail").value);
  poststr += "&interesse=" + encodeURI(document.getElementById("campoInteresse").value);
  poststr += "&produtos=" + encodeURI(document.getElementById("campoProdutos").value);
  poststr += "&sugestao=" + encodeURI(document.getElementById("campoSugestao").value);
  makePOSTRequest('enviaEmailCadastro.php', idLocal, poststr);
}