/*
 * I am a set of JavaScript functions for sending bookmarks using an AJAX-style in-place form.
 * Rather than using a normal form submission, the contents of the bookmark panel div
 * are replaced by server-side generated HTML fragments when a form button is clicked.
 * 
 * I use Prototype, a JavaScript framework with some AJAX helper functions.
 * See http://prototype.conio.net/ for the code and
 * http://www.sergiopereira.com/articles/prototype.js.html for documentation.
 */
 
function getControllerUrl(controllerName) {
	return $('dispatcherUri').innerHTML + '/' + controllerName;
}

function showButton()	{
	var url = getControllerUrl('showButton');
	var ajax = new Ajax.Updater('bookmarkPanel', url, {
		method: 'get'
	});
	return false;
} 

function showFormOnComplete(focusOn) {
	if ($('phoneNumber')) {
		$(focusOn).focus();
		$('captchaImage').src = getControllerUrl('captchaImage') + '?' + new Date().getTime();
		$('captchaResponse').value = "";
	};
}

function showForm()	{
	var url = getControllerUrl('showForm');
	var ajax = new Ajax.Updater('bookmarkPanel', url, {
		method: 'get',
		onComplete: function() { showFormOnComplete('phoneNumber'); }
	});
	return false;
}

function send() {
	var url = getControllerUrl('send');

	// Figure out which radio button has been selected
	var format = 'plainText';
	if ($F('nokiaOtaFormat')) format = 'nokiaOta';
	if ($F('wapPushFormat')) format = 'wapPush';
	
	var parameters = {
		phoneNumber: $F('phoneNumber'),
		captchaResponse: $F('captchaResponse'),
		format: format,
		bookmarkUrl: $('bookmarkUrl').innerHTML
	};
	var parameterString = $H(parameters).toQueryString();
	var ajax = new Ajax.Updater('bookmarkPanel', url, {
		method: 'get',
		parameters: parameterString,
		onComplete: function() { showFormOnComplete('phoneNumber'); }
	});
	return false;
}	

function sendAgain(parameterString) {
	var url = getControllerUrl('showForm');
	
	var ajax = new Ajax.Updater('bookmarkPanel', url, {
		method: 'get',
		parameters: parameterString + "&sendAgain=true",
		onComplete: function() { showFormOnComplete('captchaResponse'); }
	});
	return false;
}	

