protected void Page_Load(object sender, EventArgs e)
        {
            int ShowDetailsID = Convert.ToInt32(Request["showdetailsid"]);
            int ShowDayID = Convert.ToInt32(Request["dayid"]);
            ShowDetails _showDetails = new ShowDetails(ShowDayID);

            DataRowCollection getDayRowData = _showDetails.GetDayData(ShowDayID, true);

            String res = "<table id='classDetails' showDetailsID='" + ShowDetailsID.ToString() + "'>";
            String tmp = "";

            tmp += "<tr>";
            String dateThing = getDateOrdinalSuffix(_showDetails.ShowDate.Day);

            tmp += "<td class='classHeader' colspan='5'>" + _showDetails.ShowDate.ToString("dddd MMM d").ToUpper() + dateThing + " CLASSES</td>";
            tmp += "<td>";

            tmp += "<tr class='tableHeader'>";
            tmp += "<td class='clsno'></td>";
            tmp += "<td class='name'> </td>";
            tmp += "<td class='grades'></td>";
            tmp += "<td class='cat'></td>";
            tmp += "<td class='height'></td>";
            tmp += "</tr>";
            res += tmp;
            foreach (DataRow row in getDayRowData)
            {
                ShowClasses sc = new ShowClasses(row);
                tmp = "<tr id='" + sc.ID + "' class='tableItem' >";

                String grades = "";
                grades = "<table class='gradeTable' style='border:none'><tr>";
                String str = sc.Grades;
                for (int i = 1; i <= 7; i++)
                {
                    grades += "<td grade='" + i.ToString() + "' >";
                    if (str.IndexOf((char)(i + 48)) > -1)
                    {
                        grades += i.ToString();
                    }
                    grades += "</td>";
                }
                grades += "</tr></table>";

                tmp += String.Format("<td class='clsno'>Class {0}</td>", sc.ClassNo);
                tmp += String.Format("<td class='name'><div >{0} {1} {2} </div></td>", ShowClasses.expandHeight(row), sc.LongClassName, sc.ClassName);
                tmp += String.Format("<td class='cat'>{0}</td>", ShowClasses.expandCatagory(row) );
                tmp += String.Format("<td class='grades'>{0}</td>", grades);
                tmp += "</tr>";
                res += tmp;
            }

            res += "</table>";
            previewArea.InnerHtml = res;
        }
        public void ProcessRequest(HttpContext context)
        {
            //PDFBuilder.HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.A4);
            //builder.AddPage();
            //builder.ImportStylesheet(context.Server.MapPath("pdfstylesheet.css"));

            int ShowID = Convert.ToInt32(context.Request["showid"]);

            Shows show = new Shows(ShowID);

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

            String logoImg = context.Server.MapPath("FirstContactAgility.png") ;
            contents = contents.Replace("[CLUBLOGO]", "src='" + logoImg + "'");
            contents = contents.Replace("[SHOWDATES]", show.ShowDate.ToString("dddd, dd MMM yyyy"));
            contents = contents.Replace("[CLUBNAME]", show.ShowName);
            contents = contents.Replace("[SHOWVENUE]", show.Venue  + ", " + show.VenuePostcode);
            contents = contents.Replace("[OPENTIME]", "8:00am");
            contents = contents.Replace("[BRIEFING]", "8:15am");
            contents = contents.Replace("[JUDGINGSTARTS]", "8:30am");
            contents = contents.Replace("[SdHOWVENUE]", show.Venue + "," + show.VenuePostcode);
            contents = contents.Replace("[CLOSINGDATE]", show.ClosingDate.ToString("dd MMM yyyy"));
            contents = contents.Replace("[CHAIRMANNAME]", show.Chairman.Name);
            contents = contents.Replace("[CHAIRMANADDRESS]", show.Chairman.AddressDetails);
            contents = contents.Replace("[SECRETARYNAME]", show.Secretary.Name);
            contents = contents.Replace("[SECRETARYADDRESS]", show.Secretary.AddressDetails);
            contents = contents.Replace("[TREASURERNAME]", show.Treasurer.Name);
            contents = contents.Replace("[TREASURERADDRESS]", show.Treasurer.AddressDetails);
            contents = contents.Replace("[SHOWSECNAME]", show.ShowSec.Name);
            contents = contents.Replace("[SHOWSECADDRESS]", show.ShowSec.AddressDetails);
            contents = contents.Replace("[SHOWMANAGERNAME]", show.ShowManager.Name);
            contents = contents.Replace("[SHOWMANAGERADDRESS]", show.ShowManager.AddressDetails);
            contents = contents.Replace("[EQUIPMENTNAME]", show.Equipment.Name);
            contents = contents.Replace("[EQUIPMENTADDRESS]", show.Equipment.AddressDetails);
            contents = contents.Replace("[VETNAME]", show.Vet.Name);
            contents = contents.Replace("[VETADDRESS]", show.Vet.AddressDetails);
            contents = contents.Replace("[SHOWENTRIESNAME]", show.ShowEntries.Name);
            contents = contents.Replace("[SHOWENTRIESADDRESS]", show.ShowEntries.AddressDetails);
            contents = contents.Replace("[PAYABLENAME]", show.Payable.Name);
            contents = contents.Replace("[PAYABLEADDRESS]", show.Payable.AddressDetails);

            Document doc = new Document(PageSize.A4, 25, 10, 10, 10);
            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:yyyyMMM}_{1}.pdf", show.ShowDate, show.ShowName);
                output = new FileStream(path, FileMode.Create);

                show.setStatus(Shows.SHOW_STATUS.PUBLISHED);
            } else {
                output = new MemoryStream();
            }
            var writer = PdfWriter.GetInstance(doc, output);
            StyleSheet sheet = new StyleSheet();

            doc.Open();
            var parsedHTML = HTMLWorker.ParseToList(new StringReader(contents), sheet);
            foreach (var htmlElement in parsedHTML)
            {
                doc.Add(htmlElement as IElement);
            }
            doc.NewPage();
            foreach (DataRow row in table.Rows)
            {
                ShowDetails _showDetails = new ShowDetails(row);
                DataTable showDayTable = _showDetails.GetDayData(true);

                String dayHtml = generateClassesForDay(_showDetails.ShowDetailsID, showDayTable);
                doc.NewPage();
                var dayClasses = HTMLWorker.ParseToList(new StringReader(dayHtml), sheet);
                foreach (var htmlElement in dayClasses)
                {
                    doc.Add(htmlElement as IElement);
                }
            };

            doc.NewPage();
            contents = File.ReadAllText(context.Server.MapPath("schedule2RulesNRegs.html"));
            parsedHTML = HTMLWorker.ParseToList(new StringReader(contents), sheet);
            foreach (var htmlElement in parsedHTML)
            {
                doc.Add(htmlElement as IElement);
            }

            #if TEST
            foreach (DataRow row in table.Rows)
            {
                PdfPTable pdfTable = new PdfPTable(5);
                ShowDetails _showDetails = new ShowDetails(row);
                DataTable showDayTable = _showDetails.GetDayData(true);
                String dateThing = getDateOrdinalSuffix(_showDetails.ShowDate.Day);
                String datestr = _showDetails.ShowDate.ToString("dddd MMM d").ToUpper() + dateThing;

                PdfPCell pdfCell = new PdfPCell();
                pdfCell = new PdfPCell(new Phrase(datestr ));
                pdfCell.Colspan = 5;
                pdfTable.AddCell(pdfCell);

                pdfTable.AddCell("ClsNo");
                pdfCell = new PdfPCell(new Phrase("Name"));
                pdfCell.Colspan = 3;
                pdfTable.AddCell(pdfCell);
                pdfTable.AddCell("Grades");

                pdfTable = new PdfPTable(5);
                foreach (DataRow clsRow in showDayTable.Rows)
                {
                    PdfPTable grades = new PdfPTable(7);
                    String str = clsRow["grades"].ToString();
                    for (int i = 1; i <= 7; i++)
                    {
                        if (str.IndexOf((char)(i + 48)) > -1)
                        {
                            grades.AddCell(i.ToString());
                        }
                        else
                        {
                            grades.AddCell(" ");
                        }
                    }
                    pdfTable.AddCell(clsRow["ClsNo"].ToString() );

                    pdfCell = new PdfPCell(new Phrase(clsRow["Name"].ToString()));
                    pdfCell.Colspan = 3;
                    pdfTable.AddCell(pdfCell);
                    pdfTable.AddCell(grades);
                    doc.Add(pdfTable);
                }
                doc.NewPage();
            }
            #endif
            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());
            }
        }