﻿var message = 

$(function() {

    var $dialog = $('<div id="popup-forgot-password"></div>')
		.html('<div id="popup-forgot-password-bottom"><div id="popup-forgot-password-top"><div id="forgotPasswordCancel">close</div><div id="forgotPasswordMessage"></div><div id=\"EmailInput\"><p>Enter your email address below and we’ll send you information on how to log in</p>Email Address: <input type="text" id="forgotPasswordEmail"/></div><br/><div id="ActionButton"><input type="button" id="forgotPasswordSubmit" value="Submit"/></div><div id="ActionCancel"><input type="button" id="forgotPasswordClose" value="Submit"/><div></div></div>')
		.dialog({
		    autoOpen: false,
		    width: 392,
		    zIndex: 9999
		});

	$('#ActionCancel').hide();
    $('#forgotPasswordLink').click(function() {
        $dialog.dialog('open');
        $("#popup-forgot-password").parent(".ui-dialog").addClass(".popup-forgot");
    });

    $('#forgotPasswordCancel').click(function() {
        $dialog.dialog('close');
        $('#ActionCancel').hide();
    });

    $('#forgotPasswordClose').click(function() {
        $dialog.dialog('close');
        $('#ActionCancel').hide();
    });

    $('#forgotPasswordSubmit').click(function() {
        $('#forgotPasswordMessage').html('');
        var email = $('#forgotPasswordEmail').val();
        if (isNullOrEmpty(email)) {
            $('#forgotPasswordMessage').html('<p><strong>Please Enter a Email Address</strong></p>');
        } else if (!isValidEmail(email)) {
            $('#forgotPasswordMessage').html('<p><strong>Please Enter a valid Email Address</strong></p>');
        } else {
            forgotPasswordRequest(email);
        }
    });
});

function isValidEmail(emailText) {    
    return isMatch(emailText, /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)
}


function isNullOrEmpty(stringValue) {
    return (stringValue == null || isMatch(stringValue, /^\s*$/g));
}

function isMatch(stringObject, regExression) {
    if (stringObject.match(regExression))
        return true;
    return false;
}

function forgotPasswordRequest(emailAddress) {
    var IsValid = false;
    $.ajax({
        cache: false,
        dataType: 'xml',
        url: '/utility/ForgotPasswordEmail.asmx/ResetPassword',
        data: { emailAddress: emailAddress },
        async: false,
        success: function(data, status) {
            //get the response from the service (even if it's null)
            var returnValue = $(data).find('string').text() || '';

            //strip spaces, line breaks, etc
            returnValue = returnValue.replace(/[\t\r\n\b]/g, '');
            $('#forgotPasswordMessage').html('');
            if (returnValue == "0") {
                $('#forgotPasswordCancel').attr('value', 'Close');
                $('#forgotPasswordMessage').html('<p><strong>Thank you! An email has been sent.</strong> Please allow up to 15 minutes to receive it. (Don\'t forget to check your junk mail!)</p>');
                $('#EmailInput').hide();
                $('#ActionCancel').show();
                IsValid = true;
            } else {
                $('#forgotPasswordMessage').html('<p><strong>Uh oh, your email is not recognized!</strong></p><p>If you are a member and have not logged into the new site yet, please try logging in with the username and a password you think you chose. You will have to do another \'forgot password\' after you do this in order to log in.  If you are still having trouble, please contact us at: <a href="mailto:membership@ytv.com">membership@ytv.com</a>.</p>');
                $('#EmailInput').hide();
                $('#ActionCancel').show();
            }
        },
        error: function(xhr, status, errorThrown) {
            alert("There has been an error." + xhr.responseText);
        }
    });
    return IsValid;
}