// JavaScript Document

// initialize page
function init(){

	// add event for rating stars	
	/*$$('.rate').each(function(element,i){
			element.addEvent('click', function(){
				setArticleVote(element.getParent().get('id'), element.id);
				return false;
			});
	});*/
	
	// Removed due to conflict with intellixt
	// add envoyer link event
	/*$$('#send_link').each(function(element,i){
				element.addEvent('click', function(){
					sendArticle($('send_link_url').get('value'));
					return false;
				});
	});	*/
	jQuery('#send_link').click(function(e) {		
		e.preventDefault();
		jQuery.nyroModalManual({							   	
			url: jQuery("#send_link_url").val()
		});
		return false;
	});

}

function sendMail(thisObj){
	var thisForm = $('formSendArticle');	
	if(checkValues(thisForm)){
		var id  	 	 = $('id').get('value');	
		var nom 	 	 = $('nom').get('value');
		var prenom   	 = $('prenom').get('value');
		var email_from   = $('email_from').get('value');	
		var email_to 	 = $('email_to').get('value'); 
		var message      = $('message').get('value');		
		var url_link     = window.location.href;	
		var send_newsletter = $('send_newsletter').get('checked');			
		var req = new Request({
	           method: 'post',
	           url: '/ajax/sendArticleMail/',
	           data: { 	'id' : id,
			   			'nom' : nom,
						'prenom' : prenom,
						'email_from' : email_from,
						'email_to' : email_to,
						'message' : message,
						'link' : url_link
			   		 },
	           onRequest: function() { 
			   		$('default').dispose(); 						// remove form
					$('send_wait').set('styles', {'display': ''}); 	// display wait			
			   },
	           onComplete: function(response) { 		   		
			   		$('send_wait').dispose(); // remove loader onResponse
			   		if(response){												
						alert('Le mail a \351t\351 envoy\351.');						
						if(send_newsletter){ //redirect parent window to newsletter page...							
							var jsonObj = JSON.decode(response);
							window.location.href = 'http://www.autodeclics.com/php2/dynamic.php3?rub=0&where=NEWSLETTER&email='+encodeURI(jsonObj.email_from); 
						}
					}else{							
						alert('Erreur lors de l\'envoi du courrier \351lectronique.');	
					}
					//MOOdalBox.close(); // close moodal window
					jQuery.nyroModalRemove();
			   }
	      }).send();	
		
	}	
	return false;
}

function checkValues(thisForm){
	
	var err = "";
		
	if(thisForm.prenom.value == ""){
		err += "\n- Le pr\351nom est un champ obligatoire."
	}
	
	if(thisForm.email_from.value == ""){
		err += "\n- Votre Email est obligatoire."
	}else if(!checkEmail(thisForm.email_from.value)){
		err += "\n- Votre courriel a format invalide."
	}
	
	if(thisForm.email_to.value == ""){
		err += "\n- Email du b\351n\351ficiaire est un champ obligatoire."
	}else if(!checkEmail(thisForm.email_to.value)){
		err += "\n- Destinataire Email format est invalide."
	}	
	
	if(err == ""){
		return true;
	}else{
		alert("Les erreurs suivantes se sont produites.\n"+err);
		return false;
	}
}

function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);
}

/*function setArticleVote(id, nbstars){
	if(!Cookie.read(id)){
		var req = new Request({
					   method: 'post',
					   url: '/ajax/voteArticle/',
					   data: { 	'id'  	  : id,
								'nbstars' : nbstars
							 },
					   onRequest: function() {
							Cookie.write(id, true, {duration: 1});
							$$('#articleRating #rate').set('styles', {'display': 'none'});
							$$('#articleRating #wait').set('styles', {'display': ''});
					   },
					   onComplete: function(response) {
							if(response){
								var jsonObj = JSON.decode(response);
								var myStyles = ['nostar', 'onestar', 'twostar', 'threestar', 'fourstar', 'fivestar'];
								$$('#articleRating ul').set('class', 'rating ' + myStyles[jsonObj.nbstars]); // display new rating
								$$('#articleRating #count').set('html', jsonObj.nbvotant); // display new count
							}else{
								alert('Erreur lors de la demande');
							}
							$$('#articleRating #rate').set('styles', {'display': ''});
							$$('#articleRating #wait').set('styles', {'display': 'none'});
					   }
				  }).send();
	}else{
		alert('Limite d\'un vote par jour pour chaque article');
	}
}*/

function clearImages(thisObj){
	var hideImage	 = thisObj.checked;
	var thisElements = $(document.body).getElements('tr.photoCredit');	
	thisElements.each(	function(item, index){		
							if(hideImage){
								item.set('styles', {'display': 'none'})
							}else{
								item.set('styles', {'display': ''})
							}
						}
					 );
	var thisElements = $(document.body).getElements('tr.photoCreditFull');	
	thisElements.each(	function(item, index){		
							if(hideImage){
								item.set('styles', {'display': 'none'})
							}else{
								item.set('styles', {'display': ''})
							}
						}
					 );
	var thisElements = $(document.body).getElements('tr.photo');	
	thisElements.each(	function(item, index){		
							if(hideImage){
								item.set('styles', {'display': 'none'})
							}else{
								item.set('styles', {'display': ''})
							}
						}
					 );
}

function sendArticle(url){
	MOOdalBox.open( url, "Envoyer &agrave; un ami",	"520 490" );	
}
