function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function resetThis(ref,selected,txt){
	if (selected){
		if (ref.value == txt) ref.value = "";
		ref.type = "password";
	} else {
		if (!ref.value) ref.value = txt;
		ref.type = "text";
	}
}


var infoWindow;
function popinfo(file,w,h,xtra) {
	if (infoWindow){
		infoWindow.close();
	}
	if (!xtra) xtra='';
	if (!w) w=700;
	if (!h) h=550;
	infoWindow=window.open(file, "infoWindow", "height="+h+",width="+w+",resize=no,status=yes"+xtra);
}

var fotoWindow;
function popImg(filename,extraInfo) {
	var fundo = "#000000";
	var texto = "#999999";
	var extraHeight = extraInfo ? 60 : 0;
	if (fotoWindow){
		fotoWindow.close();
	}
	fotoWindow=window.open("", "fotoWindow", "height=150,width=150,resize=yes,scrollbars=yes,status=yes");
	fotoWindow.document.write("<HTML>");
	fotoWindow.document.write("<HEAD>");
	fotoWindow.document.write("<SCRIPT LANGUAGE=\"JavaScript\">function resizeMe(picRef) {MM_showHideLayers('tempLayer','','hide');if (window.screen){ var Wlimit = screen.width-70; var Hlimit = screen.height-80; } var picW = picRef.width+50; var picH = picRef.height+70+"+extraHeight+"; var targetW = (Wlimit) ? (picW > Wlimit) ? Wlimit : picW : picW; var targetH = (Hlimit) ? (picH > Hlimit) ? Hlimit : picH : picH; window.resizeTo(targetW,targetH); var window_left = (window.screen.width/2) - ((targetW/2) + 5); 	var window_top = (window.screen.height/2) - ((targetH/2) + 22); 	window.moveTo(window_left,window_top);} </SCR"+"IPT>");
	fotoWindow.document.write('<script language="JavaScript" type="text/JavaScript">function MM_reloadPage(init) {  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();}MM_reloadPage(true);function MM_findObj(n, d) { var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);if(!x && d.getElementById) x=d.getElementById(n); return x;}function MM_showHideLayers() { var i,p,v,obj,args=MM_showHideLayers.arguments;for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];if (obj.style) { obj=obj.style; v=(v==\'show\')?\'visible\':(v==\'hide\')?\'hidden\':v; }obj.visibility=v; }}</scr'+'ipt>');
	fotoWindow.document.write("<TITLE>Imagem</TITLE>");
	fotoWindow.document.write("<HEAD>");
	fotoWindow.document.write("<BO"+"DY onmouseover=\"window.status='';return true;\" bgcolor='"+fundo+"' leftmargin='10' topmargin='10' marginwidth='10' marginheight='10'>");
	fotoWindow.document.write('<div id="tempLayer" style="position:absolute; left:0; top:0; width:150; height:150; z-index:10; background-color: '+fundo+'; layer-background-color: '+fundo+'; border: 1px none #000000; visibility: visible; overflow: visible;"><table width="130" height="130"  border="0" cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle"><b><font color="'+texto+'" size="1" face="Verdana, Arial, Helvetica, sans-serif">Por favor aguarde.</b></td></tr></table></div>');
	fotoWindow.document.write("<A href=javascript:self.close()>");
	fotoWindow.document.write("<IMG name=foto id=foto src="+filename+" onLoad=resizeMe(this) border=0 alt=\"clique para fechar esta imagem\"></a>");
	if (extraInfo) {fotoWindow.document.write('<p><font color="#333333" size="1" face="Verdana, Arial, Helvetica, sans-serif">'+extraInfo+'</font></p>');}
	fotoWindow.document.write("</HTML>");
}

var flashWindow;
function popFlash(filename) {
	popinfo('/popupflash.shtml?'+filename,400,300)
}

function getFileName() {
	return self.location.href.substr(self.location.href.indexOf("?")+1);
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' Tem que ser um endereço de E-mail válido.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' tem que ser um número.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' tem que ser um número entre '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' é necessário.\n'; }
  } if (errors) alert('Ocorreram os seguintes erros:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function fillLayer(objName,newText) { 
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (document.layers) {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}

var cart = [];
var fullTotal = 0;

function updateList(ref) {
	fullTotal = 0;
	var id = ref.name.split("|,|")[0];
	cart[id] = new Array();
	if (ref.value) {
		cart[id].titulo = ref.name.split("|,|")[1];
		cart[id].preco = ref.name.split("|,|")[2];
		if (cart[id].preco.indexOf(",")>0) cart[id].preco = cart[id].preco.substr(0,cart[id].preco.indexOf(","))+"."+cart[id].preco.substr(cart[id].preco.indexOf(",")+1);
		cart[id].quant = (ref.value > 0 || ref.value < 100) ? ref.value : 0;
		cart[id].total = new Number(cart[id].quant*cart[id].preco);
		if (new Number(cart[id].quant) > 0) {
		} else {
			if (ref.value != "0") alert("A quantidade especificada não é válida!");
			ref.value = "";
		}
	}
	for (var x in cart) {
		fullTotal += cart[x].total;
	}
	if (fullTotal) {
		if (document.encomenda.envio.value == "normal") {
			fullTotal += 1.5;
		} else {
			fullTotal += 3.5;
		}
		fullTotal = fullTotal.toString();
		if (fullTotal.indexOf(".")>0) {
			fullTotal = fullTotal.substr(0,fullTotal.indexOf("."))+","+fullTotal.substr(fullTotal.indexOf(".")+1,2);
		}
		fillLayer("LayerInfo","<p class='txt_normal'>Preço total: <b>"+fullTotal+"€</b> (SEM PORTES DE ENVIO)</p>");
	} else {
		fillLayer("LayerInfo","");
	}
}
function resumeList() {
	document.encomenda.mensagem.value = "\n";
	for (var i in cart) {
		thisTotal = cart[i].total.toString();
		if (thisTotal.indexOf(".")>0) thisTotal = thisTotal.substr(0,thisTotal.indexOf("."))+","+thisTotal.substr(thisTotal.indexOf(".")+1,2);
		document.encomenda.mensagem.value += "Título: "+cart[i].titulo+"\nQuantidade pretendida: "+cart[i].quant+"\nPreço unitário apresentado: "+cart[i].preco+" Eur.\nPreço total: "+thisTotal+" Eur.\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\n";
	}
	if (!fullTotal) {
		alert("Para realizar uma encomenda, escolha no mínimo 1 artigo.");
		return false;
	} else if (!document.encomenda.nome.value || !document.encomenda.morada.value || !document.encomenda.pais.value || !document.encomenda.telefone.value || !document.encomenda.email.value) {
		alert("Todos os campos do formulário são necessários.");
		return false;
	} else {
		document.encomenda.mensagem.value += "TOTAL GLOBAL SEM PORTES: "+fullTotal+" Eur.\n\n";
		document.encomenda.mensagem.value += "DADOS PESSOAIS:\nNome: "+document.encomenda.nome.value;
		document.encomenda.mensagem.value += "\nMorada: "+document.encomenda.morada.value;
		document.encomenda.mensagem.value += "\nPaís: "+document.encomenda.pais.value;
		document.encomenda.mensagem.value += "\nTelefone: "+document.encomenda.telefone.value;
		document.encomenda.mensagem.value += "\nE-mail: "+document.encomenda.email.value+"\n";
		alert ("Dentro de dias irá receber no e-mail que especificou, uma confirmação da sua encomenda.\n\nAguarde agora a confirmação do envio da mensagem com a encomenda.");
	}
}
