var _aniCurr = 0;

var xpress = {
		faq_items: new Array(),
		not_frm: new Array(),
		price_strings: new Array(),
		sflist: new Array(), // liste mit allen aktivierten sonderfarben feldern
		Start: function() {
			this.SetNotFormat();
			this.SetComboBox(true);
			//this.SetupInfo();
		},
		UpdateAuflage: function(val) {
			sel = document.getElementsByTagName('select');
			for(i=0;i<sel.length;i++)
				if(sel[i].name=="auflage")
					for(j=0;j<sel[i].options.length;j++)
						if(sel[i].options[j].value==val)
							sel[i].options[j].selected=true;
						else
							sel[i].options[j].selected=false;
		},
		InitLieferkosten: function() {
			lf.each(function(val,x) {
				if(val==undefined) return;
				var cblf = 'lf_'+x;
				var cbza = 'za_'+x;

				var cbxlf = $(cblf);
				var cbxza = $(cbza);

				cbxlf.lfID=x;
				cbxza.lfID=x;

				this.RemoveAllItems(cblf);
				this.RemoveAllItems(cbza);

				cbxlf.onchange=function() {
					var selid = this.options[this.selectedIndex].value;
					lf[this.lfID]['dlvID']=this.options[this.selectedIndex].value;
					xpress.InitLieferkosten();
				}

				cbxza.onchange=function() {
					lf[this.lfID]['zaID']=this.options[this.selectedIndex].value;
					xpress.RecalcKasse();
				}
				lf[x]['dlv'].each(function(dlv,lID) {
					if(dlv==undefined) return;
					var text = dlv['n'];
					if(dlv['p']>0) text+=" ("+numf(dlv['p'])+"€)";
					this.AddComboBox(cblf,lID,text,(lf[x]['dlvID']==lID)?1:0);
					if(lf[x]['dlvID']==lID) {
						var fID = 0;
						dlv['zm'].each(function(zm,zID) {
						if(zm==undefined) return;
							if(!fID) fID=zID;
							var text = zm['n']
							if(zm['p']>0) text+=" ("+numf(zm['p'])+"€)";
							xpress.AddComboBox(cbza,zID,text,(lf[x]['zaID']==zID)?1:0);
							if(lf[x]['zaID']==zID) fID=-1;
						});
						if(fID>0) lf[x]['zaID']=fID;
					}
				},this);
				if($("inv_"+x)!==null)
				if(lf[x]['dlv'][cbxlf.options[cbxlf.selectedIndex].value]['vs']==0) {
					var cbxnv  = $("inv_"+x);
					var nvt = $("inv_txt_"+x);
					cbxnv.checked=0;
					cbxnv.disabled=1;
					nvt.style.color="gray";
					nvt.innerHTML="<b>Neutraler Versand</b> - Bei Selbstabholung ist dieses Feature nicht möglich";
				} else {
					var cbxnv  = $("inv_"+x);
					var nvt = $("inv_txt_"+x);
					cbxnv.disabled=0;
					nvt.style.color="black";
					nvt.innerHTML="<b>Neutraler Versand</b> - Absender wird dem Rechnungsempfänger entsprechen";
				}
			},this);
			this.RecalcKasse();
		},
		RecalcKasse: function() {
			var gnetto = 0;
			var glk = 0;

			lf.each(function(val,x) {
				if(val==undefined) return;
				var prs = 0;
				var elem = $("lk_"+x);

				prs = lf[x]['dlv'][lf[x]['dlvID']]['p'];
				prs += lf[x]['dlv'][lf[x]['dlvID']]['zm'][lf[x]['zaID']]['p'];

				gnetto+=lf[x]['p'];
				glk+=prs;

				elem.innerHTML=numf(prs)+"€";
			});
			var mw = ((gnetto+glk)*mwst)-(gnetto+glk);
			$("gp_n").innerHTML=numf(gnetto)+" &euro;";
			$('gp_lk').innerHTML=numf(glk)+" €";
			$('gp_mw').innerHTML=numf(mw)+" €";
			$('gp_p').innerHTML=numf(gnetto+glk+mw)+" €";
		},
		InitPreisTable: function() {
			var tbody = $('prs_tbl').getElementsByTagName("TBODY")[0];
			var col = new Array();
			prstb.each(function(prs) {
				if(prs==undefined) return;
				var row = document.createElement("TR");
				var stk = document.createElement("TD");
				stk.appendChild(document.createTextNode(numf(prs['afl'],0,',','.')+' Stk.'));
				stk.style.borderRight="3px solid white";
				row.appendChild(stk);
				col[0]=0;

				if(prs['eco']==0) {col[1]=1;col[2]=2;}
				if(prs['exp']==0) {col[5]=5;col[6]=6;}
				if(prs['nd']==0)  {col[7]=7;col[8]=8;}

				addPriceElement(prs['eco'],true);
				addPriceElement(prs['std'],true);
				addPriceElement(prs['exp'],true);
				addPriceElement(prs['nd'],false);

				function addPriceElement(p,border) {
					var td1 = document.createElement("TD");
					var td2 = document.createElement("TD");


					if(p>0) {
						td1.appendChild(document.createTextNode("€"+numf(p)))
						td2.appendChild(document.createTextNode("€"+numf(p*mwst)));
					}
					else {
						td1.appendChild(document.createTextNode("-"))
						td2.appendChild(document.createTextNode("-"))
					}
					td1.style.fontWeight="normal";
					if(border) td2.style.borderRight="3px solid white";
					row.appendChild(td1);
					row.appendChild(td2);
				};

				tbody.appendChild(row);
			});

			prs=rs_tb.create('prs','grid','row');
			prs.SelectionTyp('cross');
			prs.DisableRowsReaction(new Array(0,1));

			prs.DisableColumsReaction(col);
			prs.DisableColumsHigh(new Array(0,1));
			prs.BindColums(new Array(new Array(1,2),new Array(3,4),new Array(5,6),new Array(7,8)));
			prs.SetRowHeader(1);
			var i=2;
			prstb.each(function(afl) {
				if(afl==undefined) return;
				prs.SetRowData(i++,afl['afl']);
				if(auflage==afl['afl']) {
					x=(($('idTarif').value-1)*2)-1;
					prs.SetSelection(x,i-1);
					prs.ModifySelectedRow(afl['afl']);
				}
			});


			var prsClick = function(x,y) {
				var sel = prs.GetSelection();
				y=Math.floor((sel[0]-1)/2)+2;
				trm.ModifySelectedY(y);
				xpress.UpdateAuflage(prs.GetRowData(sel[1]));
				auflage=prs.GetRowData(sel[1]);
				tar = $('idTarif');
				tar.value=y;
				xpress.Recalc();
			}
			prs.SetClickCallback(prsClick);

		},
		Display: function(id,value) {
			var element = $(id);
			element.style.display=''+value;
		},
		RemoveAllItems: function(id) {
			var chbox = $(id);
			for (i = chbox.length - 1; i>=0; i--)
				chbox.remove(i);
		},
		faq_start: function() {
			for(i=0;i<this.faq_items.length;i++) {
				div = $(this.faq_items[i]);
				div.className="hidden";
			}
		},
		faq_click: function(id) {
			for(i=0;i<this.faq_items.length;i++) {
				div = $(this.faq_items[i]);
				div.className="hidden";
			}

			div = $('faq_'+id);
			div.className="";
		},
		faq_add: function(id) {
			this.faq_items[this.faq_items.length]=id;
		},
		AddComboBox: function(id,val,text,selected) {
			var opt = document.createElement('option');
			opt.text = text;
			opt.value = val;
			opt.selected=selected;
			var chbox = $(id);
			try {chbox.add(opt, null);} // standard version auser IE
			catch(ex) {chbox.add(opt);} // IE version
		},
		UpdateLastSelection: function() {
			eign.each(function(attrib,aID) {
				if(attrib==undefined) return;
				chbox = $('sel_'+aID);
				if(chbox.selectedIndex>=0)
					lst[aID]=chbox.options[chbox.selectedIndex].value;
			},this);
		},
		SetNotFormat: function() {
			this.not_frm=new Array();
			format = $('format');
			frmID = format.options[format.selectedIndex].value;
			if(typeof(mod_tbf[frmID])!="undefined") {
				mod_tbf[frmID].each(function(val,notID) {
					if(val==undefined) return;
					this.not_frm[this.not_frm.length]=notID;
				},this);
			}
		},
		UpdateComboBox: function() {
			this.SetComboBox(false);
		},
		SetSelection: function() {
			eign.each(function(attrib,aID) {
				if(attrib==undefined) return;
				var chbox = $('sel_'+aID);
				for(i=0;i<chbox.options.length;i++)
					if(chbox.options[i].value==lst[aID]) {
						chbox.selectedIndex=i;break;
					}
			},this);
		},
		AddToPriceString: function(val,tit) {
			if(val<0) {
				this.price_strings[3]+=tit+"<br />";
				this.price_strings[4]+=numf(val)+"&euro;<br />";
				this.price_strings[5]+=val;
			} else if(val>0) {
				this.price_strings[0]+=tit+"<br />";
				this.price_strings[1]+=numf(val)+"&euro;<br />";
				this.price_strings[2]+=val;
			}
		},
		Recalc: function() {
			function getAuflageIndex(afl) {
				ix=-1;
				prstb.each(function(val,i) {if(val==undefined) return;if(val['afl']==afl) ix=i;});
				return ix;
			};
			if(typeof prstb == "undefined") return;
			var tarife = new Array('eco','std','exp','nd')
			var tarif = tarife[$('idTarif').value-2];
			var kosten = prstb[getAuflageIndex(auflage)][tarif];
			var rbt_t = "";var rbt_v = "";var rbt = 0;
			this.price_strings=new Array("","",0,"","",0);
			this.AddToPriceString(kosten,"Druckkosten");

			//////////////////////////////
			// Options Preise lesen
			//////////////////////////////
			if(typeof opt != "undefined") {
			// Onlinebestellung?
			this.AddToPriceString(opt['olrbt'],"Onlinebestellung");
			// Datencheck?
			if(opt['dc']!=0) if(document.shp.dc[0].checked) this.AddToPriceString(opt['dc'],"Datencheck");
			if(opt['frm']) {
				if(document.shp.frm[0].checked) this.AddToPriceString(opt['frm_j'],"Format (JPG)");
				else if(document.shp.frm[1].checked) this.AddToPriceString(opt['frm_p'],"Format (PDF)");
				else this.AddToPriceString(opt['frm_a'],"Format (Offene Datei)");
			}

			if(opt['anl']) {
				if(document.shp.anl[0].checked) this.AddToPriceString(opt['anl_m'],"Datenanlieferung (Datenträger)");
				else if(document.shp.anl[1].checked) this.AddToPriceString(opt['anl_e'],"Datenanlieferung (Email)");
				else this.AddToPriceString(opt['anl_u'],"Datenanlieferung (Upload)");
			}

			if(opt['p'])
				if(document.shp.prf_yn[0].checked) {
					if(document.shp.prf[0].checked) this.AddToPriceString(opt['p_a'],"Proof-Lieferung (Selbstabholer)");
					else if(document.shp.prf[1].checked) this.AddToPriceString(opt['p_p'],"Proof-Lieferung (Post)");
					else this.AddToPriceString(opt['p_k'],"Proof-Lieferung (Kurier)");
				}
			}
			nb=this.price_strings[2]+this.price_strings[5];
			$('prs_ks').innerHTML=this.price_strings[0];
			$('prs_ksp').innerHTML=this.price_strings[1];
			$('prs_rb').innerHTML=this.price_strings[3];
			$('prs_rbp').innerHTML=this.price_strings[4];
			$('prs_nb').innerHTML=numf(nb)+"&euro;";
			$('prs_mw').innerHTML=numf((nb*mwst)-nb)+"&euro;";
			$('prs_bb').innerHTML=numf(nb*mwst)+"&euro;";
		},
		SetComboBox: function(onload) {
			if(!onload) this.UpdateLastSelection();
			not_items = new Array();

			order.each(function(attribID) {
				if(attribID==undefined) return;
				at_least_one_item=false;
				chbID = 'sel_'+attribID;
				var chbox = $(chbID);
				var sfbox = $('sf_'+attribID);

				// sonderfarben einrichten
				if(sfbox!=undefined)
					sfbox.style.display="none";
					this.sflist[attribID]=[attribID,0];

				chbox.style.width="400px";

				this.RemoveAllItems(chbID);
				selectedID=0;

				eign[attribID].each(function(val) {
					if(val==undefined) return;
					attribvalID=val[0];
					attribval=val[1];
					usable=true;
					for(i=0;i<not_items.length;i++)
						if(not_items[i]==attribvalID) {
							usable=0;
							break;
						}
					for(i=0;i<this.not_frm.length;i++) {
						if(this.not_frm[i]==attribvalID) {
							usable=0;
							break;
						}
					}
					if(usable==0) return;

					if(selectedID==0) selectedID=attribvalID;

					if(attribvalID==lst[attribID]) {
						selectedID=attribvalID;
						at_least_one_item=true;

						this.AddComboBox(chbID,attribvalID,attribval,true);
					}
					else {
						this.AddComboBox(chbID,attribvalID,attribval,false);
						at_least_one_item=true;
					}
				},this);
				if(at_least_one_item) {
					if(typeof(mod_tb[selectedID])!="undefined") {
						mod_tb[selectedID].each(function(val,notID) {if(val==undefined) return;not_items[not_items.length]=notID; },this);
					}
					lst[attribID]=selectedID;

					chbox.disabled=false;
					if(sf[attribID][selectedID]!=undefined) {
						if(sf[attribID][selectedID]>2) {
							chbox.style.width="300px";
							sfbox.style.display="inline";
							this.sflist[attribID]=[attribID,1];
						}
					}
				} else {
					this.AddComboBox(chbID,0,"Keine Auswahl möglich",true);
					var chbox = $(chbID);
					chbox.disabled=true;
				}
			},this);
			this.SetSelection();
		},
		SetupInfo: function() {
			reg_ex=/^atr_/;
			imgs = document.getElementsByTagName("img");
			for(i=0;i<imgs.length;i++) {
				if(reg_ex.exec(imgs[i].id)) {
					imgs[i].onmouseover = function(event) {
						sub = 225;
						if (!event) {
							event = window.event;
							sub=300;
						}
						info = $(this.id+'_i');
						info.style.display="inline";
						info.style.position="absolute";
						info.style.zIndex="1000";
						info.style.width="200px";
						info.style.left=(event.screenX-sub)+"px";
					};
					imgs[i].onmouseout = function() {
						info = $(this.id+'_i');
						info.style.display="none";
					};
				}
			}
		},
		getPosition: function(element) {
			var elem=element,tagname="",x=0,y=0;

			while (typeof(elem)=="object")
				if((typeof(elem.tagName)!="undefined"))
				{
				y+=elem.offsetTop;     /* Offset des jeweiligen Elements addieren */
				x+=elem.offsetLeft;    /* Offset des jeweiligen Elements addieren */
				tagname=elem.tagName.toUpperCase(); /* tag-Name ermitteln, Grossbuchstaben */

				/* wenn beim Body-tag angekommen elem fuer Abbruch auf 0 setzen */
				if (tagname=="BODY") {
						elem=0;
				}

				/* wenn elem ein Objekt ist und offsetParent enthaelt
				Offset-Elternelement ermitteln */
				if (typeof(elem)=="object")
						if (typeof(elem.offsetParent)=="object")
								elem=elem.offsetParent;
			}

			/* Objekt mit x und y zurueckgeben */
			position=new Object();
			position.x=x;
			position.y=y;
			return position;
		},
		showThumb: function(i,id) {
		var img = $('th_'+id);
		var pos = $(i).getPosition();

		img.setStyle('left',pos.x-115);
		img.setStyle('top',pos.y-40);
		img.setStyle('display','block');
	},
	hideThumb: function(id) {
		$('th_'+id).setStyle('display','none');
	},
	AddToBasket: function() {
		var ok=true;
		var sfbox=undefined;
		this.sflist.each(function(val) {
			if(val==undefined) return;
			if(val[1]==1) {
				sfbox = $('sfi_'+val[0]);
				if(sfbox!=undefined)
					if(sfbox.value=="") ok=false;
			}
		});
		if(ok) document.shp.submit();
		else alert("Sie haben mindestens eine Sonderfarbe gewählt, aber für diese keinen Farbwert bestimmt.\n\nBitte geben Sie in dem entsprechenden Feld einen Farbwert ein.");
	},
	ShowUrlaub:function() {
		var s = window.getSize();
		var s2 = window.getScrollSize();
		document.body.innerHTML = document.body.innerHTML+'<div id="urlaub_popup" style="position:absolute;left:0px;top:0px;width:'+s2.x+'px;height:'+s2.y+'px;background:#ccc;z-index:1000;"></div><div id="urlaub_img" style="position:absolute;left:'+((s.x/2)-300)+'px;top:'+((s.y/2)-200)+'px;z-index:1001;"><img id="urlaub_img_img" src="data/img/urlaub.gif" /></div>';
		$('urlaub_popup').setOpacity(0.7);
		$('urlaub_img_img').addEvent("click",function() {
			$('urlaub_popup').fade('out');
			$('urlaub_img').fade('out');
		});
		window.onresize = function() {
			var s = window.getSize();
			$('urlaub_popup').setStyle("width",s.x+"px");
			$('urlaub_popup').setStyle("height",s.y+"px");
			s = document.getScrollSize();
			$('urlaub_popup').setStyle("width",s.x+"px");
			$('urlaub_popup').setStyle("height",s.y+"px");

			s = window.getSize();
			var ix = (s.x/2)-300;
			if(ix<0) ix = 0;
			var iy = (s.y/2)-200;
			if(iy<0) iy = 0;
			$('urlaub_img').setStyle("left",ix+"px");
			$('urlaub_img').setStyle("top",iy+"px");
		};
	},
 initAnimation:function(count) {
  Ext.TaskMgr.start({run:xpress.switchImage,interval:3000});

 },
 switchImage:function() {
  switch(_aniCurr) {
   case  0: _aniCurr++;break;
   case  1: _aniCurr++;Ext.get('tpl_3_ani1').fadeOut();break;
   case  2: _aniCurr++;Ext.get('tpl_3_ani2').fadeOut();break;
   case  3: _aniCurr++;Ext.get('tpl_3_ani3').fadeOut();break;
   case  4: _aniCurr++;Ext.get('tpl_3_ani4').fadeOut();break;
   case  5: _aniCurr++;Ext.get('tpl_3_ani5').fadeOut();break;
   case  6: _aniCurr++;Ext.get('tpl_3_ani6').fadeOut();break;
   case  7: _aniCurr++;Ext.get('tpl_3_ani7').fadeOut();break;
   case  8: _aniCurr++;Ext.get('tpl_3_ani8').fadeOut();break;
   case  9: _aniCurr++;Ext.get('tpl_3_ani9').fadeOut();break;
   case 10: _aniCurr++;Ext.get('tpl_3_ani10').fadeOut();break;
   case 11: _aniCurr++;Ext.get('tpl_3_ani11').fadeOut();break;
   case 12: _aniCurr++;Ext.get('tpl_3_ani12').fadeOut();break;
   case 13: _aniCurr=1;Ext.get('tpl_3_ani1').fadeIn();
            Ext.get('tpl_3_ani2').fadeIn();Ext.get('tpl_3_ani3').fadeIn();Ext.get('tpl_3_ani4').fadeIn();
            Ext.get('tpl_3_ani5').fadeIn();Ext.get('tpl_3_ani6').fadeIn();Ext.get('tpl_3_ani7').fadeIn();
            Ext.get('tpl_3_ani8').fadeIn();Ext.get('tpl_3_ani9').fadeIn();Ext.get('tpl_3_ani10').fadeIn();
            Ext.get('tpl_3_ani11').fadeIn();Ext.get('tpl_3_ani12').fadeIn();Ext.get('tpl_3_ani13').fadeIn();break;
   default: break;
  }
 }

};

function numf(a, b, c, d) {
	var b = (b == null) ? 2 : b;
	var c = (c == null) ? ',' : c;
	var d = (d == null) ? '.' : d;
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if (!f[0]) {
		f[0] = '0';
	}
	if (!f[1]) {
		f[1] = '';
	}
	if (f[1].length < b) {
		g = f[1];
		for (i=f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j+=3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '' : c;
	return f[0] + c + f[1];
}

function popup(theURL,winName,features) { //v2.0
		window.open(theURL,winName,features);
}