// this is used to convert an hCard to a form that gets submitted to
// a server-side application to be converted to a vCard

// dynamically create a form and POST it to the specified URL
function post_to_url(url, params) {
    var form = document.createElement('form');
    form.name = 'vcard';
    form.action = url;
    form.method = 'post';

    for(var key in params) {
        if (params.hasOwnProperty(key)) {
            var input = document.createElement('input');
            input.type  = 'hidden';
            input.name  = key;
            input.value = params[key];

            form.appendChild(input);
        }
    }

    document.body.appendChild(form);
    form.submit();
}

// this assumes an hCard microformat structure
// it then creates a form with the individual elements and
// posts it to a specific location, e.g. to be converted to vCard
function row_to_vcard(el) {
    // traverse the tree until the closest "vcard" class is foun
    for (; el != null; el = el.parentNode) {
        if (el.className == 'vcard')
            break;
    }

    // we traversed up to the top and it wasn't found, bail.
    if (el == null)
        return;

    var params = new Array();
    var children = el.getElementsByTagName('*');
    for (var e=0; e < children.length; e++) {
        cl = children[e].className;
        if (cl == null)
            continue;

	var param = children[e].innerText || children[e].textContent; 
	if (param != undefined) {
	    params[cl] = param;
	}
    }

    post_to_url('/vcard/', params);
}
