/**
 * Get the value of the first instance of an XML tag in xml
 *
 * @param xml XML object to find the tag in
 * @param {String} tag XML tag to find
 *
 * @return {String} The string value of the xml element
 */
function getXMLTagData( xml, tag ) {
	if( xml ) {
		var q = xml.getElementsByTagName( tag );
		if( q.length > 0 ) {
		   return getValue( q[0] );
		}
	}
	return "";
}

/**
 * Get the value of the first instance of an XML tag in xml
 *
 * @param xml XML object to find the tag in
 * @param {String} tag XML tag to find
 *
 * @return {String} The string value of the xml element
 */
function getXMLTagDataArray( xml, tag ) {
	var q = xml.getElementsByTagName( tag );
	var arr = Array();

	for( var x = 0; x < q.length; x++ ) {
		arr.push(getValue(q[x]));
	}
	
	return arr;
}


/**
 * Get the value from an element
 *
 * @param element Element to extract the value from
 *
 *  @return {String} Value of the element
 */
function getValue(element) {
   if( element ) {
	  if( element.textContent ) {
		 return element.textContent;
	  } else if( element.text ) {
		 return element.text;
	  } else if( element.innerHTML ){
		 return element.innerHTML;
	  }
   }
   return "";
}


function XML() {

}

XML.checkResponse = function( response ) {
	var code = XML.getXMLTagData( response, 'code' );
	Console.log( 'Checking XML code: ' + code );
	if( code == '20000000' ) { //success
		return true;
	}else if( code == '20001008' ){//invalid session
		NavigationBar.executeLogoutHandler( 'sessiontimeout' ); //just kill the session
		return false;
	}else if( code == '20001001' ) { //error, sometimes invalid session
		Console.log( 'XML Error Code' );
		var desc = XML.getXMLTagData( response, 'description' );
		if( desc ) {
			if( desc.indexOf('Invalid Session') > -1 ) {
				Console.log( 'Invalid Session!');
				NavigationBar.executeLogoutHandler( 'sessiontimeout' );
			}
		}
		return false;
	}else{ //anything else is failure
		return false;
	}
}

/**
 * Checks if the XML document has a live session or indicated an invalid session
 *
 * @param response XML Document
 *
 * @return True if session is alive, false if invalid
 */
XML.checkResponseSession = function( response ) {
	var code = XML.getXMLTagData( response, 'code' );
	if( code == '20001008' ) {
		return false;
	}else if( code == '20001001' ) {
		var desc = XML.getXMLTagData( response, 'description' );
		if( desc ) {
			if( desc.indexOf( 'Invalid Session' ) > -1 ) {
				return false;
			}
		}
	}
	return true;
}

XML.getDesc = function( response ) {
	var code = XML.getXMLTagData( response, 'code' );
	if( code ) {
		if( ERROR_CODES[code] ) {
			if( ERROR_CODES[code] == "USEDESC" ) {
				var desc = XML.getXMLTagData( response, 'description' );
				return desc;
			}else{
				return ERROR_CODES[code];
			}
		}else{
			return "Unknown Error";
		}
	}
	return "Unknown Error";
}

XML.getXMLTagData = getXMLTagData;
XML.getTagData = getXMLTagData;
XML.getXMLTagDataArray = getXMLTagDataArray;
XML.getValue = getValue;

XML.makeTag = function( tagName, val ) {
	return '<' + tagName + '>' + val + '</' + tagName + '>';
}



var ERROR_CODES = Array();

 ERROR_CODES['20000000'] = "Success";
 ERROR_CODES['20000001'] = "CODE_SUCCESS_WITH_ERRORS	20000001";
 ERROR_CODES['20001000'] = "Unspecified Error";
 ERROR_CODES['20001001'] = "Server error";
 ERROR_CODES['20001002'] = "API not available";
 ERROR_CODES['20001003'] = "Numeric or value error";
 ERROR_CODES['20001004'] = "Authentication failed";
 ERROR_CODES['20001005'] = "Invalid request format";
 ERROR_CODES['20001006'] = "Not certified";
 ERROR_CODES['20001007'] = "Request not processed";
 ERROR_CODES['20001008'] = "Invalid Session";
 ERROR_CODES['20001009'] = "Permission denied";
 ERROR_CODES['20002001'] = "Application does not exist";
 ERROR_CODES['20002002'] = "Domain does not exist";
 ERROR_CODES['20002003'] = "Domain already taken";
 ERROR_CODES['20002004'] = "Username already exists";
 ERROR_CODES['20002005'] = "Enterprise name already exists";
 ERROR_CODES['20002006'] = "Enterprise is locked";
 ERROR_CODES['20002007'] = "Enterprise has been cancelled";
 ERROR_CODES['20002008'] = "User has been cancelled";
 ERROR_CODES['20002009'] = "Account (user) is locked";
 ERROR_CODES['20002010'] = "User not found";
 ERROR_CODES['20002011'] = "Enterprise not found";
 ERROR_CODES['20002012'] = "Invalid account plan";
 ERROR_CODES['20002013'] = "Invalid division";
 ERROR_CODES['20002014'] = "Invalid access limit";
 ERROR_CODES['20002015'] = "Invalid username";
 ERROR_CODES['20002016'] = "Quota / usage discrepancy";
 ERROR_CODES['20002017'] = "Alias already exists";
 ERROR_CODES['20002018'] = "Invalid domain";
 ERROR_CODES['20002019'] = "Forward already exists";
 ERROR_CODES['20002020'] = "Invalid password";
 ERROR_CODES['20002021'] = "Invalid credit card";
 ERROR_CODES['20002022'] = "Invalid user for action";
 ERROR_CODES['20002023'] = "Forward does not exist";
 ERROR_CODES['20002024'] = "SSL could not be enabled";
 ERROR_CODES['20002025'] = "Plan must support SSL";
 ERROR_CODES['20002026'] = "Invalid Reseller parent ID";
 ERROR_CODES['20002027'] = "Reseller contains other Resellers or Enterprises";
 ERROR_CODES['20002028'] = "Invalid plan or plan needs SSL";
 ERROR_CODES['20002029'] = "Reseller name is too short";
 ERROR_CODES['20002030'] = "Reseller name is too long";
 ERROR_CODES['20002031'] = "Invalid Reseller name";
 ERROR_CODES['20002032'] = "Reseller name already exists";
 ERROR_CODES['20002033'] = "Invalid Reseller account type";
 ERROR_CODES['20002034'] = "Reseller not found";
 ERROR_CODES['20002035'] = "Invalid or missing Enterprise parent";
 ERROR_CODES['20002036'] = "Enterprise name too short";
 ERROR_CODES['20002037'] = "Enterprise name too long";
 ERROR_CODES['20002038'] = "Enterprise name invalid";
 ERROR_CODES['20002039'] = "Allocated plan slots exceeded";
 /* Invalid Field Length should use the desc passed back */
 ERROR_CODES['20002040'] = "USEDESC";
 ERROR_CODES['20002041'] = "Extranet User creation failed"
 ERROR_CODES['20002042'] = "Support ID cannot be found";
 ERROR_CODES['20002043'] = "UserID or Timeout value given was not a valid numerical value.";


/**
 *	Resource Manager Notification
 */
JavaScriptResource.notifyComplete("./src/Applications/XML.js");
