
/* +-----------------------------------------------------------------+ */
/* | User DAO                                                        | */
/* +-----------------------------------------------------------------+ */
var vyreUserDAOs = new Array();

function UserDAO( userGateway ) {
	this.gateway = userGateway;
	this.loadLinkedItems = false;
	this.loadLinkedContentAndMetadata = false;
	this.xslId = 0;
	this.resultHandler = null;
	this.errorHandler = function() {
	    alert("Failed.");
	}

	vyreUserDAOs[userGateway.id] = this;
}

UserDAO.prototype.get = function( realmId, userId ) {
	var parameters = '&type=get'
		+ '&realmId=' + realmId
		+ '&userId=' + userId
		+ '&xslId=' + this.xslId
		+ '&loadLinkedItems=' + this.loadLinkedItems
		+ '&loadLinkedContentAndMetadata=' + this.loadLinkedContentAndMetadata
		;

	// send the request
	this.gateway.sendSynchronousXMLHttpRequest(parameters, new Function("xml", "vyreUserDAOResultListener('" + this.gateway.id + "', xml)"), this.errorHandler);
}

UserDAO.prototype.getCurrent = function() {
	var parameters = '&type=getCurrent'
		+ '&xslId=' + this.xslId
		+ '&loadLinkedItems=' + this.loadLinkedItems
		+ '&loadLinkedContentAndMetadata=' + this.loadLinkedContentAndMetadata
		;

	// send the request
	this.gateway.sendSynchronousXMLHttpRequest(parameters, new Function("xml", "vyreUserDAOResultListener('" + this.gateway.id + "', xml)"), this.errorHandler);
}

function vyreUserDAOResultListener(gatewayId, xml) {
	var dao = vyreUserDAOs[gatewayId];
	if ( dao.resultHandler != null ) {
		if ( dao.xslId == 0 ) {
			// send the XML
			dao.resultHandler(xml);
		} else {
			// send the HTML as a string
			var html = xml.getElementsByTagName('ajax-response')[0].firstChild.data;
			dao.resultHandler(html);
		}
	} else {
		alert('No result handler was specified!');
	}
}


/* +-----------------------------------------------------------------+ */
/* | User Searcher Configuration                                     | */
/* +-----------------------------------------------------------------+ */

function UserSearcherConfig() {
	this.loadLinkedItems = false;
	this.loadLinkedContentAndMetadata = false;
	this.xslId = 0;
	this.sortField = 'score';
	this.sortDesc = false;
}

UserSearcherConfig.prototype.getParams = function() {
	var params = '&xslId=' + this.xslId
		+ '&loadLinkedItems=' + this.loadLinkedItems
		+ '&loadLinkedContentAndMetadata=' + this.loadLinkedContentAndMetadata
		+ '&sortField=' + this.sortField
		+ '&sortDesc=' + this.sortDesc
		;
		
	if ( this.sortLocale ) {
		params += '&sortLocale=' + this.sortLocale;
	}
	return params;
}


/* +-----------------------------------------------------------------+ */
/* | User Searcher Query                                             | */
/* +-----------------------------------------------------------------+ */

function UserQuery( str ) {
	this.defaultQuery = str;
	this.realmId = '';
	this.startIndex = -1;
	this.endIndex = -1;
}

UserQuery.prototype.clear = function() {
	this.defaultQuery = '';
	this.realmId = '';
	this.startIndex = -1;
	this.endIndex = -1;
}


/* +-----------------------------------------------------------------+ */
/* | User Searcher                                                   | */
/* +-----------------------------------------------------------------+ */
var vyreUserSearchers = new Array();

function UserSearcher( userGateway, searcherConfig ) {
	this.gateway = userGateway;
	this.config = searcherConfig;
	this.resultHandler = function(xml) {
		alert(xml);
	}
	this.errorHandler = function() {
	    alert("Failed.");
	}
	
	vyreUserSearchers[userGateway.id] = this;
}

UserSearcher.prototype.search = function( query ) {

	var parameters = '&type=search'
		+ '&query=' + query.defaultQuery
		+ '&startIndex=' + query.startIndex
		+ '&endIndex=' + query.endIndex
		+ '&realmId=' + query.realmId
		+ this.config.getParams()
		;

	// send the request
	this.gateway.sendSynchronousXMLHttpRequest(parameters, new Function("xml", "vyreUserSearcherResultListener('" + this.gateway.id + "', xml)"), this.errorHandler);
}

function vyreUserSearcherResultListener(gatewayId, xml) {
	var userSearcher = vyreUserSearchers[gatewayId];
	if ( userSearcher.resultHandler != null ) {
		if ( userSearcher.config.xslId == 0 ) {
			// send the XML
			userSearcher.resultHandler(xml);
		} else {
			// send the HTML as a string
			var html = xml.getElementsByTagName('ajax-response')[0].firstChild.data;
			userSearcher.resultHandler(html);
		}
	} else {
		alert('No result handler was specified!');
	}
}


/* +-----------------------------------------------------------------+ */
/* | User Gateway                                                    | */
/* +-----------------------------------------------------------------+ */
function UserGateway( gatewayId ) {
	this.id = gatewayId;
	this.pageId = 0;
	this.displayItemId = 0;
	this.contextPath = '';
	this.servletPath = (document.location.href.indexOf('vyre4') != -1) ? 
        '/vyre4/servlet/ajax' : 
        '/servlet/ajax';
}

/**
 * Internal function for sending a synchronous http post.
 *
 * @param parameters the parameters that are to be posted
 */
UserGateway.prototype.sendSynchronousXMLHttpRequest = function(parameters, handlerFunction, errorHandler) {

	var formParams = 'action=userGateway'
		+ '&gatewayId=' + this.id
		+ '&contextPath=' + this.contextPath
		+ '&pageId=' + this.pageId
		+ '&displayItemId=' + this.displayItemId
		+ parameters;
		;

	var req = this.createXMLHttpRequest();
	if ( req ) {
		req.open("POST", this.servletPath, true);
		req.onreadystatechange = function() {
			if ( req.readyState == 4 ) {
				if ( req.status == 200 ) {
					handlerFunction(req.responseXML);
				} else {
					errorHandler();
				}
			}
		};
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		req.send(formParams);
	} else {
		alert("This action is not supported on this browser.");
	}
}

/**
 * Internal function for creating an XMLHttpRequest instance in a 
 * platform independent way.
 */
UserGateway.prototype.createXMLHttpRequest = function() {

	var req = false;
    // branch for native XMLHttpRequest object
    if ( window.XMLHttpRequest ) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if ( window.ActiveXObject ) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
    return req;
}
