/**
* Methods for making requests for administration services and handling the responses
*/

var AdminService = {

	CHANGE_PASSWORD: 'changepassword',

	LOCKS_LIST: 'locks.list',
	LOCKS_REMOVEALL: 'locks.removeall',
	LOCKS_REMOVESELECTED: 'locks.removeselected',
	LOCK_CLEAR: 'lock.clear',

	REMOVED_LIST: 'removed.list',
	REMOVED_PURGE: 'removed.purge',
	REMOVED_RESTORE: 'removed.restore',

	LOGIN: 'login',
	LOGOUT: 'logout',
	
	

	// ADMIN SERVICES
	
	/**
	* Clear current entity lock
	* @param context the context
	* @param entity entity type
	*/
	clearLock: function(context, entity) {
		var idElement = context + Service.SCOPE_SEPARATOR + Widget.EDITOR + Service.SCOPE_SEPARATOR + entity + Service.SCOPE_SEPARATOR + 'id';
		var entityID = $F(idElement);
		var pars = 'service=' + AdminService.LOCK_CLEAR;
		pars += '&' + 'context=' + context;
		pars += '&' + 'entity=' + entity;
		pars += '&' + 'entityID=' + entityID;
		// Check whether report status
		var onSuccessFunction = 'Service.success';
		var onFailureFunction = 'Service.failure';
		new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});
	},

	handlePasswordEvent: function(event, context) {
		var element = Event.element(event);	
		if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'save')) {
			// Validate
			AdminService.changePassword(context);
		} else if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'clear')) {
			var textInputElements = $(context + Service.SCOPE_SEPARATOR + 'password' + Service.SCOPE_SEPARATOR + 'form').getInputs('password');
			for (i = 0; i < textInputElements.length; i++) {
				$(textInputElements[i]).value = '';
			}
		} else return;
	},

	handleRemovedEvent: function(event, context) {
		var element = Event.element(event);
		if ($(element).tagName == 'TD') {
			var parentTR = $(element).up('tr', 0);
			// If the tr has no id then we are in view mode and don't have to do anything
			if (parentTR.id.length == 0) return;
			var previousSiblings = $(element).previousSiblings();
			var nextSiblings = $(element).nextSiblings();
			var selected = $(element).hasClassName('tableRow2');
			if (!selected) {
				$(parentTR).toggleClassName('tableRow2');
			} else {
				$(parentTR).toggleClassName('tableRow0');
			}
		}else if (element.id == 'admin-removed-select') { AdminService.selectAllRemoved(context, true);
		}else if (element.id == 'admin-removed-deselect') { AdminService.selectAllRemoved(context, false);
		}else if (element.id == 'admin-removed-purge') { AdminService.purgeRemoved(context);
		}else if (element.id == 'admin-removed-restore') { AdminService.restoreRemoved(context);}
	},

	handleAccessEvent: function(event, context) {
		var element = Event.element(event);	
		var target = context + Service.SCOPE_SEPARATOR + 'access';
		if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'login') || event.type == 'keypress') {
			var username = $(context + Service.SCOPE_SEPARATOR + 'username').value;
			var password = $(context + Service.SCOPE_SEPARATOR + 'password').value;
			AdminService.login(target, context, username, password);
		} else if (element.id.endsWith(Service.SCOPE_SEPARATOR + 'logout')) {
			AdminService.logout(target, context);
		} else return;
	},

	changePassword: function(context) {
		var oldPassword = $(context + Service.SCOPE_SEPARATOR + 'password' + Service.SCOPE_SEPARATOR + 'oldPassword').value;
		if (oldPassword == undefined || oldPassword == null || oldPassword.length == 0) {
			alert('Please enter your old password');
			return;
		}
		var newPassword = $(context + Service.SCOPE_SEPARATOR + 'password' + Service.SCOPE_SEPARATOR + 'newPassword').value;
		if (newPassword == undefined || newPassword == null || newPassword.length == 0) {
			alert('Please enter your new password');
			return;
		}
		var confirmPassword = $(context + Service.SCOPE_SEPARATOR + 'password' + Service.SCOPE_SEPARATOR + 'confirmPassword').value;
		if (confirmPassword == undefined || confirmPassword == null || confirmPassword.length == 0) {
			alert('Please confirm your new password');
			return;
		}
		if (newPassword != confirmPassword) {
			alert('The new password and the confirm password are different');				
			return;
		}
		// Collect values and invoke service
		var pars = 'service=' + AdminService.CHANGE_PASSWORD;
		pars += '&' + 'oldPassword=' + hex_md5(oldPassword);
		pars += '&' + 'newPassword=' + hex_md5(newPassword);
		var onSuccessFunction = 'Service.success';
		var onFailureFunction = 'Service.failure';
		new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});
		
	},

	purgeRemoved: function(context) {
		var answer = confirm('Please confirm your purge request. This operation cannot be reversed');
		if (answer) {
			var service = AdminService.REMOVED_PURGE;
			var pars = 'service=' + service;
			pars += '&' + 'context=' + context;
			var selected = AdminService.getRemovedSelection(context);
			if (selected.length == 0) {
				alert('Please make a selection');
				return;
			}
			else pars += '&' + selected;
			var onSuccessFunction = 'Service.success';
			var onFailureFunction = 'Service.failure';
			new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});
		}
	},

	restoreRemoved: function(context) {
		var answer = confirm('Please confirm your restore request.');
		if (answer) {
			var service = AdminService.REMOVED_RESTORE;
			var pars = 'service=' + service;
			pars += '&' + 'context=' + context;
			var selected = AdminService.getRemovedSelection(context);
			if (selected.length == 0) {
				alert('Please make a selection');
				return;
			}
			else pars += '&' + selected;
			var onSuccessFunction = 'Service.success';
			var onFailureFunction = 'Service.failure';
			new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});
		}
	},

 	listRemoved: function(context, entityTypeID) {
		var service = AdminService.REMOVED_LIST;
		var pars = 'service=' + service;
		pars += '&' + 'target=' + context + Service.SCOPE_SEPARATOR + 'removed-list';
		pars += '&' + 'context=' + context;
		pars += '&' + 'widget=' + Widget.ADMIN_REMOVED;
		//if (entityTypeID == undefined || entityTypeID == null || entityTypeID.length == 0) pars += '&' + 'entityTypeID=' + '1';
		//else pars += '&' + 'entityTypeID=' + entityTypeID;
		var onSuccessFunction = 'Service.contentSuccess';
		var onFailureFunction = 'Service.failure';
		new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});
	},

	listEntityTypes: function(context) {
		var service = Service.LISTER;
		var pars = 'service=' + service;
		pars += '&' + 'target=' + context + Service.SCOPE_SEPARATOR + 'removed-entitytype-list';
		pars += '&' + 'context=' + context;
		pars += '&' + 'widget=' + Widget.LISTER;
		pars += '&' + 'entity=' + Entity.ENTITY;
		pars += '&' + 'size=1';
		var eventHandler = 'AdminService.refreshRemoved(\'' + context + '\',\'@id\')';
		pars += '&' + 'eventHandler=' + eventHandler;
		var onSuccessFunction = 'Service.contentSuccess';
		var onFailureFunction = 'Service.failure';
		new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});
	},


/**
	* Get the ids of the removed entities selected for purge or restore
	*/
	getRemovedSelection: function(context) {
		var values = new String();
		var descendants = $(context).descendants();
		for (i = 0; i < descendants.length; i++) {
			if ($(descendants[i]).tagName == 'TR') {
				if (descendants[i].id.length > 0) {
					if ($(descendants[i]).hasClassName('tableRow2')) {
						if (values.length == 0) values += descendants[i].id;
						else values += Service.SCOPE_SEPARATOR + descendants[i].id;
					}
				}
			}
		}
		if (values.length > 0) return 'removed=' + values;
		else return '';
	},

	/**
	* Used to select or deselect all parts in the product parts widget
	*/
	selectAllRemoved: function(context, flag) {
		var descendants = $(context).descendants();
		for (i = 0; i < descendants.length; i++) {
			if ($(descendants[i]).tagName == 'TR') {
				if (descendants[i].id.length > 0) {
					if (flag) descendants[i].addClassName('tableRow2');
					else descendants[i].removeClassName('tableRow2');
				}
			}
		}
	},

	refreshRemoved: function(context, elementID){
		AdminService.listRemoved(context);
	},


	login: function(target, context, username, password) {
		// Validate here
		var message = '';
		var pars = 'service=' + AdminService.LOGIN;
		pars += '&' + 'target=' + target;
		pars += '&' + 'context=' + context;
		pars += '&' + 'widget=' + Widget.ADMIN_LOGIN;
		pars += '&' + 'username=' + username;
		pars += '&' + 'password=' + hex_md5(password);
		var onSuccessFunction = 'Service.contentSuccess';
		var onFailureFunction = 'Service.failure';
		new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});
	},

	logout: function(target, context) {
		var pars = 'service=' + AdminService.LOGOUT;
		pars += '&' + 'target=' + target;
		pars += '&' + 'context=' + context;
		var onSuccessFunction = 'Service.success';
		var onFailureFunction = 'Service.failure';
		new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});
	},


	listLocks: function(context) {
		var service = AdminService.LOCKS_LIST;
		var pars = 'service=' + service;
		pars += '&' + 'target=' + context + Service.SCOPE_SEPARATOR + 'lock';
		pars += '&' + 'context=' + context;
		pars += '&' + 'widget=' + Widget.ADMIN_LOCKS;
		var onSuccessFunction = 'Service.contentSuccess';
		var onFailureFunction = 'Service.failure';
		new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});

	},

	removeSelectedLocks: function(context) {
		var service = AdminService.LOCKS_REMOVESELECTED;
		var pars = 'service=' + service;
		pars += '&' + 'target=' + context + Service.SCOPE_SEPARATOR + 'lock';
		pars += '&' + 'context=' + context;
		pars += '&' + 'widget=' + Widget.ADMIN_LOCKS;
		// Serialize the form data so we get checkbox states of our selected locks
		var data = $(context + Service.SCOPE_SEPARATOR + 'lock' + Service.SCOPE_SEPARATOR + 'form').serialize();
		if (data.length > 0) pars += '&' + data;
		var onSuccessFunction = 'Service.contentSuccess';
		var onFailureFunction = 'Service.failure';
		new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});
	},

	removeAllLocks: function(context) {
		var service = AdminService.LOCKS_REMOVEALL;
		var pars = 'service=' + service;
		pars += '&' + 'target=' + context + Service.SCOPE_SEPARATOR + 'lock';
		pars += '&' + 'context=' + context;
		pars += '&' + 'widget=' + Widget.ADMIN_LOCKS;
		var onSuccessFunction = 'Service.contentSuccess';
		var onFailureFunction = 'Service.failure';
		new Ajax.Request(Service.URL, {parameters:pars, onSuccess:eval(onSuccessFunction), onFailure:eval(onFailureFunction)});
	}



}
