function $$( tag_id )
{
	return document.getElementById(tag_id);
}

function get_select_value( link,select_id )
{
	if( link.href.indexOf('?') <=0 ){
		link.href = link.href + '?' + select_id + '=' + $$( select_id ).value;
	}else{
		link.href = link.href + '&' + select_id + '=' + $$( select_id ).value;
	}
}


function ajax_filter_result(theme, sub_theme){
    

    $.ajax({
     type: "POST",
     url: "ajaxCallback.php?action=filter",
     data:   "theme="+escape(theme)+"&sub_theme="+escape(sub_theme),
     success: function( html ){ $('#tr_category').html( html ) }
    });
}

function ajax_modify_theme_pre( theme, select_text, select_value ){

    if(theme != '' && theme != null && select_text != '' && select_text != null && select_value != '' && select_value != null){
        
    $.ajax({
     type: "POST",
     url: "ajaxCallback.php?action=check",
     data:   "theme="+escape(theme)+"&CD_TAXO="+escape(select_value),
     success: function( html ){
         
            if(check_exites( html )==1){
                $('#cd_taxo').attr('readonly', 'readonly');
                $('#lb_esp').val(select_text);
                $('#cd_taxo').val(select_value);
                $('#action').val('updata');
                
            }else{
                alert("Valeur n'est pas valide");
            }
         
     }
    });
    
    }else{
        alert("Valeur n'est pas valide");
    }
}

function ajax_insert_theme_pre(theme){
    $('#cd_taxo').val('');
    $('#cd_taxo_msg').html('');
    $('#lb_esp').val('');
    $('#lb_esp_msg').html('');
    $('#success').html('');
    $('#action').val('insert');
    $('#cd_taxo').removeAttr('readonly');
}

function ajax_submit(theme, action, lb_esp, cd_taxo, check){
    var flag = true;
    
    if(action == 'updata' || action == 'insert'){
		if(action != check){
			return false;
		}
		/*
        if(lb_esp == ''){
            flag = false;
            $('#lb_esp_msg').html( 'Veuillez entrer un nom et un code pour le thème' ); 
        }else{
			$('#lb_esp_msg').html( '' );
		}
		*/
        if(cd_taxo == '' || lb_esp == ''){
            flag = false;
            $('#cd_taxo_msg').html( 'Veuillez entrer un nom et un code pour le thème' ); 
        }else{
			$('#cd_taxo_msg').html( '' ); 
		}
        
            
        if(flag == true){
		
            $.ajax({
                type: "POST",
                url: "ajaxCallback.php?action="+action,
                data:   "theme="+escape(theme)+"&CD_TAXO="+escape(cd_taxo)+"&LB_ESP="+escape(lb_esp),
                success: function( html ){
                   
                    if(check_exites( html )== 1 ){
                        $('#success').removeClass('red');
                        $('#success').addClass('green');
                        

                        $('#success').html( 'Op&eacute;ration r&eacute;ussie' ); 
                        if(action == 'insert'){
                            $('#lb_esp').val('');
                            $('#cd_taxo').val('');
                        }
                        window.location.reload();
                        //$('#test').html( html );
                    }
                    
                    if(check_exites( html )== 0 && action == 'insert'){
                        $('#success').removeClass('green');
                        $('#success').addClass( 'red');
                        $('#success').html( 'erreur de saisie, de dupliquer le code non autoris&eacute;');
                    }
                }
            });
        }
    }else{
        alert("Valeur n'est pas valide");
    }
}
// end for search_admin_theme

// for search accueil
function ajax_get_search_filter( filterVal, selected,type ){
$('#ajax_loader_img').html('<img src="dist/images/ajax-loader.gif"/>');

if( type == '1' ){
	var str_com = "&depcom=1";
	var com = '';
	if( document.getElementById("theme_selected_com") && document.getElementById("sub_select_filter_dep").style.display!='none' ){
		var com = '';
		var sel_dest = document.getElementById("theme_selected_com");
		var sel_dest_len = sel_dest.length;
	    for( var f=0;f<sel_dest_len; f++ ){
	    	com += sel_dest.options[f].value + "+";
	    }
	    if( sel_dest_len > 0 )
	    	com = "&com="+com;
	    else
	    	com = '';
	}else{
		com = '';
	}
}else{
	var str_com = "";
	var com = '';
}

    action = 'search_accueil';
    $.ajax({
	type: "POST",
	url: "ajaxCallback.php?action="+action,
	data:  "filterVal="+escape(filterVal)+str_com+com,
	success: function( html ){
	    
// for from result page back to search page 
if(selected != null){
  categories = selected.split(","); 
  for (var i=0; i<categories.length; i++){

   html = html.replace( '"'+categories[i]+'"' , '"'+categories[i]+ '" selected="true"');
  }
}
////////////////////////

		if( type == '1' ){
			if(filterVal != ''){
				$('#sub_select_dep').html( html );
				$('#sub_select_filter_dep').removeAttr( "style" );
				$('#label_dep').html( "Commune :" );
			}else{
				$('#label_dep').html( '' );
				$('#sub_select_filter_dep').attr( "style", "display:none;" );
			}
			
		}else{
		    $('#sub_select').html( html );
			if(filterVal != ''){
				var label = filterVal + ' : ';
				if(filterVal == 'Commune'){
					//label = filterVal  + ' : ' + '<br/>Pour sélectionner plusieurs communes, maintenez la touche "Ctrl" enfoncé';
					label = "Département"  + ' : ';
				}
				if(filterVal == 'Department')
					label = 'Départment : ';
				if(filterVal == 'Intercommunalite')
					label = 'Intercommunalité : ';
	
				$('#sub_select_filter').removeAttr( "style" );
				$('#label').html( label );
	
				if(filterVal == 'Commune'){
					$('#sub_select').css( "height","42px" );
				}else{
					$('#sub_select').css( "height","32px" );
				}
				
			}else{
				
				$('#label').html( '' );
				$('#sub_select_filter').attr( "style", "display:none;" );
			}
			$('#sub_select_filter_dep').attr( "style", "display:none;" );
		}
		
		
$('#ajax_loader_img').html('');

	}
});
}

function get_theme_val(){
	if( $('#sub_theme').val() )
		return $('#sub_theme').val();
	else
		return $('#theme').val();
}


function ajax_get_select_theme(filterVal, selected, father){
    
	$('#ajax_loader_img').html('<img src="dist/images/ajax-loader.gif"/>');
	
	if(filterVal == ''){
		//if no theme selected from theme list ;

		$('.theme_list_box').attr( "style", "display:none;" );
        $('#ajax_loader_img').html('');
        
        if( father != 'Faune' ){
        	$('.drop_down_list_1').attr( "style", "display:none;" );
		}
        
	}else {
		if(filterVal == 'Faune' || filterVal == 'Faune Natura2000'){
			$('.theme_list_box').attr( "style", "display:none;" );
			$('.drop_down_list_1').removeAttr( "style" );		// show it
			
			$('#tr_category').html( '' );
	        action = 'dropdown_filter';
	
			$.ajax({									//fill the theme list on the left
				type:"POST",
				url:"ajaxCallback.php?action="+action,
				data:"theme="+escape(filterVal),
				success: function( html ){
					// for from result page back to search page 
					
					if(selected != null){
					  categories = selected.split(","); 
					  for (var i=0; i<categories.length; i++){
					   html = html.replace( '"'+categories[i]+'"' , '"'+categories[i]+ '" selected="true"');
					  }
					}
					
					//setTimeout('', 100);
					$('#td_category').html( html );
	                $('#ajax_loader_img').html('');
				}
			});
		}else{

			$('.theme_list_box').removeAttr( "style" );
			$('.drop_down_list_filter').removeAttr( "style" );
			
			action = 'filter';
			$.ajax({
				type:"POST",
				url:"ajaxCallback.php?action="+action,
				data:"theme="+escape(filterVal),
				success: function( html ){
			    
					// for from result page back to search page . namely, make all the items selected.
					/*
					if(selected != null){
					  categories = selected.split(","); 
					  for (var i=0; i<categories.length; i++){
					      //alert(categories[i]);
					   html = html.replace( '"'+categories[i]+'"' , '"'+categories[i]+ '" selected="true"');
					  }
					}
					*/
			
					if(father != 'Faune' && father != 'Faune Natura2000'){			    
					    //$('#td_category').html( '' );
					    //$('.theme_list_box').attr( "style", "display:none;" );
						$('.drop_down_list_1').attr( "style", "display:none;" );
					}
			
					$('#tr_category').html( html );
		            $('#ajax_loader_img').html('');
				}
			});
		}
	}
}

function ajax_admin_filter_result(theme, sub_theme){

    $.ajax({
     type: "POST",
     url: "ajaxCallback.php?action=admin_filter",
     data:   "theme="+escape(theme)+"&sub_theme="+escape(sub_theme),
     success: function( html ){ $('#tr_category').html( html ) }
    });

}



function ajax_get_sub_theme( val ){
	action = 'sub_theme';
	if(val != ''){
	$.ajax({
		type: "POST",
		url: "ajaxCallback.php?action="+action,
		data:  "val="+escape(val),
		success: function( html ){
			//alert((html));
	}
});
	}
}

// end for  search accueil

function ajax_call(val,action){// for test only
alert(escape(val))
$.ajax({
	type: "POST",
	url: "ajaxCallback.php?action="+action,
	data:  "val="+escape(val),
	success: function( html ){
		//alert((html));
	}
});
}


function check_lock(){
     if($('#action').val() != 'updata'){
        $('#cd_taxo').removeAttr('readonly');
     }
}

function check_exites( obj ){
    
    reg = /.*true.*/;
    if(reg.test(obj)){    
        return 1;   
    }
    
    reg = /.*false.*/;
    if(reg.test(obj)){
        return 0;
    }
    
}
function selectOptions_com(sel_source, sel_dest ) {
	var id_target_list = sel_dest;
	
    var sel_source = document.getElementById(sel_source);
    var sel_dest = document.getElementById(sel_dest);
   
    var sel_source_len = sel_source.length;
    var sel_dest_len = sel_dest.length;
    
    // load all the options of the seleted into the array "date"
    var com = '';
    for( var f=0;f<sel_dest_len; f++ ){
    	com += sel_dest.options[f].value + "+";
    }

    // add all the selected options into the right
    	
    var dep = '';
    for (var j=0; j<sel_source_len; j++)
    {
        if(sel_source.options[j].selected == true){
        	if( !isExistOption(sel_source.options[j].value, id_target_list) ){
        		dep += sel_source.options[j].value + "+";
        	}
        }
    }
    
    if( dep != '' ){
	    $.ajax({
	    type: "POST",
	    url: "search_get_dep_com_ajax.php3",
	    data:   "dep="+dep+"&com="+com,
	    success: function( text ){
	    	$("#dep_com_selected").empty();  
	        $("#dep_com_selected").html( text );
	    }
		});
    }

}



// this function is copied from move_options which was used at back office
function selectOptions(sel_source, sel_dest ) {
	var num = 0;
	var date = new Array()
	
	var id_target_list = sel_dest;
	
    var sel_source = document.getElementById(sel_source);
    var sel_dest = document.getElementById(sel_dest);
   
    var sel_source_len = sel_source.length;
    var sel_dest_len = sel_dest.length;
    
    // load all the options of the seleted into the array "date"
    for( var f=0;f<sel_dest_len; f++ ){
    	date[num] = new Array()
    	date[num]['value'] = sel_dest.options[f].value;
    	date[num]['text'] = sel_dest.options[f].text;
    	num++;
    }
    
     
    
    
    // add all the selected options into the right
    for (var j=0; j<sel_source_len; j++)
    {
    	
        if(sel_source.options[j].selected == true){
        	
        	if( !isExistOption(sel_source.options[j].value, id_target_list) ){
        		//alert(sel_source.options[j].value + ' is already selected!');
        	
	        	date[num] = new Array()
	            date[num]['value'] = sel_source.options[j].value;
		    	date[num]['text'] = sel_source.options[j].text;
		    	num++;
        	}
        }
    }
    
    
    
    
    // this block will sort the selected array date
    for( var tmp=0; tmp<num-1; tmp++ ){
    	for( var tmp_02=tmp+1; tmp_02<num; tmp_02++ ){
    		if( date[tmp]['text'] > date[tmp_02]['text']  ){
    			temp = date[tmp]['text'];
    			date[tmp]['text'] = date[tmp_02]['text'];
    			date[tmp_02]['text'] = temp;
    			
    			temp_02 = date[tmp]['value'];
    			date[tmp]['value'] = date[tmp_02]['value'];
    			date[tmp_02]['value'] = temp_02;
    		}
    	}
    }
    
    
     
    // clear thr slected options from the list box
    for(var k=0; k<sel_dest_len; k++)
    {
        if( sel_dest.options[k] ){
            sel_dest.remove(k);
            k = -1;
        }
    }
    
    //add all the options back into the right list box
    for( var f=0;f<num; f++ ){
    	try {
    		sel_dest.add(new Option(date[f]['text'],date[f]['value']), null);
        } catch(e) {
            sel_dest.add(new Option(date[f]['text'],date[f]['value']));
        }
    }
    

    //Remove the selected options from the source list box
    /*
    for(var k=0; k<sel_source_len; k++)
    {
        if( sel_source.options[k] && sel_source.options[k].selected == true){
            sel_source.remove(k);
            k = -1;
        }
    }
    */

}
function deSelectOptions(sel_source, sel_dest){
	//Remove the selected options from the source list box
	var sel_source = document.getElementById(sel_source);
    var sel_dest = document.getElementById(sel_dest);
   
    var sel_source_len = sel_source.length;
    var sel_dest_len = sel_dest.length;
    
    for(var k=0; k<sel_dest_len; k++)
    {
        if( sel_dest.options[k] && sel_dest.options[k].selected == true){
            sel_dest.remove(k);
            k = -1;
        }
    }
}
function move_options(sel_source, sel_dest ) {
	var num = 0;
	var date = new Array()
	
    var sel_source = document.getElementById(sel_source);
    var sel_dest = document.getElementById(sel_dest);
   
    var sel_source_len = sel_source.length;
    var sel_dest_len = sel_dest.length;
    
    
    for( var f=0;f<sel_dest_len; f++ ){
    	date[num] = new Array()
    	date[num]['value'] = sel_dest.options[f].value;
    	date[num]['text'] = sel_dest.options[f].text;
    	num++;
    }
    
    for(var k=0; k<sel_dest_len; k++)
    {
        if( sel_dest.options[k] ){
            sel_dest.remove(k);
            k = -1;
        }
    }
    
    
    
    for (var j=0; j<sel_source_len; j++)
    {
        if(sel_source.options[j].selected == true){
        	date[num] = new Array()
            date[num]['value'] = sel_source.options[j].value;
	    	date[num]['text'] = sel_source.options[j].text;
	    	num++;
        }
    }
    
    for( var tmp=0;tmp<num-1;tmp++ ){
    	for( var tmp_02=tmp+1;tmp_02<num;tmp_02++ ){
    		if( date[tmp]['text'] > date[tmp_02]['text']  ){
    			temp = date[tmp]['text'];
    			date[tmp]['text'] = date[tmp_02]['text'];
    			date[tmp_02]['text'] = temp;
    			
    			temp_02 = date[tmp]['value'];
    			date[tmp]['value'] = date[tmp_02]['value'];
    			date[tmp_02]['value'] = temp_02;
    		}
    	}
    }
    
    
    
    //add
    for( var f=0;f<num; f++ ){
    	try {
    		sel_dest.add(new Option(date[f]['text'],date[f]['value']), null);
        } catch(e) {
            sel_dest.add(new Option(date[f]['text'],date[f]['value']));
        }
    }
    

    //delete
    for(var k=0; k<sel_source_len; k++)
    {
        if( sel_source.options[k] && sel_source.options[k].selected == true){
            sel_source.remove(k);
            k = -1;
        }
    }

}

// add by sam for search_admin_zone page
function doc_type_check( id,type,num_01,num_02 ){
	var tmp = document.getElementById( id ).value;
	if( tmp != '' ){
		tmp = tmp.slice( -3 );
		tmp = tmp.toLowerCase();
		if( type == tmp ){
			select_options('themes',num_01);
			select_all_options('commune_selected');
			select_options('property',num_02);
			form_submit( 'zone_editor' );
			return true;
		}else{
			alert( "Veuillez charger un fichier de type "+type );
			return false;
		}
	}
}

function select_submit( num_01,num_02 ){
	select_options('themes',num_01);
	select_all_options('commune_selected');
	select_options('property',num_02);		
}

function frm_check( num_01,num_02,type ){
	array = new Array( "localisation","date_inscr","carte_global","carte_pdf","copil","arrete_appro_DOCOB" );
	array_02 = new Array( "Localisation","Décision d'inscription du site d'importance communautaire","Arrêté de désignation : Carte globale ou assemblage ","Arrêté de désignation : Cartes PDF élémentaires","Arrêté de désignation du COPIL","Arrêté d'approbation du DOCOB " );
	for( var i=0;i<array.length;i++ ){
		var str = document.getElementById( array[i] ).value;
		if( str != "" ){
			str = str.slice( -3 );
			str = str.toLowerCase();
			
			if( array[i] == "localisation" ){
				if( str == "gif" || str == "jpg" ){
				}else{
					alert( "Veuillez charger un fichier .jpg ou gif pour le champ « Localisation »" );
					return false;
				}
			}
			
			if( array[i] != "localisation" ){
				if( str != "pdf" ){
					alert( "Veuillez charger un fichier .pdf pour le champ « "+ array_02[i] +" »" );
					return false;
				}
			}
		}
	}
	
	if( type == 1 ){
		select_options('themes',num_01);
		select_all_options('commune_selected');
		select_options('property',num_02);
		form_submit( 'zone_editor' );
	}
	return true;
}

function form_submit( id ){
	var frm = document.getElementById( id );
	frm.submit();
}


function upload_ajax( url,name ){
	var id = document.getElementById("doc_list");
	
	$.ajax({
    type: "POST",
    url: "search_admin_zone_upload_ajax.php3",
    data:   "url="+url+"&name="+name,
    success: function( text ){
       $("#doc_list").empty();  
       $("#doc_list").html( text );
    }
	});
}

function upload_ajax_dobob( url,id_zone,id,type ){
	$.ajax({
    type: "POST",
    url: "search_admin_zone_upload_ajax.php3",
    data:   "url="+escape(url)+"&id_zone="+id_zone+"&div_id="+id+"&type="+type,
    success: function( text ){
       
       if( type == 1 ){
	       $( "#doc_list_docob" ).empty();  
	       $( "#doc_list_docob" ).html( text );
       }else{
       	   $( "#doc_list_cup" ).empty();  
	       $( "#doc_list_cup" ).html( text );
       }
    }
	});
}

function property_key_ajax( div_id,id_table,name,id_list,type){
	if( type == 1 ){
		var tmp = document.getElementById("property_key"+div_id).value;
			if( tmp != "" ){
			$.ajax({
		    type: "POST",
		    url: "search_admin_zone_ajax.php3",
		    data:   "value="+escape(tmp)+"&div_id="+div_id+"&id_table="+id_table+"&name="+escape(name)+"&type=1&id_list="+id_list,
		    success: function( text ){
		       
		       $( "#id_property"+div_id ).empty();  
		       $( "#id_property"+div_id ).html( text );
		      
		    }
			});
		}
	}else{
		var tmp = document.getElementById("theme_key"+div_id).value;
			if( tmp != "" ){
			$.ajax({
		    type: "POST",
		    url: "search_admin_zone_ajax.php3",
		    data:   "value="+escape(tmp)+"&div_id="+div_id+"&id_table="+id_table+"&name="+escape(name)+"&type=2&id_list="+id_list,
		    success: function( text ){
		       
		       $( "#id_themes"+div_id ).empty();  
		       $( "#id_themes"+div_id ).html( text );
		      
		    }
			});
		}
	}
}

function communes_key_ajax( id_list ){
	var tmp = document.getElementById("communes_key").value;
	if( tmp != "" ){
		$.ajax({
	    type: "POST",
	    url: "search_admin_zone_communes_ajax.php3",
	    data:   "value="+escape(tmp)+"&id_list="+id_list,
	    success: function( text ){
	       $( "#communes_id" ).empty();  
	       $( "#communes_id" ).html( text );
	      
	    }
		});
	}
}

function select_options(id_element,num){
	for( var k=0;k<num;k++ ){
		list = document.getElementById(id_element+k+"_selected");
		if( list.length > 0 ){
			for (var i=0; i<list.length; i++){
				list.options[i].selected = true;
			}
		}
	}
}

function drop_list( id ){
	var id_list = document.getElementById( "list_display"+id );
	if( id_list.style.display == "block" ){
		id_list.style.display = "none";
		$( "#name_bar_a"+id ).removeClass( "expand" );
		$( "#name_bar_a"+id ).addClass( "close" );
		$( "#name_bar"+id ).addClass("divider");
	}else{
		id_list.style.display = "block";
		$( "#name_bar_a"+id ).removeClass( "close" );
		$( "#name_bar_a"+id ).addClass( "expand" );
		$( "#name_bar"+id ).removeClass("divider");
	}
}

function file_upload( id ){
	document.getElementById( id ).click();
}

function zone_exist_check( table, key, key_title ,num_01,num_02,id ){
	key_value = document.getElementById( key ).value;
	//if( key_value != "" ){
	if( input_filed_check( id ) ){
		$.ajax({
	    type: "POST",
	    url: "search_admin_zone_exist_check_ajax.php3",
	    data:   "key_value="+escape(key_value)+"&table="+table+"&key="+key,
	    success: function( text ){
	      if(text.indexOf("exist_by_check") != "-1"){
	      	alert( "Le champ " + key_title + " existe déjà, veuillez le modifier" );
	      	return false;
	      }else{
	      	select_submit( num_01,num_02 )
	      	form_submit( "zone_editor" );
	      }
	    }
		});
	}
}

function input_filed_check( id ){
	zoneage = new Array();
	zoneage_filed = new Array();
	
	zoneage[id] = new Array();
	zoneage_filed[id] = new Array();
	
	if( id == 1 || id == 2 ){
		zoneage[id][1] = "SITE";
		zoneage[id][2] = "CODESITE";
		zoneage[id][3] = "DATEPROCED";
		zoneage_filed[id]['SITE'] = "Nom";
		zoneage_filed[id]['CODESITE'] = "Code de la zone";
		zoneage_filed[id]['DATEPROCED'] = "Date de la procédure";
	}else if( id == 4 ){
		zoneage[id][1] = "Nom";
		zoneage[id][2] = "ID_SPN";
		zoneage[id][3] = "Date_creation";
		zoneage_filed[id]['Nom'] = "Nom";
		zoneage_filed[id]['ID_SPN'] = "Code de la zone";
		zoneage_filed[id]['Date_creation'] = "Date de création";
	}else if( id == 6 ){
		zoneage[id][1] = "Nom_reserve";
		zoneage[id][2] = "CD_ZONE";
		zoneage_filed[id]['Nom_reserve'] = "Nom";
		zoneage_filed[id]['CD_ZONE'] = "Code";
	}else if( id == 3 ){
		zoneage[id][1] = "NOM";
		zoneage[id][2] = "ID_SPN";
		zoneage[id][3] = "Date_de_protection";
		zoneage_filed[id]['NOM'] = "Nom";
		zoneage_filed[id]['ID_SPN'] = "Code";
		zoneage_filed[id]['Date_de_protection'] = "Date de protection";
	}else if( id == 5 ){
		zoneage[id][1] = "NOM";
		zoneage[id][2] = "ID_SPN";
		zoneage_filed[id]['NOM'] = "Nom";
		zoneage_filed[id]['ID_SPN'] = "Code";
	}else if( id == 8 ){
		zoneage[id][1] = "LB_ZN";
		zoneage[id][2] = "NM_SFFZN";
		zoneage[id][3] = "TY_ZONE";
		zoneage[id][4] = "AN_MAJ";
		zoneage[id][5] = "LB_REDAC";
		zoneage_filed[id]['LB_ZN'] = "Nom de la zone";
		zoneage_filed[id]['NM_REGZN'] = "Code de la zone";
		zoneage_filed[id]['TY_ZONE'] = "Type de zone";
		zoneage_filed[id]['AN_MAJ'] = "Année de mise à jour";
		zoneage_filed[id]['LB_REDAC'] = "Rédacteur";
	}else if( id == 9 ){
		zoneage[id][1] = "Zone";
		zoneage[id][2] = "Code_zone";
		zoneage_filed[id]['Zone'] = "Nom de la zone";
		zoneage_filed[id]['Code_zone'] = "Code";
	}else if( id == 10 ){
		zoneage[id][1] = "Nom_du_site";
		zoneage[id][2] = "Code";
		zoneage_filed[id]['Nom_du_site'] = "Nom du site";
		zoneage_filed[id]['Code'] = "Code de la zone";
	}else if( id == 11 ){
		zoneage[id][1] = "Zone";
		zoneage[id][2] = "Code_zone";
		zoneage_filed[id]['Zone'] = "Nom de la zone";
		zoneage_filed[id]['Code_zone'] = "Code zone";
	}else if( id == 12 ){
		zoneage[id][1] = "Nom_de_la_zone";
		zoneage[id][2] = "Code";
		zoneage_filed[id]['Nom_de_la_zone'] = "Nom de la zone";
		zoneage_filed[id]['Code'] = "Code";
	}else if( id == 7 ){
		zoneage[id][1] = "SITE_NAME";
		zoneage[id][2] = "SITECODE";
		zoneage_filed[id]['SITE_NAME'] = "Nom de la zone";
		zoneage_filed[id]['SITECODE'] = "Code";
	}
	
	message = "";
	check = false;
	
	for( var j=1;j<zoneage[id].length;j++ ){
		name = zoneage[id][j];
		
		if( document.getElementById( name ) && document.getElementById( name ).value == "" ){
			message += "Veuillez remplir le champ "+ zoneage_filed[id][name]+"\n";
			check = true;
		}
	}
	
	if( id == 8 && document.getElementById( "TY_ZONE" ) && document.getElementById( "TY_ZONE" ).value != "" ){
		tmp = document.getElementById( "TY_ZONE" ).value;
		
		if( tmp == 1 || tmp == 2 ){
			
		}else{
			tmp_name = "Type de zone";
			message += tmp_name + "	Veuillez entrer 1 ou 2" ;
			check = true;
		}
	}
	
	if( check ){
		alert( message );
		return false;
	}else{
		return true;
	}
}

function select_all_options(id_element){
	list = document.getElementById(id_element);
	if( list.length > 0 ){
		for (var i=0; i<list.length; i++){
			list.options[i].selected = true;
		}
	}
}

function select_dep_com(id_element){
	if( document.getElementById(id_element) && document.getElementById("sub_select_filter_dep").style.display!='none' ){
		select_all_options(id_element)
	}
}

function check_commune_options(id_element){
	list = document.getElementById(id_element);
	if( list.length > 0 ){
		return true;
	}else{
		alert( "Veuillez remplir au moins une commune " );
		return false;
	}
}

function keep_theme_list_ajax( id_zonage,sub_theme,option_selected,id_zone,num,name ){
	if( document.getElementById( sub_theme ).value != "" ){
		var option_selected_dest = document.getElementById(option_selected);
	    var option_selected_len = option_selected_dest.length;
	    var option_value_select_list = '';
	    var sub_theme_value = document.getElementById(sub_theme).value;;
	    
	    for( var t=0;t<option_selected_len;t++ ){
	    	option_value_select_list += option_selected_dest.options[t].value+"+";
	    }
	    
	    //alert( option_value_select_list );
	    
	    if( option_selected_len > 0 ){
	    $.ajax({
		    type: "POST",
		    url: "search_admin_zone_keep_theme_list_ajax.php3",
		    data:   "id_zonage="+id_zonage+"&sub_theme_value="+escape(sub_theme_value)+"&option_selected="+option_value_select_list+"&id_zone="+id_zone+"&num="+num+"&name="+escape(name),
		    success: function( text ){
		    	//alert( text+"aaaaaaaaaa" );
		    }
		});
	    }
	}
}

function get_theme_list_ajax( name,id_zonage,id_zone,type,div_id,title ){
	
	if( name != "" ){
		$.ajax({
	    type: "POST",
	    url: "search_admin_zone_get_theme_list_ajax.php3",
	    data:   "name="+escape(name)+"&id_zonage="+id_zonage+"&id_zone="+id_zone+"&type="+type+"&div_id="+div_id+"&title="+escape(title),
	    success: function( text ){
	    	//alert( text );
	       if( type == 1 ){
		       $( "#get_theme_ajax"+div_id ).empty();  
		       $( "#get_theme_ajax"+div_id ).html( text );
	       }else if( type == 2 ){
	       	   $( "#get_theme_mot_ajax"+div_id ).empty();  
		       $( "#get_theme_mot_ajax"+div_id ).html( text );
	       }
	    }
		});
	}
}
// end add by sam


//add by tealy 
function check_all_box(){

        if($('#checkboxall').attr('checked') == true){
            $("input[name='zone_type[]']").each(function(){ $(this).attr("checked", true); }); 
        }else{
          $("input[name='zone_type[]']").each(function(){$(this).attr("checked", false); }); 
        }
}

function getParameter(name){
 
 if( !( name=='type' || name=='filter' || name=='filtervalue' || name=='category' || name=='theme' ) ){
  return null;
 }
 var input = document.location.href.split('?')[1];
 input = unescape(input);
 var results = input.split('&');
 var names = new Array();
 var values = new Array();
 var pair = new Array();
 
 //below loop will generate 2 arrays. 1 array is the parameter name, another is for the values 
 for(var i=0; i<results.length; i++){
  pair = null;
  pair = results[i].split('=');
  names.push(pair[0]);
  if(pair.length>1){
   values.push(pair[1]);
  }else{
   values.push('');
  }
 }
 var j = -1;
 for(i=0; i<names.length; i++){
  if( names[i] == name ){
   return values[i];
   j = i;
  }
  
 }
 //if this parameter is not existing, then there is no chance to change j in the above loop, the j should be -1
 if( j==-1 ) return '';
}

function selectOptionByValue(objSelect, value){
 for(var i=0; i<objSelect.length; i++){
  if( objSelect.options[i].value == value ) objSelect.options[i].selected = true;
 }
}

function setCriteria(type_list, filter, filtervalue, theme, sub_theme, category){
	
 
 /*var type_list = getParameter("type");
 
 var filter = getParameter("filter");
 var filtervalue = getParameter("filtervalue");

 var theme = getParameter("theme");
 var category = getParameter("category");*/  

 if(type_list==null || type_list=='') return;     //no zone selected and passed in. no need to set criteria
 
 //set zone type
 var types = type_list.split(",");
 for(var i=0; i<types.length; i++) document.getElementById('zone_type_' + types[i]).checked = "checked";
 
 //set the filter
 
 search_filter1 = document.getElementById('search_filter1');
 selectOptionByValue(search_filter1, filter);
 
 
 //set the filter value

 switch(filter){
  case "":
   break;
   
  case "Commune":
   if( filtervalue != null ){
       
        ajax_get_search_filter('Commune', filtervalue);//use ajax to get back the select tag

   }
   break;
  case "Pays":
   if( filtervalue != null && filtervalue != ''){
       
    ajax_get_search_filter('Pays', filtervalue);//use ajax to get back the select tag
    
   }
   break;
  case "Intercommunalite":
   if( filtervalue != null && filtervalue != ''){
    ajax_get_search_filter('Intercommunalite', filtervalue);//use ajax to get back the select tag
   }
   break;
  case "Scot":
   if( filtervalue != ''){
       
     ajax_get_search_filter('Scot', filtervalue);//use ajax to get back the select tag

   }
   break;
  case "Sage":
   if( filtervalue != null && filtervalue != ''){
       
    ajax_get_search_filter('Sage', filtervalue);//use ajax to get back the select tag
    
   }
   break;
  case "Department":
   if( filtervalue != null && filtervalue != ''){
       
    ajax_get_search_filter('Department', filtervalue);//use ajax to get back the select tag

   }
   break;
  default:

 }

 	// set the categories on the left

	 if(theme != null && category != null && theme != '' && category != ''){
		selectOptionByValue(document.getElementById("theme"), theme);
		ajax_get_select_theme(theme, category);
	 }


	 
	 if(sub_theme != null && category != null && sub_theme != '' && category != '' ){

		ajax_get_select_theme(theme, sub_theme );
		ajax_get_select_theme(sub_theme, category, 'Faune');
	 }
 	
	 //set all the selected options into the right box
	 
 }
	
var zones = new Array();

function openZone(id_zone, id_zonage){
	if(pageload == 'end'){
	
	    $('#ajax_loader_img').html('<img src="dist/images/ajax-loader.gif"/>');
	    if(zones[id_zone + '__' + id_zonage] != null && zones[id_zone + '__' + id_zonage] != ''){ 
	        $('#ajax_loader_img').html('');
	        $('#search_result_page').css("display", "none");
	        $('#search_zone_page').css("display", "block");
	        $('#search_zone_page').html(zones[id_zone + '__' + id_zonage]);
	    }else{
	        $.ajax({
	            type: "GET",
	            url: "search_zone.php3?id_zone="+(id_zone)+"&id_zonage="+(id_zonage),
	            //data:  "id_zone="+escape(id_zone)+"id_zonage="+escape(id_zonage),
	            success: function(zone_detail){
	                $('#ajax_loader_img').html('');
	            	$('#search_result_page').css("display", "none");
	            	$('#search_zone_page').css("display", "block");
	            	$('#search_zone_page').html(zone_detail);
	            	zones[id_zone + '__' + id_zonage] = zone_detail;
	            }
	        });
	    }
	}
}

function show_search_result(id_zone, id_zonage){
	$('#search_result_page').css("display", "block");
	$('#search_zone_page').css("display", "none");
}

function setpageLoad(){
    var pageload = 'end';
    alert(pageload);
}

function popupPDfWindow(url) {		
    window.open( url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes, resizable=yes,copyhistory=no,width=300,height=300')
	return false;
}

function update_successful(){
	alert( "L'information a bien été sauvegardée" );
}

function date_format( id,date_value ){
	var date = date_value.split( "-" );
	
	//if( date[2].substring( 0,1 ) == "0")
	//	date[2] = date[2].substring( 1,2 );
	
	var date_02 = date[2]+"-"+date[1]+"-"+date[0]
	document.getElementById( id ).value = date_02;
}

function isExistOption(id_theme, id_list){
	result = false;
	var list = document.getElementById(id_list);
	for(var i=0; i<list.length; i++){
		if( list.options[i].value == id_theme ){
			result = true;
			break;
		}
	}
	return result;
}
