// Function to create the xml object for AJAX
function getHTTPObject() {
  var xmlhttp; 
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    xmlhttp = new XMLHttpRequest();
    if (xmlhttp.overrideMimeType) {
      xmlhttp.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!xmlhttp) {
    alert('Cannot create XMLHTTP instance');
    return false;
  }
  return xmlhttp; 
}

var http = getHTTPObject(); // We create the HTTP Object 

// Get Calendar table
function selects(month,year) {
  http = getHTTPObject();

  if (http) {

    http.open("GET", 'data/main.php?month='+month+'&year='+year, true);
    http.onreadystatechange = function () {
      if (http.readyState == 4) {

        if (http.responseText.indexOf('invalid') == -1) {
          var xmlDocument = http.responseText;
          document.getElementById("calDiv").innerHTML = xmlDocument;
          document.getElementById("monimg").src = "images/"+month+".gif";
          document.getElementById("yrimg").src = "images/"+year+".gif";
        } else {
          alert("Problem retrieving data");
        }
      }
    }
    http.send(null);
  }
}

function navigation(where) {

  month = document.getElementById('months').value;
  year = document.getElementById('years').value;

  if (where == 'next') {
    //alert(month);
    if ((parseInt(month)+1) > 12) {
      year = parseInt(year)+1;
      month = "01";
    } else {
      month = parseInt(month)+1;
    } 
  } else if (where == 'prev') {
    if ((month-1) < 1) {
      year = year-1;
      month = "12";
    } else {
      month = month-1;
    }
  }
  selects(month,year);
  document.getElementById('months').value = month;
  document.getElementById('years').value = year;
  //document.getElementById('imgmon').src = 
}

