// JavaScript Document
function request(url,cadre) {
	 var XHR = null;
	
	 if(window.XMLHttpRequest) 
	   XHR = new XMLHttpRequest();
	 else if(window.ActiveXObject) // Internet Explorer
	   XHR = new ActiveXObject("Microsoft.XMLHTTP");
	 else 
	 { 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest");
	   return;
	 }
	 XHR.open("GET",url, true);
	
	 XHR.onreadystatechange = function attente() 
	 {
	
	  if(XHR.readyState == 4)     
	  {
	   document.getElementById(cadre).innerHTML = XHR.responseText;
	  }
	 }
	 XHR.send(null);
	 return;
}

function timedRefresh(timeoutPeriod) {
	setTimeout("location.reload(true);",timeoutPeriod);
}

function tronque(chaine,longueur){
	return +((chaine+'').substring(0,longueur));
}

function Registration() {
	var page = 'inscrip.php' ;
	var nom = 'popup' ;
	var option = 'width=680,height=450,scrollbars=yes,menubar=no,status=no,resizable=yes' ;
	var fenetre = open(page,nom,option) ;
	if(fenetre.closed == false) {
			fenetre.focus();
        }
        else{
			window.fenetre;
        }
}

function Recover() {
	var page = 'recup.php' ;
	var nom = 'popup' ;
	var option = 'width=680,height=450,scrollbars=yes,menubar=no,status=no,resizable=yes' ;
	var fenetre = open(page,nom,option) ;
	if(fenetre.closed == false) {
			fenetre.focus();
        }
        else{
			window.fenetre;
        }
}

function Profile() {
	var page = 'profile.php' ;
	var nom = 'profile' ;
	var option = 'width=680,height=450,scrollbars=yes,menubar=no,status=no,resizable=yes' ;
	var fenetre = open(page,nom,option) ;
	if(fenetre.closed == false) {
			fenetre.focus();
        }
        else{
			window.fenetre;
        }
}

function reculer() {
	//recule sans recherche
	if(document.navigation.q.value == "") { 
		var fonds = document.navigation.fonds.value ;
		var url = document.navigation.page_url.value ;
		var actuelle_pg = document.navigation.page_actuelle.value ;
		var prev_pg = parseInt(actuelle_pg)-1 ;
		if(prev_pg > 0) { 
			window.location.href = url +"?fonds="+fonds+"&page="+ prev_pg ; 
		} else { 
		//alert("Vous êtes sur la première page") ; 
		return false ;
		}
	}
	//recule avec recherche
	else {
		var fonds = document.navigation.fonds.value ;
		var url = document.navigation.page_url.value ;
		var actuelle_pg = document.navigation.page_actuelle.value ;
		var prev_pg = parseInt(actuelle_pg)-1 ;
		var recherche = escape(document.navigation.q.value) ;
		if(prev_pg > 0) { 
			window.location.href = url +"?fonds="+fonds+"&q="+ recherche +"&page="+ prev_pg ;
		} else { 
			//alert("Vous êtes déjà sur la première page") ;
			return false ;
		}
	} 
}

function avancer() {
	//avance sans recherche
	if(document.navigation.q.value == "") { 
		//le vumero de la page
		var fonds = document.navigation.fonds.value ;
		var url = document.navigation.page_url.value ;
		var actuelle_pg = document.navigation.page_actuelle.value ;
		var next_pg = parseInt(actuelle_pg)+1 ;
		var max_pg = document.navigation.max_pg.value ;
		if(next_pg > max_pg) {
			//alert("Vous êtes sur la dernière page") ;
			return false ;
		} else {
			window.location.href = url +"?fonds="+fonds+"&page="+ next_pg ;
		}
	}
	//avance avec recherche
  	else {
		var fonds = document.navigation.fonds.value ;
		var url = document.navigation.page_url.value ;
		var actuelle_pg = document.navigation.page_actuelle.value ;
		var next_pg = parseInt(actuelle_pg)+1 ;
		var max_pg = document.navigation.max_pg.value ;
		var recherche = escape(document.navigation.q.value) ;
		if(next_pg > max_pg) {
			//alert("Vous êtes sur la dernière page") ;
			return false ;
		} else {
			window.location.href = url +"?fonds="+fonds+"&q="+ recherche +"&page="+ next_pg ;
		}
	}
}

function goto() {
	var cible = document.navigation.gotopage.value ;
	if(cible < 1) {
		var cible = 1 ;	
	}
	//allez à la page sans recherche
	if(document.navigation.q.value == "") { 
		//le vumero de la page
		var fonds = document.navigation.fonds.value ;
		var url = document.navigation.page_url.value ;
		var actuelle_pg = document.navigation.page_actuelle.value ;
		var next_pg = parseInt(actuelle_pg)+1 ;
		var max_pg = document.navigation.max_pg.value ;
		if(parseInt(cible) > parseInt(max_pg)) {
			alert("La page n'existe pas") ;
			document.navigation.gotopage.value = '' ;
			document.navigation.gotopage.focus();
			return false ;
		} else {
			window.location.href = url +"?fonds="+fonds+"&page="+ cible ;
		}
	}
	//allez à la page avec recherche
  	else {
		var fonds = document.navigation.fonds.value ;
		var url = document.navigation.page_url.value ;
		var actuelle_pg = document.navigation.page_actuelle.value ;
		var next_pg = parseInt(actuelle_pg)+1 ;
		var max_pg = document.navigation.max_pg.value ;
		var recherche = escape(document.navigation.q.value) ;
		if(parseInt(cible) > parseInt(max_pg)) {
			alert("La page n'existe pas") ;
			document.navigation.gotopage.value = '' ;
			document.navigation.gotopage.focus();
			return false ;
		} else {
			window.location.href = url +"?fonds="+fonds+"&q="+ recherche +"&page="+ cible ;
		}
	}
}

function popup(page,nom) {
	var option = 'width=700,height=480,scrollbars=yes,menubar=no,status=no,resizable=yes' ;
	var fenetre = open(page,nom,option) ;
	if(fenetre.closed == false) {
			fenetre.focus();
        }
        else{
			window.fenetre;
        }
}

function Myadmin() {
	if(navigator.appName == "Netscape") {
			var hauteur = window.innerHeight; 
			var largeur = window.innerWidth;
		}
        else {
        	var hauteur = document.body.offsetHeight; 
			var largeur = document.body.offsetWidth;
		}
	var page = 'admin/index.php' ;
	var nom = 'photo_administration' ;
	var option = 'width='+largeur+',height='+hauteur+',scrollbars=yes,menubar=no,status=no,resizable=yes' ;
	var fenetre = open(page,nom,option) ;
	if(fenetre.closed == false) {
			fenetre.focus();
        }
        else{
			window.fenetre;
        }
}

function array_search(what, where) {
	var index_du_tableau = -1 ;
 	for(elt in where) {
		index_du_tableau++;
		if (where[elt]==what){
			return index_du_tableau
		}
	}
	index_du_tableau=-1;
	return index_du_tableau
}

/** Sur documents */

function changerCSS_select(id) {
	var doc = document.getElementById(id) ;
	if(doc != null) {
	doc.className = "doc_selected";
	}
}

function changerCSS_unselect(id) {
	var doc = document.getElementById(id) ;
	if(doc != null) {
	doc.className = "doc";	
	}
}

function changerCSS_masquer(id) {
	var doc = document.getElementById(id) ;
	if(doc != null) {
	doc.className = "doc_masquer";	
	}
}

function download(id) {
	var page = 'download.php?document='+id ;
	var nom = 'download' ;
	var option = 'width=680,height=450,scrollbars=yes,menubar=no,status=no,resizable=yes' ;
	var fenetre = open(page,nom,option) ;
	if(fenetre.closed == false) {
			fenetre.focus();
        }
        else{
			window.fenetre;
        }
}

function selectionner(id,cadre) {
	request('panier.php?action=ajout&fonds=images&record='+id,cadre) ;
	changerCSS_select('photo_'+id) ;
}

function deselectionner(id,cadre) {
	request('panier.php?action=supprime&fonds=images&record='+id,cadre) ;
	changerCSS_masquer('photo_'+id) ;
	setTimeout(compter_selection,250);
}

function compter_selection() {
	if(document.getElementById('panier_refs')) {
		var selection = document.forms["panier_selection"].elements["panier_refs"].value ;
		if(selection == null) {
		maj_compteur = 0 ;
		}
		if(selection.length > 0) {
		tab_selection = selection.split(';') ;
		nb_selection = tab_selection.length ;
		maj_compteur = parseInt(nb_selection) ;
		}
	} else {
		maj_compteur = 0 ;	
	}
	/*
	var myDiv = document.getElementById("compteur");
	myDiv.appendChild(document.createTextNode(maj_compteur));
	*/
	document.getElementById('compteur').innerHTML = maj_compteur;
}

function valider_action_panier(action) {
	if(document.getElementById('panier_refs')) {
		var lselection = document.forms["panier_selection"].elements["panier_refs"].value ;
		if(lselection.length > 0) {
			var string_selection = lselection ;
			switch(action) {
				case 'download' :	
					url = 'download.php?document='+string_selection ;
					popup(url, 'download');
				break ;
				case 'group' :	
					url = 'panier_enregistrement.php' ;
					popup(url, 'enregistrement');
				break ;
				case 'empty' :	
					tab_selection = lselection.split(';') ;
					var n = tab_selection.length ;
					for (i=0; i<n; i++) {
						//alert(document.URL) ;
						changerCSS_unselect('photo_'+tab_selection[i]) ;
					}
					request('panier.php?action=initialise&fonds=images','panier_selection') ;
				break ;
			}
		}
	} else {
		alert("Aucun media dans le panier");
	}
}

function vider_selection() {
	/* fonction propore à la page selection.php */ 
	if(document.getElementById('panier_refs')) {
		var selection = document.forms["panier_selection"].elements["panier_refs"].value ;
		if(selection.length > 0) {
			var string_selection = selection ;
			tab_selection = selection.split(';') ;
			var n = tab_selection.length ;
			for (i=0; i<n; i++) {
				changerCSS_masquer('photo_'+tab_selection[i]) ;
			}
		}
		request('panier.php?action=initialise&fonds=images','panier_selection') ;
		setTimeout(compter_selection,250);
	}
}

function dossier_download() {
	if(document.getElementById('dossier_refs')) {
		var liste_selection = document.forms["dossier_selection"].elements["dossier_refs"].value ;
		if(liste_selection.length > 0) {
			var string_selection = liste_selection ;
			url = 'download.php?document='+string_selection ;
			popup(url, 'download');
		}
	}
}

function dossier_effacer(id) {
	if  (confirm("Voulez-vous supprimer ce dossier ?")) {
		request('dossier_liste.php?delete='+id,'dossier_selection') ;
		request('dossier_sommaire.php','dossier_zone_aff') ;
	}
}

function members_only(langue) {
	switch(langue) {
		default :
		case 'fr' :
			alert('Pour bénéficier de cette fonctionnalité, vous devez vous identifier') ;
		break ;
		case 'en' :
			alert('To take advantage of this feature, you must be logged') ;
		break ;
	}
}

function demande_hd(cueid,set){
	//var lien = "devis.php?action=create&f_list="+cueid+"&set="+set ; /*mettre sur les id */	
	//OuvrirPopup(lien,'devis','width=800,height=600,scrollbars=yes,menubar=no,statut=no,resizable=yes');
	var page = "demande_hd.php?action=create&recordids="+cueid+"&set="+set ; /*mettre sur les id */
	var nom = 'popup' ;
	var option = 'width=800,height=530,location:no,scrollbars=yes,menubar=no,status=no,resizable=yes' ;
	var fenetre = open(page,nom,option) ;
	if(fenetre.closed == false) {
			fenetre.focus();
        }
        else{
			window.fenetre;
        }
}


