
/*
    This file contains javascript methods used by Connect Daily for AJAX
    operations.

	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.3 $"
*/

/** 
 * Construct and return an XMLHttpRequestObject. This method handles the
 * difference between IE and other browsers automatically.
 * 
 * @return XMLHttpRequestObject 
 * 
 */
function getReqObj(){
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else {
        try {
            return new ActiveXObject("Microsoft.XMLHttp");
        } catch (finalFailure) {
            return false;
        }
    }
}

/**
 * Get remote text over the internet using AJAX. When the function finishes,
 * the callback function fCallback is invoked with 2 arguments:
 * <BR><BR>
 * <OL>
 * <LI>ResponseText - If the status code is 200, then this is the response 
 * text. If the status code is anything else, then the value is the statusText
 * or result message.
  <LI>Status Code
 * </OL>
 * 
 * @param sURL      The URL Path of the data to retrieve.
 * @param fCallback The callback function to invoke when the data is returned.
 *                  See above for arguments.
 */
function getRemoteText(sURL,fCallback){
    var oRequest=getReqObj();
    var fResult=fCallback;
    var sPath=sURL;
    if (oRequest) {
        try {
            oRequest.open("GET",sPath);
            oRequest.onreadystatechange=function(){
                if (oRequest.readyState==4) {
                    if (oRequest.status==200) {
                        fCallback(oRequest.responseText,oRequest.status);
                    } else {
                        fCallback(oRequest.statusText+" URL: ["+sPath+"]",oRequest.status);
                    }
                    delete oRequest;
                    oRequest=null;
                }
            }
            oRequest.send(null);
        } catch (eOpen) {
            fCallback((eOpen.description ? eOpen.description : eOpen)+" URL: ["+sPath+"]",400);
            return;
        }
    }
}

function getRemoteXML(sURL,fCallback){
    var oRequest=getReqObj();
    var fResult=fCallback;
    var sPath=sURL;
    if (oRequest) {
        try {
            oRequest.open("GET",sPath);
            oRequest.onreadystatechange=function(){
                if (oRequest.readyState==4) {
                    if (oRequest.status==200) {
                        fCallback(oRequest.responseXML,oRequest.status);
                    } else {
                        fCallback(oRequest.statusText+" URL: ["+sPath+"]",oRequest.status);
                    }
                    delete oRequest;
                    oRequest=null;
                }
            }
            oRequest.send(null);
        } catch (eOpen) {
            fCallback((eOpen.description ? eOpen.description : eOpen)+" URL: ["+sPath+"]",400);
            return;
        }
    }

}


function getNode(nodeParent,sName){
    var o=nodeParent.firstChild;
    while (o!=null) {
        if (o.nodeName==sName) {
            return o;
        }
        o=o.nextSibling;
    }
    return  null;
}

function getNodeValue(nodeParent,sName){
    var o=getNode(nodeParent,sName);
    if (o==null) {
        return  null;
    }
    return o.firstChild.nodeValue;
}


function convertDisplayTypeToPath(iDisplay){
    var s;
    switch (iDisplay) {
    case 0:
        s="calendar_id";
        break;
    case 1:
        s="resource_id";
        break;
    case 2:
        s="resource_type_id";
        break;
    case 3:
        s="item_type_id";
        break;
    default:
        s="calendar_id";
        break;
    }
    return s;
}

function setDayDiv(xmlData,iResult){
    if (iResult==200) {
        var channel=xmlData.documentElement.firstChild;
        var aItems=xmlData.getElementsByTagName("item")
        var s="";
        for (var i=0; i < aItems.length; i++) {
            var oItem=aItems[i];
            s=s+renderItem(oItem);
        }
        document.getElementById(ContentID).innerHTML=s;
    } else {
        alert(xmlData);
    }
}

/**
 * Callback function to set some div text.
 * 
 * @param sText
 * @param iResult
 * @example getRemoteText("http://www.mhsoftware.com/license.txt",setMyDivText);
 */
function setCalendarDivText(sText, iResult){
    if (iResult==200) {
        document.getElementById(CalendarID).innerHTML=sText;
    } else {
        alert(sText);
    }
}

/** 
 * Wrap the URL to the calendar with a call to the proxy server.
 * <br><br>
 * Proxying can be necessary because of constraints on class
 * with the XMLHttpRequest object. It can't call cross-domain,
 * or even cross host. So, you have to have a proxy bit on the 
 * source server to fetch the URL and return it.
 * 
 * @param sURL The URL for the proxy to retrieve.
 */
function createProxyURL(sURL){
    return ProxyURL+"?URL="+encodeURIComponent(sURL);
}

/** 
 * Respond to a click on the calendar.
 * 
 * @param iJulianDate
 */
function navigateDay(iJulianDate){
    var sURL=CalendarURL+"rss/"+convertDisplayTypeToPath(DisplayBy)+"/"+
             DisplayID+".xml?start="+iJulianDate+"&dayspan=0";
    if (ProxyURL) {
        sURL=createProxyURL(sURL);
    }

    getRemoteXML(sURL,setDayDiv);
}



function navigateCalendar(iJulianDate) {
    var sURL=CalendarURL+"ViewMini.html?"+convertDisplayTypeToPath(DisplayBy)+"="+DisplayID;
    if (iJulianDate!=0) {
        sURL=sURL+"&start="+iJulianDate;
    }
    if (ProxyURL) {
        sURL=createProxyURL(sURL);
    }

    getRemoteText(sURL,setCalendarDivText);  // Make the call to populate the mini-month calendar element.
}

function getNextDays(iDays) {
    var sURL=CalendarURL+"rss/"+convertDisplayTypeToPath(DisplayBy)+"/"+
             DisplayID+".xml?maxcount=2&dayspan="+iDays;
	if (ProxyURL) {
		sURL=createProxyURL(sURL);
	}
    getRemoteXML(sURL,setDayDiv);  // Make the call to populate the mini-month calendar element.
}

function simpleList(iCount){
    var sURL=CalendarURL+"rss/"+convertDisplayTypeToPath(DisplayBy)+"/"+
             DisplayID+".xml?dayspan=14&maxcount="+iCount;
    if (ProxyURL) {
        sURL=createProxyURL(sURL);
    }
    getRemoteXML(sURL,renderSimpleList);  // Make the call to populate the mini-month calendar element.
}



