﻿

/// Calls ajax request for checking user name availability.
function validateUserName(user, url)
{
    var msg = '';
    if ($.trim(user) == '')
        msg = '-Username is required<br/>';
    else
    {
        regExp = /^[^a-zA-Z0-9_]$/;
        var found = false;
        for (var i = 0; i < user.length; i++)
        {
            if (user.charAt(i).match(regExp))
            {
                found = true;
                break;
            }
        }
        if (found)
        {
            msg = '-Username should not contain any special characters <br/>'
        }
        else
        {
            if (!callAjaxPostBack(user, url, function() { }, function() { }, false))
                msg = '-Username is unavailable - Please choose another username <br/>';
        }
    }

    return msg;
}

function forgotPassword(url, emailId, redirect)
{
    var success = function(r)
    {
        if (r.IsValid)
        {
            if (redirect)
                setTimeout('window.location = "' + r.ReturnUrl + '";', 5000);
            updatePopup(r.ErrorMessage, popupEnum.success);
        }
        else
        {
            showError(r.ErrorMessage);
        }
    }
    var error = function(e)
    {
        showError('Unable to login, please try after some time.');
        return false;
    }
    callAjaxPostBack(emailId, url, success, error, true, "POST");
}

function changeStateList(countryId, stateId, provinceId, zipId, stateLabelId, USA)
{
    var country = $(countryId).find('option').filter(':selected').val();
    var zip = 'Zip Code';
    var state = 'State';
    if (country == USA)
    {
        $(stateId).show();
        $(provinceId).hide();
    }
    else
    {
        $(stateId).hide();
        $(provinceId).show();
        state = 'Province';
        zip = 'Postal Code';
    }
    
    $(stateLabelId).html(state + ':');
    $(zipId).html(zip + ':');
    $("#zipCode").attr("title", zip);
}

/// Checks whether the email is in required format or not.
function isEmail(email, url)
{
    email = $("#" + email).val();
    var msg = '';
    if ($.trim(email) == '')
    {
        msg = '-Email is required<br/>';
    }
    else
    {
        var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!regex.test(email) && email != '')
        {
            msg = '-Email Address is invalid<br/>';
        }
        else
        {

            if (url)
            {
                if (!callAjaxPostBack(email, url, function() { }, function() { }, false))
                    msg = '-Email Address is unavailable - Please choose another email address<br/>';
            }
        }
    }
    return msg;
}

/// Checks whether password and confirm password are same or not.
function checkPasswords(password, confirmPassword)
{
    if ($.trim($(password).val()) != $.trim($(confirmPassword).val()))
    {
        return false;
    }
    return true;
}

/// Checks whether password is in required format or not.
function isPassword(id)
{
    var error = '';
    var regExpAlpha = /^[A-Za-z]$/;
    var regExpNum = /^[0-9]$/;
    var regExpSplChar = /^[^A-Z^a-z^0-9]$/;
    var password = $(id).val();
    if (password.length < 6)
    {
        return '-Password must be at least 6 characters long';
    }

    var found = false;
    for (var i = 0; i < password.length; i++)
    {
        if (password.charAt(i).match(regExpAlpha))
        {
            found = true;
        }
    }

    if (!found)
    {
        return '-Password must have at least 1 letter'
    }

    found = false;

    for (var i = 0; i < password.length; i++)
    {
        if (password.charAt(i).match(regExpNum) || password.charAt(i).match(regExpSplChar))
        {
            found = true;
        }
    }
    if (!found)
    {
        return '-Password must have at least 1 number or 1 symbol'
    }
    return error;
}


/// Checks whether the phone number entered is numeric or not.
function isFieldNumeric(fieldValue)
{
    var reg = Number($.trim(fieldValue.replace(/-/g, '')));
    if (isNaN(reg))
    {
        return false;
    }
    return true;
}

function isAlphaNumeric(fieldValue)
{
    var reg = /^([a-zA-Z0-9_-]+)$/;
    return reg.test($.trim(fieldValue));
}
function allowNumbersOnly(e)
{
    var code = e.keyCode;
    if (code == 8 || (code >= 45 && code <= 57 && e.shiftKey == false) || code == 9 || (code >= 96 && code <= 105) || (code >= 35 && code <= 40) || code == 116)
        return true;
    else return false;

}

function allowAlphaNumerics(e)
{
    var code = e.keyCode;
    if (allowNumbersOnly(e) || (code >= 65 && code <= 90) || (code == 109 && e.shiftKey == false))
    {
        return true;
    }
    return false;
}

/// Enables/disables register button as per acceptance of terms and conditions.
function toggleRegisterButton(condition, regButton)
{

    if ($(condition).val())
    {
        $(regButton).removeAttr('disabled');
    }
    else
    {
        $(regButton).attr('disabled', 'disabled');
    }
}

/// Redirects to home page.
function redirectToHome(location)
{
    window.location = location;
}

/// Logs in the user with credentials
function login(userName, password, pageUrl)
{

    var loginDetails = new Object();
    var data;
    loginDetails.UserName = userName;
    loginDetails.Password = password;

    var success = function(r)
    {
        if (r.IsValid)
        {
            window.location = r.ReturnUrl;
        }
        else
        {
            showError(r.ErrorMessage);
        }
    }
    var error = function(e)
    {
        showError('Unable to login, please try after some time.');
        return false;
    }
    callAjaxPostBack(loginDetails, pageUrl, success, error, true);

}

function refreshFileUploadControl(controlId, linkid, onchangefunc )
{
    var fileUploadControl = document.getElementById(controlId);
    fileUploadControl.value = "";

    //for ie
    var clone = fileUploadControl.cloneNode(true);
    if (onchangefunc)
        clone.onchange = onchangefunc;
    else
    clone.onchange = fileUploadControl.onchange;

    fileUploadControl.parentNode.replaceChild(clone, fileUploadControl);

    $("#" + linkid).hide();
}

function validateAge(strdate)
{
    var message = '-You must be at least 13 years old.';
    if (strdate == null)
    {
        message = '';
    }
    else
    {
        if ($.trim(strdate.toString()) != '')
        {
            var today = serverDate;
            var bdate = new Date(strdate);
            bdate.setFullYear(bdate.getFullYear() + 13, bdate.getMonth(), bdate.getDate());
            var vdate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);

            if (bdate < vdate || bdate == vdate)
                message = '';
        }
    }
    return message;
}