// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function ajax_functions(task,various,id)
{
			if (task == 2) {
				var shortcode = document.getElementById("shortcodeVal").value;
				if (shortcode.length < 3) { document.getElementById("shortcode").className = 'error'; document.getElementById("shortcode").focus(); alert("Company reference must be three characters long!"); return false; } else { document.getElementById("shortcode").className = 'required'; }
				if (!checkAlphaNumeric(document.getElementById("shortcodeVal").value,0)) { document.getElementById("shortcode").className = 'error'; return false; } else { document.getElementById("shortcode").className = 'required'; }
			} if (task == 1) {
				var username = document.getElementById("usernameVal").value;
				if (username.length < 3) { document.getElementById("usernameVal").className = 'error'; document.getElementById("usernameVal").focus(); alert("Username must be at least three characters long!"); return false; } else { document.getElementById("username").className = 'required'; }				
				if (!checkAlphaNumeric(document.getElementById("usernameVal").value,1)) { document.getElementById("usernameVal").className = 'error'; return false; } else { document.getElementById("username").className = 'required'; }
			}
  
  // proceed only if the xmlHttp object isn't busy
	  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	  {
		// execute the *.php page from the server
		//alert("/ajax/ajax_functions.php?task="+task+"&various="+various+"&id="+id);
		xmlHttp.open("GET", "/ajax/ajax_functions.php?task="+task+"&various="+various+"&id="+id, true);  
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleServerResponse;
		// make the server request
		xmlHttp.send(null);
	  }
	  else
		// if the connection is busy, try again after one second  
		eval("setTimeout('ajax_functions(\""+task+"\",\""+various+"\",\""+id+"\")', 1000);");
}

// executed automatically when a message is received from the server
function handleServerResponse() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
	  xmlResponse.normalize(); 	  
      // obtain the document element (the root element) of the XML structure
      xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element

      var status     = xmlDocumentElement.childNodes[0].firstChild.nodeValue;
	  var task  	 = xmlDocumentElement.childNodes[1].firstChild.nodeValue;
	  var various    = xmlDocumentElement.childNodes[2].firstChild.nodeValue;
	  var id     	 = xmlDocumentElement.childNodes[3].firstChild.nodeValue;

	      // update the client display using the data received from the server
			if (task == 1) {
				if (status == "OK") { document.getElementById("username").className = 'required'; return true; } else { document.getElementById("username").className = 'error'; document.getElementById("username").focus(); alert(status); return false; }
			} else if (task == 2) {
				if (status == "OK") { document.getElementById("shortcode").className = 'required'; return true; } else { document.getElementById("shortcode").className = 'error'; document.getElementById("shortcode").focus(); alert(status); return false; }
			} else if (task == 3) {
				if (status == "OK") { document.getElementById("boardNumber").className = 'required'; return true; } else { document.getElementById("boardNumber").className = 'error'; document.getElementById("boardNumber").focus(); alert(status); return false; }
			} else if (task == 4) {
				if (status == "OK") 
				{ 
					var field = "boardStatus"+id;
					document.getElementById(field).innerHTML = "Removal Requested";
					var field = "dateRequested"+id;
					document.getElementById(field).innerHTML = thisDate();
					var field = "notified"+id;
				 	document.getElementById(field).innerHTML = "Not yet notified";
					var field = "removeBoard"+id;
					document.getElementById(field).innerHTML = "";
					var field = "boardTasks"+id;
					document.getElementById(field).style.display = 'none';
					var field = "boardinstructions"+id;
					document.getElementById(field).style.display = 'none';					
				} else { alert(status); return false; }
			} else if (task == 5) {
				if (status == "OK") 
				{ 
					var field = "boardStatus"+id;
					document.getElementById(field).innerHTML = "Slip Requested";
					var field = "dateRequested"+id;
					document.getElementById(field).innerHTML = thisDate();
					var field = "notified"+id;
				 	document.getElementById(field).innerHTML = "Not yet notified";
					var field = "boardTasks"+id;
					document.getElementById(field).style.display = 'none';				
					var field = "boardinstructions"+id;
					document.getElementById(field).style.display = 'none';										
				} else { alert(status); return false; }				
			} else if (task == 6)
			{
					if (status == "OK")
					{ 
						var field = "confirm"+id;
						document.getElementById(field).innerHTML = "";
					} else { alert(status); return false; }
			} else if (task == 7)
			{
					if (status == "OK")
					{
							var field = "boardDiv"+id;
							document.getElementById(field).style.display = "none";					
					} else { alert(status); return false; }
			} else if (task == 8) {
				if (status == "OK") 
				{ 
					var field = "boardStatus"+id;
					document.getElementById(field).innerHTML = "Slip Removal Requested";
					var field = "dateRequested"+id;
					document.getElementById(field).innerHTML = thisDate();
					var field = "notified"+id;
				 	document.getElementById(field).innerHTML = "Not yet notified";
					var field = "boardTasks"+id;
					document.getElementById(field).style.display = 'none';			
					var field = "boardinstructions"+id;
					document.getElementById(field).style.display = 'none';										
				} else { alert(status); return false; }
			}
    } else 
    {    // a HTTP status different than 200 signals an error
      alert("There was a problem completing this task:\n " + xmlHttp.statusText);
    }
  }
}

function thisDate()
{
	today=new Date(); // Initialize Date in raw form
	date=today.getDate(); // Get the numerical date
	year=today.getYear(); // Get the year
	month=today.getMonth()+1; // Get the month

	// Add suffix to date (1st, 2nd, 4th, etc.)
	if (date==1) suffix=("st");
	else if (date==2) suffix=("nd");
	else if (date==3) suffix=("rd");
	else if (date==21) suffix=("st");
	else if (date==22) suffix=("nd");
	else if (date==23) suffix=("rd");
	else if (date==31) suffix=("st");
	else suffix=("th");

	// Make month number correspond to month name
	if (month==1) monthName=("January");
	else if (month==2) monthName=("February");
	else if (month==3) monthName=("March");
	else if (month==4) monthName=("April");
	else if (month==5) monthName=("May");
	else if (month==6) monthName=("June");
	else if (month==7) monthName=("July");
	else if (month==8) monthName=("August");
	else if (month==9) monthName=("September");
	else if (month==10) monthName=("October");
	else if (month==11) monthName=("November");
	else monthName=("December");
	
	// Write date
	var thisDate = date + suffix + " " + monthName + " " + year;
	return thisDate;
}
