var tmpText = "";
var User    = [];
var UserID  = [];                        
           
function selectUser(userid) {
    
    if (userid == "null") {
        document.getElementById("user").value    = "";
        document.getElementById("user_id").value = 0;           
    }
    else if (User[userid] != null && UserID[userid] != null) {    
        document.getElementById("user").value    = User[userid];
        document.getElementById("user_id").value = UserID[userid];
    }
    
    hideUserBox();                
}
            
function showUserBox() {
    
    var txtfield = document.getElementById("user");
    var userbox  = document.getElementById("userbox");

    makeHttpRequest('index.php?module=contact&action=user', 'buildUserList', true);
    
    if (userbox.style.visibility != "visible") {
 
        var spantop    = absTop(txtfield);
        var spanleft   = absLeft(txtfield);
        var spanwidth  = txtfield.offsetWidth;
        var spanheight = txtfield.offsetHeight;   
    
        spantop   = spantop + spanheight;
        spanwidth = spanwidth - 2;
    
        userbox.style.width = spanwidth+"px";    
        userbox.style.left  = spanleft+"px";
        userbox.style.top   = spantop+"px";    
            
        userbox.style.visibility = "visible";    
        userbox.style.display    = "block";      
   }
}

function hideUserBox() {
    var userbox  = document.getElementById("userbox");
    userbox.style.visibility = "hidden";    
}

function searchUser() {
    
    var searchtxt = document.getElementById("usersearch").value;
    
    if (searchtxt != "") {
        makeHttpRequest('index.php?module=contact&action=user&txt='+searchtxt, 'buildUserList', true);
    }
}

function buildUserList(xmldoc) {

    var result_div = xmldoc.getElementsByTagName("user"); 
                
    User   = [];
    UserID = [];

    for (i = 0; i < result_div.length; i++) {                    
        temp = xmldoc.getElementsByTagName('user')[i];
        name = temp.getElementsByTagName('name')[0].firstChild.nodeValue;
        id   = temp.getElementsByTagName('userid')[0].firstChild.nodeValue;
        User.push(name);                    
        UserID.push(id);
    }
                                
    var temp = "";                
    tmpText = xmldoc;
    temp = temp + "<div onclick=\"selectUser('null');\">&gt;&gt;No User selected&lt;&lt;</div>";            
    if (User.length > 0 && User[0] != "") {
                    
        for (i = 0; i < User.length; i++) {  
            temp = temp + "<div onclick=\"selectUser(" + i + ");\">" + User[i] + "</div>";                        
        }
        
    }
    document.getElementById("userboxresult").innerHTML = temp;                    
}


function showInfoBox(element, header, message) {
    
    var infobox       = document.getElementById("infobox");
    var infoboxheader = document.getElementById("infoboxheader");
    var infoboxmsg    = document.getElementById("infoboxmsg");
    
    infoboxheader.innerHTML = header;    
    infoboxmsg.innerHTML    = message;    
        
    var spantop    = absTop(element);
    var spanleft   = absLeft(element);
    var spanwidth  = element.offsetWidth;
    var spanheight = element.offsetHeight; 

    spantop   = spantop + spanheight;

    infobox.style.width = spanwidth+"px";    
    infobox.style.left  = spanleft+"px";
    infobox.style.top   = spantop+"px"; 
      
    infobox.style.visibility = "visible";
    infobox.style.display    = "block";
}

function hideInfoBox() {

    var infobox  = document.getElementById("infobox");

    infobox.style.visibility = "hidden";
    infobox.style.display    = "none";
}