$(function(){    
    supplierResetPass = function(prodID) {
        $('#supplierResetPassDiv').dialog('open');                                
    }
    
    $('#supplierResetPassDiv').dialog({
        modal: true,
        autoOpen: false,
        resizable: false,
        width:400,
        title: 'Reset Password',
        position: ['center',50],
        buttons: {
            'Cancel': function() {
                $(this).dialog('close');
            },
            'Send Confirmation': function() {
                if ($("#supplierResetPassFrm").valid()) {
                    resetFormSubmit();     
                } 
            }
        }
    });
    
    // validate signup form on keyup and submit
    $("#supplierResetPassFrm").validate({
        invalidHandler: function(form, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = "";
                if (errors==1) {
                    message = 'You missed a field. It has been highlighted';
                } else {
                    message = 'You missed ' + errors + ' fields. They have been highlighted';
                }
                $("#rp_errorDiv").html(message);
                $("#rp_errorDiv").addClass('val_error');
                $("#rp_errorDiv").show();
            } else {
                $("#rp_errorDiv").hide();
            }
        },
        success:function() {
            $("#rp_errorDiv").hide();    
        },
        errorPlacement: function(error, element) {},
        rules: { 
            supplierEmailRes: {
                required: true        
            }
        }
    });
    
    function resetFormSubmit() {
        $.ajax({
            type: 'POST',
            url: "/scripts/ajax_functions.php",
            data: {
                t: 'resetPassword',
                sm: $('#supplierEmailRes').val()
            },
            success: function(data) {
                if (!data.success) { 
                    alert('Failed to reset password.\n' + data.errors.message);        
                } else {                       
                    $('#supplierEmailRes').val('');
                    $('#supplierResetPassDiv').dialog('close');
                    alert('A confirmation email has been sent to you.\nPlease follow the instructions in the email.'); 
                }                                      
            },
            dataType: 'json'
        });      
    }
    
    supplierLoginFunc = function() {
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        email = $("#supplierEmail").val();
        
        if(!emailReg.test(email)) {
            alert("Email address is invalid!\nPlease make sure you login with Email and Password!");    
        } else {
            $("#supplierLoginFrm").submit();
        }                                   
    }  
});
