var xmlHttp

function listPostingSeries(o) {
  xmlHttp = GetXmlHttpObject()
  if (xmlHttp == null) {
    alert ("Browser does not support HTTP Request")
    return
  }

  var url = "fillPostingSeries.php?validyear=" + o.value

  document.getElementById('PostingSeries').options.length = 1
  document.getElementById("txtPostings").innerHTML = " "

  xmlHttp.onreadystatechange = PostingstateChanged                   // EvalstateChanged
  xmlHttp.open("GET", url, true)
  xmlHttp.send(null)
}

function listseries(o) {
  xmlHttp = GetXmlHttpObject()
  if (xmlHttp == null) {
    alert ("Browser does not support HTTP Request")
    return
  }

  var url = "fillseries.php?validyear=" + o.value

  document.getElementById('series').options.length = 1
  document.getElementById('series').disabled = false
  document.getElementById('events').options.length = 1
  document.getElementById('events').disabled = true
  document.getElementById("txtResults").innerHTML = " "

  xmlHttp.onreadystatechange = CstateChanged                   // EvalstateChanged
  xmlHttp.open("GET", url, true)
  xmlHttp.send(null)
}

function listevents(s) {
  xmlHttp = GetXmlHttpObject()
  if (xmlHttp == null) {
    alert ("Browser does not support HTTP Request")
    return
  }

  var url = "fillevents.php?series=" + s

  var vYear = document.getElementById('vYear').value
  url = url + "&validyear=" + vYear

  document.getElementById('events').options.length = 1         // Emtpy the select box
  document.getElementById('events').disabled = false
  document.getElementById("txtResults").innerHTML = " "

  xmlHttp.onreadystatechange = CstateChanged                   // EvalstateChanged
  xmlHttp.open("GET", url, true)
  xmlHttp.send(null)
}

function listPostings(o) {
  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request")
    return
  }

  var url = "listpostings.php?seriesid=" + o.value
  url = url + "&validyear=" + document.getElementById('PostingYear').value

  xmlHttp.onreadystatechange = PstateChanged
  xmlHttp.open("GET",url,true)
  xmlHttp.send(null)
}

function listpayouts(e) {
  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request")
    return
  }

  var url = "listpayouts.php?eventid=" + e

  xmlHttp.onreadystatechange = stateChanged
  xmlHttp.open("GET",url,true)
  xmlHttp.send(null)
}

function ShowPoints() {

//  alert("We made it to ShowPoints " + d + ':' + c)
  d = document.getElementById('divisions').value;
  c = document.getElementById('classes').value;

  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request")
    return
  } 

  var url = "showpoints.php?"
  var divid = trimAll(d) 
  var classcode = trimAll(c)
  if (divid.length > 0) {
    url = url + "divid=" + divid
  }
  if ((divid.length > 0) && (classcode.length > 0)) {
    url = url + "&"
  }
  if (classcode.length > 0) {
    url = url + "classcode=" + classcode
  }

  xmlHttp.onreadystatechange = PointsstateChanged
  xmlHttp.open("GET",url,true)
  xmlHttp.send(null)
} 

function addOptions(type) {
  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request")
    return
  }

  var url = "fillselect.php?t=" + type

  switch(type) {
    case 'classes':
      document.getElementById('classes').options.length = 1;         // Emtpy the select box
      xmlHttp.onreadystatechange = PointsstateChanged;
      break;
    case 'divisions':
      document.getElementById('divisions').options.length = 1;         // Emtpy the select box
      xmlHttp.onreadystatechange = PointsstateChanged;
      url = url + '&classcode=' + document.getElementById('classes').value;
      break;
  }
  document.getElementById("txtPoints").innerHTML = " "

  xmlHttp.open("GET", url, true)
  xmlHttp.send(null)
}

function CstateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
    if (xmlHttp.status == 200) {
      document.getElementById("txtResults").innerHTML = eval(xmlHttp.responseText)
    } else {
      document.getElementById("txtResults").innerHTML = 'There was a problem with the request.(Code: ' + xmlHttp.status + ')'
    }
  }
}

function PointsstateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
    if (xmlHttp.status == 200) {
      document.getElementById("txtPoints").innerHTML = eval(xmlHttp.responseText)
    } else {
      document.getElementById("txtPoints").innerHTML = 'There was a problem with the request.(Code: ' + xmlHttp.status + ')'
    }
  }
}

function PostingstateChanged() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
    if (xmlHttp.status == 200) {
      document.getElementById("txtPostings").innerHTML = eval(xmlHttp.responseText)
    } else {
      document.getElementById("txtPostings").innerHTML = 'There was a problem with the request.(Code: ' + xmlHttp.status + ')'
    }
  }
}

function DstateChanged() {
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
    document.getElementById("txtResults").innerHTML = xmlHttp.responseText
  }
}

function leftTrim(sString) {
  while (sString.substring(0,1) == ' ') {
    sString = sString.substring(1, sString.length);
  }
  return sString;
}

function rightTrim(sString) {
  while (sString.substring(sString.length-1, sString.length) == ' ') {
    sString = sString.substring(0,sString.length-1);
  }
  return sString;
}

function trimAll(sString) {
  while (sString.substring(0,1) == ' ') {
    sString = sString.substring(1, sString.length);
  }
  while (sString.substring(sString.length-1, sString.length) == ' ') {
    sString = sString.substring(0,sString.length-1);
  }
  return sString;
}

function showLoad () {
  $('load').style.display = 'block';
}

function stateChanged() { 
//  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
//    document.getElementById("txtResults").innerHTML=xmlHttp.responseText 
//  }

  if (xmlHttp.readyState == 0) {
    document.getElementById("txtResults").innerHTML = "<center><p> Search in progress...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\"></center>"; //loading
  }
  else if(xmlHttp.readyState == 1) {
    document.getElementById("txtResults").innerHTML = "<center><p> Search in progress...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\"></center>"; //loaded
  }
  else if(xmlHttp.readyState == 2) {
    document.getElementById("txtResults").innerHTML = "<center><p> Search in progress...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\"></center>"; //interactive
  }
  else if(xmlHttp.readyState == 3) {
    document.getElementById("txtResults").innerHTML = "<center><p> Loading data...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\"></center>";
  }
  else if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") {
    document.getElementById("txtResults").innerHTML=xmlHttp.responseText
  } 
} 

function EvalstateChanged() { 
//  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
//    document.getElementById("txtResults").innerHTML=xmlHttp.responseText 
//  }

  if (xmlHttp.readyState == 0) {
    document.getElementById("txtResults").innerHTML = "<p> Search in progress...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\">"; //loading
  }
  else if(xmlHttp.readyState == 1) {
    document.getElementById("txtResults").innerHTML = "<p> Search in progress...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\">"; //loaded
  }
  else if(xmlHttp.readyState == 2) {
    document.getElementById("txtResults").innerHTML = "<p> Search in progress...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\">"; //interactive
  }
  else if(xmlHttp.readyState == 3) {
    document.getElementById("txtResults").innerHTML = "<p> Loading data...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\">";
  }
  else if(xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") {
    document.getElementById("txtResults").innerHTML=eval(xmlHttp.responseText)
  } 
} 

function PstateChanged() { 
//  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
//    document.getElementById("txtResults").innerHTML=xmlHttp.responseText 
//  }

  if (xmlHttp.readyState == 0) {
    document.getElementById("txtPostings").innerHTML = "<center><p> Search in progress...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\"></center>"; //loading
  }
  else if(xmlHttp.readyState == 1) {
    document.getElementById("txtPostings").innerHTML = "<center><p> Search in progress...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\"></center>"; //loaded
  }
  else if(xmlHttp.readyState == 2) {
    document.getElementById("txtPostings").innerHTML = "<center><p> Search in progress...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\"></center>"; //interactive
  }
  else if(xmlHttp.readyState == 3) {
    document.getElementById("txtPostings").innerHTML = "<center><p> Loading data...</p><img src=\"/images/circle-ball-dark-antialiased.gif\" border=\"0\"></center>";
  }
  else if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") {
    document.getElementById("txtPostings").innerHTML=xmlHttp.responseText
  } 
} 

function GetXmlHttpObject() { 
  var objXMLHttp=null
  
  try {
    objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //later IE
  } catch (e) {
  try {
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //earlier IE
  } catch (e) {
  objXMLHttp = null;
  }
  }
  
  if (objXMLHttp==null)
  {
    objXMLHttp=new XMLHttpRequest() //IE7, Firefox, Safari
  }
  return objXMLHttp

}
