protected void Page_Load(object sender, EventArgs e)
        {
            String cmd = Request["cmd"];
            int showid = Convert.ToInt32(Request["showid"]);
            int showDetailsID = -1;
            if (!String.IsNullOrEmpty(Request["ShowDetailsID"])) showDetailsID = Convert.ToInt32(Request["ShowDetailsID"]);
            ShowDetails showDetails = new ShowDetails();
            showDetails.ShowID = showid;

            DataTable showDays = showDetails.GetDays();
            foreach (DataRow showDayRow in showDays.Rows)
            {
                showDetailsID = Convert.ToInt32(showDayRow["ShowDetailID"]);
                DateTime showDate = Convert.ToDateTime(showDayRow["ShowDate"]);
                Response.Write( showDate.ToString("<h2>ddd, dd MMM yyyy</h2>"));
                ShowDetails currentShowDate = new ShowDetails(showDetailsID);
                Rings rings = new Rings();
                DataSet ringsTable = rings.GetRingsForShowDay(showDetailsID);

                int lastRingNo = 0;
                foreach (DataRow row in ringsTable.Tables[0].Rows)
                {
                    int ringID = Convert.ToInt32(row["RingID"]);
                    if (ringID > -1 && lastRingNo != Convert.ToInt32(row["RingNo"]))
                    {
                        lastRingNo = Convert.ToInt32(row["RingNo"]);

                        Response.Write(String.Format("<p>Ring: {0}</p>", lastRingNo));
                        DataSet judgesDS = rings.GetJudgesForRing(ringID);
                        if (judgesDS.Tables.Count > 0)
                        {
                            foreach (DataRow judgeRow in judgesDS.Tables[0].Rows)
                            {
                                Response.Write(String.Format("<p>Judge: {0}</p>", judgeRow["Name"]));
                                Judge judge = new Judge(judgeRow);
                                DataSet classesDS = judge.getClassesForJudge();
                                if (classesDS.Tables.Count > 0)
                                {
                                    foreach (DataRow classRow in classesDS.Tables[0].Rows)
                                    {
                                        Response.Write(String.Format(" {2,-5} {3} {4} {7} {5} {6}  ({1})<br>",
                                                        classRow["ClassID"], CalcDogsInCalc(classRow), classRow["ClsNo"], ShowClasses.expandHeight(classRow), ShowClasses.expandCatagory(classRow), classRow["LongName"], classRow["Name"], ShowClasses.shortenGrades(classRow)
                                                    ));
                                    }
                                }
                            }

                        }

                        //ringContainer.InnerHtml += String.Format(ringTemplate, lastRingNo, 1, judges, classes, ringID, helperList);
                        Response.Write("<br><br>");
                    }

                }

                Response.Write("<br><hr><br>");
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            int ShowID = Convert.ToInt32(context.Request["showid"]);
            int RingID = Convert.ToInt32(context.Request["ringid"]);
            String cmd = context.Request["doc"].ToString();

            Shows show = new Shows(ShowID);
            ShowDetails sd = new ShowDetails();
            DataTable table = sd.GetShowDetails(ShowID).Tables[0];
            String pdfPath = context.Server.MapPath(cmd + ".pdf");
            Boolean publish = false;
            if (!String.IsNullOrEmpty(context.Request["publish"]) && context.Request["publish"].ToString() == "1") publish = true;

            Document doc = new Document(PageSize.A4, -50, -50, 2, 2);
            Stream output;
            if (publish)
            {
                String path = context.Server.MapPath(@"..\schedules\");
                path += DateTime.Today.ToString("yyyy");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += String.Format("\\{0:yyyyMM}_{1}.pdf", show.ShowDate, show.ShowName);
                output = new FileStream(path, FileMode.Create);
            }
            else
            {
                output = new MemoryStream();
            }
            var writer = PdfWriter.GetInstance(doc, output);
            StyleSheet sheet = new StyleSheet();

            doc.Open();
            Font smallFont = new Font(Font.COURIER, 8, Font.NORMAL, Color.BLACK);
            Font normalFont = new Font(Font.COURIER, 10, Font.NORMAL, Color.BLACK);
            Font headerFont = new Font(Font.HELVETICA, 12, Font.BOLD, Color.BLACK);
            Font ClassTitleFont = new Font(Font.HELVETICA, 16, Font.NORMAL, Color.BLACK);
            Font bigFont = new Font(Font.HELVETICA, 25, Font.NORMAL, Color.BLACK);
            Font mediumFont = new Font(Font.HELVETICA, 20, Font.NORMAL, Color.BLACK);
            ShowClasses sc = new ShowClasses();
            Rings currentRing = new Rings(RingID);
            List<ShowClasses> showClasses = ShowClasses.GetAllClassesForShowRing(ShowID, RingID);
            PdfPTable callingListTbl = null;
            foreach (ShowClasses showClass in showClasses)
            {
                List<int> resultCnts = ShowClasses.getDogsInClass(showClass.ID, (showClass.Catagory == 0));

                int gradeidx = 0;
                foreach (int dogCounts in resultCnts)
                {
                    PdfPTable ptable = new PdfPTable(3);
                    PdfPCell cell;

                    String grades;
                    String subTitle;
                    if (showClass.Catagory == 0)
                    {
                        if (gradeidx < showClass.Grades.Length)
                        {
                            grades = "Grade " + showClass.Grades[gradeidx];
                        }
                        else
                        {
                            grades = showClass.Grades;
                        }
                        subTitle = String.Format("{0} {1} {2} {3} - Judge: {4}", showClass.longHeight, showClass.longCatagory, showClass.LongClassName, grades, Judge.getJudgeForClass(showClass.ID));
                    }
                    else
                    {
                        if (showClass.Grades.Length == 1)
                        {
                            grades = showClass.Grades;
                        }
                        else
                        {
                            grades = showClass.Grades[0] + " - " + showClass.Grades[showClass.Grades.Length - 1];
                        }
                        subTitle = String.Format("{0} {1} {3} {2} - Judge: {4}", showClass.longHeight, showClass.longCatagory, showClass.LongClassName, grades, Judge.getJudgeForClass(showClass.ID));

                    }
                    String tmp = String.Format("Result Sheet - Ring No:{0}", currentRing.RingNo);
                    cell = new PdfPCell(new Phrase(new Chunk(tmp, bigFont)));
                    cell.Colspan = 2;
                    cell.BorderWidth = 0;
                    ptable.AddCell(cell);

                    tmp = String.Format("Class No:{0}", showClass.ClassNo);
                    cell = new PdfPCell(new Phrase(new Chunk(tmp, bigFont)));
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    cell.BorderWidth = 2;
                    cell.Padding = 5;
                    ptable.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk(subTitle, mediumFont)));
                    cell.Colspan = 3;
                    cell.BorderWidth = 0;
                    ptable.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("", ClassTitleFont)));
                    cell.Colspan = 3;
                    cell.BorderWidth = 0;
                    ptable.AddCell(cell);
                    ptable.AddCell(cell);
                    ptable.AddCell(cell);
                    doc.Add(ptable);

                    float[] colWidths = { 75, 75, 200, 200, 50, 75 };
                    callingListTbl = new PdfPTable(colWidths);

                    cell = new PdfPCell(new Phrase(new Chunk("Place", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.FixedHeight = 35f;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Ring Number", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Handler", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Dog Name", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Clear/ Faults", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Time", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    int maxRosettes = calcRosettes(dogCounts);
                    int maxTrophies = calcTrophies(dogCounts);
                    float rowHeight = 25f;
                    if (showClass.EntryType == 4)
                    {
                        rowHeight = 65f;
                    }
                    else if (showClass.EntryType == 5)
                    {
                        rowHeight = 120f;
                    }

                    for (int resultRow = 0; resultRow < maxRosettes; resultRow++)
                    {
                        int bwb = 0;
                        if (resultRow == maxRosettes - 1)
                        {
                            bwb = 1;
                        }
                        Color background = Color.WHITE;
                        if (maxTrophies > 0)
                        {
                            background = Color.LIGHT_GRAY;
                        }
                        maxTrophies--;

                        cell = new PdfPCell(new Phrase(new Chunk(calcPlace(resultRow + 1), normalFont)));
                        cell.BorderWidth = 1;
                        cell.BorderWidthBottom = bwb;
                        cell.FixedHeight = rowHeight;
                        cell.HorizontalAlignment = Element.ALIGN_CENTER;
                        cell.BackgroundColor = background;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk("", normalFont)));
                        cell.BorderWidth = 1;
                        cell.BackgroundColor = background;
                        cell.BorderWidthBottom = bwb;

                        callingListTbl.AddCell(cell);
                        callingListTbl.AddCell(cell);
                        callingListTbl.AddCell(cell);
                        callingListTbl.AddCell(cell);
                        callingListTbl.AddCell(cell);
                    }
                    doc.Add(callingListTbl);
                    doc.NewPage();
                    gradeidx++;
                }
            }

            doc.Close();

            if (publish)
            {

            }
            else
            {
                context.Response.ClearContent();
                context.Response.ContentType = "application/pdf";
                context.Response.AddHeader("content-disposition", String.Format("inline;filename=PreviewSchedule.pdf"));
                context.Response.BinaryWrite((output as MemoryStream).ToArray());
            }
        }
        public void LoadFromRow(DataRow row)
        {
            try
            {
                _id = Convert.ToInt32(row["JudgeID"]);
                _UserID = Convert.ToInt32(row["UserID"]);
                _Notes = row["Notes"].ToString();
                if (row.Table.Columns.Contains("Status") )  _Status = (JudgeStatus) Convert.ToInt32(row["Status"]);
                if (row.Table.Columns.Contains("Allocated")) _Allocated = Convert.ToInt32(row["Allocated"]);
                if (row.Table.Columns.Contains("RingID")) _ringID = Convert.ToInt32(row["RingID"]);
                if (row.Table.Columns.Contains("ShowDate") && row["ShowDate"] != DBNull.Value)
                {
                    _showDate = Convert.ToDateTime(row["ShowDate"]);
                    _showDetailsID = Convert.ToInt32(row["ShowDetailsID"]);
                }

                _user = new User(_UserID);
                _ring = new Rings(_ringID);
            }
            catch (Exception e )
            {
                AppException.LogEvent("Judge.LoadFromRow:" + e.Message);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                String cmd = Request["cmd"];
                int showid = Convert.ToInt32(Request["showid"]);
                int showDetailsID = -1;
                if (!String.IsNullOrEmpty(Request["ShowDetailsID"])) showDetailsID = Convert.ToInt32(Request["ShowDetailsID"]);
                ShowID.Value = showid.ToString();
                ShowDetails showDetails = new ShowDetails();
                showDetails.ShowID = showid;

                DataTable showDays = showDetails.GetDays();
                foreach ( DataRow row in showDays.Rows) {
                    int currentShowDetailsID = Convert.ToInt32(row["ShowDetailID"]);

                    if (showDetailsID == -1) showDetailsID = currentShowDetailsID;

                    DateTime showDate = Convert.ToDateTime(row["ShowDate"]);

                    selShowDays.Items.Add(new ListItem(showDate.ToString("ddd, dd MMM yyyy"), row["ShowDetailID"].ToString() )) ;
                    if (showDetailsID == currentShowDetailsID)
                    {
                        selShowDays.SelectedIndex = selShowDays.Items.Count - 1;
                    }
                }

                ShowDetails currentShowDate = new ShowDetails(showDetailsID);
                ShowDetailsID.Value = showDetailsID.ToString();
                String tmp = "";

                DataRowCollection unallocatedClasses = showDetails.getUnallocatedClassFoDay(showDetailsID);
                foreach (DataRow row in unallocatedClasses)
                {
                    tmp += String.Format("<li class='ui-state-default classDetails'><div clsid='{0}' dogsinclass='{1}' judgeid='{7}' class='clsDetails' ><div class='clsNo'>{2}</div><div class='clsName'>{3} {4} {8} {5} {6}</div></div></li>",
                                                row["ClassID"], row["DogsInClass"], row["ClsNo"], ShowClasses.expandHeightShort(row), ShowClasses.expandCatagoryShort(row), row["ShortName"], row["Name"], row["JudgeID"], ShowClasses.shortenGrades(row)
                                                );
                }
                classesAvailable.InnerHtml = tmp;

                Rings rings = new Rings();
                DataSet ringsTable = rings.GetRingsForShowDay(showDetailsID);
                //DataSet judgesTable = rings.GetJudgesRingsForShow(showid);
                //DataSet helpersTable = rings.GetHelpersRingsForShow(showid);

                String ringTemplate = "<div class='ring ui-droppable ui-sortable' ringid='{0}' ringno='{1}' dogsinclass='0' judges='' helpers=''><h1 class='ui-widget-header'>Ring {1} (<span class='dogtotal'>0</span>)<span class='delthing'>X</span></h1>";
                String ringJudgeClassTemplate = "<div class='judgeAndClasses'><h2 class='judgeInfo'><span class='judgeName'>{0}</span></h2><div class='classListArea'><ul class='ui-sortable fpp_sort'>{1}</ul><ul class='ui-sortable helperlist' style='display:none'>{2}</ul></div></div>";

                int lastRingNo = 0;
                foreach (DataRow ringTableRow in ringsTable.Tables[0].Rows)
                {
                    Rings ring = new Rings(ringTableRow);
                    if (ring.ID> -1 &&  lastRingNo != ring.RingNo )
                    {
                        lastRingNo = ring.RingNo;

                        String ringTmp = String.Format(ringTemplate, ring.ID, ring.RingNo);
                        DataSet judgesDS = rings.GetJudgesForRing(ring.ID);
                        if (judgesDS.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow judgeRow in judgesDS.Tables[0].Rows)
                            {
                                Judge judge = new Judge(judgeRow);
                                ringTmp += CreateRing(currentShowDate, ring, judge, ringJudgeClassTemplate, lastRingNo);
                            }
                        }
                        else
                        {
                            ringTmp += CreateRing(currentShowDate, ring, new Judge(), "", lastRingNo);
                        }

                        ringTmp += "</div>";
                        ringContainer.InnerHtml += ringTmp;
                    }

                }

                tmp = "";
                DataSet ds = Judge.GetAllFreeJudgesForShow(showDetailsID);
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    tmp += String.Format("<li class='ui-state-default judgeDetails'><div judgeid='{0}'  class='judgeDetails'>{1}</div></li>",
                                                row["JudgeID"], row["Name"]
                                                );
                }
                judgesAvailable.InnerHtml = tmp;

                tmp = "";
                Helpers helpers = new Helpers();
                ds = helpers.GetAllFreeHelpersForShow(showid, currentShowDate.ShowDate);
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        Helpers h = new Helpers(row);
                        String job = expandJob(h.Job, h.JobDetails);
                        tmp += String.Format("<li class='ui-state-default'><div helperid='{0}' judgeid='{2}'  class='helperDetails'>{1} ({3})</div></li>",
                                                    h.ID, h.Name, row["judgeid"], job
                                                    );
                    }
                }
                helpersAvailable.InnerHtml = tmp;

            }
        }
        private String CreateRing(ShowDetails currentShowDate, Rings ring, Judge judge, String ringTemplate, int lastRingNo)
        {
            DataSet helperDS;
            String judges;
            String classes = "";
            String helperList = "";

            judges = String.Format("<p judgeid='{0}'>{1}</p>", judge.ID.ToString(), judge.Name);
            //helperDS = rings.GetHelpersForJudge(currentShowDate, Convert.ToInt32(judgeRow["JudgeID"]));
            //if (helperDS.Tables.Count > 0)
            //{
            //    foreach (DataRow helperRow in helperDS.Tables[0].Rows)
            //    {
            //        Helpers h = new Helpers(helperRow);
            //        helperList += String.Format("<li class='ui-draggable' ><div helperid='{0}' class='helperDetails'>{1} ({2})</div></li>", h.ID, h.Name, h.JobName);
            //    }
            //}

            helperDS = Helpers.GetHelpersForRing(currentShowDate, ring.ID);
            if (helperDS.Tables.Count > 0)
            {
                foreach (DataRow helperRow in helperDS.Tables[0].Rows)
                {
                    Helpers h = new Helpers(helperRow);
                    String job = expandJob(h.Job, h.JobDetails);
                    helperList += String.Format("<li class='ui-draggable' ><div helperid='{0}' class='helperDetails'>{1} ({2})</div></li>", h.ID, h.Name, job);
                }
            }

            DataSet classesDS = Rings.GetClassesForRing(ring.ID);
            classesDS = judge.getClassesForJudge();
            if (classesDS.Tables.Count > 0)
            {
                foreach (DataRow classRow in classesDS.Tables[0].Rows)
                {
                    classes += String.Format("<li><div clsid='{0}' dogsinclass='{1}' class='clsDetails'><div class='clstotal'>({1})</div><div class='clsNo'>{2}</div><div class='clsName'>{3} {4} {7} {5} {6}</div></div></li>",
                                    classRow["ClassID"], CalcDogsInCalc(classRow), classRow["ClsNo"], ShowClasses.expandHeightShort(classRow), ShowClasses.expandCatagoryShort(classRow), classRow["ShortName"], classRow["Name"], ShowClasses.shortenGrades(classRow)
                                );
                }
                if (classes.Length == 0)
                {
                    classes = "<li class='blank'></li>";
                }
            }

            return String.Format(ringTemplate, judges, classes, helperList);
        }
        public void ProcessRequest(HttpContext context)
        {
            int ShowID = Convert.ToInt32(context.Request["showid"]);
            int RingID = Convert.ToInt32(context.Request["ringid"]);
            String cmd = context.Request["doc"].ToString();
            Shows show = new Shows(ShowID);

            ShowDetails sd = new ShowDetails();
            DataTable table = sd.GetShowDetails(ShowID).Tables[0];
            String pdfPath = context.Server.MapPath(cmd + ".pdf");
            Boolean publish = false;
            if (!String.IsNullOrEmpty(context.Request["publish"]) && context.Request["publish"].ToString() == "1") publish = true;

            Document doc = new Document(PageSize.A4, -50, -50, 2, 2);
            Stream output;
            if (publish)
            {
                String path = context.Server.MapPath(@"..\schedules\");
                path += DateTime.Today.ToString("yyyy");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += String.Format("\\{0:yyyyMM}_{1}.pdf", show.ShowDate, show.ShowName);
                output = new FileStream(path, FileMode.Create);
            }
            else
            {
                output = new MemoryStream();
            }
            var writer = PdfWriter.GetInstance(doc, output);
            StyleSheet sheet = new StyleSheet();

            doc.Open();
            Font font = new Font(Font.COURIER, 10, Font.NORMAL, Color.BLACK);
            Font resFont = new Font(Font.COURIER, 9, Font.ITALIC, Color.BLACK);
            Font headerFont = new Font(Font.COURIER, 10, Font.BOLD, Color.BLACK);
            Font headerFont2 = new Font(Font.HELVETICA, 12, Font.BOLD, Color.BLACK);
            Font ClassTitleFont = new Font(Font.HELVETICA, 16, Font.NORMAL, Color.BLACK);
            ShowClasses sc = new ShowClasses();
            Rings currentRing = new Rings(RingID);
            List<ShowClasses> showClasses = ShowClasses.GetAllClassesForShowRing(ShowID, RingID);
            foreach (ShowClasses showClass in showClasses)
            {
                PdfPTable ptable = new PdfPTable(3);
                PdfPCell cell;

                String grades = showClass.Grades;
                if (grades.Length == 1)
                {
                    grades = "Grade " + grades;
                }
                else
                {
                    grades = "Grades " + grades[0] + "-" + grades[grades.Length - 1];
                }
                String tmp = String.Format("Ring No:{5}   Class No:{0} {1} {2} {3} ({4})", showClass.ClassNo, showClass.longHeight, showClass.longCatagory, showClass.LongClassName, grades, currentRing.RingNo);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, ClassTitleFont)));
                cell.Colspan = 3;
                cell.BorderWidth = 0;
                ptable.AddCell(cell);
                cell = new PdfPCell(new Phrase(new Chunk("", ClassTitleFont)));
                cell.Colspan = 3;
                cell.BorderWidth = 0;
                ptable.AddCell(cell);
                ptable.AddCell(cell);
                ptable.AddCell(cell);
                doc.Add(ptable);

                DataSet callingList = sc.getCallingList(ShowID, showClass.ID);
                float[] colWidths = { 35, 50, 200, 200 };
                PdfPTable callingListTbl = new PdfPTable(colWidths);

                cell = new PdfPCell(new Phrase(new Chunk("RO", headerFont)));
                cell.BorderWidth = 0;
                callingListTbl.AddCell(cell);

                cell = new PdfPCell(new Phrase(new Chunk("Ring No", headerFont)));
                cell.BorderWidth = 0;
                callingListTbl.AddCell(cell);

                cell = new PdfPCell(new Phrase(new Chunk("Handler", headerFont)));
                cell.BorderWidth = 0;
                callingListTbl.AddCell(cell);

                cell = new PdfPCell(new Phrase(new Chunk("Dog Name", headerFont)));
                cell.BorderWidth = 0;
                callingListTbl.AddCell(cell);
                int altFlag = 0;
                Color altLine = Color.LIGHT_GRAY;
                foreach (DataRow row in callingList.Tables[0].Rows)
                {
                    Color altColor = Color.WHITE;
                    if (altFlag % 2 == 0 && showClass.EntryType != 5)
                    {
                        altColor = altLine;
                    }

                    cell = new PdfPCell(new Phrase(new Chunk(row["RO"].ToString(), font)));
                    cell.BorderWidth = 0;
                    cell.BorderWidth = 2;
                    cell.BorderColor = Color.RED;
                    cell.BackgroundColor = altColor;
                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                    callingListTbl.AddCell(cell);

                    if (showClass.EntryType == 5)
                    {
                        cell = new PdfPCell(new Phrase(new Chunk("Team Name:", headerFont2)));
                        cell.BackgroundColor = altColor;
                        cell.BorderWidth = 1;
                        cell.Colspan = 2;
                        cell.FixedHeight = 30F;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk(ShowClasses.expandHeight(row), headerFont2)));
                        cell.BackgroundColor = altColor;
                        cell.BorderWidth = 1;
                        cell.FixedHeight = 30F;
                        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk("", font)));
                        cell.BackgroundColor = altColor;
                        cell.Colspan = 1;
                        cell.BorderWidth = 0;
                        cell.FixedHeight = 30F;
                        callingListTbl.AddCell(cell);
                    }

                    int borderWidth = 0;
                    if (showClass.EntryType == 5) borderWidth = 1;
                    cell = new PdfPCell(new Phrase(new Chunk(row["RingNumber"].ToString(), font)));
                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                    cell.BackgroundColor = altColor;
                    cell.BorderWidth = borderWidth;
                    callingListTbl.AddCell(cell);

                    int DefaultHandler = Convert.ToInt32(row["DefaultHandler"]);
                    int AltHandler = Convert.ToInt32(row["AltHandler"]);
                    String handlerName = row["Name"].ToString();
                    User user;
                    if (DefaultHandler > 0) {
                        user = new User(DefaultHandler);
                        handlerName = user.Name;
                    }
                    if (AltHandler > 0)
                    {
                        user = new User(AltHandler);
                        handlerName = user.Name;
                    }

                    cell = new PdfPCell(new Phrase(new Chunk(handlerName, font)));
                    cell.BackgroundColor = altColor;
                    cell.BorderWidth = borderWidth;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk(row["KCName"].ToString(), font)));
                    cell.BackgroundColor = altColor;
                    cell.BorderWidth = borderWidth;
                    callingListTbl.AddCell(cell);

                    if (showClass.EntryType == 4 || showClass.EntryType == 5)
                    {
                        int UserID = Convert.ToInt32(row["UserID"]);
                        int DogID = Convert.ToInt32(row["DogID"]);
                        //MultiDog md = new MultiDog(UserID, DogID, showClass.ID);

                        List<MultiDog> otherHandlers = MultiDog.getMultiDog(UserID, DogID, showClass.ID);

                        String reserves = "";
                        int dogCnt = 0;
                        foreach (MultiDog md in otherHandlers)
                        {
                            String res = "";
                            if (dogCnt > 2 && md.Handlername.Length > 0)
                            {
                                if (reserves.Length > 0) reserves += "; ";
                                reserves += String.Format("{0} & {1}", md.Handlername, md.Dogname);
                            }

                            if (dogCnt < 3)
                            {

                                cell = new PdfPCell(new Phrase(new Chunk("", font)));
                                cell.BorderWidth = 0;
                                cell.BackgroundColor = altColor;
                                cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                                callingListTbl.AddCell(cell);

                                cell = new PdfPCell(new Phrase(new Chunk(res, font)));
                                cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                                cell.BackgroundColor = altColor;
                                cell.BorderWidth = 1;
                                callingListTbl.AddCell(cell);

                                cell = new PdfPCell(new Phrase(new Chunk(md.Handlername, font)));
                                cell.BackgroundColor = altColor;
                                cell.BorderWidth = 1;
                                callingListTbl.AddCell(cell);

                                cell = new PdfPCell(new Phrase(new Chunk(md.Dogname, font)));
                                cell.BackgroundColor = altColor;
                                cell.BorderWidth = 1;
                                callingListTbl.AddCell(cell);
                            }
                            dogCnt++;

                        }

                        cell = new PdfPCell(new Phrase(new Chunk("", font)));
                        cell.BackgroundColor = altColor;
                        cell.BorderWidth = 0;
                        cell.FixedHeight = 30F;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk("Reserves: " + reserves, resFont)));
                        cell.BackgroundColor = altColor;
                        cell.Colspan = 3;
                        cell.BorderWidth = 1;
                        cell.FixedHeight = 30F;
                        callingListTbl.AddCell(cell);
                    }
                    altFlag++;

                }
                doc.Add(callingListTbl);
                doc.NewPage();
            }

            doc.Close();

            if (publish)
            {

            }
            else
            {
                context.Response.ClearContent();
                context.Response.ContentType = "application/pdf";
                context.Response.AddHeader("content-disposition", String.Format("inline;filename=PreviewSchedule.pdf"));
                context.Response.BinaryWrite((output as MemoryStream).ToArray());
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
            FormsAuthenticationTicket ticket = identity.Ticket;
            // get the roles stored as UserData into the ticket
            String[] userData = ticket.UserData.Split(':');
            String[] roles = userData[0].Split(',');
            int id = Convert.ToInt32(userData[1]);
            String html = "";

            Fpp.WebModules.Business.User currentUser = new User(id);
            int ShowID = Convert.ToInt32(Request["showid"]);
            Shows show = new Shows(ShowID);

            List<ShowDetails> showDetailsList = ShowDetails.GetShowDaysList(ShowID);
            html += "<h2>" + show.ShowName + "</h2>";

            Rings r = new Rings();
            DataSet ringList = r.GetAllRingsForShow(ShowID, "ShowDate");

            Dogs d = new Dogs();

            DogClasses dc = new DogClasses();
            DateTime dt = DateTime.Now;
            Boolean newday = false;
            int currentRingID = 0;
            foreach (DataRow ringRow in ringList.Tables[0].Rows)
            {
                int RingID = Convert.ToInt32(ringRow["RingID"]);
                if (ringRow.IsNull("ClassID") )
                {
                    continue;
                }
                int ClassID = Convert.ToInt32(ringRow["ClassID"]);
                DateTime rowDT = Convert.ToDateTime(ringRow["ShowDate"]);
                if (rowDT != dt)
                {
                    newday = true;
                    if (currentRingID != 0)
                    {
                        html += "</table></div>";
                        html += "<div class='pagebreak'></div>";
                    }
                    html += "<div class='clsDate'>" + rowDT.ToString("dddd d MMM") + "</div>";
                    dt = rowDT;
                }

                if (currentRingID != RingID)
                {
                    List<Judge> judgeList = Judge.getJudgesForRingList(RingID);

                    if (currentRingID != 0 && !newday)
                    {
                        html += "</table></div>";
                    }
                    html += "<div class='Ring'>";
                    html += "<table class='roClassesTbl'>";
                    html += "<tr class='ringInfo'>";
                    html += "<td colspan='3'>" +
                                "<div class='roTitle centered'>Ring No " + ringRow["RingNo"].ToString() + "</div>";
                    foreach (Judge judge in judgeList)
                    {
                        html += "<div class='roTitle centered'>" + judge.Name + "</div>";
                    }
                    html += "</td></tr>";
                    currentRingID = RingID;
                }

                html += "<tr>";
                DataSet dogsList = d.GetDogsInClass(currentUser.UserID, ClassID);
                int DogsInClass = d.dogsInClassCount(ClassID);

                String clsName = ShowClasses.expandCatagory(ringRow) + " " + ShowClasses.expandHeight(ringRow) + ringRow["name"].ToString();
                String grades = ShowClasses.shortenGrades(ringRow);
                int ix = clsName.IndexOf("Grd");
                if (ix == -1) ix = clsName.IndexOf("Cmb");
                if (ix > -1)
                {
                    clsName = clsName.Substring(0, ix + 3) + " (" + grades + ") " + clsName.Substring(ix + 4);
                }
                else
                {
                    ix = clsName.IndexOf(" ");
                    clsName = clsName.Substring(0, ix) + " (" + grades + ") " + clsName.Substring(ix + 1);
                }

                if (dogsList.Tables[0].Rows.Count > 0)
                {
                    html += "<td valign='top' class='clsRing InClass'>" + ringRow["ClsNo"] + "</td>";
                    html += "<td  valign='top' class='clsDetails InClass'><div>" + clsName  + "</div>";
                    foreach (DataRow dogRow in dogsList.Tables[0].Rows)
                    {
                        String img = "<img src='data:image/gif;base64," + CreateImage(dogRow["DogColour"].ToString()) + "' />";

                        html += String.Format("<div class='dogInClass'><div style='background:{2};' class='dogIcon'>{3}</div>[{1}] {0}</div>", dogRow["DogName"], dogRow["RO"], dogRow["DogColour"], img);
                    }
                    html += "</td>";
                    html += "<td valign='top' class='clsEntry'><div>(" + DogsInClass+ ")</div></td>";
                }
                else
                {
                    html += "<td valign='top' class='clsRing notInClass'>" + ringRow["ClsNo"] + "</td>";
                    html += "<td valign='top' class='clsDetails notInClass'>" + clsName + "</td>";
                    html += "<td valign='top' class='clsEntry notInClass'><div>(" + DogsInClass + ")</div></td>";
                }

                html += "</tr>";
                newday = false;

            }
            html += "</table>";
            html += "</div>";
            ringPlanContainer.InnerHtml = html;
        }
        private void printRingForUser(UserShows userShow, int UserID, Document doc, ref List<int> defaultUsers, ref int pageCount)
        {
            String html = "";
            float[] ringColumns = new float[] { 300, 300, 300, 300};

            Font pageFont = FontFactory.GetFont("Arial", 22, Font.NORMAL);
            Font headerFont = new Font(Font.HELVETICA, 16, Font.BOLD, Color.BLACK);
            Font judgeFont = FontFactory.GetFont("Arial", 9, Font.BOLD);
            Font notInClassFont = new Font(Font.HELVETICA, 6, Font.NORMAL, Color.BLACK);
            Font inClassFont = new Font(Font.HELVETICA, 9, Font.NORMAL, Color.BLACK);
            Font font = new Font(Font.HELVETICA, 10, Font.NORMAL, Color.BLACK);
            Font font1 = FontFactory.GetFont("Arial", 18, Font.BOLD);
            Font dogNotInClass = new Font(Font.HELVETICA, 6, Font.NORMAL, Color.BLACK);
            Font dogInClass = new Font(Font.HELVETICA, 9, Font.NORMAL, Color.BLACK);
            Font dogDetailsInClass = new Font(Font.HELVETICA, 9, Font.BOLD, Color.BLACK);
            Fpp.WebModules.Business.User currentUser = new User(userShow.Userid);
            Shows show = new Shows(userShow.ShowID);

            List<ShowDetails> showDetailsList = ShowDetails.GetShowDaysList(userShow.ShowID);
            doc.Add(new Paragraph(show.ShowName, pageFont));

            Rings r = new Rings();
            DataSet ringList = r.GetAllRingsForShow(userShow.ShowID, "ShowDate");

            Dogs d = new Dogs();

            DogClasses dc = new DogClasses();
            DateTime dt = DateTime.Now;
            int currentRingID = 0;
            int ShowDetailsID = -1;
            int PrevShowDetailsID = -1;
            PdfPTable rings = new PdfPTable(ringColumns);

            int ringCnt = 0;
            PdfPCell cell = null;
            PdfPTable ringDetails = null;
            PdfPTable classDetailsTable = null;
            List<int> dogsRunningToday = new List<int>();
            foreach (DataRow ringRow in ringList.Tables[0].Rows)
            {
                int RingID = Convert.ToInt32(ringRow["RingID"]);
                ShowDetailsID = Convert.ToInt32(ringRow["ShowDetailsID"]);

                if (ringRow.IsNull("ClassID"))
                {
                    continue;
                }
                int ClassID = Convert.ToInt32(ringRow["ClassID"]);
                DateTime rowDT = Convert.ToDateTime(ringRow["ShowDate"]);
                if (rowDT != dt)
                {
                    if (currentRingID != 0)
                    {
                        if (ringCnt % 4 != 0)
                        {
                            var remind = ringCnt % 4;
                            while (remind-- > 0)
                            {
                                cell = new PdfPCell(new Phrase(new Chunk(" ", pageFont)));
                                cell.BorderWidth = 0;
                                rings.AddCell(cell);
                            }
                        }

                        doc.Add(rings);
                        if (currentUser.UserID == UserID)
                        {
                            doc.Add(getHandlerDetails(userShow, currentUser, PrevShowDetailsID, dogsRunningToday));
                        }
                        else
                        {
                            User defaultHandler = new User(UserID);
                            doc.Add(getHandlerDetails(userShow, defaultHandler, PrevShowDetailsID, dogsRunningToday));
                        }
                        doc.NewPage();
                        if (dogsRunningToday.Count > 0)
                        {
                            pageCount++;
                        }
                    }
                    dogsRunningToday.Clear();
                    doc.Add(new Paragraph(rowDT.ToString("dddd d MMM"), headerFont));
                    doc.Add(new Paragraph("  ", judgeFont));
                    dt = rowDT;
                    rings = new PdfPTable(ringColumns);
                    rings.WidthPercentage = 100;
                    ringCnt = 0;
                }

                if (currentRingID != RingID)
                {
                    ringCnt++;
                    ringDetails = new PdfPTable(1);
                    rings.AddCell(new PdfPCell( ringDetails));

                    List<Judge> judgeList = Judge.getJudgesForRingList(RingID);

                    cell = new PdfPCell(new Phrase(new Chunk("Ring No " + ringRow["RingNo"].ToString(), judgeFont)));
                    cell.BorderWidth = 0;
                    cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                    ringDetails.AddCell(cell);

                    foreach (Judge judge in judgeList)
                    {
                        cell = new PdfPCell(new Phrase(new Chunk(judge.Name, judgeFont)));
                        cell.BorderWidth = 0;
                        cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                        ringDetails.AddCell(cell);
                    }
                    currentRingID = RingID;
                    classDetailsTable = new PdfPTable(new float[] { 50, 225, 100 });
                    classDetailsTable.DefaultCell.BorderWidth = 0;
                    ringDetails.AddCell(classDetailsTable);
                }
                html += "<tr>";
                DataSet dogsList = d.GetDogsInClass(userShow.Userid, ClassID);
                int DogsInClass = d.dogsInClassCount(ClassID);

                String clsName = String.Format("{0} {1} {2} {3}",
                                ShowClasses.expandCatagory(ringRow),
                                ShowClasses.expandHeight(ringRow),
                                ringRow["LongName"],
                                ringRow["name"]);
                String grades = ShowClasses.shortenGrades(ringRow);
                int ix = clsName.IndexOf("Grd");
                if (ix == -1) ix = clsName.IndexOf("Cmb");
                if (ix > -1)
                {
                    clsName = clsName.Substring(0, ix + 3) + " " + grades + " " + clsName.Substring(ix + 4);
                }
                else
                {
                    ix = clsName.IndexOf(" ");
                    clsName = clsName.Substring(0, ix) + " " + grades + " " + clsName.Substring(ix + 1);
                }

                if (dogsList.Tables[0].Rows.Count > 0)
                {
                    Phrase[] tmpCells = new Phrase[3];
                    tmpCells[0] = new Phrase(new Chunk(ringRow["ClsNo"].ToString(), dogInClass));
                    tmpCells[1] = new Phrase(new Chunk(clsName, dogInClass));
                    tmpCells[2] = new Phrase(new Chunk(String.Format("({0})", DogsInClass), dogInClass));

                    int countDogs = 0;
                    int DefaultHandler;
                    Paragraph p = new Paragraph();
                    foreach (DataRow dogRow in dogsList.Tables[0].Rows)
                    {
                        int DogID = Convert.ToInt32(dogRow["DogID"]);
                        DefaultHandler = Convert.ToInt32(dogRow["DefaultHandler"]);
                        if (DefaultHandler == 0) DefaultHandler = -1;
                        if ((DefaultHandler == -1 && currentUser.UserID == UserID) ||
                            (DefaultHandler == UserID)
                            )
                        {
                            if (countDogs == 0)
                            {
                                cell = new PdfPCell(tmpCells[0]);
                                cell.BorderWidth = 0;
                                cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
                                classDetailsTable.AddCell(cell);
                                p.Add(tmpCells[1]);
                                p.Add(Chunk.NEWLINE);
                            }
                            if (!dogsRunningToday.Contains(DogID))
                            {
                                dogsRunningToday.Add(DogID);
                            }
                            String img = "<img src='data:image/gif;base64," + CreateImage(dogRow["DogColour"].ToString()) + "' />";
                            String dogName = dogRow["DogName"].ToString();
                            if (dogName.Length == 0)
                            {
                                dogName = dogRow["KCName"].ToString();
                            }
                            var chunk = new Chunk("   ", dogDetailsInClass);
                            chunk.SetBackground(new Color( System.Drawing.ColorTranslator.FromHtml(dogRow["DogColour"].ToString())  ));
                            p.Add(new Phrase(chunk));
                            p.Add(new Phrase(new Chunk(String.Format(" [{1}] {0}", dogName, dogRow["RO"]), dogDetailsInClass)));
                            p.Add(Chunk.NEWLINE);

                            int AltHandler = Convert.ToInt32(dogRow["AltHandler"]);
                            String HandlerName = "";
                            if (AltHandler > 0)
                            {
                                User u = new User(AltHandler);
                                HandlerName = u.Name;

                                html += "<div class='altHandler'>Handler:" + AltHandler + "</div>";
                                p.Add(Chunk.NEWLINE);
                                p.Add(new Phrase(new Chunk(String.Format("Handler:", AltHandler),dogInClass)));
                            }
                            countDogs++;
                        }
                        else
                        {
                            if (defaultUsers != null && defaultUsers.IndexOf(DefaultHandler) == -1)
                            {
                                defaultUsers.Add(DefaultHandler);
                            }
                        }
                    }
                    if (countDogs == 0)
                    {
                        cell = new PdfPCell(new Phrase(new Chunk(ringRow["ClsNo"].ToString(), dogNotInClass)));
                        cell.BorderWidth = 0;
                        cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
                        classDetailsTable.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk(clsName, dogNotInClass)));
                        cell.BorderWidth = 0;
                        cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
                        cell.NoWrap = true;
                        classDetailsTable.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk(String.Format("({0})", DogsInClass), dogNotInClass)));
                        cell.BorderWidth = 0;
                        cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
                        classDetailsTable.AddCell(cell);
                    }
                    else
                    {

                        cell = new PdfPCell(p);
                        cell.BorderWidth = 0;
                        cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
                        cell.NoWrap = true;
                        classDetailsTable.AddCell(cell);

                        cell = new PdfPCell(tmpCells[2]);
                        cell.BorderWidth = 0;
                        cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
                        classDetailsTable.AddCell(cell);
                    }
                }
                else
                {
                    cell = new PdfPCell(new Phrase(new Chunk(ringRow["ClsNo"].ToString(), dogNotInClass)));
                    cell.BorderWidth = 0;
                    cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
                    classDetailsTable.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk(clsName, dogNotInClass)));
                    cell.BorderWidth = 0;
                    cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
                    classDetailsTable.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk(String.Format("({0})", DogsInClass), dogNotInClass)));
                    cell.BorderWidth = 0;
                    cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
                    classDetailsTable.AddCell(cell);
                }

                PrevShowDetailsID = ShowDetailsID;
            }

            if (ringCnt % 4 != 0)
            {
                var remind = ringCnt % 4;
                while (remind-- > 0)
                {
                    cell = new PdfPCell(new Phrase(new Chunk(" ", pageFont)));
                    cell.BorderWidth = 0;
                    rings.AddCell(cell);

                }
            }

            if (currentUser.UserID == UserID)
            {
                doc.Add(getHandlerDetails(userShow, currentUser, PrevShowDetailsID, dogsRunningToday));
            }
            else
            {
                User defaultHandler = new User(UserID);
                doc.Add(getHandlerDetails(userShow, defaultHandler, PrevShowDetailsID, dogsRunningToday));
            }
            doc.NewPage();
        }
        private String printRingForUser(UserShows userShow, int UserID, ref List<int> defaultUsers)
        {
            String html = "";

            Fpp.WebModules.Business.User currentUser = new User(userShow.Userid);
            Shows show = new Shows(userShow.ShowID);

            List<ShowDetails> showDetailsList = ShowDetails.GetShowDaysList(userShow.ShowID);
            html += "<h2>" + show.ShowName + "</h2>";

            Rings r = new Rings();
            DataSet ringList = r.GetAllRingsForShow(userShow.ShowID, "ShowDate");

            Dogs d = new Dogs();

            DogClasses dc = new DogClasses();
            DateTime dt = DateTime.Now;
            Boolean newday = false;
            int currentRingID = 0;
            int ShowDetailsID = -1;
            int PrevShowDetailsID = -1;
            foreach (DataRow ringRow in ringList.Tables[0].Rows)
            {
                try
                {
                    int RingID = Convert.ToInt32(ringRow["RingID"]);
                    int EntryType = Convert.ToInt32(ringRow["EntryType"]);
                    ShowDetailsID = Convert.ToInt32(ringRow["ShowDetailsID"]);
                    if (ringRow.IsNull("ClassID"))
                    {
                        continue;
                    }
                    int ClassID = Convert.ToInt32(ringRow["ClassID"]);
                    DateTime rowDT = Convert.ToDateTime(ringRow["ShowDate"]);
                    if (rowDT != dt)
                    {
                        newday = true;
                        if (currentRingID != 0)
                        {
                            html += "</table></div></div>";
                            if (currentUser.UserID == UserID)
                            {
                                html += getHandlerDetails(userShow, currentUser, PrevShowDetailsID);
                            }
                            else
                            {
                                User defaultHandler = new User(UserID);
                                html += getHandlerDetails(userShow, defaultHandler, PrevShowDetailsID);
                            }
                            html += "<div class='pagebreak'></div>";
                        }
                        html += "<div class='day group'>";
                        html += "<div class='clsDate'>" + rowDT.ToString("dddd d MMM") + "</div>";
                        dt = rowDT;
                    }

                    if (currentRingID != RingID)
                    {
                        List<Judge> judgeList = Judge.getJudgesForRingList(RingID);

                        if (currentRingID != 0 && !newday)
                        {
                            html += "</table></div>";
                        }
                        html += "<div class='Ring'>";
                        html += "<table class='roClassesTbl'>";
                        html += "<tr class='ringInfo'>";
                        html += "<td colspan='3'>" +
                                    "<div class='roTitle centered'>Ring No " + ringRow["RingNo"].ToString() + "</div>";
                        foreach (Judge judge in judgeList)
                        {
                            html += "<div class='roTitle centered'>" + judge.Name + "</div>";
                        }
                        html += "</td></tr>";
                        currentRingID = RingID;
                    }

                    html += "<tr>";
                    DataSet dogsList = d.GetDogsInClass(userShow.Userid, ClassID);
                    int DogsInClass = d.dogsInClassCount(ClassID);

                    String clsName = String.Format("{0} {1} {2} {3}",
                                    ShowClasses.expandCatagory(ringRow),
                                    ShowClasses.expandHeight(ringRow),
                                    ringRow["LongName"],
                                    ringRow["name"]);
                    String grades = ShowClasses.shortenGrades(ringRow);
                    int ix = clsName.IndexOf("Grd");
                    if (ix == -1) ix = clsName.IndexOf("Cmb");
                    if (ix > -1)
                    {
                        clsName = clsName.Substring(0, ix + 3) + " " + grades + " " + clsName.Substring(ix + 4);
                    }
                    else
                    {
                        ix = clsName.IndexOf(" ");
                        clsName = clsName.Substring(0, ix) + " " + grades + " " + clsName.Substring(ix + 1);
                    }

                    if (dogsList.Tables[0].Rows.Count > 0)
                    {
                        String tmphtml = "<td valign='top' class='clsRing InClass'>" + ringRow["ClsNo"] + "</td>" +
                                            "<td  valign='top' class='clsDetails InClass'><div>" + clsName + "</div>";
                        int countDogs = 0;
                        int DefaultHandler;
                        foreach (DataRow dogRow in dogsList.Tables[0].Rows)
                        {
                            int DogID = Convert.ToInt32(dogRow["DogID"]);
                            DefaultHandler = Convert.ToInt32(dogRow["DefaultHandler"]);
                            if (DefaultHandler == 0) DefaultHandler = -1;
                            if ((DefaultHandler == -1 && currentUser.UserID == UserID) ||
                                (DefaultHandler == UserID)
                                )
                            {
                                html += tmphtml;
                                tmphtml = "";

                                String img = "<img src='data:image/gif;base64," + CreateImage(dogRow["DogColour"].ToString()) + "' />";
                                String dogName = dogRow["DogName"].ToString();
                                if (dogName.Length == 0)
                                {
                                    dogName = dogRow["KCName"].ToString();
                                }
                                html += String.Format("<div class='dogInClass'><div style='background:{2};' class='dogIcon'>{3}</div>[{1}] {0}</div>", dogName, dogRow["RO"], dogRow["DogColour"], img);

                                int AltHandler = Convert.ToInt32(dogRow["AltHandler"]);
                                String HandlerName = "";
                                if (AltHandler > 0)
                                {
                                    User u = new User(AltHandler);
                                    HandlerName = u.Name;

                                    html += "<div class='altHandler'>Handler:" + AltHandler + "</div>";
                                }

                                // Pairs
                                if (EntryType == 4)
                                {

                                    MultiDog md = new MultiDog(UserID, DogID, ClassID);
                                    html += String.Format("<div class='dogInClass'><div style='width:8px;height:10px;' class='dogIcon'></div>[{1}] {0}</div>", md.Dogname, dogRow["RO"]);
                                }

                                countDogs++;
                            }
                            else
                            {
                                if (defaultUsers != null && defaultUsers.IndexOf(DefaultHandler) == -1)
                                {
                                    defaultUsers.Add(DefaultHandler);
                                }
                            }
                        }
                        if (countDogs == 0)
                        {
                            html += "<td valign='top' class='clsRing notInClass'>" + ringRow["ClsNo"] + "</td>";
                            html += "<td valign='top' class='clsDetails notInClass'>" + clsName + "</td>";
                            html += "<td valign='top' class='clsEntry notInClass'><div>(" + DogsInClass + ")</div></td>";
                        }
                        else
                        {
                            html += "</td>";
                            html += "<td valign='top' class='clsEntry'><div>(" + DogsInClass + ")</div></td>";
                        }
                    }
                    else
                    {
                        html += "<td valign='top' class='clsRing notInClass'>" + ringRow["ClsNo"] + "</td>";
                        html += "<td valign='top' class='clsDetails notInClass'>" + clsName + "</td>";
                        html += "<td valign='top' class='clsEntry notInClass'><div>(" + DogsInClass + ")</div></td>";
                    }

                    html += "</tr>";
                    newday = false;
                    PrevShowDetailsID = ShowDetailsID;
                }
                catch (Exception e)
                {
                    AppException.LogEvent("Printing Ring Plan:  " + e.Message);
                }
            }
            html += "</table>";
            html += "</div>";
            html += "</div>";

            if (currentUser.UserID == UserID)
            {
                html += getHandlerDetails(userShow, currentUser, PrevShowDetailsID);
            }
            else
            {
                User defaultHandler = new User(UserID);
                html += getHandlerDetails(userShow, defaultHandler, PrevShowDetailsID);
            }
            html += "<div class='pagebreak'></div>";

            return html;
        }
        public void ProcessRequest(HttpContext context)
        {
            int ShowID = Convert.ToInt32(context.Request["showid"]);
            int RingID = Convert.ToInt32(context.Request["ringid"]);
            String cmd = context.Request["doc"].ToString();
            Shows show = new Shows(ShowID);

            ShowDetails sd = new ShowDetails();
            DataTable table = sd.GetShowDetails(ShowID).Tables[0];
            String pdfPath = context.Server.MapPath(cmd + ".pdf");
            Boolean publish = false;
            if (!String.IsNullOrEmpty(context.Request["publish"]) && context.Request["publish"].ToString() == "1") publish = true;

            Document doc = new Document(PageSize.A6, 35, 0, 20, 2);
            Stream output;
            if (publish)
            {
                String path = context.Server.MapPath(@"..\schedules\");
                path += DateTime.Today.ToString("yyyy");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += String.Format("\\{0:yyyyMM}_{1}.pdf", show.ShowDate, show.ShowName);
                output = new FileStream(path, FileMode.Create);
            }
            else
            {
                output = new MemoryStream();
            }
            var writer = PdfWriter.GetInstance(doc, output);
            StyleSheet sheet = new StyleSheet();

            doc.Open();
            Font font = new Font(Font.HELVETICA, 10, Font.NORMAL, Color.BLACK);
            Font smallFont = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.BLACK);
            Font notSoBigFont = new Font(Font.HELVETICA, 11, Font.BOLD, Color.BLACK);
            Font fontWhite = new Font(Font.HELVETICA, 10, Font.NORMAL, Color.WHITE);
            Font headerFont = new Font(Font.HELVETICA, 12, Font.BOLD, Color.BLACK);
            Font spaceFont = new Font(Font.HELVETICA, 40, Font.BOLD, Color.WHITE);
            Font totalsFont = new Font(Font.HELVETICA, 14, Font.BOLD, Color.BLACK);
            Font bigBoyFont = new Font(Font.HELVETICA, 20, Font.BOLD, Color.BLACK);
            Font bigBoyFont2 = new Font(Font.HELVETICA, 25, Font.BOLD, Color.BLACK);
            PdfPCell blankCell = new PdfPCell(new Phrase(new Chunk("", font)));
            ShowClasses sc = new ShowClasses();
            Rings currentRing = new Rings(RingID);
            List<ShowClasses> showClasses = ShowClasses.GetAllClassesForShowRing(ShowID, RingID);
            String tmp;
            foreach (ShowClasses showClass in showClasses)
            {
                PdfPCell cell;
                DataSet callingList = sc.getCallingList(ShowID, showClass.ID);

                String grades = showClass.Grades;
                if (grades.Length == 1)
                {
                    grades = "Grade " + grades;
                }
                else
                {
                    grades = "Grades " + grades[0] + "-" + grades[grades.Length - 1];
                }
                PdfPTable ptable = new PdfPTable(1);
                tmp = String.Format("Class {0}", showClass.ClassNo);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, bigBoyFont2)));
                cell.BorderWidth = 0;
                cell.Padding = 5;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                cell.VerticalAlignment = Element.ALIGN_CENTER;
                ptable.AddCell(cell);

                tmp = String.Format("{0} {1} {2} {3}", showClass.longHeight, showClass.longCatagory, showClass.LongClassName, showClass.ClassName);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, totalsFont)));
                cell.BorderWidth = 0;
                cell.Padding = 5;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                cell.VerticalAlignment = Element.ALIGN_CENTER;
                ptable.AddCell(cell);

                tmp = String.Format("{0}", grades);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, totalsFont)));
                cell.BorderWidth = 0;
                cell.Padding = 5;
                cell.FixedHeight = 100f;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                cell.VerticalAlignment = Element.ALIGN_CENTER;
                ptable.AddCell(cell);

                tmp = String.Format("Ring {0}", currentRing.RingNo);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, totalsFont)));
                cell.BorderWidth = 0;
                cell.Padding = 5;
                cell.FixedHeight = 30f;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                ptable.AddCell(cell);

                tmp = String.Format("Ring Order = {0}", showClass.Position + 1);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, totalsFont)));
                cell.BorderWidth = 0;
                cell.Padding = 5;
                cell.FixedHeight = 30f;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                ptable.AddCell(cell);

                doc.Add(ptable);
                doc.NewPage();

                foreach (DataRow row in callingList.Tables[0].Rows)
                {
                    int Cat = Convert.ToInt32(row["Catagory"]);
                    if (Cat == 0)
                    {
                        PdfPTable tbl = new PdfPTable(1);
                        int dogGrade = Convert.ToInt32(row["Grade"]);
                        if (dogGrade == 99 ) {
                            tmp = "Veteran";
                        } else {
                            tmp = String.Format("Grade {0}", row["Grade"]);
                        }
                        cell = new PdfPCell(new Phrase(new Chunk(tmp, bigBoyFont2)));
                        cell.BackgroundColor = CMYKColor.LIGHT_GRAY;
                        cell.BorderWidth = 2;
                        cell.Padding = 9;
                        cell.PaddingTop = 4;
                        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                        tbl.AddCell(cell);
                        doc.Add(tbl);

                    }

                    float[] colWidths = { 300, 150 };
                    PdfPTable callingListTbl = new PdfPTable(colWidths);
                    tmp = String.Format("Class {0}: {1} {2} {3} {4}", showClass.ClassNo, showClass.longHeight, showClass.LongClassName, grades, showClass.ClassName);
                    cell = new PdfPCell(new Phrase(new Chunk(tmp, notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorRight = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.Padding = 5;
                    cell.PaddingTop = 1;
                    cell.PaddingLeft = 2;
                    cell.FixedHeight = 50f;
                    callingListTbl.AddCell(cell);

                    tmp = String.Format("{0}", row["RO"]);
                    cell = new PdfPCell(new Phrase(new Chunk(tmp, bigBoyFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorLeft = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    cell.Padding = 5;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Course Time", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.Padding = 5;
                    cell.BorderColorRight = Color.BLACK;
                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.Padding = 5;
                    cell.BorderColorBottom = Color.BLACK;
                    callingListTbl.AddCell(cell);

                    int DefaultHandler = Convert.ToInt32(row["DefaultHandler"]);
                    int AltHandler = Convert.ToInt32(row["AltHandler"]);
                    String handlerName = row["Name"].ToString();
                    User user;
                    if (DefaultHandler > 0)
                    {
                        user = new User(DefaultHandler);
                        handlerName = user.Name;
                    }
                    if (AltHandler > 0)
                    {
                        user = new User(AltHandler);
                        handlerName = user.Name;
                    }

                    if (showClass.EntryType == 5)
                    {
                        cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0}", handlerName), smallFont)));
                        cell.BorderWidth = 0;
                        cell.Colspan = 1;
                        cell.HorizontalAlignment = Element.ALIGN_LEFT;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0,-10}", row["KCName"]), smallFont)));
                        cell.Colspan = 1;
                        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                        cell.BorderWidth = 0;
                    }
                    else
                    {
                        cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0}", handlerName), notSoBigFont)));
                        cell.BorderWidth = 0;
                        cell.Colspan = 2;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0,-10} {1}", row["RingNumber"], row["KCName"]), notSoBigFont)));
                        cell.BorderWidth = 0;
                    }
                    if (showClass.EntryType < 4)
                    {
                        cell.FixedHeight = 170f;
                    }
                    cell.Colspan = 2;
                    callingListTbl.AddCell(cell);

                    if (showClass.EntryType == 4 || showClass.EntryType == 5 )
                    {
                        int UserID = Convert.ToInt32(row["UserID"]);
                        int DogID = Convert.ToInt32(row["DogID"]);
                        List<MultiDog> otherHandlers = MultiDog.getMultiDog(UserID, DogID, showClass.ID);

                        int dogCnt = 0;
                        foreach (MultiDog md in otherHandlers)
                        {

                            if (dogCnt < 3)
                            {
                                //html += String.Format("<div class='dogInClass'><div style='width:8px;height:10px;' class='dogIcon'></div>[{1}] {0}</div>", md.Dogname, dogRow["RO"]);
                                if (showClass.EntryType == 4)
                                {
                                    cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0}", md.Handlername), notSoBigFont)));
                                    cell.BorderWidth = 0;
                                    cell.Colspan = 2;
                                    callingListTbl.AddCell(cell);
                                    cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0,-10} {1}", "", md.Dogname), notSoBigFont)));
                                    cell.BorderWidth = 0;
                                    cell.FixedHeight = 160f;
                                    cell.Colspan = 2;
                                }
                                else
                                {
                                    cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0}", md.Handlername), smallFont)));
                                    cell.BorderWidth = 0;
                                    cell.Colspan = 1;
                                    cell.HorizontalAlignment = Element.ALIGN_LEFT;
                                    callingListTbl.AddCell(cell);
                                    cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0,-10} {1}", "", md.Dogname), smallFont)));
                                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                                    cell.BorderWidth = 0;
                                    cell.Colspan = 1;

                                }
                                callingListTbl.AddCell(cell);
                            }
                            dogCnt++;
                        }

                        if (showClass.EntryType == 5)
                        {
                            cell = new PdfPCell(new Phrase(new Chunk("", smallFont)));
                            cell.BorderWidth = 0;
                            cell.FixedHeight = 140f;
                            cell.Colspan = 2;
                            callingListTbl.AddCell(cell);

                        }
                    }

                    cell = new PdfPCell(new Phrase(new Chunk("Time", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorRight = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.Padding = 5;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);
                    callingListTbl.AddCell(blankCell);

                    cell = new PdfPCell(new Phrase(new Chunk("Course Faults", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorRight = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.Padding = 5;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);
                    callingListTbl.AddCell(blankCell);

                    cell = new PdfPCell(new Phrase(new Chunk("Time Faults", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorRight = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.Padding = 5;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);
                    callingListTbl.AddCell(blankCell);

                    cell = new PdfPCell(new Phrase(new Chunk("Totals Faults", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorRight = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.Padding = 5;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);
                    callingListTbl.AddCell(blankCell);
                    doc.Add(callingListTbl);
                    doc.NewPage();

                }
            }

            doc.Close();

            if (publish)
            {

            }
            else
            {
                context.Response.ClearContent();
                context.Response.ContentType = "application/pdf";
                context.Response.AddHeader("content-disposition", String.Format("inline;filename=PreviewSchedule.pdf"));
                context.Response.BinaryWrite((output as MemoryStream).ToArray());
            }
        }