/**
 * Box Panel version 1.05.
 * Author: Phanuwat Sukviboon.
 * Copyright (c) 2001-2005 Summit Computer Co., Ltd.
 * Create Date: 25 July '01.
  * Modify by: ___________________________
  * Modify Date: 6 Nov'01
 */

function BoxPanel(aName) {
//********************** Protected Properties  *******************************
	this.Display_Format="";
	this.Form_Name;
	this.Box_Object_Name = aName;
	this.Box_Size = 0;
	this.Stylesheet;
	this.MSStyle;
	this.When_LNF;
	this.MySelf;
	this.eventEnter;
//********************** Protected Method ***********************************
	this.setDisplayFormat=setDisplayFormat;
	this.createBoxStyle=createBoxStyle;
	this.createBoxEvent=createBoxEvent;
	this.createBoxElement=createBoxElement;
	this.lastNextField=lastNextField;	
	this.notSpecific=notSpecific;
	this.textbox_focus=textbox_focus;
	this.textbox_keyup=textbox_keyup;
	this.keyCheck=keyCheck;
//********************** Public Method ***************************************
	this.clearBox=clearBox;
	this.setStylesheet=setStylesheet;
	this.setFromName=setFromName;
	this.setDisabled=setDisabled;
	this.setEventEnter=setEventEnter;
	this.focus=focus;
	this.setMSStyle=setMSStyle;
	this.setVarMySelf=setVarMySelf;
	this.setSuffix=setSuffix;
	this.setWLNF=setWLNF;
	this.getValue=getValue;
	this.setValue=setValue;
	this.create_box=create_box;	
}  

function clearBox() {
	for (var i=1; i<=this.Box_Size; i++)
		eval("document." + this.Form_Name + "." + this.Box_Object_Name + i + ".value=''");
}
function setStylesheet(aName) {
	this.Stylesheet = aName;
}
function setEventEnter(func){
	this.eventEnter = func;
}
function setFromName(aName){	
	this.Form_Name = aName;
}
function focus(index){
		if (index == null)
			eval ("document." + this.Form_Name + "." + this.Box_Object_Name +  "1.focus()");
		else
			eval ("document." + this.Form_Name + "." + this.Box_Object_Name + index + ".focus()");
}
function setVarMySelf(varName){
	this.MySelf = varName;
}
function setMSStyle(aStyle){
	this.MSStyle = aStyle;
}
function setSuffix(flag){
	this.Suffix = flag;
}
function setWLNF(aName){
	this.When_LNF=aName;
}
function getValue() {
	var strValue="";
	for (var i=1; i<=this.Box_Size; i++)
		strValue += eval("document." + this.Form_Name + "." + this.Box_Object_Name + i + ".value");
	return strValue;	
}

function setValue(strValue){
	var prog, strTemp;
	var valueSize = strValue.length;
	for (var i=0; i<valueSize; i++)
	{
		strTemp = strValue.substr(i, 1);
		prog = "if (document." + this.Form_Name + "." + this.Box_Object_Name + (i +1) + " != null) " +
					"document." + this.Form_Name + "." + this.Box_Object_Name + (i +1) + ".value=" + "\"" + strTemp + "\"";						
		eval(prog);
	}
}

function setDisplayFormat(aFormat) {
	var strTmp;
	for (var i=0; i<aFormat.length; i++)
	{
		strTmp = aFormat.substr(i, 1);		if ((strTmp != '-') && (strTmp != "\n") && (strTmp != ",") && (strTmp != "."))			this.Display_Format += strTmp;
	}
	this.Box_Size = this.Display_Format.length;
}
function createBoxStyle(){
	var strStyle="";
	if (this.Stylesheet != null)
		strStyle += "class=\"" + this.Stylesheet + "\" ";
	if (this.MSStyle != null)
		strStyle += "style=\"" + this.MSStyle + "\" ";
	return strStyle
}
function createBoxEvent(){
	var strEvent;
	strEvent = "LANGUAGE=javascript " +
		"onKeyPress=\"return keyCheck(event, this, '"+ this.Box_Object_Name +"', '"+ this.Display_Format + "' )\" " +
		"onkeyup=\"return textbox_onkeyup(this,'" + this.Box_Object_Name + "','" + this.MySelf + "')\" " +
		"onfocus=\"return textbox_onfocus(this,'" + this.Box_Object_Name + "')\" ";
	return strEvent;
}
function createBoxElement(btName){
	var strElement = "<INPUT id=" + btName + " " +
		"name=" + btName + " " +
		"vaule=''  size=1 maxlength=1 " + 
		this. createBoxStyle() + 
		this.createBoxEvent() +
		">";		
	return strElement;
}
function create_box(displayFormat)
{	
	this.setDisplayFormat (displayFormat);
	var startTagTable = "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0><TR>";
	var endTagTable = "</TR></TABLE>";
	var order=0;
	document.write(startTagTable);			
	for (var i=0; i<displayFormat.length; i++) 
	{	
		if (displayFormat.charAt(i) == '\n')
		{
			document.write(endTagTable);		
			document.write(startTagTable);	
		}
		else if ((displayFormat.charAt(i) == '-') || (displayFormat.charAt(i) == ',') || (displayFormat.charAt(i) == '.'))
			document.write("<TD><B>" + displayFormat.charAt(i) + "</B></TD>");
		else
		{
			order += 1;
			btName = this.Box_Object_Name + order;
			document.write("<TD>");
			temp = this.createBoxElement( btName );
			document.write (temp);
			document.write("</TD>");
		}
	}
	document.write(endTagTable);
}

function notSpecific(index, element, disFormat)
{
	index--;	
	cSpec = disFormat.charAt(index);
	if (cSpec == '9')
	{
		if  (isNaN (element.value))
		{
			element.value="";
			return true;
		}	
	}
	if ((cSpec == 'C') || (cSpec == 'L') || (cSpec == 'U'))
	{
		if  (!isNaN (element.value))
		{
			element.value="";
			return true;
		}		
		if (cSpec == 'L')
			element.value = element.value.toLowerCase();		
		if (cSpec == 'U')
			element.value = element.value.toUpperCase();	
	}	
	return false;
}

function textbox_keyup( boxName) //keyPress
{
	code = window.event.keyCode;
	if (boxName == null)
		return;
	objBox = eval("document." + this.Form_Name + "." + boxName);
	if (code == 13)
	{
		if (this.eventEnter != null)
		{
			if (!eval(this.eventEnter))
				return;
		}
		this.lastNextField();
		return;
	}
	nameSize = this.Box_Object_Name.length;
	order = boxName.substr(nameSize);
	order = parseInt(order);
	if ((code == 8) || (code == 37))
	{		
		order -= 1;
		if (code == 8) objBox.value = "";
	}	
	else
	{
		if (objBox.value == "") return;
		if (this.notSpecific(order, objBox, this.Display_Format)) return;
		order += 1;
		// if (order > this.Box_Size)	this.lastNextField(); //Automatic next field
	}
	eval ("if (document." + this.Form_Name + "." + this.Box_Object_Name + order +"!=null) document." + this.Form_Name + "." + this.Box_Object_Name + order + ".focus ()");
}

function textbox_onkeyup( objBox, boxName, myName){
		eval (myName + ".textbox_keyup(\"" + objBox.name + "\")");
}
function textbox_focus(objBox) {
	objBox.select();
}

function lastNextField(){
	if (this.When_LNF != null)
		this.When_LNF.focus();
}

function textbox_onfocus(objBox, boxName) {
	this.textbox_focus(objBox); 
}

function setDisabled(flag)
{
	for (var i=1; i<=this.Box_Size; i++)
	{
		if (flag)
			eval("document." + this.Form_Name + "." + this.Box_Object_Name + i + ".disabled=true");
		else
			eval("document." + this.Form_Name + "." + this.Box_Object_Name + i + ".disabled=false");
	}
}

function keyCheck(eventObj, obj, orName, dFormat){
	var keyCode
	if (document.all)
		keyCode=eventObj.keyCode;
	else
		keyCode=eventObj.which;
	var str=obj.value;
	var pname = obj.name;
	order = pname.substr(orName.length);
	order = parseInt(order);
	cSpec = dFormat.charAt(order-1);
	if (cSpec == '9')
	{
		if(keyCode<48 || keyCode >58)
			return false;
	}
	else
	{
		if(keyCode>=48 && keyCode <=58) 
			return false;	
	}
	return true;
}


