// C2Contacts class
XMLObject = function(xmlString) {
    if (!xmlString)
        return;
    
    this.init(xmlString);
}

XMLObject.prototype.initWithElement = function(node) {
    this.xml = '';
    this.rootElement = node;
    this.dom = node.ownerDocument;
}

XMLObject.prototype.init = function(xmlString) {
    //alert('init : ' + xmlString);
    this.xml = xmlString;

    try {
        this.dom = this.parseXML(xmlString);
    } catch (exception) { 
        alert('XMLObject: Parser Exception : ' + xmlString);
        return;
    }

    this.rootElement = this.dom.documentElement;
}

XMLObject.prototype.parseXML = function(text) {
    if (typeof DOMParser != "undefined") {
        // Mozilla, Firefox, and related browsers
        return (new DOMParser()).parseFromString(text, "application/xml");
    }
    else if (typeof ActiveXObject != "undefined") {
        // Internet Explorer.
        var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.loadXML(text);
        return xmlDoc;
    }
    else {
        var url = "data:text/xml;charset=utf-8," + encodeURIComponent(text);
        var request = new XMLHttpRequest();
        request.open("GET", url, false);
        request.send(null);
        return request.responseXML;
    }
}

XMLObject.prototype.toXMLString = function() {
    if (this.rootElement.xml)
        return this.rootElement.xml;

    var serializer = new XMLSerializer();

    return serializer.serializeToString(this.rootElement);
}


XMLObject.prototype.addElement = function(tag) {
    var cc = this.dom.createElement(tag);
    this.rootElement.appendChild(cc);
    return cc;
}

XMLObject.prototype.getElementList = function(elementName) {
    return this.rootElement.getElementsByTagName(elementName);
}

XMLObject.prototype.getElementObjects = function(elementName) {
    var list = this.rootElement.getElementsByTagName(elementName);
    
    var elementArray = new Array();
    for (var i = 0; i < list.length; i++) {
        var obj = new XMLObject();
        obj.initWithElement(list[i].cloneNode(true));
        elementArray.push(obj);
    }
    return elementArray;
}

XMLObject.prototype.getElementValue = function(elementName) {
    
    var node = this.rootElement.getElementsByTagName(elementName);
    if (!node || node.length == 0)
        return '';

    if (node[0].firstChild == null)
        return '';

    return node[0].firstChild.nodeValue;
}

XMLObject.prototype.getAttributeValue = function(attributeName) {
    return this.rootElement.getAttribute(attributeName);
}

XMLObject.prototype.setElementValue = function(elementName, value) {
    
    try {
        var elem = this.rootElement.getElementsByTagName(elementName);
        if (!elem || elem.length == 0) {
            elem = this.dom.createElement(elementName);
            elem.appendChild(this.dom.createTextNode(value));
            this.rootElement.appendChild(elem);
            return;
        }
    
        elem[0].firstChild.nodeValue = value;
        
    } catch (exception) {
        var msg = '';
        for (var i in exception) {
            msg = msg + exception[i] + ' / ';
        }
        alert('Error : setElementValue : ' + elementName + ' / ' + value + ' / ' + msg + ' : ' + exception);
    }

}

XMLObject.prototype.getBaseElementValue = function(baseElement, elementName) {
    try {
        var node = baseElement.getElementsByTagName(elementName);
        if (!node || node.length == 0)
            return '';

        if (node[0].firstChild == null)
            return '';

        return node[0].firstChild.nodeValue;
        
    } catch (exception) { 
        alert('getElementValue : ' + elementName);
        return '';
    }
}

XMLObject.prototype.setBaseElementValue = function(baseElement, elementName, value) {
    
    try {
        var elem = baseElement.getElementsByTagName(elementName);
        if (!elem || elem.length == 0) {
            elem = this.dom.createElement(elementName);
            elem.appendChild(this.dom.createTextNode(value));
            baseElement.appendChild(elem);
            return;
        }
    
        elem[0].firstChild.nodeValue = value;
            
    } catch (exception) { 
        alert('Error : setBaseElementValue : ' + elementName + ' / ' + value + ' / ' + exception);
    }

}

XMLObject.prototype.getPhoneList = function() {
    
    var node = this.rootElement.getElementsByTagName('Phone');
    if (!node || node.length == 0)
        return new Array();

	var phoneList = new Array();

	for (var i = 0; i < node.length; i++) {
		if (node[i].firstChild != null && node[i].firstChild.nodeValue != null) {
			phoneList[node[i].getAttribute('numberType')] = node[i].firstChild.nodeValue;
		}
	}
    
    return phoneList;
}

XMLObject.prototype.setPhoneElement = function(numberType, value) {
    
    var nl = this.rootElement.getElementsByTagName('Phone');
    var elem = '';
    for (var i = 0; i < nl.length; i++) {
        if (nl[i].getAttribute('numberType') == numberType)
            elem = nl[i];
    }
    
    if (elem == '') {
        elem = this.dom.createElement('Phone');
        elem.setAttribute('numberType', numberType);
        elem.appendChild(this.dom.createTextNode(value));
        this.rootElement.appendChild(elem);
        return;
    }
    
    elem.firstChild.nodeValue = value;
    return;
}



// C2Contacts class
//C2Contacts = function(xmlString) {
//    this.init(xmlString);
//
//    this.contactNodes = this.rootElement.getElementsByTagName('C2CallContact');
//    
//    this.contactNodesByUserid = new Array();
//    for (var i = 0; i < this.contactNodes.length; i++) {
//        var cc = this.contactNodes.item(i);
//        
//        var uid = this.getContactInfo(cc, 'Userid');
//        this.contactNodesByUserid[uid] = cc;
//    }
//
//}
//
//C2Contacts.prototype = new XMLObject;
//
//C2Contacts.prototype.getContactByUserid = function(userid) {
//    return this.contactNodesByUserid[userid];
//}
//
//C2Contacts.prototype.toXMLString = function() {
//    return this.dom.toString();
//}
//
//C2Contacts.prototype.getContactsCount = function() {
//    return this.contactNodes.length;
//}
//
//C2Contacts.prototype.getContact = function(idx) {
//    return this.contactNodes.item(idx);
//}
//
//C2Contacts.prototype.newContact = function() {
//    var cc = this.dom.createElement('C2CallContact');
//    this.rootElement.appendChild(cc);
//    this.contactNodes = this.rootElement.getElementsByTagName('C2CallContact');
//    return cc;
//}
//
//C2Contacts.prototype.getContactInfo = function(contact, elementName) {
//    return this.getBaseElementValue(contact, elementName);
//}
//
//C2Contacts.prototype.setContactInfo = function(contact, elementName, value) {
//    this.setBaseElementValue(contact, elementName, value);
//}
//
//
//


