// old functions
function affichagemenu(calque){
	document.getElementById("CalqueMenu02").style.visibility = 'visible';
}
function PopUpCentrer(fenetre,page,largeur,hauteur,options) {
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	window.open(page,fenetre,"top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}
function VerifFormContact() {
	if (document.FormContact.Nom.value == "") {
		alert("Entrez votre Nom.")
		document.FormContact.Nom.focus();
		return false;
	} else {
		if (document.FormContact.Prenom.value == "") {
		alert("Entrez votre Prenom.")
		document.FormContact.Prenom.focus();
		return false;
		} else {
	 		if (document.FormContact.Commentaire.value == "") {
	    	alert("Entrez votre commentaire.")
	    	document.FormContact.Commentaire.focus();
	    	return false;
			} else {
				if (document.FormContact.Email.value == "") {
	  		  		alert("Entrez votre adresse mail.")
	    			document.FormContact.Email.focus();
	    			return false;
					} else {
					
						var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					
						if (!filter.test(document.FormContact.Email.value)) {
							alert("Entrez une adresse mail valide")
							document.FormContact.Email.select();
							document.FormContact.Email.focus();
							return false;
						
						
						} else return true;
					
				}
			}
		}
	}
}


/**
 * Home news navigator
 */
var actualite = {
	actualiteIdentifier	: '.actualite',	
	navIdentifier		: '#boutons_actu',
	navActiveClass		: 'on',
	currentIndex		: 0,
	
	showIt : function(index) {
		if (index == undefined) {
			index = 0;
		}
	
		actualite.currentIndex = index;
	
		// hide each element
		$(actualite.actualiteIdentifier).hide();
		// then show specified index
		$(actualite.actualiteIdentifier + ':eq('+ index +')').show();
	},
	
	activateNav	: function() {
	
		var navObj = $(actualite.navIdentifier + ' a');
		var defaultNavObj = $(actualite.navIdentifier + ' a:eq(' +  actualite.currentIndex + ')');
		actualite.styleNav(navObj, defaultNavObj);
	
		$(actualite.navIdentifier + ' a').click(function() {
			var index = navObj.index(this); 
			actualite.showIt(index);
			actualite.styleNav(navObj, this);
		}); 
	},
	
	styleNav : function(navObj, currentNavObj) {
		$(navObj).removeClass(actualite.navActiveClass);
		$(currentNavObj).addClass(actualite.navActiveClass);
	},

	load : function(index) {
		actualite.showIt(index);
		actualite.activateNav();
	}
};


var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

function fixPNG(myImage) 
{
    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
    {
       var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	   var imgTitle = (myImage.title) ? 
		             "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' "
	   var imgStyle = "display:inline-block;" + myImage.style.cssText
	   var strNewHTML = "<span " + imgID + imgClass + imgTitle
                  + " style=\"" + "width:" + myImage.width 
                  + "px; height:" + myImage.height 
                  + "px;" + imgStyle + ";"
                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                  + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"
	   myImage.outerHTML = strNewHTML	  
    }
}

function isValidEmail(str) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return !filter.test(str) ? false : true;
}


$(function() 
{
	

	/** extranet form action **/
	$('#extranetForm').submit(function() 
	{
		if ('' == $('#Identifiant').val()  ) {
			alert('Identifiant is required.');
			$('#Identifiant').focus();
			return false;
		}
		if ('' == $('#MotDePasse').val()  ) {
			alert('MotDePasse is required.');
			$('#MotDePasse').focus();
			return false;
		}

		$.post('ajax/authentification.php', { Identifiant : $('#Identifiant').val(), MotDePasse : $('#MotDePasse').val() }, function(data) 
		{
			//alert(data);
			if ('failed' ==  data) {
				$('#Identifiant').val('').focus();
				$('#MotDePasse').val('');
				alert('Les informations de connexion ne sont pas correctes');
			} else if ('success' == data) {
									window.location = '';
							}
		});
		
		return false;
	});
	
	/** request password **/
	$('#requestPasswordBtn').toggle(
		function() {
			$('#requestPasswordForm').slideDown();
		},
		function() {
			$('#requestPasswordForm').slideUp();
		}
		
	);
	$('#requestPasswordForm').submit(function() {
		var email = $('#requestPasswordForm #Email').val();
		/*if (!isValidEmail(email)) {
			alert('Valid email is required.');
			$('#requestPasswordForm #Email').focus()
		} else {*/
			$.post('ajax/request_password.php', $(this).serialize(), function(data) {
				if (data == 'success') {
					alert('Please check your email to confirm your password.');
					window.location = 'index.php';
				} else {
					alert(data);
				}
			});
		//}
		return false;
	});
	
	/** mon profil **/
	$('#profileForm').submit(function() {
	
		var error = false;
		$('#profileForm required').each(function() {
			var val = $.trim($(this).val()); 
		
			if (val == '') {
				error = true;
			}
		
		});
	
		if (error) {
			alert('All fields are required.');
		} else {
			$.post('ajax/change_password.php', $(this).serialize(), function(data) {
				if (data == 'success') {
					alert('Please check your email to confirm.');
					window.location = 'index2.php';
				} else {
					alert(data);
				}
			});
		}
	
		return false;
	});

	/** email form action **/
	$('#emailForm').submit(function() 
	{
	
		if ('' == $('#nom').val()  ) {
			alert('Nom is required.');
			$('#nom').focus();
			return false;
		}
		
		if ( '' == $('#email').val() || !isValidEmail($('#email').val()) ) {
			alert('Valid email is required.');
			$('#email').focus();
			return false;
		}

		if ('' == $('#email_friend').val() || !isValidEmail($('#email_friend').val()) ) {
			alert('Valid friend\'s email is required.');
			$('#email_friend').focus();
			return false;
		}

		
		$('#emailSubmit').val("Sending...").attr('disabled', true);

		$.post('ajax/send_to_friend.php', 
			{ 
				nom : $('#nom').val(), 
				email : $('#email').val(),
				email_friend : $('#email_friend').val(), 
				message : $('#message').val(),
				url : $('#url').val(),
				seed : $('#seed').val(),
				page_title : $('#page_title').val()
			},
			
			function(data) 
			{
				if ('failed' ==  data) {
					alert('ERROR');
				} else if ('success' == data) {
					alert('Votre envoi a réussi.');
					$.overlayClose();
					$('#emailSubmit').val("Envoyer").attr('disabled', false);
					$('#nom').val('');
					$('#email').val('');
					$('#email_friend').val(''); 
				 	$('#message').val('');
				}
			}
		);
		
		return false;
	});
	

	/** newsletter form action **/
	$('#newsletterForm').submit(function() 
	{
		if ( '' == $('#newsletterForm #nom').val()) {
			alert('Nom is required.');
			$('#newsletterForm #nom').focus();
			return false;
		}
		
		if ( '' == $('#newsletterForm #email').val() || !isValidEmail($('#newsletterForm #email').val()) ) {
			alert('Valid email is required.');
			$('#newsletterForm #email').focus();
			return false;
		}

		$('#newsletterSubmit').val("Sending...").attr('disabled', true);

		$.post('ajax/newsletter_subscription.php', 
			{ nom : $('#newsletterForm #nom').val(), societe : $('#newsletterForm #societe').val(), email : $('#newsletterForm #email').val(), seed : $('#newsletterForm #seed').val() },
			function(data) 
			{
				if ('failed' ==  data) {
					alert('ERROR');
				} else if ('success' == data) {
					alert('Votre inscription est enregistrée.');
					$.overlayClose();
				}
			}
		);
		
		return false;
	});


	/** search form **/
	$("#MotClef").focus(function() 
	{
   		if ($(this)[0].value == 'Recherchez' ) {
   			$(this)[0].value = '';
   		}
  	}).blur(function() { 
  		if ($(this)[0].value == '' ) {
   			$(this)[0].value = 'Recherchez';
   		}
   	})

	/** overlay **/
	$("a[@rel][id='o']").overlay({
		onBeforeLoad : function() {
		}
	});
    

	$("area[@rel][id='o']").overlay({
		onBeforeLoad : function() {
		}
	});

    
	$('a.externalUrl').overlay({
		onBeforeLoad : function() {
		},
	
		onLoad: function(content, link) {
			content.find('p').load(link.attr('href'));
		} 
	});
	$('a.externalImage').overlay({
		onBeforeLoad : function(content, link) {
			content.find('p').empty();
		},
	
		onLoad: function(content, link) {
			content.find('p').html('<img style="padding: 0; margin: 0;" src="' + link.attr('href') + '" />');
		} 
	});
	
});