#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using JuiceApps.BO;
using System.IO;

#endregion

namespace JuiceApps
{
    /// <summary>
    /// Summary description for WorkOrderView.
    /// </summary>
    public class WorkOrderView : System.Windows.Forms.Form
    {
        #region Instance Variables

        private Button btnLeft;
        private Button btnRight;
        private Panel panel1;
        private Label lblWON;
        private Label label2;
        private Panel pnlMain;
        private TextBox txtAmount;
        private Label label15;
        private TextBox txtMaterials;
        private Label label14;
        private TextBox txtComments;
        private Label label13;
        private TextBox txtTimeSpent;
        private Label label12;
        private Label label11;
        private Label label18;
        private Label label10;
        private TextBox txtCreated;
        private Label label9;
        private TextBox txtAssignedTo;
        private Label label8;
        private TextBox txtWOType;
        private Label label7;
        private TextBox txtMeterSN;
        private Label label5;
        private Label label6;
        private TextBox txtTransformer;
        private TextBox txtSubstation;
        private TextBox txtLocID;
        private Label label4;
        private TextBox txtName;
        private Label label3;
        private VScrollBar vScrollBar1;
        private Label lblWOCount;

        private ComboBox cbStatus;
        private MainMenu mainMenu1;
        private MenuItem miEditDone;
        private MenuItem miSave;
        private ComboBox cbSubstatus;


        private ComboBox comboBox1;
        private Label label1;
        private TextBox txtPeak;
        private TextBox txtkVA;
        private TextBox txtkWh;
        private Label label17;
        private Label label19;
        private Label label20;
        private Label label16;
        private TextBox txtAverageReading;
        private Label label21;
        private TextBox txtAddress;
        private Label label22;
        private TextBox txtAccNum;
        private Label label23;

        private int currentWorkOrder = 0;
        private ArrayList doNotEdit;
        private bool changed = false;
        private bool editing = false;
        private MenuItem miBack;
        private MenuItem menuItem1;
        private Label label24;
        private TextBox txtRoute;
        private TextBox txtModule;
        private Label label25;
        private bool newMeter = false;

        #endregion

        // Called when a meter serial number is unrecognized (i.e. not in the database)
        public WorkOrderView(string sn)
        {
            newMeter = true;
            InitializeComponent();
        }

        // Called when a meter/work order is recognized (i.e. in the database)
        public WorkOrderView(int current)
        {
            currentWorkOrder = current;
            InitializeComponent();
            initData(currentWorkOrder);

            this.btnRight.Enabled = (currentWorkOrder < Helper.WorkOrders.Count() - 1) ? true : false;
            this.btnLeft.Enabled = (currentWorkOrder > 0) ? true : false;
            lblWOCount.Text = String.Format("{0}/{1}", (currentWorkOrder + 1), Helper.WorkOrders.Count());
        }

        #region Auto-Generated
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.label2 = new System.Windows.Forms.Label();
            this.lblWOCount = new System.Windows.Forms.Label();
            this.lblWON = new System.Windows.Forms.Label();
            this.btnLeft = new System.Windows.Forms.Button();
            this.btnRight = new System.Windows.Forms.Button();
            this.pnlMain = new System.Windows.Forms.Panel();
            this.label24 = new System.Windows.Forms.Label();
            this.txtRoute = new System.Windows.Forms.TextBox();
            this.txtModule = new System.Windows.Forms.TextBox();
            this.label25 = new System.Windows.Forms.Label();
            this.txtAccNum = new System.Windows.Forms.TextBox();
            this.label23 = new System.Windows.Forms.Label();
            this.label22 = new System.Windows.Forms.Label();
            this.txtAverageReading = new System.Windows.Forms.TextBox();
            this.label21 = new System.Windows.Forms.Label();
            this.txtPeak = new System.Windows.Forms.TextBox();
            this.txtkVA = new System.Windows.Forms.TextBox();
            this.txtkWh = new System.Windows.Forms.TextBox();
            this.label17 = new System.Windows.Forms.Label();
            this.label19 = new System.Windows.Forms.Label();
            this.label20 = new System.Windows.Forms.Label();
            this.label16 = new System.Windows.Forms.Label();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.cbSubstatus = new System.Windows.Forms.ComboBox();
            this.cbStatus = new System.Windows.Forms.ComboBox();
            this.txtAmount = new System.Windows.Forms.TextBox();
            this.label15 = new System.Windows.Forms.Label();
            this.txtMaterials = new System.Windows.Forms.TextBox();
            this.label14 = new System.Windows.Forms.Label();
            this.txtComments = new System.Windows.Forms.TextBox();
            this.label13 = new System.Windows.Forms.Label();
            this.txtTimeSpent = new System.Windows.Forms.TextBox();
            this.label12 = new System.Windows.Forms.Label();
            this.label11 = new System.Windows.Forms.Label();
            this.label18 = new System.Windows.Forms.Label();
            this.label10 = new System.Windows.Forms.Label();
            this.txtCreated = new System.Windows.Forms.TextBox();
            this.label9 = new System.Windows.Forms.Label();
            this.txtAssignedTo = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.txtWOType = new System.Windows.Forms.TextBox();
            this.label7 = new System.Windows.Forms.Label();
            this.txtMeterSN = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.txtTransformer = new System.Windows.Forms.TextBox();
            this.txtSubstation = new System.Windows.Forms.TextBox();
            this.txtLocID = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.txtName = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.txtAddress = new System.Windows.Forms.TextBox();
            this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.miEditDone = new System.Windows.Forms.MenuItem();
            this.miSave = new System.Windows.Forms.MenuItem();
            this.miBack = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.SystemColors.ScrollBar;
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.lblWOCount);
            this.panel1.Controls.Add(this.lblWON);
            this.panel1.Controls.Add(this.btnLeft);
            this.panel1.Controls.Add(this.btnRight);
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Size = new System.Drawing.Size(240, 30);
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Tahoma", 14F, System.Drawing.FontStyle.Bold);
            this.label2.Location = new System.Drawing.Point(102, 0);
            this.label2.Size = new System.Drawing.Size(17, 30);
            this.label2.Text = "|";
            this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // lblWOCount
            // 
            this.lblWOCount.Font = new System.Drawing.Font("Tahoma", 14F, System.Drawing.FontStyle.Bold);
            this.lblWOCount.Location = new System.Drawing.Point(102, 2);
            this.lblWOCount.Size = new System.Drawing.Size(107, 30);
            this.lblWOCount.Text = "0/0";
            this.lblWOCount.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // lblWON
            // 
            this.lblWON.Font = new System.Drawing.Font("Tahoma", 14F, System.Drawing.FontStyle.Bold);
            this.lblWON.Location = new System.Drawing.Point(32, 2);
            this.lblWON.Size = new System.Drawing.Size(64, 30);
            this.lblWON.Text = "000";
            // 
            // btnLeft
            // 
            this.btnLeft.Enabled = false;
            this.btnLeft.Font = new System.Drawing.Font("Tahoma", 18F, System.Drawing.FontStyle.Bold);
            this.btnLeft.Location = new System.Drawing.Point(0, 0);
            this.btnLeft.Size = new System.Drawing.Size(30, 30);
            this.btnLeft.Text = "<";
            this.btnLeft.Click += new System.EventHandler(this.btnLeft_Click);
            // 
            // btnRight
            // 
            this.btnRight.Enabled = false;
            this.btnRight.Font = new System.Drawing.Font("Tahoma", 18F, System.Drawing.FontStyle.Bold);
            this.btnRight.Location = new System.Drawing.Point(210, 0);
            this.btnRight.Size = new System.Drawing.Size(30, 30);
            this.btnRight.Text = ">";
            this.btnRight.Click += new System.EventHandler(this.btnRight_Click);
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.label24);
            this.pnlMain.Controls.Add(this.txtRoute);
            this.pnlMain.Controls.Add(this.txtModule);
            this.pnlMain.Controls.Add(this.label25);
            this.pnlMain.Controls.Add(this.txtAccNum);
            this.pnlMain.Controls.Add(this.label23);
            this.pnlMain.Controls.Add(this.label22);
            this.pnlMain.Controls.Add(this.txtAverageReading);
            this.pnlMain.Controls.Add(this.label21);
            this.pnlMain.Controls.Add(this.txtPeak);
            this.pnlMain.Controls.Add(this.txtkVA);
            this.pnlMain.Controls.Add(this.txtkWh);
            this.pnlMain.Controls.Add(this.label17);
            this.pnlMain.Controls.Add(this.label19);
            this.pnlMain.Controls.Add(this.label20);
            this.pnlMain.Controls.Add(this.label16);
            this.pnlMain.Controls.Add(this.comboBox1);
            this.pnlMain.Controls.Add(this.label1);
            this.pnlMain.Controls.Add(this.cbSubstatus);
            this.pnlMain.Controls.Add(this.cbStatus);
            this.pnlMain.Controls.Add(this.txtAmount);
            this.pnlMain.Controls.Add(this.label15);
            this.pnlMain.Controls.Add(this.txtMaterials);
            this.pnlMain.Controls.Add(this.label14);
            this.pnlMain.Controls.Add(this.txtComments);
            this.pnlMain.Controls.Add(this.label13);
            this.pnlMain.Controls.Add(this.txtTimeSpent);
            this.pnlMain.Controls.Add(this.label12);
            this.pnlMain.Controls.Add(this.label11);
            this.pnlMain.Controls.Add(this.label18);
            this.pnlMain.Controls.Add(this.label10);
            this.pnlMain.Controls.Add(this.txtCreated);
            this.pnlMain.Controls.Add(this.label9);
            this.pnlMain.Controls.Add(this.txtAssignedTo);
            this.pnlMain.Controls.Add(this.label8);
            this.pnlMain.Controls.Add(this.txtWOType);
            this.pnlMain.Controls.Add(this.label7);
            this.pnlMain.Controls.Add(this.txtMeterSN);
            this.pnlMain.Controls.Add(this.label5);
            this.pnlMain.Controls.Add(this.label6);
            this.pnlMain.Controls.Add(this.txtTransformer);
            this.pnlMain.Controls.Add(this.txtSubstation);
            this.pnlMain.Controls.Add(this.txtLocID);
            this.pnlMain.Controls.Add(this.label4);
            this.pnlMain.Controls.Add(this.txtName);
            this.pnlMain.Controls.Add(this.label3);
            this.pnlMain.Controls.Add(this.txtAddress);
            this.pnlMain.Location = new System.Drawing.Point(0, 30);
            this.pnlMain.Size = new System.Drawing.Size(218, 713);
            // 
            // label24
            // 
            this.label24.Enabled = false;
            this.label24.Location = new System.Drawing.Point(3, 154);
            this.label24.Size = new System.Drawing.Size(81, 20);
            this.label24.Text = "Route:";
            this.label24.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtRoute
            // 
            this.txtRoute.Enabled = false;
            this.txtRoute.Location = new System.Drawing.Point(90, 153);
            this.txtRoute.Size = new System.Drawing.Size(128, 21);
            // 
            // txtModule
            // 
            this.txtModule.Enabled = false;
            this.txtModule.Location = new System.Drawing.Point(90, 126);
            this.txtModule.Size = new System.Drawing.Size(128, 21);
            // 
            // label25
            // 
            this.label25.Enabled = false;
            this.label25.Location = new System.Drawing.Point(3, 127);
            this.label25.Size = new System.Drawing.Size(81, 20);
            this.label25.Text = "Module:";
            this.label25.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtAccNum
            // 
            this.txtAccNum.Enabled = false;
            this.txtAccNum.Location = new System.Drawing.Point(90, 3);
            this.txtAccNum.Size = new System.Drawing.Size(128, 21);
            // 
            // label23
            // 
            this.label23.Enabled = false;
            this.label23.Location = new System.Drawing.Point(3, 4);
            this.label23.Size = new System.Drawing.Size(81, 20);
            this.label23.Text = "Acct Num:";
            this.label23.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label22
            // 
            this.label22.Enabled = false;
            this.label22.Location = new System.Drawing.Point(3, 58);
            this.label22.Size = new System.Drawing.Size(81, 20);
            this.label22.Text = "Address:";
            this.label22.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtAverageReading
            // 
            this.txtAverageReading.Enabled = false;
            this.txtAverageReading.Location = new System.Drawing.Point(90, 603);
            this.txtAverageReading.Size = new System.Drawing.Size(128, 21);
            // 
            // label21
            // 
            this.label21.Enabled = false;
            this.label21.Location = new System.Drawing.Point(3, 604);
            this.label21.Size = new System.Drawing.Size(81, 20);
            this.label21.Text = "3 Month Avg:";
            this.label21.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtPeak
            // 
            this.txtPeak.Enabled = false;
            this.txtPeak.Location = new System.Drawing.Point(90, 684);
            this.txtPeak.Size = new System.Drawing.Size(128, 21);
            // 
            // txtkVA
            // 
            this.txtkVA.Enabled = false;
            this.txtkVA.Location = new System.Drawing.Point(90, 657);
            this.txtkVA.Size = new System.Drawing.Size(128, 21);
            // 
            // txtkWh
            // 
            this.txtkWh.Enabled = false;
            this.txtkWh.Location = new System.Drawing.Point(90, 630);
            this.txtkWh.Size = new System.Drawing.Size(128, 21);
            // 
            // label17
            // 
            this.label17.Enabled = false;
            this.label17.Location = new System.Drawing.Point(3, 695);
            this.label17.Size = new System.Drawing.Size(81, 20);
            this.label17.Text = "Peak:";
            this.label17.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label19
            // 
            this.label19.Enabled = false;
            this.label19.Location = new System.Drawing.Point(3, 658);
            this.label19.Size = new System.Drawing.Size(81, 20);
            this.label19.Text = "kVA:";
            this.label19.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label20
            // 
            this.label20.Enabled = false;
            this.label20.Location = new System.Drawing.Point(3, 631);
            this.label20.Size = new System.Drawing.Size(81, 20);
            this.label20.Text = "kWh:";
            this.label20.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label16
            // 
            this.label16.Font = new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Bold);
            this.label16.Location = new System.Drawing.Point(0, 578);
            this.label16.Size = new System.Drawing.Size(218, 20);
            this.label16.Text = "Reading Information";
            this.label16.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // comboBox1
            // 
            this.comboBox1.Enabled = false;
            this.comboBox1.Items.Add("Location not found");
            this.comboBox1.Items.Add("No access");
            this.comboBox1.Items.Add("Abandoned");
            this.comboBox1.Items.Add("Meter changed");
            this.comboBox1.Items.Add("Meter not found");
            this.comboBox1.Items.Add("Module unlocked");
            this.comboBox1.Items.Add("Possible fraud");
            this.comboBox1.Location = new System.Drawing.Point(90, 422);
            this.comboBox1.Size = new System.Drawing.Size(128, 22);
            // 
            // label1
            // 
            this.label1.Enabled = false;
            this.label1.Location = new System.Drawing.Point(3, 424);
            this.label1.Size = new System.Drawing.Size(81, 20);
            this.label1.Text = "Detail:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // cbSubstatus
            // 
            this.cbSubstatus.Enabled = false;
            this.cbSubstatus.Items.Add("Failed");
            this.cbSubstatus.Items.Add("Extended");
            this.cbSubstatus.Items.Add("Invalid");
            this.cbSubstatus.Items.Add("Effective");
            this.cbSubstatus.Location = new System.Drawing.Point(90, 395);
            this.cbSubstatus.Size = new System.Drawing.Size(128, 22);
            // 
            // cbStatus
            // 
            this.cbStatus.Enabled = false;
            this.cbStatus.Items.Add("Open");
            this.cbStatus.Items.Add("In progress");
            this.cbStatus.Items.Add("Failed");
            this.cbStatus.Items.Add("Closed");
            this.cbStatus.Location = new System.Drawing.Point(90, 368);
            this.cbStatus.Size = new System.Drawing.Size(128, 22);
            // 
            // txtAmount
            // 
            this.txtAmount.Enabled = false;
            this.txtAmount.Location = new System.Drawing.Point(90, 551);
            this.txtAmount.Size = new System.Drawing.Size(128, 21);
            // 
            // label15
            // 
            this.label15.Enabled = false;
            this.label15.Location = new System.Drawing.Point(3, 552);
            this.label15.Size = new System.Drawing.Size(81, 20);
            this.label15.Text = "$ Amount:";
            this.label15.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtMaterials
            // 
            this.txtMaterials.Enabled = false;
            this.txtMaterials.Location = new System.Drawing.Point(90, 524);
            this.txtMaterials.Size = new System.Drawing.Size(128, 21);
            // 
            // label14
            // 
            this.label14.Enabled = false;
            this.label14.Location = new System.Drawing.Point(3, 525);
            this.label14.Size = new System.Drawing.Size(81, 20);
            this.label14.Text = "Materials:";
            this.label14.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtComments
            // 
            this.txtComments.Enabled = false;
            this.txtComments.Location = new System.Drawing.Point(90, 477);
            this.txtComments.Multiline = true;
            this.txtComments.Size = new System.Drawing.Size(128, 41);
            // 
            // label13
            // 
            this.label13.Enabled = false;
            this.label13.Location = new System.Drawing.Point(3, 478);
            this.label13.Size = new System.Drawing.Size(81, 20);
            this.label13.Text = "Comments:";
            this.label13.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtTimeSpent
            // 
            this.txtTimeSpent.Enabled = false;
            this.txtTimeSpent.Location = new System.Drawing.Point(90, 450);
            this.txtTimeSpent.Size = new System.Drawing.Size(128, 21);
            // 
            // label12
            // 
            this.label12.Enabled = false;
            this.label12.Location = new System.Drawing.Point(3, 451);
            this.label12.Size = new System.Drawing.Size(81, 20);
            this.label12.Text = "Time Spent:";
            this.label12.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label11
            // 
            this.label11.Enabled = false;
            this.label11.Location = new System.Drawing.Point(3, 397);
            this.label11.Size = new System.Drawing.Size(81, 20);
            this.label11.Text = "Substatus:";
            this.label11.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label18
            // 
            this.label18.Enabled = false;
            this.label18.Location = new System.Drawing.Point(3, 370);
            this.label18.Size = new System.Drawing.Size(81, 20);
            this.label18.Text = "Status:";
            this.label18.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label10
            // 
            this.label10.Enabled = false;
            this.label10.Location = new System.Drawing.Point(3, 343);
            this.label10.Size = new System.Drawing.Size(81, 20);
            this.label10.Text = "Created:";
            this.label10.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtCreated
            // 
            this.txtCreated.Enabled = false;
            this.txtCreated.Location = new System.Drawing.Point(90, 342);
            this.txtCreated.Size = new System.Drawing.Size(128, 21);
            // 
            // label9
            // 
            this.label9.Enabled = false;
            this.label9.Location = new System.Drawing.Point(3, 316);
            this.label9.Size = new System.Drawing.Size(81, 20);
            this.label9.Text = "Assigned To:";
            this.label9.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtAssignedTo
            // 
            this.txtAssignedTo.Enabled = false;
            this.txtAssignedTo.Location = new System.Drawing.Point(90, 315);
            this.txtAssignedTo.Size = new System.Drawing.Size(128, 21);
            // 
            // label8
            // 
            this.label8.Enabled = false;
            this.label8.Location = new System.Drawing.Point(3, 289);
            this.label8.Size = new System.Drawing.Size(81, 20);
            this.label8.Text = "WO Type:";
            this.label8.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtWOType
            // 
            this.txtWOType.Enabled = false;
            this.txtWOType.Location = new System.Drawing.Point(90, 288);
            this.txtWOType.Size = new System.Drawing.Size(128, 21);
            // 
            // label7
            // 
            this.label7.Enabled = false;
            this.label7.Location = new System.Drawing.Point(3, 262);
            this.label7.Size = new System.Drawing.Size(81, 20);
            this.label7.Text = "Meter S/N:";
            this.label7.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtMeterSN
            // 
            this.txtMeterSN.Enabled = false;
            this.txtMeterSN.Location = new System.Drawing.Point(90, 261);
            this.txtMeterSN.Size = new System.Drawing.Size(128, 21);
            // 
            // label5
            // 
            this.label5.Enabled = false;
            this.label5.Location = new System.Drawing.Point(3, 235);
            this.label5.Size = new System.Drawing.Size(81, 20);
            this.label5.Text = "Transformer:";
            this.label5.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // label6
            // 
            this.label6.Enabled = false;
            this.label6.Location = new System.Drawing.Point(3, 208);
            this.label6.Size = new System.Drawing.Size(81, 20);
            this.label6.Text = "Substation:";
            this.label6.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtTransformer
            // 
            this.txtTransformer.Enabled = false;
            this.txtTransformer.Location = new System.Drawing.Point(90, 234);
            this.txtTransformer.Size = new System.Drawing.Size(128, 21);
            // 
            // txtSubstation
            // 
            this.txtSubstation.Enabled = false;
            this.txtSubstation.Location = new System.Drawing.Point(90, 207);
            this.txtSubstation.Size = new System.Drawing.Size(128, 21);
            // 
            // txtLocID
            // 
            this.txtLocID.Enabled = false;
            this.txtLocID.Location = new System.Drawing.Point(90, 180);
            this.txtLocID.Size = new System.Drawing.Size(128, 21);
            // 
            // label4
            // 
            this.label4.Enabled = false;
            this.label4.Location = new System.Drawing.Point(3, 181);
            this.label4.Size = new System.Drawing.Size(81, 20);
            this.label4.Text = "Location ID:";
            this.label4.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtName
            // 
            this.txtName.Enabled = false;
            this.txtName.Location = new System.Drawing.Point(90, 30);
            this.txtName.Size = new System.Drawing.Size(128, 21);
            // 
            // label3
            // 
            this.label3.Enabled = false;
            this.label3.Location = new System.Drawing.Point(3, 31);
            this.label3.Size = new System.Drawing.Size(81, 20);
            this.label3.Text = "Name:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.TopRight;
            // 
            // txtAddress
            // 
            this.txtAddress.Enabled = false;
            this.txtAddress.Location = new System.Drawing.Point(90, 57);
            this.txtAddress.Multiline = true;
            this.txtAddress.Size = new System.Drawing.Size(128, 63);
            // 
            // vScrollBar1
            // 
            this.vScrollBar1.LargeChange = 30;
            this.vScrollBar1.Location = new System.Drawing.Point(221, 29);
            this.vScrollBar1.Maximum = 475;
            this.vScrollBar1.Size = new System.Drawing.Size(19, 265);
            this.vScrollBar1.SmallChange = 15;
            this.vScrollBar1.ValueChanged += new System.EventHandler(this.vScrollBar1_ValueChanged);
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.miEditDone);
            this.mainMenu1.MenuItems.Add(this.miSave);
            this.mainMenu1.MenuItems.Add(this.miBack);
            this.mainMenu1.MenuItems.Add(this.menuItem1);
            // 
            // miEditDone
            // 
            this.miEditDone.Text = "Edit";
            this.miEditDone.Click += new System.EventHandler(this.miEditDone_Click);
            // 
            // miSave
            // 
            this.miSave.Enabled = false;
            this.miSave.Text = "Save";
            this.miSave.Click += new System.EventHandler(this.miSave_Click);
            // 
            // miBack
            // 
            this.miBack.Text = "Back";
            this.miBack.Click += new System.EventHandler(this.miBack_Click);
            // 
            // menuItem1
            // 
            this.menuItem1.Text = "Exit";
            this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            // 
            // WorkOrderView
            // 
            this.ClientSize = new System.Drawing.Size(240, 294);
            this.ControlBox = false;
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.vScrollBar1);
            this.Controls.Add(this.pnlMain);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Location = new System.Drawing.Point(0, 0);
            this.Menu = this.mainMenu1;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.WorkOrderView_Load);
            this.Resize += new System.EventHandler(this.WorkOrderView_Resize);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.WorkOrderView_KeyDown);

        }
        #endregion

        private void WorkOrderView_Load(object sender, EventArgs e)
        {
            // Non-editable fields
            doNotEdit = new ArrayList();
            doNotEdit.Add(this.txtLocID);
            doNotEdit.Add(this.txtSubstation);
            doNotEdit.Add(this.txtTransformer);
            doNotEdit.Add(this.txtMeterSN);
            doNotEdit.Add(this.txtCreated);
            doNotEdit.Add(this.txtAverageReading);
            doNotEdit.Add(this.txtAccNum);
            doNotEdit.Add(this.txtModule);
            doNotEdit.Add(this.txtRoute);

            // Initialize changes to dataChanged event handler
            foreach (Control c in this.pnlMain.Controls)
                if (c is TextBox)
                    ((TextBox)c).TextChanged += new EventHandler(dataChanged);
                else if (c is ComboBox)
                    ((ComboBox)c).SelectedIndexChanged += new EventHandler(dataChanged);

            if (newMeter)
                MessageBox.Show("Warning! Meter not found in database! Enter data and click save to add.", "Meter Not Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
        }

        private void dataChanged(Object sender, EventArgs e)
        {
            if (editing)
            {
                changed = true;
                miSave.Enabled = true;
            }
        }

        private void vScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            this.pnlMain.Top = -this.vScrollBar1.Value + 30;
        }

        public void initData(int index)
        {
            if (Helper.FileExists())
            {
                WorkOrder w = Helper.WorkOrders[index];

                this.Text = w.Portal;
                txtAccNum.Text = w.AccountNum.ToString();
                lblWON.Text = w.WONum.ToString();
                txtAmount.Text = w.Amount.ToString();
                txtMaterials.Text = w.MatUsed;
                txtComments.Text = w.Comments;
                txtTimeSpent.Text = w.TimeSpent.ToString();
                cbSubstatus.SelectedItem = w.Substatus;
                switch (w.Substatus.ToLower())
                {
                    case "failed":
                        cbSubstatus.SelectedIndex = 0;
                        break;
                    case "extended":
                        cbSubstatus.SelectedIndex = 1;
                        break;
                    case "invalid":
                        cbSubstatus.SelectedIndex = 2;
                        break;
                    case "effective":
                        cbSubstatus.SelectedIndex = 3;
                        break;
                    default:
                        cbSubstatus.SelectedIndex = -1;
                        break;
                }
                switch (w.Status.ToLower())
                {
                    case "open":
                        cbStatus.SelectedIndex = 0;
                        break;
                    case "in progress":
                        cbStatus.SelectedIndex = 1;
                        break;
                    case "failed":
                        cbStatus.SelectedIndex = 2;
                        break;
                    case "closed":
                        cbStatus.SelectedIndex = 3;
                        break;
                    default:
                        cbStatus.SelectedIndex = -1;
                        break;
                }
                txtCreated.Text = w.Created.ToString();
                txtAssignedTo.Text = w.AssignedTo;
                txtWOType.Text = w.WOType;
                txtMeterSN.Text = w.MeterSN.ToString();
                txtTransformer.Text = w.Transformer.ToString();
                txtSubstation.Text = w.Substation;
                txtLocID.Text = w.LocID.ToString();
                txtName.Text = w.Name;
                txtAverageReading.Text = w.AverageReading.ToString();
                txtkWh.Text = w.kWh.ToString();
                txtkVA.Text = w.kVA.ToString();
                txtPeak.Text = w.Peak.ToString();
                txtAddress.Text = (w.Address1 != String.Empty) ? String.Format("{0} {1}\r\n{2}, {3} {4}", w.Address1, w.Address2, w.City, w.State, w.Zip) : "";
                txtModule.Text = w.Module;
                txtRoute.Text = w.Route;
            }
            else
                MessageBox.Show("File not found!");
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            if (!editing)
                if (currentWorkOrder > 0)
                {
                    initData(--currentWorkOrder);
                    btnRight.Enabled = true;

                    if ((currentWorkOrder - 1) < 0)
                        btnLeft.Enabled = false;

                    lblWOCount.Text = String.Format("{0}/{1}", (currentWorkOrder + 1), Helper.WorkOrders.Count());
                }

            this.Focus();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            if (!editing)
                if ((currentWorkOrder + 1) < Helper.WorkOrders.Count())
                {
                    initData(++currentWorkOrder);
                    btnLeft.Enabled = true;

                    if ((currentWorkOrder + 1) == Helper.WorkOrders.Count())
                        btnRight.Enabled = false;

                    lblWOCount.Text = String.Format("{0}/{1}", (currentWorkOrder + 1), Helper.WorkOrders.Count());
                }

            this.Focus();
        }

        private void WorkOrderView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Up)
            {
                if ((this.vScrollBar1.Value - this.vScrollBar1.SmallChange) <= this.vScrollBar1.Minimum)
                    this.vScrollBar1.Value = this.vScrollBar1.Minimum;
                else
                    this.vScrollBar1.Value -= this.vScrollBar1.SmallChange;
                //this.vScrollBar1_ValueChanged(sender, e);
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Down)
            {
                if ((this.vScrollBar1.Value + this.vScrollBar1.SmallChange) >= this.vScrollBar1.Maximum)
                    this.vScrollBar1.Value = this.vScrollBar1.Maximum;
                else
                    this.vScrollBar1.Value += this.vScrollBar1.SmallChange;
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Left)
            {
                this.btnLeft_Click(sender, e);
            }
            else if (e.KeyCode == System.Windows.Forms.Keys.Right)
            {
                this.btnRight_Click(sender, e);
            }
            else if (e.KeyCode == (Keys.RButton | Keys.Space))  // Page up button
            {
                int factor = (Helper.WorkOrders.Count() / 10);
                if (factor > 1)
                {
                    if ((currentWorkOrder + factor) < Helper.WorkOrders.Count())
                    {
                        currentWorkOrder += factor;
                        initData(currentWorkOrder);
                    }
                    else
                    {
                        currentWorkOrder = Helper.WorkOrders.Count() - 1;
                        initData(currentWorkOrder);
                        btnLeft.Enabled = true;
                        btnRight.Enabled = false;
                    }

                    lblWOCount.Text = String.Format("{0}/{1}", (currentWorkOrder + 1), Helper.WorkOrders.Count());
                }
            }
            else if (e.KeyCode == (Keys.LButton | Keys.Space))  // Page down button
            {
                int factor = (Helper.WorkOrders.Count() / 10);
                if (factor > 1)
                {
                    if ((currentWorkOrder - factor) > 0)
                    {
                        currentWorkOrder -= factor;
                        initData(currentWorkOrder);
                    }
                    else
                    {
                        currentWorkOrder = 0;
                        initData(currentWorkOrder);
                        btnRight.Enabled = true;
                        btnLeft.Enabled = false;
                    }

                    lblWOCount.Text = String.Format("{0}/{1}", (currentWorkOrder + 1), Helper.WorkOrders.Count());
                }
            }
        }

        /// <summary>
        /// Edit menu button clicked
        /// </summary>
        private void miEditDone_Click(object sender, EventArgs e)
        {
            MenuItem button = (MenuItem)sender;
            if (button.Text == "Edit")
            {
                editing = true;
                if (newMeter)
                    enableAllControls();
                else
                    enableRelevantControls();
                button.Text = "Done";
            }
            else if (button.Text == "Done")
            {
                if (changed)
                {
                    DialogResult result = MessageBox.Show("You have unsaved changes.  Save them now?", "Unsaved Changes!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
                    if (result == DialogResult.Yes)
                        save();
                    else if (result == DialogResult.No)
                    {
                        initData(currentWorkOrder);
                        changed = false;
                        miSave.Enabled = false;
                    }
                }
                editing = false;
                disableRelevantControls();
                button.Text = "Edit";
                this.Focus();
            }
        }

        /// <summary>
        /// Save menu item click
        /// </summary>
        private void miSave_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Really save changes?", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (result == DialogResult.Yes)
                save();
        }

        /// <summary>
        /// Back menu item click
        /// </summary>
        private void miBack_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        /// <summary>
        /// Enables editable* controls.
        /// *Controls not in doNotEdit.
        /// </summary>
        private void enableRelevantControls()
        {
            bool editable = true;
            foreach (Control c in this.pnlMain.Controls)
            {
                if (c is TextBox)
                    if (((TextBox)c).Equals(this.txtName))
                        Console.Write("here");
                for (int i = 0; i < doNotEdit.Count; i++)
                    if (c.Equals(doNotEdit[i]))
                        editable = false;

                if (editable)
                    c.Enabled = true;

                editable = true;
            }
        }

        private void enableAllControls()
        {
            foreach (Control c in this.pnlMain.Controls)
                if ((c is TextBox) || (c is ComboBox))
                    c.Enabled = true;
        }

        private void disableRelevantControls()
        {
            foreach (Control c in this.pnlMain.Controls)
                if ((c is TextBox) || (c is ComboBox))
                    c.Enabled = false;
        }

        private void save()
        {
            if (newMeter)
            {
                WorkOrder wo = new WorkOrder();
                wo.WONum = Helper.WorkOrders.Count();
                wo.Portal = String.Empty;
                wo.AccountNum = txtAccNum.Text;
                wo.Name = txtName.Text;
                wo.LocID = txtLocID.Text;
                wo.Substation = txtSubstation.Text;
                wo.Transformer = txtTransformer.Text;

                // ========== Begin address parsing
                string[] addr = txtAddress.Text.Split("\r\n".ToCharArray());
                string[] addr0_1 = addr[0].Split(' ');
                string[] addr2_3_4 = addr[1].Split(' ');
                wo.Address1 = addr0_1[0];
                wo.Address2 = addr0_1[1];
                wo.City = addr2_3_4[0];
                wo.State = addr2_3_4[1];
                wo.Zip = int.Parse(addr2_3_4[2]);
                // ========== End address parsing

                wo.Module = txtModule.Text;
                wo.Route = txtRoute.Text;
                wo.MeterSN = txtMeterSN.Text;
                wo.WOType = txtWOType.Text;
                wo.AssignedTo = txtAssignedTo.Text;
                wo.AverageReading = 0.0;
                wo.Created = (txtCreated.Text != String.Empty) ? new DateTime(int.Parse(txtCreated.Text.Substring(0, 4)),
                                                                              int.Parse(txtCreated.Text.Substring(5, 2)),
                                                                              int.Parse(txtCreated.Text.Substring(8, 2))) : new DateTime();
                Helper.WorkOrders.Insert(Helper.WorkOrders.Count(), wo);
            }

            int c = Helper.WorkOrders.Count() - 1;
            Helper.WorkOrders[c].Amount = double.Parse(txtAmount.Text);
            Helper.WorkOrders[c].MatUsed = txtMaterials.Text;
            Helper.WorkOrders[c].Comments = txtComments.Text;
            Helper.WorkOrders[c].TimeSpent = double.Parse(txtTimeSpent.Text);
            Helper.WorkOrders[c].Substatus = cbSubstatus.SelectedValue.ToString();
            Helper.WorkOrders[c].Status = cbStatus.SelectedIndex.ToString();
            Helper.WorkOrders[c].Name = txtName.Text;
            Helper.WorkOrders[c].kWh = (txtkWh.Text != String.Empty) ? int.Parse(txtkWh.Text) : 0;
            Helper.WorkOrders[c].kVA = (txtkVA.Text != String.Empty) ? int.Parse(txtkVA.Text) : 0;
            Helper.WorkOrders[c].Peak = (txtPeak.Text != String.Empty) ? int.Parse(txtPeak.Text) : 0;

            changed = false;
            miSave.Enabled = false;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
            Program.Exit();
        }

        private void WorkOrderView_Resize(object sender, EventArgs e)
        {
            FormWindowState m = this.WindowState;
        }
    }
}
