var countsTillBlocked = 0;
var currentState = '';
var activateProgress = '0';
var BusyText = '';
var BusyDesc = '';

function getState(BaseURL, VPSId, c) {

    if (activateProgress == '1') {


        //$("#vps-status").html(outParam[1]);
        //$("#vps-description").html(currentState);

        if ($("#vps-state-overlay").css("display") == "none") {
            $("#vps-state-overlay").fadeIn(100);
            $("#vps-state-body").fadeIn(100);

            $("#vps-status").html(BusyText);
            $("#vps-description").html(BusyDesc);
        }

        countsTillBlocked += 1;
        if (countsTillBlocked > 10 && $('#too-long').hasClass('hide')) {
            $('#too-long').removeClass('hide');
            $('#too-long').slideDown();
        }

        var param;

        param = "{VPSId:'" + VPSId + "',State:'" + currentState + "'}";

        $.ajax(

            {
                type: "POST",
                url: BaseURL + "/WebServices/VPS.asmx/getVPSState",
                data: param,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {

                    var outParam = Array();
                    outParam = msg.d.split('|');

                    //0 is Status Provisioning
                    //1 is Status Blocked
                    //2 is Status Ready
                    //3 is Status Error

                    if (outParam[0] == "0" || outParam[0] == "1") {//In progress

                        if ($("#vps-state-overlay").css("display") == "none") {
                            $("#vps-state-overlay").fadeIn(100);
                            $("#vps-state-body").fadeIn(100);
                        }
                        countsTillBlocked += 1;
                        $("#vps-status").html(outParam[1]);
                        $("#vps-description").html(outParam[2]);
                    }
                    else {

                        activateProgress = '0';
                        countsTillBlocked = 0;


                        $('#too-long').addClass('hide');

                        $("#vps-state-overlay").fadeOut(100);
                        $("#vps-state-body").fadeOut(100);


                        //if ($("#vps-state-overlay").css("display") == "block") {


                        if (currentState == "snapshot") {
                            currentState = '';
                            setTimeout("$('#next-btnRefreshList').next().click();", 1000);

                        }
                        else if (currentState == "State") {
                            currentState = '';
                            if (outParam[0] == "3") {//3=Failed

                                $('#lblEnabledState').text = "Failed";
                                setTimeout("$('#next-ibtnRefreshEnabledState').next().click();", 1000);

                            }
                            else {//Keep waiting
                                setTimeout("$('#next-ibtnRefreshEnabledState').next().click();", 1000);

                            }
                        }
                        else if (currentState == "Migrate") {
                            currentState = '';
                            if (outParam[0] == "3") {//3=Failed
                                $('#lblEnabledState').text = "Failed";
                                setTimeout("$('#next-ibtnRefreshEnabledState').next().click();", 1000);

                            }
                            else {//Keep waiting

                                setTimeout("$('#next-ibtnRefreshEnabledState').next().click();", 1000);

                            }
                        }

                    }

                    //}

                    $("#vps-status").html(outParam[1]);
                    $("#vps-description").html(outParam[2]);

                }
            });
        setTimeout("getState('" + BaseURL + "', '" + VPSId + "', '" + countsTillBlocked + "')", 10000);

    }
    else {

        setTimeout("getState('" + BaseURL + "', '" + VPSId + "', '" + countsTillBlocked + "')", 3000);

    }

}

$(document).ready(function () {
    // Dialog
    if ($('.dialogbox_vps').length != 0) {
        $('.dialogbox_vps').dialog({
            autoOpen: false,
            width: 800,
            height: 700,
            modal: true,
            title: 'HDD'
        });
    }
});

function closeDialog() {
    if ($('#dialog_addhdd').length != 0) {
        $('#dialog_addhdd').dialog("close");
    }
}

function IsBrowserIE() {

    if (navigator.appName == 'Microsoft Internet Explorer') {
        
        //alert('True');
        document.getElementById('ctl00_CPH_Content_TC_Accounts_Tab_VPS_hdnIsIE').value = 'true';
       //return true;

    } else {

        //alert('false');
        document.getElementById('ctl00_CPH_Content_TC_Accounts_Tab_VPS_hdnIsIE').value = 'false';
        //  return false;
    }
}