String.prototype.replaceAll = function (stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while(index != -1){
    	temp = temp.replace(stringToFind,stringToReplace);
    	index = temp.indexOf(stringToFind);
    }
    return temp;
};

if ( typeof String.prototype.trim !== 'function' ) {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g, ''); 
	};
}

/**
@param elemid
 */
function showhide(elemid) {
	var elem = document.getElementById(elemid);
	if (elem.style.display == 'none' || elem.style.display == '') {
		elem.style.display = 'block';
	} else {
		elem.style.display = 'none';
	}
} 

/**
 *  Load javascript on colse message button
 */
function activateCloseFeedbackMessageButton(){
    $(".hide").click(
        function () {
            $(this).parent().fadeTo(500, 0, function () { // Links with the class "close" will close parent
                $(this).slideUp(500);
            });
            return false;
        }
        );
}

/*
 * Fonction permettant de traiter le forumalire facebox d'envoi du jeton d'un document
 */
function Sendfriend() {    
                
    var noError = true;

    var message = $("#facebox p#send-message textarea").val();      
    var email 	= $("#facebox p#send-emails input").val(); 
    var name 	= $("#facebox p#send-name input").val();
    var parag 	= $("#facebox #sendFriendForm #paragId").val(); 

    if ($("#facebox p#send-name").css("display") == "block" && name == "") {  
        $("#facebox p#send-name span").text("Merci de compléter le champ"); 
        $("#facebox p#send-name").addClass("error");
        noError = false;  
    }
    else{
        $("#facebox p#send-name span").text(""); 
        $("#facebox p#send-name").removeClass("error");
    }
    
    if (message == "") {  
        $("#facebox p#send-message span").text("Merci de compléter le champ"); 
        $("#facebox p#send-message").addClass("error");
        noError = false;  
    }
    else{
        $("#facebox p#send-message span").text(""); 
        $("#facebox p#send-message").removeClass("error");
    }


    if (email == "") {  
        $("#facebox p#send-emails span").text("Merci de compléter le champ"); 
        $("#facebox p#send-emails").addClass("error");
        noError = false;  
    }else {
        $("#facebox p#send-emails span").text(""); 
        $("#facebox p#send-emails").removeClass("error");            
    }

    if(!noError)
        return false;
    
    $.ajax({
        type: 'POST',
        url: root_url + '/www/ajax/sendTokenFriend.ajax.php',
        data: {
            'email'    	: email,
            'name'     	: name,
            'message'   : message,
            'parag'   	: parag
        },
        success: function(r){
                
            var response = eval('(' + r + ')');               
                
            var type = " ";
                
            if(response.feedback_message_type){
                type = response.feedback_message_type;
            }
              
            if(response.feedback_message && type){
            	$("#facebox").find("#ajaxFeedBack").each(
                		function() {
                			$(this).show();
                			$(this).css({opacity: 1});
                			$(this).addClass('msg-'+type);
                			$(this).find(".hide").each(function () {$(this).addClass('hide-'+type);});
                			return true;
                		}
                );
                $('#facebox #ajaxFeedBackErrorMsg').html(response.feedback_message);
                $('#facebox #feedbackType').addClass('img-'+type);
                    
                if(response.email_error){
                    $('#facebox p#send-emails').addClass('error');
                    $('#facebox p#send-emails span').html(response.email_error);
                    $('#facebox p#send-emails span').show();
                }
                        
                if(response.message_error){
                    $('#facebox p#send-message').addClass('error');
                    $('#facebox p#send-message span').html(response.message_error);
                    $('#facebox p#send-message span').show();
                }
                        
                    
            }
            if(response.status){
                $('#facebox')
                .delay(1500)
                .fadeOut(300, function() {
                    $('#facebox').hide();
                        
                    // reset feedback message
                    $('#facebox  #ajaxFeedBack').hide();  
                    $('#facebox  #ajaxFeedBackErrorMsg').html('');
                    $('#facebox  #feedbackType').removeClass('img-'+type);
                    $('#facebox  #ajaxFeedBack .hide').removeClass('hide-'+type);
                    $('#facebox  #ajaxFeedBack').removeClass('msg-'+type);
                    
                    $('#facebox  p#send-emails').removeClass('error');
                    $('#facebox  p#send-emails span').html('');
                    $('#facebox  p#send-emails span').hide();
                    
                    $('#facebox  p#send-message').removeClass('error');
                    $('#facebox  p#send-message span').hide();   
                    $('#facebox  p#send-message span').html('');
                        
                        
                        
                });
                    
            }
                
        }
    });


    return false;  

}


/**
 *  Fonction permettant de controller et de soumettre le formulaire d'impression
 */
function printBox() { 
    var noError = true;
    var body = 0;                   
    var notes  = 0; 
    var remarks = 0;    
    $("#facebox").find("#printBody").each(
    		function() {
    			if ( $(this).attr('checked') ) {
    				body=1;
    			}
    		}
    );
    $("#facebox").find("#printNotes").each(
    		function() {
    			if ( $(this).attr('checked') ) {
    				notes=1;
    			}
    		}
    );
    $("#facebox").find("#printRemarks").each(
    		function() {
    			if ( $(this).attr('checked') ) {
    				remarks=1;
    			}
    		}
    );
    //if nothing was checked
    if ((body + notes + remarks) == 0){      
        $("#facebox").find("#printAjaxFeedBack").each(
        		function() {
        			$(this).show();
        			$(this).css({opacity: 1});
        			$(this).find("p").each(function () {$(this).text("Veuillez cocher au minimum un élément")});
        			return true;
        		}
        );
        noError = false;
    }
    if(!noError) {
        return false;
    }
    else{
    	$("#facebox").find("#printAjaxFeedBack").each(
        		function() {
        			$(this).hide();
        			$(this).find("p").each(function () {$(this).text("")});
        			return true;
        		}
        );
        $("#facebox").find("#printSelectionForm").each(
        		function() {
        			$(this).submit();
        			return true;
        		}
        );
        return true;  
    }
}


/**
 *  Fonction permettant de controller et de soumettre le formulaire d'impression
 */
function pdfBox() { 
    var noError = true;
    var body = 0;                   
    var notes  = 0; 
    var remarks = 0;
    $("#facebox").find("#pdfBody").each(
    		function() {
    			if ( $(this).attr('checked') ) {
    				body=1;
    			}
    		}
    );
    $("#facebox").find("#pdfNotes").each(
    		function() {
    			if ( $(this).attr('checked') ) {
    				notes=1;
    			}
    		}
    );
    $("#facebox").find("#pdfRemarks").each(
    		function() {
    			if ( $(this).attr('checked') ) {
    				remarks=1;
    			}
    		}
    );
    //if nothing was checked
    if ((body + notes + remarks) == 0){      
        $("#facebox").find("#pdfAjaxFeedBack").each(
        		function() {
        			$(this).show();
        			$(this).css({opacity: 1});
        			$(this).find("p").each(function () {$(this).text("Veuillez cocher au minimum un élément")});
        			return true;
        		}
        );
        noError = false;
    }
    if(!noError) {
        return false;
    }
    else{
    	$("#facebox").find("#pdfAjaxFeedBack").each(
        		function() {
        			$(this).hide();
        			$(this).find("p").each(function () {$(this).text("")});
        			return true;
        		}
        );
        $("#facebox").find("#pdfSelectionForm").each(
        		function() {
        			$(this).submit();
        			return true;
        		}
        );
        return true;  
    }
}



jQuery(document).ready(function($) {
    
    // 
    //    Suppression de l'avatar par Ajax
    //    
    $("#delete_avatar").click(function(e){
        $.ajax({
            type: 'POST',
            url: '/www/ajax/deleteAvatar.ajax.php',       
            success: function(response){                                   
                var data = response.split('###SEPARATOR###');
                // on remplace l'image affichée par l'image par défaut, renvoyé par ajax.
                $("#photo").attr('src', data[1]);              
                // La réponse passée par la fonction est le message feedback à afficher.                
                // on supprime un eventuel feedback message existant
                if($(".msg").attr('class')){
                    $(".msg").remove();
                }                                   
                // on insère notre réponse
                $(".onglets").after(data[0]);     
                            
                $("#delete_avatar").remove();
            }
        }); 
           
    }); 

});
