﻿function setPosition(table, divLoader) {
    $("#" + divLoader).css($("#" + table).offset());
    $("#" + divLoader).css("width", $("#" + table).width() + "px");
    $("#" + divLoader).css("height", $("#" + table).height() + "px");
}

function coverPage(divLoader) {
    var documentHeight = document.height ? document.height : document.body.clientHeight;
    var documentWidth = document.width ? document.width : document.body.clientWidth;

    $("#" + divLoader).css("width", documentWidth + "px");
    $("#" + divLoader).css("height", documentHeight + "px");
}

function centerToCurrentView(controlId) {
    var documentHeight = document.height ? document.height : document.body.clientHeight;
    var documentWidth = document.width ? document.width : document.body.clientWidth;

    var scrollTop = $(window).scrollTop();
    var windowHeight = $(window).height();

    var control = $('#' + controlId);
    control.css("left", ((documentWidth - control.width()) / 2) + 'px');
    control.css("top", (scrollTop + (windowHeight - control.height()) / 2) + 'px');
}

function scrollToControl(control) {
    setTimeout("window.scrollTo(0, $('#" + control + "').offset().top)", 0);
}

function clearText(control, emptyText, maxlength) {
    if (control.value == emptyText) {
        control.style.textTransform = 'uppercase';
        control.style.fontStyle = 'normal';
        control.style.color = '';
        
        control.value = '';
        control.maxLength = maxlength;
    }
}

function setEmptyText(control, emptyText) {
    if (control.value == '') {
        control.style.textTransform = 'none';
        control.style.fontStyle = 'italic';
        control.style.color = '#999999';
        control.setAttribute('maxLength', emptyText.length);
        
        control.value = emptyText;
    }
}

function displayMembers(value) {
    var count = parseInt(value);
    if (count == NaN || count < 1 || count > 8) {
        count = 0;
    }
    
    for (var i = 1; i <= 8; i++) {
        var member = $('#trMember' + i);
        if (i <= count) {
            member.show();
        }
        else {
            member.hide();
        }
    }
}

function displaySmoker(id, value) {
    var age = parseInt(value);
    if(age == NaN) {
        return;
    }

    var smoker = $("#tableSmoker" + id);
    var smokerLabel = $("#lbSmoker" + id);
    var smokerDiv = $("#divSmoker" + id);
    if (age >= 18) {
        smoker.show();
        smokerLabel.show();
        smokerDiv.show();
    }
    else {
        smoker.hide();
        smokerLabel.hide();
        smokerDiv.hide();
    }
}

function switchSEProfessional(senderId, targetId) {
    var senderSwitch = $('#[id $= ' + senderId + ']');
    var targetSwitch = $('#[id $= ' + targetId + ']');
    if (senderSwitch.attr('checked') == true && targetSwitch.attr('checked') == true) {
        targetSwitch.attr('checked', false).change();
    }
}

function addRemoveExtraNHS(id, add, prefix) {
    if ($('#' + prefix + 'lbExtra' + id + '_1').html().length == 0) {
        return;
    }

    if (add) {
        $('#' + prefix + 'trAddExtra' + id).hide();
        $('#' + prefix + 'trRemoveExtra' + id).show();

        var found = false;
        $('#[id ^= ' + prefix + 'trAddExtra]').each(function (index) {
            if ($(this).css('display') != 'none') {
                found = true;
                return;
            }
        });

        if (!found) {
            $('#' + prefix + 'trNoExtras').show();
            $('#' + prefix + 'trHeaderExtras').hide();
        }
    }
    else {
        $('#' + prefix + 'trAddExtra' + id).show();
        $('#' + prefix + 'trRemoveExtra' + id).hide();

        $('#' + prefix + 'trNoExtras').hide();
        $('#' + prefix + 'trHeaderExtras').show();
    }

    __doPostBack(prefix.replace(/_/g, '$') + 'aExtra_' + id, '');
}

var maxAdults = 7;

function displaySmokerDiv(count) {
    coverPage('divCover');
    centerToCurrentView('divSmoker');

    $('#divCover').show();
    $('#divSmoker').show();

    setTimeout(function () {
        for (var i = 0; i < count; i++) {
            displaySmoker(i + 1, 18);
        }

        for (var i = count; i < maxAdults; i++) {
            displaySmoker(i + 1, 0);
        }
    }, 1);
}
