var loader = new Image;
loader.src = racine+'/css/images/loader.gif';

function submitReview(review, shop, link) {
  var empty = true;
  var ilegal_caracters = false;
  var i = 0;

  while(i < review.length) {
    if(review.substr(i, 1) != ' ' && review.substr(i, 1) != '\n')
      empty = false;

    if(review.substr(i, 1) == '¤' || review.substr(i, 1) == '§')
      ilegal_caracters = true;

    i++;
  }

  if(empty)
    alert('You must write something to submit it !');
  else if(ilegal_caracters)
    alert('The following characters : ¤ and § are not allowed !');
  else {
    blockCleaner('modal-window');

    if(document.all)
      var visible_height = 260;
    else
      var visible_height = window.innerHeight;

    var margin_top = (visible_height - 60) / 2;

    var container = document.createElement('div');
    container.style.width = '60px';
    container.style.height = '60px';
    container.style.background = '#ffffff';
    container.style.border = '1px solid #ff0000';

    var loader = document.createElement('img');
    loader.setAttribute('src', racine+'/css/images/loader.gif');
    loader.setAttribute('alt', '');

    container.style.marginLeft = 'auto';
    container.style.marginRight = 'auto';
    container.style.marginTop = margin_top+'px';

    container.appendChild(loader);
    document.getElementById('modal-window').appendChild(container);
    document.getElementById('modal-window').style.display = 'block';

    sr = createRequest();
    sr.open('post', racine+'/js/review.php', true);
    sr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    sr.onreadystatechange = submitReviewControl;
    sr.send('review='+review+'&shop-id='+shop_id+'&shop='+shop+'&link='+link);
  }
}

function submitReviewControl() {
  if(sr.readyState == 4 && sr.status == 200) {
    var response = sr.responseText.split('/');

    if(response[0] == '1') {
      changePage(1);
      alert('Your review has been saved !');
      document.getElementById('review-written').value = '';
      document.getElementById('write-review').style.display = 'none';
    }
  }
}

function changePage(page) {
  if(document.getElementById('modal-window').style.display == 'none') {
    blockCleaner('modal-window');

    if(document.all)
      var visible_height = 260;
    else
      var visible_height = window.innerHeight;

    var margin_top = (visible_height - 60) / 2;

    var container = document.createElement('div');
    container.style.width = '60px';
    container.style.height = '60px';
    container.style.background = '#ffffff';
    container.style.border = '1px solid #ff0000';

    var loader = document.createElement('img');
    loader.setAttribute('src', racine+'/css/images/loader.gif');
    loader.setAttribute('alt', '');

    container.style.marginLeft = 'auto';
    container.style.marginRight = 'auto';
    container.style.marginTop = margin_top+'px';

    container.appendChild(loader);
    document.getElementById('modal-window').appendChild(container);
    document.getElementById('modal-window').style.display = 'block';
  }

  cp = createRequest();
  cp.open('post', racine+'/js/review_change_page.php', true);
  cp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  cp.onreadystatechange = changePageControl;
  cp.send('page='+page+'&per_page='+per_page+'&shop_id='+shop_id);
}

function changePageControl() {
  if(cp.readyState == 4 && cp.status == 200) {
    document.getElementById('modal-window').style.display = 'none';
    var response = cp.responseText.split('¤');

    if(response[0] == '1') {
      var nb_results = response[response.length - 1];
      current_page = parseInt(response[response.length - 2]);

      var i, j;
      var lines = new Array();
      var box, paragraph, block, image, link, returnLine;
      for(i = 1; i < response.length - 2; i++) {
        var data = response[i].split('§');

        lines[i] = document.createElement('tr');
        box = document.createElement('th');
        box.setAttribute('scope', 'row');

        block = document.createElement('div');

        if(document.all)
          block.setAttribute('className', 'img');
        else
          block.setAttribute('class', 'img');

        link = document.createElement('a');
        link.setAttribute('href', '#');

        image = document.createElement('img');
        image.setAttribute('src', racine+'/photos/members/'+data[0]);
        image.setAttribute('alt', '');

        returnLine = document.createElement('br');

        link.appendChild(image);
        block.appendChild(link);
        box.appendChild(block);
        box.appendChild(document.createTextNode('Reviews'));
        box.appendChild(returnLine);
        box.appendChild(document.createTextNode('Written: '+data[7]));
        lines[i].appendChild(box);

        box = document.createElement('td');
        paragraph = document.createElement('p');

        if(document.all)
          paragraph.setAttribute('className', 'date');
        else
          paragraph.setAttribute('class', 'date');

        paragraph.appendChild(document.createTextNode(data[1] + ' at ' + data[2]));
        box.appendChild(paragraph);

        paragraph = document.createElement('p');

        if(document.all)
          paragraph.setAttribute('className', 'title');
        else
          paragraph.setAttribute('class', 'title');

        link = document.createElement('a');
        link.setAttribute('href', racine+'/profile-'+data[0]+'.html');
        link.appendChild(document.createTextNode(data[3]+' '+data[4].substr(0, 1)));

        paragraph.appendChild(link);
        box.appendChild(paragraph);

        paragraph = document.createElement('p');

        if(document.all)
          paragraph.setAttribute('className', 'vote');
        else
          paragraph.setAttribute('class', 'vote');

        for(j = 1; j <= 5; j++) {
          image = document.createElement('img');

          if(j <= parseInt(data[6]))
            image.setAttribute('src', racine+'/css/images/sstar.gif');
          else
            image.setAttribute('src', racine+'/css/images/sstarg.gif');

          image.setAttribute('alt', '');
          paragraph.appendChild(image);
          paragraph.appendChild(document.createTextNode(' '));
        }

        box.appendChild(paragraph);

        paragraph = document.createElement('p');

        j = 0;
        var to_show = '';
        while(j < data[5].length) {
          if(data[5].substr(j, 6) == '<br />') {
            paragraph.appendChild(document.createTextNode(to_show));
            paragraph.appendChild(document.createElement('br'));
            to_show = '';
            j += 6;
          }
          else {
            to_show += data[5].substr(j, 1);

            if((j + 1) >= data[5].length)
              paragraph.appendChild(document.createTextNode(to_show));

            j++;
          }
        }

        box.appendChild(paragraph);
        lines[i].appendChild(box);
      }

      // PAGING
      var nb_pages = Math.ceil(parseInt(nb_results) / parseInt(per_page));

      if(nb_pages > 5) {
        var option;
        var list = document.createElement('select');
        list.setAttribute('id', 'pages-list');

        list.onchange = function() {
          if(document.getElementById('pages-list').value != 'null' && document.getElementById('pages-list').value != page)
            changePage(document.getElementById('pages-list').value);
        }

        if(document.all)
          list.style.setAttribute('cssText', 'float:right');
        else
          list.setAttribute('style', 'float:right');

        option = document.createElement('option');
        option.setAttribute('value', 'null');
        option.appendChild(document.createTextNode('All pages'));
        list.appendChild(option);

        for(j = 1; j <= nb_pages; j++) {
          option = document.createElement('option');
          option.setAttribute('value', j);
          option.appendChild(document.createTextNode('Page '+j));
          list.appendChild(option);
        }
      }

      var table = document.createElement('table');
      table.style.border = '0px';
      table.setAttribute('border-spacing', '0px');
      table.style.borderCollapse = 'collapse';
      table.align = 'right';

      var tbody = document.createElement('tbody');

      var line = document.createElement('tr');

      var pages_list = new Array();
      pages_list[0] = 1;
      pages_list[4] = nb_pages;
      pages_list[5] = nb_pages;

      if(current_page == 1 || current_page == 2 || current_page == 3 || current_page == 4) {
        pages_list[1] = 2;
        pages_list[2] = 3;
        pages_list[3] = 4;
      }
      else if(current_page == nb_pages || current_page == (nb_pages - 1)) {
        pages_list[1] = nb_pages - 3;
        pages_list[2] = nb_pages - 2;
        pages_list[3] = nb_pages - 1;
      }
      else {
        pages_list[1] = current_page - 1;
        pages_list[2] = current_page;
        pages_list[3] = current_page + 1;
      }

      if(current_page > 1 && nb_pages > 1) {
        box = document.createElement('td');
        box.style.padding = '2px';
        link = document.createElement('a');
        link.setAttribute('href', '#');
        link.onclick = function() {
          changePage(current_page - 1);
          return false;
        }
        link.appendChild(document.createTextNode('<'));
        box.appendChild(link);

        line.appendChild(box);
      }

      for(i = 0; i < 5 && i < nb_pages; i++) {
        box = document.createElement('td');
        box.style.padding = '2px';

        link = document.createElement('a');
        link.setAttribute('href', '#');
        if(current_page == pages_list[i]) {
          if(document.all)
            link.setAttribute('className', 'sel');
          else
            link.setAttribute('class', 'sel');
        }
        else
          link = updateEvent(link, pages_list[i]);

        link.appendChild(document.createTextNode(pages_list[i]));
        box.appendChild(link);

        if((pages_list[i + 1] - pages_list[i]) > 1)
          box.appendChild(document.createTextNode(' ...'));

        line.appendChild(box);
      }

      if(current_page < nb_pages) {
        box = document.createElement('td');
        box.style.padding = '2px';
        link = document.createElement('a');
        link.setAttribute('href', '#');
        link.onclick = function() {
          changePage(current_page + 1);
          return false;
        }
        link.appendChild(document.createTextNode('>'));
        box.appendChild(link);
      }

      line.appendChild(box);
      tbody.appendChild(line);
      table.appendChild(tbody);

      block = document.getElementById('table-reviews');
      blockCleaner(block);

      for(i = 1; i < response.length - 2; i++) {
        block.appendChild(lines[i]);
      }

      blockCleaner('paging');

      if(nb_pages > 5)
        document.getElementById('paging').appendChild(list);

      document.getElementById('paging').appendChild(table);
      blockCleaner('first-id'); document.getElementById('first-id').appendChild(document.createTextNode(parseInt((current_page - 1) * per_page) + 1));

      blockCleaner('last-id'); 
      if(nb_results < (parseInt((current_page - 1) * per_page) + parseInt(per_page)))
        document.getElementById('last-id').appendChild(document.createTextNode(parseInt(nb_results)));
      else
        document.getElementById('last-id').appendChild(document.createTextNode(parseInt((current_page - 1) * per_page) + parseInt(per_page)));

      blockCleaner('total-results'); document.getElementById('total-results').appendChild(document.createTextNode(nb_results));
    }
  }
}

function updateEvent(element, page) {
  element.onclick = function() {
    changePage(page);
    return false;
  }

  return element;
}