function findObj(n, d) {
  var p,i,x;  if(!d) d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
/*
dt = new Date();
mc_tmp = dt.getMonth() + 1;
if(mc_tmp < 10)
	mc_tmp = "0" + mc_tmp;
ac_tmp = "" + dt.getFullYear();

var MC = mc_tmp;
var AC = ac_tmp.substr(2, 2);*/

function strToInt(v) {
	return parseInt(v, 10);
}
			
function complete_date(obj) {
	if(obj.value.length == 2)
		obj.value += MC + AC;
	if(obj.value.length == 4)
		obj.value += AC;
	complete_date2(obj);
}

function complete_date2(obj) {
	if(obj.value.length == 6) {
		if(obj.name == "date_1") {
			if(obj.form.name == "tdb") {
				date_ajoute_7_jour(obj.value, obj.form.elements['date_2']);
			}
			else {
				date_ajoute_1_jour(obj.value, obj.form.elements['date_2']);
			}
		}
		if(obj.name == "date_3") {
			date_ajoute_1_jour(obj.value, obj.form.elements['date_4']);
		}
	}
}

function date_ajoute_1_jour(dte, chp) {
	var dt1 = new Date(2000,1,1);
	dt1.setMonth(strToInt(dte.substr(2, 2))-1);
	dt1.setFullYear(strToInt("20"+dte.substr(4, 2)));
	dt1.setDate(strToInt(dte.substr(0, 2))+1); // On ajoute 1 jour
	
	chp.value = date2chpstr(dt1);
}

function date_ajoute_7_jour(dte, chp) {
	var dt1 = new Date(2000,1,1);
	dt1.setMonth(strToInt(dte.substr(2, 2))-1);
	dt1.setFullYear(strToInt("20"+dte.substr(4, 2)));
	dt1.setDate(strToInt(dte.substr(0, 2))+7); // On ajoute 7 jour
	
	chp.value = date2chpstr(dt1);
}

function date2chpstr(dt) {
	var j,m,a,s;
	j = ""+dt.getDate();
	m = ""+(dt.getMonth()+1);
	a = ""+dt.getFullYear();
	
	if(j.length == 1)
		j = "0"+j;
	if(m.length == 1)
		m = "0"+m;
	a = a.substr(2, 2);
	return j+m+a;
}

function display_message_infos(msg) {
	var elt = document.getElementById('message-infos');
	elt.innerHTML = "&raquo; " + msg + " &laquo;";
	elt.style.top = (document.body.scrollTop + (getWindowHeight() / 2) - 50);
	elt.style.left = (document.body.scrollLeft + (getWindowWidth() / 2) - 115);
	elt.style.display = 'inline';
	
	// Pour IE : on n'affiche plus les listbox
	if(navigator.appVersion.indexOf("MSIE") != -1) {
		var sEls = document.getElementsByTagName("select");
		for(var i = 0 ; i < sEls.length ; i++) {
			sEls[i].style.visibility = 'hidden';
		}
	}
	
	setTimeout('clear_message_infos()', 3000);
}
function clear_message_infos() {
	var elt = document.getElementById('message-infos');

	// Pour IE
	if(navigator.appVersion.indexOf("MSIE") != -1) {
		var sEls = document.getElementsByTagName("select");
		for(var i = 0 ; i < sEls.length ; i++) {
			sEls[i].style.visibility = 'visible';
		}
	}


	elt.style.display = 'none';
	elt.innerHTML = "";
}


function toInt(v) {
	return parseInt(v, 10);
}

function arrondi(val) {
	// arrondi 2 chiffre apres la virgule
	return Math.round(val * 100) / 100;
}

function format_prix(prix) {
	prix = ""+prix;
	if(prix == "NaN")
		return "0.00";
	if(prix.indexOf(".") != -1) {
		var t = prix.substr(prix.indexOf(".")+1);
		if(t.length == 1)
			return prix+"0";
		else
			return prix;
	}
	else
		return prix+".00";
}

function dt2str(dt) {
	var j,m,a,s;
	j = ""+dt.getDate();
	m = ""+(dt.getMonth()+1);
	a = ""+dt.getFullYear();
	
	if(j.length == 1)
		j = "0"+j;
	if(m.length == 1)
		m = "0"+m;
	a = a.substr(2, 2);
	return j+m+a;
}

function array_key_exists(key, haystack) {
	for(var clef in haystack) {
		if(key == clef)
			return true;
	}
	return false;
}

function in_array(needle, haystack) {
	for(var i = 0 ; i < haystack.length ; i++) {
		if(needle == haystack[i])
			return true;
	}
	return false;
} 

function getWindowHeight() {
    var windowHeight=0;
    if (typeof(window.innerHeight)=='number') {
        windowHeight=window.innerHeight;
    }
    else {
     if (document.documentElement&&
       document.documentElement.clientHeight) {
         windowHeight = document.documentElement.clientHeight;
    }
    else {
     if (document.body&&document.body.clientHeight) {
         windowHeight=document.body.clientHeight;
      }
     }
    }
    return windowHeight;
}


function getWindowWidth() {
    var windowWidth=0;
    if (typeof(window.innerWidth)=='number') {
        windowWidth=window.innerWidth;
    }
    else {
     if (document.documentElement&&
       document.documentElement.clientWidth) {
         windowWidth = document.documentElement.clientWidth;
    }
    else {
     if (document.body&&document.body.clientWidth) {
         windowWidth=document.body.clientWidth;
      }
     }
    }
    return windowWidth;
}

//
// recup la val d'info 
//
var info = '';
if(top) {
	var url = top.location.href;
	var tmp = url.match(/\?info=([^&]+)/);
	if(tmp)
		info = unescape(tmp[1]);
}




/*
sfHover = function() {
	var sfEls = document.getElementsByTagName("table");
	for (var i=0; i<sfEls.length; i++) {
		if(sfEls[i].className == "tableau-de-bord") {
			var sfEls2 = sfEls[i].getElementsByTagName("tr");
			for (var j=0; j<sfEls2.length; j++) {
				sfEls2[j].onmouseover=function() {
					this.className+=" effectHover";
				}
				sfEls2[j].onmouseout=function() {
					this.className=this.className.replace(new RegExp(" effectHover\\b"), "");
				}
			}
		}
		
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
*/
function isShow(calque){
  if($('#'+calque).css('display') == "block" || $('#'+calque).css('display') == "" || $('#'+calque).css('display') == null){
	return true;
  }else{
	return false;
  }
}

function showHide(calque,time){
	var time = (time != null) ? time : "fast";
	if(!isShow(calque)){
		show(calque,time);
	}else{
		hide_div(calque,time);
	}
	return false;
}

function show(calque,time){
	var time = (time != null) ? time : "fast";
	if(!isShow(calque)){
		$('#'+calque).slideDown(time);
	}
	return false;
}

function hide(calque,time){
	var time = (time != null) ? time : "fast";
	if(isShow(calque)){
		$('#'+calque).slideUp(time);
	}
	return false;
}

function hide_div(calque,time){
	var time = (time != null) ? time : "fast";
	if(isShow(calque)){
		$('#'+calque).slideUp(time);
	}
	return false;
}

function show_div(calque,time){
	var time = (time != null) ? time : "fast";
	if(!isShow(calque)){
		$('#'+calque).slideDown(time);
	}
	return false;
}

function setContent(calque,txt){
	$('#'+calque).text(txt);
}

function insertDiv(id,id_insert,param,value){
	var param = (param != null) ? param : "";
	var value = (value != null) ? value : "";
	$("#"+id).before("<div id=\""+id_insert+"\" "+param+">"+value+"</div>");
	return false;
}

function insertDivAfter(id,id_insert,param,value){
	var param = (param != null) ? param : "";
	var value = (value != null) ? value : "";
	$("#"+id).after("<div id=\""+id_insert+"\" "+param+">"+value+"</div>");
	return false;
}

function deleteDiv(id){
	hide_div('detail_'+id);
	$("#"+id).remove();
	return false;
}

function moveDiv(id,id_before){
	$("#"+id).after($("#"+id_before));
	return false;
}

function tab_supp(tab,id,ligne){
	var ligne = (ligne != null) ? ligne : -1;
	var obj = document.getElementById(id);
	var nb = obj.rowIndex;
	if(nb == -1 && ligne != -1){
		nb = ligne;
	}
	var tableau = document.getElementById(tab);
	tableau.deleteRow(Number(nb));
	return false;
}

function strpos( haystack, needle, offset){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
	var offset = (offset != null) ? offset : 0;	
    var i = (haystack+'').indexOf( needle, offset ); 
    return i===-1 ? false : i;
}

function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
 
    var s = subject;
    var ra = r instanceof Array, sa = s instanceof Array;
    var f = [].concat(search);
    var r = [].concat(replace);
    var i = (s = [].concat(s)).length;
    var j = 0;
    
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    }
 
    return sa ? s : s[0];
}

function implode( glue, pieces ) {
    // Joins array elements placing glue string between items and return one string  
    // 
    // version: 905.412
    // discuss at: http://phpjs.org/functions/implode
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Waldo Malqui Silva
    // *     example 1: implode(' ', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'Kevin van Zonneveld'
    return ( ( pieces instanceof Array ) ? pieces.join( glue ) : pieces );
}

function explode( delimiter, string, limit ) {
    // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.  
    // 
    // version: 905.412
    // discuss at: http://phpjs.org/functions/explode
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||
        typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === '' ||
        delimiter === false ||
        delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}



function getPourcent(total,value){
	pourcente = (value*100/total);
	return pourcente;
}

function getReduc(total,pourcent){
	return total*pourcent/100;
}


function getTotal(value,pourcent){
	return value*100/pourcent;
}

function ceil(value) {
    // http://kevin.vanzonneveld.net
    // +   original by: Onno Marsman
    // *     example 1: ceil(8723321.4);
    // *     returns 1: 8723322
 
    return Math.ceil(value);
}

function round ( val, precision ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +    revised by: Onno Marsman
    // *     example 1: round(1241757, -3);
    // *     returns 1: 1242000
    // *     example 2: round(3.6);
    // *     returns 2: 4
 
    return parseFloat(parseFloat(val).toFixed(precision));
}

function get_select(index){
	var lst = new Array();
	var i = 0;
	$("input[@name="+index+"]").each(function()
	{
		if(this.checked){
			lst[i] = this.value;
			i++;
		}
	});
	return lst;
}

function set_select(index){
	var flag_check = $("#select_all_"+index).val();
	if(flag_check == 0){
		$("#select_all_"+index).val(1);
		flag_check = true;
	}else{
		$("#select_all_"+index).val(0);
		flag_check = false;
	}
	var lst = new Array();
	var i = 0;
	$("input[@name="+index+"]").each(function()
	{
		$(this).removeAttr("checked");
		$(this).attr("checked",flag_check);
	});
	return false;
}


