var xmlhttp = null;
var KEY_ESCAPE = 27;
var KEY_ENTER = 13;
function AddKeywordForm(element)
{
	this.IMG_LOADING = new Image();
	this.IMG_LOADING.src = '/assets/images/themePopup/loading.gif';
	
	this.ANCHOR = element;
	this.ANCHOR.innerHTML+= '&nbsp;';
	this.ANCHOR.parentNode.insertBefore(this.ANCHOR,element.nextSibling);
	//this.ANCHOR.firstChild.style.display = 'none';
	
	//Append an input box
	this.INPUT = document.createElement('INPUT');
	this.ANCHOR.appendChild(this.INPUT);
	//Now add a button
	this.BUTTON = document.createElement('INPUT');
	this.BUTTON.type = 'button';
	this.BUTTON.value = 'Submit';
	this.BUTTON.ADD_KEYWORD_FORM = this;
	this.BUTTON.onclick = function()
	{
		this.ADD_KEYWORD_FORM.sendTag();
	}
	this.ANCHOR.appendChild(this.BUTTON);
	
	
	var frm = this;
	var keyCapt = function(e)
	{
		if(typeof window.event!="undefined")
		{
			e=window.event;//code for IE
		}
		if(e.type=="keypress")
		{
			switch(e.keyCode)
			{
				case KEY_ESCAPE:
				frm.ANCHOR.removeChild(frm.INPUT);
				frm.ANCHOR.removeChild(frm.BUTTON);
				element.style.display = '';
				return false;
				break;
				
				case KEY_ENTER:
				frm.sendTag();
				return false;
				break;				
			}
		}
		return true;
	}
	if(document.addEventListener)
	{
		document.addEventListener("keypress",keyCapt,false);
	}
	else
	{
		document.attachEvent("onkeypress",keyCapt);
	}
}
AddKeywordForm.prototype.sendTag = function()
{
	xmlhttp=null;
	var url = '/assets/popups/addTag.php?tag='+this.INPUT.value+'&identifier='+IDENTIFER;
	
	
	//Append the tag and the loading image to the list
	var li = document.createElement('LI');
	li.innerHTML = this.INPUT.value;
	li.appendChild(this.IMG_LOADING);
	this.ANCHOR.parentNode.insertBefore(li,this.ANCHOR.nextSibling);
	//Remove th input and button
	this.ANCHOR.removeChild(this.INPUT);
	this.ANCHOR.removeChild(this.BUTTON);
	

	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	
	if (xmlhttp!=null)
	{
		var frm = this;
		xmlhttp.onreadystatechange=function(){
			try{
				if (xmlhttp.readyState==4)
				{
					//alert(xmlhttp.responseText);
					// if "OK"
					if (xmlhttp.status==200)
					{
						if(!xmlhttp.responseText)
						{
							li.parentNode.removeChild(li);
						}
					}
					frm.IMG_LOADING.style.display = 'none';	
				}
			}catch(e){
				alert(e);
			}
		}
		xmlhttp.open("GET",url,true)
		xmlhttp.send(null)
	}
}