
/************************************/
/***  Signatux ~ Julien VAUBOURG  ***/
/***  ---   www.jsand.net   ---   ***/
/***   Help ? CtrlAltSuppr.com    ***/
/************************************/

function sgtux_ajoutsmiley(smile) {
	var compgen = navigator.userAgent.toLowerCase();
	var navsafari = ((compgen.indexOf('safari') != -1) || (navigator.vfinor == 'Apple'));
	var navie = ((compgen.indexOf('msie') != -1) && (!navopera) && (!navsafari) && (!navwebtv));
	var navopera = (compgen.indexOf('opera') != -1);
	var navwebtv = (compgen.indexOf('webtv') != -1);
	var oswin = ((compgen.indexOf('win') != -1) || (compgen.indexOf('16bit') != -1));
	var navers = parseInt(navigator.appVersion);
	var textarea = document.getElementById('sgtux_message');
	var fermer = false;

	smile = ' '+smile+' ';

	if((navers >= 4) && navie && oswin) {
		if(textarea.isTextEdit) {
			textarea.focus();
			var dselect = document.selection;
			var cr = dselect.createRange(); cr.colapse;
			if((dselect.type == 'Text' || dselect.type == 'None') && cr != null) {
				fermer = true;
				cr.text = smile;
			}
		} else {
			fermer = true;
			textarea.value += smile;
		}
	} else {
		if(textarea.selectionEnd) {
			var selectdebut = textarea.selectionStart;
			var selecthaut = textarea.scrollTop;
			var selectfin = textarea.selectionEnd;

			if(selectfin <= 2)
				selectfin = textarea.textLength;

			var debut = (textarea.value).substring(0, selectdebut);
			var milieu = (textarea.value).substring(selectdebut, selectfin);
			var fin = (textarea.value).substring(selectfin, textarea.textLength);

			if(textarea.selectionEnd - textarea.selectionStart > 0)
				milieu = smile + milieu;
			else {
				milieu = smile + milieu;
				fermer = true;
			}

			textarea.value = debut + milieu + fin;
			var cpos = selectdebut + (milieu.length);
			textarea.selectionStart = cpos;
			textarea.selectionEnd = cpos;
			textarea.scrollTop = selecthaut;
		} else {
			fermer = true;
			textarea.value += smile;
		}

		textarea.focus();
		return fermer;
	}
}

function sgtux_smiliesadd() {
	var smilies = document.getElementById('sgtux_div_smilies');
	var plus = document.getElementById('sgtux_plus');

	if(smilies.style.display == 'block') {
		smilies.style.display = 'none';
		plus.src = plus.src.replace('moins.gif', 'plus.gif');
	} else {
		smilies.style.display = 'block';
		plus.src = plus.src.replace('plus.gif', 'moins.gif');
	}
}

function sgtux_compteur(limite, visible) {
	var textarea = document.getElementById('sgtux_message');
	var compteur = document.getElementById('sgtux_max');

	if(textarea.value.length > limite) {
		textarea.value = textarea.value.substring(0, limite);
		return false;
	}

	var taille = textarea.value.length;
	var difference = ((limite - taille) == -1) ? 0 : limite - taille;

	if(visible)
		compteur.value = difference;
	else
		compteur.value = '';
}

/*
	StarRate
	Script de Olivier PATRY
	http://tutoriels.olivier-patry.net
	Adapté pour Signatux
*/
function sgtux_notation(root) {
	var img;
	var imgs;
	var note;
	root += 'images/';
	var extension = 'png';
	var mySelect = document.getElementById('sgtux_starNote');
	imgs = mySelect.parentNode.getElementsByTagName('img');
	var opt = mySelect.getElementsByTagName('option');

	mySelect.style.display = 'none';

	for(var o = 0; o < opt.length; ++o) {
		img = document.createElement('img');
		img.setAttribute('className', 'sgtux_notation0');
		img.setAttribute('class', 'sgtux_notation0');

		if(o == 0) {
			img.setAttribute('src', root+'notationNone.'+extension);
			img.setAttribute('alt', '[-]');
			img.setAttribute('title', 'Non-noté');
			img.style.cursor = 'pointer';
		} else {
			img.setAttribute('src', root+'notation0.'+extension);
			img.setAttribute('alt', (o+1));
			img.setAttribute('title', opt[o].text+' (sur '+(opt.length-1)+')');
		}
		mySelect.parentNode.appendChild(img);

		if(o != 0) {
			img.onmouseover = function()
			{
				imgs = this.parentNode.getElementsByTagName('img');
				for(var j = 1; j < imgs.length; ++j) {
					bool = j+1<=this.getAttribute('alt');
					imgs[j].setAttribute('src', (bool) ? root+'notation1.'+extension : root+'notation0.'+extension);
					imgs[j].setAttribute('className', (bool) ? 'sgtux_notation1' : 'sgtux_notation0');
					imgs[j].setAttribute('class', (bool) ? 'sgtux_notation1' : 'sgtux_notation0');
				}
			};

			img.onmouseout = function(e)
			{
				imgs = this.parentNode.getElementsByTagName('img');
				if(!e) var e = window.event;
				var tg = (window.event) ? e.srcElement : e.target;
				if(tg.nodeName == 'img') return;
				for(var j = 1; j < imgs.length; ++j) {
					bool = j+1<=note;
					imgs[j].setAttribute('src', (bool) ? root+'notation1.'+extension : root+'notation0.'+extension);
					imgs[j].setAttribute('className', (bool) ? 'sgtux_notation1' : 'sgtux_notation0');
					imgs[j].setAttribute('class', (bool) ? 'sgtux_notation1' : 'sgtux_notation0');
				}
			};
		}

		img.onclick = function()
		{
			imgs = this.parentNode.getElementsByTagName('img');
			for(var j = 1; j < imgs.length; ++j) {
				bool = j+1<=this.getAttribute('alt');
				imgs[j].setAttribute('src', (bool) ? root+'notation1.'+extension : root+'notation0.'+extension);
				imgs[j].setAttribute('className', (bool) ? 'sgtux_notation1' : 'sgtux_notation0');
				imgs[j].setAttribute('class', (bool) ? 'sgtux_notation1' : 'sgtux_notation0');
			}
			note = (this.getAttribute('alt') > 1) ? this.getAttribute('alt') : 1;
			option = this.parentNode.getElementsByTagName('select')[0].getElementsByTagName('option')[note-1];
			option.selected = true;
		};
	}
}

function sgtux_mail(extension, nom, domaine) {
	if(extension == null && domaine == null)
		alert(nom);
	else
		document.location = 'mailto:'+nom+'@'+domaine+'.'+extension;
}

function sgtux_patientez(id) {
	if(document.getElementById && !window.getComputedStyle) {
		document.getElementById(id).style.position = 'absolute';
		window.onscroll = function() {
			var haut = (document.body.scrollTop) ? document.body.scrollTop : document.documentElement.scrollTop;
			document.getElementById(id).style.top = parseInt(haut) + 'px';
		}
	}

	document.getElementById(id).style.display = 'block';
}

function urlencode(texte) {
	texte = texte.replace(/[ ]/g,'+');
	return escape(texte);
}

function sgtux_editLive(id, Action, root, message) {
	var objetAppel = null;
	var rootGet = root+'valide.php?LIVE';

	if(window.XMLHttpRequest) // Firefox
		objetAppel = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
		objetAppel = new ActiveXObject('Microsoft.XMLHTTP');
	else { // XMLHttpRequest non supporté par le navigateur
		alert('Votre navigateur ne supporte pas les objets XMLHttpRequest.');
		return;
	}

	if(Action == 'supprimer') {
		if(confirm(message)) {
			var prochainMessage = document.getElementById('sgtux_editLive_prochain').value;
			objetAppel.open('GET', rootGet+'&action=supprimer&admin=1&id='+id+'&prochainMessage='+prochainMessage, true);
			document.getElementById('sgtux'+id).style.display = 'none';
			objetAppel.onreadystatechange = function() {
				if(objetAppel.readyState < 4)
					sgtux_patientez('sgtux_patientez');

				if(objetAppel.readyState == 4) {
					if(objetAppel.responseText == 'timeout') {
						alert(document.getElementById('sgtux_editLive_deconnexion').value);
						window.location.reload();
					} else {
						document.getElementById('sgtux_patientez').style.display = 'none';

						if(objetAppel.responseText != 0) {
							var retour = objetAppel.responseText.split('|');
							document.getElementById('sgtux_editLive_prochain').value = retour[0];
							var message = objetAppel.responseText.replace(retour[0]+'|', '');
							document.getElementById('sgtux_editLive_messages').innerHTML += message;
						}
					}
				}
			}

			var data = null;
		} else
			return 0;
	}

	if(Action == 'editerAdmin') {
		Action = 'editer';
		var admin = true;
	} else
		var admin = false;

	if(Action == 'editer') {
		if(document.getElementById('sgtuxMessage'+id).title.substring(0, 7) != '(Actif)') {
			objetAppel.open('GET', rootGet+'&action=editer'+((admin) ? '&admin=1' : '')+'&mode=message&editionmessage&id='+id, true);
			objetAppel.onreadystatechange = function() {
				if(objetAppel.readyState < 4)
					sgtux_patientez('sgtux_patientez');

				if(objetAppel.readyState == 4) {
					document.getElementById('sgtux_patientez').style.display = 'none';
					var message = objetAppel.responseText.split('<>');

					if(message[1] != null) {
						if(message[1] == 'timeout' && admin) {
							alert(document.getElementById('sgtux_editLive_deconnexion').value);
							window.location.reload();
						} else {
							alert(message[0]);
							document.getElementById('sgtuxMessageEditer'+id).style.display = 'none';

							if(document.getElementById('sgtuxMessageModifie'+id))
								document.getElementById('sgtuxMessageModifie'+id).className = 'sgtux_editVisiteur';
						}
					} else {
						message = message[0];
						var lignes = message.split('\n');
						document.getElementById('sgtuxMessage'+id).innerHTML = '<textarea rows="'+lignes.length+'" id="sgtuxMessageText'+id+'" onblur="sgtux_editLive('+id+', \'editer'+((admin) ? 'Admin' : '')+'\', \''+root+'\')">'+message+'</textarea>';
						document.getElementById('sgtuxMessage'+id).title = '(Actif)'+((document.getElementById('sgtuxMessage'+id).title == '') ? '' : ' ')+document.getElementById('sgtuxMessage'+id).title;
						document.getElementById('sgtuxMessageText'+id).focus();
					}
				}
			}
			var data = null;
		} else {
			var data = 'LIVE=1&action=editer'+((admin) ? '&admin=1' : '')+'&editionmessage&id='+id+'&message='+urlencode(document.getElementById('sgtuxMessageText'+id).value);
			objetAppel.open('POST', root+'valide.php', true);
   			objetAppel.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

			objetAppel.onreadystatechange = function() {
				if(objetAppel.readyState < 4)
					sgtux_patientez('sgtux_patientez');

				if(objetAppel.readyState == 4) {
					document.getElementById('sgtux_patientez').style.display = 'none';
					var message = objetAppel.responseText.split('<>');

					if(message[1] != null && message[1] != 'maxedit' && message[1] != 'timeout') {
						alert(message[0]);
						document.getElementById('sgtuxMessageText'+id).focus();
					} else {
						if(message[1] == 'maxedit')
							document.getElementById('sgtuxMessageEditer'+id).style.display = 'none';

						if(message[1] == 'timeout') {
							if(admin) {
								alert(document.getElementById('sgtux_editLive_deconnexion').value);
								message = message[0];
								window.location.reload();
							} else {
								alert(message[0]);
								document.getElementById('sgtuxMessageEditer'+id).style.display = 'none';
								message = message[2];
							}
						} else
							message = message[0];

						document.getElementById('sgtuxMessage'+id).innerHTML = message;
						document.getElementById('sgtuxMessage'+id).title = document.getElementById('sgtuxMessage'+id).title.substring(8);
					}
				}
			}
		}
	}

	if(Action == 'editerNom') {
		if(document.getElementById('sgtuxMessageNom'+id).title.substring(0, 7) != '(Actif)') {
			objetAppel.open('GET', rootGet+'&action=editer&admin=1&mode=nom&id='+id, true);
			objetAppel.onreadystatechange = function() {
				if(objetAppel.readyState < 4)
					sgtux_patientez('sgtux_patientez');

				if(objetAppel.readyState == 4) {
					if(objetAppel.responseText == 'timeout') {
						alert(document.getElementById('sgtux_editLive_deconnexion').value);
						window.location.reload();
					} else {
						document.getElementById('sgtux_patientez').style.display = 'none';
						var nom = objetAppel.responseText;
						var caracteres = nom.length;
						document.getElementById('sgtuxMessageNom'+id).innerHTML = '<span class="sgtux_nom"><input type="text" id="sgtuxMessageNomText'+id+'" size="'+caracteres+'" onblur="sgtux_editLive('+id+', \'editerNom\', \''+root+'\')" value="'+nom+'" /></span>';
						document.getElementById('sgtuxMessageNom'+id).title = '(Actif)'+((document.getElementById('sgtuxMessageNom'+id).title == '') ? '' : ' ')+document.getElementById('sgtuxMessageNom'+id).title;
						document.getElementById('sgtuxMessageNomText'+id).focus();
					}
				}
			}
			var data = null;
		} else {
			var data = 'LIVE=1&action=editer&admin=1&id='+id+'&nom='+urlencode(document.getElementById('sgtuxMessageNomText'+id).value);
			objetAppel.open('POST', root+'valide.php', true);
   			objetAppel.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

			objetAppel.onreadystatechange = function() {
				if(objetAppel.readyState < 4)
					sgtux_patientez('sgtux_patientez');

				if(objetAppel.readyState == 4) {
					if(objetAppel.responseText == 'timeout') {
						alert(document.getElementById('sgtux_editLive_deconnexion').value);
						window.location.reload();
					} else {
						document.getElementById('sgtux_patientez').style.display = 'none';
						var nom = objetAppel.responseText;
						document.getElementById('sgtuxMessageNom'+id).innerHTML = nom;
						document.getElementById('sgtuxMessageNom'+id).title = document.getElementById('sgtuxMessageNom'+id).title.substring(8);
					}
				}
			}
		}
	}

	if(Action == 'editerReponse') {
		if(document.getElementById('sgtuxMessageReponse'+id).title.substring(0, 7) != '(Actif)') {
			objetAppel.open('GET', rootGet+'&action=editer&admin=1&mode=reponse&id='+id, true);
			objetAppel.onreadystatechange = function() {
				if(objetAppel.readyState < 4)
					sgtux_patientez('sgtux_patientez');

				if(objetAppel.readyState == 4) {
					if(objetAppel.responseText == 'timeout') {
						alert(document.getElementById('sgtux_editLive_deconnexion').value);
						window.location.reload();
					} else {
						document.getElementById('sgtux_patientez').style.display = 'none';
						var reponse = objetAppel.responseText;

						if(reponse == 0) {
							document.getElementById('sgtuxMessageReponseHTML'+id).style.display = 'block';
							document.getElementById('sgtuxMessageReponse'+id).innerHTML = '<textarea id="sgtuxMessageReponseText'+id+'" onblur="sgtux_editLive('+id+', \'editerReponse\', \''+root+'\')"></textarea>';
						} else {
							var lignes = reponse.split('\n');
							document.getElementById('sgtuxMessageReponse'+id).innerHTML = '<textarea rows="'+(lignes.length)+'" id="sgtuxMessageReponseText'+id+'" onblur="sgtux_editLive('+id+', \'editerReponse\', \''+root+'\')">'+reponse+'</textarea>';
						}

						document.getElementById('sgtuxMessageReponse'+id).title = '(Actif)'+((document.getElementById('sgtuxMessageReponse'+id).title == '') ? '' : ' ')+document.getElementById('sgtuxMessageReponse'+id).title;
						document.getElementById('sgtuxMessageReponseText'+id).focus();
					}
				}
			}
			var data = null;
		} else {
			var newreponse = document.getElementById('sgtuxMessageReponseText'+id).value;
			var data = 'LIVE=1&action=editer&admin=1&id='+id+'&reponse='+urlencode(newreponse);
			objetAppel.open('POST', root+'valide.php', true);
   			objetAppel.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

			objetAppel.onreadystatechange = function() {
				if(objetAppel.readyState < 4)
					sgtux_patientez('sgtux_patientez');

				if(objetAppel.readyState == 4) {
					if(objetAppel.responseText == 'timeout') {
						alert(document.getElementById('sgtux_editLive_deconnexion').value);
						window.location.reload();
					} else {
						document.getElementById('sgtux_patientez').style.display = 'none';
						var reponse = objetAppel.responseText;

						if(reponse == 0) {
							document.getElementById('sgtuxMessageReponseHTML'+id).style.display = 'none';
							document.getElementById('sgtuxMessageReponse'+id).innerHTML = '';
							document.getElementById('sgtuxMessageReponseBouton'+id).style.display = 'inline';
						} else {
							document.getElementById('sgtuxMessageReponse'+id).innerHTML = reponse;
							document.getElementById('sgtuxMessageReponseBouton'+id).style.display = 'none';
						}

						document.getElementById('sgtuxMessageReponse'+id).title = document.getElementById('sgtuxMessageReponse'+id).title.substring(8);
					}
				}
			}
		}
	}

	if(Action == 'approuver') {
		objetAppel.open('GET', rootGet+'&action=approuver&admin=1&id='+id, true);
		document.getElementById('sgtuxMessageApprouverBouton'+id).style.display = 'none';
		objetAppel.onreadystatechange = function() {
			if(objetAppel.readyState < 4)
				sgtux_patientez('sgtux_patientez');

			if(objetAppel.readyState == 4) {
				if(objetAppel.responseText == 'timeout') {
					alert(document.getElementById('sgtux_editLive_deconnexion').value);
					window.location.reload();
				} else {
					document.getElementById('sgtux_patientez').style.display = 'none';

					if(objetAppel.responseText == '') {
						document.getElementById('sgtux'+id).style.borderWidth = 0;
						document.getElementById('sgtuxMessageAll'+id).style.borderLeftStyle = 'solid';
						document.getElementById('sgtuxMessageReponse'+id).style.borderLeftStyle = 'solid';
						document.getElementById('sgtuxMessageCacherBouton'+id).style.display = 'inline';
					}
				}
			}
		}

		var data = null;
	}

	if(Action == 'cacher') {
		objetAppel.open('GET', rootGet+'&action=cacher&admin=1&id='+id, true);
		document.getElementById('sgtuxMessageCacherBouton'+id).style.display = 'none';
		objetAppel.onreadystatechange = function() {
			if(objetAppel.readyState < 4)
				sgtux_patientez('sgtux_patientez');

			if(objetAppel.readyState == 4) {
				if(objetAppel.responseText == 'timeout') {
					alert(document.getElementById('sgtux_editLive_deconnexion').value);
					window.location.reload();
				} else {
					document.getElementById('sgtux_patientez').style.display = 'none';

					if(objetAppel.responseText == '') {
						document.getElementById('sgtux'+id).style.border = '3px solid #F00';
						document.getElementById('sgtuxMessageAll'+id).style.borderLeftStyle = 'dashed';
						document.getElementById('sgtuxMessageReponse'+id).style.borderLeftStyle = 'dashed';
						document.getElementById('sgtuxMessageApprouverBouton'+id).style.display = 'inline';
					}
				}
			}
		}

		var data = null;
	}

	if(Action == 'reponseCopieMail') {
		objetAppel.open('GET', rootGet+'&action=reponseCopieMail&admin=1&sonMail='+urlencode(message)+'&id='+id, true);
		objetAppel.onreadystatechange = function() {
			if(objetAppel.readyState < 4)
				sgtux_patientez('sgtux_patientez');

			if(objetAppel.readyState == 4) {
				if(objetAppel.responseText == 'timeout') {
					alert(document.getElementById('sgtux_editLive_deconnexion').value);
					window.location.reload();
				} else {
					document.getElementById('sgtux_patientez').style.display = 'none';
					alert(objetAppel.responseText);
				}
			}
		}

		var data = null;
	}

	objetAppel.send(data);
}

function sgtux_copyright(root) {
	document.write('<img src="'+root+'/im/spacer.gif" alt="[A]" ondblclick="window.location=\''+root+'\'" />');
}