// JavaScript Document
function isblank(s) {
    for (var i = 0; i < s.value.length; i++) {
        var c = s.value.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}
function validate(obj) {
    if (isblank(obj.comment)) {
        alert("Please enter some text.");
        obj.comment.focus();
        return false;
    }
    return true;
}
function validateEmail(obj) {
    if (isblank(obj.name)) {
        alert("Please enter your name.");
        obj.name.focus();
        return false;
    }
    if (isblank(obj.recipient)) {
        alert("Please enter recipient's email.");
        obj.recipient.focus();
        return false;
    }
    if (!isblank(obj.recipient)) {
        var valid = true; // Initially set valid to true
        var atcount = 0;
        var email = obj.recipient.value;
        var email_length = obj.recipient.value.length;

        for (index = 0; index < email_length; index++) {
            if ((email.substring(index, index + 1) == ":") || (email.substring(index, index + 1) == ",") || (email.substring(index, index + 1) == "'") || (email.substring(index, index + 1) == " ")) {
                valid = false;
            }
        }

        for (i = 0; i < email_length; i++) {
            if (email.substring(i, i + 1) == "@") {++atcount;
                if (email.substring(i - 1, i) == ".") valid = false;
                if (email.substring(i + 1, i + 2) == ".") valid = false;
            }
        }
        if (email.substring(email_length - 1, email_length) == "@") valid = false;
        if (email.substring(email_length - 1, email_length) == ".") valid = false;
        if (email.substring(0, 1) == "@") valid = false;
        if (email.substring(0, 1) == ".") valid = false;
        if (obj.recipient.value.indexOf(".") == -1) valid = false;
        if (atcount > 1 || atcount == 0) valid = false;

        if (!valid) {
            alert("Please include a proper email address (Generally in the form 'username@hostname.domain').");
            obj.recipient.focus();
            return false;
        }
    }
    return true;
}
function subscriberLinks() {
    links = $('articleBottom').getElementsBySelector('a');
    for (i = 0; i < links.length; i++) {
        links[i].href = "/register";
    }
    $('commentform').action = "/register";
    $('rateup').href = "/register";
    $('ratedown').href = "/register";
}
function columnize() {
    multiColumnSettings = new MultiColumnSettings;
    multiColumnSettings.classNameScreen = 'screenContainer';
    multiColumnSettings.extraHeight = 10;
    multiColumnSettings.numberOfColumns = 2;
    new MultiColumn(document.getElementById('columns'), multiColumnSettings);
}
function printIt(ID) {
    window.open('/print-article.php?ID=' + ID, 'printwin', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
}

function sendEmail() {
    if (validateEmail($('email-form'))) {

        var url = '/process.php?t=email-article';
        var pars = $('email-form').serialize();
        var target = 'email_result';
        $('email_result').show();
        $('email_result').innerHTML = 'Sending email...';
        var myAjax = new Ajax.Updater(target, url, {
            method: 'get',
            parameters: pars,
            onSuccess: function() {
                setTimeout(up, 4000);
            }
        });
    }
}
function up() {
    Effect.BlindUp('emailBox', {
        duration: 0.5
    })
}
