﻿var ie = /MSIE/.test(navigator.userAgent);
var moz = !ie && navigator.product == "Gecko";
var csch = "/";

/*
if (moz) {
    	HTMLElement.prototype.__defineGetter__("children", function() {
			var arr = new Array(), i = 0, l = this.childNodes.length;
			for ( i = 0; i < l; i++ ) {
				if ( this.childNodes[ i ].nodeType == 1 ) {
					arr.push( this.childNodes[ i ] );
				}
			}
			return arr;
		});
		HTMLElement.prototype.__defineGetter__("firstChild", function() {
			var node = this.childNodes[ 0 ];
			while (node.nodeType != 1) node = node.nextSibling;
			return node;
		});
		HTMLElement.prototype.__defineGetter__("lastChild", function() {
			var node = this.childNodes[ this.childNodes.length - 1 ];
			while (node.nodeType != 1) node = node.previousSibling;
			return node;
		});
}
*/

function deco(Chemin)
{
    window.open(Chemin + 'deconnexion.aspx', 'DecoTS', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,height=250,width=250');
}

/*
function hover(obj){
  if(ie){
    UL = obj.getElementsByTagName('ul');
    if(UL.length > 0){
      sousMenu = UL[0].style;
      if(sousMenu.display == 'none' || sousMenu.display == ''){
        sousMenu.display = 'block';
      }else{
        sousMenu.display = 'none';
      }
    }
  }
}


function setHover(){
  LI = document.getElementById('menu').getElementsByTagName('li');
  for(i=0; i < LI.length; i++){
    LI[i].onmouseover = function(){
      hover(this);
    }
    LI[i].onmouseout = function(){
      hover(this);
    }
  }
}
*/

function valideformbutton(evt, btn)
{
    if (document.all) {
        if (evt.keyCode==13) 
        {
            evt.keyCode=0;
            document.all[btn].click();
        }
    } else if (document.getElementById) {
        if (evt.keyCode==13) 
        {
            document.getElementById(btn).click();
        }
    } else {
    
    }
}


function jnu(u) { 
    window.location = u;
}

function jnutb(u,winname) { 
    window.open(u, winname);
}

function howManyChecked(whichCheckBox)
{
	var _countChecked = 0;
	var err = 0;
	if(document.getElementById(whichCheckBox).checked==true)
	{
		_countChecked++;
	}
	if(_countChecked == 0)
		{ alert('Veuillez lire la charte de bonne conduite puis cochez la case comme quoi vous en accepté les termes');
			err = 1;}
	if (err == 1) { 
	    return false; 
    } else {
        return true;
    }
}

/* Gestion des notes */
var agent = navigator.userAgent.toLowerCase(); 
var AllowOpacity = (agent.indexOf('opera') == -1 )

var DefaultNote = 5;
var AlreadyVote = false;
var LowOpacity = 40;
var MediumOpacity = 60;
var HighDensity = 100;

function Note_event(n) 
{
	try
	{
		for (var i=1; i < 11; i++ )
		{
				SetOpacity(document.getElementById("Note").getElementsByTagName("img")[i - 1], LowOpacity);
        }
        

        if (AlreadyVote)
            n = DefaultNote;
		if (n == null && AlreadyVote == false)
			for (var j=1; j < DefaultNote + 1; j++ )
				SetOpacity(document.getElementById("Note").getElementsByTagName("img")[j - 1], MediumOpacity);
		else
			for (var j=1; j < n + 1; j++ )
				SetOpacity(document.getElementById("Note").getElementsByTagName("img")[j - 1], HighDensity);
	}
	catch(ex)
	{
			//alert("Il y a une erreur : \n Surement à cause du browser qui accepte pas l'opacité \n\n" + ex);
	}
}

function SetOpacity(elmt, opacity)
{
	if(document.all)
	{
		elmt.style.filter = "alpha(opacity=" + opacity + ");"
	}
	else
	{
		elmt.style.setProperty("-moz-opacity", opacity/100, "");
		elmt.style.setProperty("-khtml-opacity", opacity/100, "");
		elmt.style.setProperty("opacity", opacity/100, "");
	}
}

var LAnote = 0;
var LEChemin = "/";
var LECID = 0;
var LL = true;
var TT = "code";

function GiveNote(note, Chemin, CID, L, T)
{
    if (!L)
    {
        alert("Pour donner une note vous devez être authentifié\nVeuillez vous authentifier pour donner votre note");
        LL = false;
        showlogin();
    } else {
        if (!AlreadyVote)
        {
            LAnote = note;
            LEChemin = Chemin;
            LECID = CID;
            if (T == "code")
            {
                SendNote(Chemin + "codevote.aspx?n=" + note + "&id=" + CID + "&r=" + escape((new Date()).getTime()))
            } else {
                SendNote(Chemin + "tutovote.aspx?n=" + note + "&id=" + CID + "&r=" + escape((new Date()).getTime()))
            }
        } else {
            alert("Vous avez déjà voté pour ce code");
        }
    }
}

function SendNote(ReqUrl)
{
    if ( window.XMLHttpRequest ) {
        ReqXhttp = new window.XMLHttpRequest();
	} else if ( window.ActiveXObject ) {
		ReqXhttp = new ActiveXObject( 'Microsoft.XMLHTTP' );
	}
	ReqXhttp.onreadystatechange = function() { SendNoteCallBack(); };
	ReqXhttp.open( 'GET', ReqUrl);
	ReqXhttp.send( null );
}

SendNoteCallBack = function()
{
	if ( ReqXhttp.readyState == 4 ) {
		if ( ReqXhttp.status == 200 ) {
			if (ReqXhttp.responseText != "")
			{
			    if (ReqXhttp.responseText == "1")
			    {
			        AlreadyVote = true;
                    DefaultNote = LAnote;
                    LoadNote(LEChemin,LECID, LL, TT);
                    if (TT == "code")
                    {
                        alert("Vous avez assigné la note de " + LAnote + " sur 10 à cette source");
                    } else {
                        alert("Vous avez assigné la note de " + LAnote + " sur 10 à ce tutorial");
                    }
			    } else {
			        alert("Il y a eu une erreur lors de la notation, veuillez réessayer");
			    }
			}
		}
	}
}

function LoadNote(Chemin, CID, L, T)
{
    LL = L;
    TT = T;
	if (AllowOpacity)
	{
		if (T == "code")
		{
    		document.getElementById("Note").innerHTML = '<b>Donnez une note à cette source : </b><br />'
    	} else {
    	    document.getElementById("Note").innerHTML = '<b>Donnez une note à cet article : </b><br />'
    	}
		for(var i=1; i < 11; i++)
		{
			if (AlreadyVote)
			{
			    if (T == "code")
			    {
				    document.getElementById("Note").innerHTML += '<img src="' + Chemin + 'g/st.gif" alt="' + DefaultNote + ' sur 10" onclick="alert(\' Vous avez déjà vôté pour cette source \')" align="absmiddle" />';
                } else {
                    document.getElementById("Note").innerHTML += '<img src="' + Chemin + 'g/st.gif" alt="' + DefaultNote + ' sur 10" onclick="alert(\' Vous avez déjà vôté pour ce tutorial \')" align="absmiddle" />';
                }
            } else {
                document.getElementById("Note").innerHTML += '<img src="' + Chemin + 'g/st.gif" alt="Assigner la note de ' + i + ' sur 10" onmouseover="Note_event(' + i + ')"  onmouseout="Note_event()" onclick="GiveNote(' + i + ', \'' + Chemin + '\', \'' + CID + '\', ' + L + ', \'' + T + '\')" align="absmiddle" />';
            }
		}
		Note_event();
	}
	else
	{
		document.getElementById("DropDownListNote").options[10 - DefaultNote].selected = true ;
	}
}
/* FIN Gestion des notes */

function validate_Confirm(txt)
{
	if(confirm(txt))
	{
		return true;
	}else{
		alert('Action annulée');
		return false;
	}
}

        // Déclaration des variables 'domaine' et 'date d'expiration'
        var pathname=location.pathname;
        var myDomain=pathname.substring(0,pathname.lastIndexOf('/')) +'/';
        var date_exp = new Date();
        date_exp.setTime(date_exp.getTime()+(365*24*3600*1000)); 
        
        function getCookieVal(offset) {
            var endstr=document.cookie.indexOf (";", offset);
            if (endstr==-1)
                    endstr=document.cookie.length;
            return unescape(document.cookie.substring(offset, endstr));
        }
        function GetCookie(name) {
            var arg= name + "=";
            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 getCookieVal (j);
                            i=document.cookie.indexOf(" ",i)+1;
                                    if (i==0) break;}
            return null;
        }
        function SetCookie (name, value) {
            // un cookie a besoin d'un nom, d'une valeur, d'un nom de domaine, d'une date d'expiration
            //
            var argv=SetCookie.arguments;
            var argc=SetCookie.arguments.length;
            var expires=(argc > 2) ? argv[2] : null;
            var path=(argc > 3) ? argv[3] : null;
            var domain=(argc > 4) ? argv[4] : null;
            var secure=(argc > 5) ? argv[5] : false;
            document.cookie=name+"="+escape(value)+
                ((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
                ((path==null) ? "" : ("; path="+path))+
                ((domain==null) ? "" : ("; domain="+domain))+
                ((secure==true) ? "; secure" : "");
        }