String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
		
jQuery.fn.goCollapse = function(zone, visible) {
    if(visible) {
        $(this).prepend('<span class="collapse_moins">');
    }
    else {
        $(this).prepend('<span class="collapse_plus">');
        zone.hide();
    }
    
    $(this)
        .find('span')
            .click(function() {
                $(this).toggleClass('collapse_moins').toggleClass('collapse_plus');
                zone.toggle();
            });
};

jQuery.fn.goEditable = function(settings) {
    settings = jQuery.extend({
        text: 'Cliquez ici pour ajouter du contenu'
    }, settings);
    
    if($(this).val() != '') {
        var val = $(this).val();
        $(this)
            .css('border', '1px solid white')
            .mouseover(function() { $(this).css({ border: '1px solid silver', background: '#edfebe' }); })
            .mouseout(function() { $(this).css({ border: '1px solid white', background: 'white' }); })
            .click(function() { $(this).css({ border: '1px solid silver', background: 'white' }).unbind(); });
    }
    else {
        var html = $(this).parent().html();
        $(this).parent().html('<a href="#">'+settings.text+'</a>').find('a').click(function() { $(this).parent().html(html); });
    }
};

function checkOblig(){	
	var nb_error = 0;
	$('.oblig')
		.each(function() {
          	var data = $(this).val();
          
              if(data.trim() == '') { // champ vide
              
                  $(this)
                      .addClass('error')
                      .focus(function() { $(this).removeClass('error'); });   
                  nb_error++;                  
              }
          });
    if(nb_error != 0) {
              alert('Merci de bien vouloir renseigner tous les éléments encadrés en rouge');
              scrollTo(0,0);
              return false;
          }
   return true;
}
$(document).ready(function() {
    $(this)
        .find('form')
            .submit(function() { // validation des champs obligatoires
                var nb_error = 0;
                $(this)
                    .find('.oblig')
                        .each(function() {
                        	var data = $(this).val();
                        
                            if(data.trim() == '') { // champ vide
                            
                                $(this)
                                    .addClass('error')
                                    .focus(function() { $(this).removeClass('error'); });   
                                nb_error++;
                            }
                        })
                    .end();
                if(nb_error != 0) {
                    alert('Merci de bien vouloir renseigner tous les éléments encadrés en rouge');
                    scrollTo(0,0);
                    return false;
                }
                return true;
            })
        .end();
    //for noborder class style
    
    $('form input.noborder').focus(function(){
	  return false;
	}).mousedown(function(){
	  return false;
	}) ;
     //set width for .flexible_list_title
    var flexible_list_title_width = 0;    
    $.each($('.flexible_list_title'), function() {
    	if($(this).width()>flexible_list_title_width)
    		flexible_list_title_width = $(this).width();
    });
    $('.flexible_list_title').width(flexible_list_title_width);
    $('#jsddm').css({'width':$('#jsddm').width()+'px','float':'none'});
    
    //-----------override style in chrome------------//
	if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
		$(window).load(function(){
		    $('input:-webkit-autofill').each(function(){
		        var text = $(this).val();
		        var name = $(this).attr('name');
		        $(this).after(this.outerHTML).remove();
		        $('input[name=' + name + ']').val(text);
		    });
		});
	}
    if(front_) changeButton_();
});


/**********************************************************************************
 * To remove all accent characters, replace space by hiphen and return small letter  
 **********************************************************************************/
function removeAccents(str) {
	
	str = str.toLowerCase();

	str = str.replace(/[\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5]/g,'a');
	str = str.replace(/[\u00E7]/g,'c');
	str = str.replace(/[\u00E8\u00E9\u00EA\u00EB]/g,'e');
	str = str.replace(/[\u00EC\u00ED\u00EE\u00EF]/g,'i');
	str = str.replace(/[\u00F2\u00F3\u00F4\u00F5\u00F6\u00F8]/g,'o');
	str = str.replace(/[\u00F9\u00FA\u00FB\u00FC]/g,'u');
	str = str.replace(/[\u00FD\u00FF]/g,'y');
	str = str.replace(/[\u00F1]/g,'n');
	str = str.replace(/[\u0153]/g,'oe');
	str = str.replace(/[\u00E6]/g,'ae');
	str = str.replace(/[\u00DF]/g,'ss');

	str = str.replace(/[^a-z0-9_\s\'\:\/\[\]-]/g,'');
	str = str.replace(/[\s\'\:\/\[\]-]+/g,' ');
	str = str.replace(/[ ]/g,'-');

	return str;
	
}

function minString(obj,val){
	//obj.length;	
	if($(obj).val().length < val) {
		return $(obj).attr('alt')+' require at least '+(val)+' characters';
	}else{return true;}
	
}

function getOffsetLeft(object){
	offset = $(object).offset(); 
	if($.browser.msie && $.browser.version<8){
		//alert(offset.left/2)+10;
		return (offset.left);
	}
	return(offset.left);	
}
function autoPairClass(){//parent_class_name,tag_child
	parent_class_name = arguments[1]?arguments[1]:'auto_pair';
	tag_child = arguments[2]?arguments[2]:'tr';
	jQuery.each($("."+parent_class_name+" > "+tag_child), function(i, val) {
		$(this).attr('class',(i%2?'impair':'pair'));
    });
}
pngfix_=function(els){
	if($.browser.msie && $.browser.version>6 || !$.browser.msie)return;
	var ip=/\.png/i;
	var i=$(els).size();
	if(i==1){
		var el=els;
		//var es=el.style;
		var es=$(el).attr('style');
		if($(el).attr('src')&&($(el).attr('src')).match(ip)&&!$(el).css('filter')){			
			//es.height=el.height;
			$(el).attr('height',$(el).height());
			//es.width=el.width;
			$(el).attr('width',$(el).width());
			//es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";			
			$(el).css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')");
			//el.src=clear;
			$(el).attr('src',clear);
		}else{
			//var elb=el.currentStyle.backgroundImage;
			var elb=$(el).css('background-image');
			if(elb.match(ip)){
				var path=elb.split('"');
				var original_width = $(el).width();
				//var rep=(el.currentStyle.backgroundRepeat=='no-repeat')?'crop':'scale';
				var rep=($(el).css('background-repeat')=='no-repeat')?'crop':'scale';
				//es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')";
				$(el).css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')");
				//alert(el.clientHeight+"=="+$(el).height()+'cssheight:'+$(el).css('height'));
				$(el).css('height',$(el).height());
				//alert($(el).width()+'=='+$(els).width()+'=='+original_width);
				$(el).css('width',original_width);
				$(el).css('background-image','none');
				//es.backgroundImage='none';
				//$('#mh_jouer').text('=>'+es.ClassName+'<=');					
			
			}
		}
	}
}

//**************************************//
function htmlspecialchars (string, quote_style, charset, double_encode) {
    // http://kevin.vanzonneveld.net
    // +   original by: Mirek Slugen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nathan
    // +   bugfixed by: Arno
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Ratheous
    // +      input by: Mailfaker (http://www.weedem.fr/)
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +      input by: felix
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: charset argument not supported
    // *     example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
    // *     returns 1: '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;'
    // *     example 2: htmlspecialchars("ab\"c'd", ['ENT_NOQUOTES', 'ENT_QUOTES']);
    // *     returns 2: 'ab"c&#039;d'
    // *     example 3: htmlspecialchars("my "&entity;" is still here", null, null, false);
    // *     returns 3: 'my &quot;&entity;&quot; is still here'

    var optTemp = 0, i = 0, noquotes= false;
    if (typeof quote_style === 'undefined' || quote_style === null) {
        quote_style = 2;
    }
    string = string.toString();
    if (double_encode !== false) { // Put this first to avoid double-encoding
        string = string.replace(/&/g, '&amp;');
    }
    string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;');

    var OPTS = {
        'ENT_NOQUOTES': 0,
        'ENT_HTML_QUOTE_SINGLE' : 1,
        'ENT_HTML_QUOTE_DOUBLE' : 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE' : 4
    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
        quote_style = [].concat(quote_style);
        for (i=0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;
            }
            else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }
        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/'/g, '&#039;');
    }
    if (!noquotes) {
        string = string.replace(/"/g, '&quot;');
    }

    return string;
}
//=======Change button========//
function changeButton(){
	$("input[type='button'],input[type='submit']").each(function(){
		var pr = $(this).parent();
		var nr = $(this).next();
		var clas = $(this).attr('class');
		var cc = $(pr).attr('class');
//alert(cc);
	if(cc){
		if($(this).css("display")=="none"){		
			if(!cc.match(/btn_right/)){		
			$(this).wrap("<span class='btn_right "+clas+"'' style='display:none;'></span>");
			}
		}
		else{		
			if(!cc.match(/btn_right/)){
				$(this).wrap("<span class='btn_right "+clas+"''></span>");
			}	
		}
	}else{
		//$(this).wrap("<span class='btn_right "+clas+"''></span>");
	}
	});
}
function changeButton_(){
	$("input[type='button'],input[type='submit'],a.button").each(function(){
		var pr = $(this).parent();
		var nr = $(this).next();
		var clas = $(this).attr('class');
		
		var cc = $(pr).attr('class');
		if(cc){
			if(!cc.match(/btn_right/)){		
				$(this).wrap("<span class='btn_right "+clas+"'></span>");
			}		
		}else{
			if(clas != '' && clas != undefined){
				if($(this).attr('type') == 'submit') clas.replace(/submit/i, "");
			}
	
			if($(this).css("display")=="none"){
				if($(this).attr("id")=="end_report")$(this).wrap("<span class='monthly_btn_right "+clas+"'' style='display:none;'></span>");
				else $(this).wrap("<span class='btn_right "+clas+"'' style='display:none;'></span>");
			}
			else{
				if($(this).attr("id")=="end_report")$(this).wrap("<span class='monthly_btn_right "+clas+"''></span>");	
				else $(this).wrap("<span class='btn_right "+clas+"''></span>");			
			}
		}
	});
}
