
var /*: String :*/ lastError = null ;


/*window.onerror = function() {
};*/


function ajouteUsrApplicationIDalURL(url, usrApplicationID){

	if(url==null){
		return url;
	}

	/*
	if(usrApplicationID==null){
		alert("Erreur: le parametre usrApplicationID doit etre indique pour ce popup.");
	}else if(usrApplicationID.length==0){
		alert("Erreur: il faut renseigner usrApplicationID pour ce popup.");
	}
	*/

	var paramNameUsrApplicationID = "aid";

	url2=url;

	//Si le usrSessionID est renseign� et qu'il ne contient pas de b�tise
	if(usrApplicationID!=null && usrApplicationID.length>0 && usrApplicationID.indexOf("?",0)<0  && usrApplicationID.indexOf("&",0)<0){
		//usrApplicationID ne doit pas �tre d�j� indiqu� dans l'url
		if(url.indexOf("&" + paramNameUsrApplicationID + "=", 0)<0 && url.indexOf("?" + paramNameUsrApplicationID + "=", 0)<0){
			//On ajoute ? ou &
			if(url.indexOf("?",0)<0){
				url2 = url2 + "?";
			}else{
				url2 = url2 + "&";
			}
			//On ajoute le param�tre usrSessionID � l'url
			url2 = url2 + paramNameUsrApplicationID + "=" + usrApplicationID;
		}
	}

	//alert("url modif=" + url2);

	return url2;
}


//Ajouter usrSessionID � l'URL
function ajouteUsrSessionIDalURL(url, usrSessionID){

	if(url==null){
		return url;
	}

	/*
	if(usrSessionID==null){
		alert("Erreur: le parametre usrSessionID doit etre indique pour ce popup.");
	}else if(usrSessionID.length==0){
		alert("Erreur: il faut renseigner usrSessionID pour ce popup.");
	}
	*/
	var paramNameUsrSessionID="sid";


	url2=url;

	//Si le usrSessionID est renseign� et qu'il ne contient pas de b�tise
	if(usrSessionID!=null && usrSessionID.length>0 && usrSessionID.indexOf("?",0)<0  && usrSessionID.indexOf("&",0)<0){
		//usrSessionID ne doit pas �tre d�j� indiqu� dans l'url
		if(url.indexOf("&" + paramNameUsrSessionID + "=",0)<0 && url.indexOf("?" + paramNameUsrSessionID + "=",0)<0){
			//On ajoute ? ou &
			if(url.indexOf("?",0)<0){
				url2 = url2 + "?";
			}else{
				url2 = url2 + "&";
			}
			//On ajoute le param�tre usrSessionID � l'url
			url2 = url2 + paramNameUsrSessionID + "=" + usrSessionID;
		}
	}

	//alert("url modif=" + url2);

	return url2;
}


function verif(message)
{
	var query = location.href.split("?");
	var querystring = query[1];

	if (querystring.indexOf("loadev=1")!=-1)
	{

		var devis = '&dev=' + top.bas.document.forms[0].dev.value + '&cli=' + top.bas.document.forms[0].cli.value + '&veh=' + top.bas.document.forms[0].veh.value;

		var s=top.bas.location.href;

		if (s.indexOf("DevisFinal")==-1)
			top.bas.document.location.href='Devis?eta=1' + devis + '&' + querystring;
		else
			top.bas.document.location.href='DevisFinal?eta=1' + devis + '&' + querystring;
	}
}

function resizeDevisBas()
{
	var presenceDevis=top.bas;
	if(presenceDevis!=null){
		var s=top.bas.location.href;
		//alert(s);
		if (s.indexOf("DevisFinal")==-1) {
			window.parent.document.body.rows="*,80"; //devis
		}
		else {
			window.parent.document.body.rows = top.haut.document.getElementById('ligneDeDeuxPixels').offsetTop+',*';
		}
	}
}

function build_url_piece_valide(debut)
{
	var tables = document.getElementsByTagName ("input");
	var len = tables.length;
	var url_final = '';

	for (var i = 0; i < len; i++)
	{
		var n = tables[i].name;
		var sub_n = n.substring(0,4);

		//if (sub_n=='txtP' && parseInt(document.getElementById(tables[i].name).value)>0) {
		if ((sub_n=='txtP' && parseInt(document.getElementById(tables[i].name).value)>0) || (sub_n!='txtP' && sub_n!='posp' && sub_n!='load')) {

			if(sub_n=='cbxT'){
				if (document.getElementById(tables[i].name).checked)
					url_final+="&"+document.getElementById(tables[i].name).name+"="+document.getElementById(tables[i].name).value;
			}
			else
				url_final+="&"+document.getElementById(tables[i].name).name+"="+document.getElementById(tables[i].name).value;
		}
	}
//alert(url_final);
	return url_final;
}


function valide_devis_pour_reparation(dest,query)
{
	top.haut.document.frmRepDon.action=dest+'?'+query+'&loadev=1';
	top.haut.document.forms[0].loadev.value='1';
	//alert('valide_devis_pour_reparation : '+top.haut.document.frmRepDon.action);
	document.frmRepDon.submit();
	//alert('valide_devis_pour_reparation : '+top.haut.document.frmRepDon.action);

}

function bonCommande(msgErreur){
	var xmlHttp = getXmlHttpObject();

	var url = 'BonCommande?';
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	if(xmlHttp.readyState==4){
		if(xmlHttp.responseText.length>0){
			closeObject();
			Modalbox.show(xmlHttp.responseText,{width:500, height: 300, slideDownDuration:0,slideUpDuration:0,overlayClose:true,autoFocusing:false,title:''});
			document.getElementById('MB_window').className='bonCommande';
			return;
		}
	}
	alert(msgErreur);
}

function popupLayer(url, width, height, usrSessionID, usrApplicationID, title)
{
	if (title == null){
		title = "&nbsp;"
	}
	var url2 = url;
	if (usrSessionID != null){
		url2 = ajouteUsrSessionIDalURL(url, usrSessionID);
	}
	if (usrApplicationID != null) {
		url2 = ajouteUsrApplicationIDalURL(url2, usrApplicationID);
	}

	var h,w, h2;
      h2 = height;
      h = screen.height;
      w = screen.width;
      var l,t;
      if(h2>h)
      	h2 = (2*h)/3;

      l = parseInt((w-width)/2);
      t = parseInt((h-h2)/2);

   //   url2 += '&rdmz='+ Math.random();

      showIoPopupCentree(url2,title, width, h2);
      /*var a = window.open(url2,null,'height='+h2+',width='+width+',status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes,left='+l+',top='+t);

      if (a!=null)
       a.focus();*/

}

function popupLayerDefaultSize(url, usrSessionID, usrApplicationID, title){
	popupLayer(url, 500, 400, usrSessionID, usrApplicationID, title);
}


function fft2(url, usrSessionID, usrApplicationID){
	//var tablesInput = document.getElementsByTagName ("input");

	var tablesInput = document.getElementsByTagName("input");

	var lenInput = tablesInput.length;
	for (var i = 0; i < lenInput; i++) {
		var n = tablesInput[i].name;
		var sub_n = n.substring(0,5);
		var sub_n_1 = n.substring(0,6);
		var sub_n_2 = n.substring(0,6);
		if (sub_n=='txtP_' || sub_n_1=='txtPC_'){
			if (url.length < 3500){
				url = url + '&ref='+n;
			}
		}
		if (sub_n=='cbxT_'){
			url = url + '&tps='+n;
		}
		if(sub_n=='txtI_'){
			url = url + '&refloc='+n;
		}
	}

	popupFormulaire(url, usrSessionID, usrApplicationID);
}

//Popup pour le formulaire de FFT
function popupFormulaire(url, usrSessionID, usrApplicationID) {
	popup2(url, 900, 650, usrSessionID, usrApplicationID);
}

function _popup(url, name, h, w, l, t, title){
	if (!name)
		name = null;
	if (!title)
		title = null;

	// Options
	var options = 'height='+h+',width='+w+',status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes,left='+l+',top='+t;

	var wnd;
	if (title == null)
		wnd = window.open(url, name, options);
	else{
		wnd = window.open('', name, options);
		wnd.document.write('<html><head><title>'+title+'</title></head>\n');
		wnd.document.write('<body style="margin:0;paddding:0;overflow:hidden;"><iframe src="'+url+'" id="ifrm" name="ifrm" width="100%" height="100%"></iframe>\n');
		wnd.document.write('</body></html>\n');
		wnd.document.close();
	}

	if (wnd!=null)
		wnd.focus();
}

function popup(url, usrSessionID, usrApplicationID, title){
	popup2(url, 500, 400, usrSessionID, usrApplicationID, title);
}

function popup2(url, width, height, usrSessionID, usrApplicationID, title)
{
	var url2 = ajouteUsrSessionIDalURL(url, usrSessionID);
	url2 = ajouteUsrApplicationIDalURL(url2, usrApplicationID);

    var h = screen.height;
    var w = screen.width;
    var h2 = height;
    if (h2>h)
    	h2 = (2*h)/3;

    var l = parseInt((w-width)/2);
    var t = parseInt((h-h2)/2);

    _popup(url2, null, h2, width, l, t, title);
}

function popup3(url, usrSessionID, usrApplicationID, name, title)
{
	var url2 = ajouteUsrSessionIDalURL(url, usrSessionID);
	url2 = ajouteUsrApplicationIDalURL(url2, usrApplicationID);

	var h = screen.height;
	var w = screen.width;

	_popup(url2, name, h, w, 0, 0, title);
}

function popupTitle(url, usrSessionID, usrApplicationID, title)
{
	popupTitle2(url, usrSessionID, usrApplicationID, title);
}

function popupHaut(url, usrSessionID, usrApplicationID) {
	var url2 = ajouteUsrSessionIDalURL(url, usrSessionID);
	url2 = ajouteUsrApplicationIDalURL(url2, usrApplicationID);

	//alert("url2: "+ url2);

	var h = screen.height;
	var w = screen.width;
	var l = parseInt((w-400)/2);

	_popup(url2, "popupWindow", 400, 500, l, 10);
}

/* Ouvre une popup � l'url indiqu�e' */
function ouvrePopup(url){

	var h = screen.height;
	var w = screen.width;
	var l = parseInt((w-400)/2);

	_popup(url, "popupWindow", 400, 500, l, 10);
}

function getElementsDevisByClassName(className, tag, elm){
	var testClass = new RegExp("(^|s)" + className + "(s|$)");
	var tag = tag || "*";
	var elm = elm || top.bas.document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i ];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function closeObject()
{
	var presenceDevis=top.bas;
	if(presenceDevis!=null){
		var s=top.bas.location.href;

		if (s.indexOf("DevisFinal")==-1)
		{

			var lis = getElementsDevisByClassName('boiteRigideCentreeDevis','div');
			for (var i = 0 ; i < lis.length ; i++) {
				lis[i].style.display='none';
			}

			var lis = getElementsDevisByClassName('boiteRigideCentreePanier','div');
			for (var i = 0 ; i < lis.length ; i++) {
				lis[i].style.display='none';
			}

			var lis = getElementsDevisByClassName('partieAMasquerAvecLeDevis', 'li');
			for (var i = 0 ; i < lis.length ; i++) {

				lis[i].style.display='none';
			}

			var lis = getElementsDevisByClassName('partieAMasquer');
			for (var i = 0 ; i < lis.length ; i++) {
				lis[i].style.display='none';
			}
		}
	}
}
function openObject()
{
	top.bas.document.getElementById('cor').style.visibility = 'visible';
	top.bas.document.getElementById('cor').style.display='block';

	top.bas.document.getElementById('fin').style.visibility = 'visible';
	top.bas.document.getElementById('fin').style.display='block';

	top.bas.document.getElementById('sansBas').style.visibility = 'visible';
	top.bas.document.getElementById('sansBas').style.display='block';

	top.bas.document.getElementById('sansHaut').style.visibility = 'visible';
	top.bas.document.getElementById('sansHaut').style.display='block';

	top.bas.document.getElementById('avecHaut').style.visibility = 'hidden';
	top.bas.document.getElementById('avecHaut').style.display='none';

	top.bas.document.getElementById('avecHautComplet').style.visibility = 'hidden';
	top.bas.document.getElementById('avecHautComplet').style.display='none';

	top.bas.document.getElementById('pie1').style.visibility = 'visible';
	top.bas.document.getElementById('pie1').style.display='block';

	top.bas.document.getElementById('pie2').style.visibility = 'hidden';
	top.bas.document.getElementById('pie2').style.display='none';

	top.bas.document.getElementById('hau').style.visibility = 'hidden';
	top.bas.document.getElementById('hau').style.display='none';

	top.bas.document.getElementById('bas').style.visibility = 'hidden';
	top.bas.document.getElementById('bas').style.display='none';

}

function openAllObject()
{
	top.bas.document.getElementById('cor').style.visibility = 'visible';
	top.bas.document.getElementById('cor').style.display='block';

	top.bas.document.getElementById('fin').style.visibility = 'visible';
	top.bas.document.getElementById('fin').style.display='block';

	top.bas.document.getElementById('sansBas').style.visibility = 'visible';
	top.bas.document.getElementById('sansBas').style.display='block';

	top.bas.document.getElementById('sansHaut').style.visibility = 'hidden';
	top.bas.document.getElementById('sansHaut').style.display='none';

	top.bas.document.getElementById('avecHaut').style.visibility = 'hidden';
	top.bas.document.getElementById('avecHaut').style.display='none';

	top.bas.document.getElementById('avecHautComplet').style.visibility = 'visible';
	top.bas.document.getElementById('avecHautComplet').style.display='block';

	top.bas.document.getElementById('pie1').style.visibility = 'visible';
	top.bas.document.getElementById('pie1').style.display='block';

	top.bas.document.getElementById('pie2').style.visibility = 'hidden';
	top.bas.document.getElementById('pie2').style.display='none';

	top.bas.document.getElementById('hau').style.visibility = 'visible';
	top.bas.document.getElementById('hau').style.display='block';

	top.bas.document.getElementById('bas').style.visibility = 'visible';
	top.bas.document.getElementById('bas').style.display='block';
}

function format(valeur,decimal,separateur) {

	var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ;
	var val=Math.floor(Math.abs(valeur));
	if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;}
	var val_format=val+"";
	var nb=val_format.length;
	for (var i=1;i<4;i++) {
		if (val>=Math.pow(10,(3*i))) {
			val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i));
		}
	}
	if (decimal>0) {
		var decim="";
		for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";}
		deci=decim+deci.toString();
		val_format=val_format+"."+deci;
	}
	if (parseFloat(valeur)<0) {val_format="-"+val_format;}
	return val_format;
}

function suppEspace(chaine)
{
	if (chaine =="")
		chaine="0";
	var ch = chaine;
	if (ch.indexOf(" ")!=-1) {
		do
		{
			ch = ch.substring(0,ch.indexOf(" ")) + ch.substring(ch.indexOf(" ")+1,ch.length);
		}
		while (ch.indexOf(" ")!=-1);
	}
	return ch;
}
function suppVirgule(chaine)
{
	if (chaine =="")
		chaine="0";
	var ch = chaine;
	if (ch.indexOf(",")!=-1) {
		do
		{
			ch = ch.substring(0,ch.indexOf(",")) + "." + ch.substring(ch.indexOf(",")+1,ch.length);
		}
		while (ch.indexOf(",")!=-1);
	}
	return ch;
}


function sablier(){
	document.body.className = 'sablier';
}

function sablier_off(){
	document.body.className = '';
}



function reportError(msg,url,line) {
	var str ="Une erreur Javascript s'est produite:"
	+ "\nErreur: " + msg
	+ "ligne: " + line
	+ "\nURL: " + url
	+ "\n\nNavigateur: " + navigator.appName
	+ "\nVersion:  " + navigator.appVersion;
	alert( str);
	return true;
}

function ouvrirChassis(cha){
	document.getElementById('d'+cha).style.display='inline';
	document.getElementById('o'+cha).style.display='none';
	document.getElementById('f'+cha).style.display='inline';
}


function fermerChassis(cha){
	document.getElementById('d'+cha).style.display='none';
	document.getElementById('o'+cha).style.display='inline';
	document.getElementById('f'+cha).style.display='none';
}
//window.onerror = reportError;

function ouvreGlossaire(idGlossaire) {
	var glossaire = document.getElementById(idGlossaire);
	if (glossaire.style.display=='none')
		glossaire.style.display = 'block';
	else
		glossaire.style.display = 'none';
}
function popSuppressionDevis(element,url, width, height, message, usrSessionID, usrApplicationID){
	var valeur;
	var periodicite;
	var reg=new RegExp("^[0-9]*$","g");
	var reg2 = new RegExp("^0*$","g");
	var lien = url;
	if(element == 'vide'){
		valeur = document.frmConf.valueSuppressionDevisVideSansFacture.value;
		periodicite = document.frmConf.precisionSuppressionDevisVideSansFacture.value;
		if(!reg.test(document.frmConf.valueSuppressionDevisVideSansFacture.value)||reg2.test(document.frmConf.valueSuppressionDevisVideSansFacture.value)){
		 	alert(message)
			document.frmConf.valueSuppressionDevisVideSansFacture.value='';
			document.frmConf.valueSuppressionDevisVideSansFacture.focus();
		  	return;
		}
	}else
	{
		valeur = document.frmConf.valueSuppressionDevisNonVideSansFacture.value;
		periodicite = document.frmConf.precisionSuppressionDevisNonVideSansFacture.value;
		if(!reg.test(document.frmConf.valueSuppressionDevisNonVideSansFacture.value)||reg2.test(document.frmConf.valueSuppressionDevisNonVideSansFacture.value)){
			alert(message)
			document.frmConf.valueSuppressionDevisNonVideSansFacture.value='';
			document.frmConf.valueSuppressionDevisNonVideSansFacture.focus();
			return;
		}
	}
	lien = lien + "&valeur=" + valeur +"&precision=" + periodicite;
	popup2(lien, width, height, usrSessionID, usrApplicationID);
}

function sansPopSuppressionDevis(element,url, width, height,message){
	var valeur;
	var periodicite;
	var reg=new RegExp("^[0-9]*$","g");
	var reg2 = new RegExp("^0*$","g");
	var lien = url;
	if(element == 'vide'){
		valeur = document.frmConf.valueSuppressionDevisVideSansFacture.value;
		periodicite = document.frmConf.precisionSuppressionDevisVideSansFacture.value;
		if(!reg.test(document.frmConf.valueSuppressionDevisVideSansFacture.value)||reg2.test(document.frmConf.valueSuppressionDevisVideSansFacture.value)){
		 	alert(message)
			document.frmConf.valueSuppressionDevisVideSansFacture.value='';
			document.frmConf.valueSuppressionDevisVideSansFacture.focus();
		  	return;
		}
	}else
	{
		valeur = document.frmConf.valueSuppressionDevisNonVideSansFacture.value;
		periodicite = document.frmConf.precisionSuppressionDevisNonVideSansFacture.value;
		if(!reg.test(document.frmConf.valueSuppressionDevisNonVideSansFacture.value)||reg2.test(document.frmConf.valueSuppressionDevisNonVideSansFacture.value)){
			alert(message)
			document.frmConf.valueSuppressionDevisNonVideSansFacture.value='';
			document.frmConf.valueSuppressionDevisNonVideSansFacture.focus();
			return;
		}
	}
	lien = lien + "&valeur=" + valeur +"&precision=" + periodicite;
	document.location.href=lien;
}

function checkValeurSuppressionAutoDevis(message){
	var	reg=new RegExp("^[0-9]*$","g");
	var reg2 = new RegExp("^0*$","g");

	if(!reg.test(document.frmConf.valueSuppressionDevisVideSansFacture.value)||reg2.test(document.frmConf.valueSuppressionDevisVideSansFacture.value)){
    		alert(message);
    		document.frmConf.valueSuppressionDevisVideSansFacture.value='';
   			document.frmConf.valueSuppressionDevisVideSansFacture.focus();
   			 return false;
    	}
    reg=new RegExp("^[0-9]*$","g");
    reg2 = new RegExp("^0*$","g");

	if(!reg.test(document.frmConf.valueSuppressionDevisNonVideSansFacture.value)||reg2.test(document.frmConf.valueSuppressionDevisNonVideSansFacture.value)){
		alert(message);
		document.frmConf.valueSuppressionDevisNonVideSansFacture.value='';
		document.frmConf.valueSuppressionDevisNonVideSansFacture.focus();
		return false;
	}
	return true;
}

//Cette fonction permet de verifier si le parametre 'valeur' est un nombre
// entier positif
function isInteger(valeur) {
	if (valeur==null||valeur=='')
		return true;
	try{
		valeur = suppEspace(valeur);
		valeur = suppVirgule(valeur);
		var reg=new RegExp("^[0-9]*$");
		return (reg.test(valeur));
	}
	catch(e){
		return false;
	}
}

//Cette fonction permet de verifier si le parametre 'valeur' est un double
function isDouble(valeur) {
	if (valeur==null||valeur=='')
		return true;
    var reg=new RegExp("^[0-9]*(\\.|,)?[0-9]+$","g");
    try{
		valeur = suppEspace(valeur);
		return reg.test(valeur);
	}
	catch(e){
		return false;
	}
}

//Cette fonction permet de verifier si le parametre 'valeur' est un entier
function isEntier(valeur) {
	if (valeur==null||valeur=='')
		return true;
    var reg=new RegExp("^[0-9]*$","g");
    try{
		valeur = suppEspace(valeur);
		return reg.test(valeur);
	}
	catch(e){
		return false;
	}
}

//Cette focntion permet de verifier si le parametre est bin un email
function isEmail(valeur) {
	if (valeur==null||valeur=='')
		return true;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(valeur);
}

function isADate(valeur) {
	if (valeur==null||valeur=='')
		return true;
    var reg=new RegExp("(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/]{1}(19|20)[0-9]{2}$", "g");
    try{
		valeur = suppEspace(valeur);
		return reg.test(valeur);
	} catch(e){
		return false;
	}
}

// Module information
function openWindow(url){
	var a = window.open(url);
	if (a != null)
		a.focus();
}

function recopie(str, value){
	var listInput = document.getElementsByTagName('input');
	for (var i = 0 ; i<listInput.length ; i++){
		var input = listInput[i];
		if (input.name.indexOf(str) != -1){
			input.value=value;
		}
	}
}

function changeBackground(id, color, urlImg){
	//alert('test');
	_div = document.getElementById(id);
	_div.style.backgroundColor = "#"+color;
	document.getElementById("img" + id).src = urlImg;
}

//Utilise pour le lien hover sur une ligne de tableau
function linkHover(lien) {
	if (lien == null)
		return;
	lien.setAttribute("class","hover"); // Firefox
	lien.setAttribute("className","hover"); // Internet Explorer
}

//Utilise pour le lien out sur une ligne de tableau
function linkOut(lien) {
	if (lien == null)
		return;
	lien.setAttribute("class","out"); // Firefox
	lien.setAttribute("className","out"); // Internet Explorer
}
//Utilise pour le lien out sur une ligne de tableau
function linkOut(lien, i) {
	if (lien == null)
		return;
	lien.setAttribute("class","out" + i); // Firefox
	lien.setAttribute("className","out" + i); // Internet Explorer
}

function selectionPremierForfait(){
	var inputs = document.getElementsByTagName("input");
	for (var i=0;i<inputs.length;i++){
		var idI = "" + inputs[i].id;
		if(idI!=""&&idI.length>17&&idI.substring(0,17)=="forfaitsNationaux"){
			inputs[i].checked = true;
			break;
		}
	}
}


/*
** fonctions pour l'aide contextuelle des Trucks
*/

function fermerAideEtValideMsg(idMsg){
	document.getElementById('close_help').href = "javascript:masquerAide()";
	masquerAide();
	valideMessage(idMsg);
}

function afficherAide(){
	document.getElementById('msg_help').style.display = 'block';
	document.getElementById('bouton_aide').style.display = 'none';
	activeZoneMessage();
}

function masquerAide(){
	document.getElementById('msg_help').style.display = 'none';
	document.getElementById('bouton_aide').style.display = 'inline';
	desactiveZoneMessage();
}

function activeZoneMessage(){
	if (document.getElementById("message") != null){
		document.getElementById("message").style.display = 'block';
	}
}
function desactiveZoneMessage(){
	if (document.getElementById("message") != null){
		document.getElementById("message").style.display = 'none';
	}
}


function valideMessage(idMsg){
	var xmlHttp = getXmlHttpObject();

	var url = 'ValideMessage?idMsg=' + idMsg + "&type=HELP";
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
			if (xmlHttp.status != 200)
				alert("erreur d'envoie de la validation au serveur : code = " + xmlHttp.status + " reponse = " + xmlHttp.responseText);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getXmlHttpObject() {
	var objXMLHttp = null;

	if (typeof XMLHttpRequest != 'undefined') {
		objXMLHttp = new XMLHttpRequest();
	} else {
		try {
			objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //later IE
		} catch (e) {
			try {
				objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //earlier IE
			} catch (e) {
				objXMLHttp = null;
			}
		}
	}
	return objXMLHttp;
}

function ajaxCallSynchrone(url) {
	var request = getXmlHttpObject();
	if (!request)
		return null;

	url = addDateToUrl(url);

	request.open("GET",url,false);
	request.send(null);
	if(request.readyState==4){
		return request;
	}
	return null;
}

function addParamToUrl(url, param) {

	if (url.indexOf('?') >=0){
		return url + '&' + param;
	}
	return url + '?' + param;
}

function addDateToUrl(url) {
	var dateParam = "date="+new Date().getTime();
	url = addParamToUrl(url, dateParam);
	return url;
}

function load(message){
	showMessageAttente(message,message.length*8+104,50,false);
}

function validFormRechercheVin(pfield,pMessage){
	if (pfield == null)
		return true;

	if(pfield.value != ""){
		var reg2 = new RegExp("^\"[0-9]+\"$","g");
			if (pfield.value.replace(reg2,"") == "")
				return true;
	}
	return validFormatRechVin(pfield.value,pMessage);
}

function validFormatRechVin(pValue,pMessage){
	if( pValue=="" || pValue.length < 3 ){
			alert(pMessage);
		return false;
	} else {
		return true;
	}
}

/* Checks to see if string is empty */
function strtrim(value) {
  return value.replace(/^\s+/,'').replace(/\s+$/,'');
}

function checkNullString(value){
  if (!value || strtrim(value) == ""){
    return true;
  }
  return false;
}


/* Debut Gestion de la fenetre modale des CGU Immatriculation */
function accepterCguImmatriculation() {
	document.getElementById('choixCguImmatriculation').value='accepter';
}

function refuserCguImmatriculation() {
	document.getElementById('choixCguImmatriculation').value='refuser';
}

/* On garde des fonctions qui ont le meme nombre de parametres que la methode submit a appeler lors de la validation des CGU Immatriculation */
function afficheCguImmatriculationFromAccueil(message) {
	var options = getOptionsFromAccueil(message);
	afficheCguImmatriculation(options);
}

function afficheCguImmatriculationFromIdent(pFormName, pAction) {
	var options = getOptionsFromIdent(pFormName, pAction);
	afficheCguImmatriculation(options);
}

function afficheCguImmatriculationFromAccueilTruck(pform, pfield) {
	var options = getOptionsFromAccueilTruck(pform, pfield);
	afficheCguImmatriculation(options);
}

/* Comme le nombre de parametres est variable, on s'en sort avec 1 variable tableau "options" contenant un "from"  */
function getOptionsFromAccueil(inputMessage) {
	return {from: "accueil", message: inputMessage};
}

function getOptionsFromIdent(inputFormName, inputAction) {
	return {from: "ident", pFormName: inputFormName, pAction: inputAction};
}

function getOptionsFromAccueilTruck(inputForm, inputField) {
	return {from: "accueilTruck", pform: inputForm, pfield: inputField};
}

function afficheCguImmatriculation(options) {
	var url = 'CguImmatriculation?page=afficher';
	Modalbox.show(url,{title:'cguImmatriculation', width:630, height:430, verticalOffset:10, slideDownDuration:0.5, slideUpDuration:0.5, overlayClose:false, afterLoad: function() { afterLoadCguImmatriculation(options); }, afterHide: function() { afterHideCguImmatriculation(options); } });
	document.getElementById('MB_window').className='cguImmatriculation';

	return false;
}

function masqueCguImmatriculation() {
	Modalbox.hide({title:'cguImmatriculation'});

	return false;
}

function afterLoadCguImmatriculation(options) {
	document.getElementById('choixCguImmatriculation').value='refuser';
}

function afterHideCguImmatriculation(options) {
	if (document.getElementById('choixCguImmatriculation').value == 'accepter') {
		updateCguAndSubmitFormImmat(options);
	} else {
		gestionFocus(options);
	}
}

function updateCguAndSubmitFormImmat(options) {
	var urlForUpdate = 'CguImmatriculation?page=accepter';
	ajaxCallSynchrone(urlForUpdate);

	if (options != null) {
		var from = options['from'];

		if (from == "accueil") {
			var message = options['message'];

			submitFormImmat(message);
		} else if (from == "ident") {
			var pFormName = options['pFormName'];
			var pAction = options['pAction'];

			submitActionSearchOnGoing(pFormName, pAction);
		} else if (from == "accueilTruck") {
			var pform = options['pform'];
			var pfield = options['pfield'];

			submitFormImmatTruck(pform, pfield);
		}
	}
}

function gestionFocus(options) {
	if (options != null) {
		var from = options['from'];

		if (from == "accueil") {
			document.frmIdent.txtImmat.focus();
		} else if (from == "ident") {
			var pFormName = options['pFormName'];
			document.forms[pFormName].txtImmat.focus();
		} else if (from == "accueilTruck") {
			var pfield = options['pfield'];
			pfield.focus();
		}
	}
}
/* Fin Gestion de la fenetre modale des CGU Immatriculation */

function hoverTd()
{
	var trs = document.getElementsByTagName('tr');
	for (var i = 0; i < trs.length; i++) {
		var tr = trs[i];
		if (tr.className == 'oddLine' || tr.className =='evenLine'){
			attachMouseEvent(tr, tr);
		}
	}

}
function isElementHovered(element){
	return (
		element != null && element.tagName != null &&
			(element.tagName.toLowerCase()=='span'
				|| element.tagName.toLowerCase()=='tr'
				|| element.tagName.toLowerCase()=='td'));
}

function attachMouseEvent(element, father){
	if ( isElementHovered(element) && element.onmouseover == null && element.onmouseout == null){
		element.onmouseover = function() {
			changeCss(father, true);
		};

		element.onmouseout = function() {
			changeCss(father, false);
		};

		var currentChildren = element.childNodes;
		if (currentChildren != null){
			for (var k = 0; k < currentChildren.length; k++) {
				attachMouseEvent(currentChildren[k], father);
			}
		}
	}
}

function changeCss(father, isHover){
	if (isElementHovered(father)){
		if (isHover){
			father.className = 'hover '+father.className;
		}
		else{
			if (father.className.indexOf('hover ')== 0){
				father.className = father.className.substring(6);
			}
		}
	}
	var children = father.childNodes;
	if (children != null){
		for (var j = 0; j < children.length; j++) {
			changeCss(children[j], isHover);
		}
	}
}

/* Fin Gestion de la fenetre modale des CGU Immatriculation */

function getIdElementChecked(){
	var listResult = new Array();
	var listInput = document.getElementsByTagName('input');
	for (var i = 0; i<listInput.length ; i++){
		if (listInput[i].checked == true){
			listResult.push(listInput[i].id);
		}
	}
	return listResult;
}

/* Debut Gestion de la fenetre modale des CGV statistiques */
function accepterCgvStatistiques(fromPage) {
	document.getElementById('choixCgvStatistiques').value='accepte';
	updateCgvAndSubmitFormStat(fromPage);
}

function refuserCgvStatistiques(fromPage) {
	document.getElementById('choixCgvStatistiques').value='refuse';
	updateCgvAndSubmitFormStat(fromPage);
}

function afficheCgvStatistiques() {
	var url = 'CgvStatistiques';

	Modalbox.show(url,{title:'cgvStatistiques', width:610, height:430, slideDownDuration:0.5, slideUpDuration:0.5, overlayClose:false});
	document.getElementById('MB_window').className='cgvStatistiques';

	return false;
}

function masqueCgvStatistiques() {
	if (Modalbox.initialized){
		Modalbox.hide({title:'cgvStatistiques'});
		return false;
	}
	return true;
}


function updateCgvAndSubmitFormStat(fromPage) {
	var urlForUpdate = 'CgvStatistiques?choixCgv='+ document.getElementById('choixCgvStatistiques').value;
	if (fromPage == 'configuration') {
		AppuyerBoutonEnregistrement();
	} else {
		ajaxCallSynchrone(urlForUpdate);
		submitFormStats();
	}
}

/*
 * renvoie le prix de vente ou 0 si on n'a pas l'information
 */
function getPrixVentePieces(nb_piece,prix_vente,prix_vente_piece){
	jQuery.noConflict();
	var spanPrixVenteHtml = jQuery('#'+prix_vente+' > span').html();
	var prixVente = 0;
	// tester si l'element a un prix de vente
	if (spanPrixVenteHtml != null) {
		var value = parseFloat(spanPrixVenteHtml.split(' ')[0]);
		var nb = parseInt(jQuery('#'+nb_piece).val());
		prixVente = value*nb;
	}
	return prixVente;
}
var viewTotal = true;
function calculTotal() {
	jQuery.noConflict();
	result = 0;
	var tab_prix_vente_piece = jQuery('input[name=prix_vente_piece]');
	if(tab_prix_vente_piece != null){
		for(i=0 ; i<tab_prix_vente_piece.size() ; i++){
			var index = tab_prix_vente_piece[i].id;
			result = result + getPrixVentePieces('txtP_'+index,'prixVente_'+index,'prix_vente_piece_'+index);
		}
	}
	var tab_prixHTT = jQuery('input[name=cbxT_{M opp.getCodeope}]');
	if(tab_prixHTT != null){
		for(j=0 ; j<tab_prixHTT.size() ; j++){
			if(tab_prixHTT[j].checked){
				 var prixHTT = jQuery('#prixHTT'+tab_prixHTT[j].value.substring(4,tab_prixHTT[j].value.length)).html().replace(/^\s+|\s+$/g, '');
				 result = result + parseFloat(prixHTT.substring(0,prixHTT.length-1));
			}
		}
	}
	if(viewTotal && (!((typeof isPackCarro === 'undefined') || (isPackCarro == '')))){

				jQuery('#sous_total').html('{M libelle.getString messageSousTotal}:&nbsp;&nbsp;'+result.toFixed(2)+' &euro; HT');
				jQuery('#sous_total').addClass('message sous_total');
	} else {

		viewTotal?viewTotal = !((typeof isPackCarro === 'undefined') || (isPackCarro == '')):viewTotal;
	}
}

function obtientFocusReference(pfield, exemple){
	pfield.className = 'champLorsDeSaisie_R';
	if (pfield.value == exemple) {
		pfield.value = '';
	}
}
function exempleReference(pfield, exemple){
	 pfield.className = 'champDeSaisieParDefaut_R';
	 if (pfield.value == '') {
		 pfield.value = exemple;
	}
}
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|s)" + className + "(s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i ];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

