﻿function CreateProfileCardWrapper(_sUserName) {

    var newdiv = document.createElement('div');
    var divIdName = 'ProfileCard_Wrapper';
    newdiv.setAttribute('id', divIdName);
    document.body.appendChild(newdiv);
    document.getElementById('ProfileCard_Wrapper').title = _sUserName;

    var _buttonsProfileCard = {};
    _buttonsProfileCard[_closeButton] = function () {
        jQuery(this).remove();
    }

    jQuery("#ProfileCard_Wrapper").dialog({
        autoOpen: false,
        width: 580,
        resizable: false,
        modal: true,
        buttons: _buttonsProfileCard,
        close: function (event, ui) { jQuery(this).remove(); }
    });
}

function ShowContent(_sUserName) {
    CreateProfileCardWrapper(_sUserName);
    jQuery("#ProfileCard_Wrapper").load("/partial/profile", { UserName: _sUserName, ShowPlayer: false }, function ()
    { jQuery('#ProfileCard_Wrapper').dialog('open'); });
}

function ShowContentWithPlayer(_sUserName) {
    CreateProfileCardWrapper(_sUserName);
    jQuery("#ProfileCard_Wrapper").load("/partial/profile", { UserName: _sUserName, ShowPlayer: true }, function ()
    { jQuery('#ProfileCard_Wrapper').dialog('open'); });
}

function ShowPasswordForgetForm() {
    CreatePasswordForgetForm();
    jQuery("#PasswordReturn_Wrapper").load("/partial/password", function ()
    { jQuery('#PasswordReturn_Wrapper').dialog('open'); });   
}

function ShowActivateReSend() {
    CreateEmailReSendForm();
    jQuery('#PasswordReturn_Wrapper').remove();
    jQuery("#EmailReSend_Wrapper").load("/partial/activationemail", function ()
    { jQuery('#EmailReSend_Wrapper').dialog('open'); });
}

function CreateEmailReSendForm() {
    var newdiv = document.createElement('div');
    var divIdName = 'EmailReSend_Wrapper';
    newdiv.setAttribute('id', divIdName);
    newdiv.setAttribute('title', _resendActivation);
    document.body.appendChild(newdiv);

    var senddiv = document.createElement('div');
    var senddivIdName = 'ActivationIsSend';
    senddiv.setAttribute('id', senddivIdName);
    senddiv.setAttribute('title', _activationEmailSent);
    senddiv.innerHTML = "<p>" + _activationEmailSent + "</p>";
    document.body.appendChild(senddiv);

    var _buttonsResendActivation = {};
    _buttonsResendActivation[_sendButton] = function () {
        jQuery.post("/partial/sendactivationemail", { email: document.getElementById('txt_email').value }, function (txt) {
                    if (txt == "OK") {
                        document.getElementById('WrongEmail').style.display = 'none';
                        document.getElementById('EmptyEmail').style.display = 'none';
                        document.getElementById('IsActivate').style.display = 'none';
                        jQuery('#ActivationIsSend').dialog('open');
                    }
                    if (txt == "FormIsEmpty") {
                        document.getElementById('EmptyEmail').style.display = 'block';
                        document.getElementById('WrongEmail').style.display = 'none';
                        document.getElementById('IsActivate').style.display = 'none';
                    }
                    if (txt == "WrongEmail") {
                        document.getElementById('WrongEmail').style.display = 'block';
                        document.getElementById('EmptyEmail').style.display = 'none';
                        document.getElementById('IsActivate').style.display = 'none';
                    }
                    if (txt == "IsActivate") {
                        document.getElementById('IsActivate').style.display = 'block';
                        document.getElementById('WrongEmail').style.display = 'none';
                        document.getElementById('EmptyEmail').style.display = 'none';
                    }
                });
    }
    _buttonsResendActivation[_cancelButton] = function () {
        jQuery(this).remove();
    }

    jQuery("#EmailReSend_Wrapper").dialog({
        autoOpen: false,
        width: 580,
        resizable: false,
        modal: true,
        buttons: _buttonsResendActivation
    });

    var _buttonsActivationSent = {};
    _buttonsActivationSent[_closeButton] = function () {
        jQuery("#EmailReSend_Wrapper").remove();
        jQuery(this).remove();
    }

    jQuery("#ActivationIsSend").dialog({
        autoOpen: false,
        height: 200,
        width: 200,
        resizable: false,
        modal: true,
        buttons: _buttonsActivationSent
    });
}

function CreatePasswordForgetForm() {
    var newdiv = document.createElement('div');
    var divIdName = 'PasswordReturn_Wrapper';
    newdiv.setAttribute('id', divIdName);
    newdiv.setAttribute('title', _passwordReset);
    document.body.appendChild(newdiv);

    var senddiv = document.createElement('div');
    var senddivIdName = 'PasswordIsSend';
    senddiv.setAttribute('id', senddivIdName);
    senddiv.setAttribute('style', 'display:none;');
    senddiv.setAttribute('title', _passwordSent);
    senddiv.innerHTML = "<p>" + _passwordSent + ".</p>";
    document.body.appendChild(senddiv);

    var _buttonsPasswordReset = {};
    _buttonsPasswordReset[_sendButton] = function () {
        jQuery.post("/partial/sendpassword", { email: document.getElementById('txt_email').value }, function (txt) {
            if (txt == "OK") {
                document.getElementById('WrongEmail').style.display = 'none';
                document.getElementById('EmptyEmail').style.display = 'none';
                document.getElementById('VipEmail').style.display = 'none';
                jQuery('#PasswordIsSend').dialog('open');
            }
            else if (txt == "FormIsEmpty") {
                document.getElementById('EmptyEmail').style.display = 'block';
                document.getElementById('WrongEmail').style.display = 'none';
                document.getElementById('VipEmail').style.display = 'none';
            }
            else if (txt == "WrongEmail") {
                document.getElementById('WrongEmail').style.display = 'block';
                document.getElementById('EmptyEmail').style.display = 'none';
                document.getElementById('VipEmail').style.display = 'none';
            }
            else if (txt == "VipEmail") {
                document.getElementById('WrongEmail').style.display = 'none';
                document.getElementById('EmptyEmail').style.display = 'none';
                document.getElementById('VipEmail').style.display = 'block';
            }
        });
    }
    _buttonsPasswordReset[_cancelButton] = function () {
        jQuery(this).remove();
    }

    jQuery("#PasswordReturn_Wrapper").dialog({
        autoOpen: false,
        width: 580,
        resizable: false,
        modal: true,
        buttons: _buttonsPasswordReset
    });

    var _buttonsPasswordSent = {};
    _buttonsPasswordSent[_closeButton] = function () {
        jQuery("#PasswordReturn_Wrapper").remove();
        jQuery(this).remove();
    }

    jQuery("#PasswordIsSend").dialog({
        autoOpen: false,
        height: 200,
        width: 200,
        resizable: false,
        modal: true,
        buttons: _buttonsPasswordSent
    });
}

function CapString(sStringToCap, iStringLength, sTrailingChars)
{
    var _sNewString = "";
    if (sStringToCap.length > iStringLength)
    {
       _sNewString = sStringToCap.substring(0, iStringLength);
       _sNewString += sTrailingChars;
    }
    else
    { _sNewString = sStringToCap; }

    return _sNewString;
}

function IsInteger(value)
{
    var objRegExp = /(^-?\d\d*$)/;

    //check for integer characters
    return objRegExp.test(value);
}

function Trim(stringValue)
{
    var trimmed = stringValue.replace(/^\s+|\s+$/g, '');
    return trimmed;
}

function IsEmail(emailValue)
{
    var _isValid = true;
    var emailRegexStr = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    var isvalid = emailRegexStr.test(emailValue);

    if (!isvalid)
    { _isValid = false; }

    return _isValid;
}

function ShowFeedbackForm() {
    CreateFeedbackForm();
    jQuery("#Feedback_Wrapper").load("/partial/feedback", function ()
    { jQuery('#Feedback_Wrapper').dialog('open'); });

    function CreateFeedbackForm() {
        var newdiv = document.createElement('div');
        var divIdName = 'Feedback_Wrapper';
        newdiv.setAttribute('id', divIdName);
        newdiv.setAttribute('title', _feedback);
        newdiv.setAttribute('style', 'margin-left: 10px;');
        document.body.appendChild(newdiv);

        var senddiv = document.createElement('div');
        var senddivIdName = 'FeedbackIsSend';
        senddiv.setAttribute('id', senddivIdName);
        senddiv.setAttribute('style', 'display:none;');
        senddiv.setAttribute('title', _feedbackSent);
        senddiv.innerHTML = "<p>" + _feedbackSent + ".</p>";
        document.body.appendChild(senddiv);

        var _buttons = {};
        _buttons[_sendButton] = function () {
            jQuery.post("/partial/sendfeedback",
            {
                name: document.getElementById('txtFeedbackName').value,
                email: document.getElementById('txtFeedbackEmail').value,
                city: document.getElementById('txtFeedbackCity').value,
                phone: document.getElementById('txtFeedbackPhone').value,
                text: document.getElementById('txtFeedbackText').value,
                NumericCaptchaAnswer: jQuery('#PVFeedBackForm input[name=NumericCaptchaAnswer]').val(),
                NumericCaptchaQuestion: jQuery('#PVFeedBackForm input[name=NumericCaptchaQuestion]').val(),
                sourceurl: window.location.href
            }, function (txt) {
                if (txt == "OK") {
                    document.getElementById('RequiredDataMissing').style.display = 'none';
                    document.getElementById('IncorrectEmail').style.display = 'none';
                    document.getElementById('IncorrectCaptcha').style.display = 'none';
                    jQuery('#FeedbackIsSend').dialog('open');
                }
                else if (txt == "RequiredDataMissing") {
                    document.getElementById('RequiredDataMissing').style.display = 'block';
                    document.getElementById('IncorrectCaptcha').style.display = 'none';
                    document.getElementById('IncorrectEmail').style.display = 'none';
                }
                else if (txt == "IncorrectEmail") {
                    document.getElementById('RequiredDataMissing').style.display = 'none';
                    document.getElementById('IncorrectCaptcha').style.display = 'none';
                    document.getElementById('IncorrectEmail').style.display = 'block';
                }
                else if (txt == "IncorrectCaptcha") {
                    document.getElementById('RequiredDataMissing').style.display = 'none';
                    document.getElementById('IncorrectEmail').style.display = 'none';
                    document.getElementById('IncorrectCaptcha').style.display = 'block';
                }
            });
        }
        _buttons[_cancelButton] = function () { jQuery(this).remove(); }

        jQuery("#Feedback_Wrapper").dialog({
            autoOpen: false,
            width: 595,
            resizable: false,
            modal: true,
            buttons: _buttons
        });

        var _buttonsSent = {};
        _buttonsSent[_closeButton] = function () {
            jQuery("#Feedback_Wrapper").remove();
            jQuery(this).remove();
        }

        jQuery("#FeedbackIsSend").dialog({
            autoOpen: false,
            height: 200,
            width: 200,
            resizable: false,
            modal: true,
            buttons: _buttonsSent
        });
    }
}

function CheckAuthenticationForGridIt() {
    CreateCheckAuthenticationForGridIt();
    jQuery("#GriditAuthenticationCheck_Wrapper").load("/partial/checkauthenticationforgridit", function ()
    { jQuery('#GriditAuthenticationCheck_Wrapper').dialog('open'); });
}

function CreateCheckAuthenticationForGridIt() {
    var newdiv = document.createElement('div');
    var divIdName = 'GriditAuthenticationCheck_Wrapper';
    newdiv.setAttribute('id', divIdName);
    newdiv.setAttribute('title', _redirectToGridit);
    document.body.appendChild(newdiv);

    var _buttonsGridit = {};
    _buttonsGridit[_cancelButton] = function () {
        jQuery(this).remove();
    }
    _buttonsGridit[_registerButton] = function () {
        document.location = '/' + _registration;
    }
    _buttonsGridit[_redirectGriditButton] = function () {
        window.open('/gridit');
        jQuery(this).remove();
    }

    jQuery("#GriditAuthenticationCheck_Wrapper").dialog({
        autoOpen: false,
        width: 580,
        resizable: false,
        modal: true,
        buttons: _buttonsGridit
    });
}

function SetSearchFieldActivity(sender, mode, defaultUserNameLabel) {
    if (mode == 'active') {
        if (sender.value == defaultUserNameLabel)
        { sender.value = ''; }
    }

    if (mode == 'inactive') {
        if (sender.value == '')
        { sender.value = defaultUserNameLabel; }
    }
}

function Encode(input) {
    return $('<div/>').text(input).html();
}

function Decode(value) {
    return $('<div/>').html(value).text();
}

function BanCommunityUser(UserId, CommunityId) {
    var _BanWrapper = document.getElementById('BanUser_Wrapper');
    if (_BanWrapper == null) {
        var banWrapper = document.createElement('div');
        banWrapper.setAttribute('id', "BanUser_Wrapper");
        document.body.appendChild(banWrapper);
        _BanWrapper = document.getElementById('BanUser_Wrapper');
    }

    jQuery("#BanUser_Wrapper").load(("/partial/community_ban_user?gUserProfileId=" + UserId + "&gCommunityId=" + CommunityId + "&randIEFix=" + (Math.random() * 99999)), function () {
        ShowBanUserModalPopup();
    });
}

function BanCommunityUserByUserName(UserName, CommunityId) {
    var _BanWrapper = document.getElementById('BanUser_Wrapper');
    if (_BanWrapper == null) {
        $('<div/>', { id: 'BanUser_Wrapper' }).appendTo(document.body).load(("/partial/community_ban_username?sUserName=" + UserName + "&gCommunityId=" + CommunityId + "&randIEFix=" + (Math.random() * 99999)), function () {
            ShowBanUserModalPopup();
        }); ;
    }
    else {
        jQuery("#BanUser_Wrapper").load(("/partial/community_ban_username?sUserName=" + UserName + "&gCommunityId=" + CommunityId + "&randIEFix=" + (Math.random() * 99999)), function () {
            ShowBanUserModalPopup();
        });  
     }
}

function RemoveCommunityUserBan(CommunityId, UserId) {
    var _RemoveBanWrapper = document.getElementById('RemoveBanUser_Wrapper');
    if (_RemoveBanWrapper == null) {
        $('<div/>', { id: 'RemoveBanUser_Wrapper' }).appendTo(document.body).load(("/partial/community_removeban_user?gUserProfileId=" + UserId + "&gCommunityId=" + CommunityId + "&randIEFix=" + (Math.random() * 99999)), function () {
            ShowRemoveBanModalPopup();
        }); ;
    }
    else {
        jQuery("#RemoveBanUser_Wrapper").load(("/partial/community_removeban_user?gUserProfileId=" + UserId + "&gCommunityId=" + CommunityId + "&randIEFix=" + (Math.random() * 99999)), function () {
            ShowRemoveBanModalPopup();
        });
    }

  
}
