/* dynamic loading of fixed site parts */

function getFixed( page ) {
	ajaxLoad('getFixed.php', 'main-content', page );
	return ;
}

/* JSON function + clean entries */

function makeJSON( data ) {
	JSONstring.compactOutput=true;     
 	JSONstring.includeProtos=true;     
 	JSONstring.includeFunctions=false;     
 	JSONstring.detectCirculars=false;          
 	JSONstring.restoreCirculars=false
	var result = JSONstring.make(data);
	return result;
}

function replace(expr,a,b) {
	var i=0
	while (i!=-1) {
		i=expr.indexOf(a,i);
		if (i>=0) {
			expr=expr.substring(0,i)+b+expr.substring(i+a.length);
			i+=b.length;
		}
	}
	return expr
}

function cleanStr(str) {
	var result = replace(str, '&', '%26');
	return result ;
} 


/* display du share link */

function toggleShareLink(item) {
	if($('shareLink' + item ).style.display == 'block') {
		$('shareLink' + item ).style.display = 'none';
		$('shareImg' + item ).src = "img/icon_share.png";
	} else {
		$('shareLink' + item ).style.display = 'block';
		$('shareImg' + item ).src = "img/close.png";
	}
	return;
}

/* fonctions pour le chat */

function openChat() {
	window.open('chat/popup.php','chat_popup','toolbar=0,menubar=0,scrollbars=1,width=680,height=650');
	return false;
}

/* soumission du form de contact */

/* on checke les champs essentiels */

function verifmail(mail) {
	var valide=false;
	for(var j=1;j<(mail.length);j++){
		if(mail.charAt(j)=='@'){
			if(j<(mail.length-4)){
				for(var k=j;k<(mail.length-2);k++){
					if(mail.charAt(k)=='.') valide=true;
				}
			}
		}
	}
	
	return valide;
}

function checkmail() {

if($('nom').value == '') {
	alert('Veuillez indiquer votre nom');
	$('nom').focus();
	return(false);
}

if($('mail').value == '') {
	alert('Veuillez indiquer votre email');
	$('mail').focus();
	return(false);
}

if(!verifmail($('mail').value)) {
	alert('Veuillez vérifier le format de votre email');
	$('mail').focus();
	return(false);
}

if($('objet').value == '') {
	alert('Veuillez indiquer un objet pour ce message');
	$('objet').focus();
	return(false);
}

if($('message').value == '') {
	alert('Le message est vide...');
	$('message').focus();
	return(false);
}

return(true);

}

function clearContactForm() {
	$('objet').value = '';
	$('message').value = '';
	$('contactZone').style.display = 'block';
	$('contactResponse')	.style.display = 'none';
}

function sendmail() {
	var data = new Object;
	data['contactype']	= $('contacttype').value;
	data['civilite']		= $('civilite').value;
	data['prenom']			= cleanStr($('prenom').value );
	data['nom']				= cleanStr($('nom').value );
	data['mail']			= cleanStr($('mail').value );
	data['phone']			= cleanStr($('phone').value );
	data['objet']			= cleanStr($('objet').value );
	data['message']		= cleanStr($('message').value );

	var result = makeJSON( data );
	ajaxLoad('content/sendmail.php', 'contactResponse', result );
	$('contactResponse').style.display = 'block';
	$('contactZone').style.display = 'none';
	return;
}

// affichage/masquage des zones de flux

function toggleFlux( id ) {
	if(!id) return false;
	var folder_img = 'img/rss-orange.png';
	var folder_open_img = 'img/rss-green.png';
	var zone = 'flux' + id;
	var imgFlux = 'imgFlux' + id;
	if($(zone).style.display == 'none') {
		$(zone).style.display = 'block';
		$(imgFlux).src = folder_open_img;
		ajaxLoad('content/getFeed.php', zone, id );
	} else {
		$(zone).style.display = 'none';
		$(imgFlux).src = folder_img;
		$(zone).innerHTML = '<img src="img/ajax-loader.gif" alt="ajax-loader" style="border:none" />';
	}
	return;	
}
