// Author : Raghavendar Thirumala (Thiru) 


//var default_svr = "http://hs-test.ucdmc.ucdavis.edu";
var default_svr = "http://www.ucdmc.ucdavis.edu";


//Method to display the news articles on an index page or on a search results page
// PARAMETERS
//    keyword - If display is for a search results page, this parameter's value is used to hightlight the keywords
//    display_header - determines if the standard header shoould be displayed for the page
//    news_detail_page - if passed, the value is used as the link to the news detail page, else the default "newsdetail.html" page is used
//    advanced_search_page - if passed, the value is used as the link to the Advanced search page, else the default "advanced_news_search.html" page is used
//    search_results_page - if passed, the value is used as the link to the search results page for the simple keyworkd searcg, else the default "searchresults.html" page is used
//    is_index_page - determines if its an index page or not to display the appropriate header
//    category - if passed, this value is used to enable search within the site
//    site - The site name (applicable only if category is passed)

    
function displayArticles(keyword, display_header, news_detail_page, advanced_search_page, search_results_page, is_index_page, category, site, no_of_months) {
     
     var keywords;
     if(keyword){
        if (typeof keywordArray != 'undefined') {
            keywords = getKeywordsString(keywordArray);
        }
     }
        
        
    var articleListHtml = "";
    if(is_index_page){
        articleListHtml = "<h1>News releases</h1><p><strong>Sorry, no articles found. Please <a href='javascript:history.go(-1);'>retry or search for news articles.</a></p>";
        articleListHtml += getSearchForm(advanced_search_page, search_results_page, category, site);
    } else {
        articleListHtml = "<h1>Search Results</h1><p><strong>Sorry, no articles matched your search criteria. Please <a href='javascript:history.go(-1);'>revise or try a new search.</a></p>";    
    }

    var display_date;
    var prev_display_date;
    //var month_counter = 0;
    if (article == null) {
        setTimeout("delayCall",2000);
    }
    
    if (article.length > 0) {
        if(display_header) {
            if(is_index_page){
                articleListHtml = "<h1>News releases</h1><p>Below are the latest news releases from UC Davis Health System's Public Affairs Office . To reach the office, call (916) 734-9040.</p>";
            } else {
                articleListHtml = "<h1>Search Results</h1>";
            }
        } else {
            articleListHtml = "";
        }
        articleListHtml += "<div>";
        articleListHtml += getSearchForm(advanced_search_page, search_results_page, category, site);        

        // Determine the date until which the news releases should be displayed
        var display_until_date = get_display_until_date(no_of_months);

        if(!is_index_page){
            articleListHtml = articleListHtml + '<h2>' + article.length + ' results found based on the search criteria</h2>';
        }
        
        var arrlen = article.length;
        // Loop through the articles and get the article information
        for (var i=0; i<arrlen; i++) {
        	var articleListHtml_temp="";
            //alert(printArticleList(article[i]));
            var publish_date = new Date (article[i].display_published_date);
            var display_date = publish_date.format("MMM yyyy");
            //alert (display_date);


            // Sample HTML
            /*<h2>October 2007</h2>
                    <div class="detail"> <p><span class="pageSubHeading"><span style="LINE-HEIGHT: normal"><a href="/newsroom/releases/archives/other/2007/nosmoking10-2007.html">UC DAVIS HEALTH SYSTEM GOING SMOKE-FREE IN 2008</a></span></span> <span class="main"><br>
            October 8, 2007 &mdash;&nbsp; Joining a growing list of hospitals and health systems around the country, UC Davis' Sacramento campus will become a completely smoke-free environment on July 1, 2008.</span></p>

            <p><span class="pageSubHeading"><span style="LINE-HEIGHT: normal"><a href="/newsroom/releases/archives/other/2007/botox_seminar10-2007.html">BOTOX&reg; AND FILLERS</a></span></span> <span class="main"><br>
            October 8, 2007 &mdash;&nbsp; The UC Davis Division of Plastic Surgery kicks off an informative, aesthetic surgery seminar series on Wednesday, October 24, with an introduction to Botox&reg; and soft tissue fillers.</span></p>



            <p><a title="" href="/newsroom/releases/archives/childrenshospital/2007/nat_child_study10-2007.html">UC DAVIS JOINS NATIONAL CHILDREN'S STUDY</a><br>
            October 4&nbsp;&mdash; UC Davis Health System has been selected as a new site for the National Children's Study, which is assessing the effects of environmental and genetic factors on 100,000 children from before birth to age 21 in the United States. It is the largest study of child and human health ever conducted in the United States.</p></div>
            */

            if ( (!prev_display_date) ||  (display_date != prev_display_date) ){
                articleListHtml_temp += "<\div>";
                //if((month_counter == 3) && (is_index_page) ) {
                //    break;
                //}         
                
                if((publish_date < display_until_date) && (is_index_page)) {
                                    break;
                }         
                
                
                articleListHtml_temp += "<h2>" + display_date +"</h2>";
                articleListHtml_temp += "<div class='detail'>";
                //month_counter++;
            }






            // headline
                articleListHtml_temp += "<p><a href='";
                if(news_detail_page){
                    articleListHtml_temp += news_detail_page;
                } else {
                    articleListHtml_temp += "newsdetail.html";
                }
                articleListHtml_temp += "?key=";
                articleListHtml_temp += article[i].id;
                if(keywords)                
                    articleListHtml_temp += keywords;

                articleListHtml_temp += "\&svr=";
        if(article[i].svr) {
            articleListHtml_temp += article[i].svr;
        }
        else {
            articleListHtml_temp += default_svr;
        }

        if(article[i].table) {
            articleListHtml_temp += "\&table=";
            articleListHtml_temp += article[i].table;
        }


                //articleListHtml_temp += article[i].svr;
                //articleListHtml_temp += "\&siteroot=";
                //articleListHtml_temp += getBranch();
                articleListHtml_temp += "'";
                articleListHtml_temp += "' style='text-transform:uppercase; font-weight:bold;'>";
                articleListHtml_temp += article[i].headline_text;
                articleListHtml_temp += "</a>";

                // tagline
                articleListHtml_temp += "<br>";

                //articleListHtml_temp += article[i].tagline; 
                articleListHtml_temp += article[i].display_published_date;
                articleListHtml_temp += " &#0151 ";
                //articleListHtml_temp += article[i].lead;
                var lead = article[i].lead;
                lead = lead.replace("<p>","");
                lead = lead.replace("</p>","");
                articleListHtml_temp += lead;
                articleListHtml_temp += "</p>";

                 prev_display_date = display_date;
                articleListHtml += articleListHtml_temp;
        }
    }

    //Replace the HTML of articleList element with the articles information
    document.getElementById("articleList").innerHTML = articleListHtml;
    //If search page, highlight the keywords
    if (typeof keywordArray != 'undefined') {
        if (keywordArray != null) {
            if (keywordArray.length > 0 ) {
                highlightSearchTerms(keywordArray);
            }
        }    
    }
}



// Function that returns the article object information as a string, used for debugging purpose
function printArticleList(item){

    return "URL =  " + item.url + "\nSVR = " + item.svr  + "\nID = " + item.id + "\nCategory = " + item.category + "\nHeadline text = " + item.headline_text + "\nSub Headline = " + item.subheadline_text + "\nLead = " + item.lead + "\nTrimmed Lead = " + item.trimmed_lead  + "\nStandard Text = " + item.standardText + "\nAddl Contact Info = " +  item.additional_contact_info + "\nAddl Info = " +  item.additional_info + "\nRelease Notes = " + item.release_notes+ "\nDisplay Published date = " + item.display_published_date + "\nSource = " + item.source + "\nMedia Type = " + item.media_type + "\nCluster = " + item.cluster + "\nTag Line = " + item.tagline + "\nDoc URL = " + item.document_url + "\nHarvest Time = " + item.harvest_time + "\nAccess Status = " + item.access_status + "\nAccess Registration = " + item.access_registration;

}


// Function that replaces the HTML on Home page with the news retrieved from the database
// This call is made for each news article that has to be displayed on the home page
// PARAMETERS
//    elementid - The HTML Element ID, whose HTML is to be replaced
//    newscounter - News sequence number to identify which news release from the database should be displayed
function displayHomePageNewsArticle(elementid, newscounter) {
    //Sample HTML
    /*<td style="padding-bottom: 10px;">
        <a href="{iw_value name='subn.News-URL'}">
            <img src="{iw_value name='subn.News-Photo'}"alt="{iw_value name='subn.News-Alt-Text'}" align="left" border="0" style="margin-right: 10px">
                <iw_value name="subn.News-Lead"/>
         </a>
      </td>    
    */
    
    //alert ("News counter is " + newscounter + "\nElement id is " + elementid);
    
    if (article[newscounter]) {
        var articleListHtml = "";
        articleListHtml += "<a href='";
        articleListHtml += "/newsroom/newsdetail.html?key=";
        articleListHtml += article[newscounter].id;
        articleListHtml += "\&svr=";
        if(article[newscounter].svr) {
            articleListHtml += article[newscounter].svr;
        }
        else {
            articleListHtml += default_svr;
        }
        
    if(article[newscounter].table) {
        articleListHtml += "\&table=";
        articleListHtml += article[newscounter].table;
    }
        

        //articleListHtml += "\&siteroot=newsroom";
        articleListHtml += "'>";

        if(article[newscounter].image_url2) {
            articleListHtml += '<img src="' + article[newscounter].image_url2 + '" alt="' + article[newscounter].image2_alt_text + '" align="left" border="0" style="margin-right: 10px" width="67" height="67">';
        }

        articleListHtml += article[newscounter].headline_text;
        articleListHtml += "</a>";
        document.getElementById(elementid).innerHTML = articleListHtml;
    }
}


// Function that processes the request to submit the simple keyword search from an index page or the search results page
// PARAMETERS
//    search_results_page - if passed, the value is used as the link to the search results page for the simple keyworkd searcg, else the default "searchresults.html" page is used
//    site - The site name if the search within a site is enabled.

function processInlineQuery(search_results_page, site) {
    //alert("Entering processInlineQuery");
    var keyword = document.getElementById("keyword").value;
    var url = "searchresults.html";
    var NewsSearchForm = document.getElementById("NewsSearchForm");
    //alert(search_results_page);
    //alert((search_results_page !='null' && search_results_page != ''));
    if(search_results_page && (search_results_page !='null' && search_results_page != '')){
        url = search_results_page;
    } 
    url += "?mode=search";
    if (keyword > '') {
        keyword = encodeURIComponent(keyword);
        url += '&keyword=' + keyword;
    }
    if (site) {
        url += '&site=' + site;
    }

    if(NewsSearchForm.cat) {
        for (counter = 0; counter < NewsSearchForm.cat.length; counter++)
        {
           if (NewsSearchForm.cat[counter].checked)
              url += '&cat=' + NewsSearchForm.cat[counter].value;
        }
    }
    if(NewsSearchForm.gencat) {
        url += '&cat=' + NewsSearchForm.gencat.value;
    }    
    window.location = url;
}


// Function that returns the HTML for creating the search form on the index page or on the search results page
// PARAMETERS
//    advanced_search_page - if passed, the value is used as the link to the Advanced search page, else the default "advanced_news_search.html" page is used
//    search_results_page - if passed, the value is used as the link to the search results page for the simple keyworkd searcg, else the default "searchresults.html" page is used
//    category - if passed, this value is used to enable search within the site
//    site - The site name (applicable only if category is passed)

function getSearchForm(advanced_search_page, search_results_page, category, site) {
    var site1 = escape(site);
    var searchForm = '<form id="NewsSearchForm" name="NewsSearchForm" onsubmit="processInlineQuery(\'' + search_results_page;
    searchForm += '\',\'' + site1 + '\'';
    searchForm += '); return false;" action="" method="post">';
    
    searchForm += '<input id="keyword" name="keyword" size=20 />';
    //if(getURLParam("cat")) {
    //   category =  getURLParam("cat");
    //}
    //if(getURLParam("site",true)) {
    //   site =  getURLParam("site");
    //}
    
    if(category){
        searchForm += '&nbsp;<input type="radio" value="gn" name="cat">All news releases';
        if(site) {
            searchForm += '&nbsp;&nbsp;<input type="radio" checked value="'+ category+'" name="cat">';
            searchForm += site;
            searchForm += ' news releases only';
            searchForm += '<input type="hidden" value="'+ site +'" name="site">';
        } else {
            searchForm += '&nbsp;&nbsp;<input type="radio" checked value="'+ category+'" name="cat">Search site news releases only';
        }
    } else {
        searchForm += '<input type="hidden" value="gn" name="gencat">';
    }
    searchForm += '&nbsp;&nbsp; <input type="button"  onClick="javascript:processInlineQuery(\'' + search_results_page;
    searchForm += '\',\'' + site1 + '\'';
    searchForm += ')" name="search" value="Search" />';
    
    searchForm += '</form>';
    if(advanced_search_page){
        searchForm += '<a href="' + advanced_search_page + '">Advanced Search</a> <br /><br />';
    } else {
        searchForm += '<a href="advanced_news_search.html">Advanced Search</a> <br /><br />';
    }

    return searchForm;
}

// Function to display the articles in the rightnav on any web page
// Parameters
//    newsdetailpage - if passed, the value is used as the link to the news detail page, else the default "/newsroom/newsdetail.html" page is used
//    position - The position on the right nav where the news articles should be displayed
//    no_of_releases - The number of releases that should be displayed
function displayRightNavArticles(newsdetailpage, position, no_of_releases) {
    //Sample HTML
    /*
        <li> News Headlines 1 </li>
        <li> News Headlines 2 </li>
        <li> News Headlines 3 </li>
    */
    
    var articleListHtml = "";
    if (article.length > 0) {
        for (var i=0; (i<article.length&&i<no_of_releases); i++) {
            articleListHtml += "<li>";
            articleListHtml += "<a href='";
            if(newsdetailpage){
                articleListHtml += newsdetailpage;
                articleListHtml += "?key=";
            } else {
                articleListHtml += "/newsroom/newsdetail.html?key=";
            }
            articleListHtml += article[i].id;
            articleListHtml += "\&svr=";
            if(article[i].svr) {
                articleListHtml += article[i].svr;
            }
            else {
                articleListHtml += default_svr;
            }
        if(article[i].table) {
        articleListHtml += "\&table=";
        articleListHtml += article[i].table;
        }
            
            
            articleListHtml += "'>";
            articleListHtml += article[i].display_published_date;
            articleListHtml += " &mdash; ";
            articleListHtml += article[i].headline_text;
            articleListHtml += "</a>";
            articleListHtml += "</li>";
        }
        var rightnavelement = "rightnavlinkleveltwo" + position;
        document.getElementById(rightnavelement).innerHTML = articleListHtml;
    }
}

// Function to display the news articles on a welcome page
// Parameters
//    newsdetailpage - if passed, the value is used as the link to the news detail page, else the default "/newsroom/newsdetail.html" page is used
//    no_of_releases - The number of releases that should be displayed


function displayWelcomePageArticles (newsdetailpage, no_of_releases) {
    var articleListHtml = "";
    //Sample HTML
    /*
        <p>November 20<br /><a title="UC receives $22 million FCC telehealth grant" 
        href="http://www.ucdmc.ucdavis.edu/newsroom/releases/archives/other/2007/telemed11-2007.html">
        UC receives $22 million FCC telehealth grant
        </a>
        <br /></p>
        <p></p>
        <h1><a href="/newsroom/releases/">More news...</a></h1>
    */
    if (article.length > 0) {
        for (var i=0; (i<article.length&&i<no_of_releases); i++) {
            articleListHtml += '<p>';
            articleListHtml += article[i].display_published_date;
            articleListHtml += '<br />';
            articleListHtml += '<a title="';
            articleListHtml += article[i].headline_text;
            articleListHtml += '" href="';
            if(newsdetailpage){
                articleListHtml += newsdetailpage;
                articleListHtml += '?key=';
            } else {
                articleListHtml += '/newsroom/newsdetail.html?key=';
            }
            articleListHtml += article[i].id;
            articleListHtml += '\&svr=';
            if(article[i].svr) {
                articleListHtml += article[i].svr;
            }
            else {
                articleListHtml += default_svr;
            }
        if(article[i].table) {
        articleListHtml += "\&table=";
        articleListHtml += article[i].table;
        }
            
            
            articleListHtml += '">';
            articleListHtml += article[i].headline_text;
            articleListHtml += '</a>';
            articleListHtml += '<br /></p> <p><br /></p>';
        }
        document.getElementById("articleList").innerHTML = articleListHtml;
    }
}



function displayMedCenterArticles (newsdetailpage, no_of_releases) {
    var articleListHtml = "";
    //Sample HTML
    /*
        <p>November 20<br /><a title="UC receives $22 million FCC telehealth grant" 
        href="http://www.ucdmc.ucdavis.edu/newsroom/releases/archives/other/2007/telemed11-2007.html">
        UC receives $22 million FCC telehealth grant
        </a>
        <br /></p>
        <p></p>
        <h1><a href="/newsroom/releases/">More news...</a></h1>
    */
    if (article.length > 0) {
        for (var i=0; (i<article.length&&i<no_of_releases); i++) {
            articleListHtml += '<p>';
            articleListHtml += '<a title="';
            articleListHtml += article[i].headline_text;
            articleListHtml += '" href="';
            if(newsdetailpage){
                articleListHtml += newsdetailpage;
                articleListHtml += '?key=';
            } else {
                articleListHtml += '/newsroom/newsdetail.html?key=';
            }
            articleListHtml += article[i].id;
            articleListHtml += '\&svr=';
            if(article[i].svr) {
                articleListHtml += article[i].svr;
            }
            else {
                articleListHtml += default_svr;
            }
        if(article[i].table) {
        articleListHtml += "\&table=";
        articleListHtml += article[i].table;
        }
            
            articleListHtml += '">';
            articleListHtml += article[i].display_published_date;
            articleListHtml += " &mdash; ";
            articleListHtml +=  article[i].headline_text;
            articleListHtml += '</a>';
            articleListHtml += '</p>';
        }
        document.getElementById("articleList").innerHTML = articleListHtml;
    }
}



function delayCall(){
    //alert("Delays the call");
}