// -----------------------------------------------------------------------------
// --- Fonctions utilitaires
// -----------------------------------------------------------------------------
    var ie = (document.all)? true:false;
    var ns4 = (document.layers)? true:false;
    var ns6 = (document.getElementById)? true:false;
var interdit = '_*&?!:;,#"%${}<>|\\/`\'+=\t';

function isForbidden(chaine) {

	if (chaine.length <1) {
		return false;
	}
	for(i=0;i<chaine.length;i++){
		if (interdit.indexOf(chaine.charAt(i)) >= 0) {
			return true;
		}
	}
	return false;
}

function isArray(variable) {
		if ((variable=="") || (variable==null)) {
			return false;
		}
		if (ie) {
			return variable.constructor == Array
		} else {
   			var source = variable.toSource();
   			return (source.search(/^\[([^,]*,)+[^,]*\]/) != -1);
   		}
	}

function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	} else{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	} //End While
	return strTemp;
} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;
	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

function VerifMail(a){
	for (var i=1; i<(a.length); i++){
		if (a.charAt(i)=='@'){
			if (i>=2){
				arobace=i;
				for (var j=i; j<(a.length-1); j++){
					if(a.charAt(j)=='.'){
						if ((j>=(arobace+2)) && (j>=(a.length-4)) && (j<=(a.length-3))){return(true);}
					}
				}
			}
			else{return(false);}
		}
	}
}

function is_numeric(num) {
	var exp = new RegExp("^[0-9-.]*$","g");
	return exp.test(num);
}

// --- Popup a new window with the specified page, width and height
function Pop(page,width,height) {

	if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
    else
            var xMax = 800, yMax=600;
    var xOffset = (xMax - width)/2, yOffset = (yMax - height)/2;
	mywin=window.open(page,'pop','resizable=no, scrollbars=yes, location=no, status=no, toolbar=no, menubar=no, width='+width+', height='+height+', left='+xOffset+', top='+yOffset+'');
	mywin.focus();
}

// TO BE DELETED NORMALLY
function PopUpload() {

	if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
    else
            var xMax = 800, yMax=600;
    var xOffset = (xMax - 600)/2, yOffset = (yMax - 370)/2;
	mywin=window.open('upload.html','upload','resizable=no, scrollbars=no, location=no, status=no, toolbar=no, menubar=no, width=600, height=370, left='+xOffset+', top='+yOffset+'');
	mywin.focus();
}

function OpenFullWindow(page) {
	if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
    else
            var xMax = 800, yMax=600;
    xMax=xMax-10;yMax=yMax-10;
	mywin=window.open(page,'pop','resizable=yes, scrollbars=yes, location=no, status=yes, toolbar=yes, menubar=yes, width='+xMax+', height='+yMax+', left=0, top=0');
	mywin.focus();
}

function PopDocument(page) {
	if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
    else
            var xMax = 800, yMax=600;
    xMax=xMax-10;yMax=yMax-10;
	mywin=window.open(page,'popdoc','resizable=yes, scrollbars=yes, location=no, status=no, toolbar=no, menubar=no, width='+xMax+', height='+yMax+', left=0, top=0');
	mywin.focus();
}

function PopTemplate() {

	if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
    else
            var xMax = 800, yMax=600;
    var xOffset = (xMax - 600)/2, yOffset = (yMax - 500)/2;
	mywin=window.open('add2.html','pop','resizable=no, scrollbars=yes, location=no, status=no, toolbar=no, menubar=no, width=600, height=500, left='+xOffset+', top='+yOffset+'');
	mywin.focus();
}

function MenuReload(){
	top.left.menu.document.location.href='menu.php';
}
// -----------------------------------------------------------------------------
// --- Fonctions de détection de resize des frames
// -----------------------------------------------------------------------------
    FramesSize= new Array(50);
    // Valeur du tableau FramesHeight :
	// 0=Hauteur page principale simple
	// 1=Largeur menu
	// 2=Largeur help
	// 3= undefined
	// 4=undefined
	// 5=undefined
	// 6=hauteur fonction 1
	// 7=hauteur fonction 6 ...
	// si c une frame double, =[hauteur frameliste],[hauteur pageprinc]

	// By default fonctionId=0, Only the double frame fonction has a fonction id
	FonctionId=0;

	// Etat de la frame détails
	DETAILS_CLOSE=0;
	DETAILS_BIG=1;
	DETAILS_MIDDLE=2;
	DETAILS_SMALL=3;
	FrameDetailStatus=DETAILS_CLOSE;

	// --- Initialisation des tailles de frame
	function InitFramesSize(){
		FonctionId=parseInt(FonctionId);
		SizeUpdated=false;
		CookieValue=GetFrameCookie();
		if (CookieValue == null) {
			// frame cookie not exist
			FramesSize[0]=parent.parent.basefrm.document.body.clientHeight;
			FramesSize[1]=230;
			FramesSize[2]=-1;
			FramesSize[3]=-1;
			FramesSize[4]=-1;
			FramesSize[5]=-1;
			for (i=6;i<50;i++)
				FramesSize[i]=-1;
			SizeUpdated=true;
		} else {
			FramesSize=CookieValue.split("#");
			for (i=6;i<50;i++) {
				if (FramesSize[i].indexOf(',') != -1) {
					Temp=FramesSize[i].split(',');
					FramesSize[i]= new Array(Temp[0],Temp[1]);
				}
			}
		}
		// Give the registered size to each frame
		// menu frame
		top.document.getElementById("topmenu").cols=FramesSize[1]+",*";
		// Page principale
		if (parent.parent.basefrm.document.body.clientHeight != FramesSize[0]) {
			FramesSize[0]=parent.parent.basefrm.document.body.clientHeight;
			SizeUpdated=true;
		}
		if (parent.frames[0].name=="frameliste") {
			// if the frame is not registered as a double frame, register it, splitting the screen in 2
			if (!isArray(FramesSize[FonctionId+5])) {
				FramesSize[FonctionId+5]=new Array(Math.round(FramesSize[0]/2),FramesSize[0]);
				SizeUpdated=true;
			}
			// don't update the physical size because the details frame is initialy closed
		} else {
			// simple frame
			// if the page was previously a double frame, delete the registered sizes
			if (isArray(FramesSize[FonctionId+5])) {
				FramesSize[FonctionId+5]=-1;
				SizeUpdated=true;
			}
		}
		// upate the cookie if needed
		if (SizeUpdated == true) {
			RegisterFramesSize();
		}
	}

	function RegisterFramesSize(){
		// Write in one cookie value, each Frames size separated by a "#"
		CookieValue=FramesSize[0];
		for (i=1;i<50;i++) {
			if (isArray(FramesSize[i])) {
				// double frame
				CookieValue=CookieValue+"#"+FramesSize[i][0]+","+FramesSize[i][1];
			} else {
				CookieValue=CookieValue+"#"+FramesSize[i];
			}
		}
		SetFrameCookie(CookieValue);
	}

	function FramePrincResizeDetected(){

		/* Get the size if the different frames */
		SizeUpdated=false;
		// Check the menu Frame width
		if (top.left.menu.document.body.clientWidth != FramesSize[1]) {
			FramesSize[1]=top.left.menu.document.body.clientWidth;
			SizeUpdated=true;
		}
		// Check PagePrinc height
		if (parent.parent.basefrm.document.body.clientHeight != FramesSize[0]) {
			FramesSize[0]=parent.parent.basefrm.document.body.clientHeight;
			SizeUpdated=true;
		}
		// Check a change in a double frame
		if (parent.frames[0].name=="frameliste" && FrameDetailStatus==DETAILS_MIDDLE) {
			if ((parent.frameliste.document.body.clientHeight != FramesSize[FonctionId+5][0]) || (FramesSize[0] != FramesSize[FonctionId+5][1])) {
				FramesSize[FonctionId+5][0]=parent.frameliste.document.body.clientHeight;
				FramesSize[FonctionId+5][1]=FramesSize[0];
				SizeUpdated=true;
			}
		}
		if (SizeUpdated == true) {
			RegisterFramesSize();
		}
	}

	// Lorsqu'on est en framedouble, seul le resize en hauteur de la frame menu
	// permet de détecter un changement de taille de la frame principale
	function MenuHeightResizeDetected(){
		FramePrincResizeDetected();
	}

// -----------------------------------------------------------------------------
// --- Gestion des icones de redimensionnement de la frame details
// -----------------------------------------------------------------------------

	// Double frame resizing which can be requested from the icons of the frame details
	// used only in a frameliste page
	function AutoSize(Action){
		switch (Action) {
  			case DETAILS_CLOSE:
			  	top.basefrm.document.getElementById("framedouble").rows="*,0";
			  	parent.document.getElementById("frameliste").setAttribute('noResize',true);
   			break;
     		case DETAILS_BIG:
     			top.basefrm.document.getElementById("framedouble").rows="50,*";
				parent.document.getElementById("frameliste").setAttribute('noResize',true);
     	    break;
     	    case DETAILS_MIDDLE:
     	    	// Adapt the registered size according to the size of the pageprinc
				top.basefrm.document.getElementById("framedouble").rows=""+Math.round((FramesSize[FonctionId+5][0]/FramesSize[FonctionId+5][1])*FramesSize[0])+",*";
				if (ie) {
					parent.document.getElementById("frameliste").setAttribute('noResize',false);
				} else {
					parent.document.getElementById("frameliste").removeAttribute('noResize');
				}
     	    break;
     	    case DETAILS_SMALL:
     	    	top.basefrm.document.getElementById("framedouble").rows="*,50";
     	    	parent.document.getElementById("frameliste").setAttribute('noResize',true);
     	    break;
     	}
     	FrameDetailStatus=Action;
	}

	// Actions associées aux icones de la frame details
	// Used only in a framedetails page
	function ClickIconDetails(Number){
		// update the clicked icon
		NewIcon=document.getElementById("idetails"+Number);
		NewIcon.src="images/details_"+Number+"_on.gif";
		NewIcon.setAttribute('OnMouseOver',"");
		NewIcon.setAttribute('OnMouseOut',"");
		NewIcon.setAttribute('OnClick',"");
		NewIcon.setAttribute('style',"cursor: default");
		// Restablish the previous icon
		OldIcon=document.getElementById("idetails"+FrameDetailStatus);
		OldIcon.src="images/details_"+FrameDetailStatus+"_off.gif";
		OldIcon.setAttribute('OnMouseOver',"idetails"+FrameDetailStatus+".src='images/details_"+FrameDetailStatus+"_on.gif';");
		OldIcon.setAttribute('OnMouseOut',"idetails"+FrameDetailStatus+".src='images/details_"+FrameDetailStatus+"_off.gif';");
		OldIcon.setAttribute('OnClick',"ClickIconDetails("+FrameDetailStatus+");");
		OldIcon.setAttribute('style',"cursor: pointer");
		// Call the function in the frameliste to update the frames
		parent.frameliste.AutoSize(Number);
		FrameDetailStatus=Number;
	}

// -----------------------------------------------------------------------------
// --- Gestion du cookie pour le dimensionnement des frames
// -----------------------------------------------------------------------------

	function getFrameCookieVal (offset) {
      var endstr = document.cookie.indexOf (";", offset);
      if (endstr == -1)
        endstr = document.cookie.length;
      return unescape(document.cookie.substring(offset, endstr));
    }

    // Function to get the frame cookie values
    function GetFrameCookie () {
      var arg = "FramesSize=";
      var alen = arg.length;
      var clen = document.cookie.length;
      var i = 0;
      while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
          return getFrameCookieVal (j);
    	i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
      }
      return null;
    }


    //  Function to create or update the frame cookie.
    function SetFrameCookie (value) {
    	var expDays = 365;
		var exp = new Date();
		exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

		CookieValue = "FramesSize=" + escape (value)
		+ "; expires=" + exp.toGMTString()
		//+ "; path="
		//+ "; domain=http://"+document.domain;;
		//+ ";"+false;
		document.cookie=CookieValue;
    }

    //  Function to delete the frame size cookie.
    function DeleteFrameCookie () {
      if (GetCookie("FramesSize")) {
        document.cookie = "FramesSize="
		//+ "; path=/"
		//+ "; domain=<? echo $DOMAIN;?>"
		+ "; expires=Thu, 01-Jan-70 00:00:01 GMT";
      }
    }