trim = function(txt) {
	for(var str='',tmp=[],y=-1,x=0;x<txt.length;x++) {
		if(txt.charAt(x)!=" ") {
			(tmp[((y==-1)?0:y)]==undefined)?tmp[((y==-1)?0:y)]=txt.charAt(x):tmp[((y==-1)?0:y)]+=txt.charAt(x);
		}
		else {
			(tmp[((y==-1)?0:y)]!=undefined)?y++:"";
		}
	}
	for(var x=0;x<tmp.length;x++) {
		(x<tmp.length-1)?str+=tmp[x]+" ":str+=tmp[x];
	}

	return str;
};

//Sesson-Cookie OHNE DATUM eingeben (null), NICHT -1
//setCookie("CookieName" , "Wert", null);
function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "")
	document.cookie = curCookie
}

function getCookie(cookieName) {
	var lf = "\n";
	var cookieString = document.cookie;
	var cookieSet = cookieString.split(';');
	var SetSize = cookieSet.length;
	var cookiePieces;
	var returnValue = "";
	var x = 0;
	for (x = 0; ((x < SetSize) && (returnValue == "")); x++) {
		cookiePieces = cookieSet[x].split('=');
		if (cookiePieces[0].substring (0,1) == ' ') {
			cookiePieces[0] = cookiePieces[0].substring (1, cookiePieces[0].length);
		}
		if (cookiePieces[0] == cookieName) {
			returnValue = cookiePieces[1];
		}
	 }
	return unescape(returnValue);
}

function hasCookie(name) {
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return false;
	if (start == -1) return false;

	return true;
}

function getCookieValue(name) {
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;

	if( !start && (name != document.cookie.substring(0,name.length)) ) return '';
	if( start == -1 ) return '';

	var end = document.cookie.indexOf(";",len);
	if( end == -1 ) end = document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}


function isSSLOff() {
	return (document.location.protocol.indexOf("https")==-1);
}


function determineBasketCounterCookie() {
	if( hasCookie('BasketCounter') ) {
		counterValue 	= getCookieValue('BasketCounter');

		if(counterValue == null || counterValue == '') {
			counterValue = '0';
		}
		return counterValue;
	}
	else {
		return '0';
	}
}


function clearItem(item, value) {
	if( item.value==value ) {
		item.value = '';
	}
}

function fillItem(item, value) {
	if( item.value=='' ) {
		item.value = value;
	}
}


function determineLoginStatusByCookie() {
	if (hasCookie('LoginCounter')) {
		loginValue = getCookieValue('LoginCounter');

		if (loginValue == null || loginValue == '')
			loginValue = 0;
			return loginValue;
		}
		else {
		return 0;
	}
}

function openWindow(src,name,width,height,features){
	if(features){
		return window.open(src, name, "width="+width+",height="+height+","+features);
	} else {
		return window.open(src, name, "width="+width+",height="+height+",scrollbars=yes");
	}
}

function add2Merkzettel(url) {
	add2Basket(url);
}


/*** Erweiterte Suche ***/
function checkProSearch() {
	if (productSearch.queryAuthor.value == ''
		&& productSearch.queryTitle.value == ''
		&& productSearch.queryNumber.value == ''
		&& productSearch.queryActor.value == ''
		&& productSearch.queryText.value == '')
	{
		alert("Bitte füllen Sie mindestens eines der Suchfelder aus.")
		return false;
	}

	var queryText, queryActor;

	queryText 	= trim(productSearch.queryText.value);
	queryActor 	= trim(productSearch.queryActor.value);

	if(queryActor != '') {
		if(queryText != '') {
			productSearch.queryText.value = queryText +' '+ queryActor;
		}
		else {
			productSearch.queryText.value = queryActor;
		}
	}

	return true;
}

/* Suchfeld im Header */
function volltextFocus() {
	clearItem(document.search.volltext, 'Suchbegriff oder Bestellnummer eingeben');

	document.search.volltext.className = "searchkeywordactive";
}

function volltextBlur() {
	fillItem(document.search.volltext, 'Suchbegriff oder Bestellnummer eingeben');

	if( document.search.volltext.value == 'Suchbegriff oder Bestellnummer eingeben' ) {
		document.search.volltext.className = "searchkeyword";
	}
	else {
		document.search.volltext.className = "searchkeywordactive";
	}
}

/*
 * caller: Node, in dem diese Funktion aufgerufen wird (normalerweise this).
 * msg: Anzuzeigende Nachricht
 * x, y: Koordination, wo die Quickinfo angezeigt werden soll (relativ zum caller)
 * width: Breite des Quickinfo-Divs.
 *
 * x, y, width sind optional.
 *
 * Beispiel: derclub/account/login.isml
 */
function show_quickinfo(caller, msg, x, y, width) {
	if (!x) x = "0";
	if (!y) y = "-300";
	if (!width) width = "300";
	qi_node = make_quickinfo(msg, x, y, width);
	// Opera Workaround:
	qi_parent = document.getElementById("quickinfo_parent");
	if (qi_parent) qi_parent.parentNode.removeChild(qi_parent);
	// wegen position:absolute müssen wir sicherstellen, dass
	// das Elternelement des Quickinfos nicht static positioniert ist.
	// Also basteln wir uns einfach selbst eins ohne weitere Eigenschaften.
	qi_parent = document.createElement("div");
	qi_parent.id = "quickinfo_parent";
	qi_parent.appendChild(make_quickinfo(msg, x, y, width));
	caller.appendChild(qi_parent);
}

function make_quickinfo(msg, x, y, width) {
	qi_node = document.createElement("div");
	qi_node.innerHTML = msg;
	qi_node.id = "quickinfo";
	qi_node.style.left = x;
	qi_node.style.top = y;
	qi_node.style.width = width;
	return qi_node;
}

function hide_quickinfo() {
	qi_node = document.getElementById("quickinfo_parent");
	qi_node.style.display = "none";
	// das qi an dieser Stelle aus dem DOM zu kicken verwirrt Opera...
	//if (qi_node) qi_node.parentNode.removeChild(qi_node); else alert("DSD");
}

function OpenHelpMain() {
	OpenHelpPar('hilfe','');
}

function add2basket(url) 
{
	new Ajax.Request(url, 
	{
		evalJS:false,
		onCreate: add2basketcreate,
		onException: add2basketexception,
		onSuccess: showAdd2basketResponse
	});
}

function showAdd2basketResponse(response) 
{
	var respjson = response.responseJSON;
	if (respjson && respjson.status)
	{
		if (respjson.status=='error')
		{
			$('lightbox_warenkorb_header').update(respjson.headline);
			$('lightbox_warenkorb_artikel').update(respjson.message);
			$('lightbox_warenkorb_artikel').show();
			$('lightbox_warenkorb_buttons').show();
		}
		else
		{
			if (respjson.productname2 != null && '' != respjson.productname2)
			{
				$('lightbox_warenkorb_header').update('Diese Artikel wurden gerade in Ihren Warenkorb gelegt:');
			}
			else
			{
				$('lightbox_warenkorb_header').update('Dieser Artikel wurde gerade in Ihren Warenkorb gelegt:');
			}
			$('lightbox_warenkorb_artikel').show();
			$('lightbox_warenkorb_buttons').show();
			$('lightbox_warenkorb_artikel2').hide();
			$('lightbox_warenkorb_product_name').update(respjson.productname.replace(/[,;']/g,' '));
			$('lightbox_warenkorb_product_price').update('<strong>' + respjson.productprice + ' &euro;*</strong>');
			$('lightbox_warenkorb_product_img').update(respjson.productimg);
			if (respjson.productname2 != null && '' != respjson.productname2)
			{
				$('lightbox_warenkorb_artikel2').show();
				$('lightbox_warenkorb_product_name2').update(respjson.productname2);
				$('lightbox_warenkorb_product_price2').update('<strong>' + respjson.productprice2 + ' &euro;*</strong>');
				$('lightbox_warenkorb_product_img2').update(respjson.productimg2);
			}
			refresh();
			Tracking.track({
				pageName:'Hinzufügen zum Warenkorb',
				channel :'checkout',
				events  :'scAdd'+(respjson.status_basket_created=='true'?',scOpen':''),
				products:respjson.producttype+';'+respjson.productname.replace(/[,;']/g,' ')+' ('+respjson.productnr+');1;'+respjson.productprice.replace(',','.'),
				hier1   :'checkout,add2basketpopup'
			});
			
		}
	}
}

function add2basketcreate()
{
	$('lightbox_warenkorb_header').update('<p>Ihre Anfrage wird verarbeitet. Bitte haben Sie einen Moment Geduld.</p>');
	$('lightbox_warenkorb_artikel').hide();
	$('lightbox_warenkorb_buttons').hide();
	iBox.showdiv('#lightbox_warenkorb',null,'ibox&width=332&height=230');
}

function add2basketexception()
{
	$('lightbox_warenkorb_header').update('Ihre Anfrage konnte leider nicht verarbeitet werden. Bitte versuchen Sie es zu einem sp&auml;teren Zeitpunkt erneut.');
	$('lightbox_warenkorb_artikel').hide();
	$('lightbox_warenkorb_buttons').hide();
}

//SiteCatalyst Tracking
	var Tracking = {
		myhash : new Hash(),
		
		'track':function(obj){
	
			if(s && obj) {
				h = $H(obj);
	
				['events'].each(function(name){
					if (!h.get(name)) {
						s[name] = '';
					}
				});
	
				h.each(function(pair) {
					s[pair.key] = pair.value;
				});
	
				s.tl();
			}
			
		},
		
		'setProperty':function(property,value){
			this.myhash.set(property,value);
			
		},
		
		'addEvent':function(event) {
			previousEvents = this.myhash.get('events');
			if (typeof(previousEvents) == 'undefined'
				|| previousEvents == null 
				|| previousEvents.blank()) {
				
				this.myhash.set('events', event);
			} else {
				previousEvents = this.myhash.unset('events');
				this.myhash.set('events', previousEvents + ',' + event);
			}
		},
		
		'update':function(obj){	
			
			this.myhash.each(function(pair) {
				s[pair.key] = pair.value;
			});
		}
	};
	   
	function vipSubmitCheck(action) {	
	
		if(document.getElementById("vip_confirm").checked) {
			if(action == "refresh") {
				process_become_VIP_Reload();
				iBox.hide();
			} else if(action == "norefresh") {
				process_become_VIP();
				iBox.hide();
			}else if(action == "login") {
				window.location.href = "/is-bin/INTERSHOP.enfinity/WFS/BC-ZR-Site/de_DE/-/EUR/ViewApplication-LogInOut?check=stammkunde";
			}
		} else {
			document.getElementById("vipConfirmText").style.color = 'red';
			document.getElementById("vipConfirmLink").style.color = 'red';
		}
	}
	// Überprüft ob der User eingeloggt ist
	// und Updatet die Links auf den "Stammkunde werden" Buttons
	// die aus dem CMS geladen werden.
	function vipButtonAction() {
	
		if(Cookie.get("LoginCounter") == "1") {
			iBox.showURL('/is-bin/INTERSHOP.enfinity/WFS/BC-ZR-Site/de_DE/-/EUR/ViewLayer-LoginBecomeVIP','',{width:810,height:620});
		} else {
			window.location.href = "/is-bin/INTERSHOP.enfinity/WFS/BC-ZR-Site/de_DE/-/EUR/ViewApplication-LogInOut?check=stammkunde";
		}
	
	}
	
	function vipBasketAction() {
		iBox.showURL('/is-bin/INTERSHOP.enfinity/WFS/BC-ZR-Site/de_DE/-/EUR/ViewLayer-TwoPrices?fromBasket=true','',{width:810,height:620});
	}
	
	// Öffnet den Layer TwoPrices, wird über diese Funktion
	// geöffnet da es Protokoll Probleme im CMS gibt.
	function openTwoPricesLayer() {
		iBox.showURL('/is-bin/INTERSHOP.enfinity/WFS/BC-ZR-Site/de_DE/-/EUR/ViewLayer-TwoPrices','',{width:810,height:620});
	}
	
	function openTwoPricesLayerBasket() {
		iBox.showURL('/is-bin/INTERSHOP.enfinity/WFS/BC-ZR-Site/de_DE/-/EUR/ViewLayer-TwoPrices?fromBasketFooter=true','',{width:810,height:620});
	}
	
	// Öffnet den Vorteilsausgabe-Layer, wird über diese Funktion
	// geöffnet da es Protokoll Probleme im CMS gibt.
	function openVorteilsAusgabeLayer() {
		iBox.showURL('/is-bin/INTERSHOP.enfinity/WFS/BC-ZR-Site/de_DE/-/EUR/ViewLayer-VorteilsAusgabe','',{width:760,height:540});
	}