/* Utility functions for PressOffice */

var reLeadingBlanks=/^\s/;
var reTrailingBlanks=/\s$/;

/**
 * cbid: a checkbox id
 * Checks all checkboxes in the same form simultaneously to the
 * given checkbox
function checkLike(cbid) {
    var cb=document.getElementById(cbid);
 */
function checkLike(me) {
    var boo=me.checked;
    var foo=me.form;
    for (i=0; i<foo.elements.length; i++) {
        var e=foo.elements[i];
        if (e.type == "checkbox")
            if (e != me)
                e.checked = boo;
    }
}

/**
 * fidx: a form index
 * checks for all input elements of type input (for the given form)
 * if they have a non-empty value (after removing leading and trailing blanks)
 * and returns false, if any of the fields doesn't contain a value
 */
function assertStringFields(form) {
    for (i=0; i<form.elements.length; i++) {
        var e=form.elements[i];
        if (e.type == "string") {
            if (! _stringNotEmpty(e))
                return false;
        }
    }
    return true;
}

/*
 * fid: an id of an input field (of type string)
 */
function assertStringField(fid) {
    var e=document.getElementById(fid);
    return _stringNotEmpty(e);
}

/*
 * e: an element (of type string)
 */
function _stringNotEmpty(e) {
    var val=e.value;
    if (reLeadingBlanks.test(val) || reTrailingBlanks.test(val)) {
        val = val.replace(/^\s*(.*)\s*$/, '$1');
        e.value = val;
    }
    if (val == "") {
        e.focus();
        alert("Please enter a non-empty value here!");
        return false;
    }
    return true;
}

/**
 * assures that at least one significant checkbox of the form is checked;
 * usage: onsubmit="return assertSelection(this, '{excbid}')"
 * form:   the form 
 * excbid: the id of the checkbox to be ignored
 */
function assertSelection(form, excbid) {
    // var form=document.forms[fidx];
    var cb='';
    if (excbid != "")
        cb=document.getElementById(excbid);
    var boo=false;
    var sel=0;
    for (i=0; i<form.elements.length; i++) {
        var e=form.elements[i];
        if (e.type == "checkbox") {
            if (e != cb) {
                if (sel == 0)
                    sel = e;
                if (e.checked) {
                    return true;
                }
            }
        }
    }
    if (sel != 0) sel.focus();
    alert("You didn't select anything!");
    return false;
}

/**
 * iid: the id of an eMail input field
 */
function assertValidEmail(iid) {
    var input=document.getElementById(iid);
    var val=input.value;
    var re=/\w[a-zA-Z0-9_-]*(\.[a-zA-Z0-9_-]*)*@([a-zA-Z0-9_-]+\.)+\w+/;
    if (re.test(val)) {
        return true;
    }
    alert("eMail address "+ val+ " is not valid!");
    input.focus();
    return false;
}

function subscribeChecks(form) {
    if (assertStringFields(form) &&
        assertSelection(form,'checklikeme') &&
        assertValidEmail('remail'))
    {
        document.getElementById('srtype').value="subscribe";
        return true;
    }
    return false;
}

function unsubscribeChecks(form) {
    if (assertStringField('remail') &&
        assertSelection(form, 'checklikeme') &&
        assertValidEmail('remail'))
    {
        document.getElementById('srtype').value="unsubscribe";
        return true;
    }
    return false;
}

function subscribe(form) {
    document.getElementById('srtype').value = 'subscribe';
    form.submit();
}

function unsubscribe(form) {
    document.getElementById('srtype').value = 'unsubscribe';
    form.submit();
}

/*
 * for ...
 */
function dynamicFormCheck(form) {
    var val = document.getElementById('srtype').value;
    // alert("Aufruf von dynamicFormCheck(val="+val+")");
    if (val == 'subscribe')
        return subscribeChecks(form);
    else if (val == 'unsubscribe')
        return unsubscribeChecks(form);
    else
        return false;
}

/*
 * copies the value of the calling submit button
 * to the "chosen_action" field of its form
 */
function chooseAction(me) {
    // var form = me.form;
    // alert("chosen_action: "+ form.chosen_action.value+'\n'+ "me: "+me.value);
    me.form.chosen_action.value = me.value;
    return true;
}

/*
 * for Newsletter.edit_[un]sentLetter
 */
function dynamicCheckSendLetter(form) {
    // var val = document.getElementById('').value;
    var val = form.chosen_action.value;
    // alert("dynamicCheckSendLetter: form.action ist "+val);
    if (val == 'send') {
        return assertStringField('title') &&
               assertStringField('subject') &&
               assertStringField('from') &&
               assertSelection(form, 'ALL')
    }
    else if (val == 'test') {
        return assertStringFields(form)
    }
    else if (val == 'save')
        return true;
    else
        return false;
    
    /* Old version:
    //var val = form.action;
    alert("dynamicCheckSendLetter: form.action ist "+val);
    if (val == 'send_newsLetter') {
        alert("dynamicCheckSendLetter: send_newsLetter");
        return assertStringField('title') &&
               assertStringField('subject') &&
               assertStringField('from') &&
               assertSelection(form, 'ALL')
    }
    else if (val == 'test_newsLetter') {
        alert("dynamicCheckSendLetter: test_newsLetter");
        return assertStringFields(form)
    }
    else
        return false;
        */
}

/*
 * for buttons; usage:
 * onclick="setActionAndSubmit(this, 'theDesiredAction')"
 */
function setActionAndSubmit(me, act) {
    var myform = me.form;
    myform.action = act;
    alert("action von "+myform+" ist jetzt "+myform.action+". Submit:");
    myform.submit();
}

/*
 * helper function for localTime()
 */
function _00(a) {
    if (a < 10)
        return '0'+a;
    else
        return a;
}

/*
 * returns the current time in H:MM:SS format
 */
function localTime() {
    var jetzt = new Date();
    return jetzt.getHours()+':'+_00(jetzt.getMinutes())+':'+_00(jetzt.getSeconds());
}
// vi: sw=4 ts=4 expandtab noic cindent
