function openChat()
{
    window.open("http://chat.privemailbox.nl/chatapp/chat","chat","width=962,height=700");
}

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)
{
    if(responseStatus == 200)
    {
        alert(responseText);

        //var splittedResponse = convertResponseToArray(responseText, ';');

        //alert(splittedResponse[0]);
    }
    else
    {
        alert(responseStatus + ' -- Error Processing Request');
    }
}

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)
{
    if(responseStatus == 200)
    {
        var splittedResponse = convertResponseToArray(responseText, ';');

        alert(splittedResponse[0]);
    }
    else
    {
        alert(responseStatus + ' -- Error Processing Request');
    }
}

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)
{
    if(responseStatus == 200)
    {
        var splittedResponse = convertResponseToArray(responseText, ';');

        alert(splittedResponse[0]);
        window.location.reload(true);
    }
    else
    {
        alert(responseStatus + ' -- Error Processing Request');
    }
}

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)
{
    if(responseStatus == 200)
    {
        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";

    }
    else
    {
        alert(responseStatus + ' -- Error Processing Request');
    }

}

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)
{
    if(responseStatus == 200)
    {
        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";

    }
    else
    {
        alert(responseStatus + ' -- Error Processing Request');
    }

}

//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)
{
    if(responseStatus == 200)
    {
        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();
    }
    else
    {
        alert(responseStatus + ' -- Error Processing Request');
    }
}

//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)
{
    if(responseStatus == 200)
    {
        var text_div = document.getElementById('search_results');
        text_div.innerHTML = responseText;
    }
    else
    {
        alert(responseStatus + ' -- Error Processing Request');
    }
}

//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 string = getFormValues('profile_message');
    var land = document.getElementsByName('land')[0].value;

    sendString = string + '&country=' + land;

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

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

function processMessage(responseText, responseStatus)
{
    if(responseStatus == 200)
    {
        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;
        }
    }
    else
    {
        alert(responseStatus + ' -- Error Processing Request');
    }
}

// ADD TO FAVOURITES
var favoriteToChange;

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

    favoriteToChange = img_object;

    var ajaxRequest = new ajaxObject(sendString, processAddToFavorites);

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

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

        //TODO: change the alt of the picture
        if(splittedResponse[0])
        {
            favoriteToChange.src = rootURL + 'images/nl/knoppen/knop_favo_verwijder.jpg';
        }
        else
        {
            favoriteToChange.src = rootURL + 'images/nl/knoppen/knop_favo_toevoegen.jpg';
        }
    }
    else
    {
        alert(responseStatus + ' -- Error Processing Request');
    }
}

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;
    }
}
