/*
	Connect Daily Web Calendar Software

	Copyright 2007, MH Software, Inc. All Rights Reserved
        5023 W 120th Ave #311, Broomfield CO 80020
	+1 303 438 9585

	Last Edit By: "$Author: gsexton $"
	Last Checkin: "$Date: 2008/09/17 20:24:29 $"
	Revision #  : "$Revision: 1.4 $"
*/

/**
 *  This file contains functions used in rendering RSS items.
 */

/** 
 * Search for URLs embedded in the text, and make them clickable HTML links.
 * 
 * @param sInput The input to search and replace.
 */
function makeURLsClickable(sInput){
    return sInput
    .replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim,
             '<a href="$&" class="AjaxLink" target="_blank'+(new Date().getTime())+'">$&</a>')
    .replace(/([^\/])(www[\S]+(\b|$))/gim,
             '$1<a href="http://$2" class="AjaxLink" target="_blank'+(new Date().getTime())+'">$2</a>')
}

/** 
 * Format the description for an item. Basically, make URLs clickable, and replace 
 * carriage return/line feeds characters with <br> tags.
 * 
 * @param sInput The event Description
 */
function formatDescription(sInput){
    var s=makeURLsClickable(sInput);
    s=s.replace(/(\r\n|[\r\n])/g, "<br>");
    return s;
}

/** 
 * Format a date in RFC822 format to locale date format.
 * If the date is midnight in local time, then only the
 * date portion is returned, otherwise the date and time
 * portions are returned.
 * 
 * @param s The input date.
 */
function formatRFCDate(s){
    var sResult;
    var oDate=new Date(Date.parse(s));
    if (oDate.getHours()==0 && oDate.getMinutes()==0 && oDate.getSeconds()==0) {
        sResult=oDate.toLocaleDateString();
    } else {
        sResult=oDate.toLocaleString();
    }
    return sResult;
}


/** 
 * Render an RSS feed Item. This is the method to change if
 * you want to modify how an item in the RSS feed is displayed.
 * 
 * @param oItem The XML node for the item.
 */
function renderItem(oItem){
    var s="<H3 Class=AjaxItem>"
    var sScratch,sLink;
    sScratch=getNodeValue(oItem,"title");
    s=s+sScratch;
    s=s+'</H3><DL Class=AjaxItem>';
    
    sScratch=getNodeValue(oItem,"cdaily:eventStartDate");
    s=s+"<DT>When<DD>"+formatRFCDate(sScratch);
    sScratch=getNodeValue(oItem,"cdaily:addlInfoURL");
    if (sScratch!=null) {
        s=s+'<DT>URL<DD><A CLASS=AjaxLink TARGET=_BLANK HREF="'+sScratch+'">'+sScratch+"</A>"
    }
    sScratch=getNodeValue(oItem,"cdaily:contactName");
    var sContact=sScratch;
    if (sScratch!=null) {
        s=s+"<DT>Contact<DD>"+sScratch;
    }
    sScratch=getNodeValue(oItem,"cdaily:contactInfo");
    if (sScratch!=null) {
	if (sContact==null){
		s=s+"<DT>Contact<DD>";;
	}
        s=s+" "
        if (sScratch.indexOf('@')>0) {
            s=s+'<A HREF="MAILTO:'+sScratch+'">'+sScratch+'</A>';
        } else {
            s=s+sScratch;
        }
    }
    sScratch=getNodeValue(oItem,"description");
    if (sScratch!=null) {
        s=s+"<DT>Description<DD>"+formatDescription(sScratch);
    }
    s=s+"</DL>";
    
    return s;
}


function renderSimpleList(xmlData,iResult){

    if (iResult==200) {
        var aItems=xmlData.getElementsByTagName("item")
        var s="<UL Class=AjaxItemList>";
        for (var i=0; i < aItems.length; i++) {
            var oItem=aItems[i];
            var sLink=getNodeValue(oItem,"link"), sTitle=getNodeValue(oItem,"title");
            s=s+'<LI><A _TARGET=_AJAXITEM Class=AjaxLink HREF="'+sLink+'">'+sTitle+'</A>'
            renderItem(oItem);
        }
        s=s+'</UL>'
        document.getElementById(ContentID).innerHTML=s;
    } else {
        alert(xmlData);
    }
}
