 var openPopupId='';
 var transBgId='trans_background';
 var popupContainerId='se_cal';
 var popupContentId='se_content';
 var jsonData=Array(); 
 var htmlTemplate=Array();
 var constVars=Array();
 var preloadConstants=Array('thumbmaxw','thumbmaxh','galmaxw','galmaxh','transBgZindex');
			
 var movePicGal=false;
 var picGalStep=5;
 var picGalWidth=-1;
 var picgalObjects=Array();
 var picgalObjectCnt=0;
 var picgalContainerId=popupContainerId;
 var picgalContentId=popupContentId;
 var picMemo=Array();

window.addEvent('domready', function(){
	if(Cookie.read('PHPSESSID')==null && location.href.replace(/^.+\//,'').replace(/\.(php|html).*/,'')!='error'){
		var a=new Request.JSON({url:'/scripts/ajax.php',onSuccess:function(response){
			if(response.status!='OK'){
				location.href('/error.php?error=nocookies');
			}
		}}).send('fn=checkCookies');
	}
	
	if(document.id('trans_background')){
		document.id('trans_background').addEvent('click',function(e){
			closePopup();
		});
	}
	
	Hyphenator.config({remoteloading : true, onerrorhandler : function (e) {
                       alert('Fehler');
                }});
	Hyphenator.run();	
	$$('.hide').fade('hide').removeClass('hide');	

	$$('.showAgb').each(function(item){
		item.addEvent('click',function(){
			showMedia('agb');
		});
	});	

	//Falls Qenta-Fenster vorhanden, verhindern das im hintergrund geclicked werden kann. Doppel-Buchungen? 
	if(document.id('bodyid')){
		document.id('bodyid').addEvent('click',function(e){
			if(document.id('qenta')){
				//devLog('document id qenta vorhanden kein click auf body möglich');
				if(e){ e.stop(); }
			}
		});
	}

	getConstants(preloadConstants);
	setFormFunctions();
	loadHtmlTemplates();
	initWkorbFunctions();
	initToolTips();	
});
 
function loadHtmlTemplates(){
	var a=new Request({ method: 'post',url: '/scripts/ajax.php', onComplete: function(response){
		var jsonResponse=JSON.decode(response);
		if(jsonResponse.status!='OK'){
			alert(jsonResponse.status);
		}else{
			jsonResponse.data.each(function(item){
				htmlTemplate[item.id]=item.value.substitute(langTexts);
			});
			
		}
	}});
	a.send('fn=getHtmlTemplate&htmlTemplate=*');	
}

function showMedia(media,opts){
	if(document.id('trans_background').style.visibility=='hidden'){
		document.id('trans_background').fade(0,0.6);
	}
	if(media=='search'){
		document.id(popupContentId).innerHTML=htmlTemplate['search'];
		document.id(popupContainerId).position({
			position: 'centerTop',
			offset: {x:-200,y: 150}
		});
		document.id(popupContainerId).fade(0,1);				
		setTimeout("document.id('pattern').focus()",500);
	}else if(media=='picgal'){
		openPopupId=popupContainerId;
		document.id(popupContainerId).position({
			position: 'centerTop',
			offset: {x:(getConstant('galmaxw').toInt()/2*-1),y: 80}
		});		
		document.id(popupContentId).innerHTML=htmlTemplate['picgal'];
		getPicgal(opts);
		if(document.id(popupContainerId).style.visibility=='hidden'){
			document.id(popupContainerId).fade(0,1);
		}	
	}else if(media=='bigpic'){
		var picObj='picgalBigPic'; /* +picgalId */
		var picContentObj='picgalBigPic_content'; /* +picgalId */
		openPopupId=popupContainerId;
		document.id(popupContainerId).position({
			position: 'centerTop',
			offset: {x:(getConstant('galmaxw').toInt()/2*-1),y: 80}
		});		
		document.id(popupContentId).innerHTML=htmlTemplate['bigpic'];
	
		window.setTimeout('document.id(\'picgalBigPic\').set(\'html\',loadBigPic(\''+opts[0]+'\',\''+opts[1]+'\',\''+opts[2]+'\',\''+opts[3]+'\'))',100);
		document.id(picObj).style.display='block';
					
		if(document.id(popupContainerId).style.visibility=='hidden'){
			document.id(popupContainerId).fade(0,1);
		}	
	}else if(media=='lostpassword'){
		document.id(popupContentId).innerHTML=htmlTemplate['lostpassword'];
		document.id('form_password').addEvent('submit', function(e){
			if(e){ e.stop(); }
			if(checkFormular('form_password','info_password')){
				document.id('info_password').set('html','');
				this.set('send',{onComplete: function(response){
					var formResponse=JSON.decode(response);
					if(formResponse.status=='OK'){     
						document.id('password_div').setStyle('display','none');
						document.id('info_password').set('html',langTexts.passwordLostSend);
					}else{
						document.id('password_div').setStyle('display','none');
						document.id('info_password').set('html',formResponse.status);
					}	
				}});
				this.send();
			}else{
				return false;
			}
		});		    		   
		document.id(popupContainerId).position({
			position: 'centerTop',
			offset: {x:-200,y: 150}
		});
		if(document.id(popupContainerId).style.visibility=='hidden'){
			document.id(popupContainerId).fade(0,1);
		}			
		setTimeout("document.id('email').focus()",500);
	}else if(media=='lostactivatekey'){
		document.id(popupContentId).innerHTML=htmlTemplate['lostactivatekey'];
		document.id('form_password').addEvent('submit', function(e){
			if(e){ e.stop(); }
			if(checkFormular('form_password','info_password')){
				document.id('info_password').set('html','');
				this.set('send',{onComplete: function(response){
					var formResponse=JSON.decode(response);
					if(formResponse.status=='OK'){     
						document.id('password_div').setStyle('display','none');
						document.id('info_password').set('html',langTexts.activateKeyLostSend);
					}else{
						document.id('password_div').setStyle('display','none');
						document.id('info_password').set('html',formResponse.status);
					}	
				}});
				this.send();
			}else{
				return false;
			}
		});		    		   
		document.id(popupContainerId).position({
			position: 'centerTop',
			offset: {x:-200,y: 150}
		});
		if(document.id(popupContainerId).style.visibility=='hidden'){
			document.id(popupContainerId).fade(0,1);
		}			
		setTimeout("document.id('email').focus()",500);		
	}else if(media=='agb'){
		openPopupId=popupContainerId;
		document.id(popupContainerId).position({
			position: 'centerTop',
			offset: {x:-300,y: 150}
		});		
		document.id(popupContentId).innerHTML=htmlTemplate['agb'];
		var a=new Request.HTML({ url: langTexts.agbUrl,update: document.id('agb')}).send();
		if(document.id(popupContainerId).style.visibility=='hidden'){
			document.id(popupContainerId).fade(0,1);
		}
	}	
}

function loadBigPic(pic,w,h,alt_txt){
	return '<img src="/se-bin/mkthumb.pl?maxw='+ w + '&maxh=' + h +'&quality=90&pic=' + pic + '" title="' + alt_txt + '" alt="' + alt_txt + '" />';

}

function closePopup(){	
	if(document.id('search_content')){ document.id('search_content').morph({height: 0}); }
	document.id('trans_background').fade('out');
	document.id(popupContainerId).fade('out');
	document.id(transBgId).setStyle('z-Index',getConstant('transBgZindex'));
	document.id(popupContentId).set('html','');
}
function doSearch(page){
	document.id('search_content').set('morph', {duration: 500 });
	document.id('search_content').morph({height: 50});
	document.id('search_content').set('html',langTexts.doSearch+'...');
	var a = new Request({ url: '/scripts/ajax.php', method: 'post', data: 'fn=doSearch&pattern='+document.id('pattern').value + '&page='+page});
	a.addEvent('success', function(response) {
	    var searchResponse=JSON.decode(response);
		document.id('search_content').morph({height: 350});
		document.id('search_browse').innerHTML=searchResponse.browse;
	    document.id('search_content').innerHTML='<div>';
	    var searchObj=searchResponse.data;
		var space='';
		for(var i=0;i < searchObj.length; i++){
			if(searchObj[i][6]!=''){
				searchObj[i][6]='<a href="' + searchObj[i][6] + '">&nbsp;&raquo;&raquo;&nbsp;' + langTexts.Details + '</a>';
			}
			/*
				searchObj[i][0]=ID
				searchObj[i][1]=Titel
				searchObj[i][2]=Kurztext
				searchObj[i][3]=Bild
				searchObj[i][4]=KatID
				searchObj[i][5]=Rank (derzeit in %, könnte aber auch als Zahl repräsentiert werden)
				searchObj[i][6]=Link
				
			*/
			document.id('search_content').innerHTML+=  '<div class="contentblock" style="width:320px"><h2>' + searchObj[i][1] + '</h2></div>'
													 + '<div class="contentblock hyphenate">' + searchObj[i][3] + '' 
													 + searchObj[i][2] + '<br/>' 
													 + ''+searchObj[i][6] + ''
													 + '<div class="relevanz">' + langTexts.Relevanz + ': '+searchObj[i][5] + '</div></div>'
													 + '</div><div class="clear"></div><hr/>';													 
		}
		if(searchObj.length==0){
			document.id('search_content').innerHTML+=langTexts.SearchNotFound;
		}		
		document.id('search_content').innerHTML+='</div>';
	});
	a.send();	
}
function getPicgal(picgalId){
	var a=new Request({ method: 'post',url: '/scripts/ajax.php', onComplete: function(response){
		var jsonResponse=JSON.decode(response);
		if(jsonResponse.status!='OK'){
			alert(jsonResponse.status);
		}else{
			picgalObjects=jsonResponse.pics;
			fillPicgal(jsonResponse);
		}
	}});
	a.send('fn=getPicgal&picgal_id='+picgalId);
}

function showPicgal(picgalId,pic,alt_txt){
	var picObj='picgalBigPic'; /* +picgalId */
	var picContentObj='picgalBigPic_content'; /* +picgalId */
	if(document.id(picObj)){
		if(pic==false){ //Schlieszen
			document.id(picObj).style.display='none';
		}else{
			//document.id(+picgalId).innerHTML=loadPicgalPic(pic,alt_txt);
			//IE7 Bug, wenn man direkt zuweist, wird das Bild erst beim 2. mal angezeigt.			
			window.setTimeout('document.id(\''+picContentObj+'\').set(\'html\',loadPicgalPic(\''+pic+'\',\''+alt_txt+'\'))',100);
			document.id(picObj).style.display='block';
		}
	}else{
		alert(picObj + ' nicht gefunden');
	}
	return false;
}
function loadPicgalPic(pic,alt_txt){
	var picgalImageIndex=-1;
	for(var i=0;i < picgalObjects.length;i++){
		if(picgalObjects[i][1]==pic){
			picgalImageIndex=i;
		}
	}
	return '<img src="/se-bin/mkthumb.pl?maxw=' +getConstant('galmaxw') + '&maxh=' + getConstant('galmaxh')+'&quality=90&pic=' + picgalObjects[picgalImageIndex][1] + '" title="' + alt_txt + '" alt="' + alt_txt + '" />';
}

function fillPicgal(gal){
	/* gal.picgal	[0] = id
				 	[1] = titel
				 	[2] = memo
	   gal.pics	  	[0] = id
			  	 	[1] = src
			  	 	[2] = alt
	*/	
	document.id('picgalTitel').set('html',gal.picgal[1]);
	document.id('picgalMemo').set('html',gal.picgal[2]);
	var pics=gal.pics;
	var pichtml='';
	//picMemo.splice(0,picMemo.length);
	for(var i=0;i < pics.length; i++){
		pichtml += '<a href="javascript:void(0)" onclick="showPicgal(' + pics[i][0] + ',\'' + pics[i][1] + '\',\''+pics[i][2]+'\')">'
			     + '<img src="/se-bin/mkthumb.pl?pic=' + pics[i][1] + '&maxw=' + getConstant('thumbmaxw') + '&maxh=' + getConstant('thumbmaxh') + '" alt="' + pics[i][2] + '"/>'
			     + '</a>';
		//picMemo['picmemo'+pics[i][0]]=pics[i][3]; 
	}
	document.id('picgalPreviewBoxContent').set('html',pichtml);
}

function getPicGalWidth(obj){
	for(var i=0;i<obj.childNodes.length;i++){
		if(obj.childNodes[i].offsetWidth>0){
			picGalWidth+=obj.childNodes[i].offsetWidth;
		}
	}
}
function picgalPreviewBoxMove(id,direction){
	if(movePicGal==true){
		if(document.id('picgalPreviewBoxContent')){
			var obj=document.id('picgalPreviewBoxContent');
			if(picGalWidth==-1){
				getPicGalWidth(obj);
			}
			//debug('vorher ' + obj.style.left,false);
			var objLeft=0;
			if(!isNaN(parseInt(obj.style.left))){
				objLeft=parseInt(obj.style.left);
			}
			if(direction=='left'){
				if(objLeft > ( getConstant('galmaxw').toInt() - 2 * getConstant('galmoveWidth').toInt())-picGalWidth){
					objLeft-=picGalStep;
				}else{
					movePicGal=false;
				}
					
			}else{
				if(objLeft<=(picGalStep*-1)){
					objLeft+=picGalStep;			
					//debug('nachher ' + (objLeft+picGalStep) + 'px',true);	
				}else{
					objLeft=0;
					movePicGal=false;
				}							
			}
			obj.style.left=objLeft + 'px';
		}
		window.setTimeout('picgalPreviewBoxMove(' + id + ',\'' + direction + '\')',20);
	}
}
function getConstants(arrConst){
	var a=new Request.JSON({url: '/scripts/ajax.php',async:false,onSuccess: function(response){
		if(response.status=='OK'){
			arrConst.each(function(item){
				constVars[item]=response.constNames[item];
			});
		}
	}}).send('fn=getConstant&constNames='+JSON.encode(arrConst));
}
function getConstant(constName,asyncReq){
	if(asyncReq==undefined){ asyncReq=false; }
	if(constVars[constName]==undefined){		
		var a=new Request.JSON({url: '/scripts/ajax.php',async:asyncReq,onSuccess: function(response){
			if(response.status=='OK'){
				constVars[constName]=response.constName;
			}else{
				constVars[constName]='';
				alert('Constante ' + constName + ' nicht gefunden!');
			}
		}}).send('fn=getConstant&constName='+constName);
	}
	return constVars[constName];
}

function chkValidate(){
	var myValidator = new FormValidator(document.id('form_validate'),{useTitles: true
							, onElementFail: function(element,errTests){
			if(!document.id('errmsg_' + element.name)){
				var fieldName='';
				if(element.title){				
					fieldName=element.title;
				}else if(element.name){
					fieldName=element.name.capitalize();
				}else if(element.id){
					fieldName=element.id.capitalize();
				}
				var errStr='<span id="errmsg_' + element.name +'">' + langTexts.errorAtField + ' ' + fieldName + '</span>: ';
				var errSpace='';
				errTests.each(function(item){
					if(item=='required'){				
						errStr+=errSpace + langTexts.Pflichtfeld;
					}else if(item=='validate-email'){
						errStr+=element.value + ' ' + langTexts.noValidEmail + '.';
					}else{
						errStr+=errSpace + item;
					}
					errSpace=', ';
				});
				var errHtml=document.id('error_validate').get('html');
				if(errHtml==''){
					errHtml='<b>' + langTexts.formNotSent + '</b><br/>';
				}else{
					errHtml+='<br/>';
				}
				document.id('error_validate').set('html',errHtml+errStr);
			}
		}});
	if (myValidator.validate()) {
		return true;
	}else{
		return false;
	}
}

function wkorb(action,dsId,anzahl,wkorbId){
	/*
		wkorbId Notwendig fuer Adminbuchungen
	*/
	if(wkorbId==undefined){ wkorbId=0; }
	var a=new Request({ method: 'post',url: '/scripts/ajax.php', onComplete: function(response){
		if(action=='del'){
			location.href=location.href;
		}else{
			updateWkorbDisplay(JSON.decode(response));
		}
	}});
	a.send('fn=wkorb&action='+action+'&dsId='+dsId+'&anzahl='+anzahl+'&wkorbId='+wkorbId);
}

function updateWkorbDisplay(jsonResponse){
	if(jsonResponse.status!='OK'){
		alert(jsonResponse.status);
	}else{
		document.id('warenkorb').set('html',jsonResponse.warenkorb);
		document.id('marketbasket_div').set('html',jsonResponse.marketbasket);
		initToolTips();
		if(document.id('contentPositions')){
			if(jsonResponse.content){
				document.id('contentPositions').set('html',jsonResponse.content);
				initWkorbFunctions();
			}else{
				if(jsonResponse.summe){
					document.id('v_versand').set('html',getPriceFormat(jsonResponse.versandkosten.toFloat()));
					document.id('summe').set('html',jsonResponse.summe);
					
					jsonResponse.mwst.each(function(item){
						document.id(item.mwst+'mwst').set('html',getPriceFormat(item.summe.toFloat()));
					});
					//document.id('mwst').set('html',jsonResponse.mwst);
					if(jsonResponse.elementId){
						document.id(jsonResponse.elementId).set('html',jsonResponse.preis);
					}			
				}
			}
			
		}
	}
}

function initWkorbFunctions(){
	$$('.quantity').each(function(item){
		item.addEvent('keyup',function(){
			var doRequest=true;		
			if(item.value != ''){
				if(!isNaN(item.value.toInt())){
					var a=new Request.JSON({url:'/scripts/ajax.php',onComplete: function(response){
						if(response.status=='OK'){						
							updateWkorbDisplay(response);
						}else{
							alert(response.status);
						}
					}});
					a.send('fn=wkorb&action=update&dsId='+item.title+'&anzahl='+item.value.toInt());
				}else{
					alert(langTexts.NaNText);
				}
			}
		});
	});
	$$('.price').each(function(item){
		item.addEvent('keyup',function(){
			var doRequest=true;		
			if(!isNaN(item.value.toInt())){
				var a=new Request.JSON({url:'/scripts/ajax.php',onComplete: function(response){
					if(response.status=='OK'){						
						updateWkorbDisplay(response);
					}else{
						alert(response.status);
					}
				}});
				var postype='b';
				if(item.id.contains('price_zp_')){
					postype='zp';
				}else if(item.id.contains('price_p_')){
					postype='p';
				}
				a.send('fn=changePrice&id='+item.title+'&price='+item.value.toFloat()+'&postype='+postype);
			}else{
				alert(langTexts.NaNText);
			}
		});
	});
}

function doLogin(){
	if(document.id('login').value==''){
		document.id('login_error').set('html','Bitte Benutzernamen eintragen.');
		document.id('login').focus();
		return;
	}
	if(document.id('password').value==''){
		document.id('login_error').set('html','Bitte Passwort eintragen.');
		document.id('password').focus();
		return;
	}
			
	var a=new Request({ method: 'post',url: '/scripts/ajax.php', onComplete: function(response){
		var jsonResponse=JSON.decode(response);
		if(jsonResponse.status!='OK'){
			document.id('login_error').innerHTML=jsonResponse.status;
		}else{
			location.href=self.location;
		}
	}});
	a.send('fn=doLogin&login='+document.id('login').value+'&password='+document.id('password').value);
}

function setFormFunctions(){
	if(document.id('form_contact')){
		document.id('form_contact').addEvent('submit', function(e){
			if(e){ e.stop(); }
			if(checkFormular('form_contact','error_contact')){
				this.submit();
			}else{
				return false;
			}			
		});
	}
	if(document.id('form_register')){
		document.id('form_register').addEvent('submit', function(e){
			if(e){ e.stop(); }
			if(checkFormular('form_register','error_register')){			
				if(document.id('password').value != document.id('password2').value){
					document.id('error_register').set('html','<b>' + langTexts.passwordWrong + '</b><br/>');
					document.id('password').focus();
					return false;
				}
				this.set('send',{onComplete: function(response){
					var formResponse=JSON.decode(response);					
					if(formResponse.status=='OK'){
						if(formResponse.saveAction=='replaceForm'){
							document.id('registerBlock').set('html',formResponse.codeForm);
							setFormFunctions();
						}else{
							document.id('info_register').set('html',formResponse.codeForm);
						}
					}else if(formResponse.status=='EXISTS'){
						alert(formResponse.codeForm);
						top.location.href='/wkorb.html';
					}else{
						alert(formResponse.status);
					}	
				}});
				this.send();
			}else{
				return false;
			}
		});
	}
	if(document.id('form_registercode')){
		document.id('form_registercode').addEvent('submit', function(e){
			if(e){ e.stop(); }
			if(checkFormular('form_registercode','error_registercode')){
				this.set('send',{onComplete: function(response){
					var formResponse=JSON.decode(response);
					if(formResponse.status=='OK'){
						location.href='/wkorb.html';
					}else{
						document.id('error_registercode').set('html',formResponse.status);
						//alert(formResponse.status);
					}	
				}});
				this.send();
			}else{
				return false;
			}			
		});
	}
	if(document.id('form_paymentselect')){
		document.id('form_paymentselect').addEvent('submit', function(e){
			if(e){ e.stop(); }
			if(checkFormular('form_paymentselect','error_paymentselect')){
				this.set('send',{onComplete: function(response){
					var formResponse=JSON.decode(response);
					if(formResponse.status=='OK'){
						location.href='/wkorb.html';
					}else{
						alert(formResponse.status);
					}	
				}});
				this.send();
			}else{
				return false;
			}			
		});
	}
	if(document.id('form_review')){
		document.id('form_review').addEvent('submit', function(e){
			if(e){ e.stop(); }
			if(checkFormular('form_review','error_review')){
				if(document.id('trans_background').style.visibility=='hidden'){
					document.id('trans_background').fade(0,0.6);
				}			
				openPopupId=popupContainerId;
				document.id(popupContainerId).position({
					position: 'centerTop',
					offset: {x:-300,y: 150}
				});		
				document.id(popupContentId).innerHTML=htmlTemplate['qenta'];
				if(document.id(popupContainerId).style.visibility=='hidden'){
					document.id(popupContainerId).fade(0,1);
				}
				
				this.submit();
			}else{
				return false;
			}			
		});
	}
	if(document.id('form_nachnahme')){
		document.id('form_nachnahme').addEvent('submit', function(e){
			if(e){ e.stop(); }
			if(checkFormular('form_nachnahme','error_review') ){
				document.id('form_nachnahme').submit();
			}else{
				return false;
			}			
		});
	}		
}

function sendWarenkorb(){
	var newsletter='null';
	if(document.id('newsletter')){
		newsletter=document.id('newsletter').checked;
	}

	var a=new Request({ method: 'post',url: '/scripts/ajax.php', onComplete: function(response){
		var jsonResponse=JSON.decode(response);
		if(jsonResponse.status!='OK'){
			top.location.href='/checkout.html?error=doppelbuchung';
		}else{
			document.id('form_review').fireEvent('submit');
		}
	}}).send('fn=checkWarenkorb&wkorbId='+document.id('wkorbId').value+'&newsletter='+newsletter);
}

function checkFormular(formId,errorId){
	document.id(errorId).set('html','');
	var myValidator = new FormValidator(document.id(formId),{useTitles: true
							, onElementFail: function(element,errTests){
			if(!document.id('errmsg_' + element.name)){
				var fieldName='';
				if(element.title){				
					fieldName=element.title;
				}else if(element.name){
					fieldName=element.name.capitalize();
				}else if(element.id){
					fieldName=element.id.capitalize();
				}
				var errStr='<span id="errmsg_' + element.name +'">' + langTexts.errorAtField + ' ' + fieldName + '</span>: ';
				var errSpace='';
				errTests.each(function(item){
					if(item=='required'){				
						errStr+=errSpace + langTexts.Pflichtfeld;
					}else if(item=='validate-email'){
						errStr+=element.value + ' ' + langTexts.noValidEmail + '.';
					}else if(item=='validate-reqchk-byname'){
						errStr+=errSpace + ' ' + langTexts.noRadioChecked + '.';
					}else if(item=='validate-digits'){
						errStr+=errSpace + ' ' + langTexts.noValidDigits + '.';
					}else{
						errStr+=errSpace + item;
					}
					errSpace=', ';
				});
				var errHtml=document.id(errorId).get('html');
				if(errHtml==''){
					errHtml='<b>' + langTexts.formNotSent + '</b><br/>';
				}else{
					errHtml+='<br/>';
				}
				document.id(errorId).set('html',errHtml+errStr);
			}
		}});
	if (myValidator.validate()) {
		return true;
	}else{
		return false;
	}
}

function lostPassword(){
	document.id('form_password').fireEvent('submit');
}

function showActivate(){
	var a=new Request({ method: 'post',url: '/scripts/ajax.php', onComplete: function(response){
		var jsonResponse=JSON.decode(response);
		if(jsonResponse.status!='OK'){
			//alert(jsonResponse.status);
			document.id('error_registercode').set('html',formResponse.status);
		}else{
			document.id('container_loginform').set('html',jsonResponse.codeForm);
			setFormFunctions();
		}
	}});
	a.send('fn=showActivate');
}	

function paymentAction(state){
	if(state=='CANCEL'){
		parent.document.id('qenta').src='about:blank';
		parent.closePopup();
	}else if(state=='SUCCESS'){
		parent.location.href='/danke.php?thanksType=wkorb';
	}else if(state=='setbestellterror'){
		parent.location.href='/checkout.html?error=setbestellterror';
	}else{
		alert('Für Status ' + state + ' ist keine Routine implementiert.');
	}
}

function initToolTips(){
	$$('.tooltip').each(function(element,index) {
		if(element.title){
			var content = element.get('title').split('::');
			element.store('tip:title', content[0]);
			element.store('tip:text', content[1]);
		}
	});
	
	//create the tooltips
	var tipz = new Tips('.tooltip',{
		hideDelay: 50,
		showDelay: 50
	});
}

/**
 * Funktion zum Formatieren eines Numerischen Wertes in eine Waehrung (Euro)
 * @param vPrice
 * @return string
 */
function getPriceFormat(vPrice){
	return '&euro; ' + vPrice.numberFormat(2,',','.');
}

Number.implement({
/*
	Property: numberFormat
	Format a number with grouped thousands.
	
	Arguments:
	decimals, optional - integer, number of decimal percision; default, 2
	dec_point, optional - string, decimal point notation; default, '.'
	thousands_sep, optional - string, grouped thousands notation; default, ','
	
	Returns:
	a formatted version of number.
	
	Example:
	>(36432.556).numberFormat() // returns 36,432.56
	>(36432.556).numberFormat(2, '.', ',') // returns 36,432.56
*/
	numberFormat : function(decimals, dec_point, thousands_sep) {
		decimals = Math.abs(decimals) + 1 ? decimals : 2;
		dec_point = dec_point || '.';
		thousands_sep = thousands_sep || ',';
		var matches = /(-)?(\d+)(\.\d+)?/.exec((isNaN(this) ? 0 : this) + ''); // returns matches[1] as sign, matches[2] as numbers and matches[3] as decimals
		var remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;
		return (matches[1] ? matches[1] : '') + (remainder ? matches[2].substr(0, remainder) + thousands_sep : '') + matches[2].substr(remainder).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep) +
			   (decimals ? dec_point + (+matches[3] || 0).toFixed(decimals).substr(2) : '');
	}
});

function changeCountry(countryId){
	if(countryId==undefined){
		document.id('changeCountry').setStyle('display','inline');
	}else{
		var a=new Request.JSON({url:'/scripts/ajax.php',onSuccess:function(response){
			if(response.status=='OK'){
				updateWkorbDisplay(response);
			}else{
				alert(response.status);
			}
		}}).send('fn=changeCountry&countryId='+countryId);
		document.id('changeCountry').setStyle('display','none');
		//alert('aendere auf '+countryId);
	}
}

/**
 * Development Funktionen
 */

/**
 * Logging in die Console von FireBug
 * @param string str Der Text, der Protokolliert werden soll
 * @param string level Der Loglevel (debug|info|warn|error)
 * @param boolean force Soll ein Alert ausgegeben werden, wenn die Konsole nicht verfügbar ist? 
 */
function devLog(str,level,force){
	if(force==undefined){ force=false; }
	if(window.console && window.console.firebug){
		if(level==undefined){ level='debug'; }
		if(level=='debug'){
			console.debug(str);
		}else if(level=='info'){
			console.info(str);
		}else if(level=='warn'){
			console.warn(str);
		}else if(level=='error'){
			console.error(str);
		}else{
			console.log(str); //Eigentlich auch wieder debug
		}
	}else{
		if(force==true){
			alert('Konsole nicht gefunden (FF mit FireBug). DEBUG: ' + str);
		}
	}
}

