using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Maintenance_TEC_ToolsDetail : System.Web.UI.Page {
    //the function code for localization on this page
    protected override void InitializeCulture() {
        ProfileCommon p = (ProfileCommon)this.Context.Profile;

        if (Request.Form.Get("ctl00$selLanguage") != null)
            p.lang = Request.Form.Get("ctl00$selLanguage").ToString();
        if (p.lang == String.Empty)
            p.lang = Session["Localization"].ToString();

        if (p.lang.Length > 0) {
            this.Culture = p.lang;
            this.UICulture = System.Globalization.CultureInfo.CreateSpecificCulture(p.lang).Name;
        }
    }
    //select theme
    protected void Page_PreInit(object sender, EventArgs e) {
        Page.Theme = Profile.UI.Theme;

        // 沒有權限
        if (!SES_Common.Authorize(Request.Url.ToString(), Profile.UserName)) {
            Response.Redirect("/SEST/Common/NoAuthorization.aspx");
        }
    }


    // Guarantee changing
    protected void ddlGuarantee_Changed(object sender, EventArgs e) {
        DropDownList ddlGuarantee = ((DropDownList)sender);
        if (ddlGuarantee.ID == "ddlEditGuarantee")
            ((HiddenField)DetailsView1.FindControl("hidEditGuarantee")).Value = ddlGuarantee.Text;
        else if (ddlGuarantee.ID == "ddlInsertGuarantee")
            ((HiddenField)DetailsView1.FindControl("hidInsertGuarantee")).Value = ddlGuarantee.Text;
    }
    // Guarantee data binding
    protected void Guarantee_DataBinding(object sender, EventArgs e) {
        DropDownList ddl;
        if (((HiddenField)sender).ID == "hidEditGuarantee") {
            ddl = ((DropDownList)DetailsView1.FindControl("ddlEditGuarantee"));
            ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(((HiddenField)sender).Value));
        }
        else if (((HiddenField)sender).ID == "hidInsertGuarantee") {
            ddl = ((DropDownList)DetailsView1.FindControl("ddlInsertGuarantee"));
            ((HiddenField)sender).Value = ddl.SelectedValue;
        }
    }


    // Fab id changing
    protected void ddlFABID_Changed(object sender, EventArgs e) {
        DropDownList ddlFABID = ((DropDownList)sender);
        if (ddlFABID.ID == "ddlEditFABID") // 
            ((HiddenField)DetailsView1.FindControl("hidEditFABID")).Value = ddlFABID.Text;//.Substring(0, ddlFABID.Text.IndexOf(" - "));
        else if (ddlFABID.ID == "ddlInsertFABID")
            ((HiddenField)DetailsView1.FindControl("hidInsertFABID")).Value = ddlFABID.Text;//.Substring(0, ddlFABID.Text.IndexOf(" - "));
    }
    //Fab id data binding
    protected void lblFABID_DataBinding(object sender, EventArgs e) {
        DropDownList ddl;
        if (((HiddenField)sender).ID == "hidEditFABID") {
            ddl = ((DropDownList)DetailsView1.FindControl("ddlEditFABID"));
            for (int i = 0; i < ddl.Items.Count; i++) {
                if (ddl.Items[i].Text.IndexOf(((HiddenField)sender).Value) >= 0) {
                    ddl.SelectedIndex = i;
                    break;
                }
            }
            //ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(((Label)sender).Text));
        }
        else if (((HiddenField)sender).ID == "hidInsertFABID") {
            ddl = ((DropDownList)DetailsView1.FindControl("ddlInsertFABID"));
            ddl.SelectedIndex = 0; //ddl.Items.IndexOf(ddl.Items.FindByText(((Label)sender).Text));
            ((HiddenField)sender).Value = ddl.SelectedValue;
        }
    }

    //// Calendar id changing
    //protected void Calendar_Changed(object sender, EventArgs e) {
    //    Calendar cal = ((Calendar)sender);
    //    //if (((Calendar)sender).ID == "EditCalendar")
    //    //    ((Label)DetailsView1.FindControl("lblEditCalendar")).Text = cal.SelectedDate.ToString();
    //    //else 
    //        if (((Calendar)sender).ID == "InsertCalendar")
    //            ((HiddenField)DetailsView1.FindControl("hidInsertInstallDate")).Value = cal.SelectedDate.ToString();
    //}

    
    protected void DetailsViewItemCommanded(object sender, EventArgs e) {
        Page.Response.RedirectLocation = "Tools.aspx";
    }
    protected void Page_Load(object sender, EventArgs e) {
        //if (DetailsView1.FindControl("hlInstallDate") != null) {
        //    ((HyperLink)DetailsView1.FindControl("hlInstallDate")).Attributes.Add(
        //        "onclick", "openSubPageCalendar('" + DetailsView1.FindControl("txtEditInstallDate").ClientID + "')");
        //    ((HyperLink)DetailsView1.FindControl("hlInstallDate")).Attributes.Add("style", "cursor: hand");
        //}
        //if (DetailsView1.FindControl("hlGuaranteeStartDate") != null) {
        //    ((HyperLink)DetailsView1.FindControl("hlGuaranteeStartDate")).Attributes.Add(
        //        "onclick", "openSubPageCalendar('" + DetailsView1.FindControl("txtGraranteeStartDate").ClientID + "')");
        //    ((HyperLink)DetailsView1.FindControl("hlGuaranteeStartDate")).Attributes.Add("style", "cursor: hand");
        //}
        //if (DetailsView1.FindControl("hlGraranteeEndDate") != null) {
        //    ((HyperLink)DetailsView1.FindControl("hlGraranteeEndDate")).Attributes.Add(
        //        "onclick", "openSubPageCalendar('" + DetailsView1.FindControl("txtGraranteeEndDate").ClientID + "')");
        //    ((HyperLink)DetailsView1.FindControl("hlGraranteeEndDate")).Attributes.Add("style", "cursor: hand");
        //}
    }


    protected void DetailsView1_DataBound(object sender, EventArgs e) {
        if (DetailsView1.FindControl("hlReleasedDate") != null) {
            ((HyperLink)DetailsView1.FindControl("hlReleasedDate")).Attributes.Add(
                "onclick", "openSubPageCalendar('" + DetailsView1.FindControl("txtReleasedDate").ClientID + "')");
            ((HyperLink)DetailsView1.FindControl("hlReleasedDate")).Attributes.Add("style", "cursor: hand");
        }
        if (DetailsView1.FindControl("hlMovingDate") != null) {
            ((HyperLink)DetailsView1.FindControl("hlMovingDate")).Attributes.Add(
                "onclick", "openSubPageCalendar('" + DetailsView1.FindControl("txtMovingDate").ClientID + "')");
            ((HyperLink)DetailsView1.FindControl("hlMovingDate")).Attributes.Add("style", "cursor: hand");
        }
        if (DetailsView1.FindControl("hlInstalledDate") != null) {
            ((HyperLink)DetailsView1.FindControl("hlInstalledDate")).Attributes.Add(
                "onclick", "openSubPageCalendar('" + DetailsView1.FindControl("txtInstalledDate").ClientID + "')");
            ((HyperLink)DetailsView1.FindControl("hlInstalledDate")).Attributes.Add("style", "cursor: hand");
        }
        if (DetailsView1.FindControl("hlGuaranteeStartDate") != null) {
            ((HyperLink)DetailsView1.FindControl("hlGuaranteeStartDate")).Attributes.Add(
                "onclick", "openSubPageCalendar('" + DetailsView1.FindControl("txtGuaranteeStartDate").ClientID + "')");
            ((HyperLink)DetailsView1.FindControl("hlGuaranteeStartDate")).Attributes.Add("style", "cursor: hand");
        }
        if (DetailsView1.FindControl("hlGuaranteeEndDate") != null) {
            ((HyperLink)DetailsView1.FindControl("hlGuaranteeEndDate")).Attributes.Add(
                "onclick", "openSubPageCalendar('" + DetailsView1.FindControl("txtGuaranteeEndDate").ClientID + "')");
            ((HyperLink)DetailsView1.FindControl("hlGuaranteeEndDate")).Attributes.Add("style", "cursor: hand");
        }


        // 不是來自管理的路徑 (功能 hidden)
        if (Request.Url.ToString().ToUpper().IndexOf("SEST/MAINTENANCE/") < 0) {
            DetailsView1.Fields[24].Visible = false;    //新增,刪除,編輯
        }
    }


    protected void ObjectDataSource_ItemCommanded(object sender, EventArgs e) {
        //// 往前移一頁(避免重新整理時再執行一次)再重新載入
        //string sScript = "";
        //sScript = "<script language='javascript' type='text/javascript'>\r\n";
        //sScript += "window.history.go(-3);";
        //sScript += "window.location.replace('Tools.aspx');";
        //sScript += "</script>\r\n";
        //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Back", sScript);
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Back", SES_Common.BackScript(Request.Url.ToString(), -1));
    }

}
