var  URL_BASE = '/carto/fonctionnalites/';

var Lancement = {
	urlBase : URL_BASE, 
	popEmprisePerso: null,
	
	winOl : null ,
	openLocalisation : function(){
		var top=(screen.height-500)/2;
		var left=(screen.width-500)/2;
		this.winOl = window.open(this.urlBase+'localisation/localisation_recherche.php', "Localisation", "top="+top+",left="+left+",width=800,height=600,status=no,resizable=no,scrollbars=no");
		this.winOl.focus();		
	}, 
	close : function(){
		this.winOl.close() ;	
	}
	
};

/**************************************************************************
Objet : Objet contenant toutes les fonctions
d'appel du territoire de compétence
**********************************************************************************/
var territoire = {
	popVisuTc : null,
	popCreationTc : null,

	open : function(idOrganisme, mode) {

		id = idOrganisme || '';
		mode = mode || 'ext';

		if(Ext.get('window_pop_creation_tc')) {
			return ;
		}

	    if(mode == 'ext') {
    		//window.open(Lancement.urlBase+'territoire_competence/index.php?id='+id, 'territoire_competence', 'menubar=no, status=no, scrollbars=no, menubar=no, width=800, height=704, resizable=0');
    		var _url = Lancement.urlBase+'territoire_competence/index.php?mode='+mode+'&id='+id;
    		this.popCreationTc = new Ext.Window({
        	    title: '.: CRIGE PACA : applications cartographiques :.',
        	    id: 'window_pop_creation_tc',
        	    html : "<iframe src='" + _url + "' width='100%' style='background: #fff;'height='100%' frameborder=0 scrolling='no'></iframe>",
        	    width: 834,
        	    height: 680,
        		bodyStyle : 'padding-top:10px' ,
        	    collapsible: false,
        	    plain: true,
        	    resizable : false,
        	    autoScroll: false
            });
            this.popCreationTc.show();
        }else {
    		window.open(
    		    Lancement.urlBase+'territoire_competence/index.php?mode='+mode+'&id='+id,
    		    'territoire_competence',
    		    'menubar=no,status=no, scrollbars=no, menubar=no, width=800, height=650, resizable=0'
    	    );
        }
	},
	
	visualiser : function(idOrganisme) {
		if(Ext.get('window_pop_visu_tc')) {
			return ;
		}
		id = idOrganisme || '';
		//window.open(Lancement.urlBase+'visualisation_tc/index.php?id='+id, 'visualisation_tc', 'menubar=no, status=no, scrollbars=no, menubar=no, width=800, height=600, resizable=0');
	  this.popVisuTc = new Ext.Window({
	    title: '.: CRIGE PACA : applications cartographiques :.',
	    id: 'window_pop_visu_tc',
	    width: 834,
		bodyStyle : 'padding-top:10px' ,
	    html : "<iframe src='"+Lancement.urlBase+'visualisation_tc/index.php?id='+id+"' style='background: #fff' width='100%' height='100%' frameborder=0  scrolling='no'></iframe>",
	    height: 587,
		y: 5,
	    collapsible: false,
	    plain: true,
	    resizable : false,
	    autoScroll: false
	  });
	  this.popVisuTc.show();
	},
	
	fermerVisuTc: function() {
		this.popVisuTc.destroy();
	}
	
};


/**************************************************************************
Objet : Objet contenant toutes les fonctions
d'appel de la sélection du panier
**********************************************************************************/
var extraction = {

	open : function(idlot_donnees_carto) {
		//var top=(screen.height-500)/2;
		var top=10;
		//var left=(screen.width-500)/2;
		var left=200;
		
		//window.open(Lancement.urlBase+'panier/index.php?id='+idlot_donnees_carto, 'Commande_panier', 'top='+top+',left='+left+',width=810,height=755,status=no,resize=no,scrollbars=no');
		var _url = Lancement.urlBase+'panier/index.php?id='+idlot_donnees_carto;
		Panier.openPanier(_url);
	},
	
	relance: function(idDiffusion) {
	    idDiffusion = idDiffusion || '';
	    
	    if(idDiffusion == '') {
	        idDiffusion = prompt("idDiffusion non défini, entrez le 'id' ici:", "");
	    }
	    if(typeof idDiffusion != 'string') {
	        return ;
	    }
	    if(idDiffusion.trim() == '') {
	        this.relance();
	        return ;
	    }
    	new Ajax(Cartotheque.urlBase + 'extraction/ajax.php',{
    	 	method: 'get',
    		postBody: {
    			act: 'relance',
    			'idDiffusion': idDiffusion
    		},
    		onComplete: function(str) {
    			if(str.trim() == 'ok') {
    			    // traitement ok => action à définir
    			    
    			    
    			}else {
    			    // erreur traitement => action à définir
    			    
    			    
    			}
    		}
    	}).request();
    }
}

/**************************************************************************
Objet : Objet contenant toutes les fonctions
d'appel de la modification et suppression du panier
**********************************************************************************/
var Panier = {
	popPanier: null,
	
	openPanier: function(_url) {
		if(Ext.get('window_pop_commande_panier')) {
			return ;
		}
	  Panier.popPanier = new Ext.Window({
	    title: ".: CRIGE PACA : applications cartographiques :.",
	    id: 'window_pop_commande_panier',
	    width: 820,
	    html : "<iframe src='"+_url+"' width='100%' style='background: #fff;' height='750px' frameborder=0  scrolling='no'></iframe>",
	    height: 760,
	    collapsible: false,
	    plain: true,
			y: 5,
			bodyStyle : 'padding-top:10px' ,
	    resizable : false,
	    autoScroll: false
	  });
	  Panier.popPanier.show();
	},
	
	open: function(idCommande) {
		var top=(screen.height-500) / 2;
		var left=(screen.width-500) / 2;
		window.open(Lancement.urlBase+'panier/index.php?idCommande='+idCommande, 'Commande_panier', 'top='+top+',left='+left+',width=800,height=650,status=no,resize=no,scrollbars=no');
	},
	open_popup_error:function(){
		//window.open(Lancement.urlBase+"panier/error.php", "Erreur", 'menubar=no, status=no, scrollbars=no, menubar=no, width=700, height=204, resizable=0');
		var _url = Lancement.urlBase+"panier/error.php";
	  new Ext.Window({
	    title: ".: CRIGE PACA : applications cartographiques :.",
	    width: 720,
	    html : "<iframe src='"+_url+"' width='100%' style='background: #fff;' height='790px' frameborder=0  scrolling='no'></iframe>",
	    height: 250,
			bodyStyle : 'padding-top:10px' ,
	    collapsible: false,
	    plain: true,
	    resizable : false,
	    autoScroll: false
	  }).show();
	},
	test_secure:function(add,idlotdiff){
		var parurl = Lancement.urlBase+'panier/index.php?act=vider&test_session=true';
		
		var param = "act=vider&test_session=true";
		if(add) param = param+'&id='+idlotdiff; else param = parurl+'&iddiffusion='+idlotdiff;
		if(window.XMLHttpRequest)var xhrcadre = new XMLHttpRequest();
	  else if(window.ActiveXObject){
	  	try {
	    	var xhrcadre = new ActiveXObject("Msxml2.XMLHTTP");
	    } 
	    catch (e) {
	    	var xhrcadre = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }		
		xhrcadre.onreadystatechange = function(){
			if(xhrcadre.readyState == 4 && xhrcadre.status == 200){
				var shtml = xhrcadre.responseText;
				if(shtml!="error_session"){
					if(add){
						extraction.open(idlotdiff);
					}else{
						Panier.modifier(idlotdiff);
					}
				}else{
					Panier.open_popup_error();
				}
			}
		}
	  xhrcadre.open("POST",parurl,true);
		xhrcadre.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhrcadre.send(param);
	},
	vider : function(iduser) {
	    if(confirm('Confirmez-vous l\'annulation de ce panier ?')) {
        		var parurl = Lancement.urlBase+'panier/ajax.php';
        		var param = "act=vider&iduser="+iduser;
        		if(window.XMLHttpRequest)var xhrcadre = new XMLHttpRequest();
                else if(window.ActiveXObject){
                    try {
                    	var xhrcadre = new ActiveXObject("Msxml2.XMLHTTP");
                    } 
                    catch (e) {
                    	var xhrcadre = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                }		
        		xhrcadre.onreadystatechange = function(){
        			if(xhrcadre.readyState == 4 && xhrcadre.status == 200){
        				var shtml = xhrcadre.responseText;
        				//alert("Suppression réussie avec succès");
                        //Ext.MessageBox.alert("", "Votre panier est vide");
        				window.location.reload();
        			}
        		}
        	    xhrcadre.open("POST",parurl,true);
        		xhrcadre.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        		xhrcadre.send(param);    
	    }
	},
	valider : function (iduser) {
	    if(confirm('Confirmez-vous la validation de ce panier de commande?')) {
        		var parurl = Lancement.urlBase+'panier/ajax.php';
        		var param = "act=valider&iduser="+iduser;
        		if(window.XMLHttpRequest)var xhrcadre = new XMLHttpRequest();
                else if(window.ActiveXObject){
                try {
                	var xhrcadre = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                	var xhrcadre = new ActiveXObject("Microsoft.XMLHTTP");
                }
                }		
        		xhrcadre.onreadystatechange = function(){
        			if(xhrcadre.readyState == 4 && xhrcadre.status == 200){
        				var shtml = xhrcadre.responseText;
        				//alert("Validation réussie avec succès");
                        Ext.MessageBox.alert("", "Votre panier a bien Ã©tÃ© enregistrÃ© !\n Vous pouvez consulter l\'Ã©volution des extractions demandÃ©es au travers du lien \"Mes commandes\"");
           				setTimeout("window.location.reload()",5000);
        			}
        		}
        	    xhrcadre.open("POST",parurl,true);
        		xhrcadre.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        		xhrcadre.send(param);
	    }
	},
	suppr	 : function (iddiffusion) {		
	    if(confirm('Confirmez-vous vouloir supprimer cette commande ?')) {
        		var parurl = Lancement.urlBase+'panier/ajax.php';
        		var param = "act=suppr&iddiffusion="+iddiffusion;
        		if(window.XMLHttpRequest)var xhrcadre = new XMLHttpRequest();
                else if(window.ActiveXObject){
                    try {
                    	var xhrcadre = new ActiveXObject("Msxml2.XMLHTTP");
                    } 
                    catch (e) {
                    	var xhrcadre = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                }		
        		xhrcadre.onreadystatechange = function(){
        			if(xhrcadre.readyState == 4 && xhrcadre.status == 200){
        				var shtml = xhrcadre.responseText;
        				//alert("Suppression réussie avec succès");
                        Ext.MessageBox.alert("", "Commande supprimée");
                        window.location.reload();
        			}
        		}
                xhrcadre.open("POST",parurl,true);
        		xhrcadre.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        		xhrcadre.send(param);
	    }
	},
	
	modifier : function(iddiffusion) {
		var top=(screen.height-500) / 2;
		var left=(screen.width-500) / 2;
		
		//window.open(Lancement.urlBase+'panier/index.php?act=edit&iddiffusion='+iddiffusion+'', 'Commande_panier', 'top='+top+',left='+left+',width=810,height=730,status=no,resizable=no,scrollbars=no');

		//window.open(Lancement.urlBase+'panier/index.php?id='+idlot_donnees_carto, 'Commande_panier', 'top='+top+',left='+left+',width=810,height=755,status=no,resize=no,scrollbars=no');
		var _url = Lancement.urlBase+'panier/index.php?act=edit&iddiffusion='+iddiffusion;
		Panier.openPanier(_url);

	},
	verifier_lot : function (idlot) {
		
		var parurl = Lancement.urlBase+'panier/ajax.php';
		var param = "act=verifier_lot&id="+idlot;
		if(window.XMLHttpRequest)var xhrcadre = new XMLHttpRequest();
	    else if(window.ActiveXObject){
	  	try {
	    	var xhrcadre = new ActiveXObject("Msxml2.XMLHTTP");
	    } 
	    catch (e) {
	    	var xhrcadre = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }		
		xhrcadre.onreadystatechange = function(){
			if(xhrcadre.readyState == 4 && xhrcadre.status == 200){
				var shtml = xhrcadre.responseText;
                Ext.MessageBox.alert("", shtml);
				//alert(shtml);
				return;
				if(shtml=='not_valide'){
					//window.open(Lancement.urlBase+'panier/error.php');
					var _url = Lancement.urlBase+'panier/error.php';
				  new Ext.Window({
				    title: ".: CRIGE PACA : applications cartographiques :. ",
				    width: 720,
				    html : "<iframe src='"+_url+"' width='100%' style='background: #fff;' height='790px' frameborder=0  scrolling='no'></iframe>",
				    height: 250,
				    collapsible: false,
				    plain: true,
				    resizable : false,
				    autoScroll: false
				  }).show();

				}else{
					extraction.open(idlot);
				}
			}
		}
	  xhrcadre.open("POST",parurl,true);
		xhrcadre.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhrcadre.send(param);
	},
	
    visuEmpriseExtraction : function(idDiffusion) {
    	var _url = Cartotheque.urlBase+"visu_commande/index.php?id=" + idDiffusion;
    	new Ext.Window({
    	    title: "Territoire d'extraction",
    	    //id: 'window_pop_visu_terr_ext',
    	    html : "<iframe src='" + _url + "'style='background: #fff;' width='510px' height='480px' frameborder=0  scrolling='no'></iframe>",
    	    width: 510,
    	    height: 480,
    	    collapsible: false,
    	    plain: true,
    	    resizable : false,
    	    autoScroll: false
    	}).show();
    }
	
}

/**
**/

var Cartotheque = {
	urlBase : URL_BASE,
	viewerUrl : 'http://www.crige-paca.org/carto_viewer_v2/index.php' ,
	winOl : null,
	

    /**********************
    synchronisation géosource
    **********************/
    
 	changeFormulaireSynchro: function(idGeosource) {
		Cartotheque.resetFormulaireSynchro();
		if(idGeosource == '') {
			return ;
		}
		new Ajax(URL_BASE + 'synchronisation_geosource/ajax.php',{
		 	method: 'post',
			postBody: {
				act: 'listeAttribut',
				'idGeosource': idGeosource,
				'tp' : 'carthoteque'
			},
			onComplete: function(str) {

				try {
					eval('var res =' + str);				
				}catch(evt) {
					var res = false;
				}
				if(res) {
					if(typeof res.thematique == 'object') {
						res.thematique = '<li>' + res.thematique.join('</li><li>') + '</li>';
					}
					
					$('formsyncrho_carte_motcle').innerHTML = res.mot_cle;
					$('formsyncrho_carte_titre').innerHTML = res.titre;
					$('formsyncrho_carte_descriptif').innerHTML = res.descriptif;
					$('formsyncrho_carte_producteur').innerHTML = res.producteur;
					$('formsyncrho_carte_thematique').innerHTML = res.thematique;

					$('formsyncrho_carte_datecreation').innerHTML = res.date_creation;
					$('formsyncrho_carte_datepublication').innerHTML = res.date_publication;
					$('formsyncrho_carte_daterevision').innerHTML = res.date_revision;
					
					// path_vignette
					
					if(res.contrainte_acces && res.contrainte_acces == 1) {
					    var tpacces = 'Restreint';
					}else {
					    var tpacces = 'Libre';
					}
				    $('formsyncrho_carte_acces').innerHTML = tpacces;
				    
				    $('formsyncrho_carte_url_site_web').innerHTML = res.url_site_web;
				    
				    $('formsyncrho_carte_extent_minx').innerHTML = res.extent.minx;
				    $('formsyncrho_carte_extent_miny').innerHTML = res.extent.miny;
				    $('formsyncrho_carte_extent_maxx').innerHTML = res.extent.maxx;
				    $('formsyncrho_carte_extent_maxy').innerHTML = res.extent.maxy;
				    
				}else {
					// 
					
					
				}
				
			}
		}).request();		
	},
	resetFormulaireSynchro: function() {
		$('formsyncrho_carte_motcle').innerHTML = '';
		$('formsyncrho_carte_titre').innerHTML = '';
		$('formsyncrho_carte_descriptif').innerHTML = '';
		$('formsyncrho_carte_producteur').innerHTML = '';
		$('formsyncrho_carte_thematique').innerHTML = '';

		$('formsyncrho_carte_datecreation').innerHTML = '';
		$('formsyncrho_carte_datepublication').innerHTML = '';
		$('formsyncrho_carte_daterevision').innerHTML = '';

		$('formsyncrho_carte_resolutionspatiale').innerHTML = '';
		$('formsyncrho_carte_acces').innerHTML = '';

        $('formsyncrho_carte_url_site_web').innerHTML = '';
        $('formsyncrho_carte_url_dl').innerHTML = '';

		$('formsyncrho_carte_extent_minx').innerHTML = '';
		$('formsyncrho_carte_extent_miny').innerHTML = '';
		$('formsyncrho_carte_extent_maxx').innerHTML = '';
		$('formsyncrho_carte_extent_maxy').innerHTML = '';
	},	
	//////////////////


	emprisePerso: function(){
		var _top=(screen.height-500)/2;
		var _left=(screen.width-500)/2;
		
		//this.winOl = window.open(this.urlBase+'localisation/localisation_recherche.php', "Localisation", "top="+top+",left="+left+",width=610,height=430,status=no,resizable=no,scrollbars=no");
		if(Ext.get('window_pop_emprise_perso')) {
			return ;
		}
		var _url = this.urlBase+'localisation/localisation_recherche.php?tp=cartotheque';
		Lancement.popEmprisePerso = new Ext.Window({
	    title: ".: CRIGE PACA : applications cartographiques :.",
	    id: 'window_pop_emprise_perso',
	    width: 616,
	    html: "<iframe scrolling='no' style='overflow:hidden;background: #fff;' src='" + _url + "' width='100%' height='100%' frameborder=0  scrolling='no'></iframe>",
	    height: 467,
			bodyStyle : 'padding-top:10px' ,
	    collapsible: false,
	    plain: true,
	    resizable: false,
	    autoScroll: false
	  });
	  Lancement.popEmprisePerso.show();
	},
	
	
	consulteFiche : function(idCarte){
		var top=(screen.height-500)/2;
		var left=(screen.width-500)/2;
		//this.winOl = window.open(this.urlBase+'cartotheque/geosource.php?idlot='+idlot, "ConsultationFiche", "top="+top+",left="+left+",width=620,height=670,status=no,resizable=no,scrollbars=no");

		if(Ext.get('window_pop_cartotk_consulter_fiche')) {
			return ;
		}
		var _url = this.urlBase+'cartotheque/geosource.php?idcarte='+idCarte;
		this.popConsulteFiche = new Ext.Window({
	    title: ".: CRIGE PACA : applications cartographiques :. ",
	    id: 'window_pop_cartotk_consulter_fiche',
	    html : "<iframe src='" + _url + "'style='background: #fff;' width='100%' height='100%' frameborder=0  scrolling='no'></iframe>",
	    width: 620,
	    height: 600,
	    collapsible: false,
	    plain: true,
	    resizable : false,
	    autoScroll: false
	  });
	  this.popConsulteFiche.show();

	},
    //cartotheque
	visualiser : function(idcarto){
	    
	    var popCartovisu = window.open();
	    
		var parurl = Cartotheque.urlBase+'cartotheque/ajax.php';
		var param = "idcarto="+idcarto+"&act=viewer";
		if(window.XMLHttpRequest)var xhrcadre = new XMLHttpRequest();
	    else if(window.ActiveXObject){
    	  	try {
    	    	var xhrcadre = new ActiveXObject("Msxml2.XMLHTTP");
    	    } 
    	    catch (e) {
    	    	var xhrcadre = new ActiveXObject("Microsoft.XMLHTTP");
    	    }
	    }		
		xhrcadre.onreadystatechange = function(){
			if(xhrcadre.readyState == 4 && xhrcadre.status == 200){
				eval('var res = ' + xhrcadre.responseText);
				if(res.error == 'failed'){
					var _url = Cartotheque.urlBase+"cartotheque/error.php";
					new Ext.Window({
    				    title: ".: CRIGE PACA : applications cartographiques :. ",
    				    id: 'window_pop_cartotk_error',
    				    html : "<iframe src='" + _url + "'style='background: #fff;' width='720px' height='240px' frameborder=0  scrolling='no'></iframe>",
    				    width: 720,
    				    height: 250,
    				    collapsible: false,
    				    plain: true,
    				    resizable : false,
    				    autoScroll: false
    				}).show();
       				popCartovisu.close();
					return false;
				}
				else {
                    if(res.success == 'viewer'){
          				popCartovisu.location.href = res.url;
                        //window.open(res.url);
                    }
                    else{
                        if(res.url != '') {
                            window.open(res.url);
                        }else {
                            Ext.MessageBox.alert("", "URL invalide");
            				popCartovisu.close();
                        }
                    }
				}
			}
		}
	    xhrcadre.open("POST",parurl,true);
		xhrcadre.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhrcadre.send(param);
	},
	close : function(){
		this.winOl.close() ;	
	},
	btover:function(oBtn){
		oBtn.style.backgroundPosition = "0 -"+oBtn.offsetHeight+"";
	},
	btout:function(oBtn){
		oBtn.style.backgroundPosition = "0 0";
	},
	ouvreSite : function(idcarte){
		var parurl = Cartotheque.urlBase+'cartotheque/ajax.php';
		var param = "idCarte="+idcarte+"&act=openUrl";
		if(window.XMLHttpRequest)var xhrcadre = new XMLHttpRequest();
	  else if(window.ActiveXObject){
	  	try {
	    	var xhrcadre = new ActiveXObject("Msxml2.XMLHTTP");
	    } 
	    catch (e) {
	    	var xhrcadre = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }		
		xhrcadre.onreadystatechange = function(){
			if(xhrcadre.readyState == 4 && xhrcadre.status == 200){
				eval('var res = ' + xhrcadre.responseText);
				if(res.error){
					var _url = Cartotheque.urlBase+"cartotheque/error.php";
					new Ext.Window({
				    title: ".: CRIGE PACA : applications cartographiques :. ",
				    id: 'window_pop_cartotk_erreursite',
				    html : "<iframe src='" + _url + "'style='background: #fff;' width='100%' height='100%' frameborder=0  scrolling='no'></iframe>",
				    width: 800,
				    height: 600,
				    collapsible: false,
				    plain: true,
				    resizable : false,
				    autoScroll: false
				  }).show();
					return false;
				}
				var siteWeb = res.webSite;
				var regHttp = new RegExp("^http://","");
			  if(!regHttp.test(siteWeb)){
			      siteWeb = 'http://'+siteWeb;
			  }
				window.open(siteWeb);
			}
		}
	  xhrcadre.open("POST",parurl,true);
		xhrcadre.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhrcadre.send(param);
	},
	download : function(idcarte){
		var parurl = Cartotheque.urlBase+'cartotheque/ajax.php';
		var param = "idCarte="+idcarte+"&act=download";
		if(window.XMLHttpRequest)var xhrcadre = new XMLHttpRequest();
        else if(window.ActiveXObject){
            try {
                var xhrcadre = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) {
                var xhrcadre = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        
        
		xhrcadre.onreadystatechange = function(){
			if(xhrcadre.readyState == 4 && xhrcadre.status == 200){
                eval('var res = ' + xhrcadre.responseText);
                if(res.error){
                    Ext.MessageBox.alert("", "La carte est introuvable");
                    return false;
                }
                var url_download = res.url_download;
                var regHttp = new RegExp("^http://","");
                if(!regHttp.test(url_download)){
                    url_download = 'http://'+url_download;
                }
                //window.location.href = res.url_download ;
                var _url = res.url_download;
                /*
                new Ext.Window({
                    title: ".: CRIGE PACA : applications cartographiques :.",
                    id: 'window_pop_cartotk_download',
                    html : "<iframe src='" + _url + "'style='background: #fff;' width='100%' height='100%' frameborder=0  scrolling='no'></iframe>",
                    width: 820,
                    height: 620,
                    collapsible: false,
                    plain: true,
                    resizable : true,
                    autoScroll: false
                }).show();
                */
                if(_url != '') {
   				    window.open(_url, '__blank', 'width=800,height=600');
                }else {
                    Ext.MessageBox.alert("", "URL invalide");
                }
			}
		}
        xhrcadre.open("POST",parurl,true);
        xhrcadre.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhrcadre.send(param);
	},

	// RECHERCHE
	
	/*
	Traitement avant d'effectuer la recherche
	Réccupération notemment:
		Du choix de la localisation (boutton radio)
		De la valeur de ce choix (code dept ou code com)
	*/
	// Cartotheque
	traitementPostRecherche: function() {
		var mot_cle = $('cartotheque_MotCle').value;
		var type_donnee = $('cartotheque_TypeDonnee').value;
		var producteur = $('cartotheque_Producteur' ).value;
		var thematique = $('cartotheque_Thematique').value;
		var acces = $('cartotheque_Acces').value;
		var reference_temporelle_du = $('cartotheque_ReferenceTemporelleDu').value;
		var reference_temporelle_au = $('cartotheque_ReferenceTemporelleAu').value;
		
		// type localisation / valeur localisation
		if($('cartotheque_TypeLocalisation_1').checked) {
			var id_localisation = $('cartotheque_TypeLocalisation_1').value;
			var valeur_localisation = '';
		}else if($('cartotheque_TypeLocalisation_2').checked) {
			var id_localisation = $('cartotheque_TypeLocalisation_2').value;
			var valeur_localisation = '';
		}else if($('cartotheque_TypeLocalisation_3').checked) {
			var id_localisation = $('cartotheque_TypeLocalisation_3').value;
			var valeur_localisation = '';
		}else if($('cartotheque_TypeLocalisation_4').checked) {
			var id_localisation = $('cartotheque_TypeLocalisation_4').value;
			var valeur_localisation = $('cartotheque_ChoixDepartement').value;
		}else if($('cartotheque_TypeLocalisation_5').checked) {
			var id_localisation = $('cartotheque_TypeLocalisation_5').value;
			var valeur_localisation = $('cartotheque_ChoixCommune').value;
		}else if($('cartotheque_TypeLocalisation_6').checked) {
			var id_localisation = $('cartotheque_TypeLocalisation_6').value;
			var valeur_localisation = '';
		}
		
		Cartotheque.recherche(
			{
				'mot_cle' : mot_cle,
				'type_donnee' : type_donnee,
				'producteur' : producteur,
				'thematique' : thematique,
				'acces' : acces,
				'reference_temporelle_du' : reference_temporelle_du,
				'reference_temporelle_au' : reference_temporelle_au,
				'id_localisation' : id_localisation,
				'valeur_localisation' : valeur_localisation
			}
		);
	},
	
	/*
	Action du bouton Lancer la recherche
	Appel : Cartotheque.recherche(oCritere);
	Argument: oCritere - Objet contenant les éléments à recherché:
	{
		'mot_cle' : '...'  // varchar
		'type_donnee' : '...'	// id_type_donnee
		'producteur' : '...' 	// varchar
		'thematique' : '...'	// varchar
		'acces' : '...'	
		'mode_telechargement' : '...'
		'reference_temporelle_du' : '...'
		'reference_temporelle_au' : '...'
		'id_localisation' : '...'
		'valeur_localisation' : '...'
	}
	*/
	recherche: function(oCritere) {
		new Ajax(Cartotheque.urlBase + 'cartotheque/ajax.php',{
		 	method: 'post',
			postBody: {
				act: 'recherche',
				'mot_cle': oCritere.mot_cle || '',
				'type_donnee': oCritere.type_donnee || '',
				'producteur': oCritere.producteur || '',
				'thematique': oCritere.thematique || '',
				'acces': oCritere.acces || '',
				'reference_temporelle_du': oCritere.reference_temporelle_du || '',
				'reference_temporelle_au': oCritere.reference_temporelle_au || '',
				'id_localisation': oCritere.id_localisation || '',
				'valeur_localisation': oCritere.valeur_localisation || ''
			},
			onComplete: function(str) {
                Ext.MessageBox.alert("", str);
				//alert(str);
			}
		}).request();		
	},

	/*
	Synchronisation géosource
	Argument:
	    id_carte_carto - id de la carte_carto
	    id_geosource - id géosource
	*/
    synchro_geosource: function (id_carte_carto, id_geosource) {
        Depot_donnees.synchroniser(id_carte_carto, id_geosource, 'cartotheque');        
    }



};

var Geocatalogue = {
	urlBase : URL_BASE,
	winOl : null,
    
	emprisePerso : function(){
		var _top=(screen.height-500)/2;
		var _left=(screen.width-500)/2;
		
		//this.winOl = window.open(this.urlBase+'localisation/localisation_recherche.php', "Localisation", "top="+top+",left="+left+",width=610,height=430,status=no,resizable=no,scrollbars=no");
		if(Ext.get('window_pop_emprise_perso')) {
			return ;
		}
		var _url = this.urlBase+'localisation/localisation_recherche.php?tp=geocatalogue';
		Lancement.popEmprisePerso = new Ext.Window({
    	    title: ".: CRIGE PACA : applications cartographiques :.",
    	    id: 'window_pop_emprise_perso',
    	    width: 620,
    	    html : "<iframe scrolling='no' style='overflow:hidden;background: #fff;' src='" + _url + "' width='100%' height='100%' frameborder=0 scrolling='no'></iframe>",
    	    height: 480,
    	    collapsible: false,
    	    plain: true,
    	    resizable : false,
    	    autoScroll: false
	  });
	  Lancement.popEmprisePerso.show();
	},


	consulteFiche : function(idlot){
		var top=(screen.height-500)/2;
		var left=(screen.width-500)/2;
		//this.winOl = window.open(this.urlBase+'geocatalogue/geosource.php?idlot='+idlot, "ConsultationFiche", "top="+top+",left="+left+",width=620,height=670,status=no,resizable=no,scrollbars=no");

		if(Ext.get('window_pop_geocat_consult_fiche')) {
			return ;
		}
		var _url = this.urlBase+'geocatalogue/geosource.php?idlot='+idlot;
		this.popCounsultFiche = new Ext.Window({
    	    title: ".: CRIGE PACA : applications cartographiques :.",
    	    id: 'window_pop_geocat_consult_fiche',
    	    html : "<iframe src='" + _url + "'style='background: #fff;' width='100%' height='100%' frameborder=0 scrolling='no'></iframe>",
    	    width: 620,
    	    height: 600,
    	    collapsible: false,
    	    plain: true,
    	    resizable : false,
    	    autoScroll: false
	  });
	  this.popCounsultFiche.show();
	},
	visualiser : function(idlot) {
		var top=(screen.height-500)/2;
		var left=(screen.width-500)/2;
		//this.winOl = window.open(this.urlBase+'geocatalogue/index.php?idlot='+idlot, "Visualisation", "top="+top+",left="+left+",width=500,height=400,status=no,resizable=no,scrollbars=no");
		
		if(Ext.get('window_pop_geocat_visualiser')) {
			return ;
		}
		var _url = this.urlBase+'geocatalogue/index.php?idlot='+idlot;
		this.popVisualisation = new Ext.Window({
			title: ".: CRIGE PACA : applications cartographiques :.",
			id: 'window_pop_geocat_visualiser',
			html : "<iframe src='" + _url + "' width='100%' height='100%' style='background: #fff;' frameborder=0 scrolling='no'></iframe>",
			width: 517,
			height: 510,
			bodyStyle : 'padding-top:10px' ,
			collapsible: false,
			plain: true,
			resizable : false,
			autoScroll: false
		});
		this.popVisualisation.show();
		
	},
	
	fermerVisuPop : function(){
		this.popVisualisation.close();
	},
	openUrl : function(idlot){
		var parurl = Geocatalogue.urlBase+'geocatalogue/site.php';
		var param = "idLot="+idlot;
		if(window.XMLHttpRequest)var xhrcadre = new XMLHttpRequest();
	  else if(window.ActiveXObject){
	  	try {
	    	var xhrcadre = new ActiveXObject("Msxml2.XMLHTTP");
	    } 
	    catch (e) {
	    	var xhrcadre = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }		
		xhrcadre.onreadystatechange = function(){
			if(xhrcadre.readyState == 4 && xhrcadre.status == 200){
				eval('var res = ' + xhrcadre.responseText);
				if(res.error){
					window.open(Geocatalogue.urlBase + "geocatalogue/error.html");
					return false;
				}
				var siteWeb = res.webSite;
				var regHttp = new RegExp("^http://","");
			  if(!regHttp.test(siteWeb)){
			      siteWeb = 'http://'+siteWeb;
			  }
				window.open(siteWeb);
			}
		}
	    xhrcadre.open("POST",parurl,true);
		xhrcadre.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhrcadre.send(param);
	},
	
	
	
	/*
	Traitement avant d'effectuer la recherche
	Réccupération notemment:
		Du choix de la localisation (boutton radio)
		De la valeur de ce choix (code dept ou code com)
	*/
	// Geocatalogue
	traitementPostRecherche: function() {
		var mot_cle = $('geocataloque_MotCle').value;
		var type_donnee = $('geocataloque_TypeDonnee').value;
		var producteur = $('geocataloque_Producteur' ).value;
		var thematique = $('geocataloque_Thematique').value;
		var acces = $('geocataloque_Acces').value;
		var mode_telechargement = $('geocataloque_ModeTelechargement').value;
		var reference_temporelle_du = $('geocataloque_ReferenceTemporelleDu').value;
		var reference_temporelle_au = $('geocataloque_ReferenceTemporelleAu').value;
		
		// type localisation / valeur localisation
		if($('geocataloque_TypeLocalisation_1').checked) {
			var id_localisation = $('geocataloque_TypeLocalisation_1').value;
			var valeur_localisation = '';
		}else if($('geocataloque_TypeLocalisation_2').checked) {
			var id_localisation = $('geocataloque_TypeLocalisation_2').value;
			var valeur_localisation = '';
		}else if($('geocataloque_TypeLocalisation_3').checked) {
			var id_localisation = $('geocataloque_TypeLocalisation_3').value;
			var valeur_localisation = '';
		}else if($('geocataloque_TypeLocalisation_4').checked) {
			var id_localisation = $('geocataloque_TypeLocalisation_4').value;
			var valeur_localisation = $('geocataloque_ChoixDepartement').value;
		}else if($('geocataloque_TypeLocalisation_5').checked) {
			var id_localisation = $('geocataloque_TypeLocalisation_5').value;
			var valeur_localisation = $('geocataloque_ChoixCommune').value;
		}else if($('geocataloque_TypeLocalisation_6').checked) {
			var id_localisation = $('geocataloque_TypeLocalisation_6').value;
			var valeur_localisation = '';
		}
		
		Geocatalogue.recherche(
			{
				'mot_cle' : mot_cle,
				'type_donnee' : type_donnee,
				'producteur' : producteur,
				'thematique' : thematique,
				'acces' : acces,
				'mode_telechargement' : mode_telechargement,
				'reference_temporelle_du' : reference_temporelle_du,
				'reference_temporelle_au' : reference_temporelle_au,
				'id_localisation' : id_localisation,
				'valeur_localisation' : valeur_localisation
			}
		);
	},
	
	/*
	Action du bouton Lancer la recherche
	Appel : Cartotheque.recherche(oCritere);
	Argument: oCritere - Objet contenant les éléments à recherché:
	{
		'mot_cle' : '...'  // varchar
		'type_donnee' : '...'	// id_type_donnee
		'producteur' : '...' 	// varchar
		'thematique' : '...'	// varchar
		'acces' : '...'	
		'mode_telechargement' : '...'
		'reference_temporelle_du' : '...'
		'reference_temporelle_au' : '...'
		'id_localisation' : '...'
		'valeur_localisation' : '...'
	}
	*/
	recherche: function(oCritere) {
		new Ajax(Geocatalogue.urlBase + 'geocatalogue/ajax.php',{
		 	method: 'post',
			postBody: {
				act: 'recherche',
				'mot_cle': oCritere.mot_cle || '',
				'type_donnee': oCritere.type_donnee || '',
				'producteur': oCritere.producteur || '',
				'thematique': oCritere.thematique || '',
				'acces': oCritere.acces || '',
				'mode_telechargement': oCritere.mode_telechargement || '',
				'reference_temporelle_du': oCritere.reference_temporelle_du || '',
				'reference_temporelle_au': oCritere.reference_temporelle_au || '',
				'id_localisation': oCritere.id_localisation || '',
				'valeur_localisation': oCritere.valeur_localisation || ''
			},
			onComplete: function(str) {
				
				alert(str);		
				
			}
		}).request();		
	},
	
	/*
	Synchronisation géosource
	Argument:
	    id_lot_donnee - id du lot
	    id_geosource - id géosource
	*/
    synchro_geosource: function (id_lot_donnee,id_geosource) {
        Depot_donnees.synchroniser(id_lot_donnee, id_geosource, 'geocatalogue');        
    }
};

var Commmandes = {
	
	downloadhtml : function(idcommande) {
		this.download(idcommande,'html');
	},
	
	downloadftp : function(idcommande, lgn, mdp) {
		this.download(idcommande,'ftp', lgn, mdp);
	},
	download : function(idcommande,typeurl, lgn, mdp){
		var parurl = Cartotheque.urlBase+'commande/ajax.php';
		var param = "idcommande="+idcommande+"&act=download&typeurl="+typeurl;		

		if(lgn && mdp) {
			param += '&lgn='+lgn+'&mdp='+mdp;
		}

		if(window.XMLHttpRequest)var xhrcadre = new XMLHttpRequest();
	  else if(window.ActiveXObject){
	  	try {
	    	var xhrcadre = new ActiveXObject("Msxml2.XMLHTTP");
	    } 
	    catch (e) {
	    	var xhrcadre = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }
	  		
		xhrcadre.onreadystatechange = function(){
			if(xhrcadre.readyState == 4 && xhrcadre.status == 200){
				eval('var res = ' + xhrcadre.responseText);
				if(res.error){
					alert(res.error);
					return false;
				}				
				var url_download = res.url_download;
				var regHttp = new RegExp("^http://","");
			  if(!regHttp.test(url_download)){
			      url_download = 'http://'+url_download;
			  }	  
				window.location.href = res.urldownload ;
			}
		}
	  xhrcadre.open("POST",parurl,true);
		xhrcadre.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhrcadre.send(param);
	}
}

/******
Objet js gérant les formulaire de synchronisation
******/
var Depot_donnees = {
	
	/*
	Réccupère les attributs de l'id géosource séléctionée
	 * Arguments : idGeosource - id du géosource à valider
	 * Return:  met à jour le formulaire de syncrhonisation par les valeur de l'id géosource séléctionné	 
	
	Appelé sur lévenement onchange du select de l'id géosource
	 */
 	changeFormulaireSynchro: function(idGeosource) {
		Depot_donnees.resetFormulaireSynchro();
		if(idGeosource == '') {
			return ;
		}
		new Ajax(URL_BASE + 'synchronisation_geosource/ajax.php',{
		 	method: 'post',
			postBody: {
				act: 'listeAttribut',
				'idGeosource': idGeosource,
				'tp' : 'geocatalogue'
			},
			onComplete: function(str) {

				try {
					eval('var res =' + str);				
				}catch(evt) {
					var res = false;
				}
				if(res) {
					if(typeof res.thematique == 'object') {
						res.thematique = '<li>' + res.thematique.join('</li><li>') + '</li>';
					}
					
					$('formsyncrho_motcle').innerHTML = res.mot_cle;
					$('formsyncrho_titre').innerHTML = res.titre;
					$('formsyncrho_descriptif').innerHTML = res.descriptif;
					$('formsyncrho_producteur').innerHTML = res.producteur;
					$('formsyncrho_thematique').innerHTML = res.thematique;

					$('formsyncrho_datecreation').innerHTML = res.date_creation;
					$('formsyncrho_datepublication').innerHTML = res.date_publication;
					$('formsyncrho_daterevision').innerHTML = res.date_revision;
					
					if(res.contrainte_acces && res.contrainte_acces == 1) {
					    var tpacces = 'Restreint';
					}else {
					    var tpacces = 'Libre';
					}
				    $('formsyncrho_acces').innerHTML = tpacces;
				    
				    $('formsyncrho_url_site_web').innerHTML = res.url_site_web;
				    $('formsyncrho_url_dl').innerHTML = res.url_dl;
				    
				    $('formsyncrho_extent_minx').innerHTML = res.extent.minx;
				    $('formsyncrho_extent_miny').innerHTML = res.extent.miny;
				    $('formsyncrho_extent_maxx').innerHTML = res.extent.maxx;
				    $('formsyncrho_extent_maxy').innerHTML = res.extent.maxy;
				    
				}else {
					// 
					
					
				}
				
			}
		}).request();		
	},
	
	/*
	Reset le formulaire de synchronisation géosource
	 * Arguments : 
	 * Return:   
	
	Appelé sur lévenement onchange du select de l'id géosource
	 */
	resetFormulaireSynchro: function() {
		$('formsyncrho_motcle').innerHTML = '';
		$('formsyncrho_titre').innerHTML = '';
		$('formsyncrho_descriptif').innerHTML = '';
		$('formsyncrho_producteur').innerHTML = '';
		$('formsyncrho_thematique').innerHTML = '';

		$('formsyncrho_datecreation').innerHTML = '';
		$('formsyncrho_datepublication').innerHTML = '';
		$('formsyncrho_daterevision').innerHTML = '';

		$('formsyncrho_resolutionspatiale').innerHTML = '';
		$('formsyncrho_acces').innerHTML = '';

        $('formsyncrho_url_site_web').innerHTML = '';
        $('formsyncrho_url_dl').innerHTML = '';

		$('formsyncrho_extent_minx').innerHTML = '';
		$('formsyncrho_extent_miny').innerHTML = '';
		$('formsyncrho_extent_maxx').innerHTML = '';
		$('formsyncrho_extent_maxy').innerHTML = '';
	},
	
	/*
	Valide la synchronisation d'une fiche géosource
	 * Arguments :
		id - id du lot ou du carte_carto
		idGeosource - id de la fiche géosource à synchroniser
		tp: cartotheque ou geocatalogue
	 * Return:   
	 */
	synchroniser: function(id, idGeosource, tp) {
		if(id == '') {
			id = prompt("id non défini, entrez le 'id' ici:", "");
		}
		
		if(id === null) {
		    return ;		    
		}
		
		if(id == '' || id === null) {
			//return ;
		}
		if(idGeosource == '') {
			return ;
		}
		new Ajax(URL_BASE + 'synchronisation_geosource/ajax.php', {
		 	method: 'post',
			postBody: {
				act: 'synchroniser',
				'id': id,
				'idGeosource': idGeosource,
				'tp': tp
			},
			onComplete: function(str) {
				if(str == 'ok') {
					// synchronisation réussie
					
					
				}else {
					// synchronisation echec
					
					
				}
			}
		}).request();		
	
	},
	/*
	    Fonctions pour lister le metadata 
	*/
	chargerMetadataGeosourceGeocatalogue:function(){
	    Ext.Ajax.request({
            url: 'fonctionnalites/synchronisation_geosource/ajax.php',
            success: function(xhr){
                Ext.MessageBox.alert('Résultats',xhr.responseText);
            },
            failure: function(){
               Ext.MessageBox.alert('Erreur de retour', 'Erreur de retour'); 
            },
            params: { 
            	act : 'geosourceGeocatalogue' 
            }
        });
	},
	
	chargerMetadataGeosourceCartotheque:function(){
	    Ext.Ajax.request({
            url: 'fonctionnalites/synchronisation_geosource/ajax.php',
            success: function(xhr){
                Ext.MessageBox.alert('Résultats',xhr.responseText);
            },
            failure: function(){
               Ext.MessageBox.alert('Erreur de retour', 'Erreur de retour'); 
            },
            params: { 
            	act : 'geosourceCartotheque' 
            }
        });
	},
	/*
	*   Fonctions pour lister les tables commençant par extract 
	*/
	getListeTables:function(){
        Ext.Ajax.request({
            url     : 'fonctionnalites/liste/ajax.php',
            params  :
            {    
                act : 'listetable'
            },
            success :function(xhr){
                Ext.MessageBox.alert('Résultats',xhr.responseText);
            },
            failure :function(){
                Ext.messageBox.alert('Erreur','Erreur de retour');
            }
        });
    }               
};


/******
Objet js gérant les formulaire de synchronisation
******/
var Synchronisation_geosource = {
    /*
	*   Fonctions pour ajout user dans la base geosource alimenté avec la base crige
	*/
	addUser:function(){
        Ext.Ajax.request({
            url     : 'fonctionnalites/synchronisation_geosource/ajax.php',
            params  :
            {    
                act : 'adduser'
            },
            success :function(xhr){
                Ext.MessageBox.alert('Résultats',xhr.responseText);
            },
            failure :function(){
                Ext.messageBox.alert('Erreur','Erreur de retour');
            }
        });
    },
    
    /*
	*   Fonctions pour la mise à jour user dans la base geosource 
	*/
	updateUser:function(){
        Ext.Ajax.request({
            url     : 'fonctionnalites/synchronisation_geosource/ajax.php',
            params  :
            {    
                act : 'updateuser'
            },
            success :function(xhr){
                Ext.MessageBox.alert('Résultats',xhr.responseText);
            },
            failure :function(){
                Ext.messageBox.alert('Erreur','Erreur de retour');
            }
        });
    },
    
    /*
	*   Fonctions pour la suppression user dans la base geosource 
	*/
	deleteUser:function(){
        Ext.Ajax.request({
            url     : 'fonctionnalites/synchronisation_geosource/ajax.php',
            params  :
            {    
                act : 'deleteuser'
            },
            success :function(xhr){
                Ext.MessageBox.alert('Résultats',xhr.responseText);
            },
            failure :function(){
                Ext.messageBox.alert('Erreur','Erreur de retour');
            }
        });
    },
    
    /*
	*   Fonctions pour la suppression user dans la base geosource 
	*/
	synchronizeUser:function(){
        Ext.Ajax.request({
            url     : 'fonctionnalites/synchronisation_geosource/ajax.php',
            params  :
            {    
                act : 'synchronizeuser'
            },
            success :function(xhr){
                Ext.MessageBox.alert('Résultats',xhr.responseText);
            },
            failure :function(){
                Ext.messageBox.alert('Erreur','Erreur de retour');
            }
        });
    }
};

Data = {
    listing :function(){
        Ext.Ajax.request({
            url     : 'fonctionnalites/data/ajax.php',
            success :function(str){
                Ext.MessageBox.alert('Résultats',str.responseText);
            }
        });
    }
};


document.write('<style>body .x-window-mc, body .x-window-plain .x-window-mc { background: #fff; }</style>');
document.write('<style>body .x-window-header-text { font-weight: bold; color: #555555; }</style>');

