var _selbgcol='#666666';
var _selcol='#ffffff';
var _detailbgcol='#ffcc66';

var _selectedNode;
var _numAddFriends;
var _friendsAdded=false;

var _relOptions = [
    "foaf:knows",
    "rel:friendOf",
    "rel:closeFriendOf",
    "rel:acquaintanceOf",
    "rel:hasMet",
    "rel:spouseOf",
    "rel:worksWith",
    "rel:collaboratesWith",
    "rel:lostContactWith"
];

var friendAddInfoList=[
	{l: "FOAF profile URL", id: "g"},
    {l: "del.icio.us user name", id: "h"},
    {l: "homepage URL", id: "f"}
];

function se(id) {
	document.entries["a"+id].checked=true;
}

function markAll(state) {
	for(var i=1; i<document.entries.numEntries.value; i++) document.entries["a"+i].checked=state;
}

function checkEntries() {
    for(var i=1; i<document.entries.numEntries.value; i++) {
        if (document.entries["a"+i].checked) return true;
    }
    if (_numAddFriends>0) return true;
    return true; //(confirm('None of the imported contacts have been selected to be included in your FOAF profile. The FOAF file will only contain information about yourself.\n\nContinue?'));
}

function mailtoscramble(a,b,c) {
    location.href="mailto:"+a+"@"+b+c;
}

function createCustomElement(parent,elem,cl,txt) {
    var e = document.createElement(elem);
    if(cl != null)
        e.className = cl;
    if(txt != null)
        e.appendChild(document.createTextNode(txt));
    if(parent != null)
        parent.appendChild(e);
    return(e);
}

function on(e) {
    if (_selectedNode!=undefined) off(_selectedNode);
    e.style.background=_selbgcol;
    e.style.color=_selcol;
    if (document.entries['f'+e.id.substr(1,3)]!=undefined) {
        var id='fdr'+e.id.substr(1,3)+'_';
        document.getElementById(id+'0').style.background=_detailbgcol;
        document.getElementById(id+'1').style.background=_detailbgcol;
        document.getElementById(id+'2').style.background=_detailbgcol;
    }
    _selectedNode=e;
}

function off(e) {
    e.style.background='';
    e.style.color='';
    if (document.entries['f'+e.id.substr(1,3)]!=undefined) {
        var id='fdr'+e.id.substr(1,3)+'_';
        document.getElementById(id+'0').style.background='';
        document.getElementById(id+'1').style.background='';
        document.getElementById(id+'2').style.background='';
    }
}

function ins(e,skipColumns,id) {
    var base;
    if (e!=null) {
        base=e.parentNode.parentNode;
        id=e.parentNode.getAttribute('id').substr(1,3);
    } else {
        base=document.getElementById('f'+id);
        e=document.getElementById('fd'+id);
    }
    if (document.entries["f"+id]==undefined) {
        for(var i=0; i<friendAddInfoList.length; i++) {
            var cn='details';
            var tr=createCustomElement(null,'tr');
            tr.id='fdr'+id+'_'+i;
            if (!skipColumns) {
                createCustomElement(tr,'td');
                createCustomElement(tr,'td');
            }
            createCustomElement(tr,'td',cn,friendAddInfoList[i].l);
            var td=createCustomElement(tr,'td',cn);
            td.innerHTML='<input type="text" name="'+friendAddInfoList[i].id+id+'" onchange="se('+id+');" />';
            td.setAttribute('colspan',4);
            base.insertBefore(tr,e.parentNode.nextSibling);
        }
        //e.parentNode.hasDetails=true;
        e.removeChild(e.firstChild);
        on(e.parentNode);
    }
}

function addFriend() {
    if (_numAddFriends==undefined) _numAddFriends=Math.max(parseInt(document.entries.numEntries.value),1);
    alert(_numAddFriends);
    var ft=document.getElementById('friendstable');
    if (!_friendsAdded) {
        ft.style.display='table';
        _friendsAdded=true;
    }
    var tr=createCustomElement(ft,'tr');
    tr.setAttribute('id','f'+_numAddFriends);
    tr.setAttribute('onmouseover','on(this);');
    var td=createCustomElement(tr,'td');
    // insert select
    var sel=createCustomElement(td,'select');
    sel.name='b'+_numAddFriends;
    // relationship options
    for(var i=0; i<_relOptions.length; i++) {
        var opt=createCustomElement(sel,'option',null,_relOptions[i]);
        opt.value=i+1;
    }
    td=createCustomElement(tr,'td');
    td.innerHTML='<input type="hidden" name="a'+_numAddFriends+'" value="on" /><input type="text" name="c'+_numAddFriends+'" />';
    td=createCustomElement(tr,'td');
    td.innerHTML='<input type="text" name="d'+_numAddFriends+'" />';
    td=createCustomElement(tr,'td');
    td.innerHTML='<input type="text" name="e'+_numAddFriends+'" />';
    td=createCustomElement(tr,'td');
    td.setAttribute('onclick','ins(this,true);');
    td.innerHTML='<span class="bt2">details...</span>';
    on(tr);
    document.entries.numEntries.value=++_numAddFriends;
}