// charge les informations de la page en AJAX
function ajaxLoadLivreOr(id_voyage, debut, fin)
{
  //alert('urlLivreOrLoad');
  //alert(id_voyage);
  var strURL = urlLivreOrLoad;

  if (debut != null)
    strURL += '?debut=' + debut;
  else
    strURL += '?debut=1';

  if (fin != null)
    strURL += '&fin=' + fin;
  else
    strURL += '&fin=10';

  if (id_voyage != null)
  {
    strURL += '&id_voyage=' + id_voyage;
  }
  
  //alert(strURL);
  var xhr = getXHR();
  xhr.onreadystatechange = function() { ajaxDisplayComments(xhr); };
  xhr.open('GET', strURL, true);
  xhr.send(null);
}

function ajaxDisplayComments(xhr)
{
  //alert('ajaxDisplayComments');
  //alert(xhr.responseText);
  
  if (xhr.responseXML == null || xhr.responseXML.documentElement == null)
    return;
  
  if (xhr.responseXML.documentElement.childNodes.length == 0)
  {
    alert("XML response vide");
    return;
  }
  
  if (xhr.responseXML.documentElement.childNodes[0].nodeName != 'commentaires')
  {
    alert(xhr.responseText);
    return;
  }
  
  var div_layout =  document.getElementById('layout_comment');
  while (div_layout.childNodes.length > 0)
    div_layout.removeChild(div_layout.firstChild);
  
	var docXML = xhr.responseXML.documentElement;
	var items = docXML.childNodes[0].childNodes;
	
	//alert(docXML.childNodes[0].nodeName);
	
	var div = null;
	var div_class = null;
	var table = null;
	var tbody = null;
	var cellspacing = null;
	var cellpadding = null;
	var table_class = null;
	var tr = null;
	var td = null;
	var td_class = null;
	var colspan = null;
	var textNode = null;
	var br = null;
	var a = null;
	var href = null;
	
  for (var i = 0; i < items.length; i++)
  {
    //alert(items[i].nodeName);

    div = document.createElement('div');
    div_class = document.createAttribute('class');
    div_class.nodeValue = 'livre_or';
    div.setAttributeNode(div_class);

    table = document.createElement('table');
    cellspacing = document.createAttribute('cellspacing');
    cellspacing.nodeValue = '0';
    table.setAttributeNode(cellspacing);
    cellpadding = document.createAttribute('cellpadding');
    cellpadding.nodeValue = '0';
    table.setAttributeNode(cellpadding);
    table_class = document.createAttribute('class');
    table_class.nodeValue = 'message';
    table.setAttributeNode(table_class);
    
    tbody = document.createElement('tbody');

    tr = document.createElement('tr');
    
    td = document.createElement('td');
    td_class = document.createAttribute('class');
    td_class.nodeValue = 'message_header_left';
    td.setAttributeNode(td_class);
    var str = items[i].childNodes[4].firstChild.nodeValue;
    if (items[i].childNodes[6].firstChild.nodeValue != null && items[i].childNodes[6].firstChild.nodeValue != '')
    {
      str += ' (' + items[i].childNodes[6].firstChild.nodeValue;
      if (items[i].childNodes[7].firstChild.nodeValue != null && items[i].childNodes[7].firstChild.nodeValue != '')
        str += ', ' + items[i].childNodes[7].firstChild.nodeValue;
      str += ')';
    }
    else if (items[i].childNodes[7].firstChild.nodeValue != null && items[i].childNodes[7].firstChild.nodeValue != '')
    {
      str += ' (' + items[i].childNodes[7].firstChild.nodeValue + ')';
    }
    textNode = document.createTextNode(str);
    td.appendChild(textNode);

    tr.appendChild(td);
    
    td = document.createElement('td');
    td_class = document.createAttribute('class');
    td_class.nodeValue = 'message_header_right';
    td.setAttributeNode(td_class);
    textNode = document.createTextNode(items[i].childNodes[2].firstChild.nodeValue + ' ' + items[i].childNodes[3].firstChild.nodeValue);
    td.appendChild(textNode);

    tr.appendChild(td);
    
    tbody.appendChild(tr);
    
    tr = document.createElement('tr');
    
    td = document.createElement('td');
    colspan = document.createAttribute('colspan');
    colspan.nodeValue = '2';
    td.setAttributeNode(colspan);
    td_class = document.createAttribute('class');
    td_class.nodeValue = 'message_body';
    td.setAttributeNode(td_class);
    td.innerHTML = items[i].childNodes[8].firstChild.nodeValue;

    tr.appendChild(td);

    tbody.appendChild(tr);

    table.appendChild(tbody);
    
    div.appendChild(table);
    //alert(div.innerHTML);

    div_layout.appendChild(div);
  }
  
  div = document.getElementById('msg_range');
  while (div.childNodes.length > 0)
    div.removeChild(div.firstChild);

  textNode = document.createTextNode('Message : ' + docXML.childNodes[1].childNodes[1].firstChild.nodeValue + ' à ' + docXML.childNodes[1].childNodes[2].firstChild.nodeValue);
  div.appendChild(textNode);
  
  div = document.getElementById('msg_total');
  while (div.childNodes.length > 0)
    div.removeChild(div.firstChild);
  
  textNode = document.createTextNode('Nombre de messages : ' + docXML.childNodes[1].childNodes[0].firstChild.nodeValue);
  div.appendChild(textNode);
  
  div = document.getElementById('msg_paging');
  while (div.childNodes.length > 0)
    div.removeChild(div.firstChild);

  textNode = document.createTextNode('Pages : ');
  div.appendChild(textNode);
  
  var nbPages = Math.ceil(docXML.childNodes[1].childNodes[0].firstChild.nodeValue / 10);
  var pageCourante = Math.ceil(docXML.childNodes[1].childNodes[1].firstChild.nodeValue / 10);
  var id_voyage = null;
  if (docXML.childNodes[1].childNodes.length == 4)
    id_voyage = docXML.childNodes[1].childNodes[3].firstChild.nodeValue;
  
  for (var i = 1; i <= nbPages; ++i)
  {
    if (i == pageCourante)
    {
      textNode = document.createTextNode(i);
      div.appendChild(textNode);
    }
    else
    {
      a = document.createElement('a');
      href = document.createAttribute('href');
      href.nodeValue = 'javascript:onClickPage(' + id_voyage + ', ' + i + ')';
      a.setAttributeNode(href);
      textNode = document.createTextNode(i);
      a.appendChild(textNode);

      div.appendChild(a);
    }
    
    if (i < nbPages)
    {
      textNode = document.createTextNode(' - ');
      div.appendChild(textNode);
    }
  }
  
}


function onClickEnvoyer()
{
  //alert('onClickEnvoyer');
  
  var user =  document.getElementById('user');
  var email =  document.getElementById('email');
  var ville =  document.getElementById('ville');
  var pays =  document.getElementById('pays');
  var texte =  document.getElementById('texte');
  
  var id_voyage = '';
  var strQueryString = window.location.href;
  var i1 = strQueryString.lastIndexOf('id_voyage=');
  var i2 = strQueryString.indexOf('&', i1+1);

  if (i1 != -1)
  {
    if (i2 == -1)
      id_voyage = strQueryString.substr(i1+10, 10);
    else
      id_voyage = strQueryString.substr(i1+10, i2-i1-10);
  }
  
  if (user.value == "")
  {
    alert("Le champ nom ou pseudo n'a pas été renseigné");
    user.focus();
    return;
  }
  
  /*if (email.value == "")
  {
    alert("Le champ e-mail n'a pas été renseigné");
    email.focus();
    return;
  }*/

  if (texte.value == "")
  {
    alert("Le champ message n'a pas été renseigné");
    texte.focus();
    return;
  }
  
  //alert(texte.value.split("\n").join("<br\/>"));

  var xhr = getXHR();
  xhr.onreadystatechange = function() { ajaxDisplayComments(xhr); };
  
  strXML  = '<query>';
  strXML += '<commentaire>';
  strXML += '<user>';
  strXML += '<![CDATA[';
  strXML += user.value;
  strXML += ']]>';
  strXML += '</user>';
  strXML += '<email>';
  strXML += '<![CDATA[';
  strXML += email.value;
  strXML += ']]>';
  strXML += '</email>';
  strXML += '<ville>';
  strXML += '<![CDATA[';
  strXML += ville.value;
  strXML += ']]>';
  strXML += '</ville>';
  strXML += '<pays>';
  strXML += '<![CDATA[';
  strXML += pays.value;
  strXML += ']]>';
  strXML += '</pays>';
  strXML += '<texte>';
  strXML += '<![CDATA[';
  if (texte.value.indexOf("\r\n") != -1)
  	strXML += texte.value.split("\r\n").join("<br\/>");
  else
  	strXML += texte.value.split("\n").join("<br\/>");
  strXML += ']]>';
  strXML += '</texte>';
  
  if (id_voyage != '')
  {
    strXML += '<id_voyage>';
    strXML += id_voyage;
    strXML += '</id_voyage>';
  }

  strXML += '</commentaire>';
  strXML += '</query>';
  //alert(strXML);
  
  var strURL = urlLivreOrSave;
  if (id_voyage != '')
    strURL += '?id_voyage=' + id_voyage;
  //alert(strURL);
  xhr.open('POST', strURL, true);
  xhr.setRequestHeader("Content-Type", "text/xml");
  xhr.setRequestHeader("Content-length", strXML.length);
  xhr.setRequestHeader("Connection", "close");  
  xhr.send(strXML);
  
  user.value = '';
  email.value = '';
  ville.value = '';
  pays.value = '';
  texte.value = '';
}

function onClickPage(id_voyage, i)
{
  ajaxLoadLivreOr(id_voyage, 10*(i-1)+1, 10*i);
}

function onChangeVoyage()
{
  //alert('onChangeVoyage');
  var sel = document.getElementById('sel_voyage');
  //alert(sel.selectedIndex);
  
  if (sel.selectedIndex == 0)
    location = urlLivreOr;
  else
    location = urlLivreOr + '?id_voyage=' + sel.selectedIndex;
}

function onClickSmiley(strSmile)
{
  //alert('onClickSmiley');
  //alert(strSmile);
  
  var ta = document.getElementById('texte');

  if (ta.selectionEnd > 0)
  { 
    var selectStart = ta.selectionStart;
    var selectTop = ta.scrollTop;
    var selectEnd = ta.selectionEnd;
    
    if (selectEnd <= 2)
      selectEnd = chem.textLength;
      
    var strStart = ta.value.substring(0, selectStart);
    var strMiddle = ta.value.substring(selectStart, selectEnd);
    var strEnd = ta.value.substring(selectEnd, ta.textLength);
    
    strMiddle = ' ' + strSmile + ' ' + strMiddle;

    ta.value = strStart + strMiddle + strEnd;
    var curPos = selectStart + (strMiddle.length);
    ta.selectionStart = curPos;
    ta.selectionEnd = curPos;
    ta.scrollTop = selectTop;
  }
  else
  { 
   ta.value += strSmile; 
  }
  
  ta.focus();
}
