﻿$(document).ready(function() {
    $('#email-fullname').activateFormReplace('focus', 'FULL NAME');
    $('#email-email').activateFormReplace('focus', 'EMAIL');
    $('#email-telno').activateFormReplace('focus', 'TELEPHONE NO.');

    $('#email-btn').click(function() {
        var subscribe = $('div.right-accept img[name=subscribe-btn]').attr("class") == "on";
        var contact = $('div.right-accept img[name=contact-btn]').attr("class") == "on";
        var exids = listOfExIds();
        if (exids == "") {
            alert('No exercises selected');
            return false;
        }

        var options = { 'exids': exids, 'from': $('#email-fullname').val(), 'email': $('#email-email').val(), 'tel': $('#email-telno').val(), 'subscribe': subscribe, 'contact': contact };

        if (options.from == $('#email-fullname').data('replaceOldVal') || options.from == "") {
            alert('Please enter your name');
            return false;
        }
        if (options.email == $('#email-email').data('replaceOldVal') || options.email == "") {
            alert('Please enter your email');
            return false;
        }

        if (options.contact && (options.tel == $('#email-telno').data('replaceOldVal') || options.tel == "")) {
            alert("Please enter a telephone number");
            return false;
        }

        if (options.email.indexOf("@") == -1) {
            alert('Please enter a valid email');
            return false;
        }


        $.get("/resources/email-exercises.aspx", options, function(data) {
            alert('The details of these exercises have been emailed to you successfully');
        });
        return false;
    });


    $('div.right-accept a').click(function() {
        var img = $('img', $(this));

        if (img.hasClass("on")) {
            img.attr("src", "images/search/decline.png").attr("class", "off").attr("alt", "No");
        } else {
            img.attr("src", "images/search/accept.png").attr("class", "on").attr("alt", "Yes");
        }
    });

    $('a#emailtab-btn').click(function() {
        var panel = $('div#hideable-email-panel');
        if (panel.css("display") == "none") {
            panel.show();
            $('img', $('a#emailtab-btn')).attr('src', "/images/emailtabCLOSE.png");
        } else {
            panel.slideUp(200, function() {
                $('img', $('a#emailtab-btn')).attr('src', "/images/emailtabOPEN.png");
            });
        }
    });

});
