					
function onPageReload(event)
{
	var registration_id = document.forms['form_registration_first_step'].elements['registration_info'].value;
	new Ajax.Request
	(
		'/Registration/ajaxGetRegistration/',
		{
			parameters: 'registration_id='+registration_id,
			onComplete:function(transport)
			{
				var text = transport.responseText;
				if (text.substr(0, 6)=='TOKEN_') // Registrierung wurde bereits gestartet, aber noch nicht beendet
				{
					document.forms['form_registration_first_step'].elements['existing_user_token'].value = text.substr(6);
					positionMessage('');	
				}

				if (text.substr(0, 9)=='FINISHED_') // Registrierung wurde bereits gestartet, aber abgeschlossen
				{
					document.forms['form_registration_first_step'].elements['existing_user_token'].value = text.substr(9);
					positionMessage('finished');	
				}
			}
		}
	);
}

function locateToSecondPage()
{
	var token = document.forms['form_registration_first_step'].elements['existing_user_token'].value;
	location.href='/Registration/secondStep/?token='+token;
}

function loadStatusPage()
{
	var token = document.forms['form_registration_first_step'].elements['existing_user_token'].value;
	location.href="/Registration/confirmFinished/?token="+token;
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
		window.onload = func;
	else
		{ window.onload = function() { oldonload(); func(); } }
}

function toggleVisibilitySelectboxes(visible)
{
	var style = (visible)?'visible':'hidden';
	if ( navigator.appName=="Microsoft Internet Explorer" && parseInt(navigator.appVersion)<=6 )
	{
		if ($('registration_usertitle'))
			$('registration_usertitle').style.visibility = style;
		if ($('registration_useracademic'))	
			$('registration_useracademic').style.visibility = style;
		if ($('registration_country'))
			$('registration_country').style.visibility = style;
		if ($('registration_salary'))
			$('registration_salary').style.visibility = style;
	}
}

function toggleVisibilityCalendarSelectboxes(visible)
{
	if (! ( navigator.appName=="Microsoft Internet Explorer" && parseInt(navigator.appVersion)<=6 ) )
		return false;

	var selects_style = (visible)?'visible':'hidden';
	var SelectsExceptions = new Array('scwMonths','scwYears');	// IDs der Selectboxen die im IE6 bei Zeigen der Modalbox auch angezeigt werden sollen
	for (var i=0;i<SelectsExceptions.length;i++)				// bzw. beim Schliessen auch mit verschwinden
		if ($(SelectsExceptions[i]))
			$(SelectsExceptions[i]).style.visibility = selects_style;
}



var global_json = null;
var disableSubmit = false;
var timeout = null;

function redirectToSecondPage(text)
{
	if (text.substr(0, 6)=='TOKEN_')
		location.href="/Registration/secondStep/?token="+text.substr(6);
}

function resetDisableSubmit()
{
	window.clearTimeout(timeout);
	disableSubmit = false;
}

function doSubmitRegistration()
{
	if (disableSubmit) return false;

	if (global_json != null)
		hideErrors();

	$('img_form_submit').style.display = '';
	
	disableSubmit = true;
	timeout = window.setTimeout(resetDisableSubmit,5000); // nach 5 Sekunden darf erneut abgesendet werden

	new Ajax.Request('/Registration/firstStep/',
	{
		method:'post',
		requestHeaders: { Accept: 'application/json' },
		parameters:  $('form_registration_first_step').serialize(),
		onFailure: function(transport)
		{
			$('img_form_submit').style.display = 'none';
			var json = transport.responseText.evalJSON(true);
			showValidationResults(json);
		},
		onSuccess: function(transport)
		{
			redirectToSecondPage(transport.responseText);
		},
		onComplete: function(transport)
		{
			disableSubmit = false;
		}
	});
}

function hideErrors()
{
	for ( keyVar in global_json )
	{
		if ($(keyVar))
			if ($(keyVar).className == 'registration_input_error')
				$(keyVar).className = 'registration_input';
				
			if ($(keyVar).className == 'registration_input_error MB_focusable')	// Modalbox.js adds MB_focusable to classname
				$(keyVar).className = 'registration_input MB_focusable';

		if($(keyVar+'_error'))
		{
			$(keyVar+'_error').style.display = 'none';
			$(keyVar+'_error').innerHTML = '';
		}
	}
	
	if ($('footer_global'))
	{
		$('footer_global').style.position = 'absolute';
		$('footer_global').style.bottom = '0px';
	}

	if($('MB_window'))
        Modalbox.resizeToContent();
}

function showValidationResults(json)
{
	global_json = json;

	if (json[0]!=undefined)
	if ((json[0]["html_output"]!=undefined)) // show some information
	{
		$('div_registration_form_alternative').innerHTML = json[0]["html_output"];
		$('div_registration_form').style.display = 'none'; // hide the original form
		$('div_registration_form_alternative').style.display = 'block';
		if($('MB_window'))
			Modalbox.resizeToContent();
		return false;
	}

	for ( keyVar in json )
	{
		if ($(keyVar))
			if ($(keyVar).className == 'registration_input')
				$(keyVar).className = 'registration_input_error';
			
			if ($(keyVar).className == 'registration_input MB_focusable')	// Modalbox.js adds MB_focusable to classname
				$(keyVar).className = 'registration_input_error MB_focusable';

		if($(keyVar+'_error'))
		{
			$(keyVar+'_error').innerHTML = json[keyVar];
			$(keyVar+'_error').style.display = 'block';
		}
	}
	
	if ($('footer_global'))
	{
		$('footer_global').style.position = 'absolute';
		$('footer_global').style.bottom = '0px';
	}
	
	if($('MB_window'))
        Modalbox.resizeToContent();
}



function positionMessage(param)
{
	var m = '';
	
	if (typeof param != 'undefined');
		if (param=='finished')
			m = '_finished';

	if ($("messagebox"+m))
	{
		//if (m=='_finished' && $("messagebox"))	// es wird noch die alte Nachricht angezeigt
		//	$("messagebox").style.display = 'none';
		Modalbox.show( $('messagebox'+m), { title:'', width: 350, overlayClose: false});
	}
}
