// ONLOAD

$(function() {
    initEnhancedTables();
    initFaq();

    attentionGrabber("#convention_meetings h2 a");
    attentionGrabber("#register_for_conventions h2 a");
    attentionGrabber("#events h2 a");
});

// FUNCTIONS

/*
 * Replaces the contents of 'selector' with a repeating
 * "New! > New! > New! > [Contents]" animation
 */
function attentionGrabber(selector) {
    var speed      = "normal";
    var container  = $(selector);

    if (container.length > 0) {
        var original   = container.html();

        container.css("display", "block");
        container.css("height", container.height());
        container.html("");

        $("<span class=\"new\">New!</span>")
            .appendTo(container)
            .hide()
            .fadeIn(speed)
            .fadeOut(speed, function() {
                container.css("text-align", "center");
            })
            .fadeIn(speed)
            .fadeOut(speed, function() {
                container.css("text-align", "right");
            })
            .fadeIn(speed)
            .fadeOut(speed, function() {
                container
                    .hide()
                    .css("text-align", "left")
                    .html(original)
                    .fadeIn(speed, function() {
                        window.setTimeout(function() {
                            attentionGrabber(selector)
                        }, 4000);
                    });
                $(this).remove();
            });
    }
}

/*
 * Add a class to even rows, and child cells, of enhancedtables
 */
function initEnhancedTables() {
    $("table.enhancedtable tr:even").addClass("row1");
    $("table.enhancedtable tr:even td").addClass("row1");
}

/*
 * Allow FAQ questions to toggle the display of answers
 */
function initFaq() {
    $("dl.faq dd").hide();
    $("dl.faq dt")
        .css("cursor", "pointer")
        .click(function() {
            $("~ dd", this).toggle();

            var state = ($("~ dd", this).css("display") == "block") ? "opened" : "closed";
            $(this).css("background-image", "url('images/bullet_" + state + ".png')");
        });
}

function calWin(f, e, t) {
    var height = t ? 230 : 200;
    window.open("admin/calendar.html", "calendar", "width=400,height=" + height + ",status=yes");
    dateField = document.forms[f].elements[e];
    if (!t){
        dateType = "date";
    } else {
        dateType = t;
    }
}

function show(t,e){
    divs=document.getElementsByTagName('div');
    for(i=0;i<divs.length;i++){
        if (divs[i].className.indexOf('hide')>-1) divs[i].style.display='none';
    }
    document.getElementById(e).style.display='block';
    if (document.getElementById(e+'_images')) document.getElementById(e+'_images').style.display='block';
    h2s = document.getElementsByTagName('h2');
    for (k=0;k<h2s.length;k++){
        if (h2s[k].className.indexOf('subnav') >=0){
            lnks=h2s[k].getElementsByTagName('a');
            if (lnks.length > 0){
                for(i=0;i<lnks.length;i++) lnks[i].style.color='#244d9d';
            }
        }
    }
    t.style.color='#c00';
}

/*
 * Toggle the display of an element
 */
function showHide(e) {
    $("#" + e).toggle();
}

function showCat(c){
    divs=document.getElementsByTagName('div');
    for(i=0;i<divs.length;i++) if (divs[i].className.indexOf('hide') >=0) divs[i].style.display='none';
    document.getElementById('cat'+c).style.display='block';
    as=document.getElementsByTagName('a');
    for(i=0;i<as.length;i++){
        if (as[i].className.indexOf('tabs') >= 0){
            as[i].className = 'tabs';
        }
    }
    if(document.getElementById('a_cat'+c))
        document.getElementById('a_cat'+c).className = 'tabs on';
    document.getElementById('a_cat'+c+'2').className = 'tabs on';

    /* Remove confirmation message */
    $(".intro.confirmation").remove();
}

function validate(p) {
    var e   = document.forms.thisform.elements;
    var str = '';

    if (e.dopublish) {
        e.dopublish.value = p;
    }
    if (e.name.value==''){
        str = str+'\nEnter a name for this request;';
    }
    if (e.location.value==''){
        str = str+'\nEnter a location for this request;';
    }
    if (e.department_number.value.length < 1) {
        str = str +'\nEnter a department number for this request;';
    }
    if (e.description.value==''){
        str = str+'\nEnter a meeting overview for this request;';
    }
    if (e.room_count.value==''){
        str = str+'\nEnter the number of people for this request;';
    }
    if (e.owner.value==''){
        str = str+'\nEnter your name for this request;';
    }
    if (e.address.value==''){
        str = str+'\nEnter your address for this request;';
    }
    if (e.city.value==''){
        str = str+'\nEnter your city for this request;';
    }
    if (e.province.value==''){
        str = str+'\nSelect your state or province for this request;';
    }
    if (e.country.value==''){
        str = str+'\nEnter your country for this request;';
    }
    if (e.postalcode.value==''){
        str = str+'\nEnter your ZIP/postal code for this request;';
    }
    if (e.email.value==''){
        str = str+'\nEnter your email address for this request;';
    }
    if (e.phone.value==''){
        str = str+'\nEnter your telephone number for this request;';
    }
    if (e.fax.value==''){
        str = str+'\nEnter your fax number for this request;';
    }
    str += validate_questions(e);

    if (str.length > 0) {
        return (confirm('Your request is incomplete; you should:\n'+ str +'\n\nAre you sure you want to save the request now?'));
    }
}

function validate_radiobutton(element) {
    for (i=0; i<element.length; i++) {
        if (element[i].checked) {
            return element[i].value;
        }
    }
    return "";
}

function validate_meeting_request(tab){
    var e      = document.forms.thisform.elements;
    var str    = '';
    if(tab == "Meeting_Request" || tab == "All"){
        if (e.convention.value==''){
            str = str+'\nEnter a convention for this request;';
        }
        if (e.company.value==''){
            str = str+'\nEnter a company name for this request;';
        }
        if (e.department_number.value == '') {
            str = str +'\nEnter a department number for this request;';
        }
    }
    if ('Base' == tab || 'All' == tab) {
        if (e.meeting_date.value == '') {
            str = str+'\nEnter a meeting date for this request;';
        }

        /* Meeting Times */

        var start_hr    = $(":input[name='start_time_hr']:not([disabled])").val();
        var start_mn    = $(":input[name='start_time_min']:not([disabled])").val();
        var start_ampm  = $(":input[name='start_time_ampm']:not([disabled])").val();

        start_hr = parseInt(start_hr) % 12;
        if ('pm' == start_ampm) {
            start_hr = start_hr + 12;
        }

        var start_time  = new Date();
        start_time.setHours(start_hr);
        start_time.setMinutes(start_mn);
        start_time.setSeconds(0);

        var end_hr      = $(":input[name='finish_time_hr']:not([disabled])").val();
        var end_mn      = $(":input[name='finish_time_min']:not([disabled])").val();
        var end_ampm    = $(":input[name='finish_time_ampm']:not([disabled])").val();

        end_hr = parseInt(end_hr) % 12;
        if ('pm' == end_ampm) {
            end_hr = end_hr + 12;
        }

        var end_time  = new Date();
        end_time.setHours(end_hr);
        end_time.setMinutes(end_mn);
        end_time.setSeconds(0);

        if (end_time <= start_time) {
            str += "\nPlease select a Finish Time that occurs after your Start Time;";
        }

        /* Equipment-AV */

        if (validate_radiobutton(e.equipment) == "") {
            str = str+'\nPlease fill out Equipment/AV set-up required?';
        }
        var equipment_required = ($(":input[name='equipment'][checked]").val() == "Yes");
        if (equipment_required) {
            var equipment_option_chosen = ($(":input[name='equipment_details[]'][checked]").length > 0);
            if (!equipment_option_chosen) {
                str += "\nPlease select Equipment/AV options;";
            }

            var equipment_other_chosen  = ($(":input[name='equipment_details[]'][checked]:last").val() == 'other');
            var equipment_other_value   = ($(":input[name='equipment_details[]'][type='text']").val().length > 0);
            if (equipment_other_chosen && !equipment_other_value) {
                str += "\nPlease enter a value for the Equipment/AV Other option;";
            }
        }

        /* Food & Beverage */

        if (validate_radiobutton(e.food) == ""){
            str = str+'\nPlease fill out Food & Beverage Required?';
        }
        var food_required = ($(":input[name='food'][checked]").val() == "Yes");
        if (food_required) {
            var food_option_chosen = ($(":input[name='food_details[]'][checked]").length > 0);
            if (!food_option_chosen) {
                str += "\nPlease select Food & Beverage options;";
            }
        }
    }
    if ('Host' == tab || 'All' == tab) {
        if (e.host_name.value.length < 1) {
            str = str + "\nEnter the name of the onsite host;";
        }
        if (e.host_email.value.length < 1) {
            str = str + "\nEnter the email address of the onsite host;";
        } else {
            var isEmail = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[_a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/i.test(e.host_email.value);
            if (!isEmail) {
                str = str + "\nEnter a valid email address for the onsite host;";
            }
        }
        if (e.host_cellphone.value.length < 1) {
            str = str + "\nEnter the cell phone number of the onsite host;";
        }
        if (e.host_company.value.length < 1) {
            str = str + "\nEnter the company of the onsite host;";
        }
    }
    if(tab == "Owner" || tab == "All"){
        if (e.owner.value==''){
            str = str+'\nEnter your name for this request;';
        }
        if (e.email.value==''){
            str = str+'\nEnter your email address for this request;';
        }
        if (e.phone.value==''){
            str = str+'\nEnter your telephone number for this request;';
        }
        if (e.fax.value==''){
            str = str+'\nEnter your fax number for this request;';
        }
    }
    if (str != ''){
        alert('Your request is incomplete; you should:\n'+str+'\n\n');
        return false;
    }
    return true;
}

/*
 * Auto-fill the location when a convention is selected.
 * Locations array is built in-page.
 */
function ConventionChanged(locations) {
    var meeting_id          = $(":input[name='convention']").val();
    var meeting_location    = locations[meeting_id];

    if (meeting_location) {
        if (meeting_location.length < 1) {
            meeting_location = "";
        }

        $(":input[name='location']").val(meeting_location);
    }
}
