/* # coding: utf-8  */
var BASEURL = 'http://planete.dodie.jnj-mut-web1.johnson.lbn.fr/';
/*var BASEURL = 'http://planete.dodie.jnj-mut-web1.johnson.lbn.fr/';*/
/*var BASEURL = 'http://dodie.ntic.fr/planete/';*/
/*var BASEURL = 'http://localhost/D:\WWWROOT\planete.dodie.fr/';*/

function initPage() {
	var nav = document.getElementById("nav");
	if (nav) {
		var nodes = nav.getElementsByTagName("li");
		for (var i = 0; i < nodes.length; i++) {
			nodes[i].onmouseover = function () {
				if (this.className.indexOf("hover") == -1) {
					this.className += " hover";
				}
			}
			nodes[i].onmouseout = function () {
				this.className = this.className.replace(" hover", "");
			}
		}
	}
}
if (window.attachEvent && !window.opera)
	window.attachEvent("onload", initPage);

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function popupInscription(url) {
	var lalargeur =  650 ; // ici la largeur du popup
	var lahauteur = 850;  // ici la hauteur du popup
	var yes = 1;
	var no = 0;
	var menubar = no; //  Fichier, Edition, Affichage...
	var scrollbars = yes; // scrollbarre horizontale & verticale 
	var locationbar = no; // Barre d'adresse
	var directories = no; // Barre de liens
	var resizable = yes; // Permettre la fenetre d'être redimensionné 
	var statusbar = no; // Status bar (with 'Document: Done')
	var toolbar = no; // retour, Home, Stop bar d'outil
	var t = (screen.height-lahauteur)/2;  // pour centre selon la hauteur de l'écran
	var l = (screen.width-lalargeur)/2; // pour centrer selon la largeur de l'écran
	propriete = 'width=' + (lalargeur) + ',height=' + (lahauteur) +  ',top=' + t +',left=' + l;
	propriete += (menubar ? ',menubars' : '') + (scrollbars ? ',scrollbars' : '') + (locationbar ? ',location' : '') + (directories ? ',directories' : '') + (resizable ? ',resizable' : '') + (statusbar ? ',status' : '') + (toolbar ? ',toolbar' : '');
	
	window.open(url, 'Inscription', propriete);
}

function verifEmail(mail) {
	if(mail.search(/^[-a-zA-Z0-9._]*[-a-zA-Z0-9_]@[-a-zA-Z0-9_]+(\.[-a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/)!=0) {
		return false;
	} else {
		return true;
	}
}

function evaluation(code) {
	if(code==1 && window.document.getElementById('lapin_1').src.search("lapin_on")>0) {
		for(var i=1; i<=4; i++) {
			window.document.getElementById('lapin_'+i).src = BASEURL+"imgs/lapin_off.gif";
		}
		code = 0;
	}
	for(var i=1; i<=4; i++) {
		window.document.getElementById('lapin_'+i).src = BASEURL+"imgs/lapin_off.gif";
		if(i<=code) {
			window.document.getElementById('lapin_'+i).src = BASEURL+"imgs/lapin_on.gif";
		}
	}
	window.document.getElementById('commentaire_note').value = code;
}

function verifCommentaire() {
	var message = "";
	if(window.document.getElementById('commentaire_nom').value == "" || window.document.getElementById('commentaire_nom').value == "Nom") {
		message += " - Nom\n";
	}
	if(window.document.getElementById('commentaire_prenom').value == "" || window.document.getElementById('commentaire_prenom').value == "Prénom") {
		message += " - Prénom\n";
	}
	if(window.document.getElementById('commentaire_email').value == "" || window.document.getElementById('commentaire_email').value == "E-mail") {
		message += " - E-mail\n";
	}
	if(window.document.getElementById('commentaire_titre').value == "" || window.document.getElementById('commentaire_titre').value == "Titre") {
		message += " - Titre\n";
	}
	if(window.document.getElementById('commentaire_texte').value == "" || window.document.getElementById('commentaire_texte').value == "Votre commentaire") {
		message += " - Commentaire\n";
	}
	if(window.document.getElementById('commentaire_check').checked == false) {
		message += " - vous devez accepter la publication de votre commentaire pour poster.\n";
	}
	
	if(message!="") {
		alert("Veuillez renseigner les champs suivants :\n\n" + message);

		return false;
	}else {
		return true;
	}
}

function verifCata() {
	var message = "";
	if(window.document.getElementById('cata_nom').value == "" || window.document.getElementById('cata_nom').value == "Nom") {
		message += " - Nom\n";
	}
	if(window.document.getElementById('cata_prenom').value == "" || window.document.getElementById('cata_prenom').value == "Prénom") {
		message += " - Prénom\n";
	}
	if(window.document.getElementById('cata_email').value == "" || window.document.getElementById('cata_email').value == "E-mail") {
		message += " - E-mail\n";
	}
	if(window.document.getElementById('cata_texte').value == "" || window.document.getElementById('cata_texte').value == "Votre cata") {
		message += " - Cata\n";
	}
	//if(window.document.getElementById('cata_check').checked == false) {
	//	message += " - vous devez accepter la publication de votre cata pour poster.\n";
	//}
	
	if(message!="") {
		alert("Veuillez renseigner les champs suivants :\n\n" + message);

		return false;
	}else {
		return true;
	}
}

function cata(id) {
	//jQuery('#cata_'+id).css('display','none');
	jQuery.ajax({
		url : BASEURL+"include/cata/votecata.php",
		dataType: 'xml',
		data: { cata_id : id, cata_type : 'cata' },
		success: function(xml) {
			retourCata = jQuery("cata",xml).text();
			retourPasCata = jQuery("pascata",xml).text();
			if (retourCata == 'NA') {
				jQuery('#cata_'+id).html('vous avez déjà voté').fadeIn();
			} else {
				jQuery('#return_'+id).html('<img src="/imgs/merci.gif" id="imgvoter"/>').fadeIn();
				jQuery('#comback_'+id).html('<span><span class="votecata"><span class="score">('+retourCata+')</span></span><span class="votepascata"><span class="score">('+retourPasCata+')</span>			</span></span>').fadeIn();
			}
		}
	});
}
function pascata(id) {
	//jQuery('#pascata_'+id).css('display','none');
	jQuery.ajax({
		url : BASEURL+"include/cata/votecata.php",
		dataType: 'xml',
		data: { cata_id : id, cata_type : 'pascata' },
		success: function(xml) {
			retourCata = jQuery("cata",xml).text();
			retourPasCata = jQuery("pascata",xml).text();
			if (retourPasCata == 'NA') {
				jQuery('#pascata_'+id).html('vous avez déjà voté').fadeIn();
			} else {
			jQuery('#return_'+id).html('<img src="/imgs/merci.gif" id="imgvoter"/>').fadeIn();
				jQuery('#comback_'+id).html('<span><span class="votecata"><span class="score">('+retourCata+')</span></span><span class="votepascata"><span class="score">('+retourPasCata+')</span>			</span></span>').fadeIn();
			}
		}
	});
}
function catahome(id) {
	jQuery.ajax({
		url : BASEURL+"include/cata/votecata.php",
		dataType: 'xml',
		data: { cata_id : id, cata_type : 'cata' },
		success: function(xml) {
			retourCata = jQuery("cata",xml).text();
			retourPasCata = jQuery("pascata",xml).text();
			if (retourCata == 'NA') {
				jQuery('#cataCont_'+id).html('<span style="display:block;text-align:center;padding-top:5px;font-weight:bold;">vous avez déjà voté</span>');
			} else {
				jQuery('#catahome_'+id).html('<img src="'+BASEURL+'imgs/sidebar-cata.gif" alt="Cata !" class="noblock"/> ('+retourCata+')').fadeIn();
				jQuery('#pascatahome_'+id).html('<img src="'+BASEURL+'imgs/sidebar-pascata.gif" alt="Pas Cata !" class="noblock" style="position:relative;top:5px;" /> ('+retourPasCata+')').fadeIn();
			}
		}
	});
}
function pascatahome(id) {
	jQuery.ajax({
		url : BASEURL+"include/cata/votecata.php",
		dataType: 'xml',
		data: { cata_id : id, cata_type : 'pascata' },
		success: function(xml) {
			retourCata = jQuery("cata",xml).text();
			retourPasCata = jQuery("pascata",xml).text();
			if (retourPasCata == 'NA') {
				jQuery('#cataCont_'+id).html('<span style="display:block;text-align:center;padding-top:5px;font-weight:bold;">vous avez déjà voté</span>');
			} else {
				jQuery('#catahome_'+id).html('<img src="'+BASEURL+'imgs/sidebar-cata.gif" alt="Cata !" class="noblock" /> ('+retourCata+')').fadeIn();
				jQuery('#pascatahome_'+id).html('<img src="'+BASEURL+'imgs/sidebar-pascata.gif" alt="Pas Cata !" class="noblock" style="position:relative;top:5px;" /> ('+retourPasCata+')').fadeIn();
			}
		}
	});
}
function cut(input) {
	if (input.val().length >= 300) {
		input.val(input.val().substring(0,300));
	}
	var reste = 300 - input.val().length;
 	var print =  reste;
	jQuery("#cpt_caracteres").html(input.val().length+'/300');
}

function verifNewsletter() {
	var message_fac = "";
	
	if(window.document.getElementById('ccatei').value == "" || window.document.getElementById('ccatei').value == "0")
		message_fac += " Vous êtes\n";
	if(window.document.getElementById('cnomea').value == "")
		message_fac += " Nom\n";
	if(window.document.getElementById('cprena').value == "")
		message_fac += " Prénom\n";
	if(window.document.getElementById('cmaila').value == "")
		message_fac += " E-mail\n";
	else {
		if (verifEmail(window.document.getElementById('cmaila').value.toLowerCase())==false)
			message_fac += " E-mail invalide\n";
	}
	
	if( message_fac == "" )
		return true;
	else {
		alert( 'Merci de renseigner les champs suivants :\n\n' + message_fac );
		return false;
	}
}

jQuery(document).ready(function() {
	jQuery('#select_dossier_tri').change(function() {
		if(jQuery(this).val() != '') {
			jQuery('#form-dossier-tri').submit();
		}
	});
	jQuery('#select_actu_tri').change(function() {
		if(jQuery(this).val() != '') {
			jQuery('#form-actu-tri').submit();
		}
	});
	jQuery('#select_album_tri').change(function() {
		if(jQuery(this).val() != '') {
			jQuery('#form-album-tri').submit();
		}
	});
	if(jQuery("#footer-tabs")) {
		jQuery("#footer-tabs").tabs();
	}
	if(jQuery("#popupInscription")) {
		jQuery("#popupInscription").click(function(){
			popupInscription(jQuery(this).attr('href'));
			return false;
		});
	}
	if(jQuery("#popupInscription2")) {
		jQuery("#popupInscription2").click(function(){
			popupInscription(jQuery(this).attr('href'));
			return false;
		});
	}
	if(jQuery("#cata_texte")) {
		jQuery("#cata_texte").keyup(function(){
			cut(jQuery(this));
		});
	}
	jQuery('.votecata').click(function(){
		tabID = jQuery(this).parent().attr("id").split("_");
		cata(tabID[1]);
		return false;
	});
	jQuery('.votepascata').click(function(){
		tabID = jQuery(this).parent().attr("id").split("_");
		pascata(tabID[1]);
		return false;
	});
	jQuery('.votecatahome').click(function(){
		tabID = jQuery(this).parent().attr("id").split("_");
		catahome(tabID[1]);
		return false;
	});
	jQuery('.votepascatahome').click(function(){
		tabID = jQuery(this).parent().attr("id").split("_");
		pascatahome(tabID[1]);
		return false;
	});
	
	jQuery('.btnCourbes').click(function(){
		MM_openBrWindow(jQuery(this).attr('href'), 'courbes', 'width=700,height=700');
		return false;
	});
	jQuery('#otherform').click(function(){
		jQuery('#frmCata2').show();
		return false;
	});
	jQuery('#btnEnvoiNewsletter').click(function(){
		return verifNewsletter();
	});
});