/*
 * Team 1 Project - Palo Alto Bicycle Club
 * Author: Archana Nagarkatti
 * Class: COIN 71
 * Filename: announcement.js 
 * Date Created: 3/15/2009
 * Functionality: List all the announcements.
 * Modification History:
 *      Cleaned  by Tyler,  3/22/09
 */


//builds the list of announcements by accessing the responseXML                
function buildList () {	
    responseXML = this.req.responseXML.documentElement;    
    listAnnouncements(responseXML);    
}


/*
 * lists all the previously posted announcements by 
 * pulling it from announcements.xml file.
 * The most recent announcements are listed at the top.
 */ 
function listAnnouncements(resp){
    //get announcement div node
	var announcementNode = document.getElementById("announcement");

    var array = resp.getElementsByTagName('announcement');	
	for (i=array.length-1;i>=0; --i)
	{
		$title = array[i].getElementsByTagName('title')[0].firstChild.nodeValue ;
		$author = array[i].getElementsByTagName('author')[0].firstChild.nodeValue ;
		$postdate = array[i].getElementsByTagName('postdate')[0].firstChild.nodeValue ;
		$message = array[i].getElementsByTagName('message')[0].firstChild.nodeValue ;
		
		addElement(announcementNode, "h4", $title);				
		addElement(announcementNode, "p", $message);	
		addElement(announcementNode, "br", "");
		addElement(announcementNode, "i", "Posted By: ");
		addElement(announcementNode, "label", $author);
		addElement(announcementNode, "i", " On: ");
		addElement(announcementNode, "label", $postdate);	
	}	
}


//adds elements for various attributes to the announcement parent node
function addElement(parentNode, element, text){
	var childEl = document.createElement(element);
	if (text != ""){
		childEl.appendChild(document.createTextNode(text));
	}
	parentNode.appendChild(childEl);
}


// validate entries in the "Post A New Announcement" form for length.
function validateAnnouncement()
	{
		var errorMessage = "";
		var titleValue = document.getElementById('title').value;
		var authorValue = document.getElementById('author').value;		
		var msgValue = document.getElementById('message').value;
		//title, author, message should not be empty
		if (titleValue.length < 1 || authorValue.length < 1 || msgValue.length < 1 )
		{
			errorMessage = "All fields should have a value to post an announcement.\n";
		}
		
		//message
		if(msgValue.length > 300)
		{
			errorMessage += 'Message length should be no greater than 300 characters.';
		}	
		if (errorMessage.length > 0)
		{
			alert (errorMessage);
			return false;
		}
	}