using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.UI;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                         Started Date  : 12-Aug-2007
* Module          : Organisation Levels                               Last Modified : 20-Aug-2007
* Programmer      : Senthil Kumar NP
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_LevelChildModify : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objcommon = new cls_Common();
    DataSet ds_ParentLevels = new DataSet();
    byte[] Image;
    int id;
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        //get the culture info to set the language
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        //strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_Entervalid") + "','" + rm.GetString("Alert_number") + "','" + rm.GetString("Alert_PhoneNumber") + "','" + rm.GetString("Alert_ConfirmSave") + "');";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_Entervalid") + "','" + rm.GetString("Alert_number") + "','" + rm.GetString("Alert_PhoneNumber") + "','" + rm.GetString("Alert_Entervalidphonenumber") + "','" + rm.GetString("Alert_EntervalidFaxnumber") + "','" + rm.GetString("Alert_EntervalidMobilenumber") + "','" + rm.GetString("Alert_ConfirmSave") + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        //Set UserRights
        UserRights();
        int LevelID = Convert.ToInt16(Session["orgLevelid"]);
        objcommon.GetParentLevels_FromLevelID(LevelID);
        ds_ParentLevels = objcommon.GetParentLevels();
        if (Request.QueryString["tid"] != null)
        {
            if (Convert.ToInt32(Request.QueryString["tid"].ToString()) != 1)
            {
                LogoIdView.Visible = false;
                if (ds_ParentLevels.Tables[0].Rows.Count > 0)
                {
                    lbl_Man.Text = "<span style='color: Red'>|</span>" + rm.GetString("IndicatesMandatoryFields", ci) + " (" + Convert.ToString(ds_ParentLevels.Tables[0].Rows.Count) + " No.)";
                }
            }
            else
            {
                LogoIdView.Visible = true;
                if (ds_ParentLevels.Tables[0].Rows.Count > 0)
                {
                    lbl_Man.Text = "<span style='color: Red'>|</span>" + rm.GetString("IndicatesMandatoryFields", ci) + " (" + Convert.ToString(ds_ParentLevels.Tables[0].Rows.Count) + " No.)";
                }
                else
                {
                    lbl_Man.Text = "<span style='color: Red'>|</span>" + rm.GetString("IndicatesMandatoryFields", ci) + " (1 No.)";
                }
            }
        }
        try
        {
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            if (!IsPostBack)
            {
                id = Convert.ToInt16(Request.QueryString["tid"]);
                Session["orgLevelid"] = id;
                txtLevelName.Focus();
                int levelid = Convert.ToInt16(Request.QueryString["chID"]);
                if (levelid != 0)
                {
                    Session["levelid"] = levelid;
                    getLevelDetails(levelid);
                }
                objcommon.menu("OrgSettings", "6");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    protected void getLevelDetails(int levelid)
    {
        org_levelsEventData selectLevel = new org_levelsEventData();
        DataSet dsLevHed = new DataSet();
        dsLevHed = selectLevel.selectLevelDetails(Convert.ToInt16(Session["orgLevelid"]), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["LevelRights"]), Convert.ToInt16(Session["parentLevelId"]));
        if (dsLevHed.Tables[0].Rows.Count > 0)
        {
            if ((Convert.ToString(dsLevHed.Tables[0].Rows[0]["levelName"]) != ""))
            {
                this.lblHead.Text = Convert.ToString(dsLevHed.Tables[0].Rows[0]["levelName"]) + " - Modify";
            }
        }
        dsLevHed.Clear();
        dsLevHed.Dispose();
        //org_levelsEventData selectLevel = new org_levelsEventData();
        DataSet dsLev = new DataSet();
        dsLev = selectLevel.selectOrgDetails(Convert.ToInt16(levelid));
        if (dsLev.Tables[0].Rows.Count > 0)
        {
            if (dsLev.Tables[0].Rows[0]["parentId"] != System.DBNull.Value)
            {
                Session["parentLevelId"] = dsLev.Tables[0].Rows[0]["parentId"];
                Session["parentLevelId_modify"] = dsLev.Tables[0].Rows[0]["parentId"];
            }
            if (dsLev.Tables[0].Rows[0]["name"] != System.DBNull.Value)
            {
                //this.lblHead.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["levelName"]) + " Details";
                this.txtLevelName.Text = Server.HtmlDecode(Convert.ToString(dsLev.Tables[0].Rows[0]["name"]));
            }
            if (dsLev.Tables[0].Rows[0]["description"] != System.DBNull.Value)
            {
                this.txtDescription.Text = Server.HtmlDecode(Convert.ToString(dsLev.Tables[0].Rows[0]["description"]));
            }
            if (dsLev.Tables[0].Rows[0]["city"] != System.DBNull.Value)
            {
                this.txtCity.Text = Server.HtmlDecode(Convert.ToString(dsLev.Tables[0].Rows[0]["city"]));
            }
            if (dsLev.Tables[0].Rows[0]["state"] != System.DBNull.Value)
            {
                this.txtState.Text = Server.HtmlDecode(Convert.ToString(dsLev.Tables[0].Rows[0]["state"]));
            }
            if (dsLev.Tables[0].Rows[0]["country"] != System.DBNull.Value)
            {
                this.txtCountry.Text = Server.HtmlDecode(Convert.ToString(dsLev.Tables[0].Rows[0]["country"]));
            }
            if (dsLev.Tables[0].Rows[0]["zip"] != System.DBNull.Value)
            {
                this.txtZip.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["zip"]);
            }
            if (dsLev.Tables[0].Rows[0]["phone1"] != System.DBNull.Value)
            {
                this.txtPhone1.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["phone1"]);
            }
            if (dsLev.Tables[0].Rows[0]["phone2"] != System.DBNull.Value)
            {
                this.txtPhone2.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["phone2"]);
            }
            if (dsLev.Tables[0].Rows[0]["fax1"] != System.DBNull.Value)
            {
                this.txtFax.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["fax1"]);
            }
            if (dsLev.Tables[0].Rows[0]["fax2"] != System.DBNull.Value)
            {
                this.txtMobile.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["fax2"]);
            }
            if (dsLev.Tables[0].Rows[0]["email"] != System.DBNull.Value)
            {
                this.txtEmail.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["email"]);
            }
            if (dsLev.Tables[0].Rows[0]["webAddress"] != System.DBNull.Value)
            {
                this.txtWeb.Text = Convert.ToString(dsLev.Tables[0].Rows[0]["webAddress"]);
            }
            if (dsLev.Tables[0].Rows[0]["logo"] != System.DBNull.Value)
            {
                Image = (byte[])dsLev.Tables[0].Rows[0]["logo"];
            }
        }
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        org_levelsEventData selectLevel = new org_levelsEventData();
        org_levelsEventData ObjClass = new org_levelsEventData();
        if (this.upLogo.HasFile)
        {
            int index = upLogo.PostedFile.FileName.LastIndexOf(".");
            string fileFormat = upLogo.PostedFile.FileName.Substring(index + 1);
            fileFormat = fileFormat.ToLower();
            //if (fileFormat == "jpeg" || fileFormat == "jpg" || fileFormat == "bmp")
            if (fileFormat == "bmp")
            {
                Image = upLogo.FileBytes;
            }
            else
            {
                //this.lblErr.Text = "Please Select jpeg,jpg,bmp file File for logo";
                //base.Message("Please Select jpeg,jpg,bmp file File for logo");
                base.Message(rm.GetString("Alert_PleaseSelectFileforlogo", ci));
                return;
            }
        }
        else
        {
            DataSet dsLev = new DataSet();
            dsLev = selectLevel.selectOrgDetails(Convert.ToInt16(Session["levelid"]));
            if (dsLev.Tables[0].Rows.Count > 0)
            {
                if (dsLev.Tables[0].Rows[0]["logo"] != System.DBNull.Value)
                {
                    Image = (byte[])dsLev.Tables[0].Rows[0]["logo"];
                }
            }
        }
        //org_levelsEventData selectLevel = new org_levelsEventData();
        int orgId = Convert.ToInt16(Session["parentLevelId"]);
        int MinLevel = 0;
        DataSet dsMin = new DataSet();
        dsMin = ObjClass.SelectMinLevel(Convert.ToInt16(Session["orgLevelid"]), Convert.ToInt16(Session["parentLevelId"]));
        if (dsMin != null)
        {
            if (dsMin.Tables.Count > 0)
            {
                if (dsMin.Tables[0].Rows.Count > 0)
                {
                    if (dsMin.Tables[0].Rows[0]["Result"] != DBNull.Value)
                    {
                        MinLevel = Convert.ToInt16(dsMin.Tables[0].Rows[0]["Result"]);
                    }
                }
            }
        }
        //if (orgId != 0 || Convert.ToInt16(Session["orgLevelid"]) == MinLevel)
        if (MinLevel == 1)
        {
            if (selectLevel.CheckLevelNameUpdate(Convert.ToInt16(Session["parentLevelId"]), id, Convert.ToInt16(Session["levelid"]), txtLevelName.Text.Trim()) == 0)
            {
                selectLevel.InsertChildLevel("U", Convert.ToInt16(Session["orgLevelid"]), Server.HtmlEncode(this.txtLevelName.Text.Trim()), Convert.ToInt16(Session["parentLevelId"]), Server.HtmlEncode(this.txtDescription.Text), Server.HtmlEncode(this.txtCity.Text), Server.HtmlEncode(this.txtState.Text), Server.HtmlEncode(this.txtCountry.Text), this.txtZip.Text, this.txtPhone1.Text, this.txtPhone2.Text, this.txtFax.Text, this.txtMobile.Text, this.txtEmail.Text, this.txtWeb.Text, Convert.ToInt16(Session["levelid"]), Image, Session["EmpAutoID"].ToString());
                Session["Logo"] = null;
                Session["parentLevelId"] = null;
                Session["levelid"] = null;
                Session["orgChildId"] = null;
                Session["parentLevelId"] = null;
                cls_master_levels class1 = new cls_master_levels();
                DataSet ds = new DataSet();
                if (Session["UserGroupName"] != null)
                {
                    ds = class1.getOrgLevelUpdated(0, Convert.ToString(Session["LevelRights"]), 0, Convert.ToString(Session["UserGroupName"]));
                }
                //stores the organization levels.
                Session["OrgLevels"] = ds;
                //string str = "alert('Level Saved successfully');location.replace('TA_UI_Master_OrganisationLevels.aspx');";
                string str = "alert('" + rm.GetString("Alert_Levelsavedsuccessful", ci) + "');location.replace('TA_UI_Master_LevelDetails.aspx?tid=" + Session["orgLevelid"] + "&chID=" + Request.QueryString["chID"].ToString() + "&sid=" + Session.SessionID + "');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
                //Response.Redirect("TA_UI_Master_OrganisationLevels.aspx");
            }
            else
            {
                //lblErr.Text = "Level Name Already Exits!...";
                //base.Message("Level name already exits!...");
                base.Message(rm.GetString("Alert_Levelnamealreadyexits", ci));
            }
        }
        else
        {
            //lblErr.Text = "Select Organization Levels";
            //base.Message("Select organization levels");
            base.Message(rm.GetString("Alert_Selectallorganizationlevels", ci));
        }
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        //Response.Redirect("TA_UI_Master_OrganisationLevels.aspx");
        Response.Redirect("TA_UI_Master_LevelDetails.aspx?tid=" + Session["orgLevelid"] + "&chID=" + Request.QueryString["chID"].ToString() + "&sid=" + Session.SessionID);
        //Response.Redirect("TA_UI_Master_LevelChildView.aspx?tid=" + Session["orgLevelid"] + "&chID=" + Request.QueryString["chID"].ToString() + "&sid=" + Session.SessionID);
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lblHead.Text = rm.GetString("Menu_Masters_OrgLevels", ci);
        lbl_Name.Text = rm.GetString("OrgLevels_CompDetails_Name", ci);
        lbl_Description.Text = rm.GetString("Designation_DesignationDesc", ci);
        lbl_City.Text = rm.GetString("Masters_Generic_City", ci);
        lbl_State.Text = rm.GetString("Masters_Generic_State", ci);
        lbl_Country.Text = rm.GetString("Masters_Generic_Country", ci);
        lbl_Zip.Text = rm.GetString("OrgLevels_CompDetails_Zip", ci);
        lbl_Phone1.Text = rm.GetString("OrgLevels_CompDetails_Phone1", ci);
        lbl_Phone2.Text = rm.GetString("OrgLevels_CompDetails_Phone2", ci);
        lbl_Fax.Text = rm.GetString("OrgLevels_CompDetails_Fax", ci);
        lbl_Mobile.Text = rm.GetString("OrgLevels_CompDetails_Mobile", ci);
        lbl_Email.Text = rm.GetString("OrgLevels_CompDetails_Email", ci);
        lbl_WebAdd.Text = rm.GetString("OrgLevels_CompDetails_WebAddr", ci);
        lbl_Logo.Text = rm.GetString("OrgLevels_CompDetails_Logo", ci);
        lbl_Man.Text = rm.GetString("IndicatesMandatoryFields", ci);
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Alert_EnterName", ci);
        RegularExpressionValidator1.ErrorMessage = rm.GetString("Alert_EnterValidEmailId", ci);
        RegularExpressionValidator2.ErrorMessage = rm.GetString("Alert_EnterValidWebAddress", ci); ;
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(6, Convert.ToInt32(Session["userGroup_Id"]), "modifyPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
        else
        {
            //   
        }
    }
}
