// JavaScript Document

var appHash = "";
var appIndex = "";
var appDefault = "home.html";

var appFunction = function carregaPagina(url) {	
	makeHistory(url);
	okk = false;
	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("{"));
			if(funcao=="paginaProduto") {
				okk = true;	
			} else {
				okk = false;		
			}	
			temFuncao = true;
		} else {
			temFuncao = false;			
		}
				
		if(url == "") {
			alert("?????");
			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) {					
					if(okk) {
						window.setTimeout(recarregaPagProd, 1000, par[0]);
					}				
					tempHTML2 = req.responseText;
					if(lugarACarregar!=null) {
						//lugarACarregar.innerHTML = req.responseText;
						if(req.responseText.substring(0, strBug.length)!=strBug) {
							lugarACarregar.innerHTML = req.responseText;	
						} else {
							carregaPagina(url);
							return ;
						}
						
						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 "fonoaudiologia.html": {
								itemMenuId = 3;
								marcarMenu(3);								
								if(abaFono!=null) {
									abaClicar(abaFono, true);
								} else {
									abaClicar(0);
								}								
								break;	
							}
							case "fisioterapia.html": {
								itemMenuId = 4;
								marcarMenu(4);
								break;
							}
							case "emDesenvolvimento.html": {
								itemMenuId = 5;
								marcarMenu(5);
								break;
							}							
							case "atualizacoes.html": {
								itemMenuId = 6;
								marcarMenu(6);
								montaCheckProd2(3, "marcarCheckAtual", "tdCheck");
								//carregarPaginaEm("atualizaTodos.html", "corpoAtual");
								break;
							}							
							case "downloads.html": {
								itemMenuId = 7;
								marcarMenu(7);							
								carregarPaginaEm("listaDownloads.html", "divFormDown");
								break;
							}
							case "controleL.html": {
								itemMenuId = 13;
								marcarMenu(13);							
								//carregarPaginaEm("listaDownloads.html", "divFormDown");
								carregarControleL();
								break;
							}					
							case "cadastro.html": {
								if(!temFuncao) {									
									interesseId = 0;
								}								
								primeiroClique = true;
								itemMenuId = 8;
								marcarMenu(8);	
								break;
							}
							case "cadastroDesenv.html": {
								primeiroClique = true;
								itemMenuId = 8;
								marcarMenu(8);	
								break;
							}
							case "representantes.html": {
								itemMenuId = 9;
								marcarMenu(9);
								break;
							}
							case "suporte.html": {
								itemMenuId = 10;
								marcarMenu(10);
								montaCheckProd2(3, "marcarCheck", "tdProduto");
								break;
							}						
							case "contato.html": {
								marcarMenu(11);
								itemMenuId = 11;
								break;
							}
							case "faqs.html": {
								marcarMenu(10);
								itemMenuId = 10;
								break;
							}
							case "comprarProdutos.html": {
								if(!podeContinuarCompra) {
									window.history.back();
								} else {
									podeContinuarCompra = false;
								}								
								break;
							}
							case "audiReport.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(0);
								break;	
							}
							case "fonoView.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(1);
								break;	
							}
							case "avalie.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(2);
								break;	
							}
							case "pedroNaCasa.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(3);
								break;	
							}
							case "desafioFonemas.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(4);
								break;	
							}
							case "voiceReport.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(5);
								break;	
							}
							case "fonoTools.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(6);
								break;	
							}
							case "voxGames.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(7);
								break;	
							}
							case "fonoFlex.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(8);
								break;	
							}
							case "voxMetria.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(9);
								break;	
							}
							case "fonospeak.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(10);								
								break;	
							}
							case "fisiMetrix.html": {
								marcarMenu(4);
								itemMenuId = 4;
								carregaPagProd(12);								
								break;	
							}
							case "earMix.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(15);								
								break;	
							}
							case "pedroParque.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(16);								
								break;	
							}
							case "motrisis.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(17);								
								break;	
							}
							case "vocalgrama.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(18);								
								break;	
							}
							case "andreaPureAudioUSB.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(19);								
								break;	
							}
							case "memoTraining.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(22);								
								break;	
							}
							case "triAudi.html": {
								marcarMenu(3);
								itemMenuId = 3;
								carregaPagProd(23);								
								break;	
							}
						}						
						scrollTo(0,0);
						document.title = "CTS Informática - Softwares para Fonoaudiologia e Fisioterapia";
						/*
						if(temFuncao) {
							delete temFuncao;
							delete temParametro;
							delete funcao;
						}
						*/
					}
				} else if(req.readyState > 4) {
					alert("erro: " + req.readyState);
				} else if(req.readyState==1 && lugarACarregar!=null) {
					//lugarACarregar.innerHTML = htmlCarregando;	
					if(htmlCarregando.substring(0, strBug.length)!=strBug) {
						lugarACarregar.innerHTML = htmlCarregando;	
					} else {
						carregaPagina(url);
						return ;
					}
				} 				
			}
			req.open("GET", url, true);
			req.send(null);
		}
	} catch(e) {
		alert("Erro ao carregar a pagina!\n\nMotivo: " + e);		
	}
}

var isIe = (navigator.userAgent.toLowerCase().indexOf("msie") > -1) ? true : false;

function $(id) { return document.getElementById(id); }

function makeHistory(newHash) {
  if(isIe === true)
    $("control-iframe").setAttribute("src", "control.htm?id=" + newHash);
  else if(newHash != appIndex)
    window.location.hash = newHash;
  else
    go2index();
	//document.getElementById("divH").innerHTML += ("<br>" + window.history.length + " - " + newHash);
}

function handleHistory() {
  var browserHash = (hash = window.location.href.split("#")[1]) ? hash : "";
  if(browserHash != appHash) {
    if(browserHash != "") {
      appFunction(browserHash);
      appHash = browserHash;
      makeHistory(browserHash);
	}
	else {
	  clearInterval(checkInterval);
	  window.location.hash = appHash;
	  makeHistory(appIndex);
	}
  }
}

function createIFrame() {
  var iFrame = document.createElement("iframe");
  iFrame.setAttribute("src", "control.htm?id=" + (hash = window.location.href.split("#")[1]) ? hash : appDefault);
  iFrame.setAttribute("id", "control-iframe");
  iFrame.style.display = "none";
  document.body.appendChild(iFrame);
}

function initialize() {
  if(isIe === true)
    createIFrame();
  checkBookmark();
  checkInterval = setInterval(handleHistory, 100);
}

function checkBookmark() {
  var browserHash = (hash = window.location.href.split("#")[1]) ? hash : "";
  window.location.hash = (browserHash == "") ? appDefault : browserHash;
}

function go2index() {
  window.location = window.location.href.split("#")[0] + "#";
  window.location.reload();
}

function goTo(strUrl) {
	window.location.hash = strUrl;
	appFunction(strUrl);
}