
$(document).ready(function () {
    $('#MsgText').maxlength({
        events: [], //Array of events to be triggered
        maxCharacters: 32000,
        status: false, // True to show status indicator below the element
        statusClass: "remainingCount", // The class on the status div
        statusText: "/32000", // The status text
        notificationClass: "", // the class added when maxlength is reached
        showAlert: false, // true to show a regular alert message
        alertText: "Cannot enter more than 32000 characters", // text in alert message
        slider: false // true use counter slider
    });
    $('#MsgText').attr("maxlength", "32000");
    // Disable enter from submitting the form

    // Disable enter from submitting the form
    $('#MsgText').on('keypress', function (e) {		// AX-24898.24919.2 .keypress(function (e) {
        //enableSubmitButton(current, true);

        // reset idle timer to not show logout warning dialog while typing
        window.parent.document.getElementById('resetAutoLogoutTimer').click();

        var max = 32000;
        var strokeValue = 0;

        // current selection
        var value = $(this).val();
        var currentSelection = getSelection(this);

        var range = currentSelection.end - currentSelection.start;

        // current expected length
        var lineBreakCount = (value.match(/\n/g) || []).length;
        var expectedLength = value.length + lineBreakCount - range;

        // onKeyDown and onKeyPress has different keyCode
        // this case, we are using keypress, which listens to all keys that outputs data
        // we only need to listen if the key stroke is an enter or backspace (FireFox behaviour),
        // to assign extra value to it

        if (e.which > 46)       // Regular keys
            strokeValue = 1;
        else if (e.which == 13) // Enter
            strokeValue = 2;
        else if (e.which == 32) // Spacebar
            strokeValue = 1;


        if (strokeValue == 0 || expectedLength + strokeValue <= max)
            return true;

        e.preventDefault();

        if (strokeValue == 2 && expectedLength + 1 <= max) {

            // replace enter with a space
            var currentPos = $(this).scrollTop();

            var formatText = value.substr(0, currentSelection.start) + " " + value.substr(currentSelection.end);
            $(this).val(formatText);
            $(this).scrollTop(currentPos);

            setSelection(this, currentSelection.start + 1, 0);
        }
        return false;
    });

    // there are cases where backspace is not registered as part of "on input" event in IE
    // this keyup handles the specific case to trigger validation
     //$('#MsgText').keyup(function (e) {
     //   // backspace
     //   if (e.which == 8) {
     //          validateLength($(this).val(), 32000); //validateLongText(this);
     //   }
     //});

    // Create validation and update of submit button
     //$('#MsgText').on("input", function () {
     //    validateLength($(this).val(), 32000); //validateLongText(this);
     //});


    $('#SubjectTxt').maxlength({
        events: [], //Array of events to be triggered
        maxCharacters: 50,
        status: false, // True to show status indicator below the element
        statusClass: "remainingCount", // The class on the status div
        statusText: "/50", // The status text
        notificationClass: "", // the class added when maxlength is reached
        showAlert: false, // true to show a regular alert message
        alertText: "Cannot enter more than 50 characters", // text in alert message
        slider: false // true use counter slider
    });
    $('#SubjectTxt').attr("maxlength", "50");
    // Disable enter from submitting the form
    $('#SubjectTxt').on('keypress', function (e) {		// AX-24898.24919.2 .keypress(function (e) {
        disableEnter(e);

        // reset idle timer to not show logout warning dialog while typing
        window.parent.document.getElementById('resetAutoLogoutTimer').click();
    });
});


function initPage() {
    resizeParentIframe('messageComposePage', window);
    scrollParentToTop();
}

function scrollDown() {
    var msgText = document.getElementById("MsgText");
    //if (/msgText.doScroll())
    //msgText.doScroll("scrollbarDown");
    //else
    msgText.scrollTop += 10;
}
function scrollUp() {
    var msgText = document.getElementById("MsgText");
    msgText.scrollTop -= 10;
    //msgText.doScroll("scrollbarUp");
}

function valEmptyFields() {
    var errorHeader;
    var errorMessage;
    if ($('#ToList').val().length == 0) {
        errorHeader = "To field is empty.";
        errorMessage = "Please make sure To field is not empty.";

        getParentWindow(window).createErrorWindow(errorHeader, errorMessage);
        return false;
    }

    if ($('#SubjectTxt').val().length == 0) {
        errorHeader = "Subject field is empty.";
        errorMessage = "Please make sure Subject field is not empty.";

        getParentWindow(window).createErrorWindow(errorHeader, errorMessage);
        return false;
    }
    return true;
}

function openMessageContactPopup() {
    
    var action = 'N';   // No set
    if ($('#hdAction').text().length > 0)
        action = $('#hdAction').text()[0];
    
    var pageTypeId = -1;
    if ($('#hdPageTypeId').text().length > 0)
        pageTypeId = $('#hdPageTypeId').text();

    var messageId = $('#hdnMessageId').val();
    var toIds = $('#hdnToIds').val();
    var ccIds = $('#hdnCcIds').val();
    var avIds = $('#hdnIdsOnlyAvail').val();

    var openUrl = '/PatientAccess/Messages/MessageContacts.aspx?' +
        'tid=' + toIds + '&' +
        'cid=' + ccIds + '&' +
        'avid=' + avIds + '&' +
        'act=' + action + '&' +
        'pti=' + pageTypeId + '&' +
        'c=' + messageId;
    
    openPopup(openUrl, 'windowMedium', 'windowMedium');
}

// remove focus from textbox control to avoid to type info in IE and not popup keyword in iPad
function removeFocus(value) {
    $(value).trigger('blur');					// AX-24898.24919.2 .blur();
}
