var SpellCheckInstance = null;
SpellCheckDialog = function(id) 
{ 
	this._id = id;
	this._index = 0;
	this._height = 0;
	this._errorindex = 0;
    this._field = null;
    this._content = null;
    this._errors = null;
    this._suggestions = null;
    this._suggestions2 = {};
    this._ignoreAll = {};
    this._word = '';
	this._from = 0;
	this._to = 0;
	this._changed = 0;
	this._frame = null;
	this._defaultContent = ''
}
SpellCheckDialog.prototype = 
{
	display : function()
	{
		var fields = new Array();
		var f = document.getElementById(this._id);
		this._height = f.clientHeight;
		if (f.value.length)
		{
			this._content = f.value;
			this._defaultContent = f.value;
			var result = jobviteexecute('spellcheck', '', f.value);
			if (result.length)
			{
				var args = result.split('\n');
				var i = 0;
				this._errors = new Array();
				
				for (; i < args.length; i++)
				{
					if (!args[i].length)
						break;
					this._errors[this._errors.length] = args[i] - 0;
				}
				
				i++;
						
				for (; i < args.length; )
				{
					var word = args[i++];
					var list = new Array();
					
					for (; i < args.length; i++)
					{
						if (!args[i].length)
							break;
						list[list.length] = args[i];
					}
					this._suggestions2[word] = list;
					i++;
				}
			}
		}
		this.display3();
	},
	display3 : function()
	{
		this._index = 0;
		this._errorindex = 0;
		this._field = null;
		this._ignoreAll = {};
		this._word = '';
		this._from = 0;
		this._to = 0;
		this._changed = 0;

		var s = '<table cellSpacing="0" cellPadding="0" border="0" class="jobvitespellcheck"><tr><td class="jobvitespellchecktop jobvitebold jobvitedefault">Spell Check</td></tr><tr><td class="jobvitespellcheckright"><iframe id="spellcheckcontent" style="height: ' + this._height + 'px;" src="" frameBorder="yes" scrolling="yes"></iframe></td></tr>';

		if (!this._errors)
		{
			s += '<tr><td id="spellcheckcell" colSpan="2" class="jobvitespellcheckbottom"><table cellSpacing="0" cellPadding="0" width="100%" border="0">' +
					'<tr></tr>' +
					'<tr><td><table cellSpacing="0" cellPadding="0" width="100%" border="0"><tr><td class="jobvitebold jobvitedefault">No&nbsp;spelling&nbsp;errors&nbsp;found</td><td width="99%"><img src="http://www.jobvite.com/images2/spix.gif" width="5" height="5"></td>' + addButton('Continue', 'SpellCheckInstance.cancel()') + '<tr></table></td></tr>' +
					'</table></td></tr>';
		}
		else
		{
			s += '<tr><td id="jobvitespellcheckcontrols" class="jobvitespellcheckbottom"><table cellSpacing="0" cellPadding="0" width="100%" border="0">' +
//					'<tr><td class="jobvitedefault"><span id="changedNo" class="small">0 words changed</span></td></tr>' +
					'<tr><td class="jobvitedefault" width="50%">Suggestions</td><td class="jobvitedefault" width="50%">Change to:&nbsp;</td></tr>' +
					'<tr><td class="jobvitespace" colspan=\"2\">&nbsp;</td></tr>' +
					'<tr><td width="50%" style="padding-right: 10px;"><span id="suggestions"></span></td><td valign="top" width="50%"><input id="changeTo" type="text" value="" style="width: 180px;"/></td></tr>' +
					'<tr><td class="jobvitespace2" colspan=\"2\">&nbsp;</td></tr>' +
					'<tr><td align="right" colspan="2"><table cellSpacing="0" cellPadding="0" border="0"><tr>' + addButton('Ignore', 'SpellCheckInstance.ignore()') + addButtonSpace() + addButton('Ignore&nbsp;All', 'SpellCheckInstance.ignoreAll()') + addButtonSpace() + addButton('Change', 'SpellCheckInstance.change()') + addButtonSpace() + addButton('Change&nbsp;All', 'SpellCheckInstance.changeAll()') + '<tr></table></td></tr>' +
					'<tr><td class="jobvitespace" colspan=\"2\">&nbsp;</td></tr>' +
					'<tr><td align="right" colspan="2"><table cellSpacing="0" cellPadding="0" border="0"><tr>' + addButton('Cancel', 'SpellCheckInstance.cancel()') + '<tr></table></td></tr>' +
					'</table></td></tr>';
		}

		if (jobviteid.length)
			jobvitedocumentcancel(jobviteid);
		jobviteenablesubmit(false);
		jobviteid = this._id;
		document.getElementById(this._id + 'div').style.display = 'none';
		document.getElementById(this._id + 'spellcheck').innerHTML = s;
		document.getElementById(this._id + 'spellcheck').style.display = 'inline';
		var o = document.getElementById('suggestions');
		var e = document.createElement('select');
		o.appendChild(e);
		e.id = 'selectSuggestions';
		e.size = 5;
		e.style.width = '100%';
		e.onchange = jobviteupdateselection;

		this._frame = document.getElementById('spellcheckcontent');
		window.setTimeout('SpellCheckInstance.initialize()', 10);
	},
	initialize : function()
	{
		if (this._frame.contentWindow)
		{
			this._frame.contentWindow.document.body.innerHTML = 
				'<span id="content"></span>\n';
			this._frame.contentWindow.document.body.style.marginLeft = '3px';
			this._frame.contentWindow.document.body.style.marginTop = '3px';
			this._frame.contentWindow.document.body.style.marginRight = '3px';
			this._frame.contentWindow.document.body.style.marginBottom = '3px';
			this._frame.contentWindow.document.body.style.fontFamily = 'Verdana, Arial, Helvetica, sans-serif';
			this._frame.contentWindow.document.body.style.fontSize = '12px';
			this.nextSection();
		}
		else
			window.setTimeout('SpellCheckInstance.initialize()', 10);
	},
    nextSection : function() 
    {
		if (!this._errors)
		{
			this.framedisplay(0, 0);
			return
		}
		
		if (this._index == 0)
		{
			this._index++;
			this._errorindex = 0;
			this.next();
		}
		else
		{
			this.framedisplay(this._from, this._from);
			var o = document.getElementById('selectSuggestions');
			
			if (o)
			{
				while (o.options.length)
					o.remove(0);
			}
			document.getElementById('jobvitespellcheckcontrols').innerHTML =
				'<table cellSpacing="0" cellPadding="0" width="100%" border="0">' +
				'<tr><td><img src="http://www.jobvite.com/images2/spix.gif" width="5" height="5"></td></tr>' +
//				'<tr><td>' + (this._changed == 1 ? '1 word' : this._changed + ' words') + ' will be changed.</td></tr>' +
//				'<tr><td><img src="http://www.jobvite.com/images2/spix.gif" width="5" height="5"></td></tr>' +
				'<tr><td><table cellSpacing="0" cellPadding="0" border="0"><tr><td class="jobvitebold jobvitedefault">Spell&nbsp;Check&nbsp;is&nbsp;complete</td><td width="99%"><img src="http://www.jobvite.com/images2/spix.gif" width="5" height="5"></td>' + addButton('Cancel', 'SpellCheckInstance.cancel()') + addButtonSpace() + addButton('Save', 'SpellCheckInstance.finish()') + '<tr></table></td></tr>' +
				'</table>';
		}
    },
    next : function()
    {
		for (;;)
		{
			if (this._errorindex < this._errors.length)
			{
				this._from = this._errors[this._errorindex++];
				this._to = this._errors[this._errorindex++];
				if (this._from == this._to)
					continue;
				this._word = this._content.substring(this._from, this._to);
				if (this._ignoreAll[this._word])
					continue;
				this.framedisplay(this._from, this._to);
				this.updateSuggestions();
				document.getElementById('changeTo').focus();
			}
			else
				this.nextSection();
			break;
		}
    },
	framedisplay : function(from, to)
	{
        this._frame.contentWindow.document.getElementById('content').innerHTML = this.toHtml(this._content.substring(0, from) + 'fjmarkerstart' + this._content.substring(from, to) + 'fjmarkerend' + this._content.substring(to));
        var o = this._frame.contentWindow.document.getElementById('wordspan').offsetTop;
        o -= Math.round(this._height / 2);
        if (o < 0)
			o = 0;
        this._frame.contentWindow.window.scrollTo(0, o);
	},
	toHtml : function(s)
	{
		s = s.replace(/(&)/g, '&amp;');
		s = s.replace(/(<)/g, '&lt;');
		s = s.replace(/(>)/g, '&gt;');
		s = s.replace(/(\r)/g, '');
		s = s.replace(/(\n)/g, '<br>');
		s = s.replace(/fjmarkerstart/g, '<span id="wordspan" style="background-color: Yellow;">');
		s = s.replace(/fjmarkerend/g, '</span>');
		
		return s;
	},
    updateSuggestions : function()
    {
		var list = this._suggestions2[this._word];
		var o = document.getElementById('selectSuggestions');
		
		if (o)
		{
			while (o.options.length)
				o.remove(0);
			
			if (list && list.length)
			{
				for (var i = 1; i < list.length; i++)
				{
					var opt = document.createElement('option');
					o.appendChild(opt);
					opt.innerHTML = list[i];
					opt.value = list[i];
				}
			}
			o.selectedIndex = 0;
		}
		this.updateSelection();
    },
    updateSelection : function()
    {
		var s = document.getElementById('selectSuggestions');
		if (s && s.value.length)
			document.getElementById('changeTo').value = s.value;
		else
			document.getElementById('changeTo').value = this._word;
    },
	change : function()
	{
		var newWord = document.getElementById('changeTo').value;
		this._content = this._content.substring(0, this._from) + newWord + this._content.substring(this._to);
		this._changed++;
		this.updateChanged();
		this.adjustOffsets(this._errorindex, newWord.length - this._word.length);
		this.next();
	},
	changeAll : function()
	{
		var newWord = document.getElementById('changeTo').value;
		this._content = this._content.substring(0, this._from) + newWord + this._content.substring(this._to);
		this._changed++;
		this.adjustOffsets(this._errorindex, newWord.length - this._word.length);

		for (var i = this._errorindex; i < this._errors.length; )
		{
			var from = this._errors[i++];
			var to = this._errors[i++];
			if (this._content.substring(from, to) == this._word)
			{
				this._content = this._content.substring(0, from) + newWord + this._content.substring(to);
				this._changed++;
				this._errors[i - 1] = from;
				this.adjustOffsets(i, newWord.length - this._word.length);
			}
		}
		
		this.updateChanged();
		this.next();
	},
	ignore : function()
	{
		this.next();
	},
	ignoreAll : function()
	{
		this._ignoreAll[this._word] = true;
		this.next();
	},
	finish : function()
	{
		document.getElementById(this._id).value = this._content;
		this.cancel();
	},
	cancel : function()
	{
		document.getElementById(this._id + 'spellcheck').innerHTML = '';
		document.getElementById(this._id + 'div').style.display = 'inline';
		jobviteenablesubmit(true);
	},
	updateChanged : function()
	{
//		document.getElementById('changedNo').innerHTML = (this._changed == 1 ? '1 word changed' : this._changed + ' words changed');
	},
	adjustOffsets : function(index, adjust)
	{
		for (var i = index; i < this._errors.length; i++)
			this._errors[i] += adjust;
	}
}
function SpellCheckOnError(args)
{
}
function jobvitespellcheck(id)
{
	SpellCheckInstance = new SpellCheckDialog(id);
	SpellCheckInstance.display();
}
function addButton(name, f)
{
	return '<td>' +
		'<input type="button" class="jobvitebutton" value="' + name + '" onclick="' + f + '">' +
		'</td>';
}
function addButtonSpace()
{
	return '<td>&nbsp;</td>';
}
function jobviteupdateselection()
{
	SpellCheckInstance.updateSelection();
}