//////////////////////////////////////////////////////////////////
//////////////////////MODAL INFO CLIENT///////////////////////////
//////////////////////////////////////////////////////////////////

// => SET MODAL INFO CLIENT
	function setModalUserInfo(){ 
		if($('.userInfoEdit').length){
			if($.trim($('#userId').attr('value'))=="" || $('#adressFacKo').length){
				postForm=false; //utile à l'ecommerce
				$('#submit_order').parent().attr('id', 'btn_submit_account_disabled');
			}
			$('.userInfoEdit').click(function(){
				modalUserInfo($(this));
			});
		}
		
		if($('#errorEmail').length){
			$('#errorEmailDesc').fadeIn();
			$('#errorEmail').hover(function(){
				$('#errorEmailDesc').fadeIn();
			},function(){
				$('#errorEmailDesc').fadeOut();
			});
		
			$('#errorEmail').click(function(){
				if(!dialogOpen)$('#errorEmailForm').dialog({
					width: 600,
					resizable: false,
					disabled:true,
					draggable:false,
					buttons: {
						cancel: function() {
							$(this).dialog("destroy");
							backOpac(false);
						},
						save: function() {
							$(this).dialog("destroy");
							$.ajax({
								type: "POST",
								url: domain+"/do/modal/gdm_modal_save_user.php",
								data: $('#errorEmailForm').serialize(),
								success:function(){
									registerOk();
								}
							});
						}
					},
					open: function(event, ui) {
						dialogOpen=true;
						backOpac(true);
						$(this).css('height', '100%');
						$(this).dialog('option', 'position', 'center');
					},
					close: function(event, ui) { 
						dialogOpen=false;
						backOpac(false);
						$(this).dialog("destroy");
					}
				});
				endLoading();
			});
		}
	}
	
// => MODAL MODIFICATION INFO CLIENT

	function modalUserInfo(elm){
		loading();	
		$.ajax({
			type: "POST",
			url: domain+"/do/modal/gdm_modal_buildForm_user.php",
			data: "language="+$(elm).attr('lang')+"&type="+$(elm).attr('rel'),
			success: function(modal){
				$('#modal').html(modal);
				log(dialogOpen);
				if($('#modalForm').length==0 || dialogOpen){
					if(!dialogOpen)errorModal();
				}else{
					$('#modalForm').dialog({
						width: 600,
						resizable: false,
						disabled:true,
						draggable:false,
						buttons: {
							cancel: function() {
								closeModal($(this));
								backOpac(false);
							},
							save: function() {
								var pass = require($('#modalFormPost'));
								if(pass==true){
									//PASSWORD
									
									if($('#currentPassword').length>0 || $('#newLogin').length>0){
										if($('#currentPassword').length>0)data = 'type=verifPass&currentPassword='+$('#currentPassword').attr('value');
										if($('#newLogin').length>0) data+='&verifNewLogin='+$('#newLogin').attr('value');
										$.ajax({
											type: "POST",
											url: domain+"/do/modal/gdm_modal_save_user.php",
											data: data,
											success:function(response){
												resp = response.split('SEP/');
												log(resp.length);
												if(resp[0]=="true" && (resp.length==1 || resp[1]=="true")){
													saveInfoClient($(elm), $('#modalFormPost').serialize());
													closeModal($('#modalForm'));
													backOpac(false);
												}else{
													if(resp[0]!="true")errorForm($('#currentPassword'));
													if(resp[1]!="true"){
														errorForm($('#newLogin'));
														if($('#newLogin').parent(".centerModal").find('.editError').length==0){
															$('#newLogin').parent(".centerModal").prepend(resp[1]);
														}else{
															$('#newLogin').parent(".centerModal").find('.editError').parent('.lineModal').remove();
															$('#newLogin').parent(".centerModal").prepend(resp[1]);
														}
													}
												}
											}
										});
									//OTHER	
									}else{
										if($('#submit_order:hidden').length){
											$('#submit_order').show();
											hoverSubmit();
										}
										saveInfoClient($(elm), $('#modalFormPost').serialize());
										closeModal($('#modalForm'));
										backOpac(false);
									}
								}
							}
						},
						open: function(event, ui) {
							backOpac(true);
							$(this).css('height', '100%');
							$(this).dialog('option', 'position', 'center');
							
							
						},
						close: function(event, ui) { 
							backOpac(false);
							closeModal($(this));
						}
		
					});
					addListFunction();
					endLoading($(elm).attr('class'));
				}
			}
		});
	}
	
// => ENREGISTREMENT DES INFO CLIENT
	function saveInfoClient(elm,data){
		$.ajax({
			type: "POST",
			url: domain+"/do/modal/gdm_modal_save_user.php",
			data: data,
			success: function(response){
				$(elm).parent().prev('.response').html(response);
				if($.trim($(elm).attr('rev'))!=""){
					var tmp= $(elm).attr('rev');
					$(elm).attr('rev', $(elm).html());
					$(elm).html(tmp);
					postForm=true; //utile à l'ecommerce
					$('#submit_order').parent().attr('id', '');
				}
			}
		});
	}
	
// => PRE REMPLI LES CHAMPS GRACE AU NUM DE TVA
	function tvaNum(num){
		if (num.substr(0,2).toLowerCase()=='be' && num.length==12){
			$.ajax({
				type: "GET",
				url: domain+"/do/other/gdm_tva_num.php",
				data: "num="+num.substring(2,num.length),
				success: function(xml){
					if($.trim($(xml).find( 'company' ).text())!=""){
						$('#minLoadInput').css('background-image', 'url(../files/design/checkMinLoad.jpg)');
						$('#company').attr('value',$(xml).find('company').text());
						$('#address').attr('value',$(xml).find('address').text());
						$('#code').attr('value',$(xml).find('postcode').text());
						$('#ville').attr('value',$(xml).find('city').text());
					}else if($('#minLoadInput').is(':hidden')){
						$('#minLoadInput').show();
						$('#minLoadInput').css('background-image', 'url(../files/design/minLoad.gif)');
					}
						
				}
			});
		}else{
			$('#minLoadInput').show();
			$('#minLoadInput').css('background-image', 'url(../files/design/minLoad.gif)');
		}
	}
	
// => GERE AJOUT OU LA SUPRRESION D'ADRESSE IP
	function addListFunction(){
		checkDelList();
		$('#addIp').click(function(){
			log($(this).parent().prev('.lineModal'));
			$(this).parent().before("<div class='lineModal'>"+$(this).parent().prev('.lineModal').html()+"</div>");
			$(this).parent().prev('.lineModal').find('input').attr('value',"");
			checkDelList();
			$('.delIp').click(function(){
				$(this).parent().remove();
				checkDelList();
			});
		});
		$('.delIp').click(function(){
			$(this).parent().remove();
			checkDelList();
		});
	}
	function checkDelList(){
		//affiche un seul label
		if($('#addIp').length){
			$('#modalFormPost').find('label').css('color','#fff');
			$('#modalFormPost').find('label:first').css('color', "#626262");
			//affiche tous les supprimer sauf si il ne reste qu'un seul champ
			$('.delIp').show();
			if($('.delIp').length==1)$('.delIp:first').hide();
		}
	}

