/* ajax.js */

/*
new Form.Element.Observer(
    'input_category',
    0.2,
    function(el, value){
        alert('The form control has changed value to: ' + value);
    }
    );
  */  
  
var aktiv= new Array();

function timercall(text,suffix) {    
    if (aktiv[suffix]) window.clearInterval(aktiv[suffix]);
    aktiv[suffix]=window.setTimeout("updateCategories('"+text+"','"+suffix+"')", 500);
}

function updateCategories(text,suffix) {           
    new Ajax.Request('/xml.php',
        {
            method: 'post',
            parameters: {
                type: 'category_start',
                start: text
            },
            onSuccess: function(transport) {
                var xml = transport.responseXML;
                
                var category = xml.getElementsByTagName("category");
                if (category)
                {
                    var categories_div = $('div_category_'+suffix);                    
                    //var count = categories_div.childNodes.length;
                    
                    //deletes all old nodes
                    var node;
                    while ((node=categories_div.firstChild)!=null) {
                        categories_div.removeChild(node);
                    }
                                      
                    
                    for(var i=0; i < category.length; i++)
                    {                        
                        addCategory(categories_div, category[i].firstChild.data,i,suffix);
                    }
                    
                }
            }
        }
        );
    
}

function addCategory(categories_div, category,i,suffix) {    
    // to be done a little more beautiful ...    
    if (i>0)  {
        var myText = document.createTextNode(" - ");
        categories_div.appendChild(myText);        
   }
    var node_a = document.createElement("span");    
    node_a.setAttribute("onclick","document.getElementById('input_category_"+suffix+"').value='"+category+"'");
    node_a.setAttribute("style","text-decoration: underline;cursor:pointer");    
    var myText = document.createTextNode(category);
    node_a.appendChild(myText);    
    categories_div.appendChild(node_a);
    //categories_div.firstChild.appendData(category_temp);
}
