// JavaScript Document

var SAVE_PREFIX = 'document_submit.php';
var DELETE_PREFIX = 'delete_document.php?';
//var situation = new definition(); // to keep track where we are in the application

function save_doc(id_doc) {
	var form_doc = 'form_document';
	var doc_title= document.forms[form_doc].elements['doc_title'].value;
	
	if (doc_title != '') {
	var http_save = new Asynchronous( ); // We create the HTTP Object
//	document.forms[form_doc].submit();
		// data treatment for the indexterms:
		var box1 = document.forms[form_doc].indexterm_select;
		var box1_content = '';
		for (i=0;i<box1.length;i+=1)
		{
			box1_content += box1.options[i].value+',';
		}
		if(box1_content.length>0){box1_content = box1_content.substr(0,box1_content.length-1);}
		document.forms[form_doc].indexterm_after.value = box1_content;
		
		//save
//	alert(doc_title);
	if(document.forms[form_doc].elements['doc_status'].checked == true) {var doc_status = encodeURIComponent('public');} else {var doc_status = encodeURIComponent('pending');};
//	alert(doc_status);
	var doc_web= encodeURIComponent(document.forms[form_doc].elements['doc_web'].value);
//	alert(doc_web);
	var doc_category_id= encodeURIComponent(document.forms[form_doc].elements['doc_category_id'].value);
//	alert(doc_category_id);
	var indexterm_before= encodeURIComponent(document.forms[form_doc].elements['indexterm_before'].value);
//	alert(indexterm_before);
	var indexterm_after= encodeURIComponent(document.forms[form_doc].elements['indexterm_after'].value);
//	alert(indexterm_after);
	var doc_precis= encodeURIComponent( document.forms[form_doc].elements['doc_precis'].value );
//	alert(doc_precis);
	var doc_text= encodeURIComponent( document.forms[form_doc].elements['doc_text'].value );
//	alert(doc_text);
	var parameters = "id="+id_doc+"&title="+doc_title+"&status="+doc_status+"&web="+doc_web+"&category_id="+doc_category_id+"&indexterm_before="+indexterm_before+"&indexterm_after="+indexterm_after+"&precis="+doc_precis+"&text="+doc_text;
	//alert(parameters);
	http_save.call('POST', SAVE_PREFIX, parameters);
	http_save.complete = function(status, statusText, responseText, responseXML) {//alert("line 87");
		if (responseText != 0) {		//chris
			if ($(".other_pages").get(0) != null){
				// Case when creating a doc from the home page
				$(".other_pages").empty().append("document was saved successfully...");
				compose_page('home.php','centrecontent');return;
			} else {alert("Document Edited and Saved");return;
				compose_document('view', responseText);
			}
			__FCKeditorNS = null;
		}	
	}

	} else {
	alert('please enter a valid title for the document');
	}

}


function delete_doc(id_doc)
{
	if (id_doc != 0) {
		 if (confirm("Are you sure you want to delete this document ?") == true) {
			var http_delete = new Asynchronous( ); // We create the HTTP Object
			http_delete.call('GET', DELETE_PREFIX+'id='+id_doc);
			http_delete.complete = function(status, statusText, responseText, responseXML) {
				list_all_documents();
				doc_selected = '';
			}
		 }
	}
}
