function controlForm(frm)
{
    //champs input file ( pour l'upload )
    jQuery("input[type=file]").each(function(){
       var ext = (/[.]/.exec(jQuery(this).val())) ? /[^.]+$/.exec(jQuery(this).val()) : undefined; 
       jQuery.ajax({
          type  : 'POST',
          url   : document.URL,
          data  : 'action=checkValidExtension&id_fcontact=' + jQuery("#id_fcontact").val() + '&name=' + jQuery(this).attr("name") + '&val=' + ext,
          success : function( html ){
              if ( html == 'false' )
              {
                  alert("L'extension (." + ext + ") du fichier n'est pas valide.");
                  jQuery(this).focus();
                  return false;
              }
          }
       }); 
    });
    
	var nb_champs = parseInt(document.getElementById("nb_champs_obligatoires").value);
	var ind = 1;
	var nom_champ,val_champ;
	var msg = document.getElementById("msg_vide").value;
	
	//destinataires
	if( (document.getElementById('destinataires') != undefined) )
	{
		$destinataires = document.getElementById('destinataires');
		
		dest_selected = false;
		
		for (var i = 0; i < $destinataires.options.length; i++)
		{
			if ($destinataires[i].selected)
			{
				dest_selected = true;
			}
		}
		
		if(dest_selected == false)
		{
			alert('Merci de selectionner un destinataire');
			document.getElementById('destinataires').focus();
			return false;
		}
	}
	
	//champs classiques
	for(ind=1;ind<=nb_champs;ind++)
	{
		nom_champ = document.getElementById("champ"+ind).value;
		val_champ = document.getElementById(nom_champ).value;
		
		if((val_champ)=="")
			{
			alert(msg);
			document.getElementById(nom_champ).focus();
			return false;
			}
	}
	
	//champs precision
	if( (document.getElementById('nb_champ_precision') != undefined) && (document.getElementById('indice_champ_precision') != undefined) )
	{
		var nb_champ_precision = document.getElementById('nb_champ_precision').value;
		var indice_precision = document.getElementById('indice_champ_precision').value;
		for(i=0;i<=nb_champ_precision;i++)
		{
			for(j=0;j<=indice_precision;j++)
			{
				if(document.getElementById("precisez_"+i+"_"+j) != undefined)
				{
					if(document.getElementById("precisez_"+i+"_"+j).style.display == 'table-row')
					{
						if(document.getElementById("id_precisez_"+i+"_"+j).value == "")
						{
							alert(msg);
							document.getElementById("id_precisez_"+i+"_"+j).focus();
							return false;
						}
					}
				}
			}
		}
	}
    
    
	
	return true;
}

var END_OF_INPUT = -1;

var base64Chars = new Array(
    'A','B','C','D','E','F','G','H',
    'I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X',
    'Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3',
    '4','5','6','7','8','9','+','/'
);

var reverseBase64Chars = new Array();
for (var i=0; i < base64Chars.length; i++){
    reverseBase64Chars[base64Chars[i]] = i;
}

var base64Str;
var base64Count;
function setBase64Str(str){
    base64Str = str;
    base64Count = 0;
}
function readBase64(){    
    if (!base64Str) return END_OF_INPUT;
    if (base64Count >= base64Str.length) return END_OF_INPUT;
    var c = base64Str.charCodeAt(base64Count) & 0xff;
    base64Count++;
    return c;
}
function encodeBase64(str){
    setBase64Str(str);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] >> 2 ]);
        if (inBuffer[1] != END_OF_INPUT){
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
            if (inBuffer[2] != END_OF_INPUT){
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                result += (base64Chars [inBuffer[2] & 0x3F]);
            } else {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76){
            result += ('\n');
            lineCount = 0;
        }
    }
    return result;
}
function readReverseBase64(){   
    if (!base64Str) return END_OF_INPUT;
    while (true){      
        if (base64Count >= base64Str.length) return END_OF_INPUT;
        var nextCharacter = base64Str.charAt(base64Count);
        base64Count++;
        if (reverseBase64Chars[nextCharacter]){
            return reverseBase64Chars[nextCharacter];
        }
        if (nextCharacter == 'A') return 0;
    }
    return END_OF_INPUT;
}

function ntos(n){
    n=n.toString(16);
    if (n.length == 1) n="0"+n;
    n="%"+n;
    return unescape(n);
}

function decodeBase64(str){
    setBase64Str(str);
    var result = "";
    var inBuffer = new Array(4);
    var done = false;
    while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
        && (inBuffer[1] = readReverseBase64()) != END_OF_INPUT){
        inBuffer[2] = readReverseBase64();
        inBuffer[3] = readReverseBase64();
        result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
        if (inBuffer[2] != END_OF_INPUT){
            result +=  ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
            if (inBuffer[3] != END_OF_INPUT){
                result +=  ntos((((inBuffer[2] << 6)  & 0xff) | inBuffer[3]));
            } else {
                done = true;
            }
        } else {
            done = true;
        }
    }
    return result;
}

function display_precision(id,debut,fin,champ,type,identifiant)
{
	if(type == 'checkbox')
	{
		for(ind=debut;ind<=fin;ind++)
		{
			if(document.getElementById(identifiant+ind) != undefined)
			{
				if(document.getElementById(identifiant+ind).checked)
				{
					if(document.getElementById("precisez_"+champ+"_"+ind) != undefined)
					{
						document.getElementById("precisez_"+champ+"_"+ind).style.display = "table-row";
					}
				}
				else
				{
					if(document.getElementById("precisez_"+champ+"_"+ind) != undefined)
					{
						document.getElementById("precisez_"+champ+"_"+ind).style.display = "none";
					}
				}
			}
		}
	}
	else if(type == 'select_multiple')
	{
		if(document.getElementById(identifiant) != undefined)
		{
			select_multiple = document.getElementById(identifiant);
			for (var i = 0; i < select_multiple.options.length; i++)
			{
				ind = select_multiple[i].value;
				if(document.getElementById("precisez_"+champ+"_"+ind) != undefined)
				{
					if (select_multiple[i].selected)
					{
						document.getElementById("precisez_"+champ+"_"+ind).style.display = "table-row";
					}
					else
					{
						document.getElementById("precisez_"+champ+"_"+ind).style.display = "none";
					}
				}
			}
		}
	}
	else
	{
		for(ind=debut;ind<=fin;ind++)
		{
			if(document.getElementById("precisez_"+champ+"_"+ind) != undefined)
			{
				if(ind != id)
				{
					document.getElementById("precisez_"+champ+"_"+ind).style.display = "none";
				}
				else
				{
					document.getElementById("precisez_"+champ+"_"+ind).style.display = "table-row";
				}
			}
		}
	}
}

