function GetDocuments(templateId)
{
    webOutput.webservices.ibts.GetDocumentsForTemplate(templateId, SucceededCallback);
}

function SucceededCallback(result)
{
    var html = ""
    // *** Use json library so we can fix up MS AJAX dates
    var result = JSON2.parse(result);

    if (result.length)
    {
        var currentMonth = "";
        for(var i=0; i <result.length; i++)
        {
            // the first item is the template itself
            if (i==0)
            {
                html += "<h1>" + result[i].Name + "</h1>";
                html += "<strong>Venue: </strong>";
                html += result[i].Venue;
                html += "<br /><br />";
                html += "<strong>Upcoming Dates:</strong><br />";
            }
            else // the documents for the template
            {
                if (result[i].AssociatedDateTicks > 0)
                {
                    var itemMonth = GetMonthName(result[i].AssociatedDate.getMonth());
                    if (currentMonth != itemMonth)
                    {
                        html += "<br /><i><strong>"+itemMonth+"</strong></i>";
                        currentMonth = itemMonth;
                    }
                    
                    //var dateObject = new Date();
                    //dateObject.setTime(result[i].AssociatedDate)
                    var linkHref = result[i].CurrentApplicationPath + result[i].FullPath;
                    linkHref = linkHref.replace("'",escape("'"));
                    var linkText = result[i].AssociatedDate.getDate() + GetDaySuffix(result[i].AssociatedDate);
  
                    html += "<br />";
                    linkText += " ";
                    linkText += itemMonth;
                    linkText += " ";
                    linkText += result[i].AssociatedDate.getFullYear();

                    html += "<a href='"+linkHref+"'>" +linkText+ "</a>";
                    if (result[i].Session1Start.length > 0)
                    {
                        html += ", " + result[i].Session1Start
                        html += " - " + result[i].Session1End
                    }
                    if (result[i].Session2Start.length > 0)
                    {
                        html += ", " + result[i].Session2Start
                        html += " - " + result[i].Session2End
                    }
                }
            }
        }
    }

    var elem = document.getElementById("ClinicVenues");
    elem.innerHTML = html;
}

function GetMonthName(index)
{
    var month=new Array(12);
    month[0]="January";
    month[1]="February";
    month[2]="March";
    month[3]="April";
    month[4]="May";
    month[5]="June";
    month[6]="July";
    month[7]="August";
    month[8]="September";
    month[9]="October";
    month[10]="November";
    month[11]="December";
    return month[index];
}

function GetDaySuffix(dateObject)
{
    var suffix="th";
    
    var day=dateObject.getDate();
    
    if (day==1){
        suffix="st";
    }
    if (day==2){
        suffix="nd";
    }
    if (day==3){
        suffix="rd";
    }
    if (day==21){
        suffix="st";
    }
    if (day==22){
        suffix="nd";
    }    
    if (day==23){
        suffix="rd";
    }
    if (day==31){
        suffix="st";
    }      
    return suffix;
}       
