	var itemID;
	
	var returnLayerID;
	
	// did you know that the handy left/right fns in asp have no equivalent in javascript??
	// well ... here's my version :)
	
	function Left(str, n){
		if (n <= 0)
		    return "";
		else if (n > String(str).length)
		    return str;
		else
		    return String(str).substring(0,n);
	}

	function Right(str, n){
	    if (n <= 0)
	       return "";
	    else if (n > String(str).length)
	       return str;
	    else {
	       var iLen = String(str).length;
	       return String(str).substring(iLen, iLen - n);
	    }
	}

	//find the current url of the site incl page
	var ajax_url = window.location.href;
	
	//cut out the http:// part ... which equals 7 characters .. if you use https:// then you
	// want to make sure you put in 8 characters here rather than 7
	var ajax_url_len = ajax_url.length - 7;
	
	//now strip out the http:// part 
	ajax_url = Right(ajax_url, ajax_url_len)
	
	// identify the first / which will be the .com/page.html part
	var where_is_slash=ajax_url.indexOf('/');

	// now cut out the /path.html .. etc .. part so we've just left with the site's URL
	ajax_url = 'http://'+ Left(ajax_url,where_is_slash);

	

	function editThisTopic(someID) {
		var agt = navigator.userAgent.toLowerCase();

		if(agt.indexOf("safari") != -1) {
			alert('FCKedior does not work with Safari\n\nFor more information see http://www.fckeditor.net/safari');		
		} else {
			//alert('I notice you\'re not using Safari. Good! We can make FCKeditor available to you!');
			makeRequest(ajax_url+'/tmengine/services/rest/widget.asp', 'mode=api&templateID=content&itemID='+someID, 'loadContent');
		}
	}


	//this completely removes any sign of this topic
	function deleteThisTopic(someID,someName) {
		var answer = confirm('Are you sure you want to delete '+ someName +'?\n\nYou can not undo this action!');
		if(answer) {
			alert('Deleting '+ someName +'!');
			makeRequest(ajax_url+'/tmengine/services/rest/widget-edit.asp', 'api=deleteTopic&itemID='+someID+'&itemName='+someName, 'deleteTopic');
		}
	}

	function deleteThisRelationship(someID,termID,termRelationship) {
		var answer = confirm('Are you sure you want to delete the relationship between '+ someID +' and '+ termID +'?\n\nYou can not undo this action!');
		if(answer) {
			//alert('Deleting the relationship between '+ someID +' and '+ termID +'!');
			makeRequest(ajax_url+'/tmengine/services/rest/widget-edit.asp', 'api=deleteRelationship&itemID='+ someID +'&termID='+termID+'&termRelationship='+termRelationship, 'deleteRelationship');
			returnLayerID = termID+termRelationship;
			
		}
	}


	function addTopicRelationship(someID) {
	
		var itemID2          = document.form.itemID2.options[document.form.itemID2.selectedIndex].value;
		var itemRelationship = document.form.itemRelationship.options[document.form.itemRelationship.selectedIndex].value;

		makeRequest(ajax_url+'/tmengine/services/rest/widget-edit.asp', 'api=addRelationship&itemID2='+itemID2+'&itemID1='+someID+'&relationshipType='+itemRelationship, 'addRelationship');
		
	}


	function setEditorValue(instanceName, text ) {  
		document.ajaxform.FCKeditor1.value = text;
	}  


	function showContentValue() {
		return FCKeditorAPI.GetInstance('FCKeditor1').GetXHTML(true);
	}


	function hideContentLayer(someID) {
		var someLayer = document.getElementById(someID);
		someLayer.style.display = 'none';
	}

	function showContentLayer(someID) {
		var someLayer = document.getElementById(someID);
		someLayer.style.display = 'block';
	}

 
	function replaceContentInLayer(id, content) {
		var someLayer = document.getElementById(id);
		someLayer.innerHTML = content;
	}

	function addContentToLayer(id, content) {
		x = document.getElementById(id);
		x.innerHTML = x.innerHTML + '<p>'+content+'</p>';
	}


	function alertContents(http_request) {
		if (http_request.readyState == 4) {
			// everything is good, the response is received        
			if (http_request.status == 200) {
				//alert('test1:'+http_request.responseText);
				return http_request.responseText;
			} else if(http_request.status == 500) {
				alert('500 Internal Server Error\nThe server encountered an unexpected condition which prevented it from fulfilling the request. ');
			} else if(http_request.status == 503) {
				alert('The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header. If no Retry-After is given, the client SHOULD handle the response as it would for a 500 response.');			
			} else if(http_request.status == 504) {
				alert('The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI (e.g. HTTP, FTP, LDAP) or some other auxiliary server (e.g. DNS) it needed to access in attempting to complete the request.	');		
			} else {
				//you can find more errors messages at:
				//http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
				alert('There was a problem with the request. Error code='+ http_request.status +'.');
			}
		} else {
			// still not ready 
			//alert('still not ready... waiting to receive msg from widget');       
		}
	}
	
    function makeRequest(url, parameters, passType) {
        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("MSXML4.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        
        http_request.onreadystatechange = function() { 

        	var strText = alertContents(http_request); 

        	if (strText == undefined) {
				//do something ... anything you like ... 
				//someMsg = document.createTextNode('.');
				//document.getElementById('story').appendChild(someMsg); 
        	} else {
        		if(passType == 'loadContent') {
        		
        			//dont want anyone doing any special actions now, do we!
					hideContentLayer('toolbar');
        		        		
					//need a reference point for these new layers?
					var targetLayer = document.getElementById('story');
					
					//create a msg layer
					var msgLayer = document.createElement('div');
					msgLayer.setAttribute('id', 'msgLayer');
					
					//stick the msg layer before the first child of the story layer
					//this is above where the story-content layer exists				
					targetLayer.insertBefore(msgLayer, targetLayer.firstChild);
	
					//create some text for our msg layer
					//var txt = document.createTextNode('Loading WYSIWYG editor...');
					//document.getElementById('msgLayer').appendChild(txt); 				
					//document.getElementById('msgLayer').innerHTML = 'Loading WYSIWYG editor...';
	
					//create a textarea layer
					var textarea = document.createElement('textarea');
					textarea.setAttribute('id', 'FCKeditor1');
					textarea.setAttribute('name', 'FCKeditor1');
	
					//create a submit button
					var someSubmitButton = document.createElement('button');
					someSubmitButton.setAttribute('id', 'submitAjaxForm');
					someSubmitButton.setAttribute('name', 'submitAjaxForm');
					someSubmitButtonTxt = document.createTextNode('Submit');
					someSubmitButton.appendChild(someSubmitButtonTxt);
					//someSubmitButton.addEventListener('click', submitFCKform, false);
					
					if(typeof someSubmitButton.addEventListener != 'undefined') {
					   someSubmitButton.addEventListener('click', submitFCKform, false);
					}
					else if(typeof someSubmitButton.addEventListener != 'undefined') {
					   someSubmitButton.addEventListener('click', submitFCKform, false);
					}
					else if(typeof someSubmitButton.attachEvent != 'undefined') {
					   someSubmitButton.attachEvent('onclick', submitFCKform);
					}				
	
					
					//create a cancel button
					var someCancelButton = document.createElement('button');
					someCancelButton.setAttribute('id', 'cancelAjaxForm');
					someCancelButton.setAttribute('name', 'cancelAjaxForm');
					someCancelButtonTxt = document.createTextNode('Cancel');
					someCancelButton.appendChild(someCancelButtonTxt);
					
					
					if(typeof someCancelButton.addEventListener != 'undefined') {
					   someCancelButton.addEventListener('click', cancelFCKform, false);
					}
					else if(typeof someCancelButton.addEventListener != 'undefined') {
					   someCancelButton.addEventListener('click', cancelFCKform, false);
					}
					else if(typeof someCancelButton.attachEvent != 'undefined') {
					   someCancelButton.attachEvent('onclick', cancelFCKform);
					}					
					
	
					//where we gonna put the textarea and the buttons?
					targetLayer.appendChild(textarea);
					//targetLayer.parentNode.appendChild(someSubmitButton);
					//targetLayer.parentNode.appendChild(someCancelButton);		
					
					targetLayer.appendChild(someSubmitButton);
					targetLayer.appendChild(someCancelButton);
			
					
					//set the value of the textarea
					textarea.value = strText;		
	
	
					//create fckeditor
					oFCKeditor = new FCKeditor('FCKeditor1');
					oFCKeditor.BasePath	= '/tmengine/assets/FCKeditor/' ;
					oFCKeditor.ReplaceTextarea();
				
					//now hide the original text layer
					hideContentLayer('story-content');
					
					//hide the loading msg info
					hideContentLayer('msgLayer');
					
				} else if (passType == 'replaceContent') {
					
					replaceContentInLayer('story', strText);
					
				} else if (passType == 'addRelationship') {
				
					addContentToLayer('xyzzy', strText);
				
				} else if (passType == 'deleteTopic') {
					alert(strText);
					window.location = "/";

				} else if (passType == 'deleteRelationship') {
					//this will effectively delete the content in this layer
					hideContentLayer(returnLayerID); 
					alert(strText);	

				} else {
				
					alert('Error: No action has been specified for the call to the widget');
				
				}
			}
	        	
        };

		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", parameters.length);
		http_request.setRequestHeader("Connection", "close");
		http_request.send(parameters);
		 
    }


	function submitFCKform() {

		var txtContent = showContentValue();
		
		itemID = document.form.itemID.value;
		
		if(txtContent.indexOf('<div id="story-content">') != 0) {
			txtContent = '<div id="story-content">'+ txtContent +'</div>';
		}

		//before we escape the string content from the editor, let's change some of the html characters into decimal entities
		txtContent = txtContent.replace('%','&#37;');
		txtContent = txtContent.replace(' & ', ' &#38; ');

		txtContent = escape(txtContent);
		replaceContentInLayer ('story', 'Saving...');
		makeRequest(ajax_url+'/tmengine/services/rest/widget-edit.asp', 'api=update&itemID='+itemID+'&FCKeditor1='+txtContent, 'replaceContent');		
		showContentLayer('toolbar');				
	}

	function cancelFCKform() {
		//return to the default state
		 window.location = ajax_url+'/tmengine/services/rest/widget.asp?itemID='+itemID+'&templateID=people';		
	}


