/*
 * Wholesaler Application
 * Revision: 1.1.0
 */

// Open the user editor
function editUser(uri) {
	// Get user ID
	var userID = document.aedForm.elements['edit-user_id'].value;
	if (userID == '0') {
		alert('You must create a user first');
	} else {
		window.location = uri + '?action=3&index=' + userID;
	}
}

// Create a new user
function createUser(appID) {
	var userID = document.aedForm.elements['edit-user_id'].value;
	
	if (parseInt(userID) > 0) {
		alert('A user already exists for this application');
		return;
	}
	
	var user = prompt('Please enter a username:', '');
	if (user) {
		// Create user
		ajaxCall(uri + '?a=user&index=' + appID + '&user=' + user.urlSafe(), 'create_callback');
		setStatus('Creating user..');
	}
}

// Link to user
function linkUser(appID) {
	var userID = document.aedForm.elements['edit-user_id'].value;
	
	var user = prompt('Please enter an existing user ID:', '');
	if (user) {
		// Create user
		ajaxCall(uri + '?a=link&index=' + appID + '&user=' + user.urlSafe(), 'create_callback');
		setStatus('Linking to user..');
	}
}

function create_callback(txt) {
	document.aedForm.elements['edit-user_id'].value = txt;
	setStatus('Done', true);
}

// Send an e-mail
function sendEmail(appID) {
	var userID = document.aedForm.elements['edit-user_id'].value;
	if (userID == '0') {
		alert('You must create a user first');
	} else {
		// Warn first
		if (!confirm("Are you sure you want to dispatch the approved e-mail?")) return;
		
		// Send e-mail
		ajaxCall(uri + '?a=email&index=' + appID, 'email_callback');
		setStatus('Sending e-mail..');
	}
}

function email_callback(txt) {
	setStatus('Done', true);
	if (txt == 'OK') {
		document.getElementById('edit-sent_email').src = '/usr/common/images/icons/silk/tick.png';
	} else {
		alert(txt);
	}
}





