var mark ='\uE000';
var punct = " .,-;:/\\!@#$%&*()+={}[]<>?\n\r";

function isPunct(ch)
{
	return (punct.indexOf(ch)>=0);
}

function LSState(tfield,display,bWords)
{
	this.mEdit = tfield;
	this.mDisplay = display;
	this.bWordSearch = bWords;
	this.cword=null;
	this.cw=null;
}

LSState.prototype.bWordSearch;
LSState.prototype.mEdit;
LSState.prototype.mDisplay;
LSState.prototype.mCWBegin;
LSState.prototype.mCWEnd;
LSState.prototype.cword;
LSState.prototype.cw;
LSState.prototype.curP;
LSState.prototype.curLocX;
LSState.prototype.curLocY;

LSState.prototype.updateText = function updTBox(){}

LSState.prototype.displaytext = function showText()
{
	var s="";
	if(this.bWordSearch && this.cword!=null)
	{
		var i,m,l,c,corg, off,woff;
		var s2;
		var me = this.mEdit.value;
		corg = this.getcwnum();
		c=Math.abs(corg);
		off=0;
		woff=this.curP-this.mCWBegin;
		m=0;
		l=me.length;
		for(i=0;i<this.cword.length;i++)
		{
			while((  (i<=c && m<this.mCWBegin )
			      ||(i>c && m<l )) 
			   && this.bWordSearch && isPunct(me.charAt(m)))
			{
				s+=me.charAt(m);
				if(i<=c)
				  off++;
				m++;
			}
			
			if (i != c) {
				s2 = this.cword[i].finaltext();
				if(s2==null || s2.length==0 && this.cword[i].text()!=null)
				  s2=this.cword[i].text(); //fallback on roman
			}
			else {
				if (corg >= 0) 
					s2 = this.cword[i].text();
				else {
					c = corg;
					i--;
					s2 = null;
				}
			}
			if (s2 != null) {
				s += s2;
				if (i < c) 
					off += s2.length;
			}
			while((  (i<c && m<this.mCWBegin )
			      ||(i>=c && m<l )) 
			     && (!this.bWordSearch || !isPunct(me.charAt(m))))
			    m++;
				
			
		}
		//trailing punct, if any
		while(m<l && this.bWordSearch && isPunct(me.charAt(m)))
		{
			s+=me.charAt(m);
			if(i<c)
			  off++;
			m++;
		}
		this.mEdit.value=s;
		
		off+=woff;
		this.mEdit.focus();
		if (document.selection && document.selection != 'undefined') {
			var r = curfield.createTextRange();
			r.move('character', -s.length);
			r.collapse();
			r.move('character', off);
			r.select();
		}
		else {
			curfield.selectionStart = off;
			curfield.selectionEnd = off;
		}
	}
	return s;
}


LSState.prototype.curword = function getCurWord(){
   return this.cword[this.cw];
}

LSState.prototype.getcw = function getcurWord(){
	if (this.cword != null) {
	  var w = this.getcwnum();
	  if(w>=0)
	     return this.cword[w];
	}
	return null;
}
LSState.prototype.getcwnum = function getCurWNum()
{
	var me = this.mEdit;
	var wnum = 0;
	var x;
	
	me.focus();
	if (document.selection && document.selection != 'undefined') {
		me.caretPos = document.selection.createRange().duplicate();

		var cp = me.caretPos;
		var s = cp.text;
		cp.text = "" + mark;
		x = me.value.indexOf(mark);
		cp.moveStart('character', -1);
		cp.text = s;
	}
	else 
		x = me.selectionStart;
	this.curP = x;
	this.mCWBegin = x
	if (x > 0 && (!this.bWordSearch || !isPunct(me.value.charAt(x-1))) ) {
		this.mCWBegin--;
		while (this.mCWBegin >= 0 && (!this.bWordSearch || !isPunct(me.value.charAt(this.mCWBegin)))) 
			this.mCWBegin--;
		this.mCWBegin++;
	}
	this.mCWEnd = x;
	
	while (this.mCWEnd < me.value.length &&(!this.bWordSearch|| !isPunct(me.value.charAt(this.mCWEnd)))) 
		this.mCWEnd++;
	if (this.mCWEnd>=x && this.mCWEnd>this.mCWBegin){
		this.mCWEnd--;
	}
	var i;
	if (this.cword != null) {
		for (i = 0; i < this.mCWBegin; i++) {
			if (this.bWordSearch && isPunct(me.value.charAt(i))) {
				while (i < this.mCWBegin &&(this.bWordSearch && isPunct(this.mCWBegin && me.value.charAt(i)))) 
					i++; //skip consecutive spaces
				wnum++;
			}
		}
	}	
	if(this.mCWBegin==this.mCWEnd && this.mCWBegin<me.value.length && isPunct(me.value.charAt(this.mCWBegin)))
	   wnum*=-1;
	return wnum;
}

LSState.prototype.save = function saveState(){

	var me = this.mEdit;
	var bRefresh=false;
	var wnum,worg;
	worg = this.getcwnum();
	wnum = Math.abs(worg);
	if(this.cword==null)
	{   this.cword = new Array();
	    this.cword[0]= new Word(me.value.substring(this.mCWBegin,this.mCWEnd+1),0);
		wnum=0;
	}
    else
    {
		if (wnum==this.cword.length) //adding to the end
		{
			this.cword[wnum]= new Word(me.value.substring(this.mCWBegin, this.mCWEnd + 1), wnum); 
		}
		else 
		{
			var i,j;
			var bottom=0;
			var s;

			for(i=this.mCWEnd;i<me.value.length;i++)
			{
				if (this.bWordSearch && isPunct(me.value.charAt(i))) {
					while (i < me.value.length && isPunct(me.value.charAt(i))) 
						i++; //skip consecutive spaces
					bottom++;
				}
			}
			bottom = this.cword.length-bottom-1;
			if (wnum == bottom) {
				if(this.cw!=wnum)
				{
					//no update?
				}
				else
				   this.cword[wnum] = new Word(me.value.substring(this.mCWBegin, this.mCWEnd + 1), wnum);
			}
			else {
				var ta = new Array();
				s = (worg >= 0) ? me.value.substring(this.mCWBegin, this.mCWEnd + 1) : "";
				for (i = 0; i < wnum; i++) 
					ta[i] = this.cword[i];
				if (s != null && s.length > 0) {
					if (wnum < this.cword.length && s == this.cword[wnum].finaltext() && this.cword[wnum].getwnum() == wnum) {
						ta[i++] = this.cword[wnum];
						bRefresh = true;
					}
					else 
						ta[i++] = new Word(s, wnum);
				}
				for (j = bottom + 1; j < this.cword.length; j++, i++) {
					ta[i] = this.cword[j];
					ta[i].setWNum(i);
				}
				this.cword = ta;
			}
		}
	}
	this.cw = wnum;
	if(bRefresh)
	  this.displaytext();
	return wnum;
}


LSState.prototype.saveLocation = function saveLoc(tb){
  var left=0, top=0;
		if (bIE) {
			var cp = tb.createTextRange();
			cp.collapse();
			cp.move('character',this.mCWBegin);
			
			left = cp.offsetLeft;
			top = cp.offsetTop+25;
			
			if(tb.tagName=='INPUT')
			{
		       left+=bufLeft;
			   top+=bufTop;				
			}
			cp.move('character', (this.curP - this.mCWBegin));
		}
		else {
			var x=0;
			if(this.curP>this.mCWBegin)
			  x=1;
			 
			tb.shadowPos.innerHTML = tb.value.substring(0, this.mCWBegin+x) 
			                           + "<span id=\"stxt\"></span>"
									   + tb.value.substring(this.mCWBegin+x)
									   
			var el = document.getElementById("stxt");
			if (el != null) {
					top = el.offsetTop + 25 + bufTop;
					left = el.offsetLeft + bufLeft;
			   
			}
			
		}

 this.curLocX=left;
 this.curLocY=top;

}


