// FORM Library. Provides some JavaScript functions that deal with forms.

var _ajax_status_window = null;

// Used for Form::Password to track strength of the user-supplied password
function FORM_checkPasswordStrength(event)
{
	var element = Event.element(event);
	
	var key = event.which || event.keyCode;
	var val = element.value + String.fromCharCode(key);
	
	new Ajax.Updater('FORM_ID_PASSWORD_STRENGTH', '/ajax/form_check_password_strength.php', { parameters: 'pw=' + val });
}

// Centers a div on the screen
function FORM_center_div_on_screen(id)
{
	var elem = $(id);
	
	// We center the status window on the screen
	elem.show();
	
	var left = (document.viewport.getWidth() - elem.getWidth()) / 2;
	elem.style.left = left + 'px';

	var top = (document.viewport.getHeight() - elem.getHeight()) / 2;
	elem.style.top = top + 'px';
}

// Used for Forms that need to track Upload progress through PHP's APC
function FORM_submit_with_status(url)
{
	// We open a hidden, absolute layer that will contain the status message. The content of this status window
	// is to be updated through Ajax, at regular time intervals.
	var elem = $('FORM_ID_STATUS_WINDOW');
	
	elem.style.display = '';

	FORM_center_div_on_screen('FORM_ID_STATUS_WINDOW');
	
	_ajax_status_window = new Ajax.PeriodicalUpdater('FORM_ID_STATUS_WINDOW', url, { frequency: 1, evalScripts: true });
}

// Hides the status window
function FORM_hide_status_window()
{
	_ajax_status_window.stop();

	var elem = $('FORM_ID_STATUS_WINDOW');
	elem.style.display = 'none';
	
	location.href = '/home/upload-complete.php'; 
}

// Executes Script via AJAX and prints result in specified ID
function FORM_dynamic_input(txtfield_id, result_div_id, url)
{
	new Ajax.Updater(result_div_id, url, { parameters: { param: $(txtfield_id).value }, evalScripts: true });
}