
function GetXmlHttpObject(){ 
	var objXMLHttp=null
	if (window.XMLHttpRequest){
	  objXMLHttp=new XMLHttpRequest()
	}else if (window.ActiveXObject){
	  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 

function executaAllScript(texto){
    var i = 0;
	
	var ini, pos_src, fim, codigo;
	
    var objScript = null;
    ini = texto.toLowerCase().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;
    }
}

//Inicia o div de conteudo
function setDivLoading(width, height, msg){
	obj = document.getElementById('divLoading');
	
	obj.style.height = height;
	obj.style.width = width;
	
	obj.style.top = '';
	obj.style.left = '';
	
	obj.style.marginTop = document.body.scrollTop - (height/2);
	obj.style.marginLeft = -(width/2);
	
	obj.innerHTML = '<table align="center" height="100%" border="0" cellpadding="0" cellspacing="0"><tr><td class="carregando"><img src="images/imgLoading.gif">&nbsp;Carregando...</td></tr></table>';
	
	obj.style.display = 'block';	
}

function closeDivLoading(){
	obj = document.getElementById('divLoading');
	
	obj.innerHTML = '';
	obj.style.display = 'none';
}

//Inicia o div de conteudo
function setDivImage(width, height, msg){
	obj = document.getElementById('divImage');
	
	obj.style.height = height;
	obj.style.width = width;
	
	obj.style.top = '';
	obj.style.left = '';
	
	obj.style.marginTop = document.body.scrollTop - (height/2);
	obj.style.marginLeft = -(width/2);
	
	obj.innerHTML = '<table align="center" height="100%" border="0" cellpadding="0" cellspacing="0"><tr><td class="carregando"><img src="images/imgLoading.gif">&nbsp;Carregando...</td></tr></table>';
	
	obj.style.display = 'block';	
}

function closeDivImage(){
	obj = document.getElementById('divImage');
	
	obj.innerHTML = '';
	obj.style.display = 'none';
}

function showCity(id){
	window.location = '?page=showCity&code='+id;
	
	return false;
/*	
	setDivLoading(document.body.clientWidth, document.body.clientHeight, 'TESTE');
	
	var url = 'rede/lojas/showStore.php?id='+id;

	page=GetXmlHttpObject()
	if (page==null){
		alert ("Browser does not support HTTP Request")
		return
	}   
	
	page.onreadystatechange=function state(){ 
			if (page.readyState==4 || page.readyState=="complete"){ 
				closeDivLoading();
				if(page.status == 200){
					document.getElementById("divHome").innerHTML = page.responseText;
				}else{
					document.getElementById("divHome").innerHTML = page.statusText;
				}
			}
		}
	page.open("GET",url,true);
	page.send(null);*/
}

function showCityLocaly(id){
	
	window.location = '?page=showCity&code='+id;
	
	return false;
	
	
	
/*	setDivLoading(document.body.clientWidth, document.body.clientHeight, 'TESTE');
	
	var url = 'rede/lojas/showStore.php?id='+id;

	page=GetXmlHttpObject()
	if (page==null){
		alert ("Browser does not support HTTP Request")
		return
	}   
	
	page.onreadystatechange=function state(){ 
			if (page.readyState==4 || page.readyState=="complete"){ 
				closeDivLoading();
				if(page.status == 200){
					document.getElementById("divStore").innerHTML = page.responseText;
				}else{
					document.getElementById("divStore").innerHTML = page.statusText;
				}
			}
		}
	page.open("GET",url,true);
	page.send(null);*/
}

function equipamentos(){
	
	setDivLoading(document.body.clientWidth, document.body.clientHeight, 'TESTE');
	
	var url = 'equipamentos.php';

	page=GetXmlHttpObject()
	if (page==null){
		alert ("Browser does not support HTTP Request")
		return
	}   
	
	page.onreadystatechange=function state(){ 
			if (page.readyState==4 || page.readyState=="complete"){ 
				closeDivLoading();
				if(page.status == 200){
					document.getElementById("divHome").innerHTML = page.responseText;
				}else{
					document.getElementById("divHome").innerHTML = page.statusText;
				}
			}
		}
	page.open("GET",url,true);
	page.send(null);
}

function loadPage(id, session, lenguage, param){
	switch(id){
		case 'equipamentos'	:	window.location = '?page=equipamentos';
								break;
	}
	
	return false;
	
	
/*	setDivLoading(document.body.clientWidth, document.body.clientHeight, 'Carregando...');

	if(id == 'index'){
		window.location = 'index.php?'+param;
		return;
	}else{
		var url = session+'/'+id+'_'+lenguage+'.php?'+param;
	}

	page=GetXmlHttpObject()
	if (page==null){
		alert ("Browser does not support HTTP Request")
		return
	}   
	
	page.onreadystatechange=function state(){ 
			if (page.readyState==4 || page.readyState=="complete"){ 
				closeDivLoading();
				if(page.status == 200){
					document.getElementById("divHome").innerHTML = page.responseText;
					executaAllScript(page.responseText);
				}else{
					document.getElementById("divHome").innerHTML = page.statusText;
				}
			}
		}
	page.open("GET",url,true);
	page.send(null);*/
	
}

function sendForm(form, destiny){
	setDivLoading(document.body.clientWidth, document.body.clientHeight, 'Carregando...');

	var url = destiny+'.php'+getFormValues(form);

	page=GetXmlHttpObject()
	if (page==null){
		alert ("Browser does not support HTTP Request")
		return
	}   
	
	page.onreadystatechange=function state(){ 
			if (page.readyState==4 || page.readyState=="complete"){ 
				closeDivLoading();
				if(page.status == 200){
					document.getElementById("divHome").innerHTML = page.responseText;
				}else{
					document.getElementById("divHome").innerHTML = page.statusText;
				}
			}
		}
	page.open("POST",url,true);
	page.send(null);
	
}

function sendNewsLetter(nome, email){
	//setDivLoading(document.body.clientWidth, document.body.clientHeight, 'Carregando...');
	var div = document.getElementById('divNewsLetter');
	
	div.innerHTML = '<table align="center" height="100%" border="0" cellpadding="0" cellspacing="0"><tr><td class="carregando"><img src="images/imgLoading.gif">&nbsp;Cadastrando...</td></tr></table>';

	var url = 'functions/newsletter.php?action=addNewsLetter&nome='+nome+'&email='+email;

	page=GetXmlHttpObject()
	if (page==null){
		alert ("Browser does not support HTTP Request")
		return
	}   
	
	page.onreadystatechange=function state(){ 
		if (page.readyState==4 || page.readyState=="complete"){ 
			if(page.status == 200){
				div.innerHTML = page.responseText;
				//executaAllScript(page.responseText);
			}else{
				div.innerHTML = page.statusText;
			}
		}
	}
	page.open("GET",url,true);
	page.send(null);
	
}

function casaMaisPerto(zipCode){
	window.location = '?page=casaMaisPerto&zipCode='+zipCode;
	
	return false;
}
