	// define the content directory relative from the php functions mkdir.php and ls.php
	
	var base_dir = '/mc/content/';
	var content_directory = '/WWW/k/kembelpa/htdocs/mc/content/';
	var lib_dir = '/doxa/';
	// TinyMCE editor functions
	
	current_filename = null;
	current_directory = null;

	var isSaving = false;

	// TODO	
	// combine these functions in the index.php
	// add a security check. 

	function login(user, pass) {
                var callback =
                        {
                                success: function(o) {
                                        var response = o.responseText;
                                        if (response == user && user!='') {
                                              //  $('login').style.display = 'none';
                                              //  $('logged_in').style.display = 'block';
                                                // show user id somewhere

                                                current_user = user;
                                                document.location = 'sitemap.php';

                                        } else {
                                                current_user = null;

                                              //  $('login').style.display = 'block';
                                              //  $('logged_in').style.display = 'none';
                                              //  document.forms.login_form['username'].value = '';
                                              //  document.forms.login_form['password'].value = '';
                                        }
                                },
                                failure: function(o) {
                                        alert("Login error");
                                }
                        };
                var cObj = YAHOO.util.Connect.asyncRequest('GET', 'proof.php?1=' + user + '&2=' + pass, callback);
        }


	function loadEditor(template){
		var anchor = getAnchor();	
		if (anchor != null && anchor != '' ) {
			// the file can be loaded by providing a full filename, the relative path in the content directory or the rdf identifier
			if (anchor.indexOf('doxa_') == -1){
				var callback =
				{
		  		success: function(o) {
					var ids = o.responseText.split('\n');
					setName(ids[0]);
					loadFile(current_filename);
				},	
		  		failure: function(o) {alert('Er is een fout opgetreden.');}
				};

				var url = 'id.php?' + anchor ;	
				var cObj = YAHOO.util.Connect.asyncRequest('GET', url, callback);
			} else {
				// the passed argument is the name of the directory
				setName(content_directory + anchor + '/index.html');
				loadFile(current_filename);
			}
		} else {
			// load the template instead
			
			loadFile(template);
		}
	}

	function setName(name){
		current_filename = name;
		if (current_filename.indexOf('file://') > -1){
			current_filename = current_filename.substr(7);
		}
		// first, strip the filename	
		current_directory = current_filename.substr(0,current_filename.lastIndexOf('/index.html'));
		
		// then, strip everything before the base_dir
		current_directory = current_directory.substr(current_filename.indexOf(base_dir));

		tinyMCE.activeEditor.documentBaseURI.setPath(current_directory);

	}

	function loadFile(name){
		var ed = tinyMCE.activeEditor;
		ed.setProgressState(1); // Show progress
		var request = YAHOO.util.Connect.asyncRequest('POST', lib_dir + 'load.php?filename=' + name,
		{
		success:function(o){
			ed.setProgressState(0); // Show progress
			ed.setContent(o.responseText);
		},
		failure: function(o){
			ed.setProgressState(0); // Show progress
			alert("load failed!");
		}
		});
	}

	function validate_urls(html_string) {
		// This function checks whether HTML elements of class
		// dc-identifier in html_string are well-formed i.e. that
		// their innerText values don't contain characters other than
		//
		// [a-z][A-Z][0-9][-$_.+/]
		//
		// Return value: null or the last found invalid character
		//
		// TODO:	This function currently only works on the first tag
		// 		tag in the html_string...

		reg_match_element = /<([^ >]+) class="dc-identifier"[^>]*>((?:([^a-zA-Z0-9$\-_.+\/])|[^<])*?)<\/\1>/;
		my_matches = reg_match_element.exec(html_string);

		if (my_matches[3]==undefined) {
			return null;
		} else {
			return my_matches[3];
		}
	}

	function save(){
			if (isSaving == false){
				isSaving = true;	
				var ed = tinyMCE.activeEditor;
				ed.setProgressState(1); // Show progress
				
				var content = ed.getContent();

				my_regex = /<([^ >]+) class="dc-identifier"[^>]*>((?:([^a-zA-Z0-9$\-_.+\/])|[^<])*?)<\/\1\>/;
				my_matches = my_regex.exec(content);
				if (my_matches[3]==undefined){

					// don't allow any other headers than the erdf one...
					
					if (content.search('<head profile="http://purl.org/NET/erdf/profile">') == -1){

						head = "<head profile='http://purl.org/NET/erdf/profile'>\n";  
						head +=	"	<link rel='schema.dc' href='http://purl.org/dc/terms/' />\n";  
						head +=	"	<link rel='schema.cc' href='http://www.cleancode.nl/schema/cc/' />\n";   
						head +=	"	<link rel='schema.foaf' href='http://xmlns.com/foaf/0.1/' />\n";
						head +=	"</head>\n";

						content = content.replace(/<head(.*\n)*<\/head>\n/,head);
						ed.setContent(content);
					}
					
					var frm=document.forms.dlgForm;
					frm.content.value = content;
					if (current_filename != null || current_filename != ''){
						frm.filename.value = current_filename;
					} else {  
						frm.filename.value = '';
					}
		
					YAHOO.util.Connect.setForm(frm);
					
					var request = YAHOO.util.Connect.asyncRequest('POST', lib_dir +'save.php',
					{
						success:function(o){
							ed.setProgressState(0); // Hide progress
							isSaving = false;
							setName(o.responseText.trim());
						},
						failure: function(o){
							ed.setProgressState(0); // Hide progress
							isSaving = false;	
							alert("upload failed!");
						}
					});

				} else {
					ed.setProgressState(0); // Hide progress
					isSaving = false;	
					alert('the identity contains invalid characters. Please correct them.');
				}
			}
		}	

	// Image Dialog functions

	function init_image_browser(){
		if (tinyMCEPopup && tinyMCEPopup.editor && tinyMCEPopup.editor.documentBaseURI.file != ''){
			loadAttachments();
			$('add_image_link').style.display = 'block';
			$('save_message').style.display = 'none';
		} else {
			$('add_image_link').style.display = 'none';
			$('save_message').style.display = 'block';
		}
	}

	function newAttachment(){
		$('attachmentDlg').style.display = 'block';
		var frm=document.forms.AttachmentForm;
		frm.upload.value = '';
	}

	function cancelAttachment(){
		$('attachmentDlg').style.display = 'none';
	}

	function addAttachment()
	{
		var frm=document.forms.AttachmentForm;
				
		frm.dir.value = content_directory + tinyMCEPopup.editor.documentBaseURI.file;

		YAHOO.util.Connect.setForm(frm, true);	// second parameter (true) indicates this form has file uploads
		var request = YAHOO.util.Connect.asyncRequest('POST', lib_dir +'upload.php',
		{
			upload:function(o){
				
				if(o.responseText=='')
				{				
					alert('Upload failed');
					return;
				}

				addAttachmentToList(o.responseText);
				cancelAttachment();
			},
			failure: function(o){
				alert("upload failed!");
				cancelAttachment();
			}
		});
	}

	function deleteAttachment(filename)
	{
		var callback=
		{
		failure: function(o){},
		success: function(o){
			var resp=o.responseText;
			if(resp!='')
				alert(resp);	// resp should contain an error message, or nothing when all is good
			
			removeAttachmentFromList(filename);
		}};

		var cObj = YAHOO.util.Connect.asyncRequest('GET', lib_dir +
		'delete.php?id='+ content_directory + '/' + tinyMCEPopup.editor.documentBaseURI.file  + '&filename=' + filename, callback);
	}


	function selectAttachment(filename) {
		$('src').value = filename;
		ImageDialog.showPreviewImage(filename);
	}

	function removeAttachmentFromList(filename)
	{
		// get main attachment list element
		var el=document.getElementById('attachmentlist');
		if(el)
		{
			// loop all childnodes (li items)
			for(var i=0; i<el.childNodes.length; i++)
			{
				// this is the one?
				if(el.childNodes[i].id==filename)
				{
					// remove and done
					el.removeChild(el.childNodes[i]);
					return;
				}
			}
		}
	}

	function addAttachmentToList(filename)
	{
		var el=document.getElementById('attachmentlist');
		if(el)
		{
			var elitem=NN('li', null, filename+' ', el);
			elitem.id=filename;
		
			var selectel=NN('a', null, '[select]', elitem);
			selectel.href="javascript:selectAttachment('"+filename+"');";

			var eldelete=NN('a', null, '[delete]', elitem);
			eldelete.href="javascript:deleteAttachment('"+filename+"');";
		}
	}

	function loadAttachments()
	{
		var callback =
		{
	  	success: function(o) {
			var response=o.responseText;
			
			// find form element to display the list in
			var el=document.getElementById('attachmentlist');
		
			// first make sure the list is cleared: remove previous list
			if (el){
				while (el.firstChild){
					el.removeChild(context.firstChild);
				}
			}
			
			// process list
			var list=response.split('\n');
			for(var i=0; i < (list.length - 2); i++)
			{
				if(list[i]!='' && list[i] != 'index.html' && list[i] != 'index.html.rdf' && list[i] != 'index.html.tmp')
				{
					// create list item
					var itemel=NN('li', null, list[i]+' ', el);
					itemel.id=list[i];
					
					var selectel=NN('a', null, '[select]', itemel);
					selectel.href="javascript:selectAttachment('"+list[i]+"');";

					var deleteel=NN('a', null, '[delete]', itemel);
					deleteel.href="javascript:deleteAttachment('"+list[i]+"');";
				}
			}
		},
		failure: function(o) {
		}};
		
		var cObj = YAHOO.util.Connect.asyncRequest('GET',  lib_dir +
		'ls.php?currentdir=' + content_directory +  tinyMCEPopup.editor.documentBaseURI.file  +  '&query=*', callback);
	}


function describe(id, link) {

	if (link.nextSibling.style.display == 'none'){
		var callback =
		{
	  	success: function(o) {
			// instead of assuming o.responseText to be html,
			// we will start to require JSON strings, and
			// handle these here.
			link.nextSibling.innerHTML = o.responseText;
			link.nextSibling.style.display = 'block';	
		},
		failure: function(o) {
		}};
		
		var cObj = YAHOO.util.Connect.asyncRequest('GET',  lib_dir +
		'describe.php?' + id , callback);
	} else {
		link.nextSibling.style.display = 'none';	
	}	
}

