/*******************************************************************************
*
* Programm:
*	omeco webshop, (c) 1998-2008 omeco GmbH
* Versions:
*	4.0
* Authors:
* 	Susanne Dutschke <sdutschke@omeco.de>
* 	Daniel Kuppitz <dkuppitz@omeco.de>
* 	Alexander Moritz <amoritz@omeco.de>
* 	Bodo Thiesen <bthiesen@omeco.de>
*
*******************************************************************************/

/**
 * Öffnet die AGB in einem neuen Popup Fenster.
 *
 * @param string mainControl
 */
function showAgb(mainControl)
{
	window.open(mainControl + "?EVENT=agbStandalone", "AGB", "toolbar=no,scrollbars=yes");
}

/**
 * Schreibt den Link zu den AGB in das HTML Dokument.
 *
 * @param string mainControl
 * @param string agbText
 */
function writeAgbLink(mainControl, agbText)
{
	document.write("<a class=\"navkleindunkel\" href=\"javascript: showAgb('" + mainControl + "');\">" + agbText + "</a>");
}

/**
 * Öffnet die Widerrufbelehrung in einem neuen Popup Fenster.
 *
 * @param string mainControl
 */
function showAvoidance(mainControl)
{
	window.open(mainControl + "?EVENT=avoidanceStandalone&amp;PHPSESSID=$PHPSESSID#avoidance", "AVOIDANCE", "toolbar=no,scrollbars=yes");
}

/**
 * Schreibt den Link zur Widerrufbelehrung in das HTML Dokument.
 *
 * @param string mainControl
 * @param string avoidanceText
 */
function writeAvoidanceLink(mainControl, avoidanceText)
{
	document.write("<a class=\"navkleindunkel\" href=\"javascript: showAvoidance('" + mainControl + "');\">" + avoidanceText + "</a>");
}

/**
 * Setzt das Attribut 'target' auf den Wert '_blank'.
 *
 * @param string id
 */
function setBlankTargetForElement(id)
{
	element = document.getElementById(id);

	if (null != element && element.target != undefined)
	{
		element.target = "_blank";
	}
}

/**
 * Zeigt ein Bild in einem Popup an.
 *
 * @param string path
 * @param string title
 * @param double width
 * @param double height
 */
function showItemImage(path, title, width, height)
{
	window.open(path, title, 'width=' + width + ', height=' + height);
}

/**
 * Schreibt den Link zum L�schen des kleinen Warenkorbs in das Dokument.
 *
 * @param string question
 * @param string redirection
 * @param string img_path
 * @param string text
 * @see deleteCart
 */
function writeDeleteCartAnchorTag(question, redirection, img_path, text)
{
	document.write("<a href=\"#\" onclick=\"deleteCart('" + question + "','" + redirection + "');\">");
	document.write('<img src="' + img_path + '" alt="' + text + '" /><br />' + text + '</a>');
}

/**
 * Öffnet einen Bestätigungsdialog zum Löschen des Warenkorbs. Bei 'OK' erfolgt eine Weiterleitung zur Seite
 * auf der der Warenkorb gelöscht wird.
 *
 * @param string question
 * @param string redirection
 */
function deleteCart(question, redirection)
{
	if (confirm(question))
	{
		document.location = redirection;
	}
}

function setupSuggest() {
    SetupMMSuggest(document.getElementById('searchterm'),
        {'requestURL' : '/mall/1/js/mmsuggest.php?query=$v$&', 'debug' : false});
}

function setElementStatus(elementId, enabled)
{
    var element = document.getElementById(elementId);

    if (null != element)
    {
        element.disabled = !enabled;
    }
}

function increment(id, inc_value)
{
    var i = document.getElementById(id);
    if (i != null && i != 'undefined')
    {
        i.value = parseFloat(i.value) + ((typeof(inc_value) == "undefined") ? 1.0 : inc_value);
    }
}

function decrement(id, minimum, dec_value)
{
    var i = document.getElementById(id);
    if (i != null && i != 'undefined')
    {
        i.value = Math.max(minimum, parseFloat(i.value) - ((typeof(dec_value) == "undefined") ? 1.0 : dec_value));
    }
}

function switchFieldStates(containerId, state) {
    $('#' + containerId + ' *').each(
        function(i) {
            if (this.disabled!='undefined') {
                this.disabled = !state ? 'disabled' : '';
            }
        });
    if (state) {
        $('#' + containerId + ' label').each(
            function(i) {
                $(this).removeClass('inaktiv');
            });
    } else {
        $('#' + containerId + ' label').each(
            function(i) {
                $(this).addClass('inaktiv');
            });
    };
}

/**
  * Lightbox
  * Öffnet ein Popup mit Bildern und Daten in einem semi-transparenten Overlay über der aktuellen Seite
  */
function toggleLightBox(activewin, overlay, mode) {
    if (activewin != null) {
    	activewin.style.display  = mode;
    	overlay.style.display = mode;
    }
}


function toggleswitch(id, imgId, accordionId)
{
	var d = document.getElementById(id);

	if (d.style.display=='block')
	{
		d.style.display = 'none';
		document.getElementById(imgId).src = '/mall/1/img/accordion_closed.gif';
		document.getElementById(accordionId).className = 'accordion closed';

	} else {

		d.style.display = 'block';
		document.getElementById(imgId).src = '/mall/1/img/accordion_opened.gif';
		document.getElementById(accordionId).className = 'accordion opened';
	}
}

function retainedPosition() {
  var y = 0;
  if (window.pageYOffset) {
    y = window.pageYOffset;

  } else if (document.body && document.body.scrollTop) {
    y = document.body.scrollTop;

  }
  /*if (y > 0) {
    window.scrollBy(0, -10);
    setTimeout('ScrollToLastPos()', 10);
  }*/
}

/*
 * Function request
 * Copyright (C) 2008 by Bodo Thiesen <bothie@gmx.de>
 * This function is distributed under the terms of the GNU General Public
 * License version 2.
 *
 * Startet einen XML-HTTP-Request. Dabei wird die URL 'url' aufgerufen, wenn
 * method = POST ist und data != null werden diese Daten an den Server
 * gesendet. callback wird aufgerufen, sobald die Seite geladen wurde.
 */
function request(url,method,data,callback) {
	var http;
	var need_polling;

	if (typeof XMLHttpRequest != 'undefined') {
		http=new XMLHttpRequest;
		need_polling=false;
	} else {
		try {
			http=new ActiveXObject("Msxml2.XMLHTTP");
			need_polling=true;
		} catch (e) {
			try {
				http=new ActiveXObject("Microsoft.XMLHTTP");
				need_polling=true;
			} catch (e) {
				http=null;
			}
		}
	}

	if (!http) {
		return false;
	}

	if (data && typeof data=='object') {
		var _data=[];

		for (var i in data) {
			_data.push(i+'='+escape(data[i]));
		}

		data=_data.join('&');

		delete _data;
	}

	method=method.toUpperCase();

	if (method=='POST') {
		http.open(method,url,!!callback);
		http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	} else {
		if (data) {
			if (url.search("/?/")==-1) {
				url+='?'+data;
			} else {
				url+='&'+data;
			}
		}
		data=null;
		http.open(method,url,!!callback);
	}

	if (callback) {
		if (need_polling) {
			var timer;
			var timeout=1200; // 1200 Versuche, alle 50ms macht eine Minute.
			var poll=function() {
				if (http.readyState==4) {
					timeout=1;
					callback(http);
				}
				if (!--timeout) {
					window.clearInterval(timer);
					timer=null;
				}
			}
			timer=window.setInterval(poll,50);
		} else {
			http.onreadystatechange=function() {
				if (http.readyState==4) {
					http.onreadystatechange=null;
					callback(http);
				}
			}
		}
	}

	http.send(data);

	return true;
}

/*
 * Liefert den Inhalt eines XML-Tags als String zurück.
 */
function getXmlElementText(xml_element) {
	if (typeof xml_element.text        != 'undefined') return xml_element.text;
	if (typeof xml_element.textContent != 'undefined') return xml_element.textContent;
	return xml_element.textContent;
}


/*
 * Startet einen Ajax-Request beim Ändern der Auswahl einer Variante, um den
 * Preis zu aktualisieren und in Anhängigkeit der Gültigkeit der Variante die
 * Dropdowns rot (oder wieder schwarz) einzufärben.
 */
function start_ajaxItemVariation_request(ajax_url,reload_url,item_id) {
	function ajaxItemVariation_request_handler(xml_http_response) {
		var xml=xml_http_response.responseXML;
		var html_element,xml_element;

		html_element=document.getElementById('baseprice_'+item_id);
		xml_element=xml.getElementsByTagName('baseprice')[0];
		if (html_element && xml_element) {
			html_element.innerHTML=getXmlElementText(xml_element);
		}

		html_element=document.getElementById('actionprice_'+item_id);
		xml_element=xml.getElementsByTagName('actionprice')[0];
		if (html_element && xml_element) {
			html_element.innerHTML=getXmlElementText(xml_element);
		}

        html_element=document.getElementById('itemnumber_'+item_id);
		xml_element=xml.getElementsByTagName('itemnumber')[0];        
		if (html_element && xml_element) {
			html_element.innerHTML=getXmlElementText(xml_element);
		}

		html_element=document.getElementById('variations_'+item_id);
		xml_element=xml.getElementsByTagName('valid_variation')[0];
		if (html_element && xml_element) {
			if (getXmlElementText(xml_element)=='true') {
				color="";
			} else {
				color='red';
			}
			select_element_array=html_element.getElementsByTagName('select');
			for (i=0; i<select_element_array.length; ++i) {
				if (color) {
					select_element_array[i].style.color=color;
				} else {
					select_element_array[i].style.color="green";
					select_element_array[i].style.color="";
					try {
						select_element_array[i].style.removeAttribute("color",true);
					} catch (e) {
						// nothing to do
					}
				}
			}
			document.getElementById('submit_allowed_'+item_id).value=getXmlElementText(xml_element);
		}
	}

	if (request(ajax_url,'get',null,ajaxItemVariation_request_handler)) {
		// nothing to do
	} else {
		if (reload_url) {
			document.location.href=reload_url;
		}
	}
}


function showCueImageWindow(bildurl){
	neu=open("","neu","resizable=1,width=980,height=250, scrollbars=yes");
	neu.document.open("text/html");
	neu.document.write("<html><img src=\""+bildurl+"\"></html>");
	neu.document.close();
	}