//------------------------------------------------------------------
function WP_TextBox_SplittedByCells_InitAll(clientID)
{
	var tb = new WP_TextBox_SplittedByCells(clientID);
	tb.Init();
}
function WP_TextBox_SplittedByCells(clientID)
{
	this.cMain		= document.getElementById(clientID+"_mainPanel");
	this.enabled	= this.cMain.c_enabled;
	this.cText		= document.getElementById(clientID+"_textValue");
	this.cCells		= document.getElementById(clientID+"_panCells");

	//this.cCells.setAttribute("c_token", "TextBoxSplittedByCells");
	this.cCells.obj = this;
	this.maxlen		= document.getElementById(clientID+"_hidMaxLength").value;
	this.doTrimLeft	= document.getElementById(clientID+"_hidTrimLeft").value=="y";
	this.doTrimRight= document.getElementById(clientID+"_hidTrimRight").value=="y";

	this.Init = WP_TextBox_SplittedByCells_Init;
}
//------------------------------------------------------------------
function WP_TextBox_SplittedByCells_Init()
{
	WP_TextBox_SplittedByCells_DoInit(this);
}
//------------------------------------------------------------------
function WP_TextBox_SplittedByCells_DoInit(obj)
{
//alert(obj.maxlen);
	obj.arr = new Array();
	var ind=0;
	var val="";try{val=obj.cText.value;}catch(e){}
	
	//var ss = "";
/*
	for(var j=0; j<obj.maxlen; j++)
	{
		ss += "<input type=text " + (obj.enabled=="y" ? "" : "disabled readonly ") + 
				"style='font-weight:bold; border:1px solid #aaaaaa; font-size:9pt;" + (obj.enabled=="y" ? "" : "background-color:gainsboro;") + "text-align:center; width:20;' " + 
				"maxlength=1>&nbsp;";
	}
	ss+="<span style='color:#999999;font-weight:bold;font-size:8pt;'></span>";

	obj.cCells.innerHTML = ss;
	*/
//alert(obj.cText.value);
	var a = obj.cCells.getElementsByTagName("INPUT");
	//for(var j=0; j<obj.cCells.all.length; j++)
	for(var j=0; j<a.length; j++)
	{
		var o = a[j];
		if (o.tagName=="INPUT" && o.type=="text")
		{
			obj.arr.push(o);
			//o.maxLength = "1";
			//o.onkeyup	= WP_TextBox_SplittedByCells_OnKeyDown;
			o.onkeyup = WP_TextBox_SplittedByCells_PressKey;
			o.setAttribute("c_ind", ind);
			try{o.value=val.substr(ind,1);}catch(e){}
			//o.readOnly = readOnly;
			ind++;
		}
	}
}
//------------------------------------------------------------------
function WP_TextBox_SplittedByCells_Designer(pan,maxlen)
{
	this.cCells = pan;
	this.cCells.c_token = "TextBoxSplittedByCells";
	this.cCells.obj = this;
	this.maxlen	= maxlen;
	
	this.Init = WP_TextBox_SplittedByCells_Init;
}
//------------------------------------------------------------------
//function WP_TextBox_SplittedByCells_OnKeyDown()
//{
//	WP_TextBox_SplittedByCells_PressKey();
//}
//------------------------------------------------------------------
function WP_TextBox_SplittedByCells_PressKey(e)
{
	//::::::::::::::::::::::::::::: IE, NC, FF, Opera, Safari, IE(Mac)
	var ev = (e) ? e : window.event;
	var k = (ev.keyCode) ? ev.keyCode : ev.which;
	var o = (ev.srcElement) ? ev.srcElement : ev.target;
	if (o.nodeType == 3) o = o.parentNode;	// defeat Safari bug
	//::::::::::::::::::::::::::::: 
	
	var ind=1*o.getAttribute("c_ind");
	var c=o;while(c.getAttribute("c_token")!="TextBoxSplittedByCells")c=c.parentNode;
	var arr=c.obj.arr;
	var moveCursor = false;
	
	if (k != null)
	{
		try
		{
			if (		k==39	//...arrow Right
					||	k==48	//...arrow Down
					||	k==34	//...PgDn
					)	
			{
				if (ind<arr.length-1)
				{
					ind++;
					moveCursor = true;
				}
			}
			else if (	k==37	//...arrow Left
					||	k==38	//...arrow Up
					||	k==33	//...PgUp
					)
			{
				if (ind>0)
				{
					ind--;
					moveCursor = true;
				}
			}
			else if (	k==36)	//...Home
			{
				ind=0;
				moveCursor = true;
			}
			else if (	k==35)	//...End
			{
				ind=arr.length-1;
				moveCursor = true;
			}
		//	else if (	k==8)	//...Backspace
		//	{
		//		ind=arr.length-1;
		//		moveCursor = true;
		//	}
			else if (	k==45)	//...Insert
			{
				for (var j=arr.length-2; j>=ind; j--)
				{
					arr[j+1].value = arr[j].value;
				}
				arr[ind].value = "";
				moveCursor = true;
			}
			else if (	k==46)	//...Delete
			{
				if (o.value.length == 0)
				{
					for (var j=ind+1; j<arr.length; j++)
					{
						arr[j-1].value = arr[j].value;
					}
					arr[arr.length-1].value = "";
					moveCursor = true;
				}
			}
			else 
			{
				if (o.value.length==1)
				{
					if (ind<(arr.length-1))
						ind++;
					moveCursor = true;
				}
			}
		}
		catch(e){}
	}
		
	//...update text value	
	if (c.obj.cText != null)
	{
		var ss="",v;
		var i1=-1,i2=-1;
		for(var j=0; j<arr.length; j++)
		{
			v = arr[j].value;
			if (v=="")v=" ";
			if (v!=" ")
			{
				i2=j;		//...last non-space/non-empty symbol on the Right
				if (i1==-1)
					i1=j;	//...1st non-space/non-empty symbol on the Left
			}
			ss+=v;
		}
		
		//...Trim string if needed
		if (c.obj.doTrimLeft){	if (i1==-1)	ss="";	else	ss=ss.substring(i1);	}
		if (c.obj.doTrimRight){	if (i2==-1)	ss="";	else	ss=ss.substr(0,i2+1);	}
		
		//...set textBox value and rise event for validators	
		c.obj.cText.value = ss;
		c.obj.cText.blur();
//		try
//		{
//			c.obj.cText.fireEvent("onchange");
//		}
//		catch(e)
//		{
//			
//		}
	}
	
	try{ o.click(); }catch(e){}
    try{ o.focus(); }catch(e){}
	
	//...move cursor if needed
	if (moveCursor)
	{
	    try{
    		o = arr[ind];
	    	o.focus();
		    o.select();
		}catch(e){}
	}
//alert(c.obj.cText.value);
}
//------------------------------------------------------------------
function WP_TextBox_CheckMaxLen_ToBlur()
{
	try
	{
		if (!WP_TextBox_CheckMaxLen_OnKey())
		{
			var o = event.srcElement;
			o.focus();
			o.click();
		}
	}catch(e){}
}
//------------------------------------------------------------------
function WP_TextBox_CheckMaxLen_OnKey()
{
	try
	{
		var o			= event.srcElement;
		var len			= o.c_maxLength*1;
		var pan=o;while(pan.c_token!="TBPanel")pan=pan.parentElement;
		var lbl			= pan.all(o.c_clientID+"_lMaxLenMsg");
		if (o.value.length > len)
		{
			lbl.innerHTML = "<br>^ Value length exceeds maximum of " + len + " characters allowed.";
			event.cancelBubble = true;
			return false;
		}
		
		lbl.innerHTML = "";
		return true;
	}catch(e){}
}