$(function() {
	if($('#trouver_vins').length > 0)
	{
		champObl = [1, 4, 10];
		
		$("#champ1").bind('change', function(e){
      		
			if ($(this).val() != 'Particulier' && $(this).val() != '') {
				if(jQuery.inArray(5, champObl) == -1)
					champObl.push(2, 5, 6, 7, 8);
			}
			else{
				champObl = [1, 4, 10];
				
				if($('#choix2').attr('checked') == true)
					champObl.push(5, 6, 7, 8);
				else if($('#choix3').attr('checked') == true)
					champObl.push(11);
				else if($('#choix1').attr('checked') == true)
					champObl.push(9);
			}
			
			for(i=1; i<$("input[name^='champ']").length; i++)
			{
				if ($("label[for='champ" + i + "']").length > 0) {
					if (jQuery.inArray(i, champObl) != -1) {
						if ($("label[for='champ" + i + "']").html().search(/(important)/g) == -1) {
							$("label[for='champ" + i + "']").html($("label[for='champ" + i + "']").html().replace(':', '<span class="important">*</span> :'))
						}
					}
					else {
						if ($("label[for='champ" + i + "']").html().search(/(important)/g) > -1) {
							$("label[for='champ" + i + "']").html($("label[for='champ" + i + "']").html().replace('<span class="important">*</span>', ''))
						}
					}
				}
			}
			
			$('#champ_obl').val(champObl.toString());
		});
		
		$("input[id^='choix']").bind('click', function(e){
			
			switch($(this).val())
			{
				case 'Fax':
					champs = [11];
					champObl.push(11);
					
					jQuery.each(champs, function() {
					
						if ($("label[for='champ" + this + "']").length > 0) {
							
							if ($("label[for='champ" + this + "']").html().search(/(important)/g) == -1) {
								$("label[for='champ" + this + "']").html($("label[for='champ" + this + "']").html().replace(':', '<span class="important">*</span> :'))
							}
						}
					});
					
					if ($("#champ1").val() == 'Particulier' || $("#champ1").val() == '') {
						champs = [5, 6, 7, 8];
						
						jQuery.each(champs, function(){						
							
							if ($("label[for='champ" + this + "']").length > 0) {
								if ($("label[for='champ" + this + "']").html().search(/(important)/g) > -1)
									$("label[for='champ" + this + "']").html($("label[for='champ" + this + "']").html().replace('<span class="important">*</span>', ''))
							}
							
							if(jQuery.inArray(parseInt(this), champObl) > -1)
							{
								champObl = unset(champObl, jQuery.inArray(parseInt(this), champObl));
							}
						});
					}
					
					if ($("label[for='champ9']").length > 0) {
						if ($("label[for='champ9']").html().search(/(important)/g) > -1)
							$("label[for='champ9']").html($("label[for='champ9']").html().replace('<span class="important">*</span>', ''))
					}
							
					if(jQuery.inArray(9, champObl) > -1)
						champObl = unset(champObl, jQuery.inArray(9, champObl));
					
					break;
				
				case 'E-mail':
					
					champs = [9];
					champObl.push(9);
					
					jQuery.each(champs, function() {
					
						if ($("label[for='champ" + this + "']").length > 0) {
							
							if ($("label[for='champ" + this + "']").html().search(/(important)/g) == -1) {
								$("label[for='champ" + this + "']").html($("label[for='champ" + this + "']").html().replace(':', '<span class="important">*</span> :'))
							}
						}
					});
					
					if ($("label[for='champ11']").html().search(/(important)/g) > -1) {
						if(jQuery.inArray(11, champObl) > -1)
							champObl = unset(champObl, jQuery.inArray(11, champObl));
						
						$("label[for='champ11']").html($("label[for='champ11']").html().replace('<span class="important">*</span>', ''))
					}
										
					if ($("#champ1").val() == 'Particulier' || $("#champ1").val() == '') {
						champs = [5, 6, 7, 8];
						
						jQuery.each(champs, function(){						
							
							if ($("label[for='champ" + this + "']").length > 0) {
								if ($("label[for='champ" + this + "']").html().search(/(important)/g) > -1)
									$("label[for='champ" + this + "']").html($("label[for='champ" + this + "']").html().replace('<span class="important">*</span>', ''))
							}
							
							if(jQuery.inArray(parseInt(this), champObl) > -1)
							{
								champObl = unset(champObl, jQuery.inArray(parseInt(this), champObl));
							}
						});
					}
					
					break;
				
				case 'Courrier':
					champs = [5, 6, 7, 8];
					champObl.push(5, 6, 7, 8);
					
					if ($("label[for='champ11']").html().search(/(important)/g) > -1) {
						if(jQuery.inArray(11, champObl) > -1)
							champObl = unset(champObl, jQuery.inArray(11, champObl));
						
						$("label[for='champ11']").html($("label[for='champ11']").html().replace('<span class="important">*</span>', ''))
					}
					
					if ($("label[for='champ9']").html().search(/(important)/g) > -1) {
						if(jQuery.inArray(9, champObl) > -1)
							champObl = unset(champObl, jQuery.inArray(9, champObl));
						
						$("label[for='champ9']").html($("label[for='champ9']").html().replace('<span class="important">*</span>', ''))
					}
					
					jQuery.each(champs, function() {
					
						if ($("label[for='champ" + this + "']").length > 0) {
							
							if ($("label[for='champ" + this + "']").html().search(/(important)/g) == -1) {
								$("label[for='champ" + this + "']").html($("label[for='champ" + this + "']").html().replace(':', '<span class="important">*</span> :'))
							}
						}
					});
					
					break;
			}
			
			$('#champ_obl').val(champObl.toString());
		});
	}
});

function unset(array, index){
  var output=[];
  var j=0;
  
  delete array[index];
  for(var i in array){
    if (i!=index){
      output[j]=array[i];
      j++;
    }
  }
  return output;
}
