var _numVideos = 0;
var _numAudio = 0;
var _numDocuments = 0;

function addDocumentDownload(file){
    var insertInto = document.getElementById('documentDownloadTable');
    var parentObj = insertInto.parentNode;
    var newContent = document.createElement("input");
	var newContentPrimaryFlag = document.createElement("input");
	var	newContentReplyToFlag = document.createElement("input");
	var removeButton = document.createElement("img");
	var tbody = document.createElement("tbody");
	var tr = document.createElement("tr");
	var td1 = document.createElement("td");
	var td2 = document.createElement("td");
	var td3 = document.createElement("td");

	// set input field attributes
	newContent.setAttribute("type", "file");
	newContent.setAttribute("name", "fileDocument" + _numDocuments);
	newContent.setAttribute("id", "fileDocument" + _numDocuments);
	newContent.setAttribute("size", "30");

	// remove button
	removeButton.setAttribute("name", _numDocuments)
	removeButton.setAttribute("src", "public/images/delete_inline.gif");
	removeButton['onclick'] = function() {
	    var oNodeToRemove = document.getElementById('fileDocumentRow' + this.name);
	    oNodeToRemove.parentNode.removeChild(oNodeToRemove);
    }
    
    tr.setAttribute("id", "fileDocumentRow" + _numDocuments);
	
	td1.appendChild(newContent);
	td1.appendChild(document.createTextNode(" "));
	td1.appendChild(removeButton);

	tr.appendChild(td1);
	tr.appendChild(td2);
	
	tbody.appendChild(tr);
	
	insertInto.appendChild(tbody);
	
	// insert the new div->input into the DOM
	parentObj.insertBefore(document.getElementById('documentTargetBody'), insertInto);

	_numDocuments++;
}

function createSomething(something_div){
	var div = document.getElementById(something_div);
	
	if(div.style.display == 'none')
		div.style.display = 'block';
	else
		div.style.display = 'none';
}

function removeObject(module, action, event, id_name, id, loadFunction, divId, document_id){
	if(confirmDelete()){
		var callback =	{
			success: function(data) {
				retrieveData(module, loadFunction, divId, document_id);
			},
			failure: function(data) {
				retrieveData(module, loadFunction, divId, document_id);
			}		  
		}
		postData = 'module='+module+'&action='+action+'&event='+event+'&'+id_name+'=' + id;
		var cObj = YAHOO.util.Connect.asyncRequest('POST','index.php', 
		callback, postData);
	}
}

function retrieveData(module, loadFunction, divId, document_id){
	var callback =	{
		success: function(data) {
			var div = document.getElementById(divId);
			div.innerHTML = data.responseText;
		},
		failure: function(data) {
			
		}		  
	}
	postData = 'module='+module+'&action=view&id='+document_id+'&event='+loadFunction;
	var cObj = YAHOO.util.Connect.asyncRequest('POST','index.php', 
	callback, postData);
}

function write_note(divId, description){
	var div = document.getElementById("note_"+divId);
	var textarea = document.createElement("textarea");

	textarea.innerHTML = description;
	textarea.setAttribute('id', 'text_'+divId);
	textarea.setAttribute('ONBLUR', 'javascript:save_note("'+divId+'");');
	textarea.setAttribute('onkeypress', 'javascript:save_note_keypress("'+divId+'", event);');
	div.innerHTML = '';
	div.appendChild(textarea);
	div.setAttribute("onclick", "");
	textarea.focus();
}

function save_note_keypress(divId, event){
	if(event.keyCode == 13){
		save_note(divId);
	}
}

function save_note(divId){
	var textarea = document.getElementById('text_'+divId);
	var description = textarea.value;
	var callback =	{
		success: function(data) {
			var div = document.getElementById("note_"+divId);
			div.setAttribute("onclick", 'javascript:write_note("'+divId+'", "'+description+'");');
			div.removeChild(textarea);
			if(description == ''){
				description = '<i>[click to add note]</i>';
			}
			div.innerHTML = description;
		},
		failure: function(data) {
			var div = document.getElementById("note_"+divId);
			div.setAttribute("onclick", 'javascript:write_note("'+divId+'", "'+description+'");');
			div.removeChild(textarea);
			if(description == ''){
				description = '<i>[click to add note]</i>';
			}
			div.innerHTML = description;
		}		  
	}
	postData = 'module=documents&action=view&revision_id='+divId+'&event=save_note&description='+description;
	var cObj = YAHOO.util.Connect.asyncRequest('POST','index.php', 
	callback, postData);
}

function share_document(){
	var formObject = document.getElementById('share_form'); 
	if(typeof formObject != 'undefined') {
		YAHOO.util.Connect.setForm('share_form', false); 
	    var cObj = YAHOO.util.Connect.asyncRequest('POST','index.php?module=documents&action=view&event=share',
	    {
	     success: function(data) {
	     	createSomething('share');
	     	alert(data.responseText);
	     	}, 
	          failure: function(data) { }
	         }
	        );
        }
}

