
//functions called by the storeXMLVCF() function in functions.js
//these functions only deal with taking the XML data already fetched and storing the events in objects.

function generateEvents(eventMap, XMLDOM){
    //alert('ie7-3'); //test only
    //alert('ie7-4.1'); //test only
    //var testSession2 = XMLDOM.getElementsByTagName('Event')[0].childNodes[1].childNodes[0].nodeValue; // test only
    //sendConsole("VCFuncs1.js:8 " + testSession2); // test only
	
	if(!XMLDOM.getElementsByTagName('Events')[0].childNodes){ //if no events are in the XMLDOM, end the function
		return;
	}
    var events = XMLDOM.getElementsByTagName('Events')[0].childNodes;
    //sendConsole("VCFuncs1.js:10 - " + events.toString());//test only
    //alert('ie7-4.2'); //test only
    
    //events.length; does not work in FF3?!?
    var numEvents = XMLDOM.getElementsByTagName('Events')[0].childNodes.length;
    
    //sendConsole("VCFuncs1.js:13 - " + numEvents);//test only
    //var testvar3 = XMLDOM.firstChild.childNodes[3].getElementsByTagName('EventName'); //test only
    //var testvar2 = events.childNodes[1].getElementsByTagName('EventName')[0].childNodes[0].nodeValue; //test only
    //var testVar = events.getElementsByTagName('EventName');//test only -- Quickly returns all of the eventNames
    
    //alert('ie7-4.3'); //test only
    //alert('test'); //test only
    for (var i = 0; i < numEvents; i++) {
        //catch to increment i if node has a value of '\n' instead of the event name, do it by type
        
        var currentXMLTag = events[i].tagName;
        
        if (currentXMLTag != "Event") { //if the XMLTag/Node is event, it will be processed, if not this will be null, which will then execute continue
            continue;
        }
        
        var name = events[i].getElementsByTagName('EventName')[0].childNodes[0].nodeValue;
        if (eventMap[name]) {//if the eventName exists in the eventMap
            //sendConsole("VCFuncs1.js:31 - " + eventMap[name]);//test only
            if (!XMLDOM.getElementsByTagName('Area')) {//if this is not customXML (doesn't have an <Area> tag)
                var date = events[i].getElementsByTagName('BookDate')[0].childNodes[0].nodeValue; //only in calXML
                if (name == "Advanced Cayaking") {
                    alert('mainXML cayaking duplicate');
                }//test only
            }
            //alert('ie7-5'); //test only
            if (name == "Advanced Cayaking") {
                alert('customXML cayaking duplicate');
            }//test only
            //create new or modify
            var eventArray = eventMap[name];
            var numEventObjects = eventArray.length;
            var foundEvent = null;
            var foundEventIndex = -1;
            for (var j = 0; j < numEventObjects; j++) {
                if (eventArray[j].getDate() == date) {
                    foundEvent = eventArray[j];
                    foundEventIndex = j;
                }
            }
            if (foundEvent) {
                eventArray[foundEventIndex] = modifyEvent(foundEvent, events[i]);
            }
            else {//generate a new object under the event name
                eventArray[eventArray.length] = generateEvent(events[i]);
                //if this is entered AND it's customXML, add have it use modify instead
            }
            //sendConsole("VCFuncs1.js:59 - " + name);
        }
        else {
            //create new
            var eventArray = new Array();
            eventMap[name] = eventArray;
            eventArray[0] = generateEvent(events[i]);
            //inspectObject("VCFuncs1.js:62", eventMap);//test only
            //sendConsole("VCFuncs1.js:63 - Else block entered in IE7");//test only
        }
        
    }
    session.addAttribute('eventMap', eventMap);//add EventMap to the global session object
    //alert('ie7-4'); //test only
}


function generateEvent(anEventNode){
    var anEvent = new Event();
    return modifyEvent(anEvent, anEventNode);
    
}


function modifyEvent(anEvent, anEventNode){
    var attributes = anEventNode.childNodes;
    var numAttributes = attributes.length;
    var XMLToSetterMap = session.getAttribute('XMLToEvtSetterMap');
    
    for (var i = 0; i < numAttributes; i++) {
        //modify the event object code goes here.
        
        //calling the setters
        //match up the tagname with the hashmap
        var currentXMLTag = attributes[i].tagName;
        var currentSetter = XMLToSetterMap[currentXMLTag];
        
        if (!currentSetter) { //this will be null if the currentTag doesn't have a match in the hashmap
            continue;
        }
        
        //automatically find the setter to use from the hashmap, and call that method
        //anEvent[setterName](attributes[i].childNodes[0].nodeValue)
        //turns into something like: anEvent.setName(newValue)
        //inspectObject("VCFuncs1.js:100 - ", attributes[i].childNodes.length); //test only
        //attributes[i].childNodes[0].nodeValue;//this makes IE7 crash
        
        if (attributes[i].childNodes.length != 0) { //if the attribute has a childNode, go get the value of it. Necessary because IE7 crashed trying to reference a node that wasn't there.
            anEvent[currentSetter](attributes[i].childNodes[0].nodeValue);
            //sendConsole("VCFuncs1.js:98 - " + attributes[i].childNodes[0].nodeValue); //test only
        }
        
    }
    return anEvent;
}

//the Event constructor
function Event(){
    this.name;
    this.bookId;
    this.description;
    this.date;
	this.newDate;
    this.sponsor;
    this.contactName;
    this.startTime;
    this.endTime;
    this.cost;
    this.costDescription;
    this.link;
    this.linkDescription;
    this.imageURL;
    this.roomName;
    this.offLocation;
	this.gearList
	this.participate  // link for service page
	this.map //for service page
	this.application //for service page
	
    this.semester; //pulled from customXML
    //getters
    this.getName = function(){
        return this.name;
    }
    this.getBookId = function(){
        return this.bookId;
    }
    this.getDescription = function(){
        return this.description;
    }
    this.getDate = function(){
        return this.date;
    }
	this.getNewDate = function(){
        return this.newDate;
    }
    this.getSponsor = function(){
        return this.sponsor;
    }
    this.getContactName = function(){
        return this.contactName;
    }
    
    this.getStartTime = function(){
        return this.startTime;
    }
    this.getEndTime = function(){
        return this.endTime;
    }
    this.getTime = function(){
		if(this.endTime != null){
			return this.startTime + " - " + this.endTime;
		}else{
			return this.startTime;
		}
    }
    this.getCost = function(){
        return this.cost;
    }
    this.getCostDescription = function(){
        return this.costDescription;
    }
    this.getLink = function(){
        return this.link;
    }
    
    this.getLinkDescription = function(){
        return this.linkDescription;
    }
    this.getImageURL = function(){
        if (this.imageURL && this.imageURL.length > 48) {
			//window.alert(this.imageURL.length);
            return this.imageURL;
        }
        return "http://activities.byui.edu/images_db/webButtons/" + session.getAttribute('curAreaSettings').area + "_default.png";
        
    }
    this.getRoomName = function(){
        return this.roomName;
    }
    this.getOffLocation = function(){
        return this.offLocation;
    }
    this.getSemester = function(){
        return this.semester;
    }
	this.getGearList = function(){
        return this.gearList;
    }
	this.getParticipate = function(){
        return this.participate;
    }
	this.getMap = function(){
        return this.map;
    }
	this.getApplication = function(){
        return this.application;
    }
	this.getOffLoc = function(){
		return this.offLoc;
	}
    
    //setters
    this.setName = function(newValue){
        this.name = newValue;
    }
    this.setBookId = function(newValue){
        this.bookId = newValue;
    }
    this.setDescription = function(newValue){
        this.description = newValue;
    }
    this.setDate = function(newValue){
        this.date = newValue;
    }
	this.setNewDate = function(newValue){
        this.newDate = newValue;
    }
	this.setNewDate = function(newValue){
        this.Newdate = newValue;
    }
    this.setSponsor = function(newValue){
        this.sponsor = newValue;
    }
    this.setContactName = function(newValue){
        this.contactName = newValue;
    }
    
    this.setStartTime = function(newValue){
        this.startTime = newValue;
    }
    this.setEndTime = function(newValue){
        this.endTime = newValue;
    }
    this.setCost = function(newValue){
        this.cost = newValue;
    }
    this.setCostDescription = function(newValue){
        this.costDescription = newValue;
    }
    this.setLink = function(newValue){
        this.link = newValue;
    }
    
    this.setLinkDescription = function(newValue){
        this.linkDescription = newValue;
    }
    this.setImageURL = function(newValue){
        this.imageURL = newValue;
    }
    this.setRoomName = function(newValue){
        this.roomName = newValue;
    }
    this.setOffLocation = function(newValue){
        this.offLocation = newValue;
    }
    this.setSemester = function(newValue){
        this.semester = newValue;
    }
	this.setGearList = function(newValue){
        this.gearList = newValue;
    }
	this.setParticipate = function(newValue){
        this.participate = newValue;
    }
	this.setMap = function(newValue){
        this.map = newValue;
    }
	this.setApplication = function(newValue){
        this.application = newValue;
    }
	this.setOffLoc = function(newValue){
		this.offLoc = newValue;
	}
}


function createXMLTagMap(){ //creates a hashmap that references the XML Tags with the appropriate Event attrbute setters
    var newMap = new Array;
    
    newMap['EventName'] = 'setName';
    newMap['BookDate'] = 'setDate';
	newMap['NewDate'] = 'setNewDate';
    newMap['BookId'] = 'setBookId';
    newMap['Description'] = 'setDescription';
    newMap['Sponsor'] = 'setSponsor';
    newMap['ContactName'] = 'setContactName';
    newMap['EventStart'] = 'setStartTime';
    newMap['EventEnd'] = 'setEndTime';
	newMap['OffLocation'] = 'setOffLocation';
	newMap['OffLoc'] = 'setOffLoc';
    newMap['RoomName'] = 'setRoomName';
    newMap['Cost'] = 'setCost';
    newMap['CostDesc'] = 'setCostDescription';
    newMap['WebAddress'] = 'setLink';
    newMap['LinkDescription'] = 'setLinkDescription';
    newMap['Image'] = 'setImageURL';
    newMap['Semester'] = 'setSemester';
	newMap['GearList'] = 'setGearList';
	newMap['Participate'] = 'setParticipate';
	newMap['Map'] = 'setMap';
	newMap['Application'] = 'setApplication';
    
    return newMap;
}

//will parse a string to XML. It's necessary for IE7 to fetch the calXMl properly
function CreateXMLStringParser(XMLString){
    try {
        var xmlParser = new DOMParser();
        xmlDoc = xmlParser.parseFromString(XMLString, "text/xml");
    } 
    catch (Err) {
        try {
            //alert("first Error catch");
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(XMLString);
        } 
        catch (Err) {
            window.alert("Browser does not support XML parsing.");
            return false;
        }
    }
    return xmlDoc;
}
