// based on MooTools, My Object Oriented Javascript Tools. 
// Copyright (c) 2006-2007 Valerio Proietti, <http://mad4milk.net>, MIT Style License.

// var path = ''; wird in head.php generiert

var PRODUCT_OPTIONS_MAX = 1; // maximal Anzahl von Attribute wie z.B. Farbe, Groesse, Geschmack
var deal = false;
var ses = "XTCsid=";
var https;
var minSuche = 4; // mind. 4 Zeichen zur Suche benoetigt

/** onload **/
window.addEvent('domready', function(){
	iniGlobalVars();
	iniWarenkorb();
	iniSucheBox();
});

/** Select Farben **/
function setSelectedColor(opt){
	opt.style.background = opt.options[opt.selectedIndex].style.background;
	opt.style.color = opt.options[opt.selectedIndex].style.color;
}

/** legt Event auf Suche-Feld, zwecks minimaler Zeicheneingabe **/
var sucheErlaubt = false;
function iniSucheBox(){
	var sucheBox = $('suche');
	if($chk(sucheBox)){
		sucheBox.addEvent('keyup', function(){
			sucheErlaubt = (sucheBox.value.length >= minSuche);
		});
	}
	if($chk($('quick_find'))){
		$('quick_find').addEvent('submit', function(){
			if(!sucheErlaubt) alert("Bitte mindestens "+minSuche+" Zeichen bei der Suche eingeben.");
			return sucheErlaubt;
		});
	}
}

/** wegen der Zeichenbeschränkung bei der Suche-Box **/
function chkInputLength(form){
	return sucheErlaubt;
}

/** hier werden globale Variablen initialisiert **/
function iniGlobalVars(){
	var myForm = document.forms['iniForm'];
	ses += myForm.sid.value;
	https = (myForm.https.value == 1);
}

/** Warenkorb **/
function iniWarenkorb(){
	if(!https && path!=""){
		var urlPath = path+"warenkorb.php?"+ses;
		switch(MooTools.version){
			case "1.11":
				new Ajax(urlPath, {
					method: 'get',
					update: $('warenkorb')
				}).request()
			break;
			
			case "1.2.2":
				var req = new Request({
					url: urlPath,
					onSuccess: function(txt){
						$('warenkorb').set('html', txt);
					},
					onFailure: function(){
						$('warenkorb').set('text', 'The request failed.');
					}
				}).send();
			break;
		}
	}
}

/** Artikel in Warenkorb legen */
function addProduct(form, showAlert){
	if(!https && path!=""){
		if(showAlert) alert("Der Artikel wird in den Warenkorb gelegt.");
		var action = "action=add_product&"+ses;
		action += "&language="+form.language.value;
		action += "&cat_id="+form.cat_id.value;
		action += "&products_id="+form.products_id.value;	
		// abfrage in welchem Feld "menge" die Angabe gemacht wurde
		// products_qty <- oder -> products_qty2
		action += "&products_qty="+form.products_qty.value;
		var opt = "";
		for(i=1; i <= PRODUCT_OPTIONS_MAX; i++){
			opt += ($chk(form.elements["id["+i+"]"]))? "&id["+i+"]="+form.elements["id["+i+"]"].value : "";
		}
		var urlPath = path+"warenkorb.php?"+action+opt;
		
		switch(MooTools.version){
			case "1.11":
				new Ajax(urlPath, {
					method: 'get',
					update: $('warenkorb')
				}).request()
			break;			
			case "1.2.2":
				var req1 = new Request({
				url: urlPath,
				onSuccess: function(txt){
					$('warenkorb').set('html', txt);
				},
				onFailure: function(){
					$('warenkorb').set('text', 'The request failed.');
				}
			}).send();
			break;
		}	
	}
}

/** statt leeren Warenkorb zu zeigen, wird eine Meldung eingeblendet **/
function warenkorb_leer(){
	var errorTxt 	= "Leer!";
	if($chk($('infoLeer'))){
		switch(MooTools.version){
			case "1.11":
				var linkHtml 	= $('infoLeer').innerHTML;
				if (linkHtml.indexOf(errorTxt) == -1) $('infoLeer').innerHTML = errorTxt;
			break;			
			case "1.2.2":
				var linkHtml 	= $('infoLeer').get("html");
				if (linkHtml.indexOf(errorTxt) == -1) $('infoLeer').set("html", errorTxt);
			break;
		}
	}
}

/** enabled bzw. disabled Eingabefelder je nach Zahlungsart **/
function zeige_zahlung(zahlung){
	if(zahlung == "cc"){
		//$("Kreditkarte").style.display = "block";
		var status = false;
	}else{
		//$("Kreditkarte").style.display = "none";
		var status = true;
	}	
	var selects = $$('#Kreditkarte select');
	selects.each(function(el,i){
		el.disabled = status;
		el.selectedIndex = 0;
	},this);
	
	var inputs = $$('#Kreditkarte input');
	inputs.each(function(el,i){
		el.disabled = status;
		el.value = "";
	},this);
}

/** Öffnet bzw. focussiert popup-Window (z.B. für Maßtabellen) **/
var myPopup = null;
function showPopup(Adresse, Titel, Options) {
	var options = $chk(Options)? Options : "width=670, height=240, left=100, top=100, status=no, scrollbars=no, resizable=no, location=no"; 
	myPopup = window.open(Adresse, Titel, options);
	myPopup.focus();
}
