/*** functions generales ***/
function runfile(fichier) 
     { 
     if(window.XMLHttpRequest) // FIREFOX 
          xhr_object = new XMLHttpRequest(); 
     else if(window.ActiveXObject) // IE 
          xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
     else 
          return(false); 
     xhr_object.open("GET", fichier, false); 
     xhr_object.send(null); 
     if(xhr_object.readyState == 4) {
        return(xhr_object.responseText);
        } else {
        return(false);
        } 
     }
function nbCaractere(champ){
        var contenu = champ.value;
        var nb = contenu.length;
        var reste = 255 - nb;
        document.getElementById("nbCaractere").innerHTML = reste + " caractères";
        if( reste <= 0 ){
                document.getElementById("commentaire").value = contenu.substr(0,255);
        }
}
function GO(url){
        window.location.href=url;
}
     
function sendCommentaire(user_id){
    var commentaire = document.getElementById("commentaire").value;
    //alert("/ajax/question.php?productId="+escape(productId)+"&message="+ escape(message));
    var retour = runfile("http://www.lenoctambule.com/ajax/commentaire.php?user_id="+user_id+"&commentaire="+ escape(commentaire));
    //alert(retour);
    document.getElementById("ajoutCommentaire").innerHTML = "Votre commentaire a bien été enregistré.";
    afficheDiv("coms");
    document.getElementById("coms").innerHTML = retour;
}
function sendCommentaireAgenda(idAgenda){
    var commentaire = document.getElementById("commentaire").value;
    //alert("/ajax/question.php?productId="+escape(productId)+"&message="+ escape(message));
    var retour = runfile("http://www.lenoctambule.com/ajax/commentaireAgenda.php?idAgenda="+idAgenda+"&commentaire="+ escape(commentaire));
    //alert(retour);
    //afficheDiv("coms");
    document.getElementById("coms").innerHTML = retour;
    document.getElementById("ajoutCommentaire").innerHTML = "Votre commentaire a bien été enregistré.";
}
function sendCommentaireNews(idNews){
    var commentaire = document.getElementById("commentaire").value;
    //alert("/ajax/question.php?productId="+escape(productId)+"&message="+ escape(message));
    var retour = runfile("http://www.lenoctambule.com/ajax/commentaireNews.php?idNews="+idNews+"&commentaire="+ escape(commentaire));
    //alert(retour);
    //afficheDiv("coms");
    document.getElementById("liste_commentaire").innerHTML = retour;
    document.getElementById("ajoutCommentaire").innerHTML = "Votre commentaire a bien été enregistré.";
}
function sendCommentaireEtablissement(idEtablissement){
    var commentaire = document.getElementById("commentaire").value;
    //alert("/ajax/question.php?productId="+escape(productId)+"&message="+ escape(message));
    var retour = runfile("http://www.lenoctambule.com/ajax/commentaireEtablissement.php?idEtablissement="+idEtablissement+"&commentaire="+ escape(commentaire));
    //alert(retour);
    document.getElementById("ajoutCommentaire").innerHTML = "Votre commentaire a bien été enregistré.";
    afficheDiv("coms");
    document.getElementById("coms").innerHTML = retour;
}
function deleteCommentaireEtablissement(idCommentaire){
    //alert("/ajax/question.php?productId="+escape(productId)+"&message="+ escape(message));
    var retour = runfile("http://www.lenoctambule.com/ajax/deleteCommentaireEtablissement.php?idCommentaire="+ escape(idCommentaire));
    document.getElementById("coms").innerHTML = retour;
}
function deleteCommentaireAgenda(idCommentaire){
    //alert("/ajax/question.php?productId="+escape(productId)+"&message="+ escape(message));
    var retour = runfile("http://www.lenoctambule.com/ajax/deleteCommentaireAgenda.php?idCommentaire="+ escape(idCommentaire));
    //alert(retour);
    document.getElementById("coms").innerHTML = retour;
}
function deleteCommentaireNews(idCommentaire){
    //alert("/ajax/question.php?productId="+escape(productId)+"&message="+ escape(message));
    var retour = runfile("http://www.lenoctambule.com/ajax/deleteCommentaireNews.php?idCommentaire="+ escape(idCommentaire));
    //alert(retour);
    document.getElementById("liste_commentaire").innerHTML = retour;
}
function signalerAbusAgenda(idCommentaire){
    //alert("/ajax/question.php?productId="+escape(productId)+"&message="+ escape(message));
    var retour = runfile("http://www.lenoctambule.com/ajax/signalerAbusAgenda.php?idCommentaire="+ escape(idCommentaire));
    //alert(retour);
    document.getElementById("abus-"+idCommentaire).innerHTML = "Abus signalé à l'administrateur";
}
function signalerAbusNews(idCommentaire){
    //alert("/ajax/question.php?productId="+escape(productId)+"&message="+ escape(message));
    var retour = runfile("http://www.lenoctambule.com/ajax/signalerAbusNews.php?idCommentaire="+ escape(idCommentaire));
    //alert(retour);
    document.getElementById("abus-"+idCommentaire).innerHTML = "Abus signalé à l'administrateur";
}
function deleteCommentaire(idCommentaire){
    //alert("/ajax/question.php?productId="+escape(productId)+"&message="+ escape(message));
    var retour = runfile("http://www.lenoctambule.com/ajax/deleteCommentaire.php?idCommentaire="+ escape(idCommentaire));
    document.getElementById("coms").innerHTML = retour;
}

                function changePhoto(newPhoto, titre){
                        document.getElementById("zoomPhoto").src = newPhoto;
                        document.getElementById("titrePhoto").innerHTML = titre;
                }
    function displayDiv(division,modeDisplay) {
        if(document.getElementById(division)!=null) {
            document.getElementById(division).style.display = modeDisplay;
        }
    }
    
    
    function afficheDiv(division) {
        if(document.getElementById(division)!=null) {
                if(document.getElementById(division).style.display == "none" ) {
                        document.getElementById(division).style.display = "block";
                } else {
                        document.getElementById(division).style.display = "none";
                }
        }
    }

function dateSlashAuto(champ) {
	var contenu = champ.value;
	//eliminer les slash en doubles!!
	contenu = contenu.replace(/(\/\/)/, "");
	if(contenu.length == 2 || contenu.length == 5) {
		contenu += "/";
		champ.value = contenu;
	}
}
function popup(page,nom,largeur,hauteur,options) {
	
    if(largeur == "100%") {
        var top = 0;
        var left = 0;
        largeur = screen.width;
        hauteur = screen.height;
    } else {
        var top=(screen.height-hauteur)/2;
    	var left=(screen.width-largeur)/2;    
    }
	
	window.open(page,nom,"top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}
    function displayDiv(division,modeDisplay) {
        if(document.getElementById(division)!=null) {
            document.getElementById(division).style.display = modeDisplay;
        }
    }
    
function zoomImage(source) {
    popup(source,'zoom','700','500','resizable=1,scrollbars=1');
}    

/*****utils*****/
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }

    return true;
}

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

/*** recherche ***/
function valideRechercheForm(formulaire) {
	formulaire.mots.value = formulaire.mots.value.trim();
	if(formulaire.mots.value=='') {
		alert('Veuillez indiquer un mot pour votre recherche !');
		formulaire.mots.focus();
        return false;	
	} else {
		if(formulaire.mots.value.length<=2) {
			alert('Veuillez indiquer un mot de plus de 2 lettres !');
			formulaire.mots.focus();
            return false;	
		}
	}
	return true;	
}

/*** required (formulaire complet) + champs aux formats speciaux (email,date) ***/
function validateAllForm(formulaire) {
        
    if(validateEmail(formulaire)==false) {
        return false;
    }
    if(validateDate(formulaire)==false) {
        return false;
    }


    var alerte = 0;
    for(f=0;f<formulaire.elements.length;f++) {
        if(formulaire.elements[f].value=='') {
            alerte++;
            break;
        }
    }

    
    if(alerte>0) {
		alert('Merci de remplir tous les champs !');
		if ((formulaire.elements[f].type == "radio") || (formulaire.elements[f].type == "select") || (formulaire.elements[f].type == "text") || (formulaire.elements[f].type == "textarea")) {
		  formulaire.elements[f].focus();
		}

		return false;   
    }
    return true;
    
}
/***validations***/
/***champs required + formats speciaux ***/
function validateRequiredForm(formulaire) {
    
    if(validateEmail(formulaire)==false) {
        return false;
    }
    if(validateDate(formulaire)==false) {
        return false;
    }


    var alerte = 0;
    for(f=0;f<formulaire.elements.length;f++) {
        var thisAttribut = formulaire.elements[f].getAttribute("required");
        if((thisAttribut!='')&&(thisAttribut!=null)) {
            if(formulaire.elements[f].value=='') {
                alert('Merci de remplir le champ "'+thisAttribut+'"');
                formulaire.elements[f].focus();
		return false;   
                break;
            }
        }
    }
    return true;
}


/*****date*****/

function validateDate(formulaire) {
    var alerte = 0;
    for(f=0;f<formulaire.elements.length;f++) {
        var thisAttribut = formulaire.elements[f].getAttribute("format");
        if(thisAttribut=='date' && thisAttribut!=null) {
            tabDate = formulaire.elements[f].value.split('/');
            if(formulaire.elements[f].value!='') {
                if(tabDate.length!=3) {
                    alerte++;
                    break;                
                } else {
                    if(tabDate[2]!='0000' && isValidDate(tabDate[0], tabDate[1], tabDate[2])==false) {
                        alerte++;
                        break;
                    }                
                }
            
            }

        }
    }
      
    if(alerte>0) {
		alert('Merci de respecter le format des dates !');
		if ((formulaire.elements[f].type == "radio") || (formulaire.elements[f].type == "select") || (formulaire.elements[f].type == "text") || (formulaire.elements[f].type == "textarea")) {
            formulaire.elements[f].focus();
		}
        
		return false;   
    }
    return true;
    
}

function isValidDate(day, month, year) {
	    if(day=='' || month=='' || year=='' || day=='undefined' || month=='undefined' || year=='undefined') {
            return false;
        } else {
            if (month < 1 || month > 12) {
                return false;
            }
            if (day < 1 || day > 31) {
                return false;
            }
            if ((month == 4 || month == 6 || month == 9 || month == 11) &&
                (day == 31)) {
                return false;
            }
            if (month == 2) {
                var leap = (year % 4 == 0 &&
                   (year % 100 != 0 || year % 400 == 0));
                if (day>29 || (day == 29 && !leap)) {
                    return false;
                }
            }        
        }
        

        return true;
}

/***email***/
function validateEmail(formulaire) {
    var alerte = 0;
    for(f=0;f<formulaire.elements.length;f++) {
        var thisAttribut = formulaire.elements[f].getAttribute("format");
        if(thisAttribut=='email' && thisAttribut!=null) {
            if(formulaire.elements[f].value!='') {
                if(checkEmail(formulaire.elements[f].value)==false) {
                        alerte++;
                        break;
                }                
            }

        }
    }
      
    if(alerte>0) {
		alert('Merci de respecter le format des e-mails !');
		if ((formulaire.elements[f].type == "text") || (formulaire.elements[f].type == "textarea")) {
            formulaire.elements[f].focus();
		}
        
		return false;   
    }
    return true;
    
}

function checkEmail(emailStr) {
       if (emailStr.length == 0) {
           return true;
       }
       var emailPat=/^(.+)@(.+)$/;
       var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
       var validChars="\[^\\s" + specialChars + "\]";
       var quotedUser="(\"[^\"]*\")";
       var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
       var atom=validChars + '+';
       var word="(" + atom + "|" + quotedUser + ")";
       var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
       var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
       var matchArray=emailStr.match(emailPat);
       if (matchArray == null) {
           return false;
       }
       var user=matchArray[1];
       var domain=matchArray[2];
       if (user.match(userPat) == null) {
           return false;
       }
       var IPArray = domain.match(ipDomainPat);
       if (IPArray != null) {
           for (var i = 1; i <= 4; i++) {
              if (IPArray[i] > 255) {
                 return false;
              }
           }
           return true;
       }
       var domainArray=domain.match(domainPat);
       if (domainArray == null) {
           return false;
       }
       var atomPat=new RegExp(atom,"g");
       var domArr=domain.match(atomPat);
       var len=domArr.length;
       if ((domArr[domArr.length-1].length < 2) ||
           (domArr[domArr.length-1].length > 4)) {
           return false;
       }
       if (len < 2) {
           return false;
       }
       return true;
    }
    
/* A FAIRE
float
*/    

/***photo detail**/
function viewPhotoDetail(photo,division) {
    document.getElementById(division).innerHTML = "<p align=\"right\"><a href=\"javascript:void(0)\" onclick=\"displayDiv('"+division+"','none')\">[X]</a></p><p align=\"center\"><img src=\""+photo+"\"/></p>";
    document.getElementById(division).style.display = "block";
}
var ie = (document.all)? true:false;
var ns4 = (document.layers)? true:false;
var ns6 = (document.getElementById)? true:false;

function creerObjet() {
    var obj = null;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        try {
            obj = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (E){
            try {
                obj = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e){
                obj = new XMLHttpRequest();
            }
        }
    }else{
        obj = new XMLHttpRequest();
    }
    if(obj==null){
        alert('Votre navigateur ne supporte pas les objets XMLHTTPRequest...'); 
    }
    return obj;
}

/*var http = creerObjet();*/

function dc(){
    var http = creerObjet();
    var url = "deconnecte.php?nid="+Math.random();
        http.open('GET', url , true);
        http.onreadystatechange = function deco(){
           if(http.readyState == 4){}
        };
        http.send(null);  
}

function loginAction(){
    var identifiant = document.loginForm.username.value;
    var motdepasse = document.loginForm.password.value;
    var sid = document.loginForm.sid.value;
    var redirect = document.loginForm.redirect.value;
    var ext = document.loginForm.ext.value;
    var login = document.loginForm.login.value;
    var autologin = document.loginForm.autologin.value;

    var newPassword = MD5(motdepasse);
    var url = 'identification.php'+ '?sid=' + escape(sid)+ '&username=' + escape(identifiant)+ '&password=' + escape(newPassword);
    url += '&login=' + escape(login) + '&redirect=' + escape(redirect)+'&ext=' + escape(ext)+'&autologin=' + escape(autologin);
    //document.write(url);
        http.open('POST', url, true);
        http.onreadystatechange = function retour(){
           if(http.readyState == 4){
                var text = http.responseText;
                if(text!=""){
                        window.location.href="http://www.lenoctambule.com/web/index2.php";
                        /*user = text.split("|");
                        contenu = "<table border=\"1\" height=\"100\"><tr><td class=\"titre\">Bienvenue "+user[0]+"</td></tr>";
                        contenu += "<tr><td><a class=\"text\" href=\"modifierProfil.php\">Votre profil</a></td></table>";
                        contenu = "<table><tr><td class=\"text\">Bienvenue "+user[0]+"</td></tr>";
                        contenu +="<tr><td class=\"lien\"><a class=\"lien\" href=\"modifierProfil.php\">Votre profil</a></td></tr>";
                        contenu +="<tr><td class=\"lien\"><a class=\"lien\" href=\"javascript:void(0);\" onClick=\"popup('photoMembre.php','PHOTOSMEMBRES','400','450','scrollbars=1');\">Vos photos</a></td></tr>";
                        if(user[1]!="0" && user[1]!=""){
                                contenu +="<tr><td><a class=\"lien\" href=\"etablissement/index.php\">Votre établissement</a></td></tr>";
                        }
                        if(user[2]!="0" && user[2]!=""){
                                contenu +="<tr><td><a class=\"lien\" href=\"javascript:void(0);\" onClick=\"popup('forum/privmsg.php?folder=inbox','FORUM','780','600','scrollbars=1');\">Nouveau message : "+user[2]+"</a></td></tr>";
                        } else {
                                contenu +="<tr><td><a class=\"lien\" href=\"javascript:void(0);\" onClick=\"popup('forum/privmsg.php?folder=inbox','FORUM','780','600','scrollbars=1');\">Nouveau message : 0</a></td></tr>";
                        }
                        contenu +="<tr><td class=\"lien\"><a class=\"lien\" href=\"javascript:void(0);\" onClick=\"deconnexion();\">Déconnexion</a></td></tr>";
                        contenu +="</table>";
                        document.getElementById("login").innerHTML = contenu;*/
                } else {
                        alert('Login/mot de passe incorrect ou compte non validé, veuillez recommencer.');       
                }
            }
        };
        http.send(null);
}
function checkUsername(username){
        var url = "checkUsername.php?username="+username;
        var http = creerObjet();
        http.open('GET', url , true);
        http.onreadystatechange = function verification(){
           if(http.readyState == 4){
                var text = http.responseText;
                if(text=="1"){
                    contenu = "<table><tr><td class=\"text\"><font color=\"#FF0000\" size=\"1\">Ce pseudo existe déjà.</font></td></tr></table>";
                    document.getElementById("checking").innerHTML=contenu;
                    document.getElementById("checking").display="block";
                } else {
                    document.getElementById("checking").innerHTML="";
                    document.getElementById("checking").display="none";
                    document.getElementById("username").value=text;
                }
            }
        };
        http.send(null);
}
function deconnexion(){
    var url = "deconnexion.php";
        http.open('GET', url , true);
        http.onreadystatechange = function deco(){
           if(http.readyState == 4){
                    contenu = "<form name=\"loginForm\" ><table height=\"100\"><tr><td>";
                    contenu +="<input type=\"text\" size=\"15\" class=\"login\" name=\"username\" /><br/>";
                    contenu +="<input type=\"password\" size=\"15\" class=\"login\" name=\"password\" />";
                    contenu +="<input type=\"hidden\" name=\"autologin\" value=\"1\"/>";
                    contenu +="<input type=\"hidden\" name=\"sid\" value=\"\" />";
                    contenu +="<input type=\"hidden\" name=\"redirect\" value=\"../index.php\" />";
                    contenu +="<input type=\"hidden\" name=\"ext\" value=\"1\" /> ";
                    contenu +="<input type=\"button\" class=\"login\" onClick=\"loginAction();\" name=\"login\" value=\"Connexion\" /> ";
                    contenu +="</td></tr>";
                    contenu +="<tr><td>";
                    contenu +="<a class=\"lien\" href=\"javascript:void(0);\" onClick=\"popup('forum/profile.php?mode=sendpassword','FORUM','780','600','scrollbars=1,resizable=1');\">Mot de passe perdu ?</a>";
                    contenu +="</td></tr>";
                    contenu +="<tr><td>";
                    contenu +="<a href=\"inscription.php\" target=\"principal\" class=\"lien\">Pas encore inscrit ?</a>";
                    contenu +="</td></tr></table></form>";
                    document.getElementById("login").innerHTML=contenu;
            }
        };
        http.send(null);
}
function clean(d){
        var bal=d.getElementsByTagName('*');
        for(i=0;i<bal.length;i++){
                a=bal[i].previousSibling;
                if(a && a.nodeType==3)
                        go(a);
                b=bal[i].nextSibling;
                if(b && b.nodeType==3)
                        go(b);
        }
        return d;
}