function getElement(id) {
    return document.getElementById(id);
}

var fade_busy = false;

// fade script for homepage banners
function opacity(id, opacStart, opacEnd, millisec) {
    
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
        
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            if(i != 100) {
                fade_busy = true;
            }
            else {
                fade_busy = false;
            }
            timer++;
        }
    }
    
}

function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

var current_thumbnail_link = null;
var current_banner_id = '1';
var num_banners = 0;

function showBanner(thumbnail_link, id, click) {

    counter = id;

    hideAllBanners();
    getElement('preview_' + id).style.display = 'inline';
    opacity('preview_' + id, 0, 100, 3000);

    if(current_thumbnail_link != null)
        current_thumbnail_link.className = '';
        
    thumbnail_link.className = 'active';
    current_thumbnail_link = thumbnail_link;
    
    if(click) {
        rotate = false;
        getElement('rotate_play').style.display = 'inline'
    }

}

function bannerLoad() {
    hideAllBanners();
    //showFirstBanner();
    rotateBanners();
}

function hideBanner(id) {
    changeOpac(0, id);
}

function hideAllBanners() {
    
    for(var i=1; i <=num_banners; i++) {
        getElement('preview_' + i).style.display = 'none';
        changeOpac(0, 'preview_' + i);
    }
    
}

function showFirstBanner() {
    getElement('preview_01').style.display = 'inline';
    opacity('preview_01', 0, 100, 3000);
}

var rotate = true;

function rotateBanners() {

    bannerRotate();

}

var counter = 1;

function bannerRotate() {
    
    var banner_time = 0;
    
    if(rotate) {
        if(counter > num_banners)
            counter = 1;
        showBanner(getElement('thumb_' + counter), counter);
        banner_time = parseInt(getElement('thumb_' + counter).title) * 1000;
        counter++;
    }
        
    setTimeout('bannerRotate()', banner_time);
    
}

function startRotate() {
    counter++;
    rotate = true;
    getElement('rotate_play').style.display = 'none'
}

function validateSubscribe(form) {
    
    var valid = false;
    
    if(form.firstname.value.length == 0) {
        alert('Please supply your first name');
        form.firstname.focus();
    }
    else if(!isValidEmail(form.email_address.value)) {
        alert('Please supply a valid email address');
        form.email_address.focus();
    }
    else if(form.country_code.value == '') {
        alert('Please select your country');
        form.country_code.focus();
    }
    else {
        valid = true;
    }
    
    return valid;
    
}

function validateContactForm(form) {
    
    var valid = false;
    
    if(form.name.value == '') {
        alert('Please supply your name.');
        form.name.focus();
    }
    else if(!isValidEmail(form.email.value)) {
        alert('Please supply a valid email address');
        form.email.focus();
    }
    else if(form.message.value == '') {
        alert('Please supply your message.');
        form.message.focus();
    }
    else {
        valid = true;
    }
    
    return valid;
    
}

function isValidEmail(email) {
    
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
    if (filter.test(email))
        return true;
    else
        return false;
}

function validateDTSApplication(form) {
    
    var valid = false;
    
    if(form.name.value == "") {
        
        alert("Please fill in your full name.");
        form.name.focus();
        
    }
    else if((form.dob_day.value == "") || (form.dob_month.value == "") || (form.dob_year.value == "")) {
        
        alert("Please fill in your full date of birth.");
        form.dob_day.focus();
        
    }
    else if(form.residential_address.value.length <  10) {
    
        alert("Please fill in your residential address.");
        form.residential_address.focus();
        
    }
    else if((form.home_phone.value.length < 7) && (form.work_phone.value.length < 7) && (form.cell_phone.value.length < 7)) {
        
        alert("Please fill in at least one contact number.");
        form.cell_phone.focus();
        
    }
    else if(!isValidEmail(form.email_address.value)) {
        
        alert("Please supply a valid email address.");
        form.email_address.focus();
        
    }
    else if(!allDTSQuestionsAnswered(form)) {
        
        alert("Please answer all questions.");
        
    }
    else if(!form.agree.checked) {
        
        alert("Please read 'MY COMMITMENT' and check 'I agree to this commitment' to submit your application.");
        
    }
    else {
        
        valid = true;
        
    }
    
    return valid;
    
}

function allDTSQuestionsAnswered(form) {
    
    var num_questions = 18;
    
    
    for(var i = 1; i <= 18; i++) {
        
        var question = getElement('q' + i);
        
        if(question.value.length < 2) {
            question.focus();
            return false;
        }
        
    }
    
    return true;
    
}


function paypal_button_over(button) {
    
    button.src = "/images/online_giving/" + button.id + "_f2.jpg";
    
}

function paypal_button_out(button) {
    
    button.src = "/images/online_giving/" + button.id + ".jpg";
    
}
