/**
 * Meet'n'Go
 * fichier : func.js
 * 
 * Fichier de fonction javascript
 * 
 * @author	Andy (Julien LENOIR)
 * @package	Meet'N'Go
 */

function verif(field) { 

	var ok = true;
	 
	for (i = 0; i < field.length; i++) {
		
		if (field[i].getAttribute('needed') == "yes") {
		
			if (field[i].nodeName == "INPUT") {
				
				if(eq = field[i].getAttribute('eq')) {
					
					veq = document.getElementById(eq).value;
					
					if(veq != field[i].value){
						alert(field[i].getAttribute('intl'));
						ok = false;
						return(ok);
					}
					
				} else if(regexp = field[i].getAttribute('regexp')) {
					
					if(!field[i].value.match(new RegExp(regexp,"i"))){
						alert(field[i].getAttribute('intl'));
						ok = false;
						return(ok);
					}
					
				} else if(field[i].value == '') {
					alert(field[i].getAttribute('intl'));
					ok = false;
					return(ok);
				}
		
			} else if (field[i].nodeName == 'SELECT') {			
			
				options = field[i].getElementsByTagName('OPTION');
				
				for(var no=0;no<options.length;no++)
					if ((options[no].selected) && (options[no].disabled)) {
						alert(field[i].getAttribute('intl'));
						ok = false;
						return(ok);
					}
				
			}
	
		}
	
	}
	
	return(ok);	
	
}

function writeFlash(chemin,width,height) {
	document.write("<object type=\"application/x-shockwave-flash\" data=\""+chemin+"\" width=\""+width+"\" height=\""+height+"\"><param name=\"movie\" value=\""+chemin+"\" /><param name=\"wmode\" value=\"transparent\" /></object>");
}

function switch_tri(from,to) {

	qstr = from+"?filtre="+to;
	page = getParam('p');
	if (page!="") qstr += "&p="+page;
	ambiance = getParam('ambiance');
	if (ambiance!="") qstr += "&ambiance="+ambiance;
	
	//alert(qstr);
	document.location.replace(qstr);

}

function switch_ambiance(from,to) {

	qstr = from+"?ambiance="+to;
	page = getParam('p');
	if (page!="") qstr += "&p="+page;
	filtre = getParam('filtre');
	if (filtre!="") qstr += "&filtre="+filtre;
	
	document.location.replace(qstr);

}

function getParam(name) {
	var start=location.search.indexOf("?"+name+"=" );
	if (start<0) start=location.search.indexOf("&"+name+"=" );
	if (start<0) return '';
	start += name.length+2;
	var end=location.search.indexOf("&",start)-1;
	if (end<0) end=location.search.length;
	var result='';
	for(var i=start;i<=end;i++) {
		var c=location.search.charAt(i);
		result=result+(c=='+'?' ':c);
	}
	return unescape(result);
} 

/**
 * Teste la validité du code postal et renvoit la ville
 */
function dosondage(radio)
{	
	
	document.getElementById('sondage').innerHTML = '<img src="images/loading.gif" vspace="60" hspace="74"/>';
	
	if (radio!=null) {
	
		var vote = 0;
		
		for (var i=0; i<radio.length;i++) {
			if (radio[i].checked) {
				vote = radio[i].value;
			}
		}
	
	} else {
		
		var vote = -1;
		
	}
	
	xhr_object.open("POST", "includes/inc.sondage.php", true);
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.onreadystatechange = function() {
	if(xhr_object.readyState == 4) {
	if(xhr_object.responseText) {
		//alert(xhr_object.responseText);		
		
		document.getElementById('sondage').innerHTML = unescape(xhr_object.responseText);
	}}}
	if (vote!=0) xhr_object.send("vote="+vote);
}

function check_title() {
	if (document.getElementById('titre').value != "")
		return true;
	else {
		alert('Veuillez entrer un titre !');
		return false;
	}
	
}

function switchPhotoTo(param,item){
	document.getElementById("photo").src = param;
	document.getElementById("item").style.background = "#98b917";
}


function addLink() {
	
	link = prompt('Veuillez entrer l\'adresse du lien');
	if (link) {
		
		nom = prompt('Veuillez entrer le nom du lien');
		
		if (nom) {
			
			addText('','<a href="'+link+'" target="_blank">'+nom+'</a>','');
			
		}
		
	}
	
}

function addForumLink() {
	
	link = prompt('Veuillez entrer l\'adresse du lien');
	if (link) {
		
		nom = prompt('Veuillez entrer le nom du lien');
		
		if (nom) {
			
			addTextForum('','<a href="'+link+'" target="_blank">'+nom+'</a>','');
			
		}
		
	}
	
}


/**
 * Fonction d'ajout de texte dans les blogs
 * @param ouverture du tag
 * @param texte par defaut (ou pas)
 * @param fermeture du tag
 */
function addText(startTag,defaultText,endTag) {	
	ie = document.all;
	if (ie)
	{
		with(document.poster)
		{
			if (contenu.createTextRange)
			{
				var text;
				contenu.focus(contenu.caretPos);
				contenu.caretPos = document.selection.createRange().duplicate();
				if(contenu.caretPos.text.length>0)
				{
					var sel = contenu.caretPos.text;
					var fin = '';
					while(sel.substring(sel.length-1, sel.length)==' ')
					{
						 sel = sel.substring(0, sel.length-1)
						 fin += ' ';
					}
					contenu.caretPos.text = startTag + sel + endTag + fin;
				}
				else
					contenu.caretPos.text = startTag+defaultText+endTag;
			}
			else
				contenu.value += startTag+defaultText+endTag;
		}
	}
	else
	{
		with(document.poster)
		{		 
			if (contenu.createTextRange && contenu.caretPos)
			{
				var caretPos = contenu.caretPos;
				caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + startTag + defaultText + endTag + ' ' : caretPos.text + text;
			}
			else if ( contenu.selectionStart || contenu.selectionStart == 0 )
			{
				var before = (contenu.value).substring(0, contenu.selectionStart);
				var defaultText = (contenu.value).substring(contenu.selectionStart,contenu.selectionEnd);
				var after = (contenu.value).substring(contenu.selectionEnd, contenu.textLength);
				contenu.value = before + startTag + defaultText + endTag  + after;
			}
			contenu.focus();
		}
	}
}

/**
 * Fonction d'ajout de texte dans le forum
 * @param ouverture du tag
 * @param texte par defaut (ou pas)
 * @param fermeture du tag
 */
function addTextForum(startTag,defaultText,endTag) {	
	ie = document.all;
	if (ie)
	{
		with(document.formulaire_post)
		{
			if (post.createTextRange)
			{
				var text;
				post.focus(post.caretPos);
				post.caretPos = document.selection.createRange().duplicate();
				if(post.caretPos.text.length>0)
				{
					var sel = post.caretPos.text;
					var fin = '';
					while(sel.substring(sel.length-1, sel.length)==' ')
					{
						 sel = sel.substring(0, sel.length-1)
						 fin += ' ';
					}
					post.caretPos.text = startTag + sel + endTag + fin;
				}
				else
					post.caretPos.text = startTag+defaultText+endTag;
			}
			else
				post.value += startTag+defaultText+endTag;
		}
	}
	else
	{
		with(document.formulaire_post)
		{		 
			if (post.createTextRange && post.caretPos)
			{
				var caretPos = post.caretPos;
				caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + startTag + defaultText + endTag + ' ' : caretPos.text + text;
			}
			else if ( post.selectionStart || post.selectionStart == 0 )
			{
				var before = (post.value).substring(0, post.selectionStart);
				var defaultText = (post.value).substring(post.selectionStart,post.selectionEnd);
				var after = (post.value).substring(post.selectionEnd, post.textLength);
				post.value = before + startTag + defaultText + endTag  + after;
			}
			post.focus();
		}
	}
}

/**
 * Enleve les images
 */
function RmIMG()
{
	var avant = document.poster.contenu.value;
	var maintenant = avant.replace(/{IMG}/,"");
	document.poster.contenu.value = maintenant;
}

/**
 * Affiche le preview
 */
function Preview(img,align)
{
	preview = window.open('','test',config='height=500, width=400, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
	var tmp	=	document.poster.contenu.value;
	tmp = tmp.replace(/\n/g,"<br />");
	if (align=="center")
	{
		tmp = tmp.replace(/{IMG}/,"<center><img src=\"http://www.lemonsound.com/visuels/blogs/billets/"+ img +".jpg\"></center>");
	}
	else
	{
		tmp = tmp.replace(/{IMG}/,"<img src=\"http://www.lemonsound.com/visuels/blogs/billets/"+ img +".jpg\" hspace=\"10\" align=\""+ align +"\">");
	}
	tmp = "<html><head></head><body style=\"background:#f5f5f5;color:#333333;font-size:11px;font-family:Trebuchet MS;text-align:justify;padding:16px\">" + tmp + "</body></html>";
	preview.document.write(tmp);
}


/**
 * Génere l'album photo a partir de la page popup.php
 * @param img : l'image en cours
 * @param login : le login de l'album photo
 */
function popup(img,login) {
	var w = window.open("popup.php?m="+login+"&i="+img, "SmallWin","width=400,height=300");
	w.focus;
}

/**
 * Charge le popup d'avatar
 * @param login : le login de l'album photo
 */
function avatar(login) {
	var w = window.open("avatar.php?m="+login, "SmallWin","width=400,height=300");
	w.focus;
}

/**
 * Création de l'objet ajax
 */
var xhr_object = null;
if(window.XMLHttpRequest) // Firefox
	xhr_object = new XMLHttpRequest();
else if(window.ActiveXObject) // IE
	xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
else { // XMLHttpRequest non supporté par le navigateur
	alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
}

/**
 * Envoit un ping au serveur en fonction de l'utilisateur en ligne :)
 */
function ping()
{
	xhr_object.open("POST", "includes/inc.online.php", true);
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.onreadystatechange = function() {
		if(xhr_object.readyState == 4) {
			if(xhr_object.responseText) {
				setTimeout('ping()',30000);
				//alert(xhr_object.responseText);
			}
		}
	}
	xhr_object.send(null);
}

/*
 * Quelques petites déclarations :)
 */

window.onload = function () {
	
	ping();
	initSlideShow();
	
};



/**
 * Teste la validité du code postal et renvoit la ville
 */
function test(str)
{
	
	function nompropre(mot) {
		var m=mot.charAt(0).toUpperCase() +
		mot.substring(1).toLowerCase();
		return m;
	}
	
	if (str.length == 5)
	{
		xhr_object.open("POST", "includes/inc.ajax.php", true);
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr_object.onreadystatechange = function() {
			if(xhr_object.readyState == 4)
			{
				if(xhr_object.responseText)
				{
					//alert(nompropre(xhr_object.responseText));
					document.getElementById('ville').value = nompropre(xhr_object.responseText);
				}
			}
		}
		xhr_object.send("f=cpostal&n="+str);	
	}
}

function abus(url)
{	
	xhr_object.open("POST", "includes/inc.abus.php", true);
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.onreadystatechange = function() {
		if(xhr_object.readyState == 4)
		{
			if(xhr_object.responseText)
			{
				alert(xhr_object.responseText);
			}
		}
	}
	
	if(confirm('Etes vous sûre de vouloir signaler un abus sur cette page !?'))
		xhr_object.send("url="+url);
	
	
}

/**
 * Check tout les checkbox du formulaire ou les décheck!
 */
var checkflag = "false";
function check(field)
{
	if (checkflag == "false")
	{
		for (i = 0; i < field.length; i++)
		{
			field[i].checked = true;
		}
		checkflag = "true";
		return "Uncheck All";
	}
	else
	{
		for (i = 0; i < field.length; i++)
		{
			field[i].checked = false;
		}
		checkflag = "false";
		return "Check All";
	}
}