/*
 * Description: Javascript FOAF-a-matic implementation
 *
 * Author: Leigh Dodds, leigh@ldodds.com
 *
 * License: Consider this PUBLIC DOMAIN code, do with it what you will, just mention where
 * you got it. Cheers.
 *
 */

/* =========================== Globals ============================= */

gDefaultNumberOfFriends=3;
gCurrentNumberOfFriends=0;
gFriendTableBody = null;

/* =========================== Globals ============================= */

/* =========================== Generate ============================= */

function generate()
{
    if (validate())
    {
        //clear text area
        document.results.rdf.value='';

        //process the form values to make a FOAF person
        person = buildPerson();

        foafNS='http://xmlns.com/foaf/0.1/'
        //dump the final RDF description
        person.dumpToTextArea(document.results.rdf);
        myRDF=new RDF()
        myRDF.loadRDFXML(document.results.rdf.value)
        if (myRDF.getSingleObject(null,null,foafNS+'mbox',null)!='mailto:'+person.email) alert("This RDF looks a bit suspect")
        else alert('The RDF says your mbox is:'+myRDF.getSingleObject(null,null,foafNS+'mbox',null)+'\n and your name is '+myRDF.getSingleObject(null,null,foafNS+'name',null))

    }
}
/* =========================== Generate ============================= */

/* =========================== Build Person =========================== */
function buildPerson()
{
    p = new Person();

    p.title = document.details.title.value;
    p.firstName = document.details.firstName.value;
    p.surname = document.details.surname.value;
    p.email = document.details.email.value;
    p.nick = document.details.nick.value;
    p.homePage = document.details.homepage.value;
    p.phone = document.details.phone.value;
    p.workplaceHomepage = document.details.workplaceHomepage.value;
    p.workInfoHomepage = document.details.workInfoHomepage.value;
    p.depiction = document.details.depiction.value;
    p.schoolHomepage = document.details.schoolHomepage.value;

    //now add friends
    for (i=1; i<=gCurrentNumberOfFriends;i++)
    {
        if (document.friends.elements['friend_' + i].value != '' && document.friends.elements['friend_' + i + '_mbox'].value != '')
        {
            friend = new Person();
            friend.name=document.friends.elements['friend_' + i].value;
            friend.email=document.friends.elements['friend_' + i + '_mbox'].value;
            p.addFriend(friend);
        }
    }

    return p;
}
/* =========================== Build Person =========================== */

/* ========================== Form Validation ========================== */

//TODO -- validate email addresses

function validate()
{

    isValid = true;
    msg = '';

    if (document.details.firstName.value=='')
    {
        isValid=false;
        msg = msg + 'First Name\n';
    }

    if (document.details.surname.value=='')
    {
        isValid=false;
        msg = msg + 'Last Name\n';
    }

    if (document.details.email.value=='')
    {
        isValid=false;
        msg = msg + 'Email Address\n';
    }

    for (i=1; i<=gCurrentNumberOfFriends;i++)
    {
        if (document.friends.elements['friend_' + i].value != '' && document.friends.elements['friend_' + i + '_mbox'].value == '')
        {
            isValid=false;
            msg = msg + 'An Email Address for Friend ' + i +'\n';
        }
    }

    if (!isValid)
    {
        alert('You must complete these fields:\n' + msg);
    }

    return isValid;
}
/* ========================== Form Validation ========================== */

/* ========================== Form Utilities ============================ */

function createFriendFields()
{
    gFriendTableBody = document.getElementById('friendtable');
    for (i=1; i<=gDefaultNumberOfFriends; i++)
    {
        addFriendFields();
    }
}

function addFriendFields()
{
    gCurrentNumberOfFriends++;
    tr = document.createElement('tr');
    tr.appendChild(addCol('Friend ' + gCurrentNumberOfFriends + ' --'));
    tr.appendChild(addCol('Name'));
    tr.appendChild(addCol('<input type=\"text\" name=\"friend_'+gCurrentNumberOfFriends+'\" value="">'));
    tr.appendChild(addCol('Email'));
    tr.appendChild(addCol('<input type=\"text\" name=\"friend_'+gCurrentNumberOfFriends+'_mbox\" value="">'));
    gFriendTableBody.appendChild(tr);
}

function addCol(html) {
 var td=document.createElement('td')
 td.innerHTML=html
 return td
}


/* ========================== Form Utilities ============================ */


