var nbce = 0;

function cmsl_redirect() {
  document.getElementById('popupconfbut').innerHTML="<p class='instructions' style='text-align:center; padding-bottom:10px;'>Connecting to your itinerary, please wait...</p>";
  top.location.href='/itineraries/configuration.php';
}

function close_cmslw(gosl) {
  if (nbce == 1) {
    if (gosl)
      naction = "cmsl_redirect();";
    else
      naction = "cs_mbox_close();";
    cs_mbox_content("<table class='lay-mbox'><tr><td class='lay-mbox-title'>My itinerary</td></tr><tr><td class='lay-mbox-text'>You may consult, edit and download your itinerary as well as obtain suggested stopping places at any time using the menu on the left.</td></tr><tr><td class='lay-mbox-text' style='text-align:center;'><div id='popupconfbut'><button onclick=\""+naction+" return false;\"  style='margin-top:5px; margin-bottom:10px;'>OK</button></div></td></tr></table>");
  }
  else {
    if (gosl)
      cmsl_redirect();
    else
      cs_mbox_close();
  }
}

function add_etape(type,iid) {
  var req = null; 
  if(window.XMLHttpRequest)
    req = new XMLHttpRequest(); 
  else if (window.ActiveXObject)
    req = new ActiveXObject('Microsoft.XMLHTTP'); 
  req.onreadystatechange = function() { 
    if(req.readyState == 4) {
      if(req.status == 200) {
        if (req.responseText == '#ERR_MAX_COUNT#')
          text_msg = "<td class='lay-mbox-error'>Your itinerary has already reached the maximum number of stopping places.";
        else if (req.responseText == '#ERR_NO_CUSTOM#')
          text_msg = "<td class='lay-mbox-error'>Your itinerary is currently in automatic mode. You must switch to manual mode in order to add stopping places. To do this, click on 'Access my itinerary'.";
        else if (req.responseText == '#ERR_BAD_ID#')
          text_msg = "<td class='lay-mbox-error'>An error has occurred and this stopping place has not been added to your itinerary.";
        else if (req.responseText == '#ERR_ALREADY_IN#')
          text_msg = "<td class='lay-mbox-error'>This stopping place is already on your itinerary.";
        else {
          nbce = req.responseText;
          if (nbce > 1)
            edisp = nbce+" stopping places";
          else
            edisp = nbce+" stopping place";
          if (document.getElementById('cmslrmdisp'))
            document.getElementById('cmslrmdisp').innerHTML = edisp;
          else if (document.getElementById('cmslrmempty'))
            document.getElementById('cmslrmempty').innerHTML="<div class='lay-lp-boxmain' onclick=\"top.location.href='/itineraries/configuration.php';\"><div class='lay-lp-boxtitle' style='margin-top:25px;'><img src='/visuals/common/circuits2.gif'>My itinerary</div><div class='lay-lp-boxcontent' style='background-position:center right; padding-bottom:5px;'><div id='cmslrmdisp'>Manual mode<br>"+edisp+"</div></div><div class='lay-lp-boxtitle' style='background-position:bottom right; font-size:10px;'>Criteria - Consult</div></div>";
          text_msg = "<td class='lay-mbox-text'>Your itinerary has been updated and now contains <b>"+nbce+"</b> stopping place";
            if (nbce > 1)
              text_msg = text_msg+"s";
        }
      }
      else
        text_msg = "<td class='lay-mbox-error'>An error occurred during connection and this stopping place has not been added to your itinerary.";
      cs_mbox_content("<table class='lay-mbox'><tr><td class='lay-mbox-title'>My itinerary</td></tr><tr>"+text_msg+"</tr><tr><td class='lay-mbox-text'><div id='popupconfbut'><table class='tdf'><tr><td class='lay-mbox-text'><button onclick=\"close_cmslw(true); return false;\" style='margin-top:5px; margin-bottom:10px;'>Access&nbsp;my&nbsp;itinerary</button></td><td class='lay-mbox-text' style='text-align:right;'><button onclick=\"close_cmslw(false); return false;\"  style='margin-top:5px; margin-bottom:10px;'>Continue</button></td></tr></table></div></td></tr></table>");
    }
  }
  cs_mbox_open("<table class='lay-mbox'><tr><td class='lay-mbox-title'>My itinerary</td></tr><tr><td class='lay-mbox-text'><p class='instructions' style='text-align:center'><br>Connecting to your itinerary, please wait...<br><img src='/visuals/common/loader.gif'><br><br></p></td></tr></table>");
  req.open('POST', '/itineraries/add_etape.php', true); 
  req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8'); 
  req.send('type='+type+'&iid='+iid);
}

