//myfunctions.js

//Variable allow us to check if we are using IE.
var xmlhttp=false;
try{
//Check for JavaScript version (5 or higher)
xmlhttp = new ActiveXObject(Msxml2.XMLHTTP");
}catch(e){
	try{
	//If we are using IE.
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}catch(E){
	//Otherwise.
	xmlhttp = false;
	}
}
// If we are not using IE, we create a JavaScript object.
if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
	xmlhttp = new XMLHttpRequest();
}


function findPosX(obj){
	var curleft = 0;
	if(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){
                curtop += obj.offsetTop
                obj = obj.offsetParent;
        }else if(obj.y){
                curtop += obj.y;
        }
        return curtop;
}




function automplete(thevalue,e){

theObject = document.getElementById("autocompletediv");

theObject.style.visibility= "visible";
theObject.style.width = "152px";

var posx = 0;
var posy = 0;

posx = (findPosX(document.getElementById("searchID")) + 1);
posy = (findPosY(document.getElementById("searchID")) + 23);

theObject.style.left = posx + "px";
theObject.style.top = posy + "px";

var theextrachar = e.which;

if(theextrachar == undefined){
theextrachar = e.keyCode;
}

//La ubicacion en la que cargamos la pagina.

var objID = "autocompletediv";

//Tener en cuenta el caracter backspace.
if(theextrachar == 8){
	if(thevalue.length == 1){
        	var serverPage = "autocomplete.php";   
	}else{
		var serverPage = "autocomp.php" + "?sstring=" + thevalue.substr (0,(thevalue.length -1));
	}
}else{
	var serverPage = "autocomplete.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("searchID").value = theValue;
}


function validateform (thevalue){

	serverPage = "validator.php?sstring=" + thevalue;
	objID = "autocompletediv";

	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);
}

