function sendFlirt(username)
{
    var ajaxRequest = new ajaxObject('flirt/send/' + username, processSendFlirt);

    string = getFormValues('flirt_form');

    sendString = string;

    ajaxRequest.update(sendString, 'POST');
}

function processSendFlirt(responseText, responseStatus, responseXML)
{
    alert(responseText);
}

function changePhotoTitleChannel(image_id)
{
    var ajaxRequest = new ajaxObject('account/photo/photochangetitle/' + image_id, processChangePhotoTitleChannel);

    var title = document.getElementById('image_title_' + image_id).value;

    sendString = '&title=' + title;

    ajaxRequest.update(sendString, 'POST');
}

function processChangePhotoTitleChannel(responseText, responseStatus, responseXML)
{
    var splittedResponse = convertResponseToArray(responseText, ';');

    alert(splittedResponse[0]);
}

function changeVideoTitleChannel(video_id)
{
    var ajaxRequest = new ajaxObject('video/changetitle/' + video_id, processChangeVideoTitleChannel);

    var title = document.getElementById('video_title_' + video_id).value;
    sendString = '&title=' + title;

    ajaxRequest.update(sendString, 'POST');
}

function processChangeVideoTitleChannel(responseText, responseStatus, responseXML)
{
    var splittedResponse = convertResponseToArray(responseText, ';');
    alert(splittedResponse[0]);
}

function voteForStory(username, story_id)
{
    var submitUrl = 'story/' + username + '/' + story_id + '/vote';

    var ajaxRequest = new ajaxObject(submitUrl, processVoteForStory);

    sendString = getFormValues('story_vote');

    ajaxRequest.update(sendString, 'POST');
}

function processVoteForStory(responseText, responseStatus, responseXML)
{
    var splittedResponse = convertResponseToArray(responseText, '(_._)');

    document.getElementById('tr_rating').innerHTML = splittedResponse[1];

    alert(splittedResponse[2]);

    var tr_story = document.getElementById("tr_story_rating");
    tr_story.className = "tr_hidden";
}


function voteForPhoto(username, image_id)
{
    var submitUrl = 'photopage/' + username + '/' + image_id + '/vote';

    var ajaxRequest = new ajaxObject(submitUrl, processVoteForPhoto);

    sendString = getFormValues('photo_vote');

    ajaxRequest.update(sendString, 'POST');
}

function processVoteForPhoto(responseText, responseStatus, responseXML)
{
    var splittedResponse = convertResponseToArray(responseText, '(_._)');

    document.getElementById('tr_rating').innerHTML = splittedResponse[1];

    alert(splittedResponse[2]);

    var tr_photo = document.getElementById("tr_photo_rating");
    tr_photo.className = "tr_hidden";
}

function createBookmark(url, title)
{
    if (window.sidebar)
    {
        window.sidebar.addPanel(title, url, "");
    }
    else if (window.external)
    {
        window.external.AddFavorite(url, title);
    }
    else if (window.opera && window.print)
    {
        return true;
    }
}

function voteForVideo(username, video_id)
{
    var submitUrl = 'video/' + username + '/' + video_id + '/vote';

    var ajaxRequest = new ajaxObject(submitUrl, processVoteForVideo);

    sendString = getFormValues('photo_vote');

    ajaxRequest.update(sendString, 'POST');
}

function noenter(e)
{
    var keyCode = window.event ? window.event.keyCode : (e.which ? e.which : e.keyCode);
    return !(keyCode == 13);
}

function processVoteForVideo(responseText, responseStatus, responseXML)
{
    var splittedResponse = convertResponseToArray(responseText, ';');

    document.getElementById('tr_rating').innerHTML = splittedResponse[1];
    alert(splittedResponse[2]);

    var tr_photo = document.getElementById("tr_photo_rating");
    tr_photo.className = "tr_hidden";
}

//Plaats een reactie bij een foto
function addPhotoPageReaction(username, image_id)
{
    var submitUrl = 'photopage/' + username + '/' + image_id + '/new';

    var ajaxRequest = new ajaxObject(submitUrl, processAddPhotoPageReaction);

    sendString = getFormValues('photoCommentForm');

    ajaxRequest.update(sendString, 'POST');
}

function processAddPhotoPageReaction(responseText, responseStatus, responseXML)
{
    var splittedResponse = convertResponseToArray(responseText, ';');

    var photoCommentError = document.getElementById('photoCommentError');

    photoCommentError.innerHTML = splittedResponse[0];

    refreshImage_comment_form_validation(document.getElementById('comment_form_validation'));

    document.getElementById('photoCommentForm').reset();
}

//Do Search on site
function doSearch()
{
    string = getFormValues('search');

    sendString = string;

    var ajaxRequest = new ajaxObject('search/new', processDoSearch);

    ajaxRequest.update(sendString, 'POST');
}

function processDoSearch(responseText, responseStatus, responseXML)
{
    var text_div = document.getElementById('search_results');
    text_div.innerHTML = responseText;
}

//Send a message
function sendMessage()
{
    var site_submit_button = document.getElementById('site_submit_button');

    site_submit_button.value = 'Een moment geduld aub...';
    site_submit_button.disabled = true;

    var sendString = getFormValues('profile_message');

    var ajaxRequest = new ajaxObject('message/send', processMessage);

    ajaxRequest.update(sendString, 'POST');
}

function processMessage(responseText, responseStatus, responseXML)
{
    var splittedResponse = convertResponseToArray(responseText, ';');
    alert(splittedResponse[1]);

    if(splittedResponse[0] == 'success')
    {
        window.location = splittedResponse[2];
    }
    else
    {
        var site_submit_button = document.getElementById('site_submit_button');

        site_submit_button.value = 'Verstuur bericht';
        site_submit_button.disabled = false;
    }
}

// ADD TO FAVOURITES
var favoriteToChange;

function addToFavorites(user_name, img_object)
{
    sendString = 'account/flipfavorite/' + user_name;

    if (typeof(img_object) == 'string')
    {
        favoriteToChange = document.getElementById(img_object);
    }
    else
    {
        favoriteToChange = img_object;
    }

    var ajaxRequest = new ajaxObject(sendString, processAddToFavorites);

    ajaxRequest.update('', 'GET');
}

function processAddToFavorites(responseText, responseStatus, responseXML)
{
    var splittedResponse = convertResponseToArray(responseText, ';');
    alert(splittedResponse[1]);

    //TODO: change the alt of the picture
    if(splittedResponse[0])
    {
        favoriteToChange.src = rootURL + 'images/layout/delfavo.gif';
    }
    else
    {
        favoriteToChange.src = rootURL + 'images/layout/addfavo.gif';
    }
}

function convertResponseToArray(response, delimiter)
{
    return response.split(delimiter);
}

function showProfileWebcam(url, wnd)
{
    v_width = 800;
    v_height = 520;

    if (! wnd) wnd = window;

    var params = new Array(
                    'left=' + (screen.availWidth/2 - v_width/2),
                    'top=' + (screen.availHeight/2 - v_height/2),
                    'width=' + v_width,
                    'height=' + v_height,
                    'border=0',
                    'scrollbars=1',
                    'resize=0',
                    'status=0',
                    'toolbar=0',
                    'menubar=0',
                    'location=0'
                );
    wnd.open(url, 'profiel', params.join(','));
}

function insertAtCursor(myField, myValue)
{
    //IE support
    if (document.selection)
    {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }

    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0')
    {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
    }
    else
    {
        myField.value += myValue;
    }
}

function reverseString(str)
{
    var outputStr = '';
    for (i = 0; i < str.length; i++)
    {
        outputStr = str.charAt(i) + outputStr;
    }

    return outputStr;
}
