<!--

/*
	*	File:					client.js
	*	Description:	Functions that records and outputs client information
	*	Date:					28 May 2004
	*	Author:				H. Korotnicki - Education Queensland
*/

//  Java Document
//  JavaScript Document
//  DESCRIPTION:	Store and read more information
//  AUTHOR: 		Sharen Chen
//  Date of Update:	04/09/2008
//	Version:		v2.00
//-----------------------------------------------------------

//	FUNCTION:			readSelected			
//	DESCRIPTION:	Finds and returns all the selected resources items for a given form		
// 	ARGUMENTS:		Name of form		
//	RETURNS:			List of resources in a string, each resource is within a '<li>' tag			
function readSelected(formName)	{
	// initialise list of selected items & get list of resource items from form
	var selectedItems = '';
	var list = eval( 'document.' + formName + '.getElementsByTagName(\'li\')');

	// go through all list items and extract list of checked resources
	for(var i=0; i<list.length; i++)	{
		var resourceItem = list[i].getElementsByTagName('input')[0];
		
		if(resourceItem != null)	{
			if(resourceItem.checked)	{
				if (arguments[1] == 'ja')	{ 
					resourceItem = resourceItem.value;
					resourceItem = resourceItem.replace(/\n/gi,'');
				}
				else {
					resourceItem = list[i].getElementsByTagName('label')[0].innerHTML;
					resourceItem = resourceItem.split(/<br>/i,1);
					if (arguments[1] == 'vl')
						resourceItem += ' <br>' + list[i].getElementsByTagName('span')[0].innerHTML;
				}
				selectedItems += '<li>' + resourceItem + '</li>';
			}
		}
	}
	
	return selectedItems;
}

//	FUNCTION:			readItems			
//	DESCRIPTION:	Finds and returns all the entered resources items for a given form		
// 	ARGUMENTS:		Name of form		
//	RETURNS:			List of resources in a string, each resource is within a '<li>' tag			
function readItems(formName)	{
	
	var enteredItems = '';	// initialise list of selected items
	var title='', article='', author='', vol='', no='', date='', publisher='', pages='';	//initialise item
	var fromDate='', toDate='', months='';		//initialise video
	var i = 1;  // initialise resource counter
	var clientForm = eval( 'document.' + formName );
	
	if( eval('document.' + formName + '.title' + i) )
		title = eval('document.' + formName + '.title' + i + '.value');
	if( eval('document.' + formName + '.article' + i) )
		article = eval('document.' + formName + '.article' + i + '.value');
	if( eval('document.' + formName + '.author' + i) )
		author = eval('document.' + formName + '.author' + i + '.value');
	if( eval('document.' + formName + '.vol' + i) )
		vol = eval('document.' + formName + '.vol' + i + '.value');
	if( eval('document.' + formName + '.no' + i) )
		no = eval('document.' + formName + '.no' + i + '.value');
	if( eval('document.' + formName + '.date' + i) )	
		date = eval('document.' + formName + '.date' + i + '.value');
	if( eval('document.' + formName + '.publisher' + i) )
		publisher = eval('document.' + formName + '.publisher' + i + '.value');
	if( eval('document.' + formName + '.pages' + i) )
		pages = eval('document.' + formName + '.pages' + i + '.value');
	if( eval('document.' + formName + '.from_day' + i) )	{
		fromDate = eval('document.' + formName + '.from_day' + i + '.value') + ' ';
		fromDate += eval('document.' + formName + '.from_month' + i + '.value') + ' ';
		fromDate += eval('document.' + formName + '.from_year' + i + '.value');
		toDate = eval('document.' + formName + '.to_day' + i + '.value') + ' ';
		toDate += eval('document.' + formName + '.to_month' + i + '.value') + ' ';
		toDate += eval('document.' + formName + '.to_year' + i + '.value');
		months = eval('document.' + formName + '.months' + i + '.value');
	}
	// while an article, title or author exists on the form, extract each item
	while ( article != null || title != null || author != null )	{
		// add formatting
		if (author) 
			author += ' ';
		if (article)
			if ( (article.charAt(0) != '\'') || (article.charAt(article.length-1) != '\'') )
				article = '\'' + article + '\', ';
		if (title)	{
			title = '<cite>' + title + '</cite>';
			if (vol || no || date || publisher || pages)	title += ', ';			
		}
		if (vol)	{
			vol = 'Vol: ' + vol;
			if (no || date || publisher || pages)	vol += ', ';
		}
		if (no)	{
			no = 'No: ' + no;
			if (date || publisher || pages) no += ', ';
		}
		if (date && (publisher || pages)) date += ', ';
		if (publisher && pages)	publisher += ', ';
		if (pages) pages = 'Pages: ' + pages;
		if (fromDate && fromDate != '  ')	fromDate = ' from: ' + fromDate;
		if (toDate && toDate != '  ')	toDate = ' to: ' + toDate;
		if (months && months != '_') months = ' (any 10 days within the next ' + months + ' months)';
		else months = '';	
		
		// create resourceItem string and add to list if it is not just white space
		var resourceItem = author + article + title + vol + no + date + publisher + pages + fromDate + toDate + months;
		if(resourceItem.replace(/\s+/,'')) enteredItems += '<li>' + resourceItem + '</li>'
		i++;
		if ( eval('document.' + formName + '.title' + i) || eval('document.' + formName + '.article' + i) || eval('document.' + formName + '.author' + i) )	{
			if( eval('document.' + formName + '.title' + i) )
				title = eval('document.' + formName + '.title' + i + '.value');
			if( eval('document.' + formName + '.article' + i) )
				article = eval('document.' + formName + '.article' + i + '.value');
			if( eval('document.' + formName + '.author' + i) )
				author = eval('document.' + formName + '.author' + i + '.value');
			if( eval('document.' + formName + '.vol' + i) )
				vol = eval('document.' + formName + '.vol' + i + '.value');
			if( eval('document.' + formName + '.no' + i) )
				no = eval('document.' + formName + '.no' + i + '.value');
			if( eval('document.' + formName + '.date' + i) )	
				date = eval('document.' + formName + '.date' + i + '.value');
			if( eval('document.' + formName + '.publisher' + i) )
				publisher = eval('document.' + formName + '.publisher' + i + '.value');
			if( eval('document.' + formName + '.pages' + i) )
				pages = eval('document.' + formName + '.pages' + i + '.value');
			if( eval('document.' + formName + '.from_day' + i) )	{
				fromDate = eval('document.' + formName + '.from_day' + i + '.value') + ' ';
				fromDate += eval('document.' + formName + '.from_month' + i + '.value') + ' ';
				fromDate += eval('document.' + formName + '.from_year' + i + '.value');
				toDate = eval('document.' + formName + '.to_day' + i + '.value') + ' ';
				toDate += eval('document.' + formName + '.to_month' + i + '.value') + ' ';
				toDate += eval('document.' + formName + '.to_year' + i + '.value');
				months = eval('document.' + formName + '.months' + i + '.value');
			}
		}
		else	{
			article = null;
			title = null;
			author = null;
		}
	}
	
	return enteredItems;
}

//	FUNCTION:			storeClient						
//	DESCRIPTION:	Stores client data from a form into a cookie			
// 	ARGUMENTS:		Name of form, type of form		
//	RETURNS:			nothing			
function storeClient(formName, type)		{
	var clientForm = eval( 'document.' + formName );
	var client = new Cookie(document, 'eq_library', 1440, '/');  // create cookie
	
	// load data and if the client is different from the stored one, clear all fields
	if ( client.load() && clientForm.FromName )	{
		if (client.name != clientForm.FromName.value)	{
			client.name = '';
			client.fname = '';
			client.my_title = '';
			client.work = '';
			client.position = '';
			client.email = '';
			client.address = '';
			client.shortadd = '';
			client.pcode = '';
			client.phone = '';
			client.fax = '';
			client.number = '';		// video borrower number
		}
	}
	
	// determine if client data is private
	var isPrivate;	
	if (clientForm.priv)	
		if (clientForm.priv.checked) isPrivate = false;
		else isPrivate = true;
	else isPrivate = true;	
		
	// if not private, store client data
	if (!isPrivate)	{
		if (clientForm.FromName) client.name = clientForm.FromName.value;
		if (clientForm.fname) client.fname = clientForm.fname.value;
		if (clientForm.my_title) client.my_title = clientForm.my_title.value;
		if (clientForm.work) client.work = clientForm.work.value;
		if (clientForm.position) client.position = clientForm.position.value;
		if (clientForm.FromEmail) client.email = clientForm.FromEmail.value;
		if (clientForm.address) client.address = clientForm.address.value;
		if (clientForm.shortadd) client.shortadd = clientForm.shortadd.value;
		if (clientForm.pcode) client.pcode = clientForm.pcode.value;
		if (clientForm.telephone) client.phone = clientForm.telephone.value;
		if (clientForm.fax) client.fax = clientForm.fax.value;
		if (clientForm.number) client.number = clientForm.number.value;
		client.priv = 'false';	
	}
	else {  // if private, erase any existing client data (if stored earlier)
		client.name = '';
		client.fname = '';
		client.my_title = '';
		client.work = '';
		client.position = '';
		client.email = '';
		client.address = '';
		client.shortadd = '';
		client.pcode = '';
		client.phone = '';
		client.fax = '';
		client.number = '';
		client.priv = 'true';
	}
	
	// store resource items data
	if (type == 'manual') client.items = readItems(formName);				// manual entry form				
	if (type == 'checkbox')	client.items = readSelected(formName);	// check box form	
	if (type == 'ja-checkbox') 
		client.items = readSelected(formName, 'ja');	// journal check box form
	if (type == 'vl-checkbox') 
		client.items = readSelected(formName, 'vl');	// video check box form
	
	client.store();		// save cookie
}

//	FUNCTION:			readClient						
//	DESCRIPTION:	Reads a cookie and prefills client data into a form			
// 	ARGUMENTS:		Name of form		
//	RETURNS:			nothing
function readClient(formName)	{
	var clientForm = eval( 'document.' + formName );
	var client = new Cookie(document, 'eq_library');
	
	// if cookie has data, insert fields on form	
	if ( client.load() )	{
		if (client.name && clientForm.FromName) clientForm.FromName.value = client.name;
		if (client.fname && clientForm.fname) clientForm.fname.value = client.fname;
		if (client.my_title && clientForm.my_title) clientForm.my_title.value = client.my_title;
		if (client.work && clientForm.work) clientForm.work.value = client.work;
		if (client.position && clientForm.position) clientForm.position.value = client.position;
		if (client.email && clientForm.FromEmail) clientForm.FromEmail.value = client.email;
		if (client.address && clientForm.address) clientForm.address.value = client.address;
		if (client.shortadd && clientForm.shortadd) clientForm.shortadd.value = client.shortadd;
		if (client.pcode && clientForm.pcode) clientForm.pcode.value = client.pcode;
		if (client.phone && clientForm.telephone) clientForm.telephone.value = client.phone;
		if (client.fax && clientForm.fax) clientForm.fax.value = client.fax;
		if (client.number && clientForm.number) clientForm.number.value = client.number;
		if (client.priv == 'true' && clientForm.priv)	clientForm.priv.checked = false;
	}
}
/*
//	FUNCTION:			outputItems						
//	DESCRIPTION:	Outputs list of stored client items			
// 	ARGUMENTS:		Name of form		
//	RETURNS:			nothing
function outputItems()	{
	var client = new Cookie(document, 'eq_library');
	client.load();
	if (client.items)
	document.write('<ul class="resource">' + client.items + '</ul>');
}*/
// -->