// JavaScript Document

/* ----------- FUNÇÕES DO AUTOCOMPLETAR ---------- */

function createForm(e)
{
	theObject = document.getElementById("createTask");
	theObject.style.visibility = "visible";
	theObject.style.height = "200px";
	theObject.style.width = "200px";
	
	var posx = 0;
	var posy = 0;
	
	posx = e.clientX  + document.body.scrollLeft;
	posy = e.clientY + document.body.scrollTop;
	theObject.style.left = posx + "px";
	theObject.style.top = posy + "px";
	
	var objID = "createTask";
	var serverPage = "curso_home.php";
	var obj = document.getElementById(objID);
	xmlhttp.open("GET",serverPage);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readystate == 4 && xmlhttp.status == 200)
		{
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);	
}

function closeTask()
{
	theObject = document.getElementById("createTask");
	theObject.style.visibility = "hidden";
	theObject.style.height = "0px";
	theObject.style.width = "0px";
	acObject = document.getElementById("autocompletediv");
	acObject.style.visibility = "hidden";
	acObject.style.height = "0px";
	acObject.style.width = "0px";
}

function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	{
		while(obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if(obj.x)
	{
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	{
		while(obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if(obj.y)
	{
		curtop += obj.y;
	}
	return curtop;
}

function autoComplete(theValue,e)
{
	var xmlhttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlhttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	if(!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	
	theObject = document.getElementById("autocompletediv");
	
	theObject.style.visibility = "visible";
	theObject.style.width = "410px";
	
	var posx = 0;
	var posy = 0;
	
	
	posx = ( findPosX(document.getElementById("yourname")) + 1 );
	posy = ( findPosX(document.getElementById("yourname")) + 23 );
	
	theObject.style.left = posx + "px";
	theObject.style.top = posy + "px";
	
	
	theextrachar = e.which;
	
	if(theextrachar == undefined)
	{
		theextrachar = e.keyCode;
	}
	
	var objID = "autocompletediv";
	
	theValue = retirarAcento(theValue);
	
	if(theextrachar == 8)
	{
		if(theValue.length == 1)
		{
			var serverPage = "includes/cursos_autocomp.php";
		}
		else
		{
			var serverPage = "includes/cursos_autocomp.php?sstring=" + theValue.substr(0, (theValue.length -1));
		}
	}
	else
	{
		if(theValue.length >= 0)
		{
			var serverPage = "includes/cursos_autocomp.php?sstring=" + theValue + String.fromCharCode(theextrachar);
		}
	}
	
	var obj = document.getElementById(objID);
	
	xmlhttp.open("GET",serverPage);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			obj.innerHTML = xmlhttp.responseText;			
		}
	}
	xmlhttp.send(null);
}

function setvalue(thevalue)
{
	acObject = document.getElementById("autocompletediv");
	
	acObject.style.visibility = "hidden";
	acObject.style.height = "0px";
	acObject.style.width = "0px";
	
	document.getElementById("yourname").value = thevalue;
}

function retirarAcento(objResp)
{   
	
	var varString = new String(objResp);   
	var stringAcentos = new String('àâêôûãõáéíóúçüÀÂÊÔÛÃÕÁÉÍÓÚÇÜ');   
	var stringSemAcento = new String('aaeouaoaeioucuAAEOUAOAEIOUCU');   
	  
	var i = new Number();   
	var j = new Number();   
	var cString = new String();   
	var varRes = '';   
	  
	for (i = 0; i < varString.length; i++)
	{   
		cString = varString.substring(i, i + 1);   
		for (j = 0; j < stringAcentos.length; j++) 
		{   
			if (stringAcentos.substring(j, j + 1) == cString)
			{   
				cString = stringSemAcento.substring(j, j + 1);   
			}   
		}   
		varRes += cString;   
	}   
		
	return varRes;
	
}  