﻿var searchServiceUrl = window.Environment.CompanyInformationServiceUrl;

//searchData e.g.,  var searchData = eval("({scope:'Stores', outputtype:'json', searchText:'82'})");
function callGetStores(searchData, successCallback, failureCallback) {

    var url = getServiceUrl(searchServiceUrl);
    url = url + "/GetStores?method=?";
    callRestService(url, "GET", searchData, successCallback, failureCallback);
}

function callGetStoreDetails(searchData, successCallback, failureCallback) {

    var url = getServiceUrl(searchServiceUrl);
    url = url + "/GetStoreDetail?method=?";
    callRestService(url, "GET", searchData, successCallback, failureCallback);
}

function getServiceUrl(serviceUrl) {
    if (serviceUrl != null && serviceUrl.toLowerCase().indexOf('http://') == -1) {
        var baseUrl = window.location.href.replace("http://", "");
        baseUrl = baseUrl.substring(0, baseUrl.indexOf("/"));
        serviceUrl = "http://" + baseUrl + serviceUrl;
    }

    return serviceUrl;
}

function callRestService(serviceurl, methodType, data, successCallback, failedCallback) {

    if (undefined == failedCallback || null == failedCallback) {
        failedCallback = serviceError;
    } 

    $.ajax({
        async: true,
        type: methodType,
        url: serviceurl,
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processdata: false,
        success: successCallback,
        error: failedCallback
    });
}

function serviceError(e) {
    alert('Failed to make Company Information Service request.' + e.toString());
}
