//Disable right mouse click Script
//By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
//For full source code, visit http://www.dynamicdrive.com

var message="Function Disabled!";

///////////////////////////////////
function clickIE4(){
if (event.button==2){
//alert(message);
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
//alert(message);
return false;
}
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("return false");

indice = 1;
newfieldscount = 1;

$(document).ready(function() 
{

	$("#achat_credit_nombre_credit").keyup(   function() 
        {
			var nombre = $("input[name=achat_credit[nombre_credit]]").val();
	        $("#achat_credit_prix").attr('value', nombre);
	        if(nombre<25){
	        	$("#achat_credit_nombre_credit").attr('style', 'background-color:red');
	        	$("#achat_credit_prix").attr('style', 'background-color:red');
	        }
	        else{
	        	$("#achat_credit_nombre_credit").attr('style', 'background-color:white');
	        	$("#achat_credit_prix").attr('style', 'background-color:white');
	        }
        }
	
    );
	

	//Affiche le devise d'un pays de facturation
	$("#utilisateur_idpays_facturation").change(   function() 
	        {
			  	if(this.value != null){
					$("#devise").load(
						$(this).parents('span').attr('title'), { id: this.value }
				    );
			  	}
	        }
		
	    );
	//Affiche le div pour ajouter des diplome dans la page d'inscription d'un rédacteur
	$("#affich_diplome").click(   function() 
	        {
				if($("#ajout_diplome").attr('style')==""){
                	$("#ajout_diplome").attr('style', "display:none");
                }else{
                	$("#ajout_diplome").attr('style', "display:''");
                }
	        }
		
	    );
    //Affiche le div pour ajouter une remarque au rédacteur dans la page commande
    $("#idremarque").click(   function() 
            {
                if($("#remarque").attr('style')==""){
                	$("#remarque").attr('style', "display:none");
                }else{
                	$("#remarque").attr('style', "display:''");
                }
            }
        );

    //Affiche le div pour convertir les crédits d'un rédacteur/traducteur
    $("#convert").click(   function() 
            {
                if($("#paypal").attr('style')==""){
                	$("#paypal").attr('style', "display:none");
                }else{
                	$("#paypal").attr('style', "display:''");
                }
            }
        );
    
    $("#submit").click(   function() 
            {
    			
    			if($('#accept_contrat:checked').val() == undefined){
    				alert("Vous devez cocher la case pour accepter les Conditions Générales d'Utilisation ainsi que le Contrat d'Achat de Crédits");
    				return false;
    			}
    			else{
    				document.paypal.submit();
    			}
            }
    	
        );
    
    $("#accept_panier").click(   function() 
            {
    			
    			if($('#contract_accept:checked').val() == undefined){
    				alert("Vous devez cocher la case pour accepter les conditions  contractuelles.");
    				return false;
    			}
            }
    	
        );
    
    $("#valider_payment").click(   function() 
            {
    			
    			if($('#accept_article_contrat:checked').val() == undefined){
    				alert("Vous devez cocher la case pour accepter les Conditions Générales d'Utilisation ainsi que le Contrat d'Achat de crédits.");
    				return false;
    			}
    			else{
    				document.achatArticle.submit();
    			}
            }
    	
        );
    
    $("#affiche_categorie").click(   function() 
	        {
		    	var popup1 = new Popup('categorie');
		    	popup1.autoHide = false;
		    	popup1.width=400;
		    	popup1.height=400;
		    	popup1.position = "center center";
		    	popup1.constrainToScreen = true;
		    	popup1.style = {'border':'3px solid black','backgroundColor':'white','padding':'1px'};
		    	popup1.show();
	        }
		
	    );
    
    

    //popup page panier
    $("#panier1").mouseover(   function() 
	        {
		    	var popup1 = new Popup('peut_faire');
		    	popup1.width=250;
		    	popup1.height=60;
		    	popup1.reference = this;
		    	popup1.position = "below left";
		    	popup1.constrainToScreen = false;
		    	popup1.style = {'border':'1px solid black','backgroundColor':'white','padding':'1px'};
		    	popup1.show();
	        }
		
	    );
    
    $("#panier2").mouseover(   function() 
	        {
		    	var popup1 = new Popup('ne_peut_faire');
		    	popup1.width=250;
		    	popup1.height=60;
		    	popup1.reference = this;
		    	popup1.position = "below left";
		    	popup1.constrainToScreen = false;
		    	popup1.style = {'border':'1px solid black','backgroundColor':'white','padding':'1px'};
		    	popup1.show();
	        }
		
	    );

    $("#panier3").mouseover(   function() 
	        {
		    	var popup1 = new Popup('detail_contrat');
		    	popup1.width=250;
		    	popup1.height=60;
		    	popup1.reference = this;
		    	popup1.position = "below left";
		    	popup1.constrainToScreen = false;
		    	popup1.style = {'border':'1px solid black','backgroundColor':'white','padding':'1px'};
		    	popup1.show();
	        }
		
	    );
    
    $('.removenew').click(function(e){
	    //e.preventDefault();
	    $(this).parent().remove();
	    //newfieldscount = newfieldscount + 1;
	    
	  });
    
    $('#addLigne').click(function(e){
        e.preventDefault();
        var id = $('#indice').val();
        if(id!=1)indice = id;
        $('ul#language span').append(addNewField(indice));
        indice = indice + 1;

      });
    

     
     $('#addCategorie').click(function(e){
         e.preventDefault();
         newfieldscount = $('#nbarticle').val();
         if(newfieldscount==2) $(".article_libre_suppcategorie_"+newfieldscount).attr('style', "display:none");
   		$('div#categorie').append(addNewCategories(newfieldscount));
   		if(newfieldscount>=2)$("#article_libre_addcategorie").attr('style', 'display:none');
   		newfieldscount = parseInt(newfieldscount) + 1;
   		$('#nbarticle').val(newfieldscount);
   		
	     
       });
     
     
    
     $('#addCategorieCmd').click(function(e){
         e.preventDefault();
         newfieldscount = $('#nbcategorie').val();
         if(newfieldscount==2) $(".commande_suppcategorie_"+newfieldscount).attr('style', "display:none");
         $('div#categorie').append(addNewCategoriesForCmd(newfieldscount));
         if(newfieldscount>=2)$("#commande_addcategorie").attr('style', "display:none");
         newfieldscount = parseInt(newfieldscount) +1;
         $('#nbcategorie').val(newfieldscount);
       });
     
     $('#addCategorieModelCmd').click(function(e){
         e.preventDefault();
         newfieldscount = $('#nbcategorie').val();
         if(newfieldscount==2) $(".commande_suppcategorie_"+newfieldscount).attr('style', "display:none");
         $('div#categorie').append(addNewCategoriesForModel(newfieldscount));
         if(newfieldscount>=2)$("#commande_addcategorie").attr('style', "display:none");
         newfieldscount = parseInt(newfieldscount) +1;
         $('#nbcategorie').val(newfieldscount);
       });

     
     $('#addMot').click(function(e){
         e.preventDefault();
         newfieldscount = $('#nbligne').val();
         if(newfieldscount==2) $(".commande_suppmot_"+newfieldscount).attr('style', "display:none");         $('div#mot').append(addNewMot(newfieldscount));
         if(newfieldscount>=2)$("#commande_addmot").attr('style', "display:none");
         newfieldscount = parseInt(newfieldscount) +1;
         $('#nbligne').val(newfieldscount);

       });

	$('#addLangue').click(function(e){
         e.preventDefault();
         newfieldscount = $('#nblangue').val();
         if(newfieldscount==2) $(".redacteur_supplangue_"+newfieldscount).attr('style', "display:none");         
         $('div#langue').append(addNewLangue(newfieldscount));
         if(newfieldscount>=2)$("#redacteur_addlangue").attr('style', "display:none");
         newfieldscount = parseInt(newfieldscount) +1;
         $('#nblangue').val(newfieldscount);

       });
     
     $('#affich_diplome').click(function(e){
         e.preventDefault();
         numero = $('#nbdiplome').val();
   		$('div#diplome').append(addNewDiplome(numero));
   		if(numero>=2)$("#redacteur_adddiplome").attr('style', 'display:none');
   		numero = parseInt(numero) + 1;
   		$('#nbdiplome').val(numero);
   		
//   		numero = parseInt(numero) + 1;
//   	    $('#nbdiplome').val(numero);
//   	    if(numero==3 || numero==6 || numero==9 || numero==12)$("#redacteur_adddiplome").attr('style', 'display:none');
       });

     
//     $("#addImage").click(   function() 
//             {
//    	 		//alert($('#image1').attr('style'));
//                 if($('#image1').attr('style')=='display: none;'){
//    	 			$("#image1").attr('style', "display:block");
//                 }
//                 else{
//                	 $("#image2").attr('style', "display:block");
//                	 $("#ajoutImage").attr('style', "display:none");
//                 }
//             }
//         );
     
      	 //liste des articles en attente de validation wordissimo
        $("#affiche_w").click(   function() 
                {
       	 			$("#attente_w").attr('style', "display:block");
       	 			$("#attente_c").attr('style', "display:none");
       	 			$("#attente_d").attr('style', "display:none");
       	 			$("#li_w").attr('class', "active");
       	 			$("#li_c").attr('class', "");
       	 			$("#li_d").attr('class', "");
                }
            );
        //liste des articles en attente de validation acheteur
        $("#affiche_c").click(  function() 
                {
       	 			$("#attente_c").attr('style', "display:block");
       	 			$("#attente_w").attr('style', "display:none");
       	 			$("#attente_d").attr('style', "display:none");
       	 			$("#li_w").attr('class', "");
       	 			$("#li_d").attr('class', "");
       	 			$("#li_c").attr('class', "active");
                }
            );
        
        $("#affiche_d").click(  function() 
                {
       	 			$("#attente_d").attr('style', "display:block");
       	 			$("#attente_w").attr('style', "display:none");
       	 			$("#attente_c").attr('style', "display:none");
       	 			$("#li_w").attr('class', "");
       	 			$("#li_c").attr('class', "");
       	 			$("#li_d").attr('class', "active");
                }
            );
     $('#redacteur_idexpertise1').dblclick(function(e){
    	 append(document.forms['redacForm'].elements['redacteur[idexpertise1][]'],document.forms['redacForm'].elements['redacteur[idexpertise][]'],true);
     });
     
     $('#client_iddomaine1').dblclick(function(e){
    	 append(document.forms['clientForm'].elements['client[iddomaine1][]'],document.forms['clientForm'].elements['client[iddomaine][]'],true);
     });
     
     $("#commande_directe").mouseover(   function() 
 	        {
 		    	var popupc = new Popup('commandeDirecte');
 		    	popupc.width=300;
 		    	popupc.height=80;
 		    	popupc.reference = this;
 		    	popupc.position = "below left";
 		    	popupc.constrainToScreen = false;
 		    	popupc.style = {'border':'1px solid black','backgroundColor':'white','padding':'1px'};
 		    	popupc.show();
 	        }
 		
 	    );
     
     $('#lien_accept').click(function(e){
         
    		$("#accepterPopup").attr('style', 'display:none');
 	     
        });
});  

//fonction pour passer des éléments d'une liste à une autre
function append (s1,s2,selected) {
	var size1 = s1.length;
	var tab = new Array();
	var j=0;
	for(i=0; i<size1; i++){
		if(s1.options[i].selected){
			op = new Option();
			op.text = s1.options[i].text;
			op.value = s1.options[i].value; 
			
			c = s2.length; // current value of select 2 
			 
			// assign to left list
			s2.options[c] = op;
			s2.options[c].selected=selected;
			
		
			tab[j]=op.value;
			j++;
		}
	}
	for(k=0;k<tab.length;k++){
		for(i=0;i<s1.length;i++){
		  if(s1.options[i].value == tab[k]){
			s1.options[i] = null;
			break;
		  }
			
		}
	}

	if(selected){
		for(k=0;k<s2.length;k++){
			s2.options[k].selected=selected;
		}

	}

}

function submitList(s1,s2){
	var s3 = document.getElementById(s1);
	var s4 = document.getElementById(s2);
	for(k=0;k<s4.length;k++){
		for(i=0;i<s3.length;i++){
		  if(s3.options[i].value == s4.options[k].value){
			s3.options[i] = null;
			break;
		  }
			
		}
		
		s4.options[k].selected=true;
	}
}

function nextPage(page){
	var p = document.getElementById('current_page');
	p.value=page;
	document.forms['pagination'].submit();
}

function addNewField(num){
  return $.ajax({
    type: 'GET',
    url: url1+'?num='+num,
    async: false
  }).responseText;
}

function addNewCategories(num){
	  return $.ajax({
	    type: 'GET',
	    url: url2+'?num='+num,
	    async: false
	  }).responseText;
}

function addNewCategoriesForCmd(num){
	  return $.ajax({
	    type: 'GET',
	    url: url3+'?num='+num,
	    async: false
	  }).responseText;
}

function addNewMot(num){
	  return $.ajax({
	    type: 'GET',
	    url: url4+'?num='+num,
	    async: false
	  }).responseText;
}

function addNewCategoriesForModel(num){
	  return $.ajax({
	    type: 'GET',
	    url: url5+'?num='+num,
	    async: false
	  }).responseText;
}

function addNewLangue(num){
	  return $.ajax({
	    type: 'GET',
	    url: url6+'?num='+num,
	    async: false
	  }).responseText;
	}

//function addNewDiplome(num){
//	  return $.ajax({
//	    type: 'GET',
//	    url: url5+'?num='+num,
//	    async: false
//	  }).responseText;
//	}
//affiche le div des pièces justificative selon le niveau sélectionné
function afficheDiv(obj)
{
	var id = obj.id;
	
	var div = document.createElement("div");
	var input = document.createElement("input");
	d = id.search('new');
	if(d!=-1){
		var indice = id.substring(0,14);
		var name = obj.name.substring(0,14);
	}
	else{
		var indice = id.substring(0,30);
		var name = obj.name.substring(0,30);
	}

	if(!document.getElementById(indice+'_piece_justificative')){
		input.setAttribute("id", indice+'_piece_justificative');
		input.setAttribute("name", name+'][piece_justificative]');
		input.setAttribute("type", "file");
		div.appendChild(document.createTextNode('Pièce justificative'));
		div.appendChild(input);
		$('#'+id).after(div);

	}

	
}
//affiche popup rédacteur dans la page résultat de recherche
function afficheRedacteurPopup(id,objet){
	var popup1 = new Popup('info_redacteur'.id);
	popup1.width=250;
   	popup1.height=100;
   	popup1.reference = objet;
   	popup1.position = "top adjacent-right";
   	popup1.constrainToScreen = false;
   	popup1.style = {'border':'1px solid black','backgroundColor':'white','padding':'1px'};
   	popup1.show();
	        
}

function deleteCategorie(id){
	idcategorie = $("select[name='article_libre[ArticleLibreCategorie]["+(id-1)+"][idcategorie]'] option:selected").val();
	 idarticle = $("input[name='article_libre[id]']").val();
	selectorName = '.w_selectbox_' ; 
	$(selectorName+id).remove(); 
	 if(id==3) { ida = id-1;  $(".article_libre_suppcategorie_"+ida).attr('style', "display:inline");  } 
	 $("#article_libre_addcategorie").attr('style', 'display:inline');
	 numero = $('#nbarticle').val();
	 numero = parseInt(numero) - 1;
	 $('#nbarticle').val(numero);
	 if(idcategorie!=null && idarticle!=null){
			$.ajax({
			    type: 'GET',
			    url: url11+'?idcategorie='+idcategorie+'&idarticle='+idarticle,
			    async: false
			  });
	}
}
	
function commanderArticle(url){
	//$(location).attr('href',url);
	window.open(url);
}

function deleteCategorieModel(id){
	 idcategorie = $("select[name='model_commande[ModelCommandeCategorie]["+(id-1)+"][idcategorie]'] option:selected").val();
	 idmodele = $("input[name='model_commande[id]']").val();
	 selectorName = '.w_selectbox_' ; 
	 $(selectorName+id).remove(); 
	 if(id==3){ida = id-1;  $(".commande_suppcategorie_"+ida).attr('style', "display:inline");}
	 $("#commande_addcategorie").attr('style', 'display:inline');
	 numero = $('#nbcategorie').val();
	 numero = parseInt(numero) - 1;
	 $('#nbcategorie').val(numero);
	 if(idcategorie!=null && idmodele!=null){
			$.ajax({
			    type: 'GET',
			    url: url10+'?idcategorie='+idcategorie+'&idmodele='+idmodele,
			    async: false
			  });
		}
}

function deleteCategorieCmd(id){
	idcategorie = $("select[name='commande[CommandeCategorie]["+(id-1)+"][idcategorie]'] option:selected").val();
	 idcommande = $("input[name='commande[id]']").val();
	selectorName = '.w_selectbox_' ; 
	 $(selectorName+id).remove(); 
	 if(id==3){ida = id-1;  $(".commande_suppcategorie_"+ida).attr('style', "display:inline");}
	 $("#commande_addcategorie").attr('style', 'display:inline');
	 numero = $('#nbcategorie').val();
	 numero = parseInt(numero) - 1;
	 $('#nbcategorie').val(numero);
	 if(idcategorie!=null && idcommande!=null){
			$.ajax({
			    type: 'GET',
			    url: url9+'?idcategorie='+idcategorie+'&idcommande='+idcommande,
			    async: false
			  });
		}
}

function deleteExpression(id){
	idexpression = $("input[name='commande[MotsAOptimiser]["+(id-1)+"][id]']").val();

	selectorName = '#mot' ; 
	$(selectorName+id).remove(); 
	if(id==3) { ida = id-1;  $(".commande_suppmot_"+ida).attr('style', "display:inline");  }
	$("#commande_addmot").attr('style', 'display:inline');
	numero = $('#nbligne').val();
	numero = parseInt(numero) - 1;
	$('#nbligne').val(numero);
	if(idexpression!=null){
		$.ajax({
		    type: 'GET',
		    url: url8+'?idexpression='+idexpression,
		    async: false
		  });
	}
}

function deleteLangue(id){
	idlangue = $("select[name='redacteur[LangueRedacteur]["+(id-1)+"][idlangue]'] option:selected").val();
	idniveau = $("select[name='redacteur[LangueRedacteur]["+(id-1)+"][idniveau]'] option:selected").val();
	selectorName = '#langue' ; 
	$(selectorName+id).remove(); 
	if(id==3) { ida = id-1;  $(".redacteur_supplangue_"+ida).attr('style', "display:inline");  }
	$("#redacteur_addlangue").attr('style', 'display:inline');
	numero = $('#nblangue').val();
	numero = parseInt(numero) - 1;
	$('#nblangue').val(numero);
	if(idlangue!=null && idniveau!=null){
		$.ajax({
		    type: 'GET',
		    url: url7+'?idniveau='+idniveau+'&idlangue='+idlangue,
		    async: false
		  });
	}
	
	
}
//function deleteDiplome(id){
////	selectorName = '.w_selectbox_' ; 
////	$(selectorName+id).hide(); 
////	$(selectorName+id+' select option:first').attr("selected", "selected");
////	$(selectorName+id+' .ddTitleText .ddTitleText').text($(selectorName+id+' select option:first').text());
////	$(selectorName+id).empty(); 
////
////	$("#redacteur_adddiplome").attr('style', 'display:block');
//	selectorName = '.w_selectbox_' ; 
//	 $(selectorName+id).hide(); 
//	 $(selectorName+id).empty(); 
//	 $("#redacteur_adddiplome").attr('style', 'display:block');
//	 numero = $('#nbdiplome').val();
//	 numero = parseInt(numero) - 1;
//	 $('#nbdiplome').val(numero);
//}

function updateNbArticle()
{
	$.ajax({
	type: 'GET',
	url: url1,
	success: function(data){$('#online').html(data);}
	});
}

function testDureePanier()
{
	$.ajax({
	type: 'GET',
	url: urlpanier,
	async: false
	});

}

function afficherNiveauRedacteur(pseudo)
{
	$.ajax({
	type: 'GET',
	url: urlR+'?pseudo='+pseudo,
	success: function(data){$('#afficheRedact').html(data);}
	});
}

function CountWords(id) {
    var this_field = document.getElementById(id);
	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;

	if (fullStr.length <2) {
		word_count = 0;
	}

	return word_count;
}
