﻿// JScript File
//----------------------------------------------------------
// scripts to call to web methods and receive the callbacks
//----------------------------------------------------------

function createHttpRequest()
{
    var oWeb;
    try
    {
        oWeb = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch(e)
    {
        try
        {
            oWeb = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(oc)
        {
            oWeb = null;
        }
    }

    if(!oWeb && typeof(XMLHttpRequest) != 'undefined')
        oWeb = new XMLHttpRequest();
    
    return oWeb;
}

/// <summary>
/// Call the web server method, wait for response and validate it, if ok then call the callback function
/// </summary>
/// <param name="urlWebMethod">The Url of the Web Method to Invoke</param>
/// <param name="bShowError">Flag to indicate how show the error</param>
/// <param name="functionCallback">The function to call if the response is ok</param>
function CallServerWebMethod(urlWebMethod, bShowError, functionCallback, postParams)
{
    var oRequest = createHttpRequest();
    
    if(oRequest != null)
    {
        oRequest.onreadystatechange = function() 
        {
            var responseOk = false;
            var result = '';
            if (oRequest.readyState == 4)
            {
                if (oRequest.status == 200)
                {
                    result = oRequest.responseText;
                    
                    if (result != null)
                    {
                        if (result.substr(0,8) == 'REDIRECT')
                        {
                            try
                            {  
                                var idxUrlBefore = redirectDefaultPage.indexOf("urlbeforenoaccess");
                                var topUrl = window.top.location.href;
                                
                                if (idxUrlBefore > 0) {
                                    pageNoAcces = redirectDefaultPage.substr(0,idxUrlBefore - 1);
                                    window.top.location.href = pageNoAcces + "&urlbeforenoaccess=" + escape(topUrl)
                                } else {
                                    location.href = redirectDefaultPage;
                                }
                                
                                return;
                            }
                            catch(exception)
                            {
                                result = 'ErrorThe Session has expired.';
                            }
                        }

                        if (oRequest.responseText.indexOf('HtmlTextOrSimilarNotAllowed') > -1)
                        {
                            var x = 'Html text or similar are not allowed.';
                            if (bShowError)
                            {
                                //ShowError('Error' + x);
                                SetFocus(controlId);
                                alert(x);
                            }
                            else
                                alert(x);
                        }
                        
                        if (result.substr(0,5) == 'Error')
                        {
                            if (bShowError)
                                ShowError(result);
                            else
                                alert(result.substr(5));
                        }
                        else
                        {
                            functionCallback(result);
                            responseOk = true;
                        }
                    }
                }
                else
                {
                    alert(oRequest.statusText);
                    alert('There was a problem retrieving data: ' + oRequest.statusText);
                }
                if (!responseOk)
                    stopWindowWait();
            }
        };
        
        var postMethod = false;
        var postData = '';
        var controlId = '';
        
        if (postParams != null)
        {
            postMethod = true;
            for(ii = 0; ii < postParams.length; ii++)
            {
                var posEqual = postParams[ii].indexOf('=');                
                var paramKey = postParams[ii].substr(0, posEqual + 1);
                var paramValue = postParams[ii].substr(posEqual + 1);
                
                if (bShowError && paramKey == 'controlClientId=')
                    controlId = paramValue;
                
                if (encodeURIComponent)
                    paramValue = encodeURIComponent(paramValue);
                else
                    paramValue = escape(paramValue);
                if (ii > 0)
                    postData += '&';
                postData += paramKey + paramValue;
            }
        }
        
        if (postMethod)
        {
            oRequest.open('POST', urlWebMethod, true);
            oRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
        }
        else
        {
            oRequest.open('GET', urlWebMethod, true);
            postData = null;
        }
        oRequest.send(postData);
    }
    return oRequest;
}

