var ajaxserver = "/typo3conf/ext/tx_nwcshop/pi1/class.tx_txnwcshop_pi1_ajax.php";


$(document).ready(function(){
	$(".basketInsertConfirm").hide();
	nwcShopSmallbasketItems();
	
	if($(".basketIcon").length > 0){
	    $(".basketIcon").click(nwcShopAddItemToBasket);
	}
});


/** Auslesen der anzahl artikel im warenkorb mit gesammtsumme */
function nwcShopSmallbasketItems(){

	var urlparam = '?wk=15&out=text&sid='+nwcShopBasketGlobSessionId;
	$.get(ajaxserver+urlparam, function(text){
	    $("#warenkorbtext").html(text);
	});

	var urlparam = '?wk=15&out=sum&sid='+nwcShopBasketGlobSessionId;
	$.get(ajaxserver+urlparam, function(text){
	    $("#warenkorbtotal").html(text);
	});
}

/**
 * Holt beim ajax server die aktuelle uid
 */
function libGetUid(){
	$.ajax({type: "POST", async: false, url: ajaxserver, data: "wk=09", success: function(uid){$("#userid").html(uid);}});
}

function nwcShopReg(){

/*	$("#nwcShopLoginRegZip").keyup(function(){
	
	    $("#nwcShopLoginRegLocationSelect").show();
	    $("#nwcShopLoginRegLocationSelect > div").remove();
	
	    var urlparam = '?wk=07&search='+$("#nwcShopLoginRegZip").val();
	    $.get(ajaxserver+urlparam, function(liste){
	        
	        $("#nwcShopLoginRegLocationSelect").append(liste);
	    });
	});
*/
}


//	login
function nwcShopLogin(){

	$("#nwcShopLoginErrormsg").hide();
	$("#nwcShopLoginErrormsg2").hide();
	$("#AjaxLoaderLogin").hide();
	$("#AjaxLoaderReg").hide();
	
	$("#nwcShopLoginRegSubmit").click(function(){
	
	    $("#AjaxLoaderReg").show();
	    
	    var urlparam = '?wk=06&name='+$("#nwcShopLoginRegName").val()+'&firstname='+$("#nwcShopLoginRegFirstname").val()+'&street='+$("#nwcShopLoginRegStreet").val();
		urlparam = urlparam+'&plz='+$("#nwcShopLoginRegZip").val()+'&ort='+$("#nwcShopLoginRegLocation").val();
		urlparam = urlparam+'&land='+$("#nwcShopLoginRegCountry").val()+'&tel='+$("#nwcShopLoginRegPhone").val();
		urlparam = urlparam+'&mail='+$("#nwcShopLoginRegMail").val()+'&pw1='+$("#nwcShopLoginRegPassword").val();
		urlparam = urlparam+'&firm='+$("#nwcShopLoginRegFirm").val();
		urlparam = urlparam+'&pw2='+$("#nwcShopLoginRegPasswortconfirm").val()+'&ucpid='+nwcShopBasketGlobUcPid+'&sid='+nwcShopBasketGlobSessionId;
	    $.get(ajaxserver+urlparam, function(code){
	    
	        var checkData = '1';
	        
			if(code == '1'){
				$("#nwcShopLoginErrormsg2").html('Bitte f&uuml;llen Sie alle mit einem * markierten Felder aus!');
				$("#nwcShopLoginErrormsg2").show();
				var checkData = '0';
			}
			if(code == '2'){
				$("#nwcShopLoginErrormsg2").html('Die beiden Passworte sind nicht identisch!');
				$("#nwcShopLoginErrormsg2").show();
				var checkData = '0';
			}
			if(code == '4'){
				$("#nwcShopLoginErrormsg2").html('Die eingegebene Mail-Adresse ist nicht g&uuml;ltig!');
				$("#nwcShopLoginErrormsg2").show();
				var checkData = '0';
			}
			if(code == '5'){
				$("#nwcShopLoginErrormsg2").html('Die Mail Adresse existiert bereits!');
				$("#nwcShopLoginErrormsg2").show();
				var checkData = '0';
			}

			// pruefen der logindaten
			if(checkData == '1'){
				window.location.href ='index.php?id='+nwcShopBasketGlobBestelluebersichtPid;
			}else{
				setTimeout("$('#nwcShopLoginErrormsg2').hide()", 1500);
			}

	    });
	    
	    $("#AjaxLoaderReg").hide();
	});
	
	$("#nwcShopLoginSubmit").click(function(){
	
		var checkData = '1';
		$("#AjaxLoaderLogin").show();
		
		if($("#nwcShopLoginPassword").val()==''){
			$("#nwcShopLoginErrormsg").html('Bitte geben Sie ihr Passwort ein!');
			$("#nwcShopLoginErrormsg").show();
			var checkData = '0';
		}
		if($("#nwcShopLoginMail").val()==''){
			$("#nwcShopLoginErrormsg").html('Bitte geben Sie ihre E-Mail-Adresse ein!');
			$("#nwcShopLoginErrormsg").show();
			var checkData = '0';
		}
		
		// pruefen der logindaten
		if(checkData == '1'){
			var urlparam = '?wk=05&u='+$("#nwcShopLoginMail").val()+'&p='+$("#nwcShopLoginPassword").val()+'&p='+$("#nwcShopLoginPassword").val()+'&sid='+nwcShopBasketGlobSessionId;
			$.get(ajaxserver+urlparam, function(text){
			
				if(text=='0'){
					$("#nwcShopLoginErrormsg").html('Die Logindaten sind nicht korrekt!');
					$("#nwcShopLoginErrormsg").show();
					setTimeout("$('#nwcShopLoginErrormsg').hide()", 1500);
				}
				else{
					window.location.href ='index.php?id='+nwcShopBasketGlobBestelluebersichtPid;
				}
			});
		}else{
			setTimeout("$('#nwcShopLoginErrormsg').hide()", 1500);
		}
		
		$("#AjaxLoaderLogin").hide();
	});
}

//	element aus dem Wahrenkorb entfernen
function nwcShopBasketDeleteItem(objekt){
	alert('test');
}

//	fuellt den wahrenkorb
function nwcShopBasketFill(){
	
	//	warenkorb elemente raumen
	if(nwcShopBasketFixHeight=='false'){$("#nwcShopBasketMainCon").css({"height":"auto"});}
	$("#nwcShopBasketMainCon > div").remove();

	//	ajax warenkorb artikel auslesen
	var ajaxurl = ajaxserver+"?wk=01&sid="+nwcShopBasketGlobSessionId;
	
	$.ajax({
		type: "POST",
		url: ajaxserver,
		data: "wk=01&sid="+nwcShopBasketGlobSessionId,
		success: function(text){
		
			//	zurueckgegebenen warenkorbliste div zuweisen
			$("#nwcShopBasketMainCon").append(text);

			// zur kasse ausblenden wenn keine artikel im warenkorb sind
			if($("#shopNoArticle").length > 0){
				$("#shopBasketButtonZurKasse").hide();
			}

			//	delete-icon funktion zuweisen
			$(".nwcShopBasketItemDelete").click(function(){

				//	produkt aus dem warenkorb loeschen
				$.ajax({
					type: "POST",
					url: ajaxserver,
					data: "wk=02&sid="+$(this).attr('id'),
					success: function(text){
						//	warenkorb neu erstellen
						nwcShopBasketFixHeight = 'false';
						nwcShopBasketFill();
					}
				});
			});
			
			//	erhoehen-icon funktion zuweisen
			$(".nwcShopBasketItemCountAdd").click(function(){

				nwcShopBasketFixHeight = 'true';
				$("#nwcShopBasketMainCon").height($("#nwcShopBasketMainCon").height());

				//	produkt aus dem warenkorb loeschen
				$.ajax({
					type: "POST",
					url: ajaxserver,
					data: "wk=03&sid="+$(this).attr('id'),
					success: function(text){
						//	warenkorb neu erstellen
						nwcShopBasketFill();
					}
				});
			});

			//	entfernen-icon funktion zuweisen
			$(".nwcShopBasketItemCountSub").click(function(){

				nwcShopBasketFixHeight = 'true';
				$("#nwcShopBasketMainCon").height($("#nwcShopBasketMainCon").height());

				//	produkt aus dem warenkorb loeschen
				$.ajax({
					type: "POST",
					url: ajaxserver,
					data: "wk=04&sid="+$(this).attr('id'),
					success: function(text){
						//	warenkorb neu erstellen
						nwcShopBasketFill();
					}
				});
			});
		  }
	});
}

function nwcShopAddItemToBasket(){

    var jetzt = new Date();
	var articleId = $(this).attr("articleid");
	var itemCountField = $("#itemCount_"+articleId);
	var confirmMsgDiv = $("#basketConfirmDiv_"+articleId);
	var urldaten = "wk=16&ok="+jetzt.getTime()+"&aid="+articleId+"&sid="+nwcShopBasketGlobSessionId+"&"+$(this).attr("url")+'&basket='+itemCountField.val();
	
	
	if($("#articleOption_"+$(this).attr("articleid")).length > 0){urldaten = urldaten+'&shop_ao_option_feld='+$("#articleOption_"+$(this).attr("articleid")).val();}

	$.ajax({
		type: "POST",
		url: ajaxserver,
		data: urldaten,
		success: function(text){
			nwcShopSmallbasketItems();
			itemCountField.val("1");
			confirmMsgDiv.fadeIn();
   			setTimeout("$(\"#basketConfirmDiv_"+articleId+"\").fadeOut()",5000);
		}
	});

}






