var edimestre_rss_inclu = '';
var hauteur_iframe='480px';
var xmldoc = false;
var rss_busy = false;
var state = 'direct';
var destination = 'edimestre_rss_externe';

function inclusion_rss()
{
  if (xmldoc.readyState == 4)
  {
    if (xmldoc.status == 200) {
      rss_busy = false;
      document.getElementById(destination).innerHTML = xmldoc.responseText;
    } else {
      generer_iframe();
    }

  }
}

function generer_iframe()
{
   if (document.getElementById(destination).title.match(/fheight=/))
   {
      edimestre_rss_inclu=document.getElementById(destination).title;
   }
   else if (document.getElementById(destination + '_url')) {
      if (document.getElementById(destination + '_url').value.match(/fheight=/)) {
        edimestre_rss_inclu=document.getElementById(destination + '_url').value;
      }
   }
   edimestre_rss_inclu=edimestre_rss_inclu.replace(/&amp;/g,"&");
   document.getElementById('edimestre_rss_externe').innerHTML = '<iframe id=\"edimestre_rss_iframe\" name=\"RSS Edimestre\" style=\"border: hidden; width: 100%; overflow: visible; height: '+ hauteur_iframe + ';\" src=\"' + edimestre_rss_inclu + '&entete=1' + '\" ></iframe>'
}

function get_direct()
{
   xmldoc.onreadystatechange = inclusion_rss;
   xmldoc.open("GET", edimestre_rss_inclu, true);
   xmldoc.send(null);
   document.getElementById(destination).title="";
}

function get_proxy()
{
   xmldoc.onreadystatechange = inclusion_rss;
   xmldoc.open("GET", "/modules/rss/proxy.php?url=" + escape(edimestre_rss_inclu), true);
   xmldoc.send(null);
  document.getElementById(destination).title="";
}

function update_content()
{
   if (document.getElementById(destination).title)
   {
      edimestre_rss_inclu=document.getElementById(destination).title;
   }
   else if (document.getElementById(destination + '_url') != null)
   {
      if ( document.getElementById(destination + '_url').value )
      {
         edimestre_rss_inclu=document.getElementById(destination + '_url').value;
      }
   }
   edimestre_rss_inclu=edimestre_rss_inclu.replace(/&amp;/g,"&");
   var url=window.location.href;
   var isIE = false;

   var url_domaine = '';
   var rss_domaine = '';
   var url_crypt = '';
   var rss_crypt = '';
   if (url.match(/^https*:\/\/([^/]+)\/.*/) != null)
   {
      url_domaine = url.match(/^https*:\/\/([^/]+)\/.*/)[1];
   }
   if (edimestre_rss_inclu.match(/^https*:\/\/([^/]+)\/.*/) != null)
   {
      rss_domaine = edimestre_rss_inclu.match(/^https*:\/\/([^/]+)\/.*/)[1];
   }
   if (url.match(/^(https*):\/\/[^/]+\/.*/) != null)
   {
      url_crypt = url.match(/^(https*):\/\/[^/]+\/.*/)[1];
   }
   if (edimestre_rss_inclu.match(/^(https*):\/\/[^/]+\/.*/) != null)
   {
      rss_crypt = edimestre_rss_inclu.match(/^(https*):\/\/[^/]+\/.*/)[1];
   }

   if (xmldoc)
   {
      // Le crypt ne match pas
      if (url_crypt != rss_crypt) { if (url_crypt == "https") { edimestre_rss_inclu = edimestre_rss_inclu.replace(/^http:\/\//,"https://"); } }

      // On procede avec le fetch determine
      try { get_direct(); }
      catch (e) { try { get_proxy(); }
      catch (e) { generer_iframe(); } }
   }
}

function run_rss(rss_id) {
  if (rss_busy == true) {
    setTimeout('run_rss("' + rss_id + '")', 1000);
    return;
  }
  rss_busy = true;
  edimestre_rss_inclu = '';
  hauteur_iframe='480px';
  xmldoc = false;
  destination = rss_id;

  state = 'direct';
  if (document.getElementById(destination).title.match(/fheight=/))
  {
    hauteur_iframe=document.getElementById(destination).title.replace(/.*fheight=/,"");
  }
  if (!(document.getElementById(destination).title.match(/fheight=/)) && document.getElementById(destination + '_url') != null)
  {
    if ( document.getElementById(destination + '_url').value.match(/fheight=/) )
    {
        hauteur_iframe=document.getElementById(destination + '_url').value.replace(/.*fheight=/,"");
    }
  }
  hauteur_iframe=hauteur_iframe.replace(/$|&.*/,"");

  try { xmldoc = new XMLHttpRequest(); }
  catch (e)
  {
      try { xmldoc  = new ActiveXObject("Msxml2.XMLHTTP"); isIE = true; }
      catch (e)
      {
        try { xmldoc  = new ActiveXObject("Microsoft.XMLHTTP"); isIE = true; }
        catch (e)
        {
            try { xmldoc  = window.createRequest(); }
            catch (e) { xmldoc  = false; }
        }
      }
  }
  update_content();
}
run_rss('edimestre_rss_externe');

