﻿/*global location, window, XMLHttpRequest, ActiveXObject */

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') !== -1) {
        // Base Url for localhost
        url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf('/', index1 + 1);
        var baseLocalUrl = url.substr(0, index2);
        return baseLocalUrl + '/';
    }
    else {
        // Root Url for domain name
        return baseURL + '/';
    }

}

var baseURL = getBaseURL();

function ResolveUrl(url) {
    if (url.indexOf("~/") === 0) {
        url = baseURL + url.substring(2);
    }
    return url;
}

function GetSynchronousJSONResponse(url, postData) {
    var xmlhttp = null;
    if (window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();}
    else if (window.ActiveXObject) {
        if (new ActiveXObject("Microsoft.XMLHTTP")){
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
        else
            {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
    }
        // to be ensure non-cached version of response
    url = url + "?rnd=" + Math.random(); 
    
    xmlhttp.open("POST", url, false);//false means synchronous
    xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    xmlhttp.send(postData);
    var responseText = xmlhttp.responseText;
    return responseText;
}
