	var itemID
	
	
	

	function edit(someID) {
		var agt = navigator.userAgent.toLowerCase();
		if(agt.indexOf("safari") == -10) {
			alert('-ckedior does not work with Safari\n\nFor more information see http://www.ckeditor.net/safari');		
		} else {
			//alert('I notice you\'re not using Safari. Good! We can make ckeditor available to you!');
			itemID = someID;
			//alert(document.getElementById(itemID).name)
			//alert(document.getElementById(itemID+'-content').innerHTML)
			makeRequest('update.asp', 'liv='+livello+'&mode=api&templateID=content&itemID='+itemID+'&itemContent='+ encodeURI((document.getElementById(itemID+'-content').innerHTML).replace(/&/g,'%26')), 'loadContent',someID);
		
		}
	}
	
	
	function setEditorValue(instanceName, text ) {  
	//itemID+'ckeditor1'
		document.ajaxform.instanceName.value = text;
	}  


	


	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 alertContents(http_request) {
		if (http_request.readyState == 4) {
			// everything is good, the response is received 
			//alert(http_request.status)
			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:' + http_request.readyState);       
		}
	}
	
    function makeRequest(url, parameters, passType, passeditemID) {
				var actualitemID = passeditemID
				function showContentValue() {
						//kv return ckeditorAPI.GetInstance(actualitemID+'ckeditor1').GetXHTML(true);
						//alert(eval('CKEDITOR.instances.' + actualitemID+'ckeditor1'  + '.getData()'))
						return eval('CKEDITOR.instances.' + actualitemID+'ckeditor1'  + '.getData()')
				}
				function submitckform() {
					var txtContent = showContentValue();
					//alert(txtContent)
					if(txtContent.indexOf('<div id="'+actualitemID +'-content">') != 0) {
					//txtContent = '<div id="'+actualitemID +'-content">'+ txtContent +'</div>';
					}
					//alert(txtContent)
					
					//txtContent =(txtContent.replace(/&amp;/g,'%26'))
					//txtContent =(txtContent.replace(/&quot;/g,'%22'))
					//txtContent = encodeURI(txtContent);
					txtContent = escape(txtContent);
					//alert(txtContent)
					//txtContent = (txtContent.replace(/&#160;/g,''));
					
					//alert(txtContent)
					//alert(unsecape(txtContent))
					//kv replaceContentInLayer (actualitemID, 'Salvataggio in corso...');
										showContentLayer('msgLayer');
										document.getElementById('msgLayer').innerHTML = 'Salvataggio in corso...';
										

					makeRequest('update.asp', 'liv='+livello+'&api=update&itemID='+actualitemID+'&ckeditor1='+txtContent, 'replaceContent',actualitemID);	
					//alert(txtContent)
					showContentLayer(actualitemID+'-toolbar');	
					eval('CKEDITOR.instances.' + actualitemID+'ckeditor1'  + '.destroy()')
					hideContentLayer('msgLayer');
				}

				function cancelckform() {
					//return to the default state
					window.location = window.location
					 //window.location = 'update.asp?itemID='+itemID+'&itemContent='+encodeURI(document.getElementById('story-content').innerHTML);		
					//makeRequest('update.asp', 'mode=api&templateID=content&itemID='+itemID+'&itemContent='+encodeURI(document.getElementById('story-content').innerHTML), 'loadContent');
				}
			
			
			
        var http_request = false;

				//creazione  XMLHttpRequest
				
				try {
								// Mozilla / Safari
								http_request = new XMLHttpRequest();
								if (http_request.overrideMimeType) {
														http_request.overrideMimeType('text/xml');
														// See note below about this line
								}

						} catch (e) {
							// Explorer
							var _ieModelos = new Array(
							'MSXML2.XMLHTTP',
							'MSXML2.XMLHTTP.5.0',
							'MSXML2.XMLHTTP.4.0',
							'MSXML2.XMLHTTP.3.0',
							'MSXML2.XMLHTTP',
							'Microsoft.XMLHTTP'
							);
							var success = false;
							for (var i=0;i < _ieModelos.length && !success; i++) {
								try {
									http_request = new ActiveXObject(_ieModelos[i]);
									success = true;
									//alert(_ieModelos[i])
								} catch (e) {
								}
							}
							if ( !success ) {
								return false;
							}
							
						}
				
				
				//fine ceazione XMLHttpRequest
				
        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(itemID +'-toolbar');
        		        		
					//need a reference point for these new layers?
					var targetLayer = document.getElementById(itemID);
					
					//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 = 'Caricamento editor...';
	
					//create a textarea layer
					var textarea = document.createElement('textarea');
					textarea.setAttribute('id', actualitemID+'ckeditor1');
					textarea.setAttribute('name', actualitemID+'ckeditor1');
	
					//create a submit button
					//<input type="button" name="Button" value="Button">
					var someSubmitButton = document.createElement('input');
					someSubmitButton.setAttribute('type', 'button');
					someSubmitButton.setAttribute('id', actualitemID+'submitAjaxForm');
					someSubmitButton.setAttribute('name', actualitemID+'submitAjaxForm');
					someSubmitButton.setAttribute('title', 'Salva i cambiamenti');
					someSubmitButton.setAttribute('value', "Salva");
					//someSubmitButton.addEventListener('click', submitckform, false);
					
					if(typeof someSubmitButton.addEventListener != 'undefined') {
					   someSubmitButton.addEventListener('click', submitckform, false);
					}
					else if(typeof someSubmitButton.addEventListener != 'undefined') {
					   someSubmitButton.addEventListener('click', submitckform, false);
					}
					else if(typeof someSubmitButton.attachEvent != 'undefined') {
					   someSubmitButton.attachEvent('onclick', submitckform);
					}				
	
					
					//create a cancel button
					var someCancelButton = document.createElement('input');
					someCancelButton.setAttribute('type', 'button');
					someCancelButton.setAttribute('id', actualitemID+'cancelAjaxForm');
					someCancelButton.setAttribute('name', actualitemID+'cancelAjaxForm');
					someCancelButton.setAttribute('title', 'Abbandona i cambiamenti');
					someCancelButton.setAttribute('value', "Cancella");
					
					
					if(typeof someCancelButton.addEventListener != 'undefined') {
					   someCancelButton.addEventListener('click', cancelckform, false);
					}
					else if(typeof someCancelButton.addEventListener != 'undefined') {
					   someCancelButton.addEventListener('click', cancelckform, false);
					}
					else if(typeof someCancelButton.attachEvent != 'undefined') {
					   someCancelButton.attachEvent('onclick', cancelckform);
					}					
					
	
					//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
					//alert(strText)
					textarea.value = strText;		
	
	
					//create ckeditor
					CKEDITOR.replace(actualitemID+'ckeditor1',{
		toolbar : [['Preview','NewPage'],['Bold','Italic','Underline','RemoveFormat','Styles','FontSize','TextColor','BGColor'],['Outdent','Indent','Rule','JustifyLeft','JustifyCenter','JustifyRight','JustifyFull','NumberedList','BulletedList'],['Link','Unlink','Anchor'],['Image','Flash','Table','SpecialChar','HorizontalRule'],['Templates','Source']],
		filebrowserBrowseUrl : '/ckeditor_/plugins/filemanager/browser/default/browser.html?Connector=../../connectors/asp/connector.asp',
		filebrowserImageBrowseUrl : '/ckeditor_/plugins/filemanager/browser/default/browser.html?Connector=../../connectors/asp/connector.asp&Type=Image',
		filebrowserFlashBrowseUrl : '/ckeditor_/plugins/filemanager/browser/default/browser.html?Connector=../../connectors/asp/connector.asp&Type=Flash',
		filebrowserMediaBrowseUrl : '/ckeditor_/plugins/filemanager/browser/default/browser.html?Connector=../../connectors/asp/connector.asp&Type=Media',
		//filebrowserUploadUrl : '/ckeditor_/plugins/filemanager/connectors/asp/connector.asp?command=QuickUpload&type=Files'
		height :  heightEl(actualitemID) + 150,
		contentsCss : '/css/stilifck.css'
		
		
		//filebrowserImageBrowseUrl : '/browser/browse.php?type=Images',
       // filebrowserUploadUrl : '/uploader/upload.php',
        //filebrowserImageUploadUrl : '/uploader/upload.php?type=Images'

	});
					
					//kv CKEDITOR = new ckeditor(actualitemID+'ckeditor1');
					//kv CKEDITOR.BasePath	= '/ckeditor_/' ;
					//kv CKEDITOR.Height = heightEl(actualitemID) ;
					//kv if (CKEDITOR.Height < 150) CKEDITOR.Height = 300
					//alert(widthEl('story-content'))
					//kv CKEDITOR.ToolbarSet = "MyToolbar"
					//CKEDITOR.Config["EnterMode"]		= 'div' ;
					//kv CKEDITOR.Width = widthEl(actualitemID +'-content') ;
					
					//kv CKEDITOR.ReplaceTextarea();
				  
					
					//now hide the original text layer
					hideContentLayer(actualitemID +'-content');
					
					//hide the loading msg info
					hideContentLayer('msgLayer');
					//var tempckeditorAPI=ckeditorAPI.GetInstance(itemID+'ckeditor1');
				} else if (passType == 'replaceContent') {
					//strText = (strText.replace(/£££/g,'&nbsp;'));
					//alert(strText)
					strText = '<div id="'+actualitemID +'-content">'+ strText +'</div>';
					//alert(strText)
					replaceContentInLayer(actualitemID, strText);
				} else {
				
					alert('Error: No action has been specified for the call to the widget');
				
				}
			}
	        	
        };

var now = "upid=" + new Date().getTime();
	//parameters += (parameters.indexOf("?")+1) ? "?" : "&";
	parameters += '&'
	parameters += now;


		http_request.open('POST', url, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
		http_request.setRequestHeader("Content-length", parameters.length);
		//http_request.setRequestHeader("Connection", "close");
		http_request.send(parameters);
		
		//alert(url+'?'+encodeURI(parameters))
		//http_request.open('GET', url+'?'+parameters, true);
		//http_request.send(null);
    }


	


//get width of text element
function widthEl(span){

	if (document.layers){
	  w=document.layers[span].clip.width;
	} else if (document.all && !document.getElementById){
	  w=document.all[span].offsetWidth;
	} else if(document.getElementById){
	  w=document.getElementById(span).offsetWidth;
	}
return w;
}

//get height of text element
function heightEl(span){

	if (document.layers){
	  h=document.layers[span].clip.height;
	} else if (document.all && !document.getElementById){
	  h=document.all[span].offsetHeight;
	} else if(document.getElementById){
	  h=document.getElementById(span).offsetHeight;
	}
	if (h < 400) h = 400
	//if (h > 600) h = 600
return h;
}


