//-----------------------------------------------------------------------
// Copyright (C) wwwRoot.cn, All rights reserved.
//-----------------------------------------------------------------------
// Root.TextEditor.js
// 实现文本实时编辑

document.textEditors = new Array();
TextEditor = function(element, action, data)
{
	if(typeof(element) == 'string') element = document.getElementById(element);
	this.element = element;
	this.textBox = null;
	if ((this.element.nodeName == 'INPUT' && this.element.type == 'text') || this.element.nodeName == 'TEXTAREA')
	{
		this.textBox = this.element;
		this.element = null;
	}
	this.action = action;
	this.data = data;
	this.rows = 1;
	this.autosize = false;
	this.maxLength = 0;
	this.defaultMode = 'textbox';
	this.validator = null;
	this.allowEmpty = false;
	this.title = null;
	this.defaultClass = null;
	this.overClass = null;
	this.editClass = null;
	this.responseXML = null;
	this.responseText = null;
	this.returnValue = ''; 
	this.type = 'Root.TextEditor';
}
TextEditor.prototype.bind = 
	function()
	{
		if ((this.element != null || this.textBox != null) && TextEditor.__ReturnEventValue(this, 'onbind'))
		{	
			this.rows = TextEditor.__EvaluateIntegerPropertyValue(this.rows, 1);			
			this.autosize = TextEditor.__EvaluateBooleanPropertyValue(this.autosize, false);
			this.maxLength = TextEditor.__EvaluateIntegerPropertyValue(this.maxLength, 0);
			if (this.defaultMode == null) this.defaultMode = '';
			this.defaultMode = this.defaultMode.toLowerCase();
			if (this.defaultMode != 'textbox' && this.defaultMode != 'element') this.defaultMode = 'textbox';
			if (this.validator != null && typeof(this.validator) == 'string') this.validator = new RegExp(this.validator);
			this.allowEmpty = TextEditor.__EvaluateBooleanPropertyValue(this.allowEmpty, false);
			if (this.title == null) this.title == '';
			if(typeof(this.textBox) == 'string')
			{
				this.textBox = document.getElementById(this.textBox);
			}
			if (this.textBox == null && this.element != null)
			{				
				var textBox;		
				if (this.rows > 1)
				{
					textBox = document.createElement('TEXTAREA');
					textBox.rows = this.rows;
					textBox.style.overflow = 'auto';
				}
				else
				{
					textBox = document.createElement('INPUT');
					textBox.type = 'text';
					if (this.maxLength > 0) textBox.maxLength = this.maxLength;
				}				
				textBox.id = this.element.id + '_TextBox';
				textBox.style.display = 'none';
				textBox.value = this.element.innerHTML;
				textBox.defaultValue = this.element.innerHTML;
				if (this.defaultMode == 'textbox')
				{
					if (this.title == '')
					{
						this.title = this.element.title;
					}
					textBox.title = this.title;					
				}
				else if (this.defaultMode == 'element')
				{
					if (this.title != '')
					{
						this.element.title = this.title;
					}
					else
					{
						this.title = this.element.title;
					}
				}
				this.element.parentNode.insertBefore(textBox, this.element);
				this.textBox = textBox;				
			}
			else if (this.textBox != null && this.element == null)
			{
				var element = document.createElement('SPAN');
				element.id = this.textBox.id + '_Element';
				element.style.display = 'none';
				element.innerHTML = this.textBox.value;
				if (this.defaultMode == 'textbox')
				{
					if (this.title != '')
					{
						this.textBox.title = this.title;
					}
					else
					{
						this.title = this.textBox.title;	
					}					
				}
				else if (this.defaultMode == 'element')
				{
					if (this.title == '')
					{
						this.title = this.textBox.title;						
					}
					element.title = this.title;
					this.textBox.title = '';
				}
				if (this.textBox.nextSibling)
				{
					this.textBox.parentNode.insertBefore(element, this.textBox.nextSibling);
				}
				else
				{
					this.textBox.parentNode.appendChild(element);
				}
				this.element = element;
			}
			if ( this.element != null && this.textBox != null)
			{
				this.textBox.className = this.defaultMode == 'textbox'? this.defaultClass : this.editClass;
				if (this.autosize && this.rows == 1) TextEditor.__SetTextBoxSize(this.textBox);
				var textEditor = this;
				if (textEditor.defaultMode == 'textbox')
				{
					textEditor.element.style.display = 'none';
					textEditor.textBox.style.display = '';
					textEditor.textBox.onfocus = 
						function()
						{
							if(TextEditor.__ReturnEventValue(textEditor, 'onedit'))
							{
								this.title = '';
								textEditor.edit();
							}
						}
					if(textEditor.overClass != textEditor.defaultClass)
					{
						textEditor.textBox.onmouseover = 
							function()
							{
								if(this.className != textEditor.editClass)
								{
									this.className = textEditor.overClass;
								}
							}
						textEditor.textBox.onmouseout = 
							function()
							{
								if(this.className != textEditor.editClass)
								{
									this.className = textEditor.defaultClass;
								}
							}
					}
				}
				else
				{
					textEditor.textBox.style.display = 'none';
					textEditor.element.style.display = '';
					textEditor.element.onclick = 
						function()
						{
							if(TextEditor.__ReturnEventValue(textEditor, 'onedit'))
							{
								textEditor.edit();
							}
							return false;
						}
				}
				textEditor.textBox.onblur = 
					function()
					{						
						var returnValue = true;
						if((textEditor.allowEmpty ? true : TextEditor.__Trim(this.value) != '') && this.value != this.defaultValue)
						{
							var valid = true;
							if (textEditor.validator != null) valid = textEditor.validator.test(this.value);							
							if (valid || (!valid && !TextEditor.__ReturnEventValue(textEditor, 'onillegal')))
							{
								if(TextEditor.__ReturnEventValue(textEditor, 'onupdate'))
								{
									textEditor.update();
								}
								else
								{
									returnValue = false;
								}
							}
							else
							{
								returnValue = false;
							}
						}
						else
						{
							if(TextEditor.__ReturnEventValue(textEditor, 'oncancel'))
							{
								textEditor.cancel();
							}
							else
							{
								returnValue = false;
							}
						}
						if (returnValue && textEditor.defaultMode == 'textbox') this.title = textEditor.title;
						return returnValue;
					}
				textEditor.textBox.onkeydown = 
					function(ev)
					{
						ev = ev || window.event;
						if(ev.keyCode == 13 && textEditor.rows == 1)
						{
							this.blur();
							return false;
						}
						else if(ev.ctrlKey && ev.keyCode == 13)
						{
							this.blur();
							return false;
						}
						else if(ev.keyCode == 27)
						{
							this.value = this.defaultValue;
							this.blur();
							return false;
						}
					}			
				if(textEditor.autosize && textEditor.rows == 1)
				{
					textEditor.textBox.onkeyup = 
						function()
						{
							var length = TextEditor.__GetTextLength(this.value);
							if(length < 2) length = 2;
							this.size = length;
						}
				}
				var id = textEditor.element.id;
				if (id.indexOf('_Element') > -1) id = id.replace('_Element', '');
				document.textEditors[id] = textEditor;
			}
		}
	};
TextEditor.prototype.edit = 
	function()
	{
		if (this.defaultMode == 'textbox')
		{
			this.textBox.className = this.editClass;
		}
		else
		{
			this.element.style.display = 'none';
			this.textBox.style.display = '';
			this.textBox.focus();
		}
	};
TextEditor.prototype.update = 
	function()
	{
		this.textBox.disabled = true;
		if (this.maxLength > 0 && this.textBox.type == 'textarea')
		{
			if (TextEditor.__GetTextLength(this.textBox.value) > this.maxLength)
			{
				this.textBox.value = this.textBox.value.substring(0, this.maxLength);
			}
		}
		while (this.textBox.value.indexOf('<') > -1)
		{
			this.textBox.value = this.textBox.value.replace('<', '&lt;');			
		}
		while (this.textBox.value.indexOf('>') > -1)
		{
			this.textBox.value = this.textBox.value.replace('>', '&gt;');
		}
		if (this.maxLength > 0)
		{
			if (TextEditor.__GetTextLength(this.textBox.value) > this.maxLength)
			{
				this.textBox.value = this.textBox.value.substring(0, this.maxLength);
			}
		}
		if (this.action != null && typeof(this.action) == 'string')
		{
			var url = this.action, data = this.data;
			if (data == null)
			{
				if(/\[Text\]/i.test(url))
				{
					url = url.replace(/\[Text\]/i, escape(this.textBox.value));
				}
				else
				{
					url += escape(this.textBox.value);
				}
			}
			else
			{
				if(/\[Text\]/i.test(data))
				{
					data = data.replace(/\[Text\]/i, escape(this.textBox.value));
				}
				else
				{
					data += escape(this.textBox.value);
				}
			}
			Xml.Request(url, data, this, 'finish');
		}
	};
TextEditor.prototype.cancel = 
	function()
	{
		if (this.textBox.value != this.textBox.defaultValue) this.textBox.value = this.textBox.defaultValue;
		if (this.autosize && this.rows == 1) TextEditor.__SetTextBoxSize(this.textBox);
		if (this.defaultMode == 'textbox')
		{
			this.textBox.className = this.defaultClass;
		}
		else
		{
			this.textBox.style.display = 'none';
			this.element.style.display = '';
		}
	};
TextEditor.prototype.finish = 
	function(xmlRequest)
	{		
		this.responseXML = xmlRequest.responseXML;
		this.responseText = xmlRequest.responseText;
		if(this.responseXML.lastChild && this.responseXML.lastChild.firstChild && this.responseXML.lastChild.firstChild.nodeType == '3')
		{
			this.returnValue = this.responseXML.lastChild.firstChild.data;
		}
		this.textBox.disabled = false;
		if(TextEditor.__ReturnEventValue(this, 'onfinish'))
		{
			this.textBox.defaultValue = this.textBox.value;
			this.element.innerHTML = this.textBox.value;
		}
		else
		{
			this.textBox.value = this.textBox.defaultValue;
		}
		if (this.defaultMode == 'element')
		{
			this.textBox.style.display = 'none';
			this.element.style.display = '';
		}
		else
		{
			this.textBox.className = this.defaultClass;
		}
	}
TextEditor.prototype.onbind = null; 
TextEditor.prototype.onedit = null; 
TextEditor.prototype.onupdate = null; 
TextEditor.prototype.oncancel = null; 
TextEditor.prototype.onfinish = null;
TextEditor.prototype.onillegal = null;
TextEditor.__GetTextLength = function(string)
{		
	var l = 0;
	for(var i=0; i<string.length; i++)
	{
		var c = string.charCodeAt(i);		
		l += (c < 256 || (c >= 0xff61 && c <= 0xff9f))? 1:2;
	}
	return l;
}
TextEditor.__SetTextBoxSize = function(textBox)
{		
	var size = TextEditor.__GetTextLength(textBox.value);
	if (size == 0) size = 20;
	textBox.size = size;
}
TextEditor.__Trim = function(str)
	{
		str = str.replace(/^ +/, '');		
		str = str.replace(/ +$/, '');
		return str;
	}
TextEditor.__ReturnEventValue = function(textEditor, eventName)
{
	var returnValue = true;
	if(textEditor[eventName] != null)
	{
		if(typeof(textEditor[eventName]) == 'function')
		{
			returnValue = textEditor[eventName]();
		}
		else if(typeof(textEditor[eventName]) == 'string')
		{
			eval('returnValue = function(){' + textEditor[eventName] + '}');							
			returnValue = returnValue.call(textEditor);
		}
		if(returnValue != false && returnValue != true) returnValue = true;
	}	
	return returnValue;
}
TextEditor.__EvaluateBooleanPropertyValue = function(propertyValue, defaultValue)
{
	if (propertyValue == null) propertyValue = defaultValue;
	if (typeof(propertyValue) == 'string')
	{
		if(/^true$/i.test(propertyValue) || /^false$/i.test(propertyValue))
		{
			propertyValue = propertyValue.toLowerCase();
		}
		propertyValue = eval(propertyValue);		
	}
	if (propertyValue != true && propertyValue != false) propertyValue = defaultValue;
	return propertyValue;
}
TextEditor.__EvaluateIntegerPropertyValue = function(propertyValue, defaultValue)
{
	if (typeof(propertyValue) != 'number')
	{
		if (typeof(propertyValue) == 'string')
		{
			propertyValue = parseInt(propertyValue);
			if (isNaN(propertyValue)) propertyValue = defaultValue;
		}
		else
		{
			propertyValue = defaultValue;
		}
	}
	return propertyValue;
}
function __InitializeTextEditors()
{
	var textEditors = document.getElementsByTagName('TextEditor');
	for(var i=0; i<textEditors.length; i++)
	{		
		var textEditor = new TextEditor(textEditors[i].getAttribute('Element'), textEditors[i].getAttribute('Action'), textEditors[i].getAttribute('Data'));
		textEditor.defaultMode = textEditors[i].getAttribute('DefaultMode');
		textEditor.allowEmpty = textEditors[i].getAttribute('AllowEmpty');
		textEditor.rows = textEditors[i].getAttribute('Rows');
		textEditor.autosize = textEditors[i].getAttribute('Autosize');
		textEditor.maxLength = textEditors[i].getAttribute('MaxLength');
		textEditor.validator = textEditors[i].getAttribute('Validator');
		textEditor.title = textEditors[i].getAttribute('Title');
		textEditor.defaultClass = textEditors[i].getAttribute('DefaultClass');
		textEditor.overClass = textEditors[i].getAttribute('OverClass');
		textEditor.editClass = textEditors[i].getAttribute('EditClass');
		textEditor.onbind = textEditors[i].getAttribute('OnBind');
		textEditor.onedit = textEditors[i].getAttribute('OnEdit');
		textEditor.onupdate = textEditors[i].getAttribute('OnUpdate');
		textEditor.oncancel = textEditors[i].getAttribute('OnCancel');
		textEditor.onfinish = textEditors[i].getAttribute('OnFinish');
		textEditor.onillegal = textEditors[i].getAttribute('OnIllegal');
		textEditor.bind();
	}
}
__InitializeTextEditors();