// these are variables that need to be set on the page after this script is loaded, but before page load finishes
var hfID; //ID of the server side hidden field control that holds the response message from a failed login
var txtUsernameID; //ID of the server side username text field
var txtPasswordID; //Id of the server side password text field
var cmdSubmitID; //ID of the sever side button that logs the user in
$(function(){
    var hf = $('#' + hfID);
    var txtUN = $('#' + txtUsernameID);
    var txtPWD = $('#' + txtPasswordID);
    var cmd = $('#' + cmdSubmitID);
    if (hf.val() != '') {
        $(function(){
            var txt = $('#' + txtUsernameID);
            txt.tooltip().create({
                static: true,
                showTail: false,
                content: function(){
                    return hf.val();
                },
                css: {
                    border: '2px solid #efefef',
                    background: '#ff0000',
                    font: 'bold normal 8pt segoe ui, arial, helvetica',
                    padding: '10px',
                    color: 'white'
                },
                relativeLeft: -214,
                relativeTop: -130
            });
            
            var tt = txt.tooltip().get();
            var flashInt_i = 0;
            var flashInt = setInterval(function(){
                if (flashInt_i % 2 == 0) {
                    tt.fadeTo(75, .5);
                }
                else {
                    tt.fadeTo(75, 1);
                }
                if (++flashInt_i > 5) 
                    clearInterval(flashInt);
            }, 250);
            
            txt.tooltip().get().click(function(){
                $(this).remove();
            });
            
            txt.focus(function(){
                txt.tooltip().get().remove();
            });
            
            $('#' + txtPasswordID).focus(function(){
                txt.tooltip().get().remove();
            });
        });
    }
    
    $('#' + cmdSubmitID).click(function(){
        var val_invalid = true;
        if (txtUN.val() == '') {
            var un_validation_i = 0;
            var un_validationInterval = setInterval(function(){
                if (un_validation_i % 2 == 0) 
                    txtUN.css('background', '#ff0000');
                else 
                    txtUN.css('background', '#f7fd55');
                
                if (++un_validation_i > 3) 
                    clearInterval(un_validationInterval);
            }, 100);
            txtUN.blur(function(){
                if ($(this).val() != '') {
                    $(this).css('background', 'white');
                }
            });
            val_invalid = false;
        }
        
        if (txtPWD.val() == '') {
            var pwd_validation_i = 0;
            var pwd_validationInterval = setInterval(function(){
                if (pwd_validation_i % 2 == 0) 
                    txtPWD.css('background', '#ff0000');
                else 
                    txtPWD.css('background', '#f7fd55');
                
                if (++pwd_validation_i > 3) 
                    clearInterval(pwd_validationInterval);
            }, 100);
            txtPWD.blur(function(){
                if ($(this).val() != '') {
                    $(this).css('background', 'white');
                }
            });
            val_invalid = false;
        }
        return val_invalid;
    });
});
