var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>? ";

function autoComplete(q, root, album)
{ 
  for (var i = 0; i < q.length; i++) {
  	if (iChars.indexOf(q.charAt(i)) != -1) {
  	 var qCorrect = document.getElementById('keywords').value;
     qCorrect = qCorrect.substring(0,qCorrect.length-1);
     document.getElementById('keywords').value = qCorrect;
     q = qCorrect;
  	}
  }
	var req = null; 
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		if (req.overrideMimeType) 
		{
			req.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject) 
	{
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)
		{
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
        	}

	req.onreadystatechange = function()
	{ 
		if(req.readyState == 4)
		{
			if(req.status == 200)
			{
				document.getElementById("dropdownBox").innerHTML  = req.responseText;
				if(req.responseText.search('Geen resultaten')!=-1)
				{
					window.setTimeout("fadeSearchbox();",750);
			    }
			    else
			   {
				document.getElementById("dropdownBox").style.visibility = 'visible';
			    }
			}	
			else	
		    {
			}	
		} 
	} 
	if(album)req.open("GET", root+"plugins/autocomplete.php?album="+album+"&q="+q, true); 
	else req.open("GET", root+"plugins/autocomplete.php?q="+q, true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(null); 
	 
}

function addTagsAutoComplete(q, root, album)
{ 
	for (var i = 0; i < q.length; i++) {
	  	if (iChars.indexOf(q.charAt(i)) != -1) {
	  	 var qCorrect = document.getElementById('addTagBox').value;
	     qCorrect = qCorrect.substring(0,qCorrect.length-1);
	     document.getElementById('addTagBox').value = qCorrect;
	     q = qCorrect;
	  	}
	  }
	var req = null; 
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		if (req.overrideMimeType) 
		{
			req.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject) 
	{
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)
		{
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
        	}

	req.onreadystatechange = function()
	{ 
		if(req.readyState == 4)
		{
			if(req.status == 200)
			{
				document.getElementById("addTagsDropdownBox").innerHTML  = req.responseText;
				if(req.responseText.search('Geen resultaten')!=-1)
				{
					window.setTimeout("fadeAddTagsSearchbox();",750);
			    }
			    else
			   {
				document.getElementById("addTagsDropdownBox").style.visibility = 'visible';
			    }
				
			}	
			else	
		    {
			}	
		} 
	} 
	if(album)req.open("GET", root+"plugins/addtagsautocomplete.php?album="+album+"&q="+q, true); 
	else req.open("GET", root+"plugins/addtagsautocomplete.php?q="+q, true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(null); 
	 
}

function hideAddTagsDropdownBox()
{
	document.getElementById('addTagsDropdownBox').style.visibility='hidden';
}

function hideDropdownBox()
{
	document.getElementById('dropdownBox').style.visibility='hidden';
}

function addTagToTagstring()
{
	var currentTags = document.getElementById('addedTags').innerHTML;
	var tagToAdd = document.getElementById('addTagBox').value;
	
	if(tagToAdd.length<4)
	{
		alert('een zoekterm moet minimaal 4 karakters lang zijn!');
	}
	
	else
	{
	var HTML = "<a href=\"#\" onclick=\"deleteTag('"+tagToAdd+"');\">"+tagToAdd+"</a>, ";
	document.getElementById('addTagBox').value = '';
	var tagPostString = document.getElementById('tagStringPostBox').value;
	
	if (currentTags == 'geen')
	{
		tagPostString = tagToAdd+",";
		document.getElementById('addedTags').innerHTML = HTML;
	}
	else
	{
		tagPostString += tagToAdd+",";
		document.getElementById('addedTags').innerHTML = currentTags + HTML;
	}
	
	document.getElementById('tagStringPostBox').value = tagPostString;
    }
}

function deleteTag(tag)
{
	
  var HTML = document.getElementById('addedTags').innerHTML;
  var temp = new Array();

  var tagPostString = document.getElementById('tagStringPostBox').value;
  
  // red rid of last komma
  HTML = HTML.slice(0,HTML.length-1);
  tagPostString = tagPostString.slice(0,tagPostString.length-1);

  // split HTML values
  temp = HTML.split(',');
  tempOnlyTags = tagPostString.split(',');

  var newTagString = '';
  var newTagPostString = ''; 
  
  for(i=0;i<temp.length;i++)
  {
	if((!temp[i].match("'"+tag+"'"))&&(temp[i]!=''))
	{
	  newTagString += temp[i]+", ";
	  newTagPostString += tempOnlyTags[i]+",";
	}
  }
  if(newTagString.length<3)
  {
    newTagString = 'geen';
    newTagPostString = '';
  }
  // remove white space from start of string if nessesary
//  if(newTagPostString.substring(0,1)==' '){newTagPostString = newTagPostString.substring(1,newTagPostString.length);}

  document.getElementById('addedTags').innerHTML = newTagString;
  document.getElementById('tagStringPostBox').value = newTagPostString;
}

function fadeSearchbox()
{
	document.getElementById('dropdownBox').style.visibility = 'hidden';
}

function fadeAddTagsSearchbox()
{
	document.getElementById('addTagsDropdownBox').style.visibility = 'hidden';
}

