// JavaScript Document for BLOG module

	$(document).ready(function() {
							   
						$("form#blogf1").submit(function() {
																 
							 var theForm = $(this) 
							 var theFormID = $(this).attr('id') 
							 var theName = $("#" + theFormID + " input#name").val();
							 var theEmail = $("#" + theFormID + " input#email").val();
							 var thePostMessage = $("#" + theFormID + " textarea#main").val();
							 var themessage = 'S.v.p vérifier le(s) champ(s) suivant: \n'
							 
							 var hasError = false;
		                     var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
							 $("#" + theFormID + " .error").remove(); 
							 
							 
							if(theName == '') {
								$("#" + theFormID + " input#name").after('<span class="error">*Erreur</span>');
								themessage = themessage + "\n- NOM";
								hasError = true;
							}

							if(theEmail == '') {
								$("#" + theFormID + " input#email").after('<span class="error">*Erreur</span>');
								themessage = themessage + "\n- EMAIL";
								hasError = true;
							} else if(!emailReg.test(theEmail)) {
								$("#" + theFormID + " input#email").after('<span class="error">*Erreur</span>');
								themessage = themessage + "\n- EMAIL";
								hasError = true;
							}
					
							if(thePostMessage == '') {
								$("#" + theFormID + " textarea#main").before('<span class="error">*Erreur</span>');
								themessage = themessage + "\n- COMMENTAIRE";
								hasError = true;
							}
							
							
						  if(hasError == false) {
							  //theForm.submit();
//							 var sPath = window.location.pathname;
//							 var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
													  
								$.ajax({
									type: "POST",
									//url: sPage,
									url: "ajax/blog.asp",
									cache: false,
									data: $("form#"+ theFormID).serialize(),
									success: function(msg){
										if(msg.indexOf("1OK1") > -1 ){
											alert('Error');
										} else {
											//alertMes(msg);
											$(".commentsForm").html(msg);
										}					
									}
								});				   
							  
							  
							  
						  }	else {
							  
							  alert(themessage);
							  return false;
						 }						
						   return false;
						 });





		 var sPath = window.location.pathname;
		 var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
				
		jQuery("a")
			.filter(".rate_1")
			
				.click(function(){
				
				   var theId=jQuery(this).attr("rel");	
              	       theId=theId.substr(2,theId.length-1);

					jQuery.get(sPage, {
						rate: '1',
						blograte: 'true',
						id: theId
					}, function(res) {
						jQuery(".ratingbox"+theId).html(res);
					});
					return false;

				})
			.end()
			.filter(".rate_2")
				.click(function(){
				
				   var theId=jQuery(this).attr("rel");	
              	       theId=theId.substr(2,theId.length-1);
				
					jQuery.get(sPage, {
						rate: '2',
						blograte: 'true',
						id: theId
					}, function(res) {
						jQuery(".ratingbox"+theId).html(res);
					});
					return false;

				})
			.end()
			.filter(".rate_3")
				.click(function(){
				
				   var theId=jQuery(this).attr("rel");	
              	       theId=theId.substr(2,theId.length-1);
				
					jQuery.get(sPage, {
						rate: '3',
						blograte: 'true',
						id: theId
					}, function(res) {
						jQuery(".ratingbox"+theId).html(res);
					});
					return false;

				})
			.end()
			.filter(".rate_4")
				.click(function(){
				
				   var theId=jQuery(this).attr("rel");	
              	       theId=theId.substr(2,theId.length-1);
				
					jQuery.get(sPage, {
						rate: '4',
						blograte: 'true',
						id: theId
					}, function(res) {
						jQuery(".ratingbox"+theId).html(res);
					});
					return false;

				})
			.end()
			.filter(".rate_5")
				.click(function(){
				
				   var theId=jQuery(this).attr("rel");	
              	       theId=theId.substr(2,theId.length-1);
				
					jQuery.get(sPage, {
						rate: '5',
						blograte: 'true',
						id: theId
					}, function(res) {
						jQuery(".ratingbox"+theId).html(res);
					});
					return false;

				})
			.end();

})
	
function pausecomp(millis) {
	date = new Date();
	var curDate = null;
	
	do { var curDate = new Date(); }
	while(curDate-date < millis);
}


var whichIsFocus;
function AddHTML(X){
  switch (whichIsFocus) {
  default:
  document.blogf1.main.value+=X
  break;
  }
}


  function getAvatar() {
	alert('Coming soon!\n\nBut you can put your own avatar full url!');
    jQuery.getJSON('http://bin.pcpixel.com/avatars/avatars.asp', function(data) {
      jQuery('#showimage').empty();
			  jQuery.each(data, function(entryIndex, entry) {
				var html = '<div class="entry">';
				html += '<h3 class="term">' + entry['nom'] + '</h3>';
				html += '<div class="part">' + entry['email'] + '</div>';
				html += '<div class="definition">';
				html += entry['idclient'];
				if (entry['nom']) {
				  html += '<div class="quote">';
				  $.each(entry['nom'], function(lineIndex, line) {
					html += '<div class="quote-line">' + line + '</div>';
				  });
				  if (entry['email']) {
					html += '<div class="quote-author">' + entry['email'] + '</div>';
				  }
				  html += '</div>';
				}
				html += '</div>';
				html += '</div>';
				$('#showimage').append(html);
			  });
    });
  };
			

