/// <summary>
 /// checks each policy parameter individually 
 /// </summary>
 /// <param name="Policy"></param>
 public PolicyValidation(Policy Policy)
 {
     this.policy = Policy;
     ValidateName();
     if (valid == true)
         ValidateAge();
     if (valid == true)
         ValidateCar();
     if (valid == true)
         ValidatePolicy();
 }
        // validates the policies paramter. If correct updates the policies details
        public string ModifyPolicy(Policy policy, int id)
        {
            PolicyValidation validate = new PolicyValidation(policy);
            string message;

            if (validate.Valid == true)
            {
                message = writeModifiedPolicy(policy, id);
            }
            else
            {
                message = validate.Message;
            }
            return message;
        }
        string user = "******"; // hard coded for now, need to merge with staff package.

        #endregion Fields

        #region Methods

        // validates the policies paramter. If correct writes a new policy to the database.
        public string CreatePolicy(Policy policy)
        {
            PolicyValidation validate = new PolicyValidation(policy);
            string message;

            if (validate.Valid == true)
            {
                message = WriteNewPolicy(policy);
            }
            else
            {
                message = validate.Message;
            }
            return message;
        }
        /// <summary>
        /// writes the new policy to the database if it passes the validation tests
        /// </summary>
        /// <param name="policy"></param>
        /// <returns></returns>
        private string WriteNewPolicy(Policy policy)
        {
            int typeId;
            int vehicleId;
            DateTime currentDate = new DateTime();
            DateTime modifiedDate = new DateTime();
            string message = "";
            currentDate = DateTime.Now;
            modifiedDate = DateTime.Now;

            typeId = getPolicyType(policy.PolicyType);
            vehicleId = getVehicleType(policy.TypeOfVechile);
            //Connect to DB
            OleDbConnection con = new OleDbConnection(connect);
            con.Open();
            //SQL command to insert a new passenger
            string sql = "INSERT INTO Policy (PolicyName, PricePerMonth, PricePerQuater, PricePerYear, PolicyTypeId, PolicyVehicleRestrictionId, AgeRange, CreatedByUser, CreatedDate, LastModified, Description)"
                         + " VALUES('" + policy.Name +
                         "'," + policy.PricePerMonth +
                         "," + policy.PricePerQuater +
                         "," + policy.PricePerYear +
                         "," + typeId +
                         "," + vehicleId +
                         ",'" + policy.AgeRange.ToString() +
                         "','" + user +
                         "','" + currentDate.ToString() +
                         "','" + modifiedDate.ToString() +
                         "','" + policy.Description +
                         "')";

            try
            {
                OleDbCommand command = new OleDbCommand(sql, con);
                if (command.ExecuteNonQuery() == 1)
                {
                    message = "New Policy Created";
                }
                con.Close();
            }
            catch (Exception ex)
            {
                message = ex.Message.ToString();
            }
            finally
            {
                // Close the connection
                if (con != null)
                {
                    con.Close();
                }
            }

            return message;
        }
        /// <summary>
        /// updates a policy if the database if it passes the validation tests
        /// </summary>
        /// <param name="policy"></param>
        /// <returns></returns>
        private string writeModifiedPolicy(Policy policy, int id)
        {
            int typeId;
            int vehicleId;

            DateTime modifiedDate = new DateTime();
            string message = "";

            modifiedDate = DateTime.Now;

            typeId = getPolicyType(policy.PolicyType);
            vehicleId = getVehicleType(policy.TypeOfVechile);
            //Connect to DB
            OleDbConnection con = new OleDbConnection(connect);
            con.Open();
            //SQL command to insert a new passenger
            string sql = "UPDATE Policy " +
                         "Set PolicyName = '" + policy.Name + "', " +
                         "PricePerMonth = " + policy.PricePerMonth + ", " +
                         "PricePerQuater = " + policy.PricePerQuater + ", " +
                         "PricePerYear = " + policy.PricePerYear + ", " +
                         "PolicyTypeId = " + typeId + ", " +
                         "PolicyVehicleRestrictionId = " + vehicleId + ", " +
                         "AgeRange = '" + policy.AgeRange.ToString() + "', " +
                         "LastModified = '" + modifiedDate.ToString() + "', " +
                         "Description = '" + policy.Description + "' " +
                         "Where ID =" + id;

            try
            {
                OleDbCommand command = new OleDbCommand(sql, con);
                if (command.ExecuteNonQuery() == 1)
                {
                    message = "Policy Modified";
                }
                else
                {
                    message = "Querry did not update anything, check that your ID is correct";
                }
                con.Close();
            }
            catch (Exception ex)
            {
                message = ex.Message.ToString();
            }
            finally
            {
                // Close the connection
                if (con != null)
                {
                    con.Close();
                }
            }

            return message;
        }