﻿/* Copyright 2006 Microsoft Corporation.  Microsoft's copyrights in this work are licensed under the Creative Commons */
/* Attribution-ShareAlike 2.5 License.  To view a copy of this license visit http://creativecommons.org/licenses/by-sa/2.5 */

function HCal(url, summary, description, dtstart, dtend, datetimelabel, location, uid, method, dtstamp)
{
    this.Url = url;
    this.Summary = summary;
    this.Description = description;
    this.DtStart = dtstart;
    this.DtEnd = dtend;
    this.DateTimeLabel = datetimelabel;
    this.Location = location;
    this.UID = uid;
    this.Method = method;
    this.DtStamp = dtstamp;

    this.formatType = "hCalendar";
    this.formatRootClassName = "vcalendar";
    this.updateCallback;
    this.HTML;
    
    var self = this;
    
    this.clearProps = function()
    {
        self.Url = null;
        self.Summary = null;
        self.Description = null;
        self.DtStart = null;
        self.DtEnd = null;
        self.DateTimeLabel = null;
        self.Location = null;
        self.UID = null;
        self.Method = null;
        self.DtStamp = null;
        
        self.buildHtml();
    }
    
    this.buildHtml = function()
    {
        var hCalString = "<span class=\"vcalendar\"><span class=\"vevent\">";
               
        if (self.Url)
        {
            hCalString += "<a class=\"url\" href=\"" + self.Url + "\">";
            
            if (self.Summary)
                hCalString += "<span class=\"summary\">" + self.Summary + "</span>: ";

            if (self.Description)
                hCalString += "<span class=\"description\" title=\"" + self.Description + "\"></span>";
                
            if (self.DtStart)
                hCalString += "<abbr class=\"dtstart\" title=\"" + self.DtStart + "\">" + self.DateTimeLabel + "</abbr>";
                
            if (self.DtEnd)
                hCalString += "<abbr class=\"dtend\" title=\"" + self.DtEnd + "\"></abbr>";
                
            if (self.Location)
                hCalString += ", at <span class=\"location\">" + self.Location + "</span>";
                
            if (self.UID)
                hCalString += "<span class=\"uid\" title=\"" + self.UID + "\"></span>";
                
            if (self.Method)
                hCalString += "<span class=\"method\" title=\"" + self.Method + "\"></span>";
                
            if (self.DtStamp)
                hCalString += "<span class=\"dtstamp\" title=\"" + self.DtStamp + "\"></span>";                                                                                                                
                            
            hCalString += "</a>";
        }
        
        hCalString +=  "</span></span>";
        self.HTML = hCalString;
    }
    
    // Initialize all contact properties from the hCal XML segment and rebuild hCal HTML.
    this.initFromXmlString = function(hCalXmlString)
    {
        this.clearProps();
        var hCalXmlStringWithHeader = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + hCalXmlString;
        
        // IE 5+
        if (window.ActiveXObject)
        {
            hCalXmlNode = new ActiveXObject("Microsoft.XMLDOM");
            hCalXmlNode.async=false;
            hCalXmlNode.loadXML(hCalXmlStringWithHeader);
    
            var node;
                       
            node = hCalXmlNode.selectSingleNode("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/@href");
            if (node)
                self.Url = node.nodeTypedValue;
            
            node = hCalXmlNode.selectSingleNode("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='summary']");
            if (node)
                self.Summary = node.nodeTypedValue;
            
            node = hCalXmlNode.selectSingleNode("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='description']/@title");
            if (node)
                self.Description = node.nodeTypedValue;
                
            node = hCalXmlNode.selectSingleNode("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/abbr[@class='dtstart']/@title");
            if (node)
                self.DtStart = node.nodeTypedValue;           
                
            node = hCalXmlNode.selectSingleNode("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/abbr[@class='dtend']/@title");
            if (node)
                self.DtEnd = node.nodeTypedValue;
            
            node = hCalXmlNode.selectSingleNode("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/abbr[@class='dtstart']");
            if (node)
                self.DateTimeLabel = node.nodeTypedValue;
            
            node = hCalXmlNode.selectSingleNode("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='location']");
            if (node)
                self.Location = node.nodeTypedValue;
                
            node = hCalXmlNode.selectSingleNode("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='uid']/@title");
            if (node)
                self.UID = node.nodeTypedValue;           
                
            node = hCalXmlNode.selectSingleNode("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='method']/@title");
            if (node)
                self.Method = node.nodeTypedValue;

            node = hCalXmlNode.selectSingleNode("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='dtstamp']/@title");
            if (node)
                self.DtStamp = node.nodeTypedValue;                
        }
        // Mozilla etc.
        else if (typeof DOMParser != "undefined")
        {
            var domParser = new DOMParser();
            var hCalXmlNode = domParser.parseFromString(hCalXmlStringWithHeader, 'application/xml');
            
            if (document.evaluate)
            { 
                var node;
                          
                node = document.evaluate("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/@href", hCalXmlNode, null, 0 /*XPathResult.ANY_TYPE*/, null).iterateNext();
                if (node)
                    self.Url = node.textContent;
                
                node = document.evaluate("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='summary']", hCalXmlNode, null, 0 /*XPathResult.ANY_TYPE*/, null).iterateNext();
                if (node)
                    self.Summary = node.textContent;
                
                node = document.evaluate("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='description']/@title", hCalXmlNode, null, 0 /*XPathResult.ANY_TYPE*/, null).iterateNext();
                if (node)
                    self.Description = node.textContent;
                    
                node = document.evaluate("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/abbr[@class='dtstart']/@title", hCalXmlNode, null, 0 /*XPathResult.ANY_TYPE*/, null).iterateNext();
                if (node)
                    self.DtStart = node.textContent;           
                    
                node = document.evaluate("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/abbr[@class='dtend']/@title", hCalXmlNode, null, 0 /*XPathResult.ANY_TYPE*/, null).iterateNext();
                if (node)
                    self.DtEnd = node.textContent;
                
                node = document.evaluate("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/abbr[@class='dtstart']", hCalXmlNode, null, 0 /*XPathResult.ANY_TYPE*/, null).iterateNext();
                if (node)
                    self.DateTimeLabel = node.textContent;
                
                node = document.evaluate("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='location']", hCalXmlNode, null, 0 /*XPathResult.ANY_TYPE*/, null).iterateNext();
                if (node)
                    self.Location = node.textContent;
                    
                node = document.evaluate("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='uid']/@title", hCalXmlNode, null, 0 /*XPathResult.ANY_TYPE*/, null).iterateNext();
                if (node)
                    self.UID = node.textContent;           
                    
                node = document.evaluate("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='method']/@title", hCalXmlNode, null, 0 /*XPathResult.ANY_TYPE*/, null).iterateNext();
                if (node)
                    self.Method = node.textContent;

                node = document.evaluate("span[@class='vcalendar']/span[@class='vevent']/a[@class='url']/span[@class='dtstamp']/@title", hCalXmlNode, null, 0 /*XPathResult.ANY_TYPE*/, null).iterateNext();
                if (node)
                    self.DtStamp = node.textContent; 
            }
            else
            {
                var vCalendarNode = null;
                var vEventNode = null;
                var urlNode = null;
                                
                for (var i = 0; i < hCalXmlNode.childNodes.length; i++)
                {                    
                    for (var j = 0; j < hCalXmlNode.childNodes[i].attributes.length; j++)
                    {
                        if ((hCalXmlNode.childNodes[i].attributes[j].nodeName == "class") && (hCalXmlNode.childNodes[i].attributes[j].nodeValue == "vcalendar"))
                        {
                            vCalendarNode = hCalXmlNode.childNodes[i];
                            break; 
                        }
                    }
                    
                    if (vCalendarNode)
                    {
                        for (var j = 0; j < vCalendarNode.childNodes.length; j++)
                        {
                            for (var k = 0; k < vCalendarNode.childNodes[j].attributes.length; k++)
                            {
                                if ((vCalendarNode.childNodes[j].attributes[k].nodeName == "class") && (vCalendarNode.childNodes[j].attributes[k].nodeValue == "vevent"))
                                {
                                    vEventNode = vCalendarNode.childNodes[j];
                                    break;
                                }
                            }
                        }
                   }
                   
                   if (vEventNode)
                   {
                        for (var j = 0; j < vEventNode.childNodes.length; j++)
                        {
                            for (var k = 0; k < vEventNode.childNodes[j].attributes.length; k++)
                            {
                                if ((vEventNode.childNodes[j].attributes[k].nodeName == "class") && (vEventNode.childNodes[j].attributes[k].nodeValue == "url"))
                                {
                                    urlNode = vEventNode.childNodes[j];
                                    break;
                                }
                            }
                        }
                   }
                   
                   if (urlNode)
                   {
                        for (var j = 0; j < urlNode.attributes.length; j++)
                        {
                            if (urlNode.attributes[j].nodeName == "href")                                                  
                                self.Url = urlNode.attributes[j].nodeValue;
                        }
                        for (var j = 0; j < urlNode.childNodes.length; j++)
                        {
                            if (urlNode.childNodes[j].attributes)
                            {
                                for (var m = 0; m < urlNode.childNodes[j].attributes.length; m++)
                                {
                                    if ((urlNode.childNodes[j].attributes[m].nodeName == "class") && (urlNode.childNodes[j].attributes[m].nodeValue == "summary"))
                                        self.Summary = urlNode.childNodes[j].childNodes[0].nodeValue;
                                    
                                    else if ((urlNode.childNodes[j].attributes[m].nodeName == "class") && (urlNode.childNodes[j].attributes[m].nodeValue == "description"))
                                    {
                                        for (var n = 0; n < urlNode.childNodes[j].attributes.length; n++)
                                        {
                                            if (urlNode.childNodes[j].attributes[n].nodeName == "title")
                                                self.Description = urlNode.childNodes[j].attributes[n].nodeValue;                                                    
                                        } 
                                    }
                                    else if ((urlNode.childNodes[j].attributes[m].nodeName == "class") && (urlNode.childNodes[j].attributes[m].nodeValue == "dtstart"))
                                    {
                                        for (var n = 0; n < urlNode.childNodes[j].attributes.length; n++)
                                        {
                                            if (urlNode.childNodes[j].attributes[n].nodeName == "title")
                                                self.DtStart = urlNode.childNodes[j].attributes[n].nodeValue;                                                    
                                        } 
                                        
                                        self.DateTimeLabel = urlNode.childNodes[j].childNodes[0].nodeValue;
                                    }
                                    else if ((urlNode.childNodes[j].attributes[m].nodeName == "class") && (urlNode.childNodes[j].attributes[m].nodeValue == "dtend"))
                                    {
                                        for (var n = 0; n < urlNode.childNodes[j].attributes.length; n++)
                                        {
                                            if (urlNode.childNodes[j].attributes[n].nodeName == "title")
                                                self.DtEnd = urlNode.childNodes[j].attributes[n].nodeValue;                                                    
                                        } 
                                    }

                                    else if ((urlNode.childNodes[j].attributes[m].nodeName == "class") && (urlNode.childNodes[j].attributes[m].nodeValue == "location"))
                                        self.Location = urlNode.childNodes[j].childNodes[0].nodeValue;
                                                                                                                    
                                    else if ((urlNode.childNodes[j].attributes[m].nodeName == "class") && (urlNode.childNodes[j].attributes[m].nodeValue == "uid"))
                                    {
                                        for (var n = 0; n < urlNode.childNodes[j].attributes.length; n++)
                                        {
                                            if (urlNode.childNodes[j].attributes[n].nodeName == "title")
                                                self.UID = urlNode.childNodes[j].attributes[n].nodeValue;                                                    
                                        } 
                                    }
                                    else if ((urlNode.childNodes[j].attributes[m].nodeName == "class") && (urlNode.childNodes[j].attributes[m].nodeValue == "method"))
                                    {
                                        for (var n = 0; n < urlNode.childNodes[j].attributes.length; n++)
                                        {
                                            if (urlNode.childNodes[j].attributes[n].nodeName == "title")
                                                self.Method = urlNode.childNodes[j].attributes[n].nodeValue;                                                    
                                        } 
                                    }
                                    else if ((urlNode.childNodes[j].attributes[m].nodeName == "class") && (urlNode.childNodes[j].attributes[m].nodeValue == "dtstamp"))
                                    {
                                        for (var n = 0; n < urlNode.childNodes[j].attributes.length; n++)
                                        {
                                            if (urlNode.childNodes[j].attributes[n].nodeName == "title")
                                                self.DtStamp = urlNode.childNodes[j].attributes[n].nodeValue;                                                    
                                        } 
                                    }                                    
                                }
                            }
                        }
                    }
                }                    
            }
        }
        
        self.buildHtml();
    }

    self.buildHtml();
}