function Book(values)
{
	this.record_id = values ? values.record_id : null;

	this.prefix    = values ? values.prefix : null;
	this.title     = values ? values.title : null;
	this.subtitle  = values ? values.subtitle : null;

	this.authors   = values ? values.authors : [];

	this.read      = values ? values.read : false;
	this.owned     = values ? values.owned : false;

	this.position  = values ? values.position : 0;

	this.note      = values ? values.note : null;

	this.getTitle = function()
	{
		if (this.prefix) {
			return this.prefix + ' ' + this.title;
		}
		return this.title;
	}

	this.getAuthorString = function()
	{
		var author_str = '';
		for (var i = 0; i < this.authors.length; i++) {
			if (this.authors[i]) {
				if (i != 0) {
					author_str += ', ';
				}
				author_str += this.authors[i];
			}
		}
		return author_str;
	}
}

///////
// Create new Book object from XML response
///////
Book.from_xml = function(xml)
{
	var values = {
		record_id: parseInt(get_value(xml, 'id')),
		title:     get_value(xml, 'title', true),
		prefix:    get_value(xml, 'prefix'),
		read:      parseInt(get_value(xml, 'read')),
		owned:     parseInt(get_value(xml, 'owned')),
		position:  parseInt(get_value(xml, 'position')),
		note:      get_value(xml, 'note'),
		authors:   [ ]
	};
		
	for (var i = 0; i < xml.getElementsByTagName('author').length; i++) {
		values.authors.push(get_value(xml.getElementsByTagName('author')[i], 'name'));
	}

	return new Book(values);
}

/*******
 ** GET VALUE FROM XML
 *******
 ** Get the value of the 'name' subelement (or attribute) of the 'element' XML
 ** node.
 ** If 'required' is true, throws an exception if the element does not exist
 ** or has no value; otherwise returns 'null' for such elements.
 *******/
function get_value(element, name, required)
{
	var value = null;

	if (element.getElementsByTagName(name).length > 0 && element.getElementsByTagName(name)[0].firstChild)
		value = element.getElementsByTagName(name)[0].firstChild.nodeValue;
	else if (element.getAttribute(name))
		value = element.getAttribute(name);

	if (required && !value)
		throw "No element/value found for required " + name + " element!";

	return value;
}

