function showLoginForm(div, url)
{
	showLayer('processUpdateReactionForm');
	
    if ($('reactionFormDiv')) {
        div = $('reactionFormDiv')              
    }
    new Ajax.Updater({success:div}, url,
    {
        onComplete:function()
        {
            Element.hide(div);
			hideLayer('processUpdateReactionForm');
            new Effect.Appear(div);
            duration:0.1
        },
        method:'get',
        evalScripts:true,
        asynchronous:true
    });
}

function processLogin(url)
{
	showLayer('processUpdateReactionForm');
	
    var params = Form.serialize($('loginForm'));
    new Ajax.Updater({success:'registrationLoginDiv'}, url,
    {
        onComplete:function()
        {
            Element.hide('registrationLoginDiv');
			hideLayer('processUpdateReactionForm');
            new Effect.Appear('registrationLoginDiv');
            duration:0.1
        },
        asynchronous:true,
        parameters:params
    });
}

function showForgotPasswordForm(url)
{
	showLayer('processUpdateReactionForm');
    new Ajax.Updater({success:'registrationLoginDiv'}, url,
    {
        onComplete:function()
        {
            Element.hide('registrationLoginDiv');
			hideLayer('processUpdateReactionForm');
            new Effect.Appear('registrationLoginDiv');
            duration:0.1
        },
        method:'get',
        evalScripts:true,
        asynchronous:true
    });
}

function processForgotPassword(url)
{
	showLayer('processUpdateReactionForm');
	
    var params = Form.serialize($('forgotPasswordForm'));
    new Ajax.Updater({success:'forgotPasswordDiv'}, url,
    {
        onComplete:function()
        {
            Element.hide('forgotPasswordDiv');
			hideLayer('processUpdateReactionForm');
            new Effect.Appear('forgotPasswordDiv');
            duration:0.1
        },
        asynchronous:true,
        parameters:params
    });
}

function showResendActivationForm(url)
{
	showLayer('processUpdateReactionForm');
    new Ajax.Updater({success:'registrationLoginDiv'}, url,
    {
        onComplete:function()
        {
            Element.hide('registrationLoginDiv');
			hideLayer('processUpdateReactionForm');
            new Effect.Appear('registrationLoginDiv');
            duration:0.1
        },
        method:'get',
        evalScripts:true,
        asynchronous:true
    });
}

function processResendActivation(url)
{
	showLayer('processUpdateReactionForm');

    var params = Form.serialize($('resendActivationForm'));
    new Ajax.Updater({success:'resendActivationDiv'}, url,
    {
        onComplete:function()
        {
            Element.hide('resendActivationDiv');
			hideLayer('processUpdateReactionForm');
            new Effect.Appear('resendActivationDiv');
            duration:0.1
        },
        asynchronous:true,
        parameters:params
    });
}

function showRegistrationForm(url)
{
	showLayer('processUpdateReactionForm');
	
    new Ajax.Updater({success:'registrationLoginDiv'}, url,
    {
        onComplete:function()
        {
            Element.hide('registrationLoginDiv');
			hideLayer('processUpdateReactionForm');
            new Effect.Appear('registrationLoginDiv');
            duration:0.1
        },
        method:'get',
        evalScripts:true,
        asynchronous:true
    });
}

function processUserAccountForm()
{
	showLayer('processUpdateReactionForm');
	
    var div = $('registrationLoginDiv');
    //document.insertReactionForm.submitreaction.disabled = true;
    var form = $('userAccountForm');
    var params = Form.serialize(form);
    new Ajax.Updater({success:div},
            form.action,
    {
        onComplete:function()
        {
            Element.hide(div);
			hideLayer('processUpdateReactionForm');
            new Effect.Appear(div);
            duration:0.1
        },
        asynchronous:true,
		evalScripts:true,
        parameters:params
    });
}

function showAccountForm(url)
{
	showLayer('processUpdateReactionForm');
	
    var div = $('accountDiv');
    new Ajax.Updater({success:div}, url,
    {
        onComplete:function()
        {
            Element.hide(div);
			hideLayer('processUpdateReactionForm');
            new Effect.Appear(div);
            duration:1.0
        },
        method:'get',
        evalScripts:true,
        asynchronous:true
    });
}

function processUserAccountUpdateUserDataForm()
{
	showLayer('processUpdateReactionForm');
	
    var div = $('updateDataDiv');
    var form = $('accountDataUpdateForm');
    var params = Form.serialize(form);
    new Ajax.Updater({success:div},
            form.action,
    {
        onComplete:function()
        {
            Element.hide(div);
			hideLayer('processUpdateReactionForm');
            new Effect.Appear(div);
            duration:0.1
        },
        asynchronous:true,
        evalScripts:true,
        parameters:params
    });
}

function processUserAccountUpdatePasswordForm()
{
	showLayer('processUpdateReactionForm');
	
    var div = $('updatePasswordDiv');
    var form = $('accountPasswordUpdateForm');
    var params = Form.serialize(form);
    new Ajax.Updater({success:div},
            form.action,
    {
        onComplete:function()
        {
            Element.hide(div);
			hideLayer('processUpdateReactionForm');
            new Effect.Appear(div);
            duration:0.1
        },
        asynchronous:true,
        evalScripts:true,
        parameters:params
    });
}

/*
 Gets executed when either the user account data or the user account password was succesfully updated.
 This will hide either the updateDate form or the updatePassword form
*/
function userAccountUpdateSuccessful() {
    var updateDataDiv = $('updateDataDiv');
    var updatePasswordDiv = $('updatePasswordDiv');
    if (updateDataDiv)
        updateDataDiv.style.display = "none";
    if (updatePasswordDiv)
        updatePasswordDiv.style.display = "none";
}

function changeLocationBelgiumStatus(inputFld) {
    var gemeenteDiv = $('gemeenteDiv');
    if (inputFld.id == 'belgium') {
        gemeenteDiv.style.display = "block";
    } else if (inputFld.id == 'other') {
        gemeenteDiv.style.display = "none";
    }
}

var geoItemsJSON;
var geoItems = new Array();
var geoItemIds = new Array();
var geoItemDescriptions = new Array();
var GeoItem = Class.create();
GeoItem.prototype = {
    initialize: function(nodeId, type, travelItem, description, parentDescription, level, postcode) {
        this.nodeId = nodeId;
        this.type = type;
        this.travelItem = travelItem;
        this.description = description;
        this.parentDescription = parentDescription;
        this.postcode = postcode;
    }
}


function createLocationAutoComplete(url){
    retrieveGeoItems(url);
/*
    var str = geoItemsJSON.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
    geoItems = eval('(' + str + ')');
    for (var i=0; geoItems.length;i++){
        geoItemDescriptions[i] = geoItems[i].description;
    }
*/
    new Autocompleter.Local('autocomplete', 'autocomplete_choices', geoItems , {minChars:1, afterUpdateElement : getSelectionId});
    
}

function retrieveGeoItems(url)
{
    new Ajax.Request(url, {
        method:'get',
        onComplete:function()
        {
            initializeAutoCompleteField();
        },
        evalScripts:true
    });
}

function initializeAutoCompleteField() {
    var locationId = $('locationId').value;

    if (locationId)
    {
        for (var i = 0; i < geoItemIds.length; i++)
        {
            if (locationId == geoItemIds[i])
            {
                $('autocomplete').value = geoItems[i];
            }
        }
    }
}

function getSelectionId(inputFld, li)
{
    for(var i=0;i<geoItems.length;i++)
    {
        if(inputFld.value == geoItems[i])
        {
            var form = inputFld.form;
            form.locationId.value=geoItemIds[i];                                                                                                                                
        }
    }
}

function checkCommunity()
{
    var correctCommunity = false;
    var community = $('autocomplete').value;

        for(var i=0;i<geoItems.length;i++)
        {
            if(community == geoItems[i])
            {
                correctCommunity = true;
            }
        }

    if(!correctCommunity)
    {
        alert('U moet een gemeente kiezen uit de lijst.');
    }

   return correctCommunity;
}
