// JavaScript Document
//function to make our ajax call
function sendTheNote(noteID) 
{
	var url = "./ListAdmin.php?id="; // url to the page we want to call
	document.getElementById('numEmailsSent').innerHTML = "emails are sending please wait";
	var GUID = prompt("Enter the correct GUID to continue");
	showdiv();
	try
	 {
	 	//this if makes sure we are not already working (busy)
	 	if (!isWorking && http) 
	 	{
			http.open("GET", url + escape(noteID) + "&guid=" + escape(GUID), true); 
		 	http.onreadystatechange = handleHttpResponse; //function to call on our state change
			isWorking = true; //we now are working(busy)
		 	http.send(null);
		}	
	 }
	 catch (e)
	 {
		alert (e);	 
	 }
}
// The server-side script 
function handleHttpResponse() 
{ 
	if (http.readyState == 4) // 4 signals we are ready
	{
		try
		{
			 if (http.responseText.indexOf('invalid') == -1) //making sure we aren't working(busy)
			 {
				 // Split the ||| delimited response into an array  
				 results = http.responseText.split("|||");
				 //if the returnedstring is empty then don't change anything
				 if (results[1] != "")
				 { // else we need to show comments and set our toggle link to hide comments
					//document.getElementById('comment' + results[1]).innerHTML = results[0]; 
					//document.getElementById('numEmailsSent').innerText = results[1];
					document.getElementById('numEmailsSent').childNodes[0].nodeValue = results[1] + " emails were sent";
					hidediv();
				 }
				 
				 isWorking = false; // we are done working now (not busy)
			 }
		 }
		 catch (e)
		 {
			alert(e);
			
		 }
	 }
}

var isWorking = false;

function getHTTPObject() {
	  http_request = false;

		// all this was borrowed from somewhere on the intraweb (internet)
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
	return http_request;
} 
var http = getHTTPObject(); // We create the HTTP Object 


///////// show/hide emailing image
function hidediv() {  
	if (document.getElementById) { // DOM3 = IE5, NS6  
	document.getElementById('emailImage').style.visibility = 'hidden';  
	}  
}  
 
function showdiv() {  
	if (document.getElementById) { // DOM3 = IE5, NS6  
	document.getElementById('emailImage').style.visibility = 'visible';  
	}  
}  

/////////////////////////////////////////////////////////////////////////////////
////////////////////////// email a note to a friend /////////////////////////////
function emailANote(noteID, poststr) 
{
	
	// url to the page we want to call	
	var url = "./php/email2friend.php?id=" + noteID + "&" + poststr; 
	document.getElementById('bttn').disabled = true;
	document.getElementById('bttn').value = "Sending E-mail...";
	try
	 {
	 	//this if makes sure we are not already working (busy)
	 	if (!isWorking && http) 
	 	{
			http.open("GET", url, true); 
		 	http.onreadystatechange = handleSendingANote; //function to call on our state change
			isWorking = true; //we now are working(busy)
		 	http.send(null);
		}	
	 }
	 catch (e)
	 {
		alert (e);	 
	 }
}
// The server-side script 
function handleSendingANote() 
{ 
	if (http.readyState == 4) // 4 signals we are ready
	{
		try
		{
			 if (http.responseText.indexOf('invalid') == -1) //making sure we aren't working(busy)
			 {
				 // Split the ||| delimited response into an array  
				 results = http.responseText.split("|||");
				 //if the returnedstring is empty then don't change anything
				 if (results[0] != "")
				 { // else we need to show comments and set our toggle link to hide comments
					
					document.getElementById('bttn').value = results[0];
				 }
				 
				 isWorking = false; // we are done working now (not busy)
			 }
		 }
		 catch (e)
		 {
			alert(e);
			
		 }
	 }
}
