	function createRequestObject() {
		var req;
		if(window.XMLHttpRequest){
			req = new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			alert('There was a problem creating the XMLHttpRequest object');
		}
		return req;
	}
var http = createRequestObject();
	
function handleResponsePostFTP() {
	if(http.readyState == 1){
			window.scrollBy(0,-11000);
			document.getElementById("recebe").innerHTML = "" ;
			document.getElementById("carrega").style.visibility="visible";
		}
		else if(http.readyState == 4 ){
			if(http.status == 200){
				var response = http.responseText;
				if(response) {
					document.getElementById("recebe").style.visibility="hidden";
					document.getElementById("recebe").innerHTML = response;
					if (retroceder == 0){
						document.getElementById("botContinuar").style.display = ''
					}
					novo_conteudo = document.getElementById("recebe");
					h = novo_conteudo.offsetHeight;
					setTimeout("changeheight1(h)",1000);
					document.getElementById("carrega").style.visibility="hidden";
				}
			}
			else{
				var response = http.responseText;
				document.getElementById("recebe").innerHTML = response;
				document.getElementById("carrega").style.visibility="hidden";
			}
		}
	}

function nfolhas1(vai,nfolhas){
	window.focus();
	if (document.albuns.nomeProjecto){
		nomeProjecto = document.albuns.nomeProjecto.value
	}
	noivo = document.albuns.noivo.value
	idAlbum = document.albuns.idAlbum.value
	if (idAlbum == '') idAlbum=0
	pais = 0
	if (document.albuns.pais){
		pais = document.albuns.pais.value
	}
	http.open('post', vai, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponsePostFTP;
	http.send('nomeProjecto='+ escape(nomeProjecto) + '&noivo=' + escape(noivo) + '&nfolhas=' + nfolhas + '&album=' + idAlbum + '&pais=' + pais);

}

function nfolhas2(vai,idAlbum)
{
	document.albuns.nfolhas.value = '20'
	if (document.albuns.nomeProjecto){
		nomeProjecto = document.albuns.nomeProjecto.value
	}
	noivo = document.albuns.noivo.value
	idAlbum = document.albuns.idAlbum.value
	pais = document.albuns.pais.value
	window.focus();
	http.open('post', vai, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponsePostFTP;
	http.send('nomeProjecto='+ escape(nomeProjecto) + '&noivo=' + escape(noivo) + '&nfolhas=20&album=' + idAlbum + '&pais=' + pais);
}

// sendPostPaginacao()
// sendPostEnviarAlbuns()
// sendPostPosters()
// sendPostTelas()
// AREAL FTP

function carregarFuncao1(){
		window.focus();
		http.open('post', '../includes/arealFTP_paginar.asp', true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send("");
}

function carregarFuncao2(){
		window.focus();
		http.open('post', '../includes/arealFTP_albumNew.asp', true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send("");
}
var retroceder
function sendPostPaginarNew(vai,como){
		window.focus();
		retroceder  = como
		if (como == 0){
			if (document.albuns.nomeProjecto){
				nomeProjecto = document.albuns.nomeProjecto.value
			}
			noivo = document.albuns.noivo.value
			nfolhas = document.albuns.nfolhas.value
			idAlbum = document.albuns.idAlbum.value
			if (document.albuns.pais.checked = true) pais='sim'
			if (document.albuns.pais.checked = true) pais='nao'
		}
		if (como == 1){
			nomeProjecto = ''
			noivo = ''
			nfolhas = ''
			idAlbum = 0
			pais = ''
		}
		http.open('post', vai, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send('nomeProjecto='+ escape(nomeProjecto) + '&noivo=' + escape(noivo) + '&nfolhas=' + nfolhas + '&idAlbum=' + idAlbum + '&pais=' + pais);
}

function paginarPlastificacao(vai,como) {
		window.focus();
		retroceder  = como
		if (como == 0){
			if (document.albuns.nomeProjecto){
				nomeProjecto = document.albuns.nomeProjecto.value
			}
			noivo = document.albuns.noivo.value
			nfolhas = document.albuns.nfolhas.value
			idAlbum = document.albuns.idAlbum.value
			if (document.albuns.pais){
				if (document.albuns.pais.checked == true){
					pais = document.albuns.pais.value
				}
				else{
					pais=''
				}
			}
			else
			{
				pais=''
			}
			idPlastificacao = document.albuns.idPlastificacao.value
			idGuarda = document.albuns.idGuarda.value
			
		}
		if (como == 1){
			if (document.albuns.nomeProjecto){
				nomeProjecto = document.albuns.nomeProjecto.value
			}
			noivo = document.albuns.noivo.value
			nfolhas = document.albuns.nfolhas.value
			idAlbum = document.albuns.idAlbum.value
			if (document.albuns.pais){
				if (document.albuns.pais.checked == true){
					pais = document.albuns.pais.value
				}
				else{
					pais=''
				}
			}
			else
			{
				pais=''
			}
			idPlastificacao = 0
			idGuarda = 0
		}
		for (i=0 ; i < nomeProjecto.length; i++){ nomeProjecto = nomeProjecto.replace(/ /,"%20"); }
		for (i=0 ; i < noivo.length; i++){ noivo = noivo.replace(/ /,"%20"); }
		http.open('post', vai, true);
        http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.onreadystatechange = handleResponsePostFTP;
		http.send('nomeProjecto='+ escape(nomeProjecto) + '&noivo=' + escape(noivo) + '&nfolhas=' + nfolhas + '&idAlbum=' + idAlbum + '&pais=' + pais + '&idPlastificacao=' + idPlastificacao + '&idGuarda=' + idGuarda);
	}

function valida(qual)
{
	if (qual==1)
	{
		document.albuns.status1.value=1
	}
	if (qual==2)
	{
		document.albuns.status2.value=1
	}
	if (document.albuns.status1.value==1 && document.albuns.status2.value==1)
	{
		document.getElementById('botContinuar').style.display = '';
	}
}

function PaginarCapas(vai,como) {
		window.focus();
		retroceder  = como
		if (como == 0){
			if (document.albuns.nomeProjecto){
				nomeProjecto = document.albuns.nomeProjecto.value
			}
			noivo = document.albuns.noivo.value
			nfolhas = document.albuns.nfolhas.value
			idAlbum = document.albuns.idAlbum.value
			pais = document.albuns.pais.value
			idPlastificacao = document.albuns.idPlastificacao.value
			idGuarda = document.albuns.idGuarda.value
			idCapa = document.albuns.idCapa.value
			idCosta = document.albuns.idCosta.value
			
		}
		if (como == 1){
			if (document.albuns.nomeProjecto){
				nomeProjecto = document.albuns.nomeProjecto.value
			}
			noivo = document.albuns.noivo.value
			nfolhas = document.albuns.nfolhas.value
			idAlbum = document.albuns.idAlbum.value
			pais = document.albuns.pais.value
			idPlastificacao = document.albuns.idPlastificacao.value
			idGuarda = document.albuns.idGuarda.value
			idCapa = 0
			idCosta = 0
		}
		for (i=0 ; i < nomeProjecto.length; i++){ nomeProjecto = nomeProjecto.replace(/ /,"%20"); }
		for (i=0 ; i < noivo.length; i++){ noivo = noivo.replace(/ /,"%20"); }
		http.open('post', vai, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send('nomeProjecto='+ escape(nomeProjecto) + '&noivo=' + escape(noivo) + '&nfolhas=' + nfolhas + '&idAlbum=' + idAlbum + '&pais=' + pais + '&idPlastificacao=' + idPlastificacao + '&idGuarda=' + idGuarda + '&idCapa=' + idCapa + '&idCosta=' + idCosta);
	}

function PaginarMalas(vai,como) {
		window.focus();
		retroceder  = como
		if (como == 0){
			if (document.albuns.nomeProjecto){
				nomeProjecto = document.albuns.nomeProjecto.value
			}
			noivo = document.albuns.noivo.value
			nfolhas = document.albuns.nfolhas.value
			idAlbum = document.albuns.idAlbum.value
			pais = document.albuns.pais.value
			idPlastificacao = document.albuns.idPlastificacao.value
			idGuarda = document.albuns.idGuarda.value
			idCapa = document.albuns.idCapa.value
			idCosta = document.albuns.idCosta.value
			idMala = document.albuns.idMala.value
			
		}
		if (como == 1){
			if (document.albuns.nomeProjecto){
				nomeProjecto = document.albuns.nomeProjecto.value
			}
			noivo = document.albuns.noivo.value
			nfolhas = document.albuns.nfolhas.value
			idAlbum = document.albuns.idAlbum.value
			pais = document.albuns.pais.value
			idPlastificacao = document.albuns.idPlastificacao.value
			idGuarda = document.albuns.idGuarda.value
			idCapa = document.albuns.idCapa.value
			idCosta = document.albuns.idCosta.value
			idMala = 0
		}
		for (i=0 ; i < nomeProjecto.length; i++){ nomeProjecto = nomeProjecto.replace(/ /,"%20"); }
		for (i=0 ; i < noivo.length; i++){ noivo = noivo.replace(/ /,"%20"); }
		http.open('post', vai, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send('nomeProjecto='+ escape(nomeProjecto) + '&noivo=' + escape(noivo) + '&nfolhas=' + nfolhas + '&idAlbum=' + idAlbum + '&pais=' + pais + '&idPlastificacao=' + idPlastificacao + '&idGuarda=' + idGuarda + '&idCapa=' + idCapa + '&idCosta=' + idCosta + '&idMala=' + idMala);
	}

function PaginarResultado(vai,como) {
		window.focus();
		retroceder  = como
		if (como == 0){
			if (document.albuns.nomeProjecto){
				nomeProjecto = document.albuns.nomeProjecto.value
			}
			noivo = document.albuns.noivo.value
			nfolhas = document.albuns.nfolhas.value
			idAlbum = document.albuns.idAlbum.value
			pais = document.albuns.pais.value
			idPlastificacao = document.albuns.idPlastificacao.value
			idGuarda = document.albuns.idGuarda.value
			idCapa = document.albuns.idCapa.value
			idCosta = document.albuns.idCosta.value
			idMala = document.albuns.idMala.value
			
		}
		if (como == 1){
			if (document.albuns.nomeProjecto){
				nomeProjecto = document.albuns.nomeProjecto.value
			}
			noivo = document.albuns.noivo.value
			nfolhas = document.albuns.nfolhas.value
			idAlbum = document.albuns.idAlbum.value
			pais = document.albuns.pais.value
			idPlastificacao = document.albuns.idPlastificacao.value
			idGuarda = document.albuns.idGuarda.value
			idCapa = document.albuns.idCapa.value
			idCosta = document.albuns.idCosta.value
			idMala = document.albuns.idMala.value
		}
		for (i=0 ; i < nomeProjecto.length; i++){ nomeProjecto = nomeProjecto.replace(/ /,"%20"); }
		for (i=0 ; i < noivo.length; i++){ noivo = noivo.replace(/ /,"%20"); }
		http.open('post', vai, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send('nomeProjecto='+ escape(nomeProjecto) + '&noivo=' + escape(noivo) + '&nfolhas=' + nfolhas + '&idAlbum=' + idAlbum + '&pais=' + pais + '&idPlastificacao=' + idPlastificacao + '&idGuarda=' + idGuarda + '&idCapa=' + idCapa + '&idCosta=' + idCosta + '&idMala=' + idMala);
	}
	
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {
    var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
    if (decimal_location == -1) {
        decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
function portes(valor,portes)
{
	if (valor == 0)
	{
		document.getElementById("DIViva").innerHTML = round_decimals((parseFloat(document.albuns.total__.value)) * 0.21,2) + '€'
		document.getElementById("DIVtotalIVA").innerHTML = round_decimals((parseFloat(document.albuns.total__.value)) * 1.21,2) + '€'
		document.getElementById('botContinuar').style.display = ''
		document.getElementById("divPortes").innerHTML = ''

	}
	if (valor == 1)
	{
		document.getElementById("divPortes").innerHTML = portes + '&nbsp;&#8364;'
		document.getElementById("DIVtotal").innerHTML = round_decimals((parseFloat(document.albuns.total__.value) + portes),2) + '€'
		document.getElementById("DIViva").innerHTML = round_decimals((parseFloat(document.albuns.total__.value) + portes) * 0.21,2) + '€'
		document.getElementById("DIVtotalIVA").innerHTML = round_decimals((parseFloat(document.albuns.total__.value) + portes) * 1.21,2) + '€'
		document.albuns.total.value = round_decimals(parseFloat(document.albuns.total__.value) + portes,2)
		document.albuns.Vportes.value = portes
		document.getElementById("botContinuar").style.display = ''
	}
	if (valor == 2)
	{
		document.getElementById("divPortes").innerHTML = '0 €'
		document.getElementById("DIVtotal").innerHTML = round_decimals((parseFloat(document.albuns.total__.value)),2) + '€'
		document.getElementById("DIViva").innerHTML = round_decimals((parseFloat(document.albuns.total__.value)) * 0.21,2) + '€'
		document.getElementById("DIVtotalIVA").innerHTML = round_decimals((parseFloat(document.albuns.total__.value)) * 1.21,2) + '€'
		document.albuns.total.value = round_decimals(parseFloat(document.albuns.total__.value),2)
		document.getElementById("botContinuar").style.display = ''
		document.albuns.Vportes.value = 0
	}
}

function gravarEncomendaPaginacao() {
		window.focus();
		produto = document.albuns.produto.value
		Vportes = document.albuns.Vportes.value
		total = document.albuns.total.value
		http.open('post', '../includes/arealFTP_funcoes.asp', true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send('fazer=gravar_encomendaPaginacao&produto='+ produto + '&total=' + total + '&Vportes=' + Vportes);
	}
function detalheEncomendaPaginacao(idEncomenda){
		http.open('post', '../includes/arealFTP_paginarDetalheEncomenda.asp', true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send('idEncomenda='+idEncomenda);
}

function gravarEncomendaAlbum() {
		window.focus();
		produto = document.albuns.produto.value
		total = document.albuns.total.value
		Vportes = document.albuns.Vportes.value
		http.open('post', '../includes/arealFTP_funcoes.asp', true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send('fazer=gravar_encomenda&produto='+ produto + '&total=' + total + '&Vportes=' + Vportes);
	}
	
// POSTERS

function sendPostPosters(como){
		window.focus();
		if (como==0){
			var nPosters = document.albuns.n.value
			var string
			var poster
			string = ''
			for (i=1 ; i <= nPosters ; i++){
				if (document.getElementById("idPoster"+i)!=0){
					poster = document.getElementById("idPoster"+i).value
				}
				else
				{
					poster = 0
				}
				if (i == 1){
					string += 'poster'+i+'='+poster
				}else{
					string += '&poster'+i+'='+poster		
				}
			}
		}
		http.open('post', '../includes/arealFTP_poster.asp', true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send(string+'&back=1');
}

function posterResultado(vai,nPosters){
		window.focus();
		var string
		var poster
		string = ''
		for (i=1 ; i < nPosters ; i++){
			if (document.getElementById("poster"+i).checked){
				poster = document.getElementById("poster"+i).value
			}
			else
			{
				poster = ''
			}
			if (i == 1){
				string += 'poster'+i+'='+poster
			}else{
				string += '&poster'+i+'='+poster		
			}
		}
		http.open('post', vai, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send(string);
}

function recalcular(quant,valor,n)
{
	var nn = document.albuns.nn.value
	var Vtotal=0
	document.getElementById("n"+n).innerHTML = round_decimals((parseFloat(quant)*parseFloat(valor)),2) + '€'
	document.getElementById("preco"+n).value = round_decimals((parseFloat(quant)*parseFloat(valor)),2)
	for (i=1; i<nn; i++)
	{
		a=1
		Vtotal = round_decimals(parseFloat(Vtotal) + parseFloat(document.getElementById("preco"+i).value),2)
		a=a+1
	}
	document.getElementById("DIViva").innerHTML = round_decimals((parseFloat(Vtotal) + parseFloat(document.albuns.Vportes.value))-((parseFloat(Vtotal) + parseFloat(document.albuns.Vportes.value))/1.21),2 ) + '€'
	document.getElementById("DIVtotal").innerHTML = round_decimals(parseFloat(Vtotal) + parseFloat(document.albuns.Vportes.value),2 ) + '€'
	document.albuns.total.value = Vtotal
}
function gravarEncomendaPoster() {
		window.focus();
		var total = document.albuns.total.value
		var Vportes = document.albuns.Vportes.value
		var nPosters = document.albuns.n.value
		var string
		var poster
		var quantidade
		var preco
		var stringQuantidade
		var stringPreco
		var stringTotal
		string = ''
		stringQuantidade = ''
		stringPreco = ''
		var j = 1
		for (i=1 ; i <= nPosters ; i++){
				
			if (document.getElementById("idPoster"+i).value != 0){
				poster = document.getElementById("idPoster"+i).value
				quantidade = document.getElementById("quantidade"+j).value
				preco = document.getElementById("preco"+j).value
				stringQuantidade += '&quantidade'+j+'='+quantidade
				stringPreco += '&preco'+j+'='+preco
				j++
			}
			else
			{
				poster = 0
			}
			string += '&idPoster'+i+'='+poster
		}
		stringTotal = string + stringQuantidade + stringPreco + '&total='+total+'&Vportes='+Vportes
		http.open('post', '../includes/arealFTP_funcoes.asp', true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send('fazer=gravar_encomenda_poster' + stringTotal);
	}
	
	
// TELAS

function sendPostTelas(como){
		window.focus();
		if (como==0){
			var nPosters = document.albuns.n.value
			var string
			var tela
			string = ''
			for (i=1 ; i <= nPosters ; i++){
				if (document.getElementById("idTela"+i)!=0){
					tela = document.getElementById("idTela"+i).value
				}
				else
				{
					tela = 0
				}
				if (i == 1){
					string += 'tela'+i+'='+tela
				}else{
					string += '&tela'+i+'='+tela		
				}
			}
		}
		http.open('post', '../includes/arealFTP_tela.asp', true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send(string+'&back=1');
}

function telaResultado(vai,nPosters){
		window.focus();
		var string
		var tela
		string = ''
		for (i=1 ; i < nPosters ; i++){
			if (document.getElementById("tela"+i).checked){
				tela = document.getElementById("tela"+i).value
			}
			else
			{
				tela = ''
			}
			if (i == 1){
				string += 'tela'+i+'='+tela
			}else{
				string += '&tela'+i+'='+tela		
			}
		}
		http.open('post', vai, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send(string);
}

function gravarEncomendaTela() {
		window.focus();
		var total = document.albuns.total.value
		var Vportes = document.albuns.Vportes.value
		var nTelas = document.albuns.n.value
		var string
		var tela
		var quantidade
		var preco
		var stringQuantidade
		var stringPreco
		var stringTotal
		string = ''
		stringQuantidade = ''
		stringPreco = ''
		var j = 1
		for (i=1 ; i <= nTelas ; i++){
				
			if (document.getElementById("idTela"+i).value != 0){
				tela = document.getElementById("idTela"+i).value
				quantidade = document.getElementById("quantidade"+j).value
				preco = document.getElementById("preco"+j).value
				stringQuantidade += '&quantidade'+j+'='+quantidade
				stringPreco += '&preco'+j+'='+preco
				j++
			}
			else
			{
				tela = 0
			}
			string += '&idTela'+i+'='+tela
		}
		stringTotal = string + stringQuantidade + stringPreco + '&total='+total+'&Vportes='+Vportes
		http.open('post', '../includes/arealFTP_funcoes.asp', true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send('fazer=gravar_encomenda_tela' + stringTotal);
	}
	
function verEncomendasFTP(onde){
		http.open('post', onde, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send("");
}
function verEncomendasDetalheFTP(onde,idEncomenda){
		http.open('post', onde, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponsePostFTP;
		http.send('idEncomenda='+idEncomenda);
}
