// global flag
var isIE = false;

// global request and XML document objects
var req;

function load_Events() {
    if(document.getElementById("Calendar_Events")){  
        
        thehost = getHost();
       // alert(thehost);
        try {
        //
            
            //alert(thehost + "/events/Calendar_xml.aspx?Organization_ID=" + Organization_ID + "&Days=" + days_into_future + "&Start_Date=" + Start_Date + "&End_Date=" + End_Date); 
            
            loadXMLDoc(thehost + "/events/Calendar_xml.aspx?Organization_ID=" + Organization_ID + "&Days=" + days_into_future + "&Start_Date=" + Start_Date + "&End_Date=" + End_Date);            
            
            //loadXMLDoc("http://www.union.wisc.edu/events/Calendar_xml.aspx?Organization_ID=" + Organization_ID + "&Days=" + days_into_future + "&Start_Date=" + Start_Date + "&End_Date=" + End_Date);
        }	
        catch(e) {
             var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
             alert("Unable to get XML data:\n" + msg);
             return;
        }
    }
}


function getHost() {
var url = window.location.href;
var nohttp = url.split('//')[1];
var hostPort = url.split('//')[0] + '//' +  nohttp.split('/')[0]

return(hostPort);
//alert (hostPort)
}

function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
//alert(url);
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			Populate();
 		} else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText + " " + req.status);
        }
    }
}

var theHTML = ""


function Populate(){

	var event_dates = req.responseXML.getElementsByTagName("event_date");
	var events;
	var the_title;
    // loop through <event_date> elements, and add each nested
    // <name> element to Calendar_Events div
    for (var i = 0; i < event_dates.length; i++) {
        
            theHTML = theHTML + "<div class=\"event_date\">" + event_dates[i].firstChild.nodeValue
            events = event_dates[i].getElementsByTagName("event");
            
            for (var ii = 0; ii < events.length; ii++) {
                
                theHTML = theHTML  + "<div class=\"event\">"
                    
                    theHTML = theHTML  + create_Tag("image",create_image_tag("image_url",getElementTextNS("", "image_url", events[ii], 0)));
                    theHTML = theHTML  + create_Tag("time",getElementTextNS("", "time", events[ii], 0));
                    if(getElementTextNS("", "series", events[ii], 0) != "" && getElementTextNS("", "event_title", events[ii], 0)){
                        the_title = getElementTextNS("", "series", events[ii], 0) + " - " + getElementTextNS("", "event_title", events[ii], 0);
                    }
                    else{
                        the_title = getElementTextNS("", "series", events[ii], 0) + getElementTextNS("", "event_title", events[ii], 0);
                    }
                    theHTML = theHTML  + create_Tag("event_title",the_title);
                    theHTML = theHTML  + create_Tag("description",getElementTextNS("", "description", events[ii], 0));
                    theHTML = theHTML  + create_Tag("short_description",getElementTextNS("", "short_description", events[ii], 0));
                    theHTML = theHTML  + create_Tag("location",getElementTextNS("", "location", events[ii], 0));
                    theHTML = theHTML  + create_Tag("cost",getElementTextNS("", "cost", events[ii], 0));
                    theHTML = theHTML  + create_Tag("url",create_url_tag("a_url","http://",getElementTextNS("", "url", events[ii], 0),getElementTextNS("", "url", events[ii], 0)));
                    theHTML = theHTML  + create_Tag("contact_name",getElementTextNS("", "contact_name", events[ii], 0));
                    theHTML = theHTML  + create_Tag("contact_email",create_url_tag("a_mail","mailto:",getElementTextNS("", "contact_email", events[ii], 0),getElementTextNS("", "contact_email", events[ii], 0)));
                    theHTML = theHTML  + create_Tag("contact_phone",getElementTextNS("", "contact_phone", events[ii], 0));

		//theHTML = theHTML  + create_Tag("url",create_url_tag("a_url","","Email Friend", getElementTextNS("", "EMAIL_FRIEND_URL", events[ii], 0)));


		ARTIST_INFO_URL = getElementTextNS("", "ARTIST_INFO_URL", events[ii], 0);
		
		if(ARTIST_INFO_URL != '') {
			theHTML = theHTML  + create_Tag("url",create_url_tag_open("a_url","","Artist Info", getElementTextNS("", "ARTIST_INFO_URL", events[ii], 0)));
		
		}
		ARTIST_VIEW_URL	  = getElementTextNS("", "ARTIST_VIEW_URL", events[ii], 0);	

		if(ARTIST_VIEW_URL != '') {
			theHTML = theHTML  + create_Tag("url",create_url_tag_open("a_url","","View Artist", getElementTextNS("", "ARTIST_VIEW_URL", events[ii], 0)));
		
		}

		ARTIST_LISTEN_URL	  = getElementTextNS("", "ARTIST_LISTEN_URL", events[ii], 0);	

		if(ARTIST_LISTEN_URL != '') {
			theHTML = theHTML  + create_Tag("url",create_url_tag_open("a_url","","Listen to Artist", getElementTextNS("", "ARTIST_LISTEN_URL", events[ii], 0)));
		
		}
		
		BUY_TICKETS_URL	  = getElementTextNS("", "BUY_TICKETS_URL", events[ii], 0);	

		if(BUY_TICKETS_URL != '') {
			theHTML = theHTML  + create_Tag("url",create_url_tag_open("a_url","","Listen to Artist", getElementTextNS("", "BUY_TICKETS_URL", events[ii], 0)));
		
		}
		
		
		
                theHTML = theHTML  + "</div>"
            }
            theHTML = theHTML + "</div>"
    }
   
    document.getElementById("Calendar_Events").innerHTML = theHTML;
   
}

function create_Tag(theClass,theContent){
    if(theContent == ""){
        return "";
    }
    else{
        
        return "<div class=\""+ theClass +"\">" + theContent + "</div>";   
    }
}


function create_url_tag_open(theClass,theType,theText,theURL){
    if(theText == "" || theURL == ""){
        return "";
    }
    else{
        return "<a target=_blank class=\""+ theClass +"\" href=\""+ theType + theURL +"\">" + theText + "</a>";   
    }
}

function create_url_tag(theClass,theType,theText,theURL){
    if(theText == "" || theURL == ""){
        return "";
    }
    else{
        return "<a class=\""+ theClass +"\" href=\""+ theType + theURL +"\">" + theText + "</a>";   
    }
}

function create_image_tag(theClass,theURL){
    if(theURL == ""){
        return "";
    }
    else{
        return "<img class=\""+ theClass +"\" src=\"http://www.union.wisc.edu/events/UWU_Calendar_Images/"+ theURL +"\" />";   
    }
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes
        //alert(result.childNodes.length) 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else if(result.childNodes.length == 0){
            return "";
        }else{
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "";
    }
}

load_Events();