// Global variables
// timerTag is a global timer set to clear the tag's message window
var timerTag;
// timerAjaxTag is a global timer set to keep track of how long it is
// taking for the Geogad server to respond. If it is longer than some
// prefered value, the program redirects the user input to another operation.
var timerAjaxTag;


function isAjaxSupportedTag() {
// Check if request can be created by is not used
   var request = null;
   if(typeof window.XMLHttpRequest != 'undefined')
   {
     request = new XMLHttpRequest();
   }
   else if(typeof window.ActiveXObject != 'undefined')
   {
     try { request = new ActiveXObject('Microsoft.XMLHTTP'); }
     catch(err) { request = null; }
   }
   if (request != null)
   	return(true); 
   else 
   	return(false); 
}

function tagSuccess(id) {
	tagClearTimers();
	var rt = document.getElementById(id);
	rt.innerHTML = '<div class="tagBox"><div style="text-align: center" ><span class="blueText">Thanks for tagging!</span></div></div>';
	timerTag = window.setTimeout('tagClearMessage("'+id+'")', 7000); 
}

function tagWaitMessage(id) {
	tagClearTimers();
	var rt = document.getElementById(id);
	rt.innerHTML = '<div class="tagBox"><div style="text-align: center" ><span class="blueText">Sending....</span></div></div>';
}

function tagProblemMessage(id) {
	tagClearTimers();
	var rt = document.getElementById(id);
	rt.innerHTML = '<div class="tagBox"><div style="text-align: center" ><span class="yellowBackground redfont">Problem sending tag. Please try again later.</span></div></div>';
	timerTag = window.setTimeout('tagClearMessage("'+id+'")', 7000); 
}

function tagPrivateMessage(id) {
	tagClearTimers();
	var rt = document.getElementById(id);
	rt.innerHTML = '<div class="tagBox"><div style="text-align: center" ><span class="yellowBackground redfont">Problem sending private tag. Please contact <a href="/userfeedback">Geogad support</a> for assistance.</span></div></div>';
	timerTag = window.setTimeout('tagClearMessage("'+id+'")', 15000); 
}

function tagInputMessage(id) {
	tagClearTimers();
	var rt = document.getElementById(id);
	rt.innerHTML = '<div class="tagBox"><div style="text-align: center" ><span class="yellowBackground redfont">Type in a tag.</span></div></div>';
	timerTag = window.setTimeout('tagClearMessage("'+id+'")', 7000); 
}


function tagClearMessage(id) {
	var rt = document.getElementById(id);
	rt.innerHTML = '';
}


function tagClearTimers() {
// free callback timer here
   window.clearTimeout(timerTag);
   window.clearTimeout(timerAjaxTag);
}

// Handler function for AJAX call
function print2DynamicMenuBox4Tag(data) {

// free callback timer here
   tagClearTimers();
// check for returned data
   if ((data == "") || (data == null)) {
   	tagProblemMessage("sendTagMessageBox");
   }  else {
	if (data.indexOf("Cannot create private tags") != -1) 
		tagPrivateMessage("sendTagMessageBox");
	else 
   		tagSuccess("sendTagMessageBox");
   }

}


function sendTag(nickname, tsID, box) { 

   // old browsers technique - seems to work fine
   var tagInput = document.getElementById("tag");
   var tag = tagInput.value;

   var checkPrivate = document.getElementById("makePrivate");
   var priv;
   if (checkPrivate.checked) 
	priv = new Boolean(true);
   else 
	priv = new Boolean(false);

   if (tag == null) {
	tagInputMessage(box);
	setfocus(tagInput);
	return false;
   }
   if (tag.length < 1)  {
	tagInputMessage(box);
	setfocus(tagInput);
	return false;
   }

   if (isAjaxSupportedTag()) {
	tagClearTimers();
   	JGetTagInfo.createTagJS(nickname, tsID, tag, priv, print2DynamicMenuBox4Tag);
	timerAjaxTag = window.setTimeout('tagProblemMessage("'+box+'")', 30000); 
	tagWaitMessage(box);
	return false;
   } else { 
	return true ; 
   } 

}