function init(sid){
    $(document).ready(function() {
	$("a,img,input").tooltip();
	$(".menu1, .menu1lila").click(function (e) {
	    $(this).next().slideToggle();
	    e.preventDefault();
	});
	$("#nlBest").submit(function(e){
	    e.preventDefault();
	    nlBest();
	});
	wkAnzahl(sid);
    });
}

function initArtikel(sid){
    $(document).ready(function() {
	$(".menu1, .menu1lila").click(function (e) {
	    $(this).next().slideToggle();
	    e.preventDefault();
	});
	$(".merken").live("click", function(){
	    merken($(this).attr("id"), sid);
	    $(this).fadeOut("fast", function() {
		$(this).html("<img src='bilder/artikel_gemerkt.gif' alt='' title='' />");
		$(this).fadeIn("fast");
		$(this).tooltip();
	    });
	    return false;
	});
	$("#nlBest").submit(function(e){
	    e.preventDefault();
	    nlBest();
	});
	// FANCYBOX
	$("a.zoom").fancybox();
	$("a.zoom1").fancybox({
	    'overlayOpacity'	:	0.7,
	    'overlayColor'		:	'#333'
	});
	$("a.zoom2").fancybox({
	    'zoomSpeedIn'		:	500,
	    'zoomSpeedOut'		:	500
	});

	$("a,img,input").tooltip();
	wkAnzahl(sid);
    });
}

function initPreis(sid, min, max){
    $(document).ready(function() {
	$(".menu1, .menu1lila").click(function (e) {
	    $(this).next().slideToggle();
	    e.preventDefault();
	});
	fillContent(min, max);

	$(".merken").live("click", function(){
	    merken($(this).attr("id"), sid);
	    $(this).fadeOut("fast", function() {
		$(this).html("<img src='bilder/artikel_gemerkt.gif' alt='' title='' />");
		$(this).fadeIn("fast");
		$(this).tooltip();
	    });
	    return false;
	});
	$("#nlBest").submit(function(e){
	    e.preventDefault();
	    nlBest();
	});

	wkAnzahl(sid);
    });
}

function initWk(sid){
    $(document).ready(function() {
	$("a,img,input").tooltip();
	$(".menu1, .menu1lila").click(function (e) {
	    $(this).next().slideToggle();
	    e.preventDefault();
	});
	$(".liblau").click(function(event) {
	    if($("#endSumme").text().replace(".", "").replace(",", ".") < 150) {
		event.preventDefault();
	    }
	});
	$("img[id^='delete']").click(function(){
	    wkArtikelLoeschen($(this).attr('id').substring(6), sid)
	});
	$(".inve").change(function () {
	    wkAnzahlAendern($(this), sid)
	});
	$(".inve").numeric();
    });
}

function initBestell(sid){
    $(document).ready(function() {
	wkBestell(sid);
	$("a,img,input").tooltip();
	$(".menu1, .menu1lila").click(function (e) {
	    $(this).next().slideToggle();
	    e.preventDefault();
	});
	$('input[name=zahlart]:radio').click(function() {
	    if($("#lastschrift").attr("checked")){
		$("#ktoDetails").fadeIn();
	    }
	    else {
		$("#ktoDetails").fadeOut();
	    }
	});
	$("#plz, #blz").numeric();
    });
}

function initDanke(sid){
    $(document).ready(function() {
	$("a,img,input").tooltip();
	$(".menu1, .menu1lila").click(function (e) {
	    $(this).next().slideToggle();
	    e.preventDefault();
	});
	$("#nlBest").submit(function(e){
	    e.preventDefault();
	    nlBest();
	});
	$(window).unload(function() {
	    endSession(sid);
	});

    });
}

function initSuch(sid, suchstring) {
    var min, max;
    $(document).ready(function() {
	$("a,img,input").tooltip();
	$(".menu1, .menu1lila").click(function (e) {
	    $(this).next().slideToggle();
	    e.preventDefault();
	});

	$("#such").val(suchstring);
	fillContent(min, max, suchstring + " ");
	wkAnzahl(sid);
		
	$(".merken").live("click", function(){
	    merken($(this).attr("id"), sid);
	    $(this).fadeOut("fast", function() {
		$(this).html("<img src='bilder/artikel_gemerkt.gif' alt='' title='' />");
		$(this).fadeIn("fast");
		$(this).tooltip();
	    });
	    return false;
	});
	$("form").submit(function(e){
	    e.preventDefault();
	    fillContent(min, max, $("#such").val() + " ");
	});
    });
}

function fillContent(min, max, suchstring){
    var inhalt = "<br /><br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    $("#content").html("&nbsp;");
    if(suchstring){
	suchstring = $.trim(suchstring);
	if(suchstring.length <= 1 || suchstring == "Artikelsuche"){
	    inhalt += "Bitte geben Sie einen Suchbegriff ein.";
	    $("#content").prepend(inhalt);
	    return;
	}
    }
    if(min >= 0 || suchstring){
	var posY = $(window).height() / 2 - 200;
	var posX = $(window).width() / 2 - 72;
	$("#content").css("text-align", "center");
	$("#content").prepend(
	    "<div style='position:fixed; top:" + posY + "px; left:" + posX + "px;'>suche ...<br />" +
	    "<img src='bilder/processingbar.gif' alt='' width='145' height='15' /></div>"
	    );
    }
    var bild, button;
    var pfad = document.URL.split("/");
    var seite = pfad[pfad.length-1];
    var fmtPreis, warnhinweise;

    $.getJSON('php/functions.php', {
	fn:"showData", 
	seite:seite, 
	min:min, 
	max:max, 
	suchstring:suchstring
    }, function(data) {
	$("#content").empty();
	if(data.length == 0){
	    inhalt += "Zu Ihrer Suchanfrage <strong>" + suchstring + "</strong> wurde leider kein Artikel gefunden."
	    $("#content").prepend(inhalt);
	}
	$("#content").css("text-align", "left");
	for(var i in data) {
	    fmtPreis = parseFloat(data[i].preis).toFixed(2).replace(".", ",");
	    warnhinweise = data[i].warnhinweise;
	    if(warnhinweise){
		warnhinweise = "Achtung: " + warnhinweise;
	    }
	    else{
		warnhinweise = "";
	    }
	    bild = data[i].artNr + ".jpg";
	    inhalt  = '<div class="item">';
	    inhalt += '<div class="spic">';
	    inhalt += '<a class="zoom1" title="" href="xpic/' + bild + '"><img src="spic/' + bild + '" alt="" width="148" height="130" class="rah0" /></a>';
	    inhalt += '</div>';
	    inhalt += '<div class="no">Art.Nr.: ' + data[i].artNr + '</div>';
	    inhalt += '<div class="capt">' + data[i].header + '</div>';
	    inhalt += '<div class="txt">' + data[i].beschreibung + '</div>';
	    inhalt += '<div class="gro">Größe: ' + data[i].gro + ' cm</div>';
	    inhalt += '<div class="ve">VE: ' + data[i].ve + ' St.</div>';
	    inhalt += '<div class="uk">Kart: ' + data[i].uk + ' St.</div>';
	    inhalt += '<div class="preis">Preis: ' + fmtPreis + ' &#8364;</div>';
	    inhalt += '<div class="hinw">' + warnhinweise + '</div>';
	    // Artikel im Warenkorb? Dann artikel_gemerkt.gif anzeigen
	    button = (data[i].BestNr ? "artikel_gemerkt.gif" : "artikel_merken.gif");
	    inhalt += '<div class="merken" id="' + data[i].artNr + '"><img src="bilder/' + button + '" alt="" title="" /></div>';
	    inhalt += '</div>';
	    $("#content").prepend(inhalt);
	}
	// FANCYBOX
	$("a.zoom").fancybox();
	$("a.zoom1").fancybox({
	    'overlayOpacity'	:	0.7,
	    'overlayColor'		:	'#333'
	});
	$("a.zoom2").fancybox({
	    'zoomSpeedIn'		:	500,
	    'zoomSpeedOut'		:	500
	});

	$("a,img,input").tooltip();
    });
}

function merken(artNr, sid) {
    $.post("php/functions.php?sid=" + sid, {
	artNr: artNr, 
	fn: "aufDenMerkzettel"
    },
    function(){
	wkAnzahl(sid);
    }
    );
}

function wkAnzahl(sid){
    var anzahl = $.ajax({
	type: "POST",
	url: "php/functions.php?sid=" + sid,
	data: ({
	    fn:"wkAnzahlAnzeigen", 
	    sid: sid
	}),
	async: false,
	cache: false
    }).responseText;
    $("#anzArtikel").text(anzahl);
}

function wkArtikelLoeschen(row_id, sid){
    $.post("functions.php?sid=" + sid, {
	row_id: row_id, 
	fn: "wkArtikelLoeschen"
    },
    function(){
	window.location.reload();
    }
    );
}

function wkAnzahlAendern(elm, sid) {
    if(elm.val() < 1) elm.val(1);
    var row_id = elm.attr("id").substring(8);
    var wk = $.ajax({
	type: "POST",
	url: "functions.php?sid=" + sid,
	data: ({
	    sid: sid,
	    row_id: row_id,
	    anzahl: elm.val(),
	    fn:"wkAnzahlAendern"
	}),
	async: false,
	cache: false
    }).responseText;
    var arrWk = wk.split(":");

    $("#stckZahl" + row_id).text(number_format(arrWk[0], 0));
    $("#summe" + row_id).html(number_format(arrWk[1], 2) + " &#8364;");
    $("#endSumme").text(number_format(arrWk[2], 2));
    if(arrWk[2] < 150) {
	$(".rot,").css("visibility", "visible");
	$(".liblau").click(function(event) {
	    event.preventDefault();
	});
    }
    else {
	$(".rot").css("visibility", "hidden");
	$(".liblau").unbind("click");
    }
}

function wkBestell(sid){
    var phpFile;
    var currProtocol = window.location.protocol;
    var currHost = window.location.hostname;
    if(currProtocol == "https:" && currHost != "ssl.roha.tst"){
	currHost = currHost + "/www.volksfestartikel-berlin.de";
    }
    phpFile = currProtocol + "//" + currHost + "/php/functions.php";
    $.post(phpFile, {
	fn:"wkBestellAnzeigen", 
	sid: sid
    }, function(wk){
	var arrWk = wk.split(":");
	$("#anzArtikel").text(arrWk[0]);
	$("#summe").text(arrWk[1]);
    }, "text");
}

function endSession(sid){
    $.post("functions.php", {
	fn: "endSession", 
	sid:sid
    });
}

function nlBest() {
    $("#nlMsg").fadeOut("fast", function(){
	var phpFile;
	var currProtocol = window.location.protocol;
	var currHost = window.location.hostname;
	if(currProtocol == "https:"){
	    currHost = currHost + "/www.volksfestartikel-berlin.de";
	}
	phpFile = currProtocol + "//" + currHost + "/php/functions.php";
	$.post(phpFile, {
	    fn: "sendNlMail", 
	    NlMail: $("#NlMail").val()
	}, function(msg){
	    var arrmsg = msg.split(":");
	    if(arrmsg[0] == "true"){
		$("#nlMsg").css("color", "#4965D6");
	    }
	    else{
		$("#nlMsg").css("color", "red");
	    }
	    $("#nlMsg").text(arrmsg[1]).fadeIn("fast");
	}, "text");
    });
}

function number_format(number, decimals) {
    // http://kevin.vanzonneveld.net
    // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // + bugfix by: Michael White (http://crestidg.com)
    // + bugfix by: Benjamin Lupton
    // + bugfix by: Allan Jensen (http://www.winternet.no)
    // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)

    var dec_point = ",", thousands_sep = ".";

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function checkFields() {
    var fehler = false;

    $("input").each(function(){
	$(this).removeAttr("style");
    });

    if($("#AGB").is(":checked")) {
	$("#msg").attr("style", "visibility:hidden");
    } else {
	$("#msg").text("Bitte akzeptieren Sie die Allgemeinen Geschäftsbedingungen.");
	$("#msg").attr("style", "visibility:visible");
	return false;
    }

    if($("#gewerbe").is(":checked")) {
	$("#msg").attr("style", "visibility:hidden");
    } else {
	$("#msg").text("Bitte bestätigen Sie, dass Sie Gewerbetreibender sind.");
	$("#msg").attr("style", "visibility:visible");
	return false;
    }

    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if($("#eMail").val().search(emailRegEx) == -1) {
	$("#eMail").attr("style", "background-color: #FFFFAA;");
	$("#msg").text("Bitte geben Sie eine gültige Email-Adresse an.");
	$("#msg").attr("style", "visibility:visible");
	return false;
    }

    var pflichtFelder = "#name, #strasse, #plz, #ort, #telefon, #eMail"
    if($("#lastschrift").attr("checked")){
	pflichtFelder += ", #ktoInh, #ktoNr, #blz, #bank"
    }
    $(pflichtFelder).each(function() {
	this.value = $.trim(this.value);
	if(this.value == "") {
	    fehler = true;
	    $(this).attr("style", "background-color: #FFFFAA;");
	} else {
	    $(this).removeAttr("style");
	}
    });
    if(fehler) {
	$("#msg").text("Bitte füllen Sie alle Pflichtfelder aus.");
	$("#msg").attr("style", "visibility:visible");
	return false;
    } else {
	$("#msg").attr("style", "visibility:hidden");
	return true;
    }

}

jQuery.fn.numeric =
    function()
    {
	return this.each(function()
	{
	    $(this).keydown(function(e)
	    {
		var key = e.charCode || e.keyCode || 0;
		// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
		return (
		    key == 8 || 
		    key == 9 ||
		    key == 46 ||
		    (key >= 37 && key <= 40) ||
		    (key >= 48 && key <= 57) ||
		    (key >= 96 && key <= 105));
	    })
	})
    };


