//GENERIC XML HTTP OBJECT GENERATOR
function getxmlhttp(){

    var xmlhttp = false;

    try{
        //IF IE and Javascript is greter than version 5
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        //Older IE ActiveXObject
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            
        }catch(E){
            //NOT IE
            xmlhttp = false;
        }
    }

    //NON IE (FUNCTIONAL) BROWSERS
    if(!xmlhttp && typeof XMLHttpRequest != "undefined"){
        xmlhttp = new XMLHttpRequest();
    }
    
    return xmlhttp;
}

//GENERIC XMLHttpRequest Processor
function dotheajax(phpfile, obj, getpost, str, response){

    var xmlhttp = getxmlhttp();
    
    if(getpost == "GET"){
        xmlhttp.open("GET", phpfile);
    }else{
        xmlhttp.open("POST", phpfile);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    }
    
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            
            if(response != "none"){
                if(response == "value"){
                    obj.value = xmlhttp.responseText;
                    //alert(xmlhttp.responseText);
                }else{
                    obj.innerHTML = xmlhttp.responseText;
                    //alert(xmlhttp.responseText);
                }
            }
            
        }

    }
    xmlhttp.send(str);
}

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){
        curleft += obj.y;
    }
    return curtop;
}

function close_object(object_id){
    theobject = document.getElementById(object_id);
    theobject.style.display = "none";
    theobject.style.height = "0px";
    theobject.style.width = "0px";
    theobject.style.padding = "0px";
    theobject.style.margin = "0px";
}

function staus_msg(object_id, status){

    var objID = object_id;
    var serverpage = root_path + "/ajax_status.php" + "?status=" + status;
    var obj = document.getElementById(objID);
    dotheajax(serverpage, obj, "POST", "null", "innerHTML");

}

function setvalue(thevalue, sourceid, destid){
	document.getElementById(destid).value = thevalue;
	close_object(sourceid);
}

function autocomp(serverpage, reference, e){
	
	var thekey = e.which;
    if(thekey == undefined){
        thekey = e.keyCode;
    }

    var objID = "autocomp_div";
    theobject = document.getElementById(objID);
	
	if(thekey == 27){
    	close_object(objID);	
    } else {
    	theobject.style.display = "block";
    	theobject.style.width = "152px";
    }
    
    if(thekey == 38){
    	autocomp_row_hilite("previous");	
    }
    
    if(thekey == 40){
    	autocomp_row_hilite("next");	
    }
	
    var posx = 0;
    var posy = 0;

    posx = (findposx(document.getElementById(reference)));
    posy = (findposy(document.getElementById(reference))+15);

    theobject.style.left = posx + "px";
    theobject.style.top = posy + "px";
    
    var usepage = root_path + serverpage;

    dotheajax(usepage, theobject, "POST", "null", "innerHTML");
    
}

function autocomp_row_hilite(prevnext){
	
	var curr_row = document.getElementById('autocomp_hilited').value;
	var total_rows = document.getElementById('autocomp_totrows').value;
	
	if(prevnext == "next"){
		var the_row = curr_row+1;
		if(the_row > total_rows){
			the_row = 1;
		}
	} else if(prevnext == "previous"){
		var the_row = curr_row-1;
		if(the_row < 1){
			the_row = total_rows;
		}
	}
	
	var row_id = 'acrow_' + the_row;
	alert(row_id);
	var row_obj = document.getElementById(row_id);
	row_obj.style.fontWeight = "bold"; 
	
}
