/*********************************************************************************************
 * Copyright 2002 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: FieldCombFrm.cs $     $Revision: 12 $
 * $Author: Kathy $   $Date: 5/21/03 12:45p $
 *
 * $History: FieldCombFrm.cs $
 * 
 * *****************  Version 12  *****************
 * User: Kathy        Date: 5/21/03    Time: 12:45p
 * Updated in $/EXE/RefObj/ROEditor
 * B2003-041: edit field name not always set
 * 
 * *****************  Version 11  *****************
 * User: Kathy        Date: 12/11/02   Time: 11:22a
 * Updated in $/EXE/RefObj/ROEditor
 * fixed - was always putting up Variable field, even if not defined
 * 
 * *****************  Version 10  *****************
 * User: Kathy        Date: 12/10/02   Time: 2:25p
 * Updated in $/EXE/RefObj/ROEditor
 * fieldname special chars
 * 
 * *****************  Version 9  *****************
 * User: Kathy        Date: 12/02/02   Time: 8:29a
 * Updated in $/EXE/RefObj/ROEditor
 * fieldname replace chars
 * 
 * *****************  Version 8  *****************
 * User: Kathy        Date: 12/02/02   Time: 6:13a
 * Updated in $/EXE/RefObj/ROEditor
 * combo field fix
 * 
 * *****************  Version 7  *****************
 * User: Kathy        Date: 10/15/02   Time: 2:17p
 * Updated in $/EXE/RefObj/ROEditor
 * wasn't finding variable text
 * 
 * *****************  Version 6  *****************
 * User: Kathy        Date: 9/27/02    Time: 1:13p
 * Updated in $/EXE/RefObj/ROEditor
 * fix digit as first char in fieldname
 * 
 * *****************  Version 5  *****************
 * User: Kathy        Date: 9/25/02    Time: 2:30p
 * Updated in $/EXE/RefObj/ROEditor
 * multitxt->variabletxt
 * 
 * *****************  Version 4  *****************
 * User: Kathy        Date: 9/25/02    Time: 9:56a
 * Updated in $/EXE/RefObj/ROEditor
 * dev
 * 
 * *****************  Version 3  *****************
 * User: Kathy        Date: 9/11/02    Time: 1:15p
 * Updated in $/EXE/RefObj/ROEditor
 * vlnxml
 * 
 * *****************  Version 2  *****************
 * User: Kathy        Date: 9/05/02    Time: 12:41p
 * Updated in $/EXE/RefObj/ROEditor
 * dev
 * 
 * *****************  Version 1  *****************
 * User: Jsj          Date: 8/23/02    Time: 3:33p
 * Created in $/EXE/RefObj/ROEditor
 *********************************************************************************************/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.Text;
using ROFields;
using RODBInterface;
namespace ROEditor
{
	/// 
	/// Summary description for FieldCombFrm.
	/// 
	public class FieldCombFrm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lblFieldName;
		private System.Windows.Forms.TextBox tbFieldName;
		private System.Windows.Forms.GroupBox gbAlternatives;
		private System.Windows.Forms.CheckBox cbSingleTxt;
		private System.Windows.Forms.CheckBox cbVariableTxt;
		private System.Windows.Forms.CheckBox cbTable;
		private System.Windows.Forms.CheckBox cbXYPlot;
		private System.Windows.Forms.Label lblW1;
		private System.Windows.Forms.Label lblW2;
		private System.Windows.Forms.Label lblW3;
		private System.Windows.Forms.Label lblW4;
		private System.Windows.Forms.TextBox tbSingleTxtWid;
		private System.Windows.Forms.TextBox tbVariableTxtWid;
		private System.Windows.Forms.TextBox tbTableWid;
		private System.Windows.Forms.TextBox tbXYPlotWid;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		/// 
		/// Required designer variable.
		/// 
		private System.ComponentModel.Container components = null;
		private ROField myrof;
		private RODB myrodb; 
		private VlnXmlElement myelem;
		private string origname;
		private int origFixedLength=0;
		private int origVariableTxtLength = 0;
		private int origTableLength = 0;
		private int origXYPlotLength = 0;
		private ArrayList avail_list;
		private ArrayList inuse_list;
		private uint editlevel;
		private bool exists = false;
		public FieldCombFrm(ROField rof, RODB rodb, VlnXmlElement elem, uint elevel, string oname, ArrayList alist, ArrayList ilist)
		{
			int idx;
			string strlen;
			string fielddef;
			//
			// Required for Windows Form Designer support
			//
			myrof = rof;
			myrodb = rodb;
			myelem = elem;
			editlevel = elevel;
			avail_list = alist;
			inuse_list = ilist;
			InitializeComponent();
			this.tbFieldName.Text = rof.GetFieldname;
			origname = oname;
			if (rof.GetRecID != null) // it's new if it's null
			{
				exists = true;
				string recid = rof.GetMasterRecID;
				if (recid != null && recid != "")
					fielddef = myrodb.RODB_GetSchemaPiece(recid,"ROMaster");
				else
					fielddef = myrodb.RODB_GetSchemaPiece(rof.GetRecID,myelem.GetAttribute("Table"));
				// search for combination items included & their original length.
				idx = fielddef.IndexOf("Fixed");
				if (idx >= 0) 
				{
					idx=fielddef.IndexOf("xsd:maxLength value=\"",idx);
					strlen = fielddef.Substring(idx+21,2);
					origFixedLength = System.Convert.ToInt32(strlen);
					this.tbSingleTxtWid.Text = strlen;
					this.cbSingleTxt.Checked = true;
				}
				else
					this.cbSingleTxt.Checked = false;
				idx = fielddef.IndexOf("type=\"Variable");
				if (idx >= 0) 
				{
					idx=fielddef.IndexOf("xsd:maxLength value=\"",idx);
					strlen = fielddef.Substring(idx+21,2);
					origVariableTxtLength = System.Convert.ToInt32(strlen);
					this.tbVariableTxtWid.Text = strlen;
					this.cbVariableTxt.Checked = true;
				}
				else
					this.cbVariableTxt.Checked = false;
				idx = fielddef.IndexOf("Table");
				if (idx >= 0) 
				{
					idx=fielddef.IndexOf("xsd:maxLength value=\"",idx);
					strlen = fielddef.Substring(idx+21,2);
					origTableLength = System.Convert.ToInt32(strlen);
					this.tbTableWid.Text = strlen;
					this.cbTable.Checked = true;
				}
				else
					this.cbTable.Checked = false;
				idx = fielddef.IndexOf("XYPlot");
				if (idx >= 0) 
				{
					idx=fielddef.IndexOf("xsd:maxLength value=\"",idx);
					strlen = fielddef.Substring(idx+21,2);
					origXYPlotLength = System.Convert.ToInt32(strlen);
					this.tbXYPlotWid.Text = strlen;
					this.cbXYPlot.Checked = true;
				}
				else
					this.cbXYPlot.Checked = false;
			}
		}
		/// 
		/// Clean up any resources being used.
		/// 
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		/// 
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// 
		private void InitializeComponent()
		{
			this.lblFieldName = new System.Windows.Forms.Label();
			this.tbFieldName = new System.Windows.Forms.TextBox();
			this.gbAlternatives = new System.Windows.Forms.GroupBox();
			this.tbXYPlotWid = new System.Windows.Forms.TextBox();
			this.tbTableWid = new System.Windows.Forms.TextBox();
			this.tbVariableTxtWid = new System.Windows.Forms.TextBox();
			this.tbSingleTxtWid = new System.Windows.Forms.TextBox();
			this.lblW4 = new System.Windows.Forms.Label();
			this.lblW3 = new System.Windows.Forms.Label();
			this.lblW2 = new System.Windows.Forms.Label();
			this.lblW1 = new System.Windows.Forms.Label();
			this.cbXYPlot = new System.Windows.Forms.CheckBox();
			this.cbTable = new System.Windows.Forms.CheckBox();
			this.cbVariableTxt = new System.Windows.Forms.CheckBox();
			this.cbSingleTxt = new System.Windows.Forms.CheckBox();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.gbAlternatives.SuspendLayout();
			this.SuspendLayout();
			// 
			// lblFieldName
			// 
			this.lblFieldName.Location = new System.Drawing.Point(16, 8);
			this.lblFieldName.Name = "lblFieldName";
			this.lblFieldName.Size = new System.Drawing.Size(72, 16);
			this.lblFieldName.TabIndex = 0;
			this.lblFieldName.Text = "Field Name:";
			// 
			// tbFieldName
			// 
			this.tbFieldName.Location = new System.Drawing.Point(96, 8);
			this.tbFieldName.Name = "tbFieldName";
			this.tbFieldName.Size = new System.Drawing.Size(384, 22);
			this.tbFieldName.TabIndex = 1;
			this.tbFieldName.Text = "";
			// 
			// gbAlternatives
			// 
			this.gbAlternatives.Controls.AddRange(new System.Windows.Forms.Control[] {
																						 this.tbXYPlotWid,
																						 this.tbTableWid,
																						 this.tbVariableTxtWid,
																						 this.tbSingleTxtWid,
																						 this.lblW4,
																						 this.lblW3,
																						 this.lblW2,
																						 this.lblW1,
																						 this.cbXYPlot,
																						 this.cbTable,
																						 this.cbVariableTxt,
																						 this.cbSingleTxt});
			this.gbAlternatives.Location = new System.Drawing.Point(16, 48);
			this.gbAlternatives.Name = "gbAlternatives";
			this.gbAlternatives.Size = new System.Drawing.Size(464, 152);
			this.gbAlternatives.TabIndex = 2;
			this.gbAlternatives.TabStop = false;
			this.gbAlternatives.Text = "Alternatives";
			// 
			// tbXYPlotWid
			// 
			this.tbXYPlotWid.Location = new System.Drawing.Point(320, 120);
			this.tbXYPlotWid.Name = "tbXYPlotWid";
			this.tbXYPlotWid.Size = new System.Drawing.Size(40, 22);
			this.tbXYPlotWid.TabIndex = 11;
			this.tbXYPlotWid.Text = "";
			// 
			// tbTableWid
			// 
			this.tbTableWid.Location = new System.Drawing.Point(320, 88);
			this.tbTableWid.Name = "tbTableWid";
			this.tbTableWid.Size = new System.Drawing.Size(40, 22);
			this.tbTableWid.TabIndex = 10;
			this.tbTableWid.Text = "";
			// 
			// tbVariableTxtWid
			// 
			this.tbVariableTxtWid.Location = new System.Drawing.Point(320, 56);
			this.tbVariableTxtWid.Name = "tbVariableTxtWid";
			this.tbVariableTxtWid.Size = new System.Drawing.Size(40, 22);
			this.tbVariableTxtWid.TabIndex = 9;
			this.tbVariableTxtWid.Text = "";
			// 
			// tbSingleTxtWid
			// 
			this.tbSingleTxtWid.Location = new System.Drawing.Point(320, 24);
			this.tbSingleTxtWid.Name = "tbSingleTxtWid";
			this.tbSingleTxtWid.Size = new System.Drawing.Size(40, 22);
			this.tbSingleTxtWid.TabIndex = 8;
			this.tbSingleTxtWid.Text = "";
			// 
			// lblW4
			// 
			this.lblW4.Location = new System.Drawing.Point(264, 120);
			this.lblW4.Name = "lblW4";
			this.lblW4.Size = new System.Drawing.Size(56, 16);
			this.lblW4.TabIndex = 7;
			this.lblW4.Text = "Width:";
			// 
			// lblW3
			// 
			this.lblW3.Location = new System.Drawing.Point(264, 96);
			this.lblW3.Name = "lblW3";
			this.lblW3.Size = new System.Drawing.Size(56, 16);
			this.lblW3.TabIndex = 6;
			this.lblW3.Text = "Width:";
			// 
			// lblW2
			// 
			this.lblW2.Location = new System.Drawing.Point(264, 64);
			this.lblW2.Name = "lblW2";
			this.lblW2.Size = new System.Drawing.Size(56, 16);
			this.lblW2.TabIndex = 5;
			this.lblW2.Text = "Width:";
			// 
			// lblW1
			// 
			this.lblW1.Location = new System.Drawing.Point(264, 32);
			this.lblW1.Name = "lblW1";
			this.lblW1.Size = new System.Drawing.Size(56, 16);
			this.lblW1.TabIndex = 4;
			this.lblW1.Text = "Width:";
			// 
			// cbXYPlot
			// 
			this.cbXYPlot.Location = new System.Drawing.Point(16, 120);
			this.cbXYPlot.Name = "cbXYPlot";
			this.cbXYPlot.Size = new System.Drawing.Size(144, 24);
			this.cbXYPlot.TabIndex = 3;
			this.cbXYPlot.Text = "X/Y Plot";
			// 
			// cbTable
			// 
			this.cbTable.Location = new System.Drawing.Point(16, 88);
			this.cbTable.Name = "cbTable";
			this.cbTable.Size = new System.Drawing.Size(144, 24);
			this.cbTable.TabIndex = 2;
			this.cbTable.Text = "Table";
			// 
			// cbVariableTxt
			// 
			this.cbVariableTxt.Location = new System.Drawing.Point(16, 56);
			this.cbVariableTxt.Name = "cbVariableTxt";
			this.cbVariableTxt.Size = new System.Drawing.Size(144, 24);
			this.cbVariableTxt.TabIndex = 1;
			this.cbVariableTxt.Text = "Variable Line Text";
			// 
			// cbSingleTxt
			// 
			this.cbSingleTxt.Location = new System.Drawing.Point(16, 24);
			this.cbSingleTxt.Name = "cbSingleTxt";
			this.cbSingleTxt.Size = new System.Drawing.Size(144, 24);
			this.cbSingleTxt.TabIndex = 0;
			this.cbSingleTxt.Text = "Single Line Text";
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(264, 216);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(88, 24);
			this.btnOK.TabIndex = 3;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(392, 216);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(88, 24);
			this.btnCancel.TabIndex = 4;
			this.btnCancel.Text = "Cancel";
			// 
			// FieldCombFrm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(504, 253);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCancel,
																		  this.btnOK,
																		  this.gbAlternatives,
																		  this.tbFieldName,
																		  this.lblFieldName});
			this.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "FieldCombFrm";
			this.Text = "Combination Field Definition";
			this.gbAlternatives.ResumeLayout(false);
			this.ResumeLayout(false);
		}
		#endregion
		private string CvtUserFldToFld(string fldname)
		{
			if (fldname.Length < 2)
				return fldname;
			// a digit cannot start an xml fieldname, prepend a "__" to it.
			string tmp0;
			if (char.IsDigit(fldname,0))
				tmp0 = "__" + fldname;
			else
				tmp0 = fldname;
			// an xml fieldname cannot have a space, change it to a "__"
			string tmpstr = tmp0.Replace(" ","__");
			int len = tmpstr.Length;
			int cnt = 0;
			// this is also our sequence that tells us the follow 3 digits is the ascii number (base 10)
			// of the character we replaced.
			string OKpunch = "-._"; 
			string outstr = "";
			int decval;
			while (cnt < len)
			{
				char tmpchr = tmpstr[cnt];
				if(!char.IsLetterOrDigit(tmpchr)&& (OKpunch.IndexOf(tmpchr) == -1) )
				{
					decval = tmpchr;
					outstr += OKpunch + decval.ToString("D3");
				}
				else
				{
					outstr += tmpchr.ToString();
				}
				cnt++;
			}
			return outstr;
		}
		private bool CheckValidWidth(string wid)
		{
			long val;
			try 
			{
				// Attempt to convert to long, if it fails, there are problems in the text
				val = System.Convert.ToInt64(wid);
			}
			catch 
			{
				MessageBox.Show("The field width is not a valid number. Please re-enter");
				return false;
			}
			return true;
		}
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			string savschemapiece;
			bool success=false;
			// check for field validity
			if (this.tbFieldName.Text == null)
			{
				MessageBox.Show("Cannot have empty field definition name.");
				return;
			}
			if (this.cbSingleTxt.Checked == true && this.tbSingleTxtWid.Text == null)
			{
				MessageBox.Show("Cannot have empty width for Single Line Text");
				return;
			}
			if (this.cbSingleTxt.Checked == true) 
			{
				success=CheckValidWidth(this.tbSingleTxtWid.Text);
				if (success == false) return;
			}
			if (this.cbVariableTxt.Checked == true && this.tbVariableTxtWid.Text == null)
			{
				MessageBox.Show("Cannot have empty width for Variable Length Text");
				return;
			}
			if (this.cbVariableTxt.Checked == true)
			{
				success=CheckValidWidth(this.tbSingleTxtWid.Text);
				if (success == false) return;
			}
			if (this.cbTable.Checked == true && this.tbTableWid.Text == null)
			{
				MessageBox.Show("Cannot have empty width for Table Text");
				return;
			}
			if (this.cbTable.Checked == true)
			{
				success=CheckValidWidth(this.tbTableWid.Text);
				if (success == false) return;
			}
			if (this.cbXYPlot.Checked == true && this.tbXYPlotWid.Text == null)
			{
				MessageBox.Show("Cannot have empty width for XYPlot Text");
				return;
			}
			if (this.cbXYPlot.Checked == true)
			{
				success=CheckValidWidth(this.tbXYPlotWid.Text);
				if (success == false) return;
			}
			// check that it is not duplicate name.
			ROField rof;
			for (int i=0; i< avail_list.Count; i++)
			{
				rof = (ROField) avail_list[i];
				if (rof.GetFieldname == this.tbFieldName.Text && rof.GetRecID != myrof.GetRecID)
				{
					MessageBox.Show("The field name is not unique. Please re-enter");
					return;
				}
			}
			for (int i=0; i< inuse_list.Count; i++)
			{
				rof = (ROField) inuse_list[i];
				if (rof.GetFieldname == this.tbFieldName.Text && rof.GetRecID != myrof.GetRecID)
				{
					MessageBox.Show("The field name is not unique. Please re-enter");
					return;
				}
			}
			// generate the new schema string based on text box input.
			string fname=CvtUserFldToFld(this.tbFieldName.Text);
			savschemapiece = myrof.MakeComboSchemaString(fname, this.cbSingleTxt.Checked, 
				this.tbSingleTxtWid.Text, this.cbVariableTxt.Checked, this.tbVariableTxtWid.Text, 
				this.cbTable.Checked, this.tbTableWid.Text, this.cbXYPlot.Checked, this.tbXYPlotWid.Text);
			myrof.SetFieldname(fname);
			// if this is 'new'. create a new element and do an insert to database
			if (exists == false) 
				success = myrodb.RODB_NewFieldRecord(myrof, myelem, savschemapiece, editlevel, "a");
			else     // modification of existing...
				success = myrodb.RODB_UpdateFieldRecord(myrof, myelem, savschemapiece, CvtUserFldToFld(origname), fname, editlevel, true);
			// update the local/internal copy of the schema.
			if (success != true)
			{
				return; // DO YET process an error.
			}
			else		 
			{
				// if this is a RRO schema, reload. We're not reloading the group schema
				// because it is rarely used and is only loaded as needed.
				if (editlevel == (uint) RecordType.Schema)
				{
					XmlSchema myschema;
					VlnXmlElement parent;
					parent = myelem;
					while (parent != null)
					{
						if (parent.HasAttribute("Schema") == true) break;
						if (parent.HasAttribute("FieldsInUse") == true) break;
						parent = (VlnXmlElement) parent.ParentNode;
					}
					// if a schema has been read in, delete it and reread it.
					if (parent.HasAttribute("Schema"))
					{
						// delete this attribute because we'll reread it in.
						parent.RemoveAttribute("Schema");
						myschema = myrodb.RODB_GetSchema(myelem);
					}
				}
			}
			
			
			this.Close();
		}
	}
}