/*
* Created: May, 2010. This notice must stay intact for usage 
* Author: Hugo Ribeiro
*/

function getHTTPObject(){
   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest) return new XMLHttpRequest();
   else {
      alert("Your browser does not support AJAX.");
      return null;
   }
}   

function setResposta(local){
	if(httpObject.readyState == 4){
		var response = httpObject.responseText;
		//alert(response);
		window.location=local;
	}
}

function sair(){
    httpObject = getHTTPObject();
	if (httpObject != null) {
		var yes = confirm("Tem a certeza que quer sair?")
		if(yes){
			httpObject.open("GET", "opt.php?do=sair", true);
			httpObject.onreadystatechange = function(xhr){setResposta("index.php")};
			httpObject.send(null);
		}
    }
}
/*
function insereSubCat(){
    httpObject = getHTTPObject();
	if (httpObject != null) {
		var pt = document.getElementById('subCatPT').value;
		var en = document.getElementById('subCatEN').value;
		var fr = document.getElementById('subCatFR').value;
		var url = url = document.URL;
		var part1 = url.split("edit=");
		var cat = parseInt(part1[1]);
		alert("opt.php?do=insereSubCat&pt="+pt+"&en="+en+"&fr="+fr+"&cat="+cat);
        httpObject.open("GET", "opt.php?do=insereSubCat&pt="+pt+"&en="+en+"&fr="+fr+"&cat="+cat, true);
		httpObject.onreadystatechange = function(xhr){setResposta(window.location)};
        httpObject.send(null);
    }
}

function insereCat(){
    httpObject = getHTTPObject();
	if (httpObject != null) {
		var pt = document.getElementById('catPT').value;
		var en = document.getElementById('catEN').value;
		var fr = document.getElementById('catFR').value;
		//alert("opt.php?do=insereCat&pt="+pt+"&en="+en+"&fr="+fr);
        httpObject.open("GET", "opt.php?do=insereCat&pt="+pt+"&en="+en+"&fr="+fr, true);
		httpObject.onreadystatechange = function(xhr){setResposta(window.location)};
        httpObject.send(null);
    }
}
*/
function remCat(idCat){
    httpObject = getHTTPObject();
	if (httpObject != null) {
        httpObject.open("GET", "opt.php?do=remCat&id="+idCat, true);
		httpObject.onreadystatechange = function(xhr){setResposta(window.location)};
        httpObject.send(null);
    }
}

function remSubCat(idSubCat){
    httpObject = getHTTPObject();
	if (httpObject != null) {
        httpObject.open("GET", "opt.php?do=remSubCat&id="+idSubCat, true);
		httpObject.onreadystatechange = function(xhr){setResposta(window.location)};
        httpObject.send(null);
    }
}
/*
function mudaNome(id){
    httpObject = getHTTPObject();
	if (httpObject != null) {
		var pt = document.getElementById('catPT').value;
		var en = document.getElementById('catEN').value;
		var fr = document.getElementById('catFR').value;	
        httpObject.open("GET", "opt.php?do=mudaNome&id="+id+"&pt="+pt+"&en="+en+"&fr="+fr, true);
		httpObject.onreadystatechange = function(xhr){setResposta('?admin=cat')};
        httpObject.send(null);	
	}
}

function mudaNomeSub(id){
    httpObject = getHTTPObject();
	if (httpObject != null) {
		var pt = document.getElementById('subCatPT').value;
		var en = document.getElementById('subCatEN').value;
		var fr = document.getElementById('subCatFR').value;	
		var cat = document.getElementById('idCat').value;	
        httpObject.open("GET", "opt.php?do=mudaNomeSub&id="+id+"&pt="+pt+"&en="+en+"&fr="+fr, true);
		httpObject.onreadystatechange = function(xhr){setResposta('?admin=cat&edit='+cat)};
        httpObject.send(null);	
	}
}
*/
function changeLang(ln){
	httpObject = getHTTPObject();
	if(httpObject!=null){	
        httpObject.open("GET", "opt.php?do=mudaLang&ln="+ln, true);
		httpObject.onreadystatechange = function(xhr){setResposta(window.location)};
        httpObject.send(null);	
	}	
}

function setSubCat(){
	if(httpObject.readyState == 4){
		var comboSubCat = document.getElementById('subCat');
		comboSubCat.options.length=0;
		var response = httpObject.responseText;
		var nomes = response.split("##");
		var valores = new Array();
		if((nomes.length-1)>0){
			for(var i=0; i<nomes.length-1;i++){
				valores[i]=nomes[i].split("$");
				comboSubCat.options[i] = new Option(valores[i][0], valores[i][1], true);
			}
		}else{
			comboSubCat.options[0] = new Option("Sem Sub Categoria", 0, true);
		}
	}
}

function loadSubCat(id){
	httpObject = getHTTPObject();
	if(httpObject!=null){	
	
        httpObject.open("GET", "opt.php?do=loadSubCat&id="+id, true);
		httpObject.onreadystatechange = setSubCat;
        httpObject.send(null);
	}
}

function doSearch(){
	httpObject = getHTTPObject();
	if(httpObject!=null){	
		var key = document.getElementById('textBoxPesquisa').value;
		window.location="index.php?do=search&key="+key;
	}
}

function remProd(id){
	httpObject = getHTTPObject();
	if(httpObject!=null){
		var yes = confirm("Tem a certeza que quer remover este produto?")
		if(yes){
	        httpObject.open("GET", "opt.php?do=remProd&id="+id, true);
			httpObject.onreadystatechange = function(xhr){setResposta(window.location)};
 	        httpObject.send(null);
		}
	}
}

function setLogin(local){
	if(httpObject.readyState == 4){
		response = httpObject.responseText;
		if(response==1){
			window.location=local;	
		}else{
			alert("Erro ao efectuar login!");		
		}
	}
}

function login(){
	httpObject = getHTTPObject();
	if(httpObject!=null){	
		var user = document.getElementById('user').value;
		var pw = document.getElementById('pw').value;
        httpObject.open("GET", "opt.php?do=login&user="+user+"&pw="+pw, true);
		httpObject.onreadystatechange = function(xhr){setLogin(window.location)};
        httpObject.send(null);
	}
}

function show(form){
	httpObject = getHTTPObject();
	if(httpObject!=null){	
        document.getElementById(form).style.display="block";
	}
}

function hide(form){
	httpObject = getHTTPObject();
	if(httpObject!=null){	
        document.getElementById(form).style.display="none";
	}
}

function enviaMail(local){
	if(httpObject.readyState == 4){
		response = httpObject.responseText;
		if(response==1){
			//alert("Contacto efectuado com sucesso!");	
			//window.location=local;	
			document.getElementById('aviso').innerHTML="Contacto efectuado com sucesso!";
			document.getElementById('fechar').style.display="block";
			//document.getElementById('tete').style.display="none";
		}else{
			//alert("Erro ao enviar o formulário de contacto!");	
			document.getElementById('aviso').innerHTML="Erro ao enviar o formulário de contacto!";
			document.getElementById('fechar').style.display="block";			
			//document.getElementById('tete').style.display="none";
		}
	}
}

function contactSend(){
	httpObject = getHTTPObject();
	if(httpObject!=null){
		var nome = document.getElementById('nome').value;
		var nrContr = document.getElementById('nrContr').value;
		if(document.form1.contacto[0].checked){
			var cnt = document.getElementById('email').value;
			var tipo = "Email";
		}else if(document.form1.contacto[1].checked){
			var cnt = document.getElementById('telefone').value;
			var tipo = "Telefone";
		}else if(document.form1.contacto[2].checked){
			var cnt = document.getElementById('fax').value;
			var tipo = "Fax";
		}
		
		var motivo = document.getElementById('motivo').value;
		var msg = document.getElementById('msg').value;
		if(nome=="" || cnt =="" || msg==""){
			alert("Por favor preencha todos os campos do formulario");
		}else{
	//		alert(nome+"-"+nrContr+"-"+cnt+"-"+motivo+"-"+msg);
			document.getElementById('aviso').innerHTML="<img src='style/images/loading.gif' /> A enviar email!";
			document.getElementById('aviso').style.display="block";
			document.getElementById('tete').style.display="block";
			document.getElementById('fechar').style.display="none";
			httpObject.open("GET", "opt.php?do=send"+"&nm="+nome+"&nr="+nrContr+"&cnt="+cnt+"&mtv="+motivo+"&msg="+msg+"&tipo="+tipo, true);
			httpObject.onreadystatechange = function(xhr){enviaMail(window.location)};
			httpObject.send(null);
		}
		
	}
}

function closeAviso(){
	httpObject = getHTTPObject();
	if(httpObject!=null){
		document.getElementById('fechar').style.display="none";
		document.getElementById('tete').style.display="none";
		document.getElementById('aviso').style.display="none";
	}
}

function doOldPw(){
	if(httpObject.readyState == 4){
		response = httpObject.responseText;
		if(document.getElementById('oldpw').value!=""){
			if(response==1){
				document.getElementById('validaoldpw').innerText="Correcto";
			}else{
				document.getElementById('validaoldpw').innerText="erro";
			}
		}else{
			document.getElementById('validaoldpw').innerText="";
		}
	}
}

function oldPW(){
	httpObject = getHTTPObject();
	if(httpObject!=null){
		var pw = document.getElementById('oldpw').value;
		httpObject.open("GET", "opt.php?do=validapw&pw="+pw, true);
		httpObject.onreadystatechange = doOldPw;
        httpObject.send(null);
	}
}

function newPW(){
	httpObject = getHTTPObject();
	if(httpObject!=null){
		var pw1 = document.getElementById('newpw1').value;
		var pw2 = document.getElementById('newpw2').value;
		if(pw1!="" && pw2!=""){
			if(pw1==pw2){
				document.getElementById('validanewpw').innerText="Correcto";			
			}else{
				document.getElementById('validanewpw').innerText="Erro ao repetir a nova password!";
			}
		}else{
			document.getElementById('validanewpw').innerText="";
		}
	}
}



function doMudaPw(){
	if(httpObject.readyState == 4){
		response = httpObject.responseText;
		if(response==1){
			alert("Password alterada com sucesso!");
			window.location="index.php";
		}else{
			alert("Erro ao alterar a password");
		}
	}
}

function mudaPW(){
	httpObject = getHTTPObject();
	if(httpObject!=null){
		var oldpw = document.getElementById('oldpw').value;
		var pw1 = document.getElementById('newpw1').value;
		var pw2 = document.getElementById('newpw2').value;
		if(pw1!="" && pw2!=""){
			if(pw1==pw2){
				httpObject.open("GET", "opt.php?do=mudapw&pw="+pw1+"&oldpw="+oldpw, true);
				httpObject.onreadystatechange = doMudaPw;
				httpObject.send(null);			
			}else{
				alert("Erro ao repetir a nova password!");
			}
		}else{
			alert("Nova password em branco!");
		}
	}
}


var httpObject = null;
