moisX=["","Janvier","Fevrier","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Decembre"];
JourM=["Di","Lu","Ma","Me","Je","Ve","Sa"];


bgcolor='#FFFFFF';
bgcolor2='#F5EBED';

var fermable_microcal=true;
var select_old= null;
var startWeek=1;//debut de la semaine 0=dim,1=lun,...
var jourPause={0:true,6:true}; //jour de pause de la semaine
var jourFeriee={"1-1":"jour an","1-5":"fête du travail","8-5":"armistice","14-7":"fête nationale","15-8":"ascencion","1-11":"armistice","11-11":"toussain","25-12":"noel"};


//structure la date 
function strucDate(dateX) {
	return {"pos":dateX.getDay(),"jour":dateX.getDate(),"mois":dateX.getMonth()+1,"annee":dateX.getFullYear()};
}

var dateS= strucDate(new Date());//date Selectionné

//retourne le ième jour du 1er du mois

function premJourMois(mois,annee) {
	return (new Date(annee,mois-1,1).getDay());
}
//retourne le jour max du mois

function JmaxMois(mois,annee) {
	return (new Date(annee,mois,0).getDate());
}

/* Test une date si elle est correct...spécial killer*/
function testTypeDate(dateEntree) {
	tst=false;
	try {
		rc=dateEntree.split("/");nd=new Date(rc[2],(rc[1]-1),rc[0]);
		tst=(rc[2]>1800&&rc[2]<2200&&rc[2]==nd.getFullYear()&&rc[1]==(nd.getMonth()+1)&&rc[0]==nd.getDate());
	} catch(e) {}
	return tst;
}

//selection de la zone avec la souris
function choix(koi,code) {
	if (code) {
		select_old= koi.style.background;
		koi.style.background ='#c0c0FF';
	} else {
		koi.style.background =select_old;
	}
}

function testTravail(oldX,xx,jj,mm,aa) {
	var dnow= strucDate(new Date());//date actuelle
	styleX="font-family:Tahoma;font-size:10px;text-align:center;";
	styleX+=(oldX)?"":"color:#e0e0e0;";
	styleX+="cursor:hand;border-right:1px #e0e0e0 solid;border-bottom:1px #e0e0e0 solid;";
	if (jourPause[xx]||jourFeriee[jj+"-"+mm]!=null) styleX+="background:#f0f0f0;";
	if (jj==dnow.jour&&mm==dnow.mois&&aa==dnow.annee) styleX+="border:2px red solid;";
	return styleX;
}

//test si année bissextile
function bissextile(annee) {
	return (annee%4==0 && annee %100!=0 || annee%400==0);
}
//Retourne le nombre de jour depuis le 1er janvier (num de semaine)
function nbJAnnee(dateX){
	var nb_mois=[,0,31,59,90,120,151,181,212,243,273,304,334];
	j=dateX.jour ; m=dateX.mois ; a=dateX.annee;
	nb=nb_mois[m]+j-1;
	if (bissextile(a) && m>2) nb++;
	return nb;
}

//affiche le calendrier
function view_microcal(actif,ki,source,mxS,axS) {
	if (actif) {
//decalage du mois su on clique sur -/+
		if (mxS!=-1) {
			clearTimeout(cc);
			ki.focus();
			fermable_microcal=true;
			dateS.mois=mxS;
			dateS.annee=axS;
			if (dateS.mois<1) {dateS.annee--;dateS.mois+=12;}
			if (dateS.mois>12) {dateS.annee++;dateS.mois-=12;}
		}
//init
		Dstart=(premJourMois(dateS.mois,dateS.annee)+7-startWeek)%7;
		jmaxi=JmaxMois(dateS.mois,dateS.annee);
		jmaxiAvant=JmaxMois((dateS.mois-1),dateS.annee);
//si on veux ajouter le numero de la semaine ...
//idxWeek=parseInt(nbJAnnee(strucDate(new Date(dateS.mois+'-01-'+dateS.annee)))/7,10)+1;
		ymaxi=parseInt((jmaxi+Dstart+1)/7,10);
//generation du tableau
//--entête
		htm="<table><tr style='font-size:10px;font-family:Tahoma;text-align:center;'>";
		htm+="<td style='cursor:hand;' onclick=\"view_microcal(true,"+ki.id+","+source.id+","+(dateS.mois-1)+","+dateS.annee+");\">-</td>";
		htm+="<td colspan='5'> <b> "+moisX[dateS.mois]+"</b>&nbsp;"+dateS.annee+"</td>";
		htm+="<td  style='cursor:hand;' onclick=\"view_microcal(true,"+ki.id+","+source.id+","+(dateS.mois+1)+","+dateS.annee+")\">+</td></tr>";
//--corps
		htm+="<tr>";
//affichage des jours DLMMJVS
		for (x=0;x<7;x++) 
		htm+="<td  style='font-size:10px;font-family:Tahoma;'><b>"+JourM[(x+startWeek)%7]+"</b></td>";
		htm+="</tr>";
//------------------------
		for (y=0;y<=ymaxi;y++){
			htm+="<tr>";
			for (x=0;x<7;x++) {
				idxP=y*7+x-Dstart+1;   //numero du jour
				aa=dateS.annee;
				xx=(x+startWeek)%7;
//jour du mois précedent
				if (idxP<=0) {
					jj=idxP+jmaxiAvant;mm=dateS.mois-1;
					if (mm==0) {mm=12;aa--;}
					htm+="<td style='"+testTravail(false,xx,jj,mm,aa)+"' onmouseover='choix(this,true)' onmouseout='choix(this,false)'  onclick=\""+(ki.id)+".value='"+((jj<10)?"0":"")+jj+"/"+((mm<10)?"0":"")+mm+"/"+aa+"';"+(ki.id)+".style.color='black';\">"+jj+"</td>";
				} else if (idxP>jmaxi) { //jour du mois suivant			
					jj=idxP-jmaxi;mm=dateS.mois+1;
					if (mm==13) {mm=1;aa++;}
					htm+="<td style='"+testTravail(false,xx,jj,mm,aa)+"' onmouseover='choix(this,true)' onmouseout='choix(this,false)'  onclick=\""+(ki.id)+".value='"+((jj<10)?"0":"")+jj+"/"+((mm<10)?"0":"")+mm+"/"+aa+"';"+(ki.id)+".style.color='black';\">"+jj+"</td>";
				} else { //jour du mois en cours
					jj=idxP;mm=dateS.mois;
					htm+="<td style='"+testTravail(true,xx,jj,mm,aa)+"' onmouseover='choix(this,true)' onmouseout='choix(this,false)'  onclick=\""+(ki.id)+".value='"+((jj<10)?"0":"")+jj+"/"+((mm<10)?"0":"")+mm+"/"+aa+"';"+(ki.id)+".style.color='black';\">"+jj+"</td>";
				}
			}
			htm+="</tr>";
		}//-------------------------
		htm+="</table>";
//affiche le tableau
		source.innerHTML=htm;
		source.style.visibility="";
	} else {
//ferme le calendrier
		if (fermable_microcal) 
		cc=setTimeout(source.id+".style.visibility='hidden'",500);
	}
}
function AffDateHeure(a,b,c) {
	d=new Date();
	hh=d.getHours();
	min=d.getMinutes();
	sec=d.getSeconds();
	if (hh<10) hh='0'+hh;
	if (min<10) min='0'+min;
	jj=d.getDate();
	mm=d.getMonth()+1;
	aaaa=d.getFullYear();
	if (jj<10) jj='0'+jj;
	if (mm<10) mm='0'+mm;
	document.forms[a].elements[c].value=hh+":"+min;
	document.forms[a].elements[b].value=jj+"/"+mm+"/"+aaaa;
}

function AffHeureJavascript() {
	var d=new Date();
	hh=d.getHours();
	min=d.getMinutes();
	sec=d.getSeconds();
	if (hh<10) hh='0'+hh;
	if (min<10) min='0'+min;
	if (sec<10) sec='0'+sec;	
	document.write(hh+":"+min+":"+sec+'<BR>');
}	

function AffDateJavascript() {
	var d=new Date();
	jj=d.getDate();
	mm=d.getMonth()+1;
	aaaa=d.getFullYear();
	if (jj<10) jj='0'+jj;
	if (mm<10) mm='0'+mm;	
	document.write(jj+"/"+mm+"/"+aaaa);
}

function hideShow(el_id){
    var el=document.getElementById(el_id);   
    if(el_id.style.display!="none"){
    	el_id.style.display="none";
    }else{
        el_id.style.display="";
    }
}

function numericonly(evt) { 
	var theEvent = evt || window.event; 
	var key = theEvent.keyCode || theEvent.which; 
	key = String.fromCharCode( key ); 
	var regex = /[0-9]/; 
    if( !regex.test(key) ) { 
		theEvent.returnValue = false; 
		theEvent.preventDefault(); 
	} 
} 

 function Warning(obj) {
   if (obj.checked)  {
    alert('Important: Make sure the sales are deleted from CM400 before integrating them again !');
   }
}

function confirmation(obj) {
	var msg = "Are you sure you want to cancel this parameter ?"
	if (confirm(msg))
	{
		window.location.replace(obj.toString());
	}
}


function GetCashunits(obj) {
    
	var Storejs = new String ("test");
	Storejs = obj.value.toString();
	
	var CashunitsCount = new String ("cashunits");
	var oOption = document.createElement('OPTION');
	
	CashunitsCount = Storejs.substring(Storejs.indexOf(";")+1);
	document.getElementById('Cashunits').length=0;
	
	for (i=0; i<=parseInt(CashunitsCount); i++) {
	  var oOption = document.createElement('OPTION');
	  if (i==0) {
        oOption.value = i.toString();
	    oOption.text = ' ';}
	  else {
	    oOption.value = i.toString();
	    oOption.text = i.toString(); 
	  }
	  
	  try { //tente methode Microsoft       
	    document.getElementById('Cashunits').add(oOption, i);
	  } 
	  catch(e) { //si erreur fait methode pour navigateur DOM compliant 
	    document.getElementById('Cashunits').add(oOption, null);
	  }
	}
	if (parseInt(CashunitsCount)>1) {
	  var oOption = document.createElement('OPTION');
        oOption.value = 'All,'+CashunitsCount.toString();
	    oOption.text = 'All'; 
	  try { //tente methode Microsoft       
	    document.getElementById('Cashunits').add(oOption, i);
	  } 
	  catch(e) { //si erreur fait methode pour navigateur DOM compliant 
	    document.getElementById('Cashunits').add(oOption, null);
	  }
	}
	
}

function calcultemps() { /* calcul du temps total magasin */
	var J_lib=new Array("LU","MA","ME","JE","VE","SA","DI");
	var x;
	for (x in J_lib) {
		var nbMinute=0;
		var ts=document.forms["formulaire"].elements[J_lib[x]+"_TSH"].value;
		var tt=document.forms["formulaire"].elements[J_lib[x]+"_TT"].value;
		var nhour=tt.substring(0,tt.lastIndexOf(":"));
		var nminute=tt.substring(tt.lastIndexOf(":")+1,tt.length);
		nbMinute=Math.round(nhour*60)+Math.round(nminute);
		var nhour=ts.substring(0,ts.lastIndexOf(":"));
		var nminute=ts.substring(ts.lastIndexOf(":")+1,ts.length);
		nbMinute=nbMinute+(Math.round(nhour*60)+Math.round(nminute));
		if (nbMinute>=60) {
			hour=Math.floor(nbMinute/60);
			minute=nbMinute-(hour*60);
			if (hour < 10) { hour = "0" + hour; }
			if (minute < 10) { minute = "0" + minute; }
			tps=hour+":"+minute;
		} else {
			minute=nbMinute;
			if (minute < 10) { minute = "0" + minute; }
			tps="00:"+minute;
		}
		document.forms["formulaire"].elements[J_lib[x]+"_TS"].value=FormatHeure(tps,3);
	}  
	document.forms["formulaire"].elements["TT_TOTV"].value=FormatHeure(cumul('_TT'),2);
	document.forms["formulaire"].elements["TS_TOTV"].value=FormatHeure(cumul('_TS'),3); 
}
function FormatHeure(HM,lg) { /* formattage de l'affichage pour les heures (lg) */
	var hour=HM.substring(0,HM.lastIndexOf(":"));
	var minute=HM.substring(HM.lastIndexOf(":")+1,HM.length);
	if (HM.length!=5) {
		if (hour.length < 2) {hour='0'+hour;}
		if (minute.length < 2) {minute='0'+minute;}
	} 
	if (lg==3) {hour="0"+hour; }
	tps=hour.substring(hour.length-lg-1,hour.length)+":"+minute.substring(0,2);
/*document.write(HM+' / '+HM.length+' / '+lg+' / '+tps); */
	return tps 
}
function ControleHeure(HM) {
	var ok=0;
	var hour=HM.substring(0,HM.lastIndexOf(":"));
	var minute=HM.substring(HM.lastIndexOf(":")+1,HM.length);
	if ((hour>=0)&&(hour<24)&&(minute>=0)&&(minute<=59)) {ok=1;}
	return ok;
}

function cumul(suffixe) {
	var nbMinute=0;
	var J_lib=new Array("LU","MA","ME","JE","VE","SA","DI");
	var x;
	for (x in J_lib) {
		var tt=document.forms["formulaire"].elements[J_lib[x]+suffixe].value;
		var nhour=tt.substring(0,tt.lastIndexOf(":"));
		var nminute=tt.substring(tt.lastIndexOf(":")+1,tt.length);
		nbMinute=nbMinute+(Math.round(nhour*60)+Math.round(nminute));
	}
	if (nbMinute>=60) {
		hour=Math.floor(nbMinute/60);
		minute=nbMinute-(hour*60);
		if (hour < 10) { hour = "0" + hour; }
		if (minute < 10) { minute = "0" + minute; }
		tps=hour+":"+minute;
	} else {
		minute=nbMinute;
		if (minute < 10) { minute = "0" + minute; }
		tps="00:"+minute;
	}
	return tps;
}
function calcul(deb,fin,tpspause) {
	var d = Date.parse ("02/02/2010 "+deb+":00");
	var d1 = Date.parse("02/02/2010 "+fin+":00");
	var hour=tpspause.substring(0,tpspause.lastIndexOf(":"));
	var minute=tpspause.substring(tpspause.lastIndexOf(":")+1,tpspause.length);
	diff=(((d1-d)/1000)/60)-(Math.round(hour*60)+Math.round(minute));
	if (diff>=0) {
		if (diff>=60) {
		  hour=Math.floor(diff/60);
		  minute=diff-(hour*60);
		  if (hour < 10) { hour = "0" + hour; }
		  if (minute < 10) { minute = "0" + minute; }
		  tps=hour+":"+minute;
		} else {
		  minute=diff;
		  if (minute < 10) { minute = "0" + minute; }
		  tps="00:"+minute;
		}
	} else {
/*				alert("erreur dans la combinaison des horaires"); */ 
		tps="00:00";
	 }
	return tps;
}

function afficher(jour) {
	var erreur=0;
	var hd=jour+"_HD";
	var hf=jour+"_HF";
	var tp=jour+"_TP";
	var tt=jour+"_TT";
	var cod=jour+"_Cod";
	var heureDeb=FormatHeure(document.forms["formulaire"].elements[hd].value,2);
	var heureFin=FormatHeure(document.forms["formulaire"].elements[hf].value,2);
	var TpsPause=FormatHeure(document.forms["formulaire"].elements[tp].value,2);
	var codabs=document.forms["formulaire"].elements[cod].value;
	if ((heureDeb!="00:00")&&(heureFin!="00:00")) {
		if (ControleHeure(heureDeb)==0)  {
			erreur=1;
			window.alert('Heure Debut faux');
		}
		if (ControleHeure(heureFin)==0)  {
			erreur=1;
			alert('Heure Fin faux'); 
		}
		if (ControleHeure(TpsPause)==0)  {
			erreur=1;
			alert('Temps de Pause faux');
		}
		if (codabs!=0) {
			heureDeb='00:00';
			heureFin='00:00';
			TpsPause='00:00';
		}
	}
	if (erreur==0) {
		document.forms["formulaire"].elements[hd].value=FormatHeure(heureDeb,2);
		document.forms["formulaire"].elements[hf].value=FormatHeure(heureFin,2);
		document.forms["formulaire"].elements[tp].value=FormatHeure(TpsPause,2);
		document.forms["formulaire"].elements[tt].value=FormatHeure(calcul(heureDeb,heureFin,TpsPause),2);
		calcultemps();
		document.forms["formulaire"].elements["TT_TOTV"].value=FormatHeure(cumul('_TT'),2);
		document.forms["formulaire"].elements["TS_TOTV"].value=FormatHeure(cumul('_TS'),3);
	}
}

function pop(msg,pos) {
		skn.visibility = "hidden";
		a=true;
		skn.top = pos;
		var content ='<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 BGCOLOR=#000000 WIDTH=250><TR><TD><TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=1>';
		pass = 0;
		while (pass < msg.length) {
			content += "<TR><TD BGCOLOR='"+bgcolor+"' onMouseOver=\"this.style.background='"+bgcolor2+"'\" onMouseOut=\"this.style.background='"+bgcolor+"'\" >"+msg[pass]+"</TD></TR>";
//			content += "<TR><TD BGCOLOR='white' onMouseOver=\"this.style.background='blue'\" onMouseOut=\"this.style.background='green' \"HEIGHT=20\">"+msg[pass]"</TD></TR>";
			pass++;
		}
		content += '</TABLE></TD></TR></TABLE>';
		document.getElementById("topdeck").innerHTML = content;
		skn.visibility = "visible";
};
function kill() {
		if(document.getElementById);
		skn.visibility = "hidden";
}

function AfficheMenuJavascript(){
	document.write('<style type="text/css">');
	document.write('.popper { POSITION: absolute; VISIBILITY: hidden; z-index:15; left:120px ');
	document.write('#topgauche { position:absolute;  z-index:10; }');
	document.write('A:hover.ejsmenu {color:#CA4E7D;}');
	document.write('.ejsmenu {color:#CA4E7D; text-transform:uppercase;}');
	document.write('</style>');
	document.write('<div style="position:relative;height:25"><DIV class=popper id=topdeck></DIV>');
	if(document.getElementById) {
			skn = document.getElementById("topdeck").style;
			skn.left = 200;
	}
	
	document.onclick = kill;
	if(document.getElementById) {
		document.write('<DIV ID=topgauche><TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 BGCOLOR=#000000 WIDTH=200 HEIGHT=40><TR><TD><TABLE CELLPADING=0 CELLSPACING=0 BORDER=0 WIDTH=100% HEIGHT=40>');
		for (var i=0; i < ztext.length; i++) { 
			document.write('<tr><TD BGCOLOR='+bgcolor+' onMouseOver="this.style.background=\''+bgcolor2+'\';pop(zlien['+i+'],'+i*20+')"				onMouseOut="this.style.background=\''+bgcolor+'\' " CLASS=ejsmenu>'+ztext[i][0]+'</TD></tr>');
		}	
		document.write('</TABLE></TD></TR></TABLE></DIV>');
		document.write('</div>');
	}
}
