
var pageLoading = "<h5 style='padding: 15px;'><img src='/shared/images/icons/loading_medium.gif'> Loading</h5>";
var searchingWait = "<h5 style='padding: 15px;'><img src='/shared/images/icons/loading_medium.gif'> Searching</h5>";

function showDetails(id, jid) {
	h=document.getElementById('help');
	h.style.visibility='visible';	
	qstr = 'action=d7_thread_file_details&File_ID='+id+'&Join_ID='+jid;
	xmlhttpPost("/index.html?action=ajax", qstr , "gotDetails" );
	t=document.getElementById('taskDetails');
	t.innerHTML = pageLoading;
}
function gotDetails(detailHTML) {
	var theDiv = document.getElementById('taskDetails');
	theDiv.innerHTML = '';
	theDiv.innerHTML = detailHTML;	

}
var join_id;
function doFileAction(item) {
	var t = item[item.selectedIndex].value;
	items = formData2QueryString(document.fileForm);	
	if (t == 'unselect') {
		qstr = 'action=d7_thread_file_unselect&Join_ID='+join_id+'&'+items;
		xmlhttpPost("/index.html?action=ajax", qstr , "unselected" );
		docForm = document.fileForm;
		for (i = 0; i < docForm.elements.length; i++) {
			formElem = docForm.elements[i];
			switch (formElem.type) {
				case 'checkbox':
					if (formElem.checked) {	
					
						var row = document.getElementById('result_'+formElem.value);					
						if (row) {
							new Effect['Fade'](row);
						}
						//alert('delet ' + formElem.value);
					}
				break;
			}
		}
	}
	else if (t == 'ownership') {
		qstr = 'action=d7_thread_file_change_ownership&Join_ID='+join_id+'&'+items;
		xmlhttpPost("/index.html?action=ajax", qstr , "ownershipChanged" );
		docForm = document.fileForm;
		for (i = 0; i < docForm.elements.length; i++) {
			formElem = docForm.elements[i];
			switch (formElem.type) {
				case 'ownership':
					if (formElem.checked) {	
					
						var row = document.getElementById('result_'+formElem.value);					
						if (row) {
							alert(row)
							//new Effect['Fade'](row);
						}
						//alert('delet ' + formElem.value);
					}
				break;
			}
		}
	}
	else if (t == 'delete') {
		h=document.getElementById('help');
		h.style.visibility='visible';	
		var theDiv = document.getElementById('taskDetails');
		theDiv.innerHTML = "Checking for Delete Conflicts..."
		qstr = 'action=d7_thread_file_delete_check&Join_ID='+join_id+'&'+items;
		xmlhttpPost("/index.html?action=ajax", qstr , "deleteCheck" );
	}
	else if (t == 'select') {
		items = formData2QueryString(document.fileForm);
		qstr = 'action=d7_thread_file_select&Join_ID='+join_id+'&'+items;
		//prompt('test',qstr);
		
		xmlhttpPost("/index.html?action=ajax", qstr , "didSelected" );
	
		item.selectedIndex = 0;
	}
	else if (t == 'tags') {
		//delete selectedTags;
		selectedTags = "";
		var selectedTags = new Array();
		//alert(selectedTags[0]);
		var i = document.getElementById('nTags');
		i.value = "";
		items = formData2QueryString(document.fileForm);
		qstr = 'action=d7_thread_file_edit_tag_confirm&Join_ID='+join_id+'&'+items;
		xmlhttpPost("/index.html?action=ajax", qstr , "confirmEdit" );
	
		item.selectedIndex = 0;
	}
	
	item.selectedIndex = 0;
	// Do some AJAX here
}

function confirmEdit(inStr) {
	h=document.getElementById('help');
	h.style.visibility='visible';	
	var theDiv = document.getElementById('taskDetails');
	theDiv.innerHTML = inStr;
}

function doTagEdit() {
	items = formData2QueryString(document.fileForm);
	qstr = 'action=d7_thread_file_edit_file_tags&Join_ID='+join_id+'&'+items;
	xmlhttpPost("/index.html?action=ajax", qstr , "finishTagUpdate" );
	var theDiv = document.getElementById('taskDetails');
	theDiv.innerHTML = 'Updating Tags, Please wait...';
}

function finishTagUpdate(inStr) {
	//var theDiv = document.getElementById('taskDetails');
	//theDiv.innerHTML = inStr;
	h=document.getElementById('help');
	h.style.visibility='hidden';	
	document.location.reload();
}

function deleteCheck(inStr) {
	h=document.getElementById('help');
	h.style.visibility='visible';	
	var theDiv = document.getElementById('taskDetails');
	theDiv.innerHTML = inStr;
}

function doDelete() {
	items = formData2QueryString(document.fileForm);	
	qstr = 'action=d7_thread_file_do_delete&Join_ID='+join_id+'&'+items;
	xmlhttpPost("/index.html?action=ajax", qstr , "doneDelete" );	
	var theDiv = document.getElementById('taskDetails');
	theDiv.innerHTML = 'Deleting, Please wait...';
}
function doneDelete() {
	h=document.getElementById('help');
	h.style.visibility='hidden';	
	docForm = document.fileForm;
	for (i = 0; i < docForm.elements.length; i++) {
		formElem = docForm.elements[i];
		if (formElem.checked) {	
			
			var row = document.getElementById('result_'+formElem.value);					
			if (row) {
				new Effect['Fade'](row);
			}
		}
	}

}

var sA = new Array();
var selectedTags = new Array();
function toggleTagRow(id, variable, inpt) {
	//alert(id);
	if (!variable) {
		variable = sA;
	}
	var row = document.getElementById(id);
	if (row) {
		//alert('have row ' + row + ", "+ sA + ", "+ id + ',' + row.style.backgroundColor);
		var a = id.split('_');
		var weDeleted;
		for (i = 0; i < variable.length; i++) {
				if (variable[i] == a[1]) {
					variable.splice(i,1);				
					row.style.backgroundColor = 'rgb(255,255,255)';										
					row.style.borderBottom	= '1px solid #ccc';					
					weDeleted = 1;
				}	
			}
		if (!weDeleted) {
			row.style.backgroundColor = 'rgb(173,210,161)';			
			row.style.borderBottom	= '1px solid #fff';
			var l =  variable.length;
			if (l < 1) {
				l='0';
			} 
			variable[l] = a[1];						
		}	
		
		getTags(variable, inpt);

		var i = document.getElementById(inpt);

	}		

}

function getTags(variable, inpt) {
	var tags = "";
	if (!variable) {
		variable = sA;
	}
	if (!inpt) {
		inpt = 'formTags';
	}
	for (i = 0; i < variable.length; i++) {
		tags += variable[i] + "|";
	}
	var i = document.getElementById(inpt);
	i.value = tags;
	return tags;
}