// JavaScript Document
// Gmail File Attachment Clone
// (c) 2005, Gavin Lynch

//number of forms currently in < span id="content" > tree
var form_count = 0;

//add file attachment form and associated elements
function add()
{
	//create new < img > element
//	var new_img = document.createElement('img');
	//give element an id
//	new_img.setAttribute('id', 'child_attachment_img_' + form_count);
	//set image source
//	new_img.setAttribute('src','images/layout/file.png');
	//set image alternative text
//	new_img.setAttribute('alt',' ');
	//set image stylings
//	new_img.setAttribute('style', 'float: left;');
	//append newly created element to < span id="content" > tree
//	document.getElementById('files').appendChild(new_img);

	//create new < span > element
	var new_text = document.createElement('span');
	//give element an id
	new_text.setAttribute('id','child_attachment_text_' + form_count);
	//set element HTML to produce 'remove' text link 
	new_text.innerHTML = ' <br /><span class="remove" onclick="remove(' + form_count + ');"> remove</span>';
	//append newly created element to < span id="content" > tree
	document.getElementById('files').appendChild(new_text);

	//create new < input > element
	var new_attachment = document.createElement('input');
	//give element an id
	new_attachment.setAttribute('id', 'child_attachment_' + form_count);
	//set element type
	new_attachment.setAttribute('type', 'file');
	//set element class
	new_attachment.setAttribute('class', 'gFile');
	//set element size
	new_attachment.setAttribute('size', '35');
	//set element name
	new_attachment.setAttribute('name', 'child_attachment_' + form_count);
	//append newly created element to < span id="content" > tree
	document.getElementById('files').appendChild(new_attachment);

	//increase the form count
	form_count++;

	//if an attachment has been added, change text to "Attach another file"
	document.getElementById('more').innerHTML = 'Attach another file';
 } 

//remove file attachment form and associated elements
function remove(remove_form_num)
{
	//decrease the form count
	form_count--;

	//remove < input > element attachment
	document.getElementById('files').removeChild(document.getElementById('child_attachment_' + remove_form_num));
	//remove < span > element text
	document.getElementById('files').removeChild(document.getElementById('child_attachment_text_' + remove_form_num));
	//remove < img > element image
//	document.getElementById('files').removeChild(document.getElementById('child_attachment_img_' + remove_form_num));

	//if all forms are removed, change text back to "Attach a file"
	if (form_count == 0)
	{
     	  	document.getElementById('more').innerHTML = 'Attach a file';
	}
}
