function OpenMecaPlanningPopup(popupn, wsrc) {
 var wdw;
 var wh = 330;
 var ww = 450;
 var wt = Math.round((screen.availWidth - ww) / 2);
 var wl = Math.round((screen.availHeight - wh) / 2);
 wdw = window.open(wsrc, popupn, "scrollbars=no,width="+ww+",height="+wh+",left="+wl+",top="+wt+",screenx="+wl+",screeny="+wt+"");
 wdw.document.close();
 wdw.focus();
}

function ServiceUnavailable() {
 var css = window.document.getElementById('css').value;
 if (window.document.getElementById('ident')) 
  {var ident = window.document.getElementById('ident').value;}
 else
  {var ident = 0;};
 if (window.document.getElementById('idsit')) 
  {var idsit = window.document.getElementById('idsit').value;}
 else
  {var idsit = 0;};
 OpenMecaPlanningPopup("MecaPlanning_Popup", "ServiceUnavailable.php?ident=" + ident + "&idsit=" + idsit + "&css=" + css );
}

function HelpContext(HelpNo) {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 OpenMecaPlanningPopup("MecaPlanning_Popup", "HelpContext.php?ident=" + ident + "&idsit=" + idsit + "&helpno=" + HelpNo + "&css=" + css );
}

function LegalMentions() {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 OpenMecaPlanningPopup("MecaPlanning_Popup", "LegalMentions.php?ident=" + ident + "&css=" + css );
}

function GetMecaPlanningParams() {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 var MPEmail = window.document.getElementById('MPEmail').value;
 OpenMecaPlanningPopup("MecaPlanning_Popup", "MecaPlanningParams.php?ident=" + ident + "&idsit=" + idsit + "&MPEmail=" + MPEmail + "&css=" + css );
}

function GetThisMecaPlanningParams(ident, idsit, css) {
 window.location.href = "MecaPlanningParams.php?ident=" + ident + "&idsit=" + idsit + "&css=" + css;
}

function SetMecaPlanningParams() {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 OpenMecaPlanningPopup("MecaPlanning_Popup", "MecaPlanningFirstTime.php?ident=" + ident + "&idsit=" + idsit + "&css=" + css );
}

function SetThisMecaPlanningParams(action, ident, idsit, css) {
 if (window.document.getElementById('clinom')) {var clinom = window.document.getElementById('clinom').value;} else {var clinom = '';};
 if (window.document.getElementById('clinum')) {var clinum = window.document.getElementById('clinum').value;} else {var clinum = '';};
 if (window.document.getElementById('immat'))  {var immat  = window.document.getElementById('immat').value;}  else {var immat  = '';};
 if ((((trim(clinom) == '') && (trim(clinum) == '')) || 
      ((trim(clinom) == '') && (immat.length < 4)) || 
      ((trim(clinum) == '') && (immat.length < 4))) && (action > 0))
  {alert("Vous devez renseigner au moins 2 des 3 informations suivantes :\n\n- Votre nom tel qu'il figure sur votre facture\n- Votre numéro de client\n- Le numéro d'immatriculation de votre véhicule");}
 else
  {window.location.href = "MecaPlanningFirstTime.php?clinom=" + clinom + "&clinum=" + clinum + "&immat=" + immat + "&action=" + action + "&ident=" + ident + "&idsit=" + idsit + "&css=" + css;};
}

function PostThisMecaPlanningParams(clinom, clinum, immat) {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 var email = FormatEmail(window.document.getElementById('MPEmail').value);
 var email2 = FormatEmail(window.document.getElementById('MPEmail2').value);
 var gsm = FormatTel(window.document.getElementById('clitel').value);
 var gsm2 = FormatTel(window.document.getElementById('clitel2').value);

 if (!EmailIsValid(email))
  {alert("Votre E-mail n'est pas valide");}
 else if (!GSMIsValid(gsm))
  {alert("Votre numéro de portable n'est pas valide");}
 else  if (email != email2)
  {alert("Les e-mails ne sont pas identiques");}
 else if (gsm != gsm2)
  {alert("Les téléphones portables ne sont pas identiques");}
 else
  {window.location.href = "MecaPlanningFirstTime.php?clinom=" + clinom + "&clinum=" + clinum + "&immat=" + immat + "&MPEmail=" + email + "&clitel=" + gsm + "&action=2&ident=" + ident + "&idsit=" + idsit + "&css=" + css;};
}

function SendThisMecaPlanningParams(ident, idsit, css) {
 var MPEmail = window.document.getElementById('MPEmail').value;
 if (!EmailIsValid(MPEmail))
  {alert("Votre e-mail n'est pas valide");}
 else
  {window.location.href = "MecaPlanningParams.php?ident=" + ident + "&idsit=" + idsit + "&MPEmail=" + MPEmail + "&action=" + 1 + "&css=" + css;};
}

function ConnectMecaPlanningKeyPress(e) {
 key = e.which ? e.which : e.keyCode;
 if (key == 13) {ConnectMecaPlanning()};
}

function ConnectMecaPlanning() {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 var MPEmail = window.document.getElementById('MPEmail').value;
 var MPPwd = window.document.getElementById('MPPwd').value;
 if (!EmailIsValid(MPEmail))
  {alert("Votre E-mail n'est pas valide");}
 else if (MPPwd == "")
  {alert("Votre mot de passe n'est pas valide");}
 else 
  {window.location.href = "Actions.php?ident=" + ident + "&idsit=" + idsit + "&MPEmail=" + MPEmail + "&MPPwd=" + MPPwd + "&action=" + 1 + "&css=" + css;};
}

function InitRdvParams(cbxname) {
 if (cbxname == 'idmar')
  {window.document.getElementById('idmod').value = 0;}
 window.document.getElementById('idint').value = 0;
}

function InitVehicleParams(wdw) {
 VecValues = wdw.document.getElementById('idvec').value.split(';');
 wdw.document.getElementById('idveh').value = VecValues[0];
 wdw.document.getElementById('idmar').value = VecValues[1];
 wdw.document.getElementById('idmod').value = VecValues[2];
 wdw.document.getElementById('idint').value = 0;
 RdvParamsChange(0, wdw);
}

function DisableVehicleList(wdw) {
 wdw.document.getElementById('svec').value = 0;
 wdw.document.getElementById('idveh').value = 0;
 wdw.document.getElementById('idmar').value = 0;
 wdw.document.getElementById('idmod').value = 0;
 wdw.document.getElementById('idint').value = 0;
 RdvParamsChange(0, wdw);
}

function RdvParamsChange(postchanges, wdw) {
 var css = wdw.document.getElementById('css').value;
 var ident = wdw.document.getElementById('ident').value;
 var idsit = wdw.document.getElementById('idsit').value;
 var rdvsitid = wdw.document.getElementById('rdvsitid').value;
 var idrdv = wdw.document.getElementById('idrdv').value;
 var action = wdw.document.getElementById('action').value;
 var idses = window.document.getElementById('idses').value;
 var idveh = wdw.document.getElementById('idveh').value;
 var idmar = wdw.document.getElementById('idmar').value;
 var idmod = wdw.document.getElementById('idmod').value;
 var idint = wdw.document.getElementById('idint').value;
 var daterdv = wdw.document.getElementById('daterdv').value;
 var heurerdv = wdw.document.getElementById('heurerdv').value;
 if (wdw.document.getElementById('vcrdv').checked) {var vcrdv = 1;} else {var vcrdv = 0;};
 if ((wdw.document.getElementById('svec')) && (wdw.document.getElementById('svec').value == 1)) {var svec = 1;} else {var svec = 0;};
 var comrdv = wdw.document.getElementById('comrdv').value;
 if (postchanges == 0)
  {wdw.location.href = "Actions.php?ident=" + ident + "&idsit=" + idsit + "&action=" + action + "&rdvsitid=" + rdvsitid + "&idrdv=" + idrdv + "&idmar=" + idmar + "&idmod=" + idmod + "&idveh=" + idveh + "&idint=" + idint + "&daterdv=" + daterdv + "&heurerdv=" + heurerdv + "&vcrdv=" + vcrdv + "&comrdv=" + comrdv + "&svec=" + svec + "&css=" + css + "&" + idses;}
 else
  {if ((idmar == 0) && (idveh == 0)) {alert("Vous devez renseigner la marque du véhicule !");}
   else if ((idmod == 0) && (idveh == 0)) {alert("Vous devez renseigner le modèle du véhicule !");}
   else if ((idmar == 0) && (idmod == 0) && (idveh == 0)) {alert("Vous devez sélectionner véhicule !");}
   else if (idint == 0) {alert("Vous devez sélectionner un type d'intervention !");}
   else if (!DateIsValid(daterdv)) {alert("La date de rendez-vous doit être au format JJ/MM/AAAA");}
   else if (heurerdv == 0) {alert("Vous devez sélectionner une heure d'arrivée !");}
   else {wdw.location.href = "ValidRdv.php?ident=" + ident + "&idsit=" + idsit + "&action=" + action + "&rdvsitid=" + rdvsitid + "&idrdv=" + idrdv + "&idmar=" + idmar + "&idmod=" + idmod + "&idveh=" + idveh + "&idint=" + idint + "&daterdv=" + daterdv + "&heurerdv=" + heurerdv + "&vcrdv=" + vcrdv + "&comrdv=" + comrdv + "&css=" + css + "&" + idses;}};
}

function RdvCancel() {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 var rdvsitid = window.document.getElementById('rdvsitid').value;
 var idrdv = window.document.getElementById('idrdv').value;
 var idses = window.document.getElementById('idses').value;
 window.location.href = "ValidRdv.php?ident=" + ident + "&idsit=" + idsit + "&action=4&rdvsitid=" + rdvsitid + "&idrdv=" + idrdv + "&css=" + css + "&" + idses;
}

function ModifyClient(info) {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 var idses = window.document.getElementById('idses').value;
 OpenMecaPlanningPopup("MecaPlanning_Client", "ModifyClient.php?ident=" + ident + "&idsit=" + idsit + "&info=" + info + "&css=" + css + "&" + idses);
}

function ViewPlanning(datebegin) {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 if (window.document.getElementById('rdvsitid')) {var idsit = window.document.getElementById('rdvsitid').value;};
 var idses = window.document.getElementById('idses').value;
 var daterdv = '';
 if (window.document.getElementById('daterdv') && DateIsValid(window.document.getElementById('daterdv').value))
  {daterdv = "&daterdv=" + window.document.getElementById('daterdv').value;};
 if (datebegin != '')
  {daterdv = daterdv + "&datebegin=" + datebegin;};
 OpenMecaPlanningPopup("MecaPlanning_Calendar", "ViewPlanning.php?ident=" + ident + "&idsit=" + idsit + daterdv + "&css=" + css + "&" + idses);
}

function ViewThisPlanning(daterdv, datebegin) {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 var idses = window.document.getElementById('idses').value;
 window.location.href = "ViewPlanning.php?ident=" + ident + "&idsit=" + idsit + "&daterdv=" + daterdv + "&datebegin=" + datebegin + "&css=" + css + "&" + idses;
}

function DateIsValid(StringDate) {
 var err = 0;
 strDate = StringDate;
 if (strDate.length != 10) {err = 1;};
 strMonth = strDate.substring(3, 5);
 strSep1 = strDate.substring(2, 3);
 strDay = strDate.substring(0, 2);
 strSep2 = strDate.substring(5, 6);
 strYear = strDate.substring(6, 10);

 if (strMonth < 1 || strMonth > 12) {err = 1;};
 if (strSep1 != '/') {err = 1;};
 if (strDay < 1 || strDay > 31) {err = 1;};
 if (strSep2 != '/') {err = 1;};
 if (strYear < 0 || strYear > 2099) {err = 1;};

 if (strMonth == 4 || strMonth == 6 || strMonth == 9 || strMonth == 11) {if (strDay == 31) {err = 1;};};

 // february, leap year
 if (strMonth == 2)
  {var Bisextile = parseInt(strYear / 4);
   if (isNaN(Bisextile)) {err = 1;};
   if (strDay > 29) {err = 1;};
   if (strDay == 29 && ((strYear / 4) != parseInt(strYear / 4))) {err = 1;}};

 if (err == 1)
  {return false;}
 else
  {return true;};
}

function SelThisDate() {
 if (opener.document.getElementById('daterdv'))
  {opener.document.getElementById('daterdv').value = window.document.getElementById('daterdv').value;
   opener.document.getElementById('rdvsitid').value = window.document.getElementById('idsit').value;
   RdvParamsChange(0, opener);};
 window.close();
}

function ViewCurrentDate() {
 if (document.getElementById('tdCurDate')) 
  {document.getElementById('tdCurDate').onclick();}
}

function ViewThisDate(tag, vehicles, week, day, month, rdvdate) {
 window.document.getElementById('DivSelect').style.visibility = 'hidden';
 window.document.getElementById('daterdv').value = rdvdate;
 window.document.getElementById('CalSelDate').innerHTML = week+' '+day+' '+month;
 window.document.getElementById('CalSelColor').className = 'MPCalendar_DateDay'+tag;
 if (tag == 1)
  {window.document.getElementById('CalSelDesc').innerHTML = "Il reste de la place disponible sur ce jour";}
 else if (tag == 2)
  {window.document.getElementById('CalSelDesc').innerHTML = "Il reste de la place pour de petites interventions";}
 else if (tag == 3)
  {window.document.getElementById('CalSelDesc').innerHTML = "Il n'y a plus de place disponible sur cette journ&eacute;e";}
 else 
  {window.document.getElementById('CalSelDesc').innerHTML = "Cette journ&eacute;e n'est pas disponible pour la prise de rendez-vous";};
 if (window.document.getElementById('CalSelVehicles'))
  {if ((vehicles > 0) && (tag < 3))
		  {window.document.getElementById('CalSelVehicles').innerHTML = "Il reste "+vehicles+" v&eacute;hicule(s) de courtoisie disponible(s) ce jour";}
		 else
		  {window.document.getElementById('CalSelVehicles').innerHTML = "Il n'y a aucun v&eacute;hicule de courtoisie disponible ce jour";};};
 if ((tag < 3) && (opener.document.getElementById('daterdv')))
  {window.document.getElementById('CalSelLink').innerHTML = '<B><A HREF="Javascript:SelThisDate();">S&eacute;lectionner cette date</A></B>';}
 else if ((window.document.getElementById('chgsit')) && (tag == 4))
  {window.document.getElementById('CalSelLink').innerHTML = '<IMG alt="Attention" src="Images/warning.gif"  onMouseOver="ShowTopAlert(\'Vous pouvez s&eacute;lectionner un autre atelier disponible &agrave; cette date\');" onMouseOut="nd();"> <B><A HREF="Javascript:SelectSite();">Chercher de la place disponible sur un autre site</A></B>';}
 else
  {window.document.getElementById('CalSelLink').innerHTML = '&nbsp;';};

 window.document.getElementById('DivSelect').style.visibility = 'visible';
}

function PerformAction(action, idrdv, MPEmail) {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 var idses = window.document.getElementById('idses').value;

 window.location.href = "Actions.php?ident=" + ident + "&idsit=" + idsit + "&action=" + action + "&idrdv=" + idrdv + "&MPEmail=" + MPEmail + "&css=" + css + "&" + idses;
}

function EnableRecBtn() {
 window.document.getElementById('ARecord').style.visibility = 'visible';
}

function ModifyThisClient(info) {
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 var idses = window.document.getElementById('idses').value;
 var modifyparams = '';
 if (info == 0)
  {var clitel = FormatTel(window.document.getElementById('clitel').value);
   var cliteldom = FormatTel(window.document.getElementById('cliteldom').value);
   var clitelbur = FormatTel(window.document.getElementById('clitelbur').value);
   var clifax = FormatTel(window.document.getElementById('clifax').value);
   if ((!GSMIsValid(clitel)) && (clitel != ''))
    {alert("Votre numéro de portable n'est pas valide");}
   else if ((!TelIsValid(cliteldom)) && (cliteldom != ''))
    {alert("Votre numéro de téléphone (Domicile) n'est pas valide");}
   else if ((!TelIsValid(clitelbur)) && (clitelbur != ''))
    {alert("Votre numéro de téléphone (Bureau) n'est pas valide");}
   else if ((!TelIsValid(clifax)) && (clifax != ''))
    {alert("Votre numéro de fax n'est pas valide");}
   else
    {modifyparams = '&clitel=' + clitel + '&cliteldom=' + cliteldom + '&clitelbur=' + clitelbur + '&clifax=' + clifax;};}
 else
  {var email = FormatEmail(window.document.getElementById('climail').value);
   var email2 = FormatEmail(window.document.getElementById('climail2').value);
   var pwd = trim(window.document.getElementById('clipwd').value);
   var pwd2 = trim(window.document.getElementById('clipwd').value);
   if (!EmailIsValid(email))
    {alert("Votre e-mail n'est pas valide");}
   else if (pwd.length < 4)
    {alert("Votre mot de passe doit contenir plus de 4 caractères");}
   else  if (email != email2)
    {alert("Les e-mails ne sont pas identiques");}
   else if (pwd != pwd2)
    {alert("Les mots de passe ne sont pas identiques");}
   else
    {modifyparams = '&climail=' + email + '&clipwd=' + pwd;};};
 if (modifyparams != '')
  {opener.location.href = "ValidClient.php?ident=" + ident + "&idsit=" + idsit + "&info=" + info + modifyparams + "&css=" + css + "&" + idses;
   window.close();};
}

function trim(string) {
 return string.replace(/(^\s*)|(\s*$)/g,'');
} 
 
function FormatEmail(email) {
 return trim(email.toLowerCase());
}

function FormatTel(num) {
 var onlynum = '';
 for (var i=0; i<num.length; i++)
  {if (!isNaN(parseInt(num.substring(i,i+1))))
    {onlynum = onlynum + num.substring(i,i+1);};};
 return onlynum;
}

function EmailIsValid(email){
 var verif = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-.]{2,}[.][a-zA-Z]{2,3}$/;
 return (verif.exec(email) != null);
}

function GSMIsValid(num){
 var verif = /^[0-9_.-]{10}$/;
// return ((num.substring(0,2) == '06') && (verif.exec(num) != null));
 return ((num.substring(0,1) == '0') && (verif.exec(num) != null));
}

function TelIsValid(num){
 var verif = /^[0-9_.-]{10}$/;
 return ((num.substring(0,1) == '0') && (verif.exec(num) != null));
}

function warning_show(){
 document.getElementById('divWarning').style.border = '2px solid #ff0000';
 window.setTimeout( 'warning_hide()', 700 );
}
	
function warning_hide(){
 document.getElementById('divWarning').style.border = '2px solid #ffffff';
 window.setTimeout( 'warning_show()', 250 );
}

function ShowAlert(ltext)
{overlib(ltext, CAPTION, 'Attention !', CAPCOLOR, '#FFFFFF', RIGHT, OFFSETY, 28, FGCOLOR, "#FAFAFA", BGCOLOR, "#D92222", BORDER, "1", TEXTFONT, "Arial", TEXTSIZE, "8pt", TEXTCOLOR, "#000000");}

function ShowTopAlert(ltext)
{overlib(ltext, CAPTION, 'Attention !', CAPCOLOR, '#FFFFFF', RIGHT, OFFSETY, -50, FGCOLOR, "#FAFAFA", BGCOLOR, "#D92222", BORDER, "1", TEXTFONT, "Arial", TEXTSIZE, "8pt", TEXTCOLOR, "#000000");}

function ShowLeftAlert(ltext)
{overlib(ltext, CAPTION, 'Attention !', CAPCOLOR, '#FFFFFF', LEFT, OFFSETY, 28, FGCOLOR, "#FAFAFA", BGCOLOR, "#D92222", BORDER, "1", TEXTFONT, "Arial", TEXTSIZE, "8pt", TEXTCOLOR, "#000000");}

function ChangeSite(newsitid){
 var daterdv = window.document.getElementById('daterdv').value;
 var datebegin = window.document.getElementById('chgsit').value;
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idses = window.document.getElementById('idses').value;
 window.location.href = "ViewPlanning.php?ident=" + ident + "&idsit=" + newsitid + "&daterdv=" + daterdv + "&datebegin=" + datebegin + "&css=" + css + "&" + idses;
}

function SelectSite(){
 var daterdv = window.document.getElementById('daterdv').value;
 var datebegin = window.document.getElementById('chgsit').value;
 var css = window.document.getElementById('css').value;
 var ident = window.document.getElementById('ident').value;
 var idsit = window.document.getElementById('idsit').value;
 var idses = window.document.getElementById('idses').value;
 window.location.href = "ViewPlanningSite.php?ident=" + ident + "&idsit=" + idsit + "&daterdv=" + daterdv + "&datebegin=" + datebegin + "&css=" + css + "&" + idses;
}

function ViewSite(datebegin) {
 if (window.document.getElementById('rdvsitid')) 
  {var css = window.document.getElementById('css').value;
   var ident = window.document.getElementById('ident').value;
   var idsit = window.document.getElementById('idsit').value;
   if (window.document.getElementById('rdvsitid')) {var idsit = window.document.getElementById('rdvsitid').value;};
   var idses = window.document.getElementById('idses').value;
   OpenMecaPlanningPopup("MecaPlanning_Site", "ViewSite.php?ident=" + ident + "&idsit=" + idsit + "&css=" + css + "&" + idses);};
}

