// based on MooTools, My Object Oriented Javascript Tools. 
// Copyright (c) 2006-2007 Valerio Proietti, <http://mad4milk.net>, MIT Style License.

// var path = 'http://www.festtagsmode-by-laube.de/'; wird in head.php generiert

var PRODUCT_OPTIONS_MAX = 3; //maximal 3 selbstvergebene Attribute wie Farbe, Groesse, Geschmack
var deal = false;
var ses = "XTCsid=";
var https;
var minSuche = 3; // mind. 4 Zeichen zur Suche benoetigt
var nachMass = false; // fuer Bestellungen nach Maß + Popup
var nachMassComplete = false; // Wenn alle Angaben gespeichert worden sind, dann true - sonst false
var lieferzeit; // bei nachMass-Produkten andere Lieferzeit

/** onload  **/
window.addEvent('domready', function(){
	iniGlobalVars();
	iniWarenkorb();
	iniSucheBox();
	iniGroesseValue();
});

/** Select Farben **/
function setSelectedColor(opt){
	opt.style.background = opt.options[opt.selectedIndex].style.background;
	opt.style.color = opt.options[opt.selectedIndex].style.color;
}

/** Wenn Maßanfertigung ausgewählt wird, dann Hinweistext via alert einblenden **/
var standartPreis;
var standartLieferzeit;
function iniGroesseValue(){
	if($chk($('preis'))){
		var clone = $('preis').clone();
		standartPreis = clone;
		standartPreis.set("id", "preis");
	}
	else standartPreis = false;
	if($chk($('lieferstatus'))){
		var clone = $('lieferstatus').clone();
		standartLieferzeit = clone;
		standartLieferzeit.set("id", "lieferstatus");
	}
	else standartLieferzeit = false;
}
function groesseSelect(selectElm){
	if(selectElm.options[selectElm.selectedIndex].text.indexOf("Größe nach Maß") != -1){
		var start = selectElm.options[selectElm.selectedIndex].text.indexOf("(");
		var ende = selectElm.options[selectElm.selectedIndex].text.indexOf(")");
		lieferzeit = selectElm.options[selectElm.selectedIndex].text.substring(start+1, ende);
		if(lieferzeit!="") $('lieferstatus').set('html', "Lieferzeit "+lieferzeit).highlight();
		nachMass = true;
		preisNachMass = $('attributPrice'+selectElm.options[selectElm.selectedIndex].value).value;
		$('preis').set('html', preisNachMass).highlight();
	}else{
		if(nachMass){
			nachMass = false;
			$('preis').set('html', standartPreis.get("html")).highlight();
			if(lieferzeit!="") $('lieferstatus').set('html', standartLieferzeit.get("html")).highlight();
		}
	}
}

/** 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(path!="" && $chk($('warenkorb'))){ // !https && 
		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!=""){
		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;	
		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;
		
		if(nachMass){
			var params = "?typ_name="+form.typ_name.value;
			params += "&typ_id="+form.typ_id.value;
			params += "&products_id="+form.products_id.value;
			params += "&lieferzeit="+lieferzeit;
			
			//alert("nachMass.php"+params+"\n"+path);
			Shadowbox.open({
				player: 'iframe',
			 	content: path+"nachMass.php"+params,
			 	height: 700,
			 	width: 570,
				options: {
	 				onClose: function(){ 
						if(!nachMassComplete){
							if(!Browser.Engine.presto) alert("Der Artikel wurde NICHT in den Warenkorb gelegt,\nbitte bestaetigen Sie zuerst Ihre Eingaben.");
							return false;
						}
						else{
							if(showAlert && !Browser.Engine.presto) alert("Der Artikel wird in den Warenkorb gelegt.");
							showWarenkorb(urlPath); 
						}
					},
					modal: "false"
 				}
			});
		}else{
			if(showAlert) alert("Der Artikel wird in den Warenkorb gelegt.");
			showWarenkorb(urlPath);
		}
	}
}

function showWarenkorb(urlPath){
	switch(MooTools.version){
		case "1.11":
			new Ajax(urlPath, {
				method: 'get',
				update: $('warenkorb')
			}).request()
		break;
		
		default:
			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;
		}
	}
}

/* öffnet den nachMass-Dialog bei der Select-Box im Kundenprofil */
function openDialog(typ_id, typ_name){
	if(typ_id != 0){
		Shadowbox.open({
			player: 'iframe',
		 	content: path+'/nachMass.php?typ_id='+typ_id+'&typ_name='+typ_name+'&account_edit=1',
		 	height: 700,
		 	width: 570,
			options: {
				onClose: function(){ 
					if(!nachMassComplete){
						alert("Ihre Werte wurden nicht gespeichert,\nbitte bestaetigen Sie zuerst Ihre Eingaben.");
						return false;
					}
					else{
						alert("Ihre Werte wurden gespeichert.");
					}
				},
				modal: "false"
			}
		});
	}
}

/** 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();
}

/** Entfernt den Kasten um Links bei klick **/

var browserName=navigator.appName;
var browserVer=parseInt(navigator.appVersion);
var version="";
var msie4=(browserName=="Microsoft Internet Explorer"&&browserVer>=4);
if((browserName=="Netscape"&&browserVer>=3)||msie4||browserName=="Konqueror"||browserName=="Opera"){
	version="n3";
}else{
	version="n2";
}
function blurLink(theObject){if(msie4){theObject.blur();}}