/**********************************************************************************/
/** © 1998 - 2009 Ultratec, Inc.                                                 **/
/**                                                                              **/
/** Disclaimer:                                                                  **/
/**   Ultratec, Inc. is providing this code as an example of how to access their **/
/**   Externally Hosted CapTel Application Programming Interface. All example    **/
/**   source code obtained from Ultratec, Inc is delivered "AS IS." Ultratec,    **/
/**   Inc specifically disclaims any and all warranties, expressed or implied,   **/
/**   including but not limited to any implied warranties of merchantability or  **/
/**   fitness for a particular purpose. Ultratec, Inc. does not represent or     **/
/**   warrant that any program is error free or that its use will be             **/
/**   uninterrupted. Ultratec, Inc. shall not be liable for any loss of profit,  **/
/**   loss of business or goodwill, loss of data, interruption in business, nor  **/
/**   for indirect, special, incidental, or consequential damages of any kind    **/
/**   under or arising out of the use or implementation of the example source    **/
/**   code, however caused, whether for breach or warranty, breach or            **/
/**   repudiation of contract, tort, negligence, or otherwise, even if Ultratec, **/
/**   Inc has been advised of the possibility of such a loss. Also Ultratec,     **/
/**   Inc. shall not have any obligation to provide techincal support or         **/
/**   development assistance for the example source code obtained from Ultratec, **/
/**   Inc.                                                                       **/
/**********************************************************************************/
/**********************************************************************************/
/** UTIResponse Object                                                           **/
/**********************************************************************************/
/**
  This is an object that processes the XML Response from an External Host CapTel webservice.
	Properties:
		action - the command that was processed
		status - the status of the command
		userid - the uniqueid for the username/password/license that was passed into the web service
		user - the name of the user
		firstname - the first name of the user
		lastname - the last name of the user
		address - the street address of the user
		city - the city of the user
		state - the state of the user
		postalcode - the postal code of the user
		email - the email address of the user
		username - the username of the user
		password - the password of the user
		sessionid - the uniqueid for the session that was created for the user from the command
		international - the flag that determines if the user can make international calls
		callid - the uniqueid for the active call
		calltype - the type of call that the user is participating in. it can be "standard" or "emergency"
		ani - the number where the call originated
		dialed - the number that was called
		messagefragmentid - the message fragment id that was retrieved by the web service
		messagefragment - the message fragment that was retrieved by the web service
		message - the status message that was returned by the web service
		errorid - the errorid that was returned by the web service
		errormessage - the error description that was returned by the web service
		
	This is the XML Input that it is expecting:
		<?xml version="1.0" encoding="UTF-8"?>
		<uti action="login" status="success" userid="6230DA85-753D-48FE-81BE-2624A64C935A" sessionid="FF83E74C-60DE-4CC3-945A-5D8806B4C461" international="0" >
			<user>
				<name><![CDATA[Win Thor]]></name>
				<firstname><![CDATA[Win]]></firstname>
				<lastname><![CDATA[Thor]]></lastname>
				<address><![CDATA[450 Science Dr]]></address>
				<city><![CDATA[Madison]]></city>
				<state><![CDATA[WI]]></state>
				<postalcode><![CDATA[53711]]></postalcode>
				<email><![CDATA[someone@ultratec.com]]></email>
				<username><![CDATA[username]]></username>
				<password><![CDATA[supersecretpassword]]></password>
			</user>
			<call id="D16C0A40-9C7A-49B4-B158-8442A717E8AE" type="standard" ani="6084434978" dialed="6086610176" messageid="0"><![CDATA[call fragment goes here...]]></call>
			<message><![CDATA[logged in]]></message>
			<states>
				<state id="WI"><![CDATA[Wisconsin]]>
				<state id="IL"><![CDATA[Illinois]]>
			</states>
			<error id="0"><![CDATA[logged in]]></error>
		</uti>
                                                                                 **/
/**********************************************************************************/
function UTIResponse(oXML){
	this.action = null, this.status = null, this.userid = null, this.sessionid = null;
	this.user = null, this.firstname = null, this.lastname = null, this.address = null, this.city = null, this.state = null, this.postalcode = null, this.email = null, this.username = null, this.password = null;
	this.callid = null, this.calltype = null, this.ani = null, this.dialed = null, this.messagefragment = null;
	this.message = null;
	this.errorid = null, this.errormessage = null;
	this.states = null;
	this.international = false;
	this.contacts = null;
	this.users = null;
	this.phonecategories = null;
	this.syncusers = null;
	
	//Private variable... shorthand for the current element in the XML Dom that is being parsed
	var element = (oXML.getElementsByTagName("uti").length > 0)?oXML.getElementsByTagName("uti")[0]:null;
	if(element!=null){ //This sets the properties for the response
		this.action = getAttribute("action");
		this.status = getAttribute("status");
		this.userid = getAttribute("userid");
		this.sessionid = getAttribute("sessionid");
		this.international = (getAttribute("international")=="1"||getAttribute("international")==1)?true:false;
	}

	// Sync Users
	if (this.action=='users_sync') {
		var element = (oXML.getElementsByTagName("syncusers").length > 0)?oXML.getElementsByTagName("syncusers"):null;
		if (element != null) {
			this.syncusers = new Array();
			var eleTemp = element;
			for (var i=0; i<eleTemp.length; i++) {
				element = eleTemp[i];
				this.syncusers[i] = new Array();
				this.syncusers["added"] = getAttribute("added");
				this.syncusers["deleted"] = getAttribute("deleted");
			}
		}
	}

	// Users
	if (this.action=='users_list' || this.action=='users_delete') {
		var element = (oXML.getElementsByTagName("user").length > 0)?oXML.getElementsByTagName("user"):null;
		if (element != null) {
			this.users = new Array();
			var eleTemp = element;
			for (var i=0; i<eleTemp.length; i++) {
				element = eleTemp[i];
				this.users[i] = new Array();
				this.users[i]["id"] = getAttribute("id").substr(1,36);
				this.users[i]["firstname"] = getChildNodeValue("firstname");
				this.users[i]["lastname"] = getChildNodeValue("lastname");
				this.users[i]["email"] = getChildNodeValue("email");
				this.users[i]["address"] = getChildNodeValue("address");
				this.users[i]["city"] = getChildNodeValue("city");
				this.users[i]["state"] = getChildNodeValue("state");
				this.users[i]["postalcode"] = getChildNodeValue("postalcode");
				this.users[i]["username"] = getChildNodeValue("username");
				this.users[i]["administrator"] = getAttribute("administrator");
				this.users[i]["active"] = getAttribute("active");
				this.users[i]["fontface"] = getChildNodeValue("fontface");
				this.users[i]["fontsize"] = getChildNodeValue("fontsize");
				this.users[i]["fontcolor"] = getChildNodeValue("fontcolor");
				this.users[i]["backgroundcolor"] = getChildNodeValue("backgroundcolor");
			}
		}
	}

	// Phone Categories
	if (this.action=='phonecategory_list') {
		var element = (oXML.getElementsByTagName("phonecategory").length > 0)?oXML.getElementsByTagName("phonecategory"):null;
		if (element != null) {
			this.phonecategories = new Array();
			var eleTemp = element;
			for (var i=0; i<eleTemp.length; i++) {
				element = eleTemp[i];
				this.phonecategories[i] = new Array();
				this.phonecategories[i]["id"] = getAttribute("id");
				this.phonecategories[i]["name"] = getChildNodeValue("name");
				this.phonecategories[i]["order"] = getChildNodeValue("order");
			}
		}
	}

	// Get Users
	if (this.action=='get_users') {
		var element = (oXML.getElementsByTagName("user").length > 0)?oXML.getElementsByTagName("user"):null;
		if (element != null) {
			this.users = new Array();
			var eleTemp = element;
			for (var i=0; i<eleTemp.length; i++) {
				element = eleTemp[i];
				this.users[i] = new Array();
				this.users[i]["id"] = getAttribute("id");
				this.users[i]["active"] = getAttribute("active");
				this.users[i]["firstname"] = getChildNodeValue("firstname");
				this.users[i]["lastname"] = getChildNodeValue("lastname");
				this.users[i]["email"] = getChildNodeValue("email");
				this.users[i]["address"] = getChildNodeValue("address");
				this.users[i]["address2"] = getChildNodeValue("address2");
				this.users[i]["city"] = getChildNodeValue("city");
				this.users[i]["state"] = getChildNodeValue("state");
				this.users[i]["postalcode"] = getChildNodeValue("postalcode");
				this.users[i]["username"] = getChildNodeValue("username");
				this.users[i]["adminsitrator"] = getAttribute("adminsitrator");
			}
		}
	}
	
	// Contacts
	var element = (oXML.getElementsByTagName("contact").length > 0)?oXML.getElementsByTagName("contact"):null;
	if (element != null) {
		this.contacts = new Array();
		var eleTemp = element;
		for (var i=0; i<eleTemp.length; i++) {
			element = eleTemp[i];
			this.contacts[i] = new Array();
			this.contacts[i]["id"] = getAttribute("id");
			this.contacts[i]["firstname"] = getChildNodeValue("firstname");
			this.contacts[i]["lastname"] = getChildNodeValue("lastname");
			var element = (element.getElementsByTagName("number").length > 0)?element.getElementsByTagName("number"):null;
			if (element != null) {
				this.contacts[i]["numbers"] = new Array();
				
				var eleTemp2 = element;
				for (var ii=0; ii<eleTemp2.length; ii++) {
					element = eleTemp2[ii];
					this.contacts[i]["numbers"][ii] = new Array();
					this.contacts[i]["numbers"][ii]["id"] = getAttribute("id");
					this.contacts[i]["numbers"][ii]["categoryid"] = getAttribute("categoryid");
					this.contacts[i]["numbers"][ii]["categoryname"] = getAttribute("categoryname");
					this.contacts[i]["numbers"][ii]["phonenumber"] = getAttribute("phonenumber");
				}
			}				
		}
	}
	
	element = (oXML.getElementsByTagName("call").length > 0)?oXML.getElementsByTagName("call")[0]:null;
	if(element!=null){ //This sets the properties for the active call
		this.callid = getAttribute("id");
		this.calltype = getAttribute("type");
		this.ani = getAttribute("ani");
		this.dialed = getAttribute("dialed");
		this.messagefragmentid = getAttribute("messageid");
		this.messagefragment = getNodeValue();
	}
	
	element = (oXML.getElementsByTagName("user").length > 0)?oXML.getElementsByTagName("user")[0]:null;
	if(element!=null){ //This sets the user's name
		//get the user's full name
		element = (oXML.getElementsByTagName("name").length > 0)?oXML.getElementsByTagName("name")[0]:null;
		this.user = (element==null)?null:getNodeValue();
		//get the user's first name
		element = (oXML.getElementsByTagName("firstname").length > 0)?oXML.getElementsByTagName("firstname")[0]:null;
		this.firstname = (element==null)?null:getNodeValue();
		//get the user's last name
		element = (oXML.getElementsByTagName("lastname").length > 0)?oXML.getElementsByTagName("lastname")[0]:null;
		this.lastname = (element==null)?null:getNodeValue();
		//get the user's street address
		element = (oXML.getElementsByTagName("address").length > 0)?oXML.getElementsByTagName("address")[0]:null;
		this.address = (element==null)?null:getNodeValue();
		//get the user's city
		element = (oXML.getElementsByTagName("city").length > 0)?oXML.getElementsByTagName("city")[0]:null;
		this.city = (element==null)?null:getNodeValue();
		//get the user's state
		element = (oXML.getElementsByTagName("state").length > 0)?oXML.getElementsByTagName("state")[0]:null;
		this.state = (element==null)?null:getNodeValue();
		//get the user's postal code
		element = (oXML.getElementsByTagName("postalcode").length > 0)?oXML.getElementsByTagName("postalcode")[0]:null;
		this.postalcode = (element==null)?null:getNodeValue();
		//get the user's email
		element = (oXML.getElementsByTagName("email").length > 0)?oXML.getElementsByTagName("email")[0]:null;
		this.email = (element==null)?null:getNodeValue();
		//get the user's username
		element = (oXML.getElementsByTagName("username").length > 0)?oXML.getElementsByTagName("username")[0]:null;
		this.username = (element==null)?null:getNodeValue();
		//get the user's password
		element = (oXML.getElementsByTagName("password").length > 0)?oXML.getElementsByTagName("password")[0]:null;
		this.password = (element==null)?null:getNodeValue();
		//get the user's password
		element = (oXML.getElementsByTagName("active").length > 0)?oXML.getElementsByTagName("active")[0]:null;
		this.active = (element==null)?null:getNodeValue();
	}
	
	element = (oXML.getElementsByTagName("message").length > 0)?oXML.getElementsByTagName("message")[0]:null;
	if(element!=null){ //This sets the properties for the status message
		this.message = getNodeValue();
	}
	
	element = (oXML.getElementsByTagName("state").length > 0)?oXML.getElementsByTagName("state"):null;
	if(element!=null){ //This sets up the supported states
		this.states = new Array();
		var eleTemp = element; //saves the states node.
		for(var i=0; i<eleTemp.length; i++){
			element = eleTemp[i]; //sets element to be the current node.
			this.states[i] = new Array();
			this.states[i]["abbreviation"] = getAttribute("id");
			this.states[i]["name"] = getNodeValue();
		}
	}
	
	element = (oXML.getElementsByTagName("error").length > 0)?oXML.getElementsByTagName("error")[0]:null;
	if(element!=null){ //This sets the properties for the error
		this.errorid = getAttribute("id");
		this.errormessage = getNodeValue();
	}
	
	//Private function returns the value for the specified attribute in the specified element
	function getAttribute(sAttribute){
		return (element.attributes!=null && element.attributes!=undefined)?(element.attributes.getNamedItem(sAttribute))?element.attributes.getNamedItem(sAttribute).value:"":"";
	}
	//Private function returns the value for the specified node
	function getNodeValue(){
		return (element.firstChild!=null && element.firstChild!=undefined)?(element.firstChild.nodeValue)?element.firstChild.nodeValue:"":"";
	}

	function getChildNodeValue(sChildNode) {
		return (element.getElementsByTagName(sChildNode)[0].firstChild!=null && element.getElementsByTagName(sChildNode)[0].firstChild!=undefined)?(element.getElementsByTagName(sChildNode)[0].firstChild.nodeValue)?element.getElementsByTagName(sChildNode)[0].firstChild.nodeValue:"":"";
	}
}
