// NEEDED!!!!

var path = "/";

//

function runde(x, n) {
		if (n < 1 || n > 14) return false;
		var e = Math.pow(10, n);
		var k = (Math.round(x * e) / e).toString();
		if (k.indexOf('.') == -1) k += '.';
		k += e.toString().substring(1);
		return k.substring(0, k.indexOf('.') + n+1);
	}

function show_Zusatz(){
	jQuery("#zusatzBox").fadeIn("slow");
}
function hide_Zusatz(){
	jQuery("#zusatzBox").fadeOut("slow");
}


// Basket

function wk_toggle(){
	scroll(0,0);
	var state = jQuery("div#wk_open").css("display");
	if (state=="none"){
		
		jQuery("#wkcontent").load("shop/module.shop.wk.php", function(){
			jQuery("#wkform").load("shop/module.shop.form.php"); // load form 
			var height= document.body.offsetHeight;
			jQuery("div#wk_open").css("height",height);
			jQuery("div#wk_open").fadeIn("fast", function() {
        		jQuery("div#wk_open").css("display","block");
        		jQuery("#wk_toggle").html('ZURÜCK');
     	 	});
		});
	}
	if (state=="block"){
		jQuery("div#wk_open").fadeOut("fast", function() {
        	jQuery("div#wk_open").css("display","none");
        	jQuery("#wk_toggle").html('BESTELLEN');
     	 });
	}
}

// Artikel im Warenkorb

function add_article(art_num){
	shop.location.href=path+"shop/shop.php?add_article="+art_num;
/*
	var amount = parseFloat(jQuery("#pieces"+art_num).html()) +1;
	update_basket(art_num,"add");
	jQuery("#pieces"+art_num).html(amount);
*/
}
function remove_article(art_num){
	var amount = parseInt(jQuery("#pieces"+art_num).html());
	if (amount >0){
		shop.location.href=path+"shop/shop.php?remove_article="+art_num;
		amount --;
		//if (amount <0) { amount=0; }
		//jQuery("#pieces"+art_num).html(amount);
	}



/*
	shop.location.href=path+"shop/shop.php?remove_article="+art_num;
	var amount = parseFloat(jQuery("#pieces"+art_num).html()) -1;
	if (amount <0) { amount=0; }
	update_basket(art_num,"remove");
	jQuery("#pieces"+art_num).html(amount);
*/
}
function wk_add_article(art_num){
	shop.location.href=path+"shop/shop.php?add_article="+art_num;
	var amount = parseFloat(jQuery("#wk_pieces"+art_num).html()) +1;
	var price = parseFloat(jQuery("#art_price"+art_num).html());
	//update_basket(art_num,"add");
	jQuery("#wk_pieces"+art_num).html(amount);
	var new_price = runde(amount * price,2);
	jQuery("#wk_price"+art_num).html(new_price);
	// update page
	//jQuery("#pieces"+art_num).html(amount);

}
function wk_remove_article(art_num){
	var amount = parseInt(jQuery("#pieces"+art_num).html());
	if (amount >0){
		shop.location.href=path+"shop/shop.php?remove_article="+art_num;
		amount --;
		//if (amount <0) { amount=0; }
		//jQuery("#pieces"+art_num).html(amount);
	}


/*
	shop.location.href=path+"shop/shop.php?remove_article="+art_num;
	var amount = parseFloat(jQuery("#wk_pieces"+art_num).html()) -1;
	if (amount <0) { amount=0; }
	var price = parseFloat(jQuery("#art_price"+art_num).html());
	update_basket(art_num,"remove");
	jQuery("#wk_pieces"+art_num).html(amount);
	//jQuery("#price"+art_num).html(runde(amount * price,2));
	var new_price = runde(amount * price,2);
	jQuery("#wk_price"+art_num).html(new_price);
	// update page
	jQuery("#pieces"+art_num).html(amount);
*/
}
function wk_delete_article(art_num){
	jQuery("#tr"+art_num).fadeOut("slow");
	shop.location.href=path+"shop/shop.php?delete_article="+art_num;
	var amount = parseFloat(jQuery("#wk_pieces"+art_num).html());
	var price = parseFloat(jQuery("#art_price"+art_num).html());
	update_basket(art_num,"delete");
	amount = 0;
	jQuery("#wk_pieces"+art_num).html(amount);
	var new_price = 0;
	jQuery("#wk_price"+art_num).html(new_price);
	jQuery("#pieces"+art_num).html(amount);
}

function update_basket(art_num,action){

	var art_pieces = parseFloat(jQuery('#pieces'+art_num).html());
	var art_price = parseFloat(jQuery('#art_price'+art_num).html().replace(",","."));
	var wk_pieces = parseFloat(jQuery('#wk_items').html());
	var wk_price =  parseFloat(jQuery('#wk_price').html().replace(",","."));

//alert("artikel: "+art_num+" action: "+action+" art_price: "+art_price+" wk_price: "+wk_price+" art_pieces: "+art_pieces+" wk_pieces: "+wk_pieces);
	
	if (art_pieces >=0 && action=="add" ){
		var new_price = wk_price + art_price;
		var new_pieces = wk_pieces + 1;
		jQuery('#wk_items').html(new_pieces);
		jQuery('#wk_price').html(runde(new_price,2));
		jQuery("#pieces"+art_num).html(art_pieces +1);
	}
	if (art_pieces >0 && action=="remove" ){
		var new_price = wk_price - art_price;
		var new_pieces = wk_pieces - 1;
		jQuery('#wk_items').html(new_pieces);
		jQuery('#wk_price').html(runde(new_price,2));
		jQuery("#pieces"+art_num).html(art_pieces-1);
		
		var art_amount = parseInt(jQuery("#wk_pieces"+art_num).html()) -1;
		jQuery("#wk_pieces"+art_num).html(art_amount);
		
		var new_art_price = runde(art_amount * art_price,2)
		//alert("art_amount: "+art_amount+" art_price: "+art_price+ "new_art_price: "+new_art_price);
		jQuery("#wk_price"+art_num).html(new_art_price);

	}
	if (action=="delete" ){
		var new_price = wk_price - (art_pieces * art_price);
		var new_pieces = wk_pieces - art_pieces;
		jQuery('#wk_items').html(new_pieces);
		jQuery('#wk_price').html(runde(new_price,2));
	}
	//alert("update: "+runde(new_price,2));
	jQuery('#wk_open_price').html(runde(new_price,2)); // Update open basket
}



// Fading

function art_in(art_num,cn_id){
	var old = jQuery("#art_list"+cn_id).html();
	var out = old.split(",");
	jQuery.each(out, function(k,v){
		if (v && v != art_num && parseFloat(jQuery("#pieces"+v).html()) == 0 ){
			jQuery("#over"+v).fadeOut("slow");
			//jQuery("#article_count"+art_num).fadeOut("slow");
		}
		if (v && v != art_num){
			jQuery("#article_count"+v).fadeOut("slow");
		}
		jQuery("#price"+v).fadeIn("slow");
	});
	jQuery("#over"+art_num).fadeIn("slow");
	//jQuery("#article_count"+art_num).fadeIn("slow");
}
function art_counter_in(art_num,cn_id){
	var old = jQuery("#art_list"+cn_id).html();
	var out = old.split(",");
	jQuery("#price"+art_num).fadeOut("fast");
	jQuery("#article_count"+art_num).fadeIn("slow");
	jQuery.each(out, function(k,v){
		if (v && v != art_num){
			jQuery("#article_count"+v).fadeOut("slow");
			jQuery("#price"+v).fadeIn("slow");
		}
	});
}

// Fading Free Text Position Template

function free_art_in(art_num,cn_id){
	var old = jQuery("#art_list"+cn_id).html();
	var out = old.split(",");
	jQuery.each(out, function(k,v){
		if (v && v != art_num && parseFloat(jQuery("#pieces"+v).html()) == 0 ){
			jQuery("#over"+v).fadeOut("slow");
		}
		if (v && v != art_num){
			jQuery("#article_count"+v).fadeOut("slow");
			if (v && v != art_num && parseFloat(jQuery("#pieces"+v).html()) > 0 ){
				jQuery("#price"+v).fadeIn("slow");
			}
		}
	});
	jQuery("#price"+art_num).fadeOut("slow");
	jQuery("#over"+art_num).fadeIn("slow");
	jQuery("#article_count"+art_num).fadeIn("slow");
}

// Fading Overlay

function art_in_overlay(art_num,cn_id){
	var old = jQuery("#art_list"+cn_id).html();
	var out = old.split(",");
	alert("art in overly");
	jQuery.each(out, function(k,v){
		if (v && v != art_num && parseFloat(jQuery("#pieces"+v).html()) == 0 ){
			jQuery("#over"+v).fadeOut("slow");
			//jQuery("#article_count"+art_num).fadeOut("slow");
		}
		if (v && v != art_num){
			jQuery("#article_count"+v).fadeOut("slow");
		}
		jQuery("#price"+v).fadeIn("slow");
	});
	jQuery("#over"+art_num).fadeIn("slow");
	//jQuery("#article_count"+art_num).fadeIn("slow");
}

function art_in_overlay_static(art_num,cn_id){
	var old = jQuery("#art_list"+cn_id).html();
	var out = old.split(",");
	jQuery.each(out, function(k,v){
		if (v && v != art_num && parseFloat(jQuery("#pieces"+v).html()) == 0 ){
			jQuery("#article_count"+v).fadeOut("fast");
			jQuery("#showpieces"+v).fadeOut("fast");
		}
		if (v && v != art_num){
			jQuery("#price"+v).fadeIn("fast");
			//jQuery("#article_count"+v).fadeOut("slow");
		}
		//jQuery("#price"+v).fadeIn("slow");
	});
	jQuery("#price"+art_num).fadeOut("fast");
	jQuery("#showpieces"+art_num).fadeIn("fast");
	jQuery("#article_count"+art_num).fadeIn("fast");
}

function art_counter_in_static(art_num,cn_id){
	var old = jQuery("#art_list"+cn_id).html();
	var out = old.split(",");
	jQuery.each(out, function(k,v){
		if (v && v != art_num && parseFloat(jQuery("#pieces"+v).html()) == 0 ){
			jQuery("#article_count"+v).fadeOut("fast");
			jQuery("#showpieces"+v).fadeOut("fast");
		}
		if (v && v != art_num){
			jQuery("#price"+v).fadeIn("fast");
			jQuery("#article_count"+v).fadeOut("fast");
		}
		//jQuery("#price"+v).fadeIn("slow");
	});
	jQuery("#price"+art_num).fadeOut("fast");
	jQuery("#showpieces"+art_num).fadeIn("fast");
	jQuery("#article_count"+art_num).fadeIn("fast");
}


// Fading Overlay

function art_counter_in_overlay(art_num,cn_id){
	var old = jQuery("#art_list"+cn_id).html();
	var out = old.split(",");
	jQuery.each(out, function(k,v){
		if (v && v != art_num && parseFloat(jQuery("#pieces"+v).html()) == 0 ){
			jQuery("#over"+v).fadeOut("slow");
			//jQuery("#article_count"+art_num).fadeOut("slow");
		}
		if (v && v != art_num){
			jQuery("#article_count"+v).fadeOut("slow");
		}
		jQuery("#price"+v).fadeIn("slow");
	});
	jQuery("#over"+art_num).fadeIn("slow");
	//jQuery("#article_count"+art_num).fadeIn("slow");
}

// Delivery

function delivery_area(plz){
	jQuery("#StoreCheck").load("shop/shop.php", { plz:plz }, function(){
		//
	});
}

// Field check

function check_field(id,typ){
	var value = jQuery("#"+id).val();
	// update session
	jQuery("#dummy").load("shop/shop.php", { field:id, val:value }, function(){
		//
	});
	//
	if (typ == "req"){
		if (value == "" ){
			
			jQuery("#"+id).css("background-color","#cb0000");
			jQuery("#"+id).css("color","#fff");
			return false;
		} else {
			jQuery("#"+id).css("background-color","#fff");
			jQuery("#"+id).css("color","#000");
			return true;
		}
	}
	if (typ == "num"){
		if (isNaN(value) == true || value == "" ){
			jQuery("#"+id).css("background-color","#cb0000");
			jQuery("#"+id).css("color","#fff");
			return false;
		} else {
			jQuery("#"+id).css("background-color","#fff");
			jQuery("#"+id).css("color","#000");
			return true;
		}
	}	
}
