/* -------------------------------------------------------------------
	=Constants
 ------------------------------------------------------------------ */
var DIRECTIONUP = "up";
var DIRECTIONDOWN = "down";


var timeoutpager = null;
/* -------------------------------------------------------------------
	=Standard Shop-Funktionen
 ------------------------------------------------------------------ */
function fieldUp (id, minVal) {
	updateField(id, DIRECTIONUP, minVal);
}
function fieldDown (id, minVal) {
	updateField(id, DIRECTIONDOWN, minVal);
}
function updateField(id,direction, minVal) {
	if (minVal == null || ( typeof(minVal) != "number" ) ) minVal = 1;

	var inputObj = $(id);
	var iVal = parseString(inputObj.value);
	if ( typeof(iVal)== "number" ) {
		if (direction == DIRECTIONUP) {
			inputObj.value = iVal+1;
		}
		else {
			if (iVal>minVal) inputObj.value = iVal-1;
		}
	}
	else {
		inputObj.value = 1;
	}
}

/** parses a string to an int **/
function parseString ( String ) {
	return ( !isNaN(parseInt(String)) ) ? parseInt(String) : String;
}

/** shows/hides the discount layer depending to the current style **/
function toggleDiscountLayer(id) {
	if (!id) {
		if (activeDisplayLayer) {
			activeDisplayLayer.style.display = 'none';
			activeDisplayLayer = null;
		}

		return;
	}
	if (!$(id)) return;
	if ($(id) == activeDisplayLayer ) return; // if active is the selected one

	if (activeDisplayLayer) {
		activeDisplayLayer.style.display = 'none';
		activeDisplayLayer = null;
	}

	$(id).style.display = "block";
	activeDisplayLayer = $(id);
}

/** activates the timeout within a mouseooverevent **/
function toogleProdpager ( aElementId, img250px, text, img400px ) {
	timeoutpager = window.setTimeout("toogleProdpagerDelay('"+aElementId+"', '"+img250px+"', '"+text+"', '"+img400px+"')", 500);
}

/** clears the timeout within a mouseoutevent **/
function clearProdpage() {
	if(timeoutpager) {
		window.clearTimeout(timeoutpager);
	}
}

/** toogles the prodpager after the delay to the new elements **/
function toogleProdpagerDelay(aElementId, img250px, text, img400px) {
	toggleActiveItem ($(aElementId));
	showBigElement(img250px, img400px);
	if (text && text.length > 0) {
		$('similarflag').innerHTML = text;
	}
	else {
		$('similarflag').innerHTML = '';
	}
	setBigPopupLink($('mc_imglnk_vergroessern'), img400px);
}

/** Update the Link on the BigImage to the LargerImage **/
function setBigPopupLink (elem, img) {
	if (elem && img) {
		elem.href = "javascript:openPopup('/ce/ProductDetailImage.html?image="+img+"', 420, 420);"
	}
}


/** Sets the selected element to active in pager **/
function toggleActiveItem(elem) {
	if (!elem) return;

	var manipulateElem = (elem.parentNode && elem.parentNode.parentNode ) ? elem.parentNode.parentNode : null;
	if (manipulateElem) {

		// restyle the 'old'-Active
		if (activeElement) {
			activeElement.className = '';
		}

		// style the 'new'-Active
		manipulateElem.className = 'active';

		// save the activeElem
		saveActiveElem(elem);
	}

}

/** saves the active Element in a global var **/
function saveActiveElem ( AElem ) {
	activeElement = (AElem) ? AElem.parentNode.parentNode : null;
}

/** Shows/Hides the Image/Flash-Layer and toggles to the right image **/
function showBigElement(elem, img400px) {
	if (elem) {
		if (elem.indexOf('.swf') > -1 ) {
			// hide Image
			$('BigProductImage').style.display = 'none';

			// Append Layer to Container
			var pL = $('FlashContainerOverall');
			pL.innerHTML = '<div id="FlashContainer"></div>';

			// Append swf
			swfobject.embedSWF(elem, "FlashContainer", "250", "250", "8.0.0", false, flashvars, params, attributes);

			pL.style.display = 'inline';
			$('FlashContainer').style.display = 'inline';
		}
		else {
			//$('FlashContainer').style.display = 'none';
			var pL = $('FlashContainerOverall');
			var bI = $('BigProductImage');
			pL.innerHTML = '';
			$('FlashContainerOverall').style.display = 'none';
			bI.src = elem;
			bI.style.display = 'inline';

			setBigPopupLink(bI.parentNode, img400px);
		}

	}
}


/*
	Die klassischen Funktionen zum öffnen von Popups und Ein-/Ausblenden von Fenstern
*/
function openPopup(url, width, height, name, params) {
	if (name == null) {
		name = "ConradPopup";
	}
	var p = (params) ? params : 'width='+width+',height='+height+',left=10,top=10,resizable=no,scrollbars=no,dependent=yes,menubar=no,location=no,toolbar=no,status=yes';
	var win = window.open(url, name , p);
	win.focus();
}
function openEbulaPopup(url, width, height, name, params) {
	if (name == null) {
		name = "EbulaPopup";
	}
	var p = (params) ? params : 'width='+width+',height='+height+',left=10,top=10,resizable=no,scrollbars=yes,dependent=yes,menubar=no,location=no,toolbar=no,status=yes';
	var win = window.open(url, name , p);
	win.focus();
}
function openPrintPopup1() {
	openPopup('', 540, 440, "Druckauswahl");
}

function openPrintPopup2(u) {
	var p = 'width=710,left=10,top=10,resizable=no,scrollbars=yes,dependent=yes,menubar=no,location=no,toolbar=no,status=yes';
	window.open(u, 'Druckansicht' , p);
	self.close();
}

function openComparePopup() {
	openPopup('',960,690, 'ConradCompare' , 'width=960,height=690,left=10,top=10,resizable=yes,scrollbars=yes,dependent=yes,menubar=no,location=no,toolbar=no,status=yes');
}

function openDeliveryCostCalculatorPopup() {
	openPopup('', 565, 425, 'DeliveryCostCalculator', 'width=565,height=425,left=10,top=10,resizable=no,scrollbars=no,dependent=yes,menubar=no,location=no,toolbar=no,status=yes');
}

function toOldWin(url, closeit) {
	if (opener == null) self.close();
	else {

		try {
			opener.focus();
			if (opener.location.href != url)
				opener.location.href = url;
		}
		catch (e) {
		}
		if (closeit)
			self.close();
	}
	return false;
}

function toLogin(loginurl) {
	if (opener.location.href != loginurl)
	opener.location.href = loginurl;
}


/* Öffnen und schließen von Layer */
function hideLayer(n) {
	$(n).hide();
}

function showLayer(n) {
	$(n).show();
}
/* Ende: Öffnen und schließen von Layer */


var b2bName = null;
function changeToB2B(name) {
	$('b2b_' + name).checked = true;
	b2bName =  name;
	$('isB2CForm').value = 'false';
	showLayer('b2bTable');
	hideLayer('b2cTable');
}

function changeToB2C() {
	if (b2bName)
		$('b2c_' + b2bName).checked = false;
	$('isB2CForm').value = 'true';
	showLayer('b2cTable');
	hideLayer('b2bTable');
}

function closeB2BLayer() {
	hideLayer('businessLayer');
	hideLayer('businessLayerShadow');
}

function closeCountryPopBLayer() {
	hideLayer('countryPopup');
	hideLayer('countryPopupLayerShadow');
}




/* IFrame zum �berlagern in der Navigation */
function showKillIframe (mLayer, width) {
  if ( ( navigator.appName.indexOf("Explorer") != -1 || navigator.userAgent.indexOf("Gecko")) && !window.opera)  {

    var kFrame = $('killSelectElement');
    if (kFrame) {

		var ua =navigator.userAgent.toLowerCase();
		var ieVer = parseInt(ua.substring(ua.indexOf("msie")+5, ua.indexOf("msie")+8));

		if (navigator.appName.indexOf("Explorer") != -1) {
			// isIE
			mLayer.appendChild(kFrame);
			kFrame.style.display = "block";
			kFrame.style.width = "181px";
			kFrame.style.height = mLayer.offsetHeight-5+"px";
		} else {
			// is NO IE
			kFrame.style.display = "";
			kFrame.style.width = "161px";
			kFrame.style.height = mLayer.offsetHeight - 4 +"px";
			mLayer.appendChild(kFrame);
		}
	}
	if (width) { 
		kFrame.style.width = width + "px";
	}
  }
}
function hideKillIframe () {
  // hide
  var kFrame = $('killSelectElement');
  if (kFrame) {
    kFrame.style.display = "none";
  }
}

/* Funktionen zum (De-)Selektieren aller Checkboxen in einem Formular */
/* f: entweder id oder formobjekt */
function checkAll(f) {
	var e, i;
	i = 0;
	if ( typeof f == 'string') {
		while (e = document.getElementById(f).elements[i++]) {
			if (e.type == 'checkbox') e.checked = true;
		}
	}
	else if ( typeof f == 'object' ) {
		while (e = f.elements[i++]) {
			if (e.type == 'checkbox') e.checked = true;
		}
	}

}

function uncheckAll(f) {
	var e, i;
	i = 0;
	if ( typeof f == 'string') {
		while (e = document.getElementById(f).elements[i++]) {
			if (e.type == 'checkbox') e.checked = false;
		}
	}
	else if ( typeof f == 'object' ) {
		while (e = f.elements[i++]) {
			if (e.type == 'checkbox') e.checked = false;
		}
	}
}

function toggleHiddenField(inputElm, field, val) {
	field.value = val;
	inputElm.src = configuration.radioImageChecked;
	if (activeElm) {
		activeElm.src = configuration.radioImage;
	}
	activeElm = inputElm;
}

/* Funktionen zur Anzeige von Raten/Gesamtpreis bei Ratenzahlung */
function showRatesLayer() {
	$('rateSelect').selectedIndex = 'values_6';
	updateRate('values_6');
	showLayer('RatesLayer');
	//$('RatesLayer').style.display = 'block';
}

function updateRate(selectValue) {
	var values = $(selectValue).firstChild.data.split('_');

	$('newPrice').firstChild.data = values[0];
	$('rateText').firstChild.data = values[1];
	$('monthlyRate').firstChild.data = values[2];
}


/* Variations Funktionen */
function setVariationAvaiblity(code, selIndex){

	// update the hidden-value if available
	if ( $('productcode') ) {
		$('productcode').value = code;
	}

	if (variationDeliveries[code] && variationDeliveries[code][0]) {
		$('varAvaibility').innerHTML = variationDeliveries[code][0];
		/* hauptverfügbarkeit soll immer auf "beste verfügbarkeit" stehen bleiben
		if ( selIndex == 0 || selIndex == null) {
			$('product_availability').innerHTML = bestAvailability[0];
		}
		else {
			$('product_availability').innerHTML = variationDeliveries[code][0];
		}
		*/
	}
	if (variationDeliveries[code] && variationDeliveries[code][1]) {
		$('varAvaibility').className = "avaibility_" + variationDeliveries[code][1];
		/*
		if ( selIndex == 0 || selIndex == null) {
			$('product_availability').className = "avaibility_" + bestAvailability[1];
		}
		else {
			$('product_availability').className = "avaibility_" + variationDeliveries[code][1];
		}
		*/


	}
}

function checkVariation() {
	if (document.addToCartForm && document.addToCartForm.variation[document.addToCartForm.variation.selectedIndex].value == "productcode0") {
		$('ErrorVariationSize').show();
		return false;
	}
	return true;
}

function updateVariationBox() {
	setVariationAvaiblity($('ProductDetailCartForm').variation[$('ProductDetailCartForm').variation.selectedIndex].value);
}


/* Vorschau-Buttons im Schaufenster 1 */
function showVorschauButton(sku,elem) {
	// Get the Preview Layer from tzhe sku and show it
	var prevElm = $('PreviewBut_' + sku);
	prevElm.style.display = 'block';
}

function hideVorschauButton(sku) {
	// Get the Preview Layer from tzhe sku and hide it
	var prevElm = $('PreviewBut_' + sku);
	prevElm.style.display = 'none';
}


/* Kontakt - Art des Anliegens */
function kindOfContact(kind) {
	var standard = $('contact_standard');
	var techFirst = $('contact_technical_first');
	var techSecond = $('contact_technical_second');
	var other = $('contact_else');

	if (kind == 'standard') {
		standard.show();
		techFirst.hide();
		techSecond.hide();
		other.hide();
	} else if (kind == 'technical1') {
		standard.hide();
		techFirst.show();
		techSecond.hide();
		other.hide();
	} else if (kind == 'technical2') {
		standard.show();
		techFirst.hide();
		techSecond.show();
	} else if (kind == 'other') {
		standard.hide();
		techFirst.hide();
		techSecond.hide();
		other.show();
	}
}

function trim (zeichenkette) {
	return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}

function showDeliverLayer() {
	//updateInfoCenter('info_delivery');  
	window.top.updateInfoCenter('info_delivery_costs');
}
function hideDeliverLayer() {
	window.top.infocenter.hide();
	//$('InfoCenter').hide();;
}

function showRecyclingLayer() {
	window.top.updateInfoCenter('infocenterMenu');
}

function checkTab (id) {
	if (document.location.hash.substring(1) == id)
		$(id).show();
	else
		$(id).hide();
}

function loadURL (url, params, async, method, onSuccess, onFailure) {
	new Ajax.Request(url, {
		parameters: params,
		asynchronous: async,
		method: method,
		onSuccess: onSuccess,
		onFailure: onFailure
	});
}

var activeSearchLayer = false;
function toggleSearchLayer(id) {
	if(!id) return;

	if (activeSearchLayer != id) {
		// anderer Menupunkt
		if (activeSearchLayer)
			$(activeSearchLayer).hide();
		$(id).show();
		activeSearchLayer = id;
	}
	else {
		// weniger
		if (activeSearchLayer)
			$(activeSearchLayer).hide();
		activeSearchLayer = null;
	}
}

/* FastRegistration Bestellbestätigung */
function showFastReg() {
	$('fastReg').hide();
	$('fastRegTable').show();
}


/**
* dlayer function
* set the filter number no to the value fvalue
* @param no the filter number, integer between 1 and 6
* @param fvalue the value, String
*/
function setFilter(no,fvalue,fid, formid, count, fieldid){
	if ($(formid)) {
		if (!fieldid) {
			$(formid).filter_selection.value = fvalue;
			$(formid).filter_selection.disabled = false;
		}
		else {
			$(fieldid).value = fvalue;
			$(fieldid).disabled = false;
		}
	}
	else {
		if (no == 'price' ) {
			$('pricerange').value = fvalue;
		} else if (no == 'feature' ) {
			$('feature').value = fvalue;
		} else if (no == 'orderby' ) {

			if( fvalue != "" ) {
				var arr = fvalue.split("&");

				$('orderBy').value = arr[0];
				$('orderSequence').value = arr[1];
			} else {
				$('orderBy').value = "";
				$('orderSequence').value = "";
			}
		}
		else {
			$('filter_selection_'+no).value = fvalue;
			for(i=1; i<=count; i++) {
				if ($('filter_selection_'+i).value != "") {
					$('filter_selection_'+i).disabled = false;
				}
			}
		}
	}
	if (!formid) {
		document.filter.submit();
	}
	else {
		$(formid).submit();
	}
}

function updateInfoCenter (param /*[url]*/, ispop) {
	if (ispop == 'opener')
		$('iCenterIframe').innerHTML = '<iframe name="infoCenterFrame" id="infoCenterFrame" src="'+configuration.infoCenterAction + "?area=" + param+'&popup=true" height="368" width="566" scrolling="no" frameborder="0" ></iframe>';
	else
		$('iCenterIframe').innerHTML = '<iframe name="infoCenterFrame" id="infoCenterFrame" src="'+configuration.infoCenterAction + "?area=" + param+'" height="468" width="658" scrolling="no" frameborder="0" ></iframe>';
	
	var elem = $('InfoCenter');
	var xCoord = 0, yCoord = 0, width = 0;
	var arrayPageSize = getPageSize();

	width = arrayPageSize[0];
	xCoord = (width-400)/2;

	if (window && window.pageYOffset) {
		//FF, OPERA, SAFARI
		yCoord = window.pageYOffset;
	}
	else if ( document.documentElement && document.documentElement.scrollTop ) {
		//IE6, IE7
		yCoord = document.documentElement.scrollTop;
	}

	elem.style.top = (yCoord+10) + "px";
	elem.style.left = xCoord + "px";
	elem.show();
}


// Produktvergleich
function updateProductComparer (code, layername, errText) {
		var params = new Hash();
		params.set("update");
		params.set("code", code);
		params.set("xml", 1);
		
		loadURL(
			configuration.actionProductsCompare, 
			params, 
			true, 
			'post', 
			function(transport) { parseResponse(transport, layername, errText);  }, 
			function(transport) { alert('failure show');  } 
		);
	}
	
	function parseResponse(transport, layername, errText) {
		var item = transport.responseXML;
		if (item && item.getElementsByTagName('status')[0]) {
			var stat = item.getElementsByTagName('status')[0].firstChild.nodeValue;
			if (stat == 1) {
				hideLayer(layername);
			}
			else {
				updateErrorLayer(item, errText,  layername);
			}
		}
		else {
			updateErrorLayer (null) 
		}
	}
	function updateErrorLayer( item, text, layerToHide ) {
		var errLayer = $$('#errors')[0];
		
		if (item) {
			errLayer.innerHTML = '<span style="color:#ff0000;">' + item.getElementsByTagName('error')[0].firstChild.nodeValue + '</span>'; 
			//errLayer.innerHTML = "No Error XML Document inner it table";
		}
		else {
			errLayer.innerHTML = text;
		}
		showLayer('productsCompareErrorLayer');
		hideLayer(layerToHide);
	}

function switchtab(num){
	if(document.getElementById("faqdiv"+num).style.display == "block"){
		document.getElementById("faqdiv"+num).style.display = "none";
		document.getElementById("faqli"+num).className = "";
		document.getElementById("faqli"+num).setAttribute("class", "");
	}
	else{
		document.getElementById("faqdiv"+num).style.display = "block";
		document.getElementById("faqli"+num).className = "open";
		document.getElementById("faqli"+num).setAttribute("class", "open");
	}
}
function closetab(i){
	document.getElementById("faqdiv"+i).style.display = "none";
	document.getElementById("faqli"+i).className = ""; 
	document.getElementById("faqli"+i).setAttribute("class", "");
}

function showTooltip(img) {
	var liObj = img.parentNode;
	var pos	= parseInt(liObj.id.substring(liObj.id.indexOf('symbol')+6))-1;
	
	var pulldown = $('symbollayer');
	var pulldownImage = pulldown.firstChild;
	pulldownImage.src = img.src.substring (0, img.src.indexOf('40')) + '80.' + img.src.substring(img.src.length-3);

	var top = ((parseInt((pos / 7))+1) * 50 ) +10;
	var left = (pos % 7)  * 53 + 10;

	pulldown.setStyle ({top: '-10px', left: left+'px'});
	pulldown.onmouseover = function () { showTooltip(img); }
	pulldown.onmouseout = function () { hideTooltip(img); }
	pulldown.show();
}
function hideTooltip (img) {
	var pulldown = $('symbollayer');
	var pulldownImage = pulldown.firstChild;
	pulldownImage.src = '';
	pulldown.hide();
}

function checkShoppingListName() {  
	if ($('shoppingListInputName').value == '') {  
		$('shoppingListInputNameError').show();  
		return false;  
	}  
}  
