// -*- Java -*-

var POPUP_WINDOW_PARAMS = 'width=700,height=450,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes';

function http_request(url)
{
    var client;

    if (window.XMLHttpRequest) {
        client = new XMLHttpRequest();
    } else {
        client = new ActiveXObject("Microsoft.XMLHTTP");
    }

    client.open("http://lisp-ecoop07.bknr.net/GET", url, false);

    try {
        if (window.XMLHttpRequest) {
            client.send(null);
        } else {
            client.send();
        }
    } 
    catch (e) {
        debug('error sending request: ', e);
    }

    return client.responseXML;
}

/* cms support */

function check(button, checkboxname, b) {
  checkboxes = button.form[checkboxname];
  for (i = 0; i < checkboxes.length; i++) {
    checkboxes[i].checked = b;
  }
}

/* profile editor */

function check_password(form) {
    if (form.password.value != form.password_repeat.value) {
	alert('Please enter the same password twice');
	form.password.focus();
	return false;
    } else {
	return true;
    }
}

/* adding/removing submitters */

function submitters_window(url) {
    var submitters_window = open(url, "changesubmitters", POPUP_WINDOW_PARAMS);
    submitters_window.focus();
    return false;
}

/* Check upload parameters */

function check_document_upload()
{
    var info_input = document.getElementById('document-info-input');
    if (info_input.value == "") {
        alert("Missing document info");
        info_input.focus();
        return false;
    }

    return true;
}

// Open document upload window

function open_document_upload_window() {
    var object_id = parseInt(window.location.href.replace(/.*\/(\d+)/, "$1"));
    open('/upload-document/' + object_id, 'upload', POPUP_WINDOW_PARAMS);
    return false;
}

// Delete a document

function delete_document(id, info) {
    if (!confirm('Delete document "' + info + '" ?')) {
        return false;
    }

    http_request('/delete-document/' + id);

    window.location.reload();

    return true;
}

// Create Participant form check

function check_form_fields(form)
{
    var input_complete = true;
    var first_missing;
    for (var i = 1; i < arguments.length; i++) {
        var field = form[arguments[i]];
        if (field.value.match(/\S+/)) {
            field.style.background = '#fff';
        } else {
            input_complete = false;
            field.style.background = '#faa';
            if (!first_missing) {
                first_missing = field;
            }
        }
    }

    if (!input_complete) {
        first_missing.focus();
    }

    return input_complete;
}

function check_participant_form(form) {
    return check_form_fields(form, 'login', 'full-name', 'email');
}

// Make a new submission

function create_submission_window() {
    window.add_submission = function(id, title) {
        var submission_selector = document.getElementById('submission-selector');
        submission_selector.innerHTML = '<option selected="selected" value="' + id + '">' + title + '</option>' + submission_selector.innerHTML;
        submission_selector.selectedIndex = 0;
    }

    open('create-submission', 'createsubmission', POPUP_WINDOW_PARAMS);

    return false;
}

function check_create_submission_form(form) {
    return check_form_fields(form, 'title');
}

// Make new participant

function check_new_participant_form(form)
{
    var retval = check_form_fields(form, 'login', 'full-name', 'email');

    if (retval && !form['email'].value.match(/^\S+@\S+\.\S+$/)) {
        alert("invalid email address");
        form['email'].focus();
        return false;
    }

    return retval;
}
