var urlFormFr = 'http://www.trendy-workshop.com/achat/paiement/';	
	Trendy.Shopping = {
        idAddress:undefined,
	initialize: function(){
		Trendy.Shopping.initializeFormInfos();
		
		
		$("#pageContent.shopping .summary-box .promotion").unbind('click').click(function(){Trendy.Shopping.listPromotions()});
		
		//$("#pageContent.shopping .summary-box .country").unbind('click').click(function(){Trendy.Shopping.listCountry()});
		$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
				
		$("#pageContent .shoppingPayment .content.col1 form input[type=image]").each(function(e){ 												 			//alert($("#pageContent .shoppingPayment .content.col1").attr('class'));
																																																						
		  if($(this).attr('class') != 'cgvCB1') $(this).click(function(){return Trendy.Shopping.checkCGV2();});
		});
		
		/*$("#pageContent .shoppingPayment .content.col2 form input[type=image]").each(function(e){ 												 			alert($("#pageContent .shoppingPayment .content.col2").attr('class'));
			if($(this).attr('class') != 'cgvCB') $(this).click(function(){return Trendy.Shopping.checkCGV1();});
			//if($(this).attr('class') != 'cgvCB1') $(this).click(function(){return Trendy.Shopping.checkCGV2();});
		});*/
		
		//alert($("#pageContent .shoppingPayment .content.col12").attr('class'));
		/*$("#pageContent .shoppingPayment .content form input[type=hidden]").each(function(e){ 												 			//alert($(this).attr('name'));
			if($(this).attr('name') == 'checks') $(this).click(function(){return Trendy.Shopping.checkCGV2();});
			
		});*/
		
		$("#pageContent .shoppingPayment .content .validCGV").each(function(e){ 
			if($(this).attr('class') != 'noCGV') $(this).click(function(){return Trendy.Shopping.checkCGV();});
		});

	},
	setProduct: function(idProduct, intQuantity, strSizeKey,strExtraColorKey, idClientSale){	
		Trendy.Lightbox.processing();
		$.ajax({
			url: '/shopping/?id='+idProduct+'&qt='+intQuantity+'&sz='+strSizeKey+'&ec='+strExtraColorKey+'&ac=set&ic='+idClientSale,
			success: function(data){
				$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{},function(){
					//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
					$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
					$("#pageContent.shopping .summary-box .promotion").click(function(){Trendy.Shopping.listPromotions()});
					Trendy.Common.hidePrice();	
					Trendy.Common.pngFix();	
				});
				$("#pageContent.shopping .shopping-list").load('/shopping/summary-col1/', {}, function(){
					Trendy.Common.hidePrice();
					Trendy.Common.pngFix();
					$(".sizeHelp").click(function(){
						Trendy.Lightbox.sizeHelp($(".sizeHelp").attr("value"));
					});
				});
				$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
					Trendy.Lightbox.close();
				});
				
				if(strExtraColorKey!=""){
					document.getElementById("shoppingcartimage").src = "/image/?height=90&width=90&filename=/public/products/extracolor/"+strExtraColorKey+".jpg"; 	
				}
			}
		});
	},
	deleteFromBag: function(idProduct){
		Trendy.Lightbox.processing();
		$.ajax({
			url: '/shopping/?id='+idProduct+'&ac=delete',
			success: function(data){
				$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{},function(){
					//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
					$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
					$("#pageContent.shopping .summary-box .promotion").click(function(){
						Trendy.Shopping.listPromotions();
					});
				
					Trendy.Common.hidePrice();
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .shopping-list").load('/shopping/summary-col1/',{},function(){
					Trendy.Common.hidePrice();
					$(".sizeHelp").click(function(){
						Trendy.Lightbox.sizeHelp($(".sizeHelp").attr("value"));
					});

				});
				$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
					Trendy.Lightbox.close();
				});
				$("#pageContent.shopping .suggested-product-container").load('/shopping/suggested-product/',{},function(){
					Trendy.Common.hidePrice();
					$(".sizeHelp").click(function(){
						Trendy.Lightbox.sizeHelp($(".sizeHelp").attr("value"));
					});
					$("#pageContent.shopping .suggested-product").show("slow");
				});
				
				$("#pageContent.shopping .header.left .number").load('/shopping/number/');
				$("#pageHeader .myBasket").load('/index/my-basket/');
			}
		});
	},
	checkBagSizes: function(){
		var test = 0;
		$(".changeSize").each(function(i){
			if($(this).val() == '' && $(this).parent().parent().parent().css('display') != 'none'){
				test = 1;
				$(this).css({'background':'#ff0000'});
			}else{
				$(this).css('background','#747474');
			}
		});
		
		if(test == 1) return false;
	},
	initializeFormInfos: function(){                
		var options = {
			url: '/shopping/edit-infos/',
			success: function(data){                                
				if(data == true)
                                        location.reload();
					//document.location.href = urlFormFr;
				else if(data == -1){
                                        location.reload();
					//document.location.href = urlFormFr;                                        
					//alert('L\'email renseigné est déjà enregistré. Merci de vous identifier ou de changer d\'email');
                                }else if(data == -2){
                                        alert('i am here');
					alert('Merci de vérifier la cohérence des données');
                                }
			}
		}
		$("#formShoppingInfos input").each(function(){
			$(this).focus(function(){
				$(this).parent().parent().find("label.error").hide();
				if ($(this).parent().parent().find(".alert").css('display') != 'block')
				{
					$(this).parent().parent().find(".about").fadeIn();
				}				
			});
			$(this).blur(function(){
				$(this).parent().parent().find(".about").hide();
				if ($(this).attr('name') == 'strEmail')
				{
					var email = $(this).val();
					$.post("/shopping/check-email/", {strEmail:email}, function(data){
						if (data == 1)
							$("#alert-email").fadeIn();
						else if (data == 0)
							$("#alert-email").hide();
					});
				}
			});
		});
		$("#formShoppingInfos").validate({
			rules:{
				strFirstName: {required:true},
				strEmail: {required:true, email: true},
				strLastName: {required:true},
				strTel: {required:true, rangelength:[8,15], number:true},
				strAddressShipLabel: {required:false},
				strStreetNumberShip: {required:true},
				strStreetNameShip: {required:true},
				strPostCodeShip: {required:true},
				strCityShip: {required:true},
				strStreetNumberInv: {required:true},
				strStreetNameInv: {required:true},
				strPostCodeInv: {required:true},
				strCityInv: {required:true}
			},
			messages:{
				strFirstName: '',
				strEmail: '',
				strLastName: '',
				strTel: '',
				strAddressShipLabel: '',
				strStreetNumberShip: '',
				strStreetNameShip: '',
				strPostCodeShip: '',
				strCityShip: '',
				strStreetNumberInv: '',
				strStreetNameInv: '',
				strPostCodeInv: '',
				strCityInv: ''
			},
			submitHandler: function(error) {                                
				$("#formShoppingInfos").ajaxSubmit(options);
			}
		});
	},
	submitInfos: function(){                
		if($("#invoiceAddressDiv").css('display') == 'none'){
			var s = $("#selectCountryShip")[0];
			$("select[name=idAddressInv]").attr("selectedIndex", $("select[name=idAddress]").attr("selectedIndex"));
			$("input[name=strAddressInvLabel]").val($("input[name=strAddressShipLabel]").val());
			$("input[name=strStreetNumberInv]").val($("input[name=strStreetNumberShip]").val());
			$("input[name=strStreetNameInv]").val($("input[name=strStreetNameShip]").val());
			$("input[name=strPostCodeInv]").val($("input[name=strPostCodeShip]").val());
			$("input[name=strCityInv]").val($("input[name=strCityShip]").val());
			$("input[name=strAddressMoreInv]").val($("input[name=strAddressMoreShip]").val());
			$("select[name=intCountryInv]").attr("selectedIndex", $("select[name=intCountryShip]").attr("selectedIndex"));
		}                
		$("#formShoppingInfos").submit();                
		return false;
			
	},
	listPromotions: function(){
		Trendy.fancybox.promotions();
	},
	checkStrCode: function(){		
		$(".waitingCode").show();
		if ($('#inputFloatRoyalties').val()){
			var options = {
				url: '/shopping/checkroyalties',
				data: "floatRoyalties=" + $('#formPromotionsCredits #inputFloatRoyalties').val(),
				success: function(data){
					if (data == 'true')
						Trendy.Shopping.promotionValidation();
					else {
						$('#formPromotionsCredits #inputFloatRoyaltiesError').fadeIn('slow').delay(5000).fadeOut('slow');
						$(".waitingCode").hide();
					}
				}
			}
			$.ajax(options);
		} else if ($('#inputFloatCredits').val()){
			var options = {
				url: '/shopping/checkcredits',
				data: 'floatCredits=' + $('#formPromotionsCredits #inputFloatCredits').val(),
				success: function(data){
					if (data == 'true')
						Trendy.Shopping.promotionValidation();
					else {
						$('#formPromotionsCredits #inputFloatCreditsError').fadeIn('slow').delay(5000).fadeOut('slow');
						$(".waitingCode").hide();
					}
				}
			}
			$.ajax(options);
		} else {
			$.ajax({
				type: "POST",
				data: "strCode=" + $("#formPromotionsCredits #inputStrCode").val(),
				url: "/shopping/checkpromotion",
				success: function(data){
					$(".waitingCode").hide();
					if (data == 1 || data == false)
						$('#strCodeError1').fadeIn('slow');
					else if (data == 2)
						$('#strCodeError2').fadeIn('slow');
					else if (data == 3)
						$('#strCodeError3').fadeIn('slow');
					else if (data == 5)
						$('#strCodeError4').fadeIn('slow');
					else if (data == 4){
						Trendy.Shopping.initializeFormPromotions();
					}
				}
			});
		}
	},
	promotionValidation: function(){
		$(".waitingCode").hide();
		var options = {
			url: '/shopping/promotions-credits/',
			success: function(data){
				$.fancybox.close();
				Trendy.Lightbox.processing();
				$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{}, function(){
					$("#pageContent.shopping .summary-box .promotion").click(function(){Trendy.Shopping.listPromotions();});
					//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
					$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
					Trendy.Common.hidePrice();
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .shopping-list").load('/shopping/summary-col1/', {}, function(){
					Trendy.Common.hidePrice();
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
					Trendy.Lightbox.close();
				});
			}
		}
		$("#formPromotionsCredits").ajaxSubmit(options);
	},
	initializeFormPromotions: function(){
		var options = {
			url: '/shopping/promotions-credits/',
			success: function(data){
				$.fancybox.close();
				Trendy.Lightbox.processing();
				$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{}, function(){
					$("#pageContent.shopping .summary-box .promotion").click(function(){Trendy.Shopping.listPromotions();});
					//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
					$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
					Trendy.Common.hidePrice();
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .shopping-list").load('/shopping/summary-col1/', {}, function(){
					Trendy.Common.hidePrice();
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
					Trendy.Lightbox.close();
				});
			}
		}
		
		$("#formPromotionsCredits").validate({
			rules:{
				floatCredits: {remote: "/shopping/checkcredits"},
				floatRoyalties: {remote: "/shopping/checkroyalties"}
			},
			messages:{
				floatCredits: '',
				floatRoyalties: ''
			},
			submitHandler: function(error) {
				$("#formPromotionsCredits").ajaxSubmit(options);
			}
		});
		$("#formPromotionsCredits").submit();
	},
	
	listCountry: function(){
		Trendy.Lightbox.open('/shopping/setcountry/','Trendy.Shopping.initializeFormCountry()',null);
	},
	initializeFormCountry: function(){
		var options = {
			url: '/shopping/setcountry/',
			success: function(data){
				//Trendy.Lightbox.close();
				$.fancybox.close();
				$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{}, function(){
					//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
					$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
					$("#pageContent.shopping .summary-box .promotion").click(function(){Trendy.Shopping.listPromotions()});
					Trendy.Common.hidePrice();
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .shopping-list").load('/shopping/summary-col1/', {}, function(){
					Trendy.Common.hidePrice();
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
				});
				//Trendy.Shopping.initialize();
			}
		}
		
		$("#formCountry").validate({
			
			messages:{intCountryShip: ''},
			submitHandler: function(error) {
				$("#formCountry").ajaxSubmit(options);
			}
		});
		$("#formCountry .submit").click(function(){$("#formCountry").submit();});
	},	
	
	checkCGV: function(){
		if($("input.cgvValidate").attr('checked')){
			
			return true;
		}else{
			alert('Merci de valider les Conditions Générales');
			return false;
		}
	},
	
	checkCGV1: function(){
		if($("input.cgvValidate1").attr('checked')){
			
			return true;
		}else{
			alert('Merci de valider les Conditions Générales');
			return false;
		}
	},
	
	checkCGV2: function(){
		if($("input.cgvValidate2").attr('checked')){
			
			return true;
		}else{
			alert('Merci de valider les Conditions Générales');
			return false;
		}
	},
	
	onChangeCountryShip: function(objet){
		if(objet.value==999){
			alert("Pour les pays non listés, veuillez nous contacter.");
			return;
		}
		$.ajax({
			type: "POST",
			data: "idCountry=" + objet.value,
			dataType: "json",
			url: '/shopping/getshippingfee/>',
			success: function(data){
				$("#floatShippingTTC")[0].innerHTML = data.floatShippingTTC + " €";
				$("#floatPriceTTC")[0].innerHTML = data.floatPriceTTC + " €";
				return;
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				if(!textStatus) textStatus="";
				if(!errorThrown) errorThrown="";
				alert(textStatus + errorThrown);
				return;
			}
		});
		
		
	},
	
	infos: function(id){
		Trendy.Lightbox.open('/shopping/suggested-infos/?id='+id,null,null);
	},
	
	addSuggestedProduct: function(id,size,color){
		
	window.location.href="/achat/?id="+id+"&ic=&ac=add&sz="+size+"&ec="+color+"&qt=1";	
		
	},
		
	controlKIDProduct: function(idProduct, intQuantity, strSizeKey, strExtraColorKey, idClientSale){
		if ($("#kid-"+idProduct).attr('checked') == true)
		{
			Trendy.Lightbox.processing();
			$.ajax({
				url: '/shopping/?id='+idProduct+'&qt='+intQuantity+'&sz='+strSizeKey+'&ec='+strExtraColorKey+'&ac=set&ic='+idClientSale,
				success: function(data){
					$("#pageContent.shopping .suggested-product-container").load('/shopping/suggested-product/',{},function(){
						Trendy.Common.hidePrice();
						$(".sizeHelp").click(function(){
							Trendy.Lightbox.sizeHelp($(".sizeHelp").attr("value"));
						});
						$("#pageContent.shopping .suggested-product").show("slow");
					});

					$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{},function(){
						//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
						$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
						$("#pageContent.shopping .summary-box .promotion").click(function(){
							Trendy.Shopping.listPromotions();
						});
					
						Trendy.Common.hidePrice();
						Trendy.Common.pngFix();

						$("#kid-container-"+idProduct).show();
					});
					$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
						Trendy.Lightbox.close();
					});
					
					$("#pageContent.shopping .header.left .number").load('/shopping/number/');
					$("#pageHeader .myBasket").load('/index/my-basket/');
				}
			});
			
		}
		else
		{
			Trendy.Lightbox.processing();
			$.ajax({
				url: '/shopping/?id='+idProduct+'&ac=delete',
				success: function(data){
					$("#pageContent.shopping .suggested-product-container").load('/shopping/suggested-product/',{},function(){
						Trendy.Common.hidePrice();
						$(".sizeHelp").click(function(){
							Trendy.Lightbox.sizeHelp($(".sizeHelp").attr("value"));
						});
						$("#pageContent.shopping .suggested-product").show();
					});

					$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{},function(){
						//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
						$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
						$("#pageContent.shopping .summary-box .promotion").click(function(){
							Trendy.Shopping.listPromotions();
						});
					
						Trendy.Common.hidePrice();
						Trendy.Common.pngFix();

						$("#kid-container-"+idProduct).hide();
					});
					$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
						Trendy.Lightbox.close();
					});
					
					$("#pageContent.shopping .header.left .number").load('/shopping/number/');
					$("#pageHeader .myBasket").load('/index/my-basket/');				
				}
			});

			
		}
	},
	
	openKIDProduct: function(idProduct) {
		$('#kid-' + idProduct).attr("checked", "checked");
		$('#kid-container-' + idProduct).show();
	},
	
	previewKidImage: function(strRef, size){
		Trendy.Lightbox.open('/shopping/preview-kid/?strRef='+strRef+'&size='+size,null,null);
	},

	editAddress: function() {
		var adress = $('#formShoppingInfos .ship #idAddressSelected').val();
		$('.shoppingInfos .loading').show();
		$("#formEditAddress-container").load('/myworkshop/myaddress-edit/?idAddress=' + adress +'&price=1', function(){
			$('#formEditAddress-container .buttonValidate').click(function(){                
				$('#formEditAddress-container #formEditAddress').submit();
				return false;
			});
			$('#backButton .actLink').click(function(){
				Trendy.Shopping.backEditAddress(); 
				return false;
			});
			$('#formEditAddress-container').show();
			$('#formShoppingInfos .inv').hide();
			$('#formShoppingInfos .ship').hide();
			$('.next-button').hide();
			$('.shoppingInfos .loading').hide();
			var options = {
				url: '/myworkshop/myaddress-edit/',
				type: 'POST',
				success: function(error){
					$('#formEditAddress-container').hide();
					$('#formShoppingInfos .ship').show();
					$('.next-button').show();
					$('#formShoppingInfos .inv').show();
					Trendy.Shopping.afterEditAddress();
					//Trendy.Shopping.afterEditAddressInv();
				}
			}
	
			$('#formEditAddress-container #formEditAddress').validate({
				rules:{
					strLabel: {required:true},
					strFirstName: {required:true},
					strLastName: {required:true},
					intStreetNumber: {required:true},
					strStreet: {required:true},
					intPostCode: {required:true},
					strCity: {required:true},
					strAdressMore: {required:false},
					strCountry: {required:true},
					strTel: {required:true, rangelength:[8,15], number:true}
				}, 
				messages:{
					strLabel: '',
					strFirstName: '',
					strLastName: '',
					intStreetNumber: '',
					strStreet: '',
					intPostCode: '',
					strCity: '',
					strAdressMore: '',
					strCountry: '',
					strTel: ''
				},
				submitHandler: function(error) {
					$('.shoppingInfos .loading').show();
					$('#formEditAddress-container #formEditAddress').ajaxSubmit(options);
				}
			});
		});
		/*        
		$('#formShoppingInfos').hide();
		$('#formEditAddress-container').show();

		//Format Edit Form
		$('#formEditAddress-container input[name=idAddress]').val($('#formShoppingInfos .ship select[name=idAddress]').val());
		$('#formEditAddress-container input[name=strLabel]').val($('#formShoppingInfos .ship input[name=strLabel]').val());
		$('#formEditAddress-container input[name=strFirstName]').val($('#formShoppingInfos .ship input[name=strFirstName]').val());
		$('#formEditAddress-container input[name=strLastName]').val($('#formShoppingInfos .ship input[name=strLastName]').val());
		$('#formEditAddress-container input[name=intStreetNumber]').val($('#formShoppingInfos .ship input[name=strStreetNumberShip]').val());
		$('#formEditAddress-container input[name=strStreet]').val($('#formShoppingInfos .ship input[name=strStreetNameShip]').val());
		$('#formEditAddress-container input[name=intPostCode]').val($('#formShoppingInfos .ship input[name=strPostCodeShip]').val());
		$('#formEditAddress-container input[name=strCity]').val($('#formShoppingInfos .ship input[name=strCityShip]').val());
		$('#formEditAddress-container input[name=strAdressMore]').val($('#formShoppingInfos .ship input[name=strAddressShipLabel]').val());
		$('#formEditAddress-container input[name=strTel]').val($('#formShoppingInfos .ship input[name=strTel]').val());
		$('#formEditAddress-container select[name=strCountry]').attr('selectedIndex', $('#formShoppingInfos .ship select[name=intCountryShip]').attr('selectedIndex'));
		$('#formEditAddress-container #chk-default-box').show();
		if ($('#formShoppingInfos .ship input[name=booDefault]').val() == 1)
		{
			$('#formEditAddress-container #chk-default-box').hide();
		}

		var options = {
			url: '/myworkshop/myaddress-edit/',
			type: 'POST',                        
			success: function(error){
				Trendy.Shopping.afterEditAddress();
				Trendy.Shopping.afterEditAddressInv();
			}
		}

		$('#formEditAddress-container #formEditAddress').validate({
			rules:{
				strLabel: {required:true},
				strFirstName: {required:true},
				strLastName: {required:true},
				intStreetNumber: {required:true},
				strStreet: {required:true},
				intPostCode: {required:true},
				strCity: {required:true},
				strAdressMore: {required:false},
				strCountry: {required:true},
				strTel: {required:true, rangelength:[8,15], number:true}
			}, 
			messages:{
				strLabel: '',
				strFirstName: '',
				strLastName: '',
				intStreetNumber: '',
				strStreet: '',
				intPostCode: '',
				strCity: '',
				strAdressMore: '',
				strCountry: '',
				strTel: ''
			},
			submitHandler: function(error) {
				$('.shoppingInfos .loading').show();
				$('#formEditAddress-container #formEditAddress').ajaxSubmit(options);
			}
		});
		*/
	},
	backEditAddress: function() {
		$('#formShoppingInfos').show();
		$('#formEditAddress-container').hide();
		$('#formShoppingInfos .inv').show();
		$('#formShoppingInfos .ship').show();
		$('.next-button').show();
	},
	afterEditAddress: function() {
		var idAddress = $('#formShoppingInfos .ship #idAddressSelected').val();
		Trendy.Subscribe.loadAddressForm(idAddress);
	},
	addAddress: function() {
		$('#formShoppingInfos').hide();
		$('#formAddAddress-container').show();

		//Format Add Form
		$('#formAddAddress-container input[type=text]').each(function(){
			$(this).val('');
			$(this).removeClass('error');
		});
		$('#formAddAddress-container label.error').each(function(){
			$(this).hide();
		});
		$('#formAddAddress-container select').attr('selectedIndex', 0);
                var that=this;
		var options = {
			url: '/size/adress/',
			type: 'POST',
			success: function(id){                                
				Trendy.Subscribe.loadAddressForm(id);
			}
		}

		$('#formAddAddress-container #formAddAddress').validate({
			rules:{
				strLabel: {required:true},
				strFirstName: {required:true},
				strLastName: {required:true},
				intStreetNumber: {required:true},
				strStreet: {required:true},
				intPostCode: {required:true},
				strCity: {required:true},
				strAdressMore: {required:false},
				strCountry: {required:true},
				strTel: {required:true, rangelength:[8,15], number:true}
			}, 
			messages:{
				strLabel: '',
				strFirstName: '',
				strLastName: '',
				intStreetNumber: '',
				strStreet: '',
				intPostCode: '',
				strCity: '',
				strAdressMore: '',
				strCountry: '',
				strTel: ''
			},
			submitHandler: function(error) {
				$('.shoppingInfos .loading').show();
				$('#formAddAddress-container #formAddAddress').ajaxSubmit(options);
			}
		});
	},
	backAddAddress: function() {
		$('#formShoppingInfos').show();
		$('#formAddAddress-container').hide();

		//Select original Address
		if ($('#formShoppingInfos select[name=idAddress]').attr('selectedIndex') == 0)
		{
			$('#formShoppingInfos select option').each(function(){
				if ($(this).val() == $('#formShoppingInfos #idAddressSelected').val())
				{
					$(this).attr('selected', true);
				}
			});
		}
	},
	afterAddAddress: function() {
		var idAddress = $('#formShoppingInfos .ship #idAddressSelected').val();
                if(idAddress=='' || idAddress==undefined){
                    idAddress = this.idAddress;
                }
		Trendy.Subscribe.loadAddressForm(idAddress);
	},

	makeDefaultAddress: function(idClient, idAddress, link) {
		$('.shoppingInfos .loading').show();
		$.ajax({
			type: "POST",
			data: "idClient=" + idClient + "&idAddress=" + idAddress,
			url: '/shopping/set-default-address/',
			success: function(data){
				$(link).remove();
				$('.shoppingInfos .loading').hide();
			}
		});
	},

	editAddressInv: function() {
		var adress = $('#formShoppingInfos .inv #idAddressInvSelected').val();
		$('.shoppingInfos .loading').show();
		$("#formEditAddress-container").load('/myworkshop/myaddress-edit/?idAddress=' + adress, function(){
			$('#formEditAddress-container .buttonValidate').click(function(){                
				$('#formEditAddress-container #formEditAddress').submit();
				return false;
			});
			$('#backButton .actLink').click(function(){
				Trendy.Shopping.backEditAddressInv(); 
				return false;
			});
			$('#formEditAddress-container').show();
			$('#formShoppingInfos .inv').hide();
			$('#formShoppingInfos .ship').hide();
			$('.next-button').hide();
			$('.shoppingInfos .loading').hide();
			var options = {
				url: '/myworkshop/myaddress-edit/',
				type: 'POST',
				success: function(error){
					$('#formEditAddress-container').hide();
					$('#formShoppingInfos .ship').show();
					$('.next-button').show();
					$('#formShoppingInfos .inv').show();
					//Trendy.Shopping.afterEditAddress();
					Trendy.Shopping.afterEditAddressInv();
				}
			}
	
			$('#formEditAddress-container #formEditAddress').validate({
				rules:{
					strLabel: {required:true},
					strFirstName: {required:true},
					strLastName: {required:true},
					intStreetNumber: {required:true},
					strStreet: {required:true},
					intPostCode: {required:true},
					strCity: {required:true},
					strAdressMore: {required:false},
					strCountry: {required:true},
					strTel: {required:true, rangelength:[8,15], number:true}
				}, 
				messages:{
					strLabel: '',
					strFirstName: '',
					strLastName: '',
					intStreetNumber: '',
					strStreet: '',
					intPostCode: '',
					strCity: '',
					strAdressMore: '',
					strCountry: '',
					strTel: ''
				},
				submitHandler: function(error) {
					$('.shoppingInfos .loading').show();
					$('#formEditAddress-container #formEditAddress').ajaxSubmit(options);
				}
			});
		});

		//Format Edit Form
		/*
		$('#formEditAddress-container input[name=idAddress]').val($('#formShoppingInfos .inv select[name=idAddressInv]').val());
		$('#formEditAddress-container input[name=strLabel]').val($('#formShoppingInfos .inv input[name=strLabelInv]').val());
		$('#formEditAddress-container input[name=strFirstName]').val($('#formShoppingInfos .inv input[name=strFirstNameInv]').val());
		$('#formEditAddress-container input[name=strLastName]').val($('#formShoppingInfos .inv input[name=strLastNameInv]').val());
		$('#formEditAddress-container input[name=intStreetNumber]').val($('#formShoppingInfos .inv input[name=strStreetNumberInv]').val());
		$('#formEditAddress-container input[name=strStreet]').val($('#formShoppingInfos .inv input[name=strStreetNameInv]').val());
		$('#formEditAddress-container input[name=intPostCode]').val($('#formShoppingInfos .inv input[name=strPostCodeInv]').val());
		$('#formEditAddress-container input[name=strCity]').val($('#formShoppingInfos .inv input[name=strCityInv]').val());
		$('#formEditAddress-container input[name=strAdressMore]').val($('#formShoppingInfos .inv input[name=strAddressInvLabel]').val());
		$('#formEditAddress-container input[name=strTel]').val($('#formShoppingInfos .inv input[name=strTelInv]').val());
		$('#formEditAddress-container select[name=strCountry]').attr('selectedIndex', $('#formShoppingInfos .inv select[name=intCountryInv]').attr('selectedIndex'));
		$('#formEditAddress-container #chk-default-box').show();
		if ($('#formShoppingInfos .inv input[name=booDefaultInv]').val() == 1)
		{
			$('#formEditAddress-container #chk-default-box').hide();
		}
		*/
	},
	backEditAddressInv: function() {
		$('#formShoppingInfos').show();
		$('#formEditAddress-container').hide();
		$('#formShoppingInfos .inv').show();
		$('#formShoppingInfos .ship').show();
		$('.next-button').show();
	},
	afterEditAddressInv: function() {
		var idAddress = 0;
		if ($('#formShoppingInfos #idAddressInvSelected').length)
			idAddress = $('#formShoppingInfos #idAddressInvSelected').val();
		Trendy.Subscribe.loadAddressInvForm(idAddress);
	},
	addAddressInv: function(target) {
		if (target != 'self')
			$('#formShoppingInfos').hide();
		$('#formAddAddress-container').show();

		//Format Add Form
		$('#formAddAddress-container input[type=text]').each(function(){
			$(this).val('');
			$(this).removeClass('error');
		});
		$('#formAddAddress-container label.error').each(function(){
			$(this).hide();
		});
		$('#formAddAddress-container select').attr('selectedIndex', 0);

		var options = {
			url: '/size/adress/',
			type: 'POST',
			success: function(error){
				Trendy.Shopping.afterAddAddressInv(error);
				Trendy.Shopping.afterAddAddress();				
			}
		}

		$('#formAddAddress-container #formEditAddress').validate({
			rules:{
				strLabel: {required:true},
				strFirstName: {required:true},
				strLastName: {required:true},
				intStreetNumber: {required:true},
				strStreet: {required:true},
				intPostCode: {required:true},
				strCity: {required:true},
				strAdressMore: {required:false},
				strCountry: {required:true},
				strTel: {required:true, rangelength:[8,15], number:true}
			}, 
			messages:{
				strLabel: '',
				strFirstName: '',
				strLastName: '',
				intStreetNumber: '',
				strStreet: '',
				intPostCode: '',
				strCity: '',
				strAdressMore: '',
				strCountry: '',
				strTel: ''
			},
			submitHandler: function(error) {
				$('.shoppingInfos .loading').show();
				$('#formAddAddress-container #formEditAddress').ajaxSubmit(options);
			}
		});
	},
	backAddAddressInv: function() {
		$('#formShoppingInfos').show();
		$('#formAddAddress-container').hide();

		//Select original Address
		if ($('#formShoppingInfos select[name=idAddressInv]').attr('selectedIndex') == 0)
		{
			$('#formShoppingInfos select option').each(function(){
				if ($(this).val() == $('#formShoppingInfos #idAddressSelected').val())
				{
					$(this).attr('selected', true);
				}
			});
		}
	},
	afterAddAddressInv: function(id) {
		/*
		var idAddress = 0;
		if ($('#formShoppingInfos #idAddressInvSelected').length)
			idAddress = $('#formShoppingInfos #idAddressInvSelected').val();
		*/
		Trendy.Subscribe.loadAddressInvForm(id);
	},
	expandInv: function() {
		if ($('#formShoppingInfos #idAddressInvSelected').length || $('#formShoppingInfos #strEmail').length)
		{
			$('#invoiceAddressDiv').fadeIn();
			$('#formShoppingInfos .next-button').show();
		}
		else
		{
			$('#formAddAddress-container').fadeIn();
			$('#formShoppingInfos .next-button').hide();

			Trendy.Shopping.addAddressInv('self');
		}
	},
	collapseInv: function() {
		$('#invoiceAddressDiv').fadeOut();
		$('#formAddAddress-container').fadeOut();
		$('#formShoppingInfos .next-button').show();
	},
	removeCredits: function() {
		$.ajax({
			url:'/shopping/remove-credits/',
			type: 'POST',
			success:function(){
				Trendy.Lightbox.processing();
				$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{}, function(){
					//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
					$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .shopping-list").load('/shopping/summary-col1/', {}, function(){
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
					Trendy.Lightbox.close();
				});
			}
		});
	},
	useCredits: function() {
		$.ajax({
			url:'/shopping/use-credits/',
			type: 'POST',
			success:function(){
				Trendy.Lightbox.processing();
				$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{}, function(){
					//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
					$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .shopping-list").load('/shopping/summary-col1/', {}, function(){
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
					Trendy.Lightbox.close();
				});
			}
		});
	},
	checkPromotion: function(){		
		$('.waitingCode').show();		
		for (i=1; i<=4; i++)
			$('#strCodeError' + i).hide();
		$.ajax({
			type: 'POST',
			data: 'strCode=' + $('#inputStrCode').val(),
			url: '/shopping/checkpromotion',
			success: function(data){
				$('.waitingCode').hide();
				if (data == 1 || data == false)
					$('#strCodeError1').fadeIn('slow').delay(5000).fadeOut('slow');
				else if (data == 2)
					$('#strCodeError2').fadeIn('slow').delay(5000).fadeOut('slow');
				else if (data == 3)
					$('#strCodeError3').fadeIn('slow').delay(5000).fadeOut('slow');
				else if (data == 5)
					$('#strCodeError4').fadeIn('slow').delay(5000).fadeOut('slow');
				else if (data == 4){
					Trendy.Shopping.applyPromotion();
				}
			}
		});
	},
	applyPromotion: function() {
		$.ajax({
			url:'/shopping/apply-promotion/',
			data: 'strCode=' + $('#inputStrCode').val(),
			type: 'POST',
			success:function(){
				Trendy.Lightbox.processing();
				$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{}, function(){
					//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
					$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .shopping-list").load('/shopping/summary-col1/', {}, function(){
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
					Trendy.Lightbox.close();
				});
			}
		});
	},
	removePromotion: function() {
		$.ajax({
			url:'/shopping/remove-promotion/',
			type: 'POST',
			success:function(){
				Trendy.Lightbox.processing();
				$("#pageContent.shopping .summary-box").load('/shopping/summary-col3/',{}, function(){
					//$("#pageContent.shopping .summary-box .country").click(function(){Trendy.Shopping.listCountry();});
					$("#pageContent.shopping .summary-box .country").fancybox({onComplete:Trendy.Shopping.initializeFormCountry,titleShow:false});
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .shopping-list").load('/shopping/summary-col1/', {}, function(){
					Trendy.Common.pngFix();
				});
				$("#pageContent.shopping .accessories").load('/shopping/summary-accessories/',{}, function(){
					Trendy.Lightbox.close();
				});
			}
		});
	}
}

Trendy.fancybox={
    promotions:function(){
        $.fancybox({
            modal:false,
            width:350,
            height:'auto',
            autoDimensions:false,
            padding:0,
            href:'/shopping/promotions-credits/'
        });
        return false;
    }
}



$().ready(function() {
	Trendy.Shopping.initialize();

	$('#formEditAddress-container .buttonValidate').click(function(){                
		$('#formEditAddress-container #formEditAddress').submit();
	});

	$('#formAddAddress-container .buttonValidate').click(function(){ 
		$('#formAddAddress-container #formAddAddress').submit(); 
	});
});
