var attachments_count = 0;
var busyIndexes = new Array();

Array.prototype.removeItem = function (s) {
	for (i = 0; i < this.length; i++) {
		if (s == this[i])
			this.splice(i, 1);
	}
}

function GetNextAvailableIndex() {
	var isBusy = 0;

	for (i = 0; i < max_attachments_count; i++) {
		isBusy = 0;
		for (j = 0; j < busyIndexes.length; j++) {
			if (busyIndexes[j] == i)
				isBusy = 1;
		}

		if (isBusy == 0)
			return i;
	}

	return -1;
}

function OnGetFocusForFileDescription(e) {
	if (e.target.value == 'type file description')
		e.target.value = '';
}

//add file attachment form and associated elements
function AddAttachment() {
	var new_path_label = document.createElement('span');
	var index = GetNextAvailableIndex();

	var attachDiv = document.createElement('div');
	attachDiv.setAttribute('id', 'attachDiv' + index);
	attachDiv.style.display = 'none';
	attachDiv.className = 'pb-5';

	new_path_label.setAttribute('id', 'child_attachment_path_label_' + index);
	document.getElementById('attachments_content').appendChild(new_path_label);

	//create new <input> element
	var new_attachment = document.getElementById(fileUploads[index]);
	//create new <input> element
	if (new_attachment == null) {
		new_attachment = document.createElement('input');
		new_attachment.type = 'file';
		new_attachment.setAttribute('id', fileUploads[index]);
		new_attachment.setAttribute('name', fileUploadNames[index]);
	}

	attachDiv.appendChild(new_attachment);
	new_attachment.style.display = '';
	
	//set element type
	if (new_attachment.getAttribute('type') == null || new_attachment.getAttribute('type') != 'file') { // IE8 feature
		new_attachment.setAttribute('type', 'file');
	}

	//set element size
	new_attachment.setAttribute('size', '40');

	//create new <input> element for text
	var new_text = document.createElement('input');

	if (document.getElementById(fileDescriptions[index]) != null)
		new_text = document.getElementById(fileDescriptions[index]);

	//give element an id
	new_text.setAttribute('id', fileDescriptions[index]);
	new_text.setAttribute('name', fileDescriptionsText[index]);
	new_text.setAttribute('value', 'type file description');
	if (new_text.getAttribute('type') == null || new_text.getAttribute('type') != 'text') { // IE8 feature
		new_text.setAttribute('type', 'text');
	}
	new_text.setAttribute('size', '50');
	new_text.setAttribute('style', 'margin-left:10px');

	$addHandler(new_text, 'focus', OnGetFocusForFileDescription);

	//append newly created element to <span id="content"> tree
	//document.getElementById('attachments_content').appendChild(new_text);
	attachDiv.appendChild(new_text);
	new_text.style.display = '';

	//create new <span> element
	new_text = document.createElement('span');
	//give element an id
	new_text.setAttribute('id', 'child_attachment_text_' + index);
	//set element HTML to produce 'remove' text link
	new_text.innerHTML = '&nbsp;<a href="javascript:RemoveAttachment(' + index + ');" class="removeBtn"></a><br />';
	//append newly created element to <span id="content"> tree

	//increase the form count
	attachments_count++;

	busyIndexes.push(index);

	//document.getElementById('attachments_content').appendChild(new_text);
	attachDiv.appendChild(new_text);
	document.getElementById('attachments_more').innerHTML = 'Attach another file';

	if (max_attachments_count == attachments_count) {
		document.getElementById('maximumExceeded').innerHTML = "The attachments count is limited to " + max_attachments_count;
		document.getElementById('attachments_more').style.display = 'none';
	}
	document.getElementById('attachments_content').appendChild(attachDiv);
	attachDiv.style.display = '';
	InsureSubmitButton();
}

function ProcessAttachment(new_attachment, new_path_label, new_text) {
	new_path_label.innerHTML = new_attachment.value;

	if (new_attachment.value != '')
		document.getElementById('attachments_content').appendChild(new_text);

	if (new_attachment.value != '')
		document.getElementById('attachments_more').innerHTML = 'Attach another file';

	if (new_attachment.value == '') {
		attachments_count--;

		document.getElementById('attachments_content').removeChild(new_attachment);
		document.getElementById('attachments_content').removeChild(new_path_label);

		if (attachments_count == 0)
			document.getElementById('attachments_more').innerHTML = 'Attach a file';
	}
}

//remove file attachment form and associated elements
function RemoveAttachment(remove_form_num) {
	//decrease the form count
	attachments_count--;

	//remove <input> element attachment
	var attachDiv = document.getElementById('attachments_content').querySelector('#attachDiv' + remove_form_num);
	document.getElementById('attachments_content').removeChild(attachDiv);
	//remove <span> element path
	document.getElementById('attachments_content').removeChild(document.getElementById('child_attachment_path_label_' + remove_form_num));

	//if all forms are removed, change text back to "Attach a file"
	if (attachments_count == 0)
		document.getElementById('attachments_more').innerHTML = 'Attach a file';

	document.getElementById('attachments_more').style.display = '';
	document.getElementById('maximumExceeded').innerHTML = '';

	busyIndexes.removeItem(remove_form_num);
	InsureSubmitButton();
}

function InsureSubmitButton() {
	var button = document.getElementById(_submitButtonID);
	if (button) {
		button.style.display = busyIndexes.length > 0 ? '' : 'none';
	}
}

function IsIE() {
	version = 0;
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		temp = navigator.appVersion.split("MSIE");
		version = parseFloat(temp[1]);
	}
	return version >= 5.5;
}

