var formContainerDiv = document.getElementById("formContainer");
var statusAnchor = document.getElementById("statusAnchor");

var textArea = document.getElementById("route_description");
var nameBox = document.getElementById("route_name");
var startBox = document.getElementById("route_start");
var endBox = document.getElementById("route_end");

var submitButton = document.getElementById("sbmt");

var mountainId;
var xmlRequest;
var t;
var cnt;

function init(mid)
{
  cnt = 0;
  mountainId = mid;

  if (submitButton.addEventListener)
  {
    submitButton.addEventListener("click", sendRoute, false);
  }
  else if (submitButton.attachEvent)
  {
    submitButton.attachEvent("onclick", sendRoute);
  }

  showForm();
  hideStatus();
}

function sendRoute()
{
  var params = "mid=" + mountainId + "&route_name=" + nameBox.value + "&start=" + startBox.value + "&end=" + endBox.value + "&contribution=" + textArea.value;

  if (window.XMLHttpRequest)
  {
    xmlRequest = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
  }

  xmlRequest.open("POST", "contribute.process.php", true);

  xmlRequest.onreadystatechange = function() 
  { 
    if (xmlRequest.readyState == 4) 
    {
      if (xmlRequest.status == 200)
      {
        processResponse(); 
      }
	else 
	{
	  setStatusText("Some error occurred.  Try again later.");
	  t = setTimeout("reset()", 1500);
	}
    }
  }

  xmlRequest.overrideMimeType("text/xml");
  xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlRequest.setRequestHeader("Content-Length", params.length);
  xmlRequest.setRequestHeader("Connection", "close");
  xmlRequest.send(params);
}

function processResponse()
{
  hideForm();
  showStatus();

  var rootNode = xmlRequest.responseXML.getElementsByTagName("NJ1KResponse")[0];

  if (rootNode.getAttribute("command") == "contributeRoute")
  {
    if (rootNode.getElementsByTagName("status")[0].textContent == "OK")
    {
	setStatusText(" ");
      t = setTimeout("timer()", 500);
    }
    else
    {
	setStatusText("Some error occurred.  Try again later.");
    }
  }
  else
  {
    setStatusText("Invalid command code.");
  }
}

function timer()
{
  if (cnt < 2)
  {
    cnt++;
    setStatusText(statusAnchor.innerHTML + " .");
    t = setTimeout("timer()", 500);
  }
  else
  {
    setStatusText("Summit route submitted successfully.  A moderator will approve it shortly!");
    t = setTimeout("reset()", 1500);
  }
}

function changeColor()
{
  statusAnchor.style.backgroundColor = "#f8eed8";
}

function orig()
{
  statusAnchor.style.backgroundColor = "";
}

function reset()
{
  hideForm();
  setStatusText("Click here to submit a summit route description.");
  showStatus();
  
  nameBox.value = "";
  startBox.value = "";
  endBox.value = "";
  textArea.value = "";
}

function showForm()
{
  formContainerDiv.style.position = "static";
}

function hideForm()
{
  formContainerDiv.style.position = "absolute";
  formContainerDiv.style.left = "-1000px";
}

function showStatus()
{
  statusAnchor.style.position = "static";
}

function hideStatus()
{
  statusAnchor.style.position = "absolute";
  statusAnchor.style.left = "-1000px";
}

function setStatusText(txt)
{
  statusAnchor.innerHTML = txt;
}
