<!--//

var bmsg = {};

function init_account(){
    var sg = document.forms[0]['society_groups[]'];
    var total_soc = document.forms[0]['society_groups[]'].length;
    if( typeof(total_soc) == "undefined" ){
        total_soc = 1;
    }

    SAYT = document.createElement('div');
    SAYT.setAttribute('id','society-result');
    Element.addClassName(SAYT,"SAYT");
    SAYT.setAttribute('style','display: none;');
    document.forms[0].appendChild(SAYT);

    for(x=0; x < total_soc; x++){
        var set = (total_soc == 1) ? sg : sg[x];
        set.setAttribute("id","society_groups"+x);
        set.onfocus = function(){return this.style.backgroundColor='';}
        set.setAttribute('autocomplete','off');
        new Autocompleter.Local($("society_groups"+x), "society-result", sayt_options,{partialSearch:true,fullSearch:true,afterUpdateElement:after_update_element});
    }
}



function init_browse_members_society_groups(){
    var sg = document.forms[0]['society_groups'];

    SAYT = document.createElement('div');
    SAYT.setAttribute('id','society-result');
    Element.addClassName(SAYT,"SAYT");
    SAYT.setAttribute('style','display: none;');
    document.forms[0].appendChild(SAYT);

    set = sg;
    set.setAttribute("id","society_groups");
    set.onfocus = function(){return this.style.backgroundColor='';}
    sg.setAttribute('autocomplete','off');
    new Autocompleter.Local("society_groups", "society-result", sayt_options,{partialSearch:true,fullSearch:true,afterUpdateElement:after_update_element});
}

function after_update_element(elem,sel){
    elem.blur();
    elem.style.backgroundColor = "#E1E9F6";
}

function add_society_group(){
    var sg = document.forms[0]['society_groups[]'];
    var total_sg = sg.length ? sg.length : 1;
    var sg_container = Element.ancestors("society_groups0")[1];

    var newSG = document.createElement('div')
    var newSGInput = Try.these(
        function(){
            return document.createElement('<input name=society_groups[]>');
        },
        function(){
            return newSGInput = document.createElement('input');
        }
    );

    Element.addClassName(newSG,"formset");
    Element.addClassName(newSGInput,"textfield");
    newSGInput.setAttribute('id',"society_groups" + total_sg);
    newSGInput.setAttribute('size',30);
    newSGInput.setAttribute('name',"society_groups[]");

    newSG.appendChild(newSGInput);
    newSG.onfocus = function(){return this.style.backgroundColor='';}
    newSG.setAttribute('autocomplete','off');
    sg_container.appendChild(newSG);

    new Autocompleter.Local(newSGInput.id, "society-result", sayt_options,{partialSearch:true,fullSearch:true,afterUpdateElement:after_update_element});

}






/*=========================================

=========================================*/
function init_account2(element,update,options,params){
    var mForm = params.form ? params.form : 0;
    var form = document.forms[mForm];

    if( form[element] ){
        init_account_single_field(element,update,options,params);
        return;
    } else if( form[element+"[]"] ){
    } else {
        return;
    }


    var fields = form[element+"[]"];
    var total_fields = fields.length;
    if( typeof(total_fields) == "undefined" ){
        total_fields = 1;
    }

    if( $(update) == null ){
        SAYT = document.createElement('div');
        SAYT.setAttribute('id',update);
        Element.addClassName(SAYT,"SAYT");
        SAYT.setAttribute('style','display: none;');
        form.appendChild(SAYT);
    }

    for(x=0; x < total_fields; x++){
        var set = (total_fields == 1) ? fields : fields[x];
        set.setAttribute("id",element+x);
        set.onfocus = function(){return this.style.backgroundColor='';}
        set.setAttribute('autocomplete','off');
        new Autocompleter.Local($(element+x), update, options,{partialSearch:true,fullSearch:true,afterUpdateElement:after_update_element});
    }
}

function init_account_single_field(element,update,options,params){
    var sg = document.forms[0][element];

    if( $(update) == null ){
        SAYT = document.createElement('div');
        SAYT.setAttribute('id',update);
        Element.addClassName(SAYT,"SAYT");
        SAYT.setAttribute('style','display: none;');
        document.forms[0].appendChild(SAYT);
    }

    var set = sg;
    set.setAttribute("id",element);
    set.onfocus = function(){return this.style.backgroundColor='';}
    set.setAttribute('autocomplete','off');
    //new Autocompleter.Local($(element), update, options,{partialSearch:true,fullSearch:true,afterUpdateElement:after_update_element});
    
    if( params.type == "dynamic" )
        new Ajax.Autocompleter($(element), update, params.url,{partialSearch:true,fullSearch:true,afterUpdateElement:after_update_element});
    else
        new Autocompleter.Local($(element), update, options,{partialSearch:true,fullSearch:true,afterUpdateElement:after_update_element});

}

function add_new_account_field(element,update,options){
    var mForm = arguments[3] ? arguments[3] : 0;
    var form = document.forms[mForm];

    var fields = form[element+'[]'];
    var total_fields = fields.length ? fields.length : 1;
    //var fields_container = Element.ancestors(element+"0")[1];
    var fields_container = Try.these(
        function(){ return Element.up(form[element+'[]'][0],1); } ,
        function(){ return Element.up(form[element+'[]'],1); }
    );

    var newFIELD = document.createElement('div')
    var newFIELDInput = Try.these(
        function(){ return document.createElement('<input name='+element+'[]>'); } ,
        function(){ return document.createElement('input'); }
    );

    Element.addClassName(newFIELD,"formset");
    Element.addClassName(newFIELDInput,"textfield");
    newFIELDInput.setAttribute('id',element+total_fields);
    newFIELDInput.setAttribute('size',30);
    newFIELDInput.setAttribute('name',element+"[]");

    newFIELD.appendChild(newFIELDInput);
    newFIELD.onfocus = function(){return this.style.backgroundColor='';}
    newFIELD.setAttribute('autocomplete','off');
    fields_container.appendChild(newFIELD);

    new Autocompleter.Local(newFIELDInput.id, update, options,{partialSearch:true,fullSearch:true,afterUpdateElement:after_update_element});
}

function add_new_element_link(elem){
    /*
    elem        = 
    label
    action
    add_new_account_field(\\\"languages_i_write\\\",\\\"society-result\\\",languages_i_write_list)
    <a class='add_new_element' href='#' onclick='add_new_account_field(\\\"languages_i_write\\\",\\\"society-result\\\",languages_i_write_list); return false'>Add More</a>\")
    */
    var container = document.createElement("div");
    var link = document.createElement("a");
    
    container.setAttribute("className","add_new_element_container");
    link.setAttribute("className","add_new_element");
    link.setAttribute("onclick","add_new_account_field('languages_i_write','society-result','languages_i_write_list'); return false;");
    link.setAttribute("href","javascript");
    link.innerHTML = "Add Another";
    
    container.appendChild(link);alert(elem.innerHTML);
    elem.appendChild(container);elem.innerHTML = "test";alert(elem.innerHTML);
}




//-->
