function goToHowItWorksPage()
{
	window.location = '/reisreporter/hoe_werkt_het.html';
}

function goToAboPage()
{
	window.location = '/abonnement.html';
}

function goToSignUpPage()
{
	window.location = '/reisreporter/inschrijven.html';
}

function goToOffersPage()
{
	window.location = '/abonnement.html';
}

function updateSearchFavReps(thisForm)
{
    new Ajax.Updater('searchFansWrapper', '/ajax_requests/search_favorite_rep.html', {asynchronous:true, parameters:Form.serialize(thisForm)});
}

function addAsFriend(user_id, username)
{
	if (confirm('Weet u zeker dat u ' + username + ' als favoriet wilt toevoegen aan uw profiel?'))
	{
		new Ajax.Request('/ajax_requests/add_favorite/' + user_id + '/reporter.html');

		alert('De gebruiker is toegevoegd aan uw profiel');
	}
}

function addAsFriend2(user_id, username)
{
	if (confirm('Weet u zeker dat u ' + username + ' als favoriet wilt toevoegen aan uw profiel?'))
	{
		new Ajax.Updater('myFavRepWrapper', '/ajax_requests/add_favorite/' + user_id + '/fan.html');

		alert('De gebruiker is toegevoegd aan uw profiel');
	}
}

function addFavoTip(user_id, title)
{
	if (confirm('Weet u zeker dat u de tip `' + title + '` als favoriet wilt toevoegen aan uw profiel?'))
	{
		new Ajax.Request('/ajax_requests/add_favorite/' + user_id + '/tip.html');

		alert('De tip is toegevoegd aan uw profiel');
	}
}

function addFavoPhoto(user_id, title)
{
	if (confirm('Weet u zeker dat u de foto `' + title + '` als favoriet wilt toevoegen aan uw profiel?'))
	{
		new Ajax.Request('/ajax_requests/add_favorite/' + user_id + '/photo.html');

		alert('De foto is toegevoegd aan uw profiel');
	}
}

function addFavoBlog(user_id, title)
{
	if (confirm('Weet u zeker dat u de blog `' + title + '` als favoriet wilt toevoegen aan uw profiel?'))
	{
		new Ajax.Request('/ajax_requests/add_favorite/' + user_id + '/blog.html');

		alert('De blog is toegevoegd aan uw profiel');
	}
}

function updateCitiesSelector()
{
    for(var i = 0; i < $('selectCountry').options.length; i++)
    {
        var curOpt = $('selectCountry').options[i];
        if(curOpt.selected == true)
        {
            new Ajax.Updater('selectCity', '/ajax_requests/return_cities_selector/' + curOpt.id + '.html');
        }
    }
}

function onlyForAbo()
{
	alert('Alleen voor Columbus abonnees.\nVul je volledige postcode en je abonneenummer in onder je profiel.\nDe functionaliteit wordt dan binnen 24 uur geactiveerd.\n\nGeen abonnee?\nGa dan naar www.columbusmagazine.nl/abonnement');
}

/***********************************************************************************************************************************************/
/*********************************************************************************************************************** Overlay ************/

function overlayAd(id)
{
        new Effect.Fade(id, {duration: 0.5});
}


var formFields = new Array();
var error = new Array();
var isError = '0';
var comment = '0';
var avatar = '0';
var currentTab = 'tab1';

/***********************************************************************************************************************************************/
/*********************************************************************************************************************** GEO SWITCH ************/

function changeGeoLocation(object)
{
    window.location = object.value;
}

/***********************************************************************************************************************************************/
/*********************************************************************************************************************** ALBUM FOOTER **********/

function newImages()
{
    new Effect.Fade('galleryContainerWrapper', {duration: 0.5});
    window.setTimeout('newAlbums()', 500);
    window.setTimeout('Effect.Appear(\'galleryContainerWrapper\', {duration:.5})', 800);
}

function newAlbums()
{
    new Ajax.Updater('galleryContainerWrapper', '/ajax_requests/more_albums/');
}


/***********************************************************************************************************************************************/
/*********************************************************************************************************************** HOMEPAGE **************/

function checkForGeodata(data1, data2)
{
    if (data2.id != '')
    {
        new Ajax.Updater('tip_geo_data', '/ajax_requests/set_tip_geodata/' + data2.id + '/');
    }
}

/***********************************************************************************************************************************************/
/*********************************************************************************************************************** HOMEPAGE **************/

function checkEditProfile(form)
{
    if ($('streamer').value.split(' ').length > 25)
    {
        alert('U kunt maximaal 25 woorden opgeven als reismotto.');
        return false;
    }
    if ($('description_field').value.split(' ').length > 200)
    {
        alert('U kunt maximaal 200 woorden opgeven als omschrijving.');
        return false;
    }

    doSubmit(form);

    return false;
}

function indexShowTab(tab)
{
    new Effect.Fade(currentTab, {duration: 0.5});
    window.setTimeout('Effect.Appear(\'' + tab + '\', {duration:.5})', 500);
    currentTab = tab;

    $('btn1').src = '/images/layout/tabMagazineOff.gif';
    $('btn2').src = '/images/layout/tabAbonneeOff.gif';
    $('btn3').src = '/images/layout/tabNieuwsbriefOff.gif';

    if (tab == 'tab1')
    {
        $('btn1').src = '/images/layout/tabMagazineOn.gif';
    }

    if (tab == 'tab2')
    {
        $('btn2').src = '/images/layout/tabAbonneeOn.gif';
    }

    if (tab == 'tab3')
    {
        $('btn3').src = '/images/layout/tabNieuwsbriefOn.gif';
    }

    return false;
}

/***********************************************************************************************************************************************/
/*********************************************************************************************************************** PROFILE ***************/

function avatarDiv()
{
    if (avatar == '0')
    {
        new Effect.SlideDown('profile_image', {duration: 0.5});
        avatar = '1';
    }
    else
    {
        new Effect.SlideUp('profile_image', {duration: 0.5});
        avatar = '0';
    }
}

function changeProfileComments(start)
{
//    ctrl = getBusyOverlay($('commentsWrapper'));
    new Ajax.Updater('commentsWrapper', '/ajax_requests/' + $('user_profile_id').value + '/' + start + '/profile_comments.html');
    return false;
}

/***********************************************************************************************************************************************/
/*********************************************************************************************************************** COMMENTAAR ************/

function commentDiv(id)
{
    if (comment == '0')
    {
        new Effect.SlideDown(id, {duration: 0.5});
        comment = '1';
    }
    else
    {
        new Effect.SlideUp(id, {duration: 0.5});
        comment = '0';
    }
}

function addProfileCommentMiez()
{
	var txt = $('comment_field').value;
    if ($('comment_field').value == '')
    {
        alert('U heeft geen reactie ingevoerd.');
    }
    else
    {
        if (txt.split(' ').length > 200)
        {
            alert('U heeft het maximaal aantal woorden overschreden');
        }
        else
        {
            new Ajax.Updater('commentsWrapper', '/ajax_requests/add_profile_comment.html', {asynchronous:true, parameters:Form.serialize($('profilecomment'))});
    		$('comment_field').value = '';
        }
    }
}

function addComment(id)
{
    new Ajax.Updater('userComments_' + id, '/ajax_requests/add_comment.html', {asynchronous:true, parameters:Form.serialize($('commentForm_' + id))});
    $('comment_' + id).value = '';
}

function addComment2(id, cid) // nieuw toegevoegd, reactie op een foto...
{
    var txt = $F(cid);
    if ($F(cid) == '')
    {
        alert('U heeft geen reactie ingevoerd.');
    }
    else
    {
        if (txt.split(' ').length > 250)
        {
            alert('U heeft het maximaal aantal woorden overschreden');
        }
        else
        {
            new Ajax.Updater('userComments_' + id, '/ajax_requests/add_comment.html', {asynchronous:true, parameters:Form.serialize($('commentForm_' + id))});
            $(cid).value = '';
        }
    }
}

/***********************************************************************************************************************************************/
/*********************************************************************************************************************** VERWERK FORMULIER *****/

function doLogin(thisForm)
{
    // do the same with a callback:
    //thisForm.request({onComplete: loginCompleteHandler})
    new Ajax.Request(thisForm.action, {method: 'post', postBody: Form.serialize(thisForm), onComplete: loginCompleteHandler});

    return false;
}

function doSubmit(thisForm)
{
    // do the same with a callback:
    //thisForm.request({onComplete: formCompleteHandler})
    new Ajax.Request(thisForm.action, {method: 'post', postBody: Form.serialize(thisForm), onComplete: formCompleteHandler});

    return false;
}

function formCompleteHandler(xml)
{
    doErrorHandling(xml);
}

function logOut()
{
	new Ajax.Request('/reisreporter/logOut.html', {onComplete: logOutComplete});
}

function logOutComplete()
{
    window.location.reload();
}

/***********************************************************************************************************************************************/
/************************************************************************************************************************** GLOBAL FUNCTIONS ***/

function loginCompleteHandler(xml)
{
    var xmlDoc = xml.responseXML.documentElement;
    var result = xmlDoc.getElementsByTagName("result");
    var urlLocation = getElementTextNS("", "link", result[0], 0);
    var status = getElementTextNS("", "description", result[0], 0);

    if (status == 'false')
    {
        $('globalErrorMessage').innerHTML = 'Inloggen mislukt';
        new Effect.Appear('globalErrorWrapper', { duration: 0.5 });
        window.setTimeout('Effect.Fade(\'globalErrorWrapper\', {duration:.5})', 5000);
    }
    else
    {
//    	var items = xmlDoc.getElementsByTagName("item");
//    	for (var i = 0; i < items.length; i++)
//        {
//        	if (getElementTextNS("", "fieldname", items[i], 0) == 'username')
//        	{
//        		$('user_name').innerHTML = getElementTextNS("", "message", items[i], 0);
//        	}
//        }
//    	$('notLoggedInBox').style.display = 'none';
//    	$('loggedInBox').style.display = 'block';
        //window.location.reload();
        window.location = urlLocation;
    }
}

function doErrorHandling(xml)
{
    $('errorMessage').innerHTML = '';

    for (var i = 0; i < formFields.length; i++)
    {
        if (formFields[i] != undefined || formFields[i] != '')
        {
            $(formFields[i].id).style.backgroundColor = '#ffffff';
        }
    }

    var xmlDoc = xml.responseXML.documentElement;
    var result = xmlDoc.getElementsByTagName("result");
    var status = getElementTextNS("", "description", result[0], 0);
    var urlLocation = getElementTextNS("", "link", result[0], 0);

    if (status == 'false')
    {
        var items = xmlDoc.getElementsByTagName("item");
        for (var i = 0; i < items.length; i++)
        {
            $('errorMessage').innerHTML = $('errorMessage').innerHTML + '<p id="errorMsg">' + getElementTextNS("", "message", items[i], 0) + '</p>';

            formField = $(getElementTextNS("", "fieldname", items[i], 0));

            formFields[i] = formField;

            if (formField != null)
            {
                formField.style.backgroundColor = '#c5181e';
            }
            else
            {
                alert(getElementTextNS("", "fieldname", items[i], 0) + ' bestaat niet');
            }
            formField = null;

        }

        isError = '1';
        new Effect.SlideDown('errorMessage', { duration: 0.5 });

        return false;
    }
    else
    {
        if (urlLocation != 'false')
        {
            window.location = urlLocation;
        }
    }
}

function getElementTextNS(prefix, local, parentElem, index)
{
    // "", "link", result[0], 0

    var result = "";
    if (prefix && isIE)
    {
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    }
    else
    {
        result = parentElem.getElementsByTagName(local)[index];
    }

    if (result)
    {
        if (result.childNodes.length > 1)
        {
            return result.childNodes[1].nodeValue;
        }
        else
        {
            return result.firstChild.nodeValue;
        }
    }
    else
    {
        return "n/a";
    }
}

function setError(error_message)
{
    if (error_message != '')
    {
        error[error.length] = error_message;
    }
}

function getErrorStatus()
{
    if (error.length > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function reloadImage(object)
{
    //img = document.getElementById('checkImg');
    object.src= '/image.php?id=' + Math.random()*1000

    return true;
}

var win = null;

function NewWindow(mypage, myname, w, h, scroll, pos)
{

    if (pos == "random")
    {
        LeftPosition = (screen.width) ? Math.floor(Math.random() * (screen.width-w)) : 100;
        TopPosition = (screen.height) ? Math.floor(Math.random() * ((screen.height-h)-75)) : 100;
    }
    if (pos == "center")
    {
        LeftPosition = (screen.width) ? (screen.width-w) / 2 : 100;
        TopPosition = (screen.height) ? (screen.height-h) / 2 : 100;
    }
    else if ((pos != "center" && pos != "random") || pos == null)
    {
        LeftPosition=0;
        TopPosition=20;
    }

    settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
    win = window.open(mypage,'',settings);
    return false;
}

function deleteFormText(form)
{
    form.setAttribute('onClick', null);
    form.onclick = null;
    form.value = '';

    return false;
}

function wordCount(field, wordCount)
{
    words = field.value.split(" ");

    if (words.length > wordCount)
    {
        alert('Het maximaal aantal woorden is: ' + wordCount);

        _length = 0;
        for (i=0;i<words.length;i++)
        {
            _length = _length + words[i].length + 1;
        }

        field.value = field.value.substr(0, _length);
    }
}

var mail = 0;

function mailafriend()
{
    if (mail == 0)
    {
        new Effect.SlideDown('sendMail', {duration: 0.5});
        mail = 1;
    }
    else
    {
        new Effect.SlideUp('sendMail', {duration: 0.5});
        mail = 0;
    }

    return false;
}

function sendMailAFriend()
{
    new Ajax.Updater('sendMail', '/ajax_requests/mailafriend/', {asynchronous:true, parameters:Form.serialize($('mailafriend'))});
    return false;
}

function mailafriend2(id)
{
    if (mail == 0)
    {
        new Effect.SlideDown(id, {duration: 0.5});
        mail = 1;
    }
    else
    {
        new Effect.SlideUp(id, {duration: 0.5});
        mail = 0;
    }

    return false;
}

function sendMailAFriend2(id)
{
    new Ajax.Updater(id, '/ajax_requests/mailafriend/', {asynchronous:true, parameters:Form.serialize($('mailafriend'))});
    return false;
}