﻿function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function modifierConfig() {
	OuvrirFenetre("lib_php/edit_config.php", 540,675,0,0,0,0,0);
}
function ajouterPage(idParent) {
	OuvrirFenetre("lib_php/ajouter_page.php?s=" + idParent, 190,440,0,0,0,0,0);
}
function ajouterSousPage(idSection, idParent) {
	OuvrirFenetre("lib_php/ajouter_souspage.php?s=" + idSection + "&p=" + idParent, 190,440,0,0,0,0,0);
}
function modifierPage(id) {
	OuvrirFenetre("lib_php/edit_page.php?p=" + id, 555,630,0,0,0,0,0);
}
function classerPage(idPage, sens) {
	//sens = 1: UP; sens = 0: DOWN
	OuvrirFenetre("lib_php/classer_page.php?ss=" + idPage + "&sens=" + sens, 140,440,0,0,0,0,0);
}
function supprimerPage(idPage) {
	OuvrirFenetre("lib_php/supprimer_page.php?ss=" + idPage, 140,440,0,0,0,0,0);
}
function activerPage(id, Etat) {
	OuvrirFenetre("lib_php/activer_page.php?p=" + id + "&etat=" + Etat, 140,440,0,0,0,0,0);
}

function ajouterFicheADMIN(idParent) {
	OuvrirFenetre("ajouter_fiche.php?page=" + idParent, 190,440,0,0,0,0,0);
}
function modifierFicheADMIN(idFiche) {
	OuvrirFenetre("edit_fiche.php?f=" + idFiche, 540,675,0,0,0,0,0);
}
function modifierGallerie(idFiche) {
	OuvrirFenetre("lib_php/edit_gallerie_fiche.php?f=" + idFiche, 270,560,0,0,0,0,0);
}
function supprimerFicheADMIN(idFiche) {
	OuvrirFenetre("supprimer_fiche.php?ss=" + idFiche, 140,440,0,0,0,0,0);
}
function activerFicheADMIN(idFiche, Etat) {
	OuvrirFenetre("activer_fiche.php?ss=" + idFiche + "&etat=" + Etat, 140,440,0,0,0,0,0);
}
function ajouterFicheADMIN() {
	OuvrirFenetre("ajouter_fiche.php?page=0", 190,440,0,0,0,0,0);
}
function OuvrirFenetre(nom,hauteur,largeur, menu, dim, barsScr, barsStat, barstool) {
	var texte = "height=" + hauteur + ",width=" + largeur + ",menubar=" + menu + ",resizable=" + dim + ",scrollbars=" + barsScr + ",status=" + barsStat + ",toolbar=" + barstool;
	var ecranL=screen.width;
	var ecranH=screen.height;
	
	if(hauteur == 0) {
   		hauteur = (ecranH-80);
   		deplaceH = 0;
   	} else
   		deplaceH=((ecranH/2)-(hauteur/2));  

	if(largeur == 0)
   		largeur = (ecranL-80);
   	
	var deplaceL=((ecranL/2)-(largeur/2));	
	
	//var texte = "height=" + hauteur + ",width=" + largeur + ",menubar=" + MonMenu + ",resizable=" + Madim + ",scrollbars=" + barsScr + ",status=" + barsStat + ",toolbar=" + barstool;
	childWin=window.open(nom, "", texte);
   
	if (childWin.opener == null) childWin.opener = self;

	childWin.moveTo(deplaceL, deplaceH);
	childOpen = true;
	childWin.focus();
	return;
}

/*--------------------------------------------------------------------------------------
	AJAX
--------------------------------------------------------------------------------------*/

var req;

function loadXMLDoc(serverFileName, variableNames, variableValues) {//(url)
	
	var paramString = '';
	
	variableNames = variableNames.split('|');
	variableValues = variableValues.split('|');
	
	for(i=0; i<variableNames.length; i++) {
		paramString += variableNames[i]+'='+variableValues[i]+'&';
	}
	paramString = paramString.substring(0, (paramString.length-1));
	
	if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
        
    if (paramString.length == 0) {
	   	req.open('post', serverFileName);
	}
	else {
		req.open('post', serverFileName+'?'+paramString);
	}
	
    req.onreadystatechange = processReqChange;
    //ajout pour post
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.overrideMimeType('text/xml');
    req.send(paramString);
    	
}

function processReqChange() {
    // only if req shows "complete"
    
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
        	// ...processing statements go here...
        	//alert(req.responseText + "::" + req.responseXML);
          if(req.responseXML.documentElement != null) {
      			response  = req.responseXML.documentElement;
      			method    = response.getElementsByTagName('method')[0].firstChild.data;
     				result    = response.getElementsByTagName('result')[0].firstChild.data;
     				array_result = result.split("|");
      			//*
      			switch (array_result[0]) {
      				case 'OK':
      					show_login('none');
      					document.location.href = document.FormPosition.adressePage.value;//reload en mode admin?!?
      					break;
      				case 'KO':
      					document.location.href = document.FormPosition.adressePage.value;//reload en mode admin?!?
      					break;       				
      				default:
      					//alert(array_result[0] + "::" + array_result[1]);
      					document.formLogin.pass.value = "";	
      			}
      			//*/	
      		}
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function LoginAdmin() {

    url  = 'lib_php/login_ajax.php';
    
    variableNames = "name|pass";
    variableValues = document.formLogin.name.value + "|" + document.formLogin.pass.value;
    loadXMLDoc(url,variableNames,variableValues);
}

function deloginAdmin() {
	url  = 'lib_php/login_ajax.php';
    
    variableNames = "name|pass";
    variableValues = "delog|";
    loadXMLDoc(url,variableNames,variableValues);
}

function show_login(etat) {
	document.getElementById('login').style.display = etat;
}

function gererMenus(idMenu) {
	OuvrirFenetre("lib_php/edit_menu.php?m=" + idMenu, 440,440,0,0,0,0,0);
}
