﻿var messageDiv;
//IE8 fix
jQuery.ajaxSetup({ xhr: function() {
    //return new window.XMLHttpRequest();
    try {
        if (window.ActiveXObject)
            return new window.ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { }
    return new window.XMLHttpRequest();
}
});
//End of IE8 fix
jQuery(document).ready(function() {
    //Setup the AJAX Call
    jQuery.ajaxSetup({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: ajaxError
    });

});
function ajaxError(XMLHttpRequest, textStatus) {
    debugger;
    var message = "";
    try {
        var responseText = eval("(" + XMLHttpRequest.responseText + ")");
        message = responseText.Message;
        alert("Error: " + message);
    }
    catch (err) {
        message = "Internal Server Error. " + err;
    }

}
var hideTimer;
function showMessage(messageText, loadImage) {
    jQuery(".messageDisplay").hide();
    if (hideTimer)
        clearTimeout(hideTimer);
    //var image = "<img src='images/ajax-loader.gif' style='text-align:inline;display:inline;' />";
    var image = "<image src='images/waitimage.gif' style='text-align:inline;display:inline;' />";
    if (loadImage == "none") {
        image = "";
    }
    //AB 20080911 - Do not display messages for now.
    //jQuery(".messageDisplay").html(image + "&nbsp;<b>" + messageText + "</b>").fadeTo('fast', 0.8).fadeIn('normal');
    if (jQuery(".newMessageDisplay").size() > 0) {
        jQuery(".newMessageDisplay").html(image + "&nbsp;" + messageText).fadeTo('fast', 0.8).fadeIn('normal');

    }
}
function autoHide() {
    hideTimer = setTimeout(hideMessage, 4000);
}
function hideMessage() {
    jQuery(".messageDisplay").fadeOut(2000);
    jQuery(".newMessageDisplay").fadeOut(2000);
}
function callAjax(messageText, serviceUrl, jsonData, successFunction) {

    jQuery.ajax({
        url: serviceUrl,
        data: jsonData,
        success: function(msg) {

            successFunction(msg);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            //alert(xhr.status);
            //alert(thrownError);
        }
    });
}
function closeModal() {
    jQuery.modal.close();
}
function selectAllCheckboxes(selectAllId, classCheckboxes) {
    jQuery("#" + selectAllId).click(function() {
        var isChecked = this.checked;
        jQuery("input[type=checkbox]." + classCheckboxes).each(function() {
            this.checked = isChecked;
        });
    });
}
function focusAndSelect(documentId, message) {
    if (message) {
        showMessage(message, "none");
        autoHide();
    }
    if (document.getElementById(documentId) != null) {
        document.getElementById(documentId).focus();
        document.getElementById(documentId).select();
    }
}
function safeForJson(text) {
    var safeText = text;
    safeText = text.replace(/\u005C/g, "");
    safeText = safeText.replace(/\u0027/g, "\\u0027");
    //safeText = safeText.replace(/\u0022/g, "\\u0022");
    return safeText;
}
function showDragHandleForRow(tableId) {
    jQuery("#" + tableId + " tr").hover(function() {
        jQuery(this).children(".dragHandle").addClass('showDragHandle');
    }, function() {
        jQuery(this).children(".dragHandle").removeClass('showDragHandle');
    });
}

//For dynamic loading of State standards, Common core, 21 Century skills
//start

function requestLoadStandards(contentId, stateCode, displayDivId) {

    document.getElementById(displayDivId).innerHTML = 'Loading standards...';
    callAjax("Loading standards...", "AjaxServices.aspx/LoadContentStateStandards", "{\"contentId\":\"" + contentId + "\",\"stateCode\":\"" + stateCode + "\",\"displayDivId\":\"" + displayDivId + "\"}", post_requestLoadStateStandards);
}

function PopulateBBStandards(stateCode, displayDivId) {

    document.getElementById(displayDivId).innerHTML = 'Loading...';
    callAjax("Loading...", "AjaxServices.aspx/PopulateBBStandards", "{\"stateCode\":\"" + stateCode + "\",\"displayDivId\":\"" + displayDivId + "\"}", post_requestLoadStateStandards);
}
function post_requestLoadStateStandards(msgObj) {
    var msg = eval(msgObj.d)
    if (msg != null && eval(msgObj.d) != 'undefined') {
        document.getElementById(msg[0].displayDivId).innerHTML = msg[0].value;
    }
}

//End
