// C2Contacts class
C2Friends = function(xmlString) {
    this.contactsXML = xmlString;
    
    try {
      //alert('xml : ' + xmlString);  
      this.contactsDOM = this.parseXML(xmlString);
    } catch (exception) {
        alert('C2Friends Parser Exception : ' + xmlString);
        return;
    }

    this.contactsList = this.contactsDOM.documentElement;
    this.contactNodes = this.contactsList.getElementsByTagName('User');
    this.numContacts = this.contactNodes.length;
    
    this.contactNodesByUserid = new Array();
    this.contactNodesByEMail = 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;

		var confirmed = this.getContactInfo(cc, 'ConfirmedToUser');
		if (confirmed != null && confirmed == 'true') {
			//alert('confirmed = ' + confirmed);
			var email = this.getContactInfo(cc, 'EMail');
			this.contactNodesByEMail[email] = cc;
		}
    }
}

C2Friends.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;
    }
}

C2Friends.prototype.getContactByUserid = function(userid) {
    return this.contactNodesByUserid[userid];
}

C2Friends.prototype.getContactByEMail = function(email) {
    return this.contactNodesByEMail[email];
}


C2Friends.prototype.toXMLString = function() {
    if (this.contactsList.xml)
        return this.contactsList.xml;

    var serializer = new XMLSerializer();

    return serializer.serializeToString(this.contactsList);
}

C2Friends.prototype.getContactsCount = function() {
    return this.numContacts;
}

C2Friends.prototype.getContact = function(idx) {
    return this.contactNodes.item(idx);
}

C2Friends.prototype.newContact = function() {
    var cc = this.contactsDOM.createElement('User');
    this.contactsList.appendChild(cc);
    
    return cc;
}

C2Friends.prototype.getContactInfo = function(contact, elementName) {
    
    var node = contact.getElementsByTagName(elementName);
    if (!node || node.length == 0)
        return '';
    
    if (node.item(0).firstChild == null)
        return '';
    
    return node.item(0).firstChild.nodeValue;
}

C2Friends.prototype.setContactInfo = function(contact, elementName, value) {
    
    var elem = contact.getElementsByTagName(elementName);
    if (!elem || elem.length == 0) {
        elem = this.contactsDOM.createElement(elementName);
        elem.appendChild(this.contactsDOM.createTextNode(value));
        contact.appendChild(elem);
        return;
    }
    
    elem.item(0).firstChild.nodeValue = value;
    return;
}
 
C2Friends.prototype.getContactPhone = function(contact) {
    
    var node = contact.getElementsByTagName('Phone');
    if (!node || node.length == 0)
        return new Array();

    var phoneList = new Array();
    for (var i = 0; i < node.length; i++) {
        phoneList[node.item(i).getAttribute('numberType')] = node.item(i).getfirstChild.nodeValue;
    }
    
    return phoneList;
}

C2Friends.prototype.setContactPhone = function(contact, numberType, value) {
    
    var nl = contact.getElementsByTagName('Phone');
    var elem = null;
    for (var i = 0; i < nl.length; i++) {
        if (nl.item(i).getAttribute('numberType') == numberType)
            elem = nl.item(i);
    }
    
    if (elem == null) {
        elem = this.contactsDOM.createElement('Phone');
        elem.setAttribute('numberType', numberType);
        elem.appendChild(this.contactsDOM.createTextNode(value));
        contact.appendChild(elem);
        return;
    }
    
    elem.firstChild.nodeValue = value;
    return;
}


