using System;
using System.Data;
public partial class Masters_AjaxEmpCodeValid : Base.BasePage
{
    EmployeeEventData objEmployee = new EmployeeEventData();
    org_levelsEventData objOrgLevel = new org_levelsEventData();
    DataSet dsChildLevel = new DataSet();
    string gstrEmpCode;
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);
        //Check for duplicate EmpCode in Employee Modify
        if (Request.QueryString["EmpCode"] != null && Request.QueryString["EmpCode"] != "")
        {
            if (Request.QueryString["AutoID"] != null && Request.QueryString["AutoID"] != "")
            {
                string EmpCode = Convert.ToString(Request.QueryString["EmpCode"]);
                int AutoID = Convert.ToInt32(Request.QueryString["AutoID"]);
                int count = objEmployee.CheckDuplicateEmployeeCodeupdate(EmpCode, AutoID, Convert.ToBoolean(Session["ApplnMode"]));
                if (count == 0)
                {
                    Response.Clear();
                    Response.Write("Success");
                    Response.End();
                }
                else
                {
                    Response.Clear();
                    Response.Write("Fail");
                    Response.End();
                }

            }
            if (Request.QueryString["EmpCode"] != null && Request.QueryString["EmpCode"] != "")
            {
                gstrEmpCode = Convert.ToString(Request.QueryString["EmpCode"]);
                int intLevelId = Convert.ToInt32(Session["TopLevel"]);
                CheckDuplicateCodeId(intLevelId);
                Response.Clear();
                Response.Write("Success");
                Response.End();
            }
        }
        else
        {
            if (Request.QueryString["EmailID"] != null && Request.QueryString["EmailID"] != "")
            {
                string strEmailID = "";
                string strTag = "";
                int AutoID = 0;
                AutoID = Convert.ToInt32(Request.QueryString["AutoID"]);
                strTag= Convert.ToString(Request.QueryString["tag"]);
                strEmailID = Convert.ToString(Request.QueryString["EmailID"]);
                int count = 0;
                count = objEmployee.CheckDuplicateEmailID(strTag, AutoID, strEmailID);
                if (count == 0)
                {
                    Response.Clear();
                    Response.Write("Success");
                    Response.End();
                }
                else
                {
                    Response.Clear();
                    Response.Write("Fail");
                    Response.End();
                }
            }
        }
        
    }
    private void CheckDuplicateCodeId(int LevelId)
    {
        //Check for duplicate Code
        int count = objEmployee.CheckDuplicateEmployeeCode(gstrEmpCode, LevelId, Convert.ToBoolean(Session["ApplnMode"]));
        if (count > 0)
        {
            Response.Clear();
            Response.Write("Fail");
            Response.End();
        }
        else
        {
            //Get Child Levels for the Given Level Id
            dsChildLevel = objOrgLevel.selectChildLevel(LevelId);
            if (dsChildLevel.Tables.Count > 0)
            {
                if (dsChildLevel.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dRow in dsChildLevel.Tables[0].Rows)
                    {
                        CheckDuplicateCodeId(Convert.ToInt32(dRow["id"]));
                    }
                }
            }
        }
    }
}
