function getXMLHttp() {
  var xmlHttp;
  var browser = navigator.appName;
  if(window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return xmlHttp;
}

function MakeRequest() {
  var xmlHttp = getXMLHttp();
  var grab = document.getElementById('grab_it').value;
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      HandleResponse(xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET", "ajax.php?data="+grab, true); 
  xmlHttp.send(null);
}

function HandleResponse(response) {
  document.getElementById('ResponseDiv').innerHTML = response;
}

function time_update() {
  // form_billable_time
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      var response = xmlHttp.responseText;
      if((!response) || (response == "")) {
        document.getElementById("time_holder").innerHTML = "";
        alert("Unable to update current time. Please refresh this page.");
        return;
      }
      document.getElementById("time_holder").innerHTML = response;
      setTimeout("time_update()", 1000); // 15000
    }
  }
  xmlHttp.open("GET", ".ajax/time.php", true);
  xmlHttp.send(null);
}

function keep_session_alive() {
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      var session = xmlHttp.responseText;
      if(session == 1) {
        //window.console.log("it is working...");
        setTimeout("keep_session_alive()", 60000); // 60 sec
      }
      else {
        alert("Your session has ended.");
        window.location.href = "https://www.cniteam.com/login.php";
      }
    }
  }
  xmlHttp.open("GET", ".ajax/keep_alive.php", true);
  xmlHttp.send(null);
}

function td_test(td_id) {
  document.getElementById(td_id).innerHTML = "junk";
}

function populate_co_profile_elements() {
  var this_gid = document.getElementById("gid").value;
  //alert(this_gid);
  for(i=0; i<co_profile_element_array.length; i++) {
    get_co_profile_form_element(this_gid,co_profile_element_array[i]);
  }
}

function get_co_profile_form_element(gid,element_id) {
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      var response = xmlHttp.responseText;
      document.getElementById(element_id+"_holder").innerHTML = response;
    }
  }
  xmlHttp.open("GET", ".ajax.co_profile_form_element.php?gid="+gid+"&element_id="+element_id, true);
  xmlHttp.send(null);
}

function populate_sc_elements() {
  var this_sc_id = document.getElementById("sc_id").value;
  for(i=0; i<sc_element_array.length; i++) {
    get_sc_form_element(this_sc_id,sc_element_array[i]);
  }
}

function get_sc_form_element(sc_id,element_id) {
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      var response = xmlHttp.responseText;
      document.getElementById(element_id+"_holder").innerHTML = response;
    }
  }
  xmlHttp.open("GET", ".ajax.service_class.php?sc_id="+sc_id+"&sc_element="+element_id, true);
  xmlHttp.send(null);
}

function refresh_dashboard() {
  get_dashboard_monitoring();
  get_dashboard_ons();
  get_dashboard_time_tracker();
  get_dashboard_channel_tracker();
  setTimeout("refresh_dashboard()", 15000);
}

function get_dashboard_monitoring() {
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      var response = xmlHttp.responseText;
      //alert(response);
      document.getElementById("dashboard_monitoring").innerHTML = response;
    }
  }
  xmlHttp.open("GET", "monitoring/.ajax/build_module.php?action=hide_all&dashboard=1&gid="+my_gid, true);
  xmlHttp.send(null);
}

function get_dashboard_ons() {
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      var response = xmlHttp.responseText;
      //alert(response);
      document.getElementById("dashboard_ons").innerHTML = response;
    }
  }
  xmlHttp.open("GET", "tt/dashboard_list.php?gid="+my_gid, true);
  xmlHttp.send(null);
}

function get_dashboard_time_tracker() {
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      var response = xmlHttp.responseText;
      //alert(response);
      document.getElementById("dashboard_time_tracker").innerHTML = response;
    }
  }
  xmlHttp.open("GET", "time_tracker/.ajax/company_list.php?tf=Active&dashboard=1&gid="+my_gid, true);
  xmlHttp.send(null);
}

function get_dashboard_channel_tracker() {
  var xmlHttp = getXMLHttp();
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      var response = xmlHttp.responseText;
      //alert(response);
      document.getElementById("dashboard_channel_tracker").innerHTML = response;
    }
  }
  xmlHttp.open("GET", ".ajax/channel_tracker.php", true);
  xmlHttp.send(null);
}
