//+----------------------------------------------------------------------------+
//| Copyright 2005, Dipl. eng. Andri Ivin.                                     |
//| Unauthorized copying of this script or any part of this script prohibited. |
//| Contact: andri.ivin@mail.ee                                                |
//+----------------------------------------------------------------------------+

//Determine coordinates------------------------------------
//Define global variables----------------------------------
var _x = 0;
var _y = 0;
function grabXY(ev) {
	if (window.navigator.userAgent.indexOf('MSIE') > -1) {
		_x = window.event.x;
		_y = window.event.y;
	}
	else {
		_x = ev.pageX;
		_y = ev.pageY;
	}
}

function motionListener() {
	document.onmousemove = grabXY;
}
//---------------------------------------------------------
//---------------------------------------------------------
var Drag = {
	init : function(_element_) {
		_element_.onmousedown = Drag.start;
		_element_.ondrag = Drag.start;     //For: IE, Maxthon etc.
	},
	start : function() {
		document.onmouseup = Drag.end;
		dragInputWindow(1);
		//Drag.drag;
		return false;
	},
	drag : function() {
		return false;
	},
	end : function() {
		dragInputWindow(0);
	}
}

var Res = {
	init : function(_element_) {
		_element_.onmousedown	= Res.start;
		_element_.ondrag = Res.start;
	},
	start : function() {
		initResize(true);
		document.onmouseup = Res.end;
		return false;
	},
	drag : function() {
		return false;
	},
	end : function() {
		initResize(false);
	}
}
//---------------------------------------------------------
var left_offset = 19; //dragbutton width 25px divided 2 plus border width 5px plus space 2px
var top_offset = 15; //dragbutton height 30px divided 2
var _l_inputwindow = 0;
var _t_inputwindow = 0;

function dragWin() {
	//DRAGGING inputwindow
	if (dragging_inputwindow) {
		element_inputwindow = document.getElementById('InputWindow');
		element_inputtextarea = document.getElementById('InputTextArea');
		element_inputtext = document.getElementById('InputText');
		element_FontCombobox = document.getElementById('FontCombobox');
		if (_IE) {
			element_inputtext.style.visibility = 'hidden';
			element_inputtextarea.style.visibility = 'hidden';
			element_FontCombobox.style.visibility = 'hidden'
		}
		if (window.navigator.userAgent.indexOf('Opera') > -1) {
			_l_inputwindow = _x - left_offset;// +  document.body.scrollLeft; 
			_t_inputwindow = _y - top_offset;// + document.body.scrollTop;
		}
		else if (window.navigator.userAgent.indexOf('Firebird') > -1) {
			_l_inputwindow = _x - left_offset;
			_t_inputwindow = _y - top_offset; 
		}
		else if (window.navigator.userAgent.indexOf('Firefox') > -1) {
			_l_inputwindow = _x - left_offset;
			_t_inputwindow = _y - top_offset; 
		}
		else if (window.navigator.userAgent.indexOf('Camino') > -1) {
			_l_inputwindow = _x - left_offset;
			_t_inputwindow = _y - top_offset; 
		}
		else if (window.navigator.userAgent.indexOf('Netscape') > -1) {
			_l_inputwindow = _x - left_offset;
			_t_inputwindow = _y - top_offset; 
		}
		else if (window.navigator.userAgent.indexOf('Gecko') > -1) {
			_l_inputwindow = _x - left_offset;
			_t_inputwindow = _y - top_offset;
		}
		else if (window.navigator.userAgent.indexOf('MSIE') > -1) {
			_l_inputwindow = _x - left_offset +  document.body.scrollLeft; 
			_t_inputwindow = _y - top_offset + document.body.scrollTop; 
		}
		else {
			_l_inputwindow = _x - left_offset +  document.body.scrollLeft; 
			_t_inputwindow = _y - top_offset + document.body.scrollTop; 
		}
		element_inputwindow.style.left = _l_inputwindow;
		element_inputwindow.style.top = _t_inputwindow;
	}
}
//---------------------------------------------------------
var dragging_inputwindow = false;

function dragInputWindow(action) {
	if (!action) {
		dragging_inputwindow = false;
		if (_IE) {
			el_inputtextarea = document.getElementById('InputTextArea');
			el_inputtext = document.getElementById('InputText');
			el_FontCombobox = document.getElementById('FontCombobox');
			el_inputtextarea.style.visibility = 'visible';
			el_inputtext.style.visibility = 'visible';
			el_FontCombobox.style.visibility = 'visible'
		}
	}
	else {
		dragging_inputwindow = true;
	}
}
//---------------------------------------------------------
var small_ = true;
var resizing = false;
var start_coord_X = 0;
var start_coord_Y = 0;
var new_coord_X = 0;
var new_coord_Y = 0;
var delta_x = 0;
var delta_y = 0;

function initResize(identifier) {
	if ((!resizing)&&(identifier)) {
		start_coord_X = _x;
		start_coord_Y = _y;
		resizing = true;
	}
	else if ((resizing)&&(!identifier)){
		resizing = false;
	}
}

function resize() {
	if (resizing) {
		var el_InputWindow = document.getElementById('InputWindow');
		var el_InputTextArea = document.getElementById('InputTextArea');
		var el_InputText = document.getElementById('InputText');
		var el_GuideLeft = document.getElementById('GuideLeft');
		var el_GuideRight = document.getElementById('GuideRight');
		var el_FontCombobox = document.getElementById('FontCombobox');
		var el_Arial10ptExample = document.getElementById('Arial10ptExample');
		var el_CourierNew10ptExample = document.getElementById('CourierNew10ptExample');
		var el_TimesNewRomanText12ptExample = document.getElementById('TimesNewRomanText12ptExample');
		var el_VerdanaText9ptExample = document.getElementById('VerdanaText9ptExample');

		new_coord_X = _x;
		new_coord_Y = _y;
		delta_x = new_coord_X - start_coord_X;
		delta_y = new_coord_Y - start_coord_Y;
		//New measures
		InputWindow_width = InputWindow_width + delta_x;
		InputWindow_height = InputWindow_height + delta_y;
		
		InputTextArea_width = InputTextArea_width + delta_x;
		InputTextArea_height = InputTextArea_height + delta_y;
		InputText_width = InputText_width + delta_x;
		InputText_height = InputText_height + delta_y;
		current_Font_Combo_top_shift = current_Font_Combo_top_shift + delta_y;
		current_Font_Example_top_shift = current_Font_Example_top_shift + delta_y;
		el_FontCombobox.style.visibility = 'hidden';	

		if ((InputWindow_width < (InputWindow_width_min-5))||(InputWindow_height < (InputWindow_height_min-5))) {
			if (InputWindow_width < (InputWindow_width_min-5)) {//Reset width
				InputWindow_width = InputWindow_width_min;
				InputTextArea_width = InputTextArea_width_min;
				InputText_width = InputText_width_min;
				// 1
				el_InputText.style.width = InputText_width;
				// 2
				el_InputTextArea.style.width = InputTextArea_width;
				// 3
				el_InputWindow.style.width = InputWindow_width;
			}
			else if (InputWindow_height < (InputWindow_height_min-5)) {//Reset height
				InputWindow_height = InputWindow_height_min;
				InputTextArea_height = InputTextArea_height_min;
				InputText_height = InputText_height_min;
				current_Font_Combo_top_shift = Font_Combo_top_shift;
				// 1
				el_InputText.style.height = InputText_height;
				// 2
				el_InputTextArea.style.height = InputTextArea_height;
				// 3, 4
				el_GuideLeft.style.height = InputTextArea_height;
				el_GuideRight.style.height = InputTextArea_height;
				// 5
				el_FontCombobox.style.top = current_Font_Combo_top_shift;
				// 6, 7, 8, 9
				el_Arial10ptExample.style.top = current_Font_Example_top_shift;
				el_CourierNew10ptExample.style.top = current_Font_Example_top_shift;
				el_TimesNewRomanText12ptExample.style.top = current_Font_Example_top_shift;
				el_VerdanaText9ptExample.style.top = current_Font_Example_top_shift;
				// 10
				el_InputWindow.style.height = InputWindow_height;
			}
		}
		else {
			if (delta_x < 0) {
				el_InputText.style.width = InputText_width;
				el_InputTextArea.style.width = InputTextArea_width;
				el_InputWindow.style.width = InputWindow_width;
			}
			else {
				el_InputWindow.style.width = InputWindow_width;
				el_InputTextArea.style.width = InputTextArea_width;
				el_InputText.style.width = InputText_width;
			}
			if (delta_y < 0) {
				// 1
				el_InputText.style.height = InputText_height;
				// 2
				el_InputTextArea.style.height = InputTextArea_height;
				// 3, 4
				el_GuideLeft.style.height = InputTextArea_height;
				el_GuideRight.style.height = InputTextArea_height;
				// 5
				el_FontCombobox.style.top = current_Font_Combo_top_shift;
				// 6, 7, 8, 9
				el_Arial10ptExample.style.top = current_Font_Example_top_shift;
				el_CourierNew10ptExample.style.top = current_Font_Example_top_shift;
				el_TimesNewRomanText12ptExample.style.top = current_Font_Example_top_shift;
				el_VerdanaText9ptExample.style.top = current_Font_Example_top_shift;
				// 10
				el_InputWindow.style.height = InputWindow_height;
			}
			else {
				el_InputWindow.style.height = InputWindow_height;
				el_InputTextArea.style.height = InputTextArea_height;
				el_InputText.style.height = InputText_height;
				el_GuideLeft.style.height = InputTextArea_height;
				el_GuideRight.style.height = InputTextArea_height;
				el_FontCombobox.style.top = current_Font_Combo_top_shift;
				el_Arial10ptExample.style.top = current_Font_Example_top_shift;
				el_CourierNew10ptExample.style.top = current_Font_Example_top_shift;
				el_TimesNewRomanText12ptExample.style.top = current_Font_Example_top_shift;
				el_VerdanaText9ptExample.style.top = current_Font_Example_top_shift;
			}
			start_coord_X = new_coord_X;
			start_coord_Y =	new_coord_Y;
		}
		el_FontCombobox.style.visibility = 'visible';
	}
}
//---------------------------------------------------------

//---------------------------------------------------------
function restore() {
	var elem_InputWindow = document.getElementById('InputWindow');
	var elem_InputTextArea = document.getElementById('InputTextArea');
	var elem_InputText = document.getElementById('InputText');
	var elem_GuideLeft = document.getElementById('GuideLeft');
	var elem_GuideRight = document.getElementById('GuideRight');
	var elem_FontCombobox = document.getElementById('FontCombobox');
	var elem_Arial10ptExample = document.getElementById('Arial10ptExample');
	var elem_CourierNew10ptExample = document.getElementById('CourierNew10ptExample');
	var elem_TimesNewRomanText12ptExample = document.getElementById('TimesNewRomanText12ptExample');
	var elem_VerdanaText9ptExample = document.getElementById('VerdanaText9ptExample');

	elem_InputTextArea.style.width = InputTextArea_width_min;
	elem_InputTextArea.style.height = InputTextArea_height_min;
	elem_InputText.style.width =InputText_width_min;
	elem_InputText.style.height =InputText_height_min;
	elem_GuideLeft.style.height = InputTextArea_height_min;
	elem_GuideRight.style.height = InputTextArea_height_min;
	elem_FontCombobox.style.top = Font_Combo_top_shift;
	elem_Arial10ptExample.style.top = Font_Example_top_shift;
	elem_CourierNew10ptExample.style.top = Font_Example_top_shift;
	elem_TimesNewRomanText12ptExample.style.top = Font_Example_top_shift;
	elem_VerdanaText9ptExample.style.top = Font_Example_top_shift;
	elem_InputWindow.style.width = InputWindow_width_min;
	elem_InputWindow.style.height = InputWindow_height_min;
}
//---------------------------------------------------------

var guides_visible = false;

function guides() {
	element_GuideLeft = document.getElementById('GuideLeft');
	element_GuideRight = document.getElementById('GuideRight');
	if (document.forms[0].Fonts.value == "CourierNewText10") {
		guides_visible = true;
		element_GuideLeft.style.visibility = 'visible';
		element_GuideRight.style.visibility = 'visible';
	}
	else {
		guides_visible = false;
		element_GuideLeft.style.visibility = 'hidden';
		element_GuideRight.style.visibility = 'hidden';
	}
	return;
}
//---------------------------------------------------------

//Open and close Input Window------------------------------
var inputWindow_visible = false;

//Open if closed for QUOTE button
function openInputWindow(quote_nr) {
	//Call function quote()
	quote(quote_nr);
	if (!inputWindow_visible) {
		inputWindow_visible = true;
		//Call function guides()
		guides();
		element_InputWindow = document.getElementById('InputWindow');
		element_InputTextArea = document.getElementById('InputTextArea');
		element_InputText = document.getElementById('InputText');
		element_button_undo_inactiv = document.getElementById('button_undo_inactiv');
		element_button_redo_inactiv = document.getElementById('button_redo_inactiv');
		element_GuideLeft = document.getElementById('GuideLeft');
		element_GuideRight = document.getElementById('GuideRight');
		element_FontCombobox = document.getElementById('FontCombobox');
		if (_IE) {
			element_InputText.style.visibility = 'hidden';
			element_InputTextArea.style.visibility = 'hidden';
		}
		if (_IE) {//((_IE)||(_Opera)) {
			element_InputWindow.style.left = ( (_x - InputWindow_width + document.body.scrollLeft + 53) > 2 ? (_x - InputWindow_width + document.body.scrollLeft + 53) : (2) );
			element_InputWindow.style.top = _y + 30 +  document.body.scrollTop;
		}
		else {
			element_InputWindow.style.left = ( (_x - InputWindow_width + 53) > 2 ? (_x - InputWindow_width +  document.body.scrollLeft + 53) : (2) );
			element_InputWindow.style.top = _y + 30;
		}
		element_InputWindow.style.visibility = 'visible';
		if (_undo_counter == 0) {
			element_button_undo_inactiv.style.visibility = 'visible';
		}
		if (_redo_counter == 0) {
			element_button_redo_inactiv.style.visibility = 'visible';
		}
		if (guides_visible) {
			element_GuideLeft.style.visibility = 'visible';
			element_GuideRight.style.visibility = 'visible';
		}
		else {
			element_GuideLeft.style.visibility = 'hidden';
			element_GuideRight.style.visibility = 'hidden';
		}
		element_FontCombobox.style.visibility = 'visible';
		//Call function setSubmitButton()
		setSubmitButton(_global_submitbutton_status);

		element_InputTextArea.style.visibility = 'visible';
		element_InputText.style.visibility = 'visible';
	}
}

function openCloseInputWindow(opened) {
	var top_shift = 20;//Space between writebutton and inputform
	element_InputWindow = document.getElementById('InputWindow');
	element_InputTextArea = document.getElementById('InputTextArea');
	element_InputText = document.getElementById('InputText');
	element_button_undo_inactiv = document.getElementById('button_undo_inactiv');
	element_button_redo_inactiv = document.getElementById('button_redo_inactiv');
	element_button_submit_inactiv = document.getElementById('button_submit_inactiv');
	element_button_submit_on = document.getElementById('button_submit_on');
	element_GuideLeft = document.getElementById('GuideLeft');
	element_GuideRight = document.getElementById('GuideRight');
	element_FontCombobox = document.getElementById('FontCombobox');

	if (opened) {
		//Call function guides()
		guides();
		if (_IE) {
			element_InputText.style.visibility = 'hidden';
			element_InputTextArea.style.visibility = 'hidden';
		}
		if (_IE) {//((_IE)||(_Opera)) {
			if (!inputWindow_visible) {
				element_InputWindow.style.left = ( (_x - InputWindow_width + document.body.scrollLeft) > 2 ? (_x - InputWindow_width +  document.body.scrollLeft) : (2) );
				inputWindow_visible = true;
			}
			element_InputWindow.style.top = _y + top_shift +  document.body.scrollTop;
		}
		else {
			if (!inputWindow_visible) {
				element_InputWindow.style.left = ( (_x - InputWindow_width) > 2 ? (_x - InputWindow_width +  document.body.scrollLeft) : (2) );
				inputWindow_visible = true;
			}
			element_InputWindow.style.top = _y + top_shift;
		}
		element_InputWindow.style.visibility = 'visible';
		if (_undo_counter == 0) {
			element_button_undo_inactiv.style.visibility = 'visible';
		}
		if (_redo_counter == 0) {
			element_button_redo_inactiv.style.visibility = 'visible';
		}
		if (guides_visible) {
			element_GuideLeft.style.visibility = 'visible';
			element_GuideRight.style.visibility = 'visible';
		}
		else {
			element_GuideLeft.style.visibility = 'hidden';
			element_GuideRight.style.visibility = 'hidden';
		}
		element_FontCombobox.style.visibility = 'visible';
		//Call function setSubmitButton()
		setSubmitButton(_global_submitbutton_status);
		element_InputTextArea.style.visibility = 'visible';
		element_InputText.style.visibility = 'visible';
		document.forms[0].InputText.focus();
	}
	else {
		inputWindow_visible = false;
		for (m=0; m<=_undo_counter; m++) {
			_text[m] = "";
		}
		_undo_counter = 0;
		_redo_counter = 0;
		document.forms[0].InputText.value = "";

		element_GuideLeft.style.visibility = 'hidden';
		element_GuideRight.style.visibility = 'hidden';
		element_FontCombobox.style.visibility = 'hidden';
		element_button_submit_inactiv.style.visibility = 'hidden';
		element_button_submit_on.style.visibility = 'hidden';
		element_button_undo_inactiv.style.visibility = 'hidden';
		element_button_redo_inactiv.style.visibility = 'hidden';
		element_InputTextArea.style.visibility = 'hidden';
		element_InputWindow.style.visibility = 'hidden';
		element_InputText.style.visibility = 'hidden';
		element_InputWindow.style.left = 0;
		element_InputWindow.style.top = 0;
	}
}

//Quote----------------------------------------------------

function quote(quote_nr) {
	document.forms[0].InputText.value = document.forms[0].InputText.value + "|BLOCK|";
	document.forms[0].InputText.value = document.forms[0].InputText.value + string_user[quote_nr];
	document.forms[0].InputText.value = document.forms[0].InputText.value + "|QUOTE|";
	count();
	return;
}

//Show Copyright Table-------------------------------------
function showCTable(show) {
	element_cTable = document.getElementById('cTable');
	if (show) {
		element_cTable.style.visibility = 'visible';
	}
	else {
		element_cTable.style.visibility = 'hidden';
	}
}

function showCopyright() {
	var copyright = "©";
	window.alert('Input Form: '+copyright+' 2005, Andri Ivin');
}

//Show font example----------------------------------------
function fontExample(show,_font) {
	var el_Arial10ptExample = document.getElementById('Arial10ptExample');
	var el_CourierNew10ptExample = document.getElementById('CourierNew10ptExample');
	var el_TimesNewRomanText12ptExample = document.getElementById('TimesNewRomanText12ptExample');
	var el_VerdanaText9ptExample = document.getElementById('VerdanaText9ptExample');
	var selected_font = document.forms[0].Fonts.value;

	if (show) {
		switch(_font) {
			case "ArialText10":
				el_CourierNew10ptExample.style.visibility = 'hidden';
				el_TimesNewRomanText12ptExample.style.visibility = 'hidden';
				el_VerdanaText9ptExample.style.visibility = 'hidden';
				el_Arial10ptExample.style.visibility = 'visible';
				break;
			case "CourierNewText10":
				el_Arial10ptExample.style.visibility = 'hidden';
				el_TimesNewRomanText12ptExample.style.visibility = 'hidden';
				el_VerdanaText9ptExample.style.visibility = 'hidden';
				el_CourierNew10ptExample.style.visibility = 'visible';
				break;
			case "TimesNewRomanText12":
				el_Arial10ptExample.style.visibility = 'hidden';
				el_CourierNew10ptExample.style.visibility = 'hidden';
				el_VerdanaText9ptExample.style.visibility = 'hidden';
				el_TimesNewRomanText12ptExample.style.visibility = 'visible';
				break;
			case "VerdanaText9":
				el_Arial10ptExample.style.visibility = 'hidden';
				el_CourierNew10ptExample.style.visibility = 'hidden';
				el_TimesNewRomanText12ptExample.style.visibility = 'hidden';
				el_VerdanaText9ptExample.style.visibility = 'visible';
				break;
			//-----
			case "undefined":
				switch(selected_font) {
					case "ArialText10":
						el_CourierNew10ptExample.style.visibility = 'hidden';
						el_TimesNewRomanText12ptExample.style.visibility = 'hidden';
						el_VerdanaText9ptExample.style.visibility = 'hidden';
						el_Arial10ptExample.style.visibility = 'visible';
						break;
					case "CourierNewText10":
						el_Arial10ptExample.style.visibility = 'hidden';
						el_TimesNewRomanText12ptExample.style.visibility = 'hidden';
						el_VerdanaText9ptExample.style.visibility = 'hidden';
						el_CourierNew10ptExample.style.visibility = 'visible';
						break;
					case "TimesNewRomanText12":
						el_Arial10ptExample.style.visibility = 'hidden';
						el_CourierNew10ptExample.style.visibility = 'hidden';
						el_VerdanaText9ptExample.style.visibility = 'hidden';
						el_TimesNewRomanText12ptExample.style.visibility = 'visible';
						break;
					case "VerdanaText9":
						el_Arial10ptExample.style.visibility = 'hidden';
						el_CourierNew10ptExample.style.visibility = 'hidden';
						el_TimesNewRomanText12ptExample.style.visibility = 'hidden';
						el_VerdanaText9ptExample.style.visibility = 'visible';
						break;
					default:
						break;
				}
				//End Case #2
				break;
			default:
				break;
		}
		//End Case #1
	}
	else {
		//window.alert('out');
		el_Arial10ptExample.style.visibility = 'hidden';
		el_CourierNew10ptExample.style.visibility = 'hidden';
		el_TimesNewRomanText12ptExample.style.visibility = 'hidden';
		el_VerdanaText9ptExample.style.visibility = 'hidden';
	}
}
//---------------------------------------------------------

//Swap buttons images--------------------------------------
function swapButtonImage(button,buttonImage) {
	document[button].src = buttonImage;
}
//---------------------------------------------------------

var _undo_counter = 0;
var _total_numberOfUndos = 50;
var _redo_counter = 0;
var _first_time = true;
var _text_buffer = "";
var _text = new Array();
_text[_undo_counter] = "";

//#####--- Delete ---#####
function _delete() { //at onload
	document.forms[0].InputText.value = "";
	document.forms[0].InputText.focus();
}

//#####--- Clear ---#####
function _clear() {
	if (!_first_time) {
		document.forms[0].InputText.value = "";
		if (_undo_counter < _total_numberOfUndos) {
			_text[(_undo_counter+1)] = "";
			_undo_counter = _undo_counter + 1;
		}
		else { //_undo_counter is maximal
			for (i=1; i<_undo_counter; i++) {
				_text[i] = _text[i+1];
			}
			_text[_undo_counter] = "";
		}
		element_undoButton = document.getElementById('button_undo_inactiv');
		element_undoButton.style.visibility = 'hidden';
		_redo_counter = 0;
		element_redoButton = document.getElementById('button_redo_inactiv');
		element_redoButton.style.visibility = 'visible';
	}
	count();
	//Set SubmitButton
	if (document.forms[0].InputText.value != "") {
		setSubmitButton('on');
	}
	else {
		setSubmitButton('inactiv');
	}
}

//#####--- Select ---#####
function _select() {
	document.forms[0].InputText.select();
	document.forms[0].InputText.focus();
}

//#####--- Save ---#####
function save() {
	_text_buffer = document.forms[0].InputText.value;
	
	//Delete Tabs
	var _text_buffer_Minus_Tabs = "";
	if (_text_buffer != "") {
		for (n=0; n<_text_buffer.length; n++) {
			if (_text_buffer.charAt(n) != "	") {//Not Tab
				_text_buffer_Minus_Tabs += _text_buffer.charAt(n);
			}
			else {
				_text_buffer_Minus_Tabs += " ";//If Tab then insert Blank
			}
		}
	}
	if (_text_buffer != _text_buffer_Minus_Tabs) {
		document.forms[0].InputText.value = _text_buffer_Minus_Tabs;
		_text_buffer = _text_buffer_Minus_Tabs;
	}
	//end delete tabs

	if (_undo_counter < _total_numberOfUndos) {
		if (_undo_counter == 0) {
			if (_text_buffer != "") {
				_text[1] = _text_buffer;
				_undo_counter = _undo_counter + 1;

				element_undoButton = document.getElementById('button_undo_inactiv');
				element_undoButton.style.visibility = 'hidden';
				_first_time = false;
				//Hide redo button and reset redo counter
				_redo_counter = 0;
				element_redoButton = document.getElementById('button_redo_inactiv');
				element_redoButton.style.visibility = 'visible';
			}
		}	
		else if (_text[_undo_counter] != _text_buffer) {
			_text[(_undo_counter+1)] = _text_buffer;
			_undo_counter = _undo_counter + 1;
			//Hide redo button and reset redo counter
			_redo_counter = 0;
			element_redoButton = document.getElementById('button_redo_inactiv');
			element_redoButton.style.visibility = 'visible';
		}
	}
	else { //_undo_counter is maximal
		if (_text[_undo_counter] != _text_buffer) {
			for (i=1; i<_undo_counter; i++) {
				_text[i] = _text[i+1];
			}
			_text[_undo_counter] = _text_buffer;
			//Hide redo button and reset redo counter
			_redo_counter = 0;
			element_redoButton = document.getElementById('button_redo_inactiv');
			element_redoButton.style.visibility = 'visible';
		}
	}
	count();
	//Set SubmitButton
	if (document.forms[0].InputText.value != "") {
		setSubmitButton('on');
	}
	else {
		setSubmitButton('inactiv');
	}
}

//#####--- Undo ---#####
function undo() {
	if (_undo_counter > 0) {
		document.forms[0].InputText.value = _text[(_undo_counter-1)];
		_undo_counter = _undo_counter - 1;
		_redo_counter = _redo_counter + 1;

		element_redoButton = document.getElementById('button_redo_inactiv');
		element_redoButton.style.visibility = 'hidden';
	}
	if (_undo_counter == 0) {
		element_undoButton = document.getElementById('button_undo_inactiv');
		element_undoButton.style.visibility = 'visible';
	}
	count();

	//Set SubmitButton
	if (document.forms[0].InputText.value != "") {
		setSubmitButton('on');
	}
	else {
		setSubmitButton('inactiv');
	}
}

//#####--- Redo ---#####
function redo() {
	if (_redo_counter > 0) {
		document.forms[0].InputText.value = _text[(_undo_counter+1)];
		_undo_counter = _undo_counter + 1;
		_redo_counter = _redo_counter - 1;
		
		element_undoButton = document.getElementById('button_undo_inactiv');
		element_undoButton.style.visibility = 'hidden';
	}
	if (_redo_counter == 0) {
		element_redoButton = document.getElementById('button_redo_inactiv');
		element_redoButton.style.visibility = 'visible';
	}
	count();
	//Set SubmitButton
	if (document.forms[0].InputText.value != "") {
		setSubmitButton('on');
	}
	else {
		setSubmitButton('inactiv');
	}
}

//#####--- Count ---#####
function count() {
	var textStringAndBlanks = "";//TO SEND!
	var textString = _text[_undo_counter];
	//window.alert(textString);
	//Blank spaces
	var wordArray = new Array();
	wordArray = textString.split(" ");

	var numberOfBlanks = 0;
	var textStringMinusBlanks = "";
	var blank_20_is_set = false;
	var last_set_string = "";

	if (textString != "") {
		for (k=0; k<wordArray.length; k++) {
			if (wordArray[k] != "") {
				textStringAndBlanks += wordArray[k];//TO SEND!
				last_set_string = wordArray[k];
				if ((k<(wordArray.length-1))&&(last_set_string != "%20")) {
					if (wordArray[k].charAt(0) != "+") {
						//A '&nbsp;' will be set after Enter
						textStringAndBlanks = textStringAndBlanks + "&nbsp;";//TO SEND!
						blank_20_is_set = false;
						last_set_string = "&nbsp;";
					}
					else {
						textStringAndBlanks = textStringAndBlanks + "%20";//TO SEND! %20 is a blank
						blank_20_is_set = true;
						last_set_string = "%20";
					}
				}
				textStringMinusBlanks += wordArray[k];
				if ( (wordArray[k].match(/[+\s]/))&&(wordArray[k].charAt(0) != "+") ) {
					numberOfBlanks = numberOfBlanks + 1;
					if (!wordArray[k].charAt(0).match(/[+\s]/)) {
						numberOfBlanks = numberOfBlanks - 1;
					}
				}
			}
			else {
				if (k == 0) {//First line
					textStringAndBlanks = textStringAndBlanks + "&nbsp;";//TO SEND!
					blank_20_is_set = false;
					last_set_string = "&nbsp;";			
				}
 				else if (last_set_string != "%20") {
					if (k != (wordArray.length-1)) {
						textStringAndBlanks = textStringAndBlanks + "%20";//TO SEND!
						blank_20_is_set = true;
						last_set_string = "%20";
					}
				}
				else {
					if (k != (wordArray.length-1)) {
						textStringAndBlanks = textStringAndBlanks + "&nbsp;";//TO SEND!
						blank_20_is_set = false;
						last_set_string = "&nbsp;";
					}
				}				
				numberOfBlanks = numberOfBlanks + 1;
			}
		}
		//window.alert(textStringAndBlanks);
	}
	
	//----------------------
	//Determine is there a blank at the beginning of a paragraph
	var blankAfterEnter = 0;
	for (i=0; i<textString.length; i++) {
		if ( (textString.charAt(i) == " ") && ((i-1) >= 0) && (textString.charAt(i-1) != " ")  && (textString.charAt(i-1).match(/[+\s]/)) && (textString.charAt(i-1) != "+") ) {
			blankAfterEnter++;
			var emptyLine = true;
			for (z=i; z<textString.length; z++) {
				if (textString.charAt(z) != " ") {
					emptyLine = false;
				}
			}
			if (emptyLine) {
				blankAfterEnter--;
			}
		}
	}
	//Determine is there a blank at the end of a paragraph and before enter
	var blankBeforeEnter = 0;
	for (j=0; j<textString.length; j++) {
		if ( (textString.charAt(j) == " ") && ((j+1) < textString.length) && (textString.charAt(j+1) != " ")  && (textString.charAt(j+1).match(/[+\s]/)) && (textString.charAt(j+1) != "+")  ) {
			blankBeforeEnter++;
		}
	}
	//---------------------
	
	//Add <BR> TO SEND!
	var temp_textStringAndBlanks = "";//TO SEND!
	if (textStringAndBlanks != "") {//TO SEND!
		for (l=0; l<textStringAndBlanks.length; l++) {//TO SEND!
			if ( (textStringAndBlanks.charAt(l).match(/[+\s]/)) && (textStringAndBlanks.charAt(l) != "+") ) {//TO SEND!
				temp_textStringAndBlanks += "<BR>";//TO SEND!
				if ((_IE)||(_Opera)) {//TO SEND!
					l++;//TO SEND!
				}//TO SEND!
			}//TO SEND!
			else {//TO SEND!
				temp_textStringAndBlanks += textStringAndBlanks.charAt(l);//TO SEND!
			}//TO SEND!
		}//TO SEND!
		textStringAndBlanks = temp_textStringAndBlanks;
	}//TO SEND!

	//Determine Number of Enters=Paragraphs
	var numberOfEnters = 0;
	if ( (textString != "")&&(textStringMinusBlanks != "") ) {
		for (k=0; k<textStringMinusBlanks.length; k++) {
			if ( (textStringMinusBlanks.charAt(k).match(/[+\s]/)) && (textStringMinusBlanks.charAt(k) != "+") ) {
				numberOfEnters = numberOfEnters + 1;
			}
		}
	}
	if ((_IE)||(_Opera)) {
	   numberOfEnters = numberOfEnters/2;
	}

	var numberOfBlankEnters = 0;
	if ( (textString != "")&&(textStringMinusBlanks != "") ) {
		for (k=0; k<textStringMinusBlanks.length; k++) {
			if ((_IE)||(_Opera)) {
				if ( (textStringMinusBlanks.charAt(k).match(/[+\s]/))&&(textStringMinusBlanks.charAt(k+1).match(/[+\s]/))&&(textStringMinusBlanks.charAt(k)!="+")&&(textStringMinusBlanks.charAt(k+1)!="+") ) {
					numberOfBlankEnters = numberOfBlankEnters + 1;
					k = k + 1;
					if ( ((k+1)<textStringMinusBlanks.length) && ( (!textStringMinusBlanks.charAt(k+1).match(/[+\s]/)) || ( (textStringMinusBlanks.charAt(k+1).match(/[+\s]/)) && (textStringMinusBlanks.charAt(k+1)=="+") ) ) ) {
						numberOfBlankEnters = numberOfBlankEnters - 1;
					}
				}
				if ( (k == (textStringMinusBlanks.length-1))&&(textStringMinusBlanks.charAt(k).match(/[+\s]/)) ) {
					numberOfBlankEnters = numberOfBlankEnters - 0;
				}
			}
			else {
				if ( (textStringMinusBlanks.charAt(k).match(/[+\s]/))&&(textStringMinusBlanks.charAt(k)!="+") ) {
					numberOfBlankEnters = numberOfBlankEnters + 1;
					if ( ((k+1)<textStringMinusBlanks.length) && ( (!textStringMinusBlanks.charAt(k+1).match(/[+\s]/)) || ( (textStringMinusBlanks.charAt(k+1).match(/[+\s]/)) && (textStringMinusBlanks.charAt(k+1)=="+") ) ) ) {
						numberOfBlankEnters = numberOfBlankEnters - 1;
					}
				}
				if ( (k == (textStringMinusBlanks.length-1))&&(textStringMinusBlanks.charAt(k).match(/[+\s]/)) ) {
					numberOfBlankEnters = numberOfBlankEnters - 0;
				}
			}
		}
	}
	//if (wordArray.length!=0) {window.alert("wordArray.length="+wordArray.length+"numberOfBlanks"+numberOfBlanks+"numberOfEnters="+numberOfEnters+"numberOfBlankEnters"+numberOfBlankEnters);}//3-1+(3-2) 
	var numberOfWords = 0;
	//Determine Number of Words
	numberOfWords = wordArray.length - numberOfBlanks + Math.round(numberOfEnters-numberOfBlankEnters) - blankBeforeEnter - blankAfterEnter;
	//Determine Number of Paragraphs
	var numberOfParagraphs = 0;
	numberOfParagraphs = Math.round(numberOfEnters);
	//Determine Number of Characters
	var numberOfCharacters = 0;
	if (textString != "") {
		numberOfCharacters = textString.length;
		if ((_IE)||(_Opera)) {
		   numberOfCharacters = Math.round(numberOfCharacters - numberOfEnters*2);
		}
		else {
		   numberOfCharacters = numberOfCharacters - numberOfEnters;
		}
	}	

	//OUTPUT
	//Output Paragraphs
	if (numberOfParagraphs == 0) {
		document.forms[0].Paragraphs.value = (numberOfWords > 0 ? 1 : numberOfParagraphs);
	}
	else {
		document.forms[0].Paragraphs.value = numberOfParagraphs + 1;
	}
	//Output Words
	document.forms[0].Words.value = numberOfWords;
	//Output Characters
	document.forms[0].Characters.value = numberOfCharacters;

	//OUTPUT USER INPUT WITH BLANKS
	document.forms[0].InputTextAndBlanks.value = textStringAndBlanks;
	//window.alert(textStringAndBlanks);

	//If empty, fill the outputs
	if (textString == "") {
		fillInputForms();
	}
	if (inputWindow_visible) {
		document.forms[0].InputText.focus();
	}
	return;
}

function fillInputForms() {
	document.forms[0].Characters.value = 0;
	document.forms[0].Words.value = 0;
	document.forms[0].Paragraphs.value = 0;
}
//---------------------------------------------------------

//setSubmitButton------------------------------------------
var _global_submitbutton_status = "inactiv";

function setSubmitButton(action) {
	element_submitButton_inactiv = document.getElementById('button_submit_inactiv');
	element_submitButton_on = document.getElementById('button_submit_on');
	element_submitButton_over = document.getElementById('button_submit_over');

	switch(action) {
		case "on":
			element_submitButton_inactiv.style.visibility = 'hidden';
			element_submitButton_on.style.visibility = 'visible';
			element_submitButton_over.style.visibility = 'hidden';
			_global_submitbutton_status = "on";
			break;
		case "over":
			element_submitButton_inactiv.style.visibility = 'hidden';
			element_submitButton_on.style.visibility = 'hidden';
			element_submitButton_over.style.visibility = 'visible';
			_global_submitbutton_status = "over";
			break;
		case "inactiv":
			element_submitButton_inactiv.style.visibility = 'visible';
			element_submitButton_on.style.visibility = 'hidden';
			element_submitButton_over.style.visibility = 'hidden';
			_global_submitbutton_status = "inactiv";
			break;
		case "hidden":
			element_submitButton_inactiv.style.visibility = 'hidden';
			element_submitButton_on.style.visibility = 'hidden';
			element_submitButton_over.style.visibility = 'hidden';
			_global_submitbutton_status = "inactiv";
			break;
		default:
			break;
	}
}

function checkSubmitButton() {
	//Set SubmitButton
	if (document.forms[0].InputText.value != "") {
		setSubmitButton('on');
	}
	else {
		setSubmitButton('inactiv');
	}
}
//---------------------------------------------------------