function init(){
	$('#loading').show();
	if($('#cartdiv')){
		$.getJSON(homeurl+'ajax/cart.php?show=1',
			function(json) {
			if(json.length==0){
				$('#cartdiv').html('<div class="Cart">'+lgInit_1+'</div>');
				$('#searchAq').html('<input type="checkbox" name="sS" id="sS" value="" style="visibility: hidden;"/>&nbsp;');
			}else{
				output='<div style="background-color:#F8BD7B">';
				var seller="";
				var used="";
				var itemCnt=0;
				var minHeight=500;
				var searchSeller="";
				for(var i=0;i<json.length;i++){
					if(seller!=json[i].aqId||used!=json[i].used){
						output+='<div style="border-bottom: 2px #000000 solid; padding: 2px; font-size: 14px; margin-bottom: 2px;">';
						switch (json[i].warengruppe) {
							case "1":	if(json[i].used==1){
										searchSeller+=json[i].aqId+'.';
										output+='<img src="'+homeurl+'gfx/bookUsed.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgInit_2+'" title="'+lgInit_2+'" />'
									}else{
										output+='<img src="'+homeurl+'gfx/bookNew.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgInit_3+'" title="'+lgInit_3+'" />'
									}
									break;
							case "2": output+='<img src="'+homeurl+'gfx/weine.gif" width="17px" height="21px" style="float: left; margin-right: 8px;" alt="'+lgInit_6+'" title="'+lgInit_6+'" />';
									break;
							case "3": output+='<img src="'+homeurl+'gfx/spielwaren.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgInit_7+'" title="'+lgInit_7+'" />';
									break;
						}
						
						if(json[i].used==1){
							output+='<a href="'+homeurl+'aq/'+json[i].aqId+'/'+json[i].sellerName+'" class="sellerLink">'+json[i].sellerName+'</a>';
						}else{
							output+=json[i].sellerName;
						}
						output+='</div>';
					}
					output+='<div id="'+json[i].buchId+'" class="cartEntry"';
					if(i<(json.length-1)){
						output+=' style="border-bottom: 1px dashed #990000; margin-bottom: 10px;"';
					}
					output+='>';
					output+='<span style="float: right; top: 0; margin-left: 3px;">[<a href="#" alt="'+lgInit_4+'" title="'+lgInit_4+'" class="cartOut" onclick="removeCart('+json[i].buchId+','+json[i].used2+')">x</a>]</span>';
					switch (json[i].warengruppe) {
						case "1": 	output+='<a href="'+homeurl+'productListing.php?used='+json[i].used2+'&productId='+json[i].buchId+'" class="productLink">'+json[i].orderNo+':</a> ';
									break;
						case "2":	output+='<a href="'+homeurl+'wein.php?weinId='+json[i].buchId+'" class="productLink">'+json[i].orderNo+':</a> ';
									break;
						case "3":	output+='<a href="'+homeurl+'spiel.php?spielId='+json[i].buchId+'" class="productLink">'+json[i].orderNo+':</a> ';
									break;
					}
					if(json[i].author!="") output+=json[i].author+' ';
					if(json[i].title!="") output+='<b>'+json[i].title+'</b>';
					output+='<br/>'+json[i].quantity+'x = '+formatZahl(json[i].quantity*json[i].price,2,true)+' '+json[i].currency;output+='</div>';
					seller=json[i].aqId;
					used=json[i].used;
					minHeight+=100;
				}
				output+='<div align="center" style="height: 25px"><input type="image" src="'+homeurl+'gfx/goToCheckout'+buttonset+lgSuffix+'.gif" name="goCheckout" onclick="javascript:self.location.href=\''+homeurl+'checkout0.html\'" /></div>';
				if($('#searchAq')){
					if(searchSeller!=""){
						$('#searchAq').html('<input type="checkbox" name="sS" id="sS" value="'+searchSeller+'" /> '+lgInit_5);
					}else{
						$('#searchAq').html('<input type="checkbox" name="sS" id="sS" value="" style="visibility: hidden;"/>&nbsp;');
					}
				}
				output+='</div>';
				$('#cartdiv').html(output);
			}
		},
		function(){alert('Fehler bei Ajax Request')}
		);
		$('#loading').hide();
		}
}

function resizeImage(name,newHeight){eval("document."+name+".height=newHeight")}

function addToCart(element){
	//$('#loading').show();
	$.get(homeurl+'ajax/cart.php?add=1',
			$('#input'+element.id).serialize(),
			function(json){init()},
			function(){$('#loading').hide(); alert('Fehler bei Ajax Request')}
	);
	//$('#loading').hide();
}

function removeCart(buchId,used){
	//$('#loading').show();
	$.get(homeurl+'ajax/cart.php?remove=1&buchId='+buchId+'&used='+used,
			function(json){init()},
			function(){$('#loading').hide(); alert('Fehler bei Ajax Request')}
	);
	//$('#loading').hide();
}

function updateSuggestNew(query,cnt){
	$.getJSON(homeurl+'ajax/updateDb.php?suggest=1&q='+escape(query)+'&cnt='+cnt,function(json){return true;},function(){alert('Fehler bei Ajax Request')})
}

function queryBuchfreund(){return true;}

function queryNeubuch(str){
	$('#loaderNeubuch').show();
	var newCnt=0;
	var reg='';
	url=location.search;
	query=url.replace(/\?/,"");
	query=query.replace(/&page=\d*/,"");
	query=query.replace(/&x=\d*/,"");
	query=query.replace(/&y=\d*/,"");
	query=query.replace(/&used=\d*/,"");
	query=query.replace(/used=\d*/,"");
	query=query.replace(/&auctions=\d*/,"");
	query=query.replace(/auctions=\d*/,"");
	if(query==""){
		url=location.href;
		url=url.replace(/http:\/\/www.buchfreund.de\//,"");
		query="q="+url.replace(/tag\//,"");
		query=query.replace(/\/$/,"");
	}
	if(str!=""){
		$.getJSON(homeurl+'ajax/searchNeubuch.php?q='+str+'&cnt=1',function(json){
				newCnt=json[0].quantity;
				if(newCnt==0) {
					$('#neubuchResults').html('<img src="'+homeurl+'gfx/bookNew.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryNeubuch_1+'" title="'+lgQueryNeubuch_1+'" /> '+lgQueryNeubuch_2+' (0)');
				}else{
					$('#neubuchResults').html('<img src="'+homeurl+'gfx/bookNew.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryNeubuch_1+'" title="'+lgQueryNeubuch_1+'" /> <a href="'+homeurl+'results.php?'+query+'&used=0" class="flag">'+lgQueryNeubuch_2+' ('+json[0].quantity+')</a>');
				}
				$('#loaderNeubuch').hide();
				//updateSuggestNew(str,newCnt);
			},
			function(){alert('Fehler bei Ajax Request')}
		);
	}else{
		$('#loaderNeubuch').hide();
		$('#neubuchResults').html('<img src="'+homeurl+'gfx/bookNew.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryNeubuch_1+'" title="'+lgQueryNeubuch_1+'" /> '+lgQueryNeubuch_2+' (0)');
	}
}

function queryNeubuchDetail(){
	$('#loaderNeubuch').show();
	var newCnt=0;
	var reg='';
	url = location.search;
	query = url.replace(/\?/,"");
	query = query.replace(/&page=\d*/,"");
	query=query.replace(/&used=\d*/,"");
	query=query.replace(/used=\d*/,"");
	query = query.replace(/&x=\d*/,"");
	query = query.replace(/&y=\d*/,"");
	
	$.getJSON(homeurl+'ajax/searchNeubuch.php?detail=1',function(json){
		newCnt=json[0].quantity;
		if(newCnt==0){
			$('#neubuchResults').html('<img src="'+homeurl+'gfx/bookNew.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryNeubuch_1+'" title="'+lgQueryNeubuch_1+'" /> '+lgQueryNeubuch_2+' (0)');
		}else{
			$('#neubuchResults').html('<img src="'+homeurl+'gfx/bookNew.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryNeubuch_1+'" title="'+lgQueryNeubuch_1+'" /> <a href="'+homeurl+'results.php?' + query + '&used=0" class="flag">'+lgQueryNeubuch_2+' ('+json[0].quantity+')</a>');
		}
		$('#loaderNeubuch').hide();
		},
		function(){alert('Fehler bei Ajax Request')}
	);
}

function queryAuctions(str){
	$('#loaderAuctions').show();
	var aucCnt=0;
	var reg='';
	url=location.search;
	query=url.replace(/\?/,"");
	query=query.replace(/&page=\d*/,"");
	query=query.replace(/&x=\d*/,"");
	query=query.replace(/&y=\d*/,"");
	query=query.replace(/&used=\d*/,"");
	query=query.replace(/used=\d*/,"");
	query=query.replace(/&auctions=\d*/,"");
	query=query.replace(/auctions=\d*/,"");
	if(query==""){
		url=location.href;
		url=url.replace(/http:\/\/www.buchfreund.de\//,"");
		query="q="+url.replace(/tag\//,"");
		query=query.replace(/\/$/,"");
	}
	if(str!=""){
		$.getJSON(homeurl+'ajax/searchAuktionen.php?q='+str+'&cnt=1',function(json){
			newCnt=json[0].quantity;
			if(newCnt==0){
				$('#auctionsResults').html('<img src="'+homeurl+'gfx/auctions.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryAuctions_1+'" title="'+lgQueryAuctions_1+'" /> '+lgQueryAuctions_1+' (0) <a href="'+homeurl+'auktionshaeuser/" class="flag">'+lgQueryAuctions_2+'</a>');
			}else{
				$('#auctionsResults').html('<img src="'+homeurl+'gfx/auctions.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryAuctions_1+'" title="'+lgQueryAuctions_1+'" /> <a href="'+homeurl+'results.php?'+query+'&auctions=1" class="flag">'+lgQueryAuctions_1+' ('+json[0].quantity+')</a>');
			}
			$('#loaderAuctions').hide();
			},
			function(){alert('Fehler bei Ajax Request')}
		)
	}else{
		$('#loaderAuctions').hide();
		$('#auctionsResults').html('<img src="'+homeurl+'gfx/auctions.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryAuctions_1+'" title="'+lgQueryAuctions_1+'" /> '+lgQueryAuctions_1+' (0)');
	}
}

function queryAuctionsDetail(){
	$('#loaderAuctions').show();
	var aucCnt=0;
	var reg='';
	$('#loaderAuctions').hide();
	$('#auctionsResults').html('<img src="'+homeurl+'gfx/auctions.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryAuctions_1+'" title="'+lgQueryAuctions_1+'" /> '+lgQueryAuctions_1+' (0)');
}

function queryWine(str){
	$('#loaderWine').show();
	var wineCnt=0;
	var reg='';
	url=location.search;
	query=url.replace(/\?/,"");
	query=query.replace(/&page=\d*/,"");
	query=query.replace(/&x=\d*/,"");
	query=query.replace(/&y=\d*/,"");
	query=query.replace(/&used=\d*/,"");
	query=query.replace(/used=\d*/,"");
	query=query.replace(/&auctions=\d*/,"");
	query=query.replace(/auctions=\d*/,"");
	if(query==""){
		url=location.href;
		url=url.replace(/http:\/\/www.buchfreund.de\//,"");
		query="q="+url.replace(/tag\//,"");
		query=query.replace(/\/$/,"");
	}
	if(str!=""){
		$.getJSON(homeurl+'ajax/searchWine.php?q='+str+'&cnt=1',function(json){
			wineCnt=json[0].quantity;
			if(wineCnt==0){
				$('#wineResults').html('<img src="'+homeurl+'gfx/weine.gif" width="17px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryWeine_1+'" title="'+lgQueryWeine_1+'" /> <a href="'+homeurl+'weine.php?'+query+'" class="flag">'+lgQueryWeine_1+' (0)</a>');
			}else{
				$('#wineResults').html('<img src="'+homeurl+'gfx/weine.gif" width="17px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryWeine_1+'" title="'+lgQueryWeine_1+'" /> <a href="'+homeurl+'weine.php?'+query+'" class="flag">'+lgQueryWeine_1+' ('+json[0].quantity+')</a>');
			}
			$('#loaderWine').hide();
			},
			function(){alert('Fehler bei Ajax Request')}
		)
	}else{
		$('#loaderWine').hide();
		$('#wineResults').html('<img src="'+homeurl+'gfx/weine.gif" width="17px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryWeine_1+'" title="'+lgQueryWeine_1+'" /> <a href="'+homeurl+'weine.php" class="flag">'+lgQueryWeine_1+' (0)</a>');
	}
}

function queryWineDetail(){
	$('#loaderWine').show();
	var wineCnt=0;
	var reg='';
	$('#loaderWine').hide();
	$('#wineResults').html('<img src="'+homeurl+'gfx/weine.gif" width="17px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQueryWeine_1+'" title="'+lgQueryWeine_1+'" /> <a href="'+homeurl+'weine.php" class="flag">'+lgQueryWeine_1+' (0)</a>');
}

function queryGames(str){
	$('#loaderGames').show();
	var gamesCnt=0;
	var reg='';
	url=location.search;
	query=url.replace(/\?/,"");
	query=query.replace(/&page=\d*/,"");
	query=query.replace(/&x=\d*/,"");
	query=query.replace(/&y=\d*/,"");
	query=query.replace(/&used=\d*/,"");
	query=query.replace(/used=\d*/,"");
	query=query.replace(/&auctions=\d*/,"");
	query=query.replace(/auctions=\d*/,"");
	if(query==""){
		url=location.href;
		url=url.replace(/http:\/\/www.buchfreund.de\//,"");
		query="q="+url.replace(/tag\//,"");
		query=query.replace(/\/$/,"");
	}
	if(str!=""){
		$.getJSON(homeurl+'ajax/searchGames.php?q='+str+'&cnt=1',function(json){
			gamesCnt=json[0].quantity;
			if(gamesCnt==0){
				$('#gamesResults').html('<img src="'+homeurl+'gfx/spielwaren.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQuerySpielwaren_1+'" title="'+lgQuerySpielwaren_1+'" /> <a href="'+homeurl+'spielwaren.php?'+query+'" class="flag">'+lgQuerySpielwaren_1+' (0)</a>');
			}else{
				$('#gamesResults').html('<img src="'+homeurl+'gfx/spielwaren.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQuerySpielwaren_1+'" title="'+lgQuerySpielwaren_1+'" /> <a href="'+homeurl+'spielwaren.php?'+query+'" class="flag">'+lgQuerySpielwaren_1+' ('+json[0].quantity+')</a>');
			}
			$('#loaderGames').hide();
			},
			function(){alert('Fehler bei Ajax Request')}
		)
	}else{
		$('#loaderGames').hide();
		$('#gamesResults').html('<img src="'+homeurl+'gfx/spielwaren.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQuerySpielwaren_1+'" title="'+lgQuerySpielwaren_1+'" /> <a href="'+homeurl+'spielwaren.php" class="flag">'+lgQuerySpielwaren_1+' (0)</a>');
	}
}

function queryGamesDetail(){
	$('#loaderGames').show();
	var gamesCnt=0;
	var reg='';
	$('#loaderGames').hide();
	$('#gamesResults').html('<img src="'+homeurl+'gfx/spielwaren.gif" width="23px" height="21px" style="float: left; margin-right: 3px;" alt="'+lgQuerySpielwaren_1+'" title="'+lgQuerySpielwaren_1+'" /> <a href="'+homeurl+'spielwaren.php" class="flag">'+lgQuerySpielwaren_1+' (0)</a>');
}

function ckLogin(ck){
	if(ck.checked){
		document.getElementById('addLogin').style.display='block';
	}else{
		document.getElementById('addLogin').style.display='none';
	}
}

function checkUserName(){
	var str=encodeURIComponent($('#cuLogin').val());
	if(str!=""){
		$.getJSON(homeurl+'ajax/checkUser.php?uN='+str,function(json){
			if(json==true){
				$('#userFlag').html('<img src="'+homeurl+'gfx/available.gif" width="20" height="19" alt="'+lgCheckUserName_1+'" title="'+lgCheckUserName_1+'" />');
			}else{
				$('#userFlag').html('<img src="'+homeurl+'gfx/notAvailable.gif" width="20" height="18" alt="'+lgCheckUserName_2+'" title="'+lgCheckUserName_2+'" />');
			}},
			function(){$('#userFlag').html('')}
		);
	}else{
		$('#userFlag').html('');
	}
}


function toggle_visibility(id){
	var e=document.getElementById(id);
	if(e.style.display=='block')
		e.style.display='none';
	else
		e.style.display='block';
}


function toggleDisclaimer(){
	$(".small").click(function(){
		// slide toggle effect set to slow you can set it to fast too.
		$(this).next(".disclaimer").slideToggle("slow");
	return true;
	});
}

function updateLoginForm(){
	switch($('#loginArea').value){
		case	"Kunde":		$('#loginLabel').html('<b>Benutzername: </b>');
								$('#button').value('Kunden-Login');
								break;
		case	"Antiquariat":	$('#loginLabel').html('<b>Kundennummer: </b>');
								$('#button').value('Antiquariats-Login');
								break;
		case	"Neubuchhändler":
								$('#loginLabel').html('<b>Kundennummer: </b>');
								$('#button').value('Neubuchhändler-Login');
								break;
		default	:				$('#loginLabel').html('<b>Benutzername: </b>');
								$('#button').value('Kunden-Login');
	}
}

function formatZahl(zahl,k,fix){if(!k)k=0;var neu='';var f=Math.pow(10,k);zahl=''+parseInt(zahl*f+(.5*(zahl>0?1:-1)))/f;var idx=zahl.indexOf('.');if(fix)
{zahl+=(idx==-1?'.':'')
+f.toString().substring(1);}
idx=zahl.indexOf('.');if(idx==-1)idx=zahl.length;else neu=','+zahl.substr(idx+1,k);while(idx>0)
{if(idx-3>0)
neu='.'+zahl.substring(idx-3,idx)+neu;else
neu=zahl.substring(0,idx)+neu;idx-=3;}
return neu;}

function showAntiquar(eventAntiquarian){
	if(!eventAntiquarian)
		eventAntiquarian=window.event;
	if(document.getElementById){
		document.getElementById("antiquarBox").style.left=eventAntiquarian.clientX+"px";
		document.getElementById("antiquarBox").style.top=eventAntiquarian.clientY+"px";
	}else if(document.all){
		document.all.antiquarBox.style.left=eventAntiquarian.clientX;
		document.all.antiquarBox.style.top=eventAntiquarian.clientY;
	}
}

function hideAntiquar(){
	document.getElementById("antiquarBox").style.left=-5000+"px";
	document.getElementById("antiquarBox").style.top=-5000+"px";
}

var currentAd=0;
function lastAd(element){
	$.getJSON(homeurl+'ajax/serveAds.php?previous=1&current='+currentAd,function(json){
		currentAd=json.id;
		//$('#adBox').empty();
		$('#adBox').html(json.ad);
		},
		function(){alert('Fehler bei Ajax Request')});
}

function nextAd(element){
	$.getJSON(homeurl+'ajax/serveAds.php?next=1&current='+currentAd,function(json){
		currentAd=json.id;
		//$('#adBox').empty();
		$('#adBox').html(json.ad);
		},
		function(){alert('Fehler bei Ajax Request')});
}
