// C2Contacts class
C2Contacts = function(xmlString) {
    this.contactsXML = xmlString;

	try {
      //alert('xml : ' + xmlString);  
      this.contactsDOM = this.parseXML(xmlString);        
    } catch (exception) { 
        alert('C2Contacts: Parser Exception : ' + xmlString);
        return;
    }
	
    this.contactsList = this.contactsDOM.documentElement;
    this.contactNodes = this.contactsList.getElementsByTagName('C2CallContact');
    
    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 email = this.getContactInfo(cc, 'EMail');
		this.contactNodesByEMail[email] = cc;
    }

}

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

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

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

    var serializer = new XMLSerializer();

    return serializer.serializeToString(this.contactsList);
}


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.contactsDOM.createElement('C2CallContact');

    var userid = new Date().getTime().toString(16) + (Math.floor(Math.random()*1000000).toString(16));
    var elem = this.contactsDOM.createElement('Userid');
    elem.appendChild(this.contactsDOM.createTextNode(userid));
    cc.appendChild(elem);
    
    this.contactsList.appendChild(cc);
    this.contactNodes = this.contactsList.getElementsByTagName('C2CallContact');
    return cc;
}

C2Contacts.prototype.deleteContact = function(userid) {
	for (var i = 0; i < this.contactNodes.length; i++) {
		var cc = this.contactNodes.item(i);
		var uid = this.getContactInfo(cc, 'Userid');
		if (uid == userid) {
			this.contactsList.removeChild(cc);
			this.contactNodes = this.contactsList.getElementsByTagName('C2CallContact');
			break;
		}
	}
}

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

C2Contacts.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;
}
 
C2Contacts.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).firstChild.nodeValue;
    }
    
    return phoneList;
}

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

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


