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

public partial class Business_GLS1 : System.Web.UI.Page
{
    public int page_id = 0;
    public string table_name1 = "",table_name2="",table_name3="";
    string url = "";
    static int intCount = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        table_name1 = "span";
        table_name2 = "lkp_bank_bin";
        table_name3 = "atm_edc_span";
        url = HttpContext.Current.Request.Url.AbsolutePath;
        string[] urlArr = url.Split('/');
        url = urlArr[2] + "/" + urlArr[3];
        page_id = loadPageIDs(url);
        if (!IsPostBack)
        {

           // this.Master.FindControl("Menu1").Visible = false;
           
            pnlControls.Visible = false;
            pnlGrid.Visible = false;
            Panel1.Visible = false;
            loadBankDetails("");
            btnAddBin.Visible = false;
            ExceptionUtility.LogException(0, "", "1", "Page_Load", "");
        }
        lblError.Text = "";
       
    }

    private int loadPageIDs(string URL)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "lookup_getter.get_pages";

            oracom.Parameters.Add("p_url", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_pages", OracleType.Cursor).Direction = ParameterDirection.Output;


            oracom.Parameters["p_url"].Value = URL;

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            oracon.Close();
            return int.Parse(ds.Tables[0].Rows[0]["pid"].ToString());
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "lookup_getter.get_pages", "", "loadPageIDs", "p_url- " + URL);
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }

    }
    protected void ddlAtmType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlBank.SelectedIndex != 0)
        {
            pnlControls.Visible = true;
            pnlGrid.Visible = true;
            Panel1.Visible = false;
            btnAddBin.Visible = true;
            Label1.Text = "";
            if (ddlBank.SelectedItem.ToString() == "------- Add New Bank ------------")
            {
                string s = ddlBank.SelectedValue; 
                txtLocalBin.ReadOnly = false;
                txtBankName.ReadOnly = false ;
                txtAbbreviation.ReadOnly = false;
                txtAbbreviation.Text = "";
                txtBankName.Text = "";
                txtLocalBin.Text = "";
                pnlGrid.Visible = false;
                btnDelete.Visible = false;
               
            }
            else
            {
                txtLocalBin.ReadOnly = true;
                txtBankName.ReadOnly = true;
                txtAbbreviation.ReadOnly = true;
                loadBankDetails(ddlBank.SelectedValue);
                loadBankBins(ddlBank.SelectedValue);
                btnDelete.Visible = true;
               
            }
        }
        ExceptionUtility.LogException(0, "", "", "", "");

    }
    private int loadChgID()
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "maker_checker_editor.get_chg_id";


            oracom.Parameters.Add("p_chg_id", OracleType.Number).Direction = ParameterDirection.Output;

            oracom.ExecuteNonQuery();
            oracon.Close();

            int nop = int.Parse(oracom.Parameters["p_chg_id"].Value.ToString());
            oracon.Close();
            ExceptionUtility.LogException(0, "maker_checker_editor.get_chg_id","483.1", "loadChgID", "");
            return nop;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "maker_checker_editor.get_chg_id","483.1", "loadChgID", "");
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }
    private int loadMaxChgID(string atm_number)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "maker_checker_editor.get_max_chg_id";

            oracom.Parameters.Add("p_changed_rec_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_id", OracleType.Number).Direction = ParameterDirection.Output;

            oracom.Parameters["p_changed_rec_id"].Value = atm_number;

            oracom.ExecuteNonQuery();
            oracon.Close();

            int nop = int.Parse(oracom.Parameters["p_chg_id"].Value.ToString());
            oracon.Close();
            ExceptionUtility.LogException(0, "maker_checker_editor.get_max_chg_id", "", "loadMaxChgID", "atm_number- " + atm_number);
            return nop;
            
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "maker_checker_editor.get_max_chg_id","","loadMaxChgID", "atm_number: " + atm_number);
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }

    private bool UpdateMakerChecker(int chgId,string tableName, string pkey, string columnName, string oldValue, string newValue, string oldValueNo, string NewValueNo, int chgType, ref string error)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "MAKER_CHECKER_EDITOR.Add_Maker_Checker";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_type", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_page_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_table_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_column_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_old_value", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_new_value", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_maker_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_made_on_date", OracleType.DateTime).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_checker_id", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_checked_on_date", OracleType.DateTime).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_changed_rec_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_status", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_checker_notes", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_old_number", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_new_number", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            oracom.Parameters["p_page_id"].Value = page_id;
            oracom.Parameters["p_table_name"].Value = tableName;
            oracom.Parameters["p_chg_id"].Value = chgId;
            oracom.Parameters["p_chg_type"].Value = chgType;
            oracom.Parameters["p_column_name"].Value = columnName;
            oracom.Parameters["p_new_value"].Value = newValue.Trim ();
            oracom.Parameters["p_old_value"].Value = oldValue.Trim ();
            oracom.Parameters["p_maker_id"].Value = Session["uid"].ToString();
            oracom.Parameters["p_made_on_date"].Value = OracleDateTime.Null;
            oracom.Parameters["p_checker_id"].Value = OracleNumber.Null;
            oracom.Parameters["p_checked_on_date"].Value = OracleDateTime.Null;
            oracom.Parameters["p_changed_rec_id"].Value = pkey;
            oracom.Parameters["p_chg_status"].Value = 0;
            oracom.Parameters["p_checker_notes"].Value = OracleString.Null;

            if (oldValueNo.Trim() == "")
                oracom.Parameters["p_old_number"].Value = OracleNumber.Null;
            else
                oracom.Parameters["p_old_number"].Value = oldValueNo;
            if (NewValueNo.Trim() == "")
                oracom.Parameters["p_new_number"].Value = OracleNumber.Null;
            else
                oracom.Parameters["p_new_number"].Value = NewValueNo;

            oracom.Parameters["p_lang"].Value = "EN";

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "MAKER_CHECKER_EDITOR.Add_Maker_Checker", "483.2", "UpdateMakerChecker", "chgId- " + chgId + ": tableName- " + tableName + ": pkey- " + pkey + ": columnName- " + columnName + ": oldValue- " + oldValue + " : newValue- " + newValue + " : oldValueNo- " + oldValueNo + ": NewValueNo- " + NewValueNo + " :chgType- " + chgType);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "MAKER_CHECKER_EDITOR.Add_Maker_Checker", "483.2", "UpdateMakerChecker", "chgId- " + chgId + ": tableName- " + tableName + ": pkey- " + pkey + ": columnName- " + columnName + ": oldValue- " + oldValue + " : newValue- " + newValue + " : oldValueNo- " + oldValueNo + ": NewValueNo- " + NewValueNo + " :chgType- " + chgType);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "MAKER_CHECKER_EDITOR.Add_Maker_Checker","483.2", "UpdateMakerChecker", "chgId- "+chgId +": tableName- "+tableName +": pkey- "+pkey +": columnName- "+columnName +": oldValue- "+oldValue +" : newValue- "+newValue +" : oldValueNo- "+oldValueNo+": NewValueNo- "+NewValueNo+" :chgType- "+chgType);
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
    private void loadBankDetails(string strLocalBin)
    {
        try
        {
            int count = 0;
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "bank_lkp_editor.get_bank";

            oracom.Parameters.Add("p_local_bin", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_lang"].Value = "EN";
            if (strLocalBin == "")
                oracom.Parameters["p_local_bin"].Value = OracleString.Null;
            else
                oracom.Parameters["p_local_bin"].Value =strLocalBin;

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);
            count = ds.Tables[0].Rows.Count;
            count = count + 1;
            ViewState["count"] = count;
            if (strLocalBin == "")
            {
                ddlBank.DataSource = ds;
                ddlBank.DataBind();

                //ddlAtmType.Items.Insert(0, "------ ATM Type ------");
                ddlBank.Items.Insert(0, new ListItem("------------- Select Bank --------------", "0"));
                ddlBank.Items.Insert(count, new ListItem("------- Add New Bank ------------", count.ToString()));
            }
            else
            {
                txtLocalBin.Text = ds.Tables[0].Rows[0]["local_bin"].ToString();
                txtBankName.Text = ds.Tables[0].Rows[0]["short_name"].ToString();
                txtAbbreviation.Text = ds.Tables[0].Rows[0]["long_name"].ToString();
            }
            oracon.Close();
            ExceptionUtility.LogException(0, "bank_lkp_editor.get_bank", "", "loadBankDetails", "strLocalBin: " + strLocalBin);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "bank_lkp_editor.get_bank","", "loadBankDetails", "strLocalBin: " + strLocalBin);
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }
    private void loadBankBins(string strLocalBin)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "bank_lkp_editor.get_bank_bins";

            oracom.Parameters.Add("p_local_bin", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_bin", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_lang"].Value = "EN";
            if (strLocalBin == "")
                oracom.Parameters["p_local_bin"].Value = OracleString.Null;
            else
                oracom.Parameters["p_local_bin"].Value = strLocalBin;

            oracom.Parameters["p_bin"].Value = OracleString.Null;
            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            GridView1.DataSource = ds;
            GridView1.DataBind();

            oracon.Close();
            ExceptionUtility.LogException(0, "bank_lkp_editor.get_bank_bins", "483.3", "loadBankBins", "strLocalBin- " + strLocalBin+":p_lang-EN");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "bank_lkp_editor.get_bank_bins", "483.3", "loadBankBins", "strLocalBin- " + strLocalBin + ":p_lang-EN");
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private DataSet GetBankBins(string bin)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "bank_lkp_editor.get_bank_bins";

            oracom.Parameters.Add("p_local_bin", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_bin", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_lang"].Value = "EN";
            //if (strLocalBin == "")
            oracom.Parameters["p_local_bin"].Value = OracleString.Null;
            //else
            //oracom.Parameters["p_local_bin"].Value = bin;

            oracom.Parameters["p_bin"].Value = bin;
            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            oracon.Close();
            ExceptionUtility.LogException(0, "bank_lkp_editor.get_bank_bins", "", "GetBankBins", "bin: " + bin + ":p_lang-EN");
            return ds;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "bank_lkp_editor.get_bank_bins", "", "GetBankBins", "bin: " + bin + ":p_lang-EN");
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return null;
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        loadBankBins(txtLocalBin.Text);
        ExceptionUtility.LogException(0, "", "", "GridView1_PageIndexChanging", "");
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("d"))
        {
            ExceptionUtility.LogException(0, "", "1.483", "GridView1_RowCommand.Delete", "");
            if (GridView1.Rows.Count > 1)
            {
                int index = int.Parse(e.CommandArgument.ToString());
                string bin = GridView1.DataKeys[index].Value.ToString();
                ViewState["bin"] = bin;
                string error = "";
                 int chgId = loadChgID();
                bool temp = false;
                temp = UpdateMakerChecker(chgId, "SPAN", bin, "BIN", bin, "", "", "", 2, ref error);
                temp = UpdateMakerChecker(chgId, "LKP_BANK_BIN", bin, "BIN", bin, "", "", "", 2, ref error);
                temp = UpdateMakerChecker(chgId, "ATM_EDC_SPAN", bin, "BIN", bin, "", "", "", 2, ref error);
                if (temp)
                {
                    lblError.Text = "Your request has been sent to Checker user for Approval";
                    loadBankBins(txtLocalBin.Text);
                    pnlControls.Visible = true;
                    Label1.Visible = false;

                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "1.483", "GridView1_RowCommand.Delete", error);
                    }
                    else 
                        lblError.Text = error;
                }
            }
            else
            {
                lblError.Text = "There must be atleast one bin";
            }

           
        }
        else if (e.CommandName.Equals("u"))
        {

            intCount = 0;
            Panel1.Visible = true;
            ExceptionUtility.LogException(0, "", "1.482", "GridView1_RowCommand.Update", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string bin = GridView1.DataKeys[index].Value.ToString();
            ViewState["mode"] = "u";
            pnlControls.Visible = true;
            pnlGrid.Enabled = false;

            ViewState["bin"] = bin;
            //put the data in the controls
            txtBin.Text = GridView1.Rows[index].Cells[2].Text;
            loadBankBins(ddlBank .SelectedValue);

        }
        else if (e.CommandName.Equals("vm"))
        {
            pnlControls.Visible = false;
            ExceptionUtility.LogException(0, "", "1.475", "GridView1_RowCommand.ViewModification", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string rowID = GridView1.DataKeys[index].Value.ToString();
            int chgId = loadMaxChgID(rowID);
            Response.Redirect("Checker.aspx?chgId=" + chgId);
        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Visible = false;
           
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        intCount = 0;
        txtBin.Text = "";
        ViewState["mode"] = "a";
        Panel1.Visible = true;
        ExceptionUtility.LogException(0, "", "1.475", "btnAdd_Click", "");
    }
    private bool addbin(ref string error)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "bank_lkp_editor.add_bank_bin";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_local_bin", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_bank_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_bank_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_bin", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            oracom.Parameters["p_local_bin"].Value = txtLocalBin.Text;
            oracom.Parameters["p_bank_code"].Value = txtBankName.Text;
            oracom.Parameters["p_bank_name"].Value = txtAbbreviation.Text;
            oracom.Parameters["p_bin"].Value = txtBin.Text;


            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "bank_lkp_editor.add_bank_bin", "485.1", "addbin", "");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "bank_lkp_editor.add_bank_bin", "485.1", "addbin", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "bank_lkp_editor.add_bank_bin","485.14", "addbin", "");
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
    protected void btnSaveBin_Click(object sender, EventArgs e)
    {
        if (intCount < 1)
            intCount++;
        else
        {
            pnlControls.Visible = false;
            lblError.Text = "Your request has been sent to Checker user for Approval";
            return;
        }
        ExceptionUtility.LogException(0, "", "1.73", "btnSaveBin_Click", "");
        if (txtBin.Text.Length > 0)
        {
            string LOCAL_BIN_NEW = "", SHORT_NAME_NEW = "", LONG_NAME_NEW = "", BIN_NEW = "";
            string LOCAL_BIN_OLD = "", SHORT_NAME_OLD = "", LONG_NAME_OLD = "", BIN_OLD = "";
            if (txtLocalBin.Text.Length > 0 && txtBankName.Text.Length > 0)
            {
                
                string mode = ViewState["mode"].ToString();
                string error = "";
                LOCAL_BIN_NEW = txtLocalBin.Text.Trim ();
                SHORT_NAME_NEW  = txtBankName.Text.Trim ();
                LONG_NAME_NEW = txtAbbreviation.Text.Trim();
                BIN_NEW = txtBin.Text;
                if (mode.Equals("a"))
                {
                    int chgId = loadChgID();
                    bool saved = false;

                    saved = UpdateMakerChecker(chgId, "SPAN", BIN_NEW, "LOCAL_BIN", "", LOCAL_BIN_NEW, "", "", 0, ref error);
                    if (saved == false)
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "Save Failed";
                            ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                        }
                        else
                            lblError.Text = error;
                        return;
                    }
                    saved = UpdateMakerChecker(chgId, "SPAN", BIN_NEW, "LENGTH", "", LOCAL_BIN_NEW.Length.ToString(), "", "", 0, ref error);
                    if (saved == false)
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "Save Failed";
                            ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                        }
                        else
                            lblError.Text = error;
                        return;
                    }
                    saved = UpdateMakerChecker(chgId, "SPAN", BIN_NEW, "SHORT_NAME", "", SHORT_NAME_NEW, "", "", 0, ref error);
                    if (saved == false)
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "Save Failed";
                            ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                        }
                        else
                            lblError.Text = error;
                        return;
                    }
                    saved = UpdateMakerChecker(chgId, "SPAN", BIN_NEW, "LONG_NAME", "", LONG_NAME_NEW, "", "", 0, ref error);
                    if (saved == false)
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "Save Failed";
                            ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                        }
                        else
                            lblError.Text = error;
                        return;
                    }
                    saved = UpdateMakerChecker(chgId, "SPAN", BIN_NEW, "BIN", "", BIN_NEW, "", "", 0, ref error);
                    if (saved == false)
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "Save Failed";
                            ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                        }
                        else
                            lblError.Text = error;
                        return;
                    }
                    saved = UpdateMakerChecker(chgId, "LKP_BANK_BIN", BIN_NEW, "BANK_CODE", "", SHORT_NAME_NEW, "", "", 0, ref error);
                    if (saved == false)
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "Save Failed";
                            ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                        }
                        else
                            lblError.Text = error;
                        return;
                    }
                    saved = UpdateMakerChecker(chgId, "LKP_BANK_BIN", BIN_NEW, "BIN", "", BIN_NEW, "", "", 0, ref error);
                    if (saved == false)
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "Save Failed";
                            ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                        }
                        else
                            lblError.Text = error;
                        return;
                    }
                    saved = UpdateMakerChecker(chgId, "ATM_EDC_SPAN", BIN_NEW, "BIN", "", BIN_NEW, "", "", 0, ref error);
                    if (saved == false)
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "Save Failed";
                            ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                        }
                        else
                            lblError.Text = error;
                        return;
                    }
                    saved = UpdateMakerChecker(chgId, "ATM_EDC_SPAN", BIN_NEW, "BANK_CODE", "", SHORT_NAME_NEW, "", "", 0, ref error);
                    if (saved == false)
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "Save Failed";
                            ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                        }
                        else
                            lblError.Text = error;
                        return;
                    }
                    saved = UpdateMakerChecker(chgId, "ATM_EDC_SPAN", BIN_NEW, "BANK_NAME", "", LONG_NAME_NEW, "", "", 0, ref error);
                    if (saved == false)
                    {
                        if (Helper.OracleError(error))
                        {
                            lblError.Text = "Save Failed";
                            ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                        }
                        else
                            lblError.Text = error;
                        return;
                    }

                    loadBankBins(txtLocalBin.Text);
                    loadBankDetails("");
                    lblError.Text = "Your request has been sent to Checker user for Approval";
                   
                }
                else if (mode.Equals("u"))
                {
                   
                    string rowID = ViewState["bin"].ToString();
                    int chgId = loadChgID();
                    bool compare = false;
                    DataSet ds = GetBankBins(rowID);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        BIN_OLD = ds.Tables[0].Rows[0]["bin"].ToString();
                        if (BIN_OLD == BIN_NEW)
                            compare = true;
                        if (compare == false)
                        {
                            bool update = false;
                            if (BIN_OLD !=BIN_NEW )                            
                                update =UpdateMakerChecker (chgId ,"SPAN" ,rowID,"BIN",BIN_OLD,BIN_NEW,"","",1,ref error );
                            if(update ==true )
                            {
                                lblError.Text = "Your request has been sent to Checker user for Approval";
                                
                            }
                            
                            else
                            {
                                if (Helper.OracleError(error))
                                {
                                    lblError.Text = "Save Failed";
                                    ExceptionUtility.LogException(9, "", "1.73", "btnSaveBin_Click", error);
                                }
                                else
                                    lblError.Text = error;
                            }
                            loadBankBins(ddlBank.SelectedValue);
                            Panel1.Visible = false;
                        }
                    }
                }

                ViewState["mode"] = null;
                ViewState["statusCode"] = null;
                pnlControls.Visible = true;
                pnlGrid.Visible = true;
                pnlGrid.Enabled = true;
                Panel1.Visible = false;
              
            }
            else
            {
                lblError.Text = "Local bin and bank name is mandatory";
            }
        }
        else
        {
            lblError.Text = "Please enter the BIN";

        }
    }
    private bool updatebin(ref string error)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "bank_lkp_editor.Update_bin";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_bin_old", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_bin_new", OracleType.Number).Direction = ParameterDirection.Input;

            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_bin_old"].Value = ViewState["bin"];
            oracom.Parameters["p_bin_new"].Value = txtBin.Text;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "bank_lkp_editor.Update_bin", "1.482", "updatebin", "");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "bank_lkp_editor.Update_bin", "1.482", "updatebin", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "bank_lkp_editor.Update_bin", "1.482", "updatebin", "");
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
    private bool deletebin(ref string error)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "BANK_LKP_EDITOR.delete_bin";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_local_bin", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_bin", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_bin"].Value = ViewState["bin"];
            oracom.Parameters["p_local_bin"].Value = txtLocalBin.Text;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "BANK_LKP_EDITOR.delete_bin", "1.483", "deletebin", "");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "BANK_LKP_EDITOR.delete_bin", "1.483", "deletebin", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "BANK_LKP_EDITOR.delete_bin","1.483", "deletebin", "");
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }
    private bool deleteBank(ref string error)
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "BANK_LKP_EDITOR.delete_bank";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_local_bin", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_local_bin"].Value = txtLocalBin.Text;
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "BANK_LKP_EDITOR.delete_bank", "1.484", "deleteBank", "");
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "BANK_LKP_EDITOR.delete_bank", "1.484", "deleteBank", "");
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "BANK_LKP_EDITOR.delete_bank","1.484", "deleteBank", "");
            ////Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtLocalBin.Text = "";
        txtBin.Text = "";
        txtAbbreviation.Text = "";
        txtBankName.Text = "";
        ExceptionUtility.LogException(0, "", "", "btnCancel_Click", "");
    }

    protected void btnSave_Click1(object sender, EventArgs e)
    {
        
    }
    protected void btnCancelBin_Click(object sender, EventArgs e)
    {
        txtLocalBin.Text = "";
        txtBin.Text = "";
        txtAbbreviation.Text = "";
        txtBankName.Text = "";
        pnlGrid.Enabled = true;
        ExceptionUtility.LogException(0, "", "1.486", "btnCancel_Click", "");
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        //string error = "";
        //bool temp = false;
        //Label1.Visible = true;
        //temp = deleteBank(ref error);
        //if (temp)            
        //    Label1.Text = "Bank Deleted";
        //else
        //    Label1.Text = error;
        //loadBankDetails("");
        //pnlGrid.Visible = false;
        //pnlControls.Visible = false;
        //btnAddBin.Visible = false;
        if (txtLocalBin.Text != "")
        {
            int chgId = loadChgID();
            bool saved = false;
            string error = "";
            ExceptionUtility.LogException(0, "", "1.484", "btnDelete_Click", "");
            saved = UpdateMakerChecker(chgId, "SPAN" ,txtLocalBin .Text .Trim(), "LOCAL_BIN", txtLocalBin.Text.Trim(), "", "", "", 2, ref error);
            if (saved == false)
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "An error occured!";
                    ExceptionUtility.LogException(0, "", "1.484", "btnDelete_Click", "");
                }
                else
                    lblError.Text = error;
                return;
            }

            saved = UpdateMakerChecker(chgId, "LKP_BANK_BIN", txtBankName .Text .Trim (), "BANK_CODE", txtBankName.Text.Trim(), "", "", "", 2, ref error);
            if (saved == false)
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "An error occured!";
                    ExceptionUtility.LogException(0, "", "1.484", "btnDelete_Click", "");
                }
                else
                    lblError.Text = error;
                return;
            }
            saved = UpdateMakerChecker(chgId, "ATM_EDC_SPAN", txtBankName.Text.Trim(), "BANK_CODE", txtBankName.Text.Trim(), "", "", "", 2, ref error);
            if (saved == false)
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "An error occured!";
                    ExceptionUtility.LogException(0, "", "1.484", "btnDelete_Click", "");
                }
                else
                    lblError.Text = error;
                return;
            }
         
       
            lblError.Text = "Your request has been sent to Checker user for Approval";
            pnlControls.Visible = false;
            pnlGrid.Visible = false;
            Panel1.Visible = false;
            loadBankDetails("");
            btnAddBin.Visible = false;
            
        }

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {

            int app_status = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "app_status"));

            if (app_status == 0)
            {
                e.Row.Cells[5].Visible = true;
                e.Row.Cells[6].Visible = false;
                e.Row.Cells[7].Visible = false;
            }
            else
            {
                e.Row.Cells[5].Visible = false;
                e.Row.Cells[6].Visible = true;
                e.Row.Cells[6].Visible = true;
            }
            e.Row.Cells[3].Visible = false;
        }
    }
}
