function getXhttp (){
    var ajax_request;
    if (  window.ActiveXObject  )  {
        var mSoftVersions = [
            'MSXML2.DOMDocument.5.0',
            'MSXML2.DOMDocument.4.0',
            'MSXML2.DOMDocument.3.0',
            'MSXML2.DOMDocument.2.0',
            'MSXML2.DOMDocument',
            'Microsoft.XmlDom',
            'Msxml2.XMLHTTP',
            'Microsoft.XMLHTTP'
        ];

        for (  i=0; i<mSoftVersions.length; i++  )  {
            try {
                ajax_request = new ActiveXObject (  mSoftVersions[i]  );
            }  catch (  e  )  {    }
        }
    }  else if (  !ajax_request && typeof XMLHttpRequest != 'undefined'  )  {
        try {
            ajax_request = new XMLHttpRequest;
        }  catch (  e  )  {    }
    }  else if (  !ajax_request && window.createRequest  )  {
        try {
            ajax_request = window.createRequest;
        }  catch (  e  )  {    }
    }  else  {
        ajax_request = false;
    }
    return ajax_request;
}

function clear_grocery_list(user_name){
  var xml = getXhttp();
  if (!xml){
    return false;
  }
  xml.open('POST', '/js/clear_grocery_list.php');
  xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xml.send('list_user_name=' + user_name);
  location.replace('/groceries');
}

function calendar_completed(calendar_id){
  var xml = getXhttp();
  if (!xml){
    return false;
  }
  xml.open('POST', '/js/calendar_completed.php');
  xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xml.send('calendar_id=' + calendar_id);
  if(document.getElementById('checkbox_calendar_' + calendar_id).checked == true){
    document.getElementById('calendar_' + calendar_id + '_h2').style.color = 'green';
  }
  else {
    document.getElementById('calendar_' + calendar_id + '_h2').style.color = 'red';
  }
}

function transaction_status(id){
  var xml = getXhttp();
  if (!xml){
    return false;
  }
  xml.open('POST', '/js/transaction_status.php');
  xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xml.send('transaction_id=' + id + '&transaction_submit=' + id);
  var checkbox = 'document.transactions.' + checkbox + '.checked';
  if(document.getElementById('checkbox_transaction_' + id).checked == true){
    document.getElementById('transaction_' + id).style.background = 'none';
  }
  else {
    document.getElementById('transaction_' + id).style.background = 'darkgrey';
  }
}
