var xmlhttp=false;

if (window.ActiveXObject)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
  try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) {
    xmlhttp = false;
   }
  }

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}

function loadFragmentInToElement(fragment_url, element_id) {
    var element = document.getElementById(element_id);
    element.innerHTML = 'Loading ...';
    xmlhttp.open("GET", fragment_url);
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      element.innerHTML = xmlhttp.responseText;
      }
    }
    xmlhttp.send(null);
}

function updateMinistry(directive, min_id, min_code, theform, leader_id, division_id, div_leader_id)
{
    //alert(directive);
    var urlstr = 'directive='+directive+'&ministry_id='+min_id+'&ministry_code='+min_code;
    if(leader_id) urlstr += '&leader_id='+leader_id;
    if(division_id) urlstr += '&division_id='+division_id;
    if(div_leader_id) urlstr += '&div_leader_id='+div_leader_id;
    if(theform!=undefined)
    {
        for(x=0;x<theform.elements.length;x++)
        {
            if(theform.elements[x].type == 'text' || (theform.elements[x].type == 'checkbox' && theform.elements[x].checked))
            {
                urlstr += '&'+theform.elements[x].name+'='+encodeURIComponent(theform.elements[x].value);
            }
        }
    }
    //alert(encodeURI(urlstr));
    xmlhttp.open("POST", '/'+site_root_path+'xmlupdateministry.php', true);
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            var result = xmlhttp.responseText;
            //alert(urlstr + ': ' + result);
            location.href='/'+site_root_path+'ministry/'+min_code+'/';
        }
    }
    xmlhttp.send(urlstr);
}

function delEvent(event_id, returnDate)
{
  var urlstr = 'event_id='+event_id;
  xmlhttp.open("POST", "/"+site_root_path+"calendar/events_calendar_delete.php", true);
  xmlhttp.onreadystatechange = function()
  {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
      var result = xmlhttp.responseText;
      if(result == 'success')
      {
        history.go();
      }
    }
  }
  xmlhttp.send(urlstr);
}

function clearCorrection(correction_id)
{
  var urlstr = 'correction_id='+correction_id;
  xmlhttp.open("POST", "/"+site_root_path+"calendar/xmlcorrectiondelete.php", true);
  xmlhttp.onreadystatechange = function()
  {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
      var result = xmlhttp.responseText;
      if(result == 'success')
      {
        document.getElementById('correction'+correction_id).style.display = 'none';
      }
    }
  }
  xmlhttp.send(urlstr);
}

function clearStat(weekly_id)
{
  var urlstr = 'weekly_id='+weekly_id;
  xmlhttp.open("POST", "/"+site_root_path+"admin/xmlstatdelete.php", true);
  xmlhttp.onreadystatechange = function()
  {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
      var result = xmlhttp.responseText;
      if(result == 'success')
      {
        document.getElementById('weekly'+weekly_id).style.display = 'none';
      }
      else
      {
        alert(result);
      }
    }
  }
  xmlhttp.send(urlstr);
}