var GeShop = new Class({

	Implements: [Options],

	options: {
		errorMsg: 'Validation failed.',
		test: function(field){return true;},
		colorName: 'tx_geshop_pi1[color]',
		sizeName: 'tx_geshop_pi1[size]',
		quantityName: 'tx_geshop_pi1[quantity]',
		submitName: 'tx_geshop_pi1[_submit_]'
	},

	form: null,
	
	initialize: function(options){
		this.setOptions(options);
	},

	getSize: function(mode){
		if (mode==null) mode='';
		if (form) return form.elements[this.sizeName+mode];
		return null;
	},

	getColor: function(mode) {
		if (mode==null) mode='';
		if (form) return form.elements[this.colorName+mode];
		return null;
	},

	getQuantity: function(mode) {
		if (mode==null) mode='';
		if (form) return form.elements[this.quantityName+mode];
		return null;
	},

	getSubmit: function(mode) {
		if (mode==null) mode='';
		if (form) return form.elements[this.submitName+mode];
		return null;
	},

	putInBasket: function(form) {
		this.form = $(form);
		// ist Element ein Form Element
		if (form && form.get('tag')=='form') {
			while (true) {
				size = this.getSize();
				color = this.getColor();
				quantity = this.getQuantity();
				submit = this.getSubmit();
				error = this.getMsg('configError');
				if (!(size && color && quantity && submit)) break;
				// Ist Farbe gewählt?
				error = this.getMsg('requiredColor');
				if (color.options[color.selectedIndex].value=='') break;
				// Ist Größe gewählt?
				error = this.getMsg('requiredSize');
				if (size.options[size.selectedIndex].value=='') break;
				// Ist Menge angegeben?
				error = this.getMsg('requiredQuantity');
				if (isNaN(eval("quantity.value")) || eval("quantity.value")==0 ) break;
				// keine Fehler dann weiter
				error ='';
				break;
			}		
			if (error) {
				alert(error);
			} else {
				submit.value="1";
				form.submit();
			}
		}
	},
	
	// Auswahl mit Select oder Hidden Fields 
	putInBasket2: function(form) {
		this.form = $(form);
		// ist Element ein Form Element
		if (form && form.get('tag')=='form') {
			while (true) {
				i=fehlercount=0;
				error = this.getMsg('requiredColor');
				while (true) {
					el = this.getColor('['+i+']');
					if (el==null) break;
					if (el.get('tag')=='select' && el.options[el.selectedIndex]=='!') fehlercount++;
					i++;
				}
				if (fehlercount==i) break;
				
				i=fehlercount=0;
				error = this.getMsg('requiredSize');
				while (true) {
					el = this.getSize('['+i+']');
					if (el==null) break;
					if (el.get('tag')=='select' && el.options[el.selectedIndex]=='!') fehlercount++;
					i++;
				}
				if (fehlercount==i) break;
		
				quantity = this.getQuantity();
				submit = this.getSubmit();

				error = this.getMsg('requiredQuantity');
				if (isNaN(eval("quantity.value")) || eval("quantity.value")==0 ) break;
				// keine Fehler dann weiter
				error ='';
				break;
			}		
			if (error) {
				alert(error);
			} else {
				submit.value="1";
				form.submit();
			}
		}
		
	},
	
	// Auswahl über Größentabelle
	putInBasket3: function( form, color, size ){
		this.form = $(form);
		// ist Element ein Form Element
		if (form && form.get('tag')=='form') {
			size = this.getSize('[0]');
			color = this.getColor('[0]');
			submit = this.getSubmit();
			if (size && color && submit) {
				// Alle Elemente sind vorhanden
				size.value = size;
				color.value = color;
				submit.value = "1";
				form.submit();
			}		
		}
	},
	getMsg : function(key) {
		return MooTools.lang.get('GeShop', key);
	}

});


function submitBasket( theform, thetype ) {
	theform['tx_geshop_pi1[submitBasket]'].value="1";
	theform.submit();
	if (thetype) {
		return true;
	}
}

function closeBasket( theform, thetype, action ) {
	theform['tx_geshop_pi1[submitBasket]'].value="2";
	theform['tx_geshop_pi1[action]'].value=action;
	theform.submit();
	if (thetype) {
		return true;
	}
}

function removeFromBasket( theform, thefield ) {
	if (theform[thefield]!=null) {
		theform[thefield].value=0;
		theform['tx_geshop_pi1[submitBasket]'].value="1";
		theform.submit();
	}	
}

function makeBundle( theform, thefield ) {
	if (theform[thefield]!=null) {
		theform[thefield].value=1;
		theform['tx_geshop_pi1[submitBasket]'].value="1";
		theform.submit();
	}	
}

function autoFocus( theform, pos ) {
	var i,color,size;
	i=0;
	while (true) {
		color = 'tx_geshop_pi1[color]['+i+']';
		size  = 'tx_geshop_pi1[size]['+i+']';
		if (theform[color]==null) {
			break;
		}
		if (pos!=i) {
			theform[color].selectedIndex=0;
			theform[size].selectedIndex=0;
		}
		i++;
	}
}


function shopBackUrl(p,nohash) {
	function ReplaceAll(Source,stringToFind,stringToReplace){

		  var temp = Source;

		    var index = temp.indexOf(stringToFind);

		        while(index != -1){

		            temp = temp.replace(stringToFind,stringToReplace);

		            index = temp.indexOf(stringToFind);

		        }

		        return temp;

		}	
	var url = location.href;
	if (nohash) {
		url = url.substring(0, url.length-location.hash.length);
	}
	p.href+='?tx_geshop_pi1[backurl]='+escape(url); //ReplaceAll(escape(url),'/','%2F');
	
}

/*
MooTools.lang.set('de-DE', 'GeShop', {
	requiredSize:'Bitte Größe wählen.',
	requiredColor:'Bitte eine Farbe wählen.',
	requiredQuantity:'Bitte Menge angeben.',
	configError:'Falsche Webshop Konfiguration!'

});

MooTools.lang.set('en-US', 'GeShop', {
	requiredSize:'Please select a size.',
	requiredColor:'Please select a color.',
	requiredQuantity:'Please specify quantity.',
	configError:'Incorrect webshop configuration!'

});
*/
window.addEvent('domready', function() {
	var sort = $$('select.geshop-sortmenu');
	sort.each(function(e) {
		e.addEvent('change', function(e) {
			e.stop();
			window.location = e.target[e.target.selectedIndex].value; 
		});
	});
	//$$('a[geshop^={').addEvent()
});
