﻿function inputClearDefaultOnFocus(i) {
    if (i.defaultValue == i.value) i.value = '';
}

// ************* AJAX FUNCTIONALITY ****************************************************
$(function () {
    $.ajaxSetup({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(xhr.statusText);
            alert(thrownError);
        }
    });

    $("#SearchBox").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/Interface.asmx/FindModelByName",
                data: JSON.stringify({ term: request.term }),
                success: function (data) {
                    response(data.d)
                }
            })
        },
        focus: function (event, ui) {
            $('#SearchBox').val(ui.item.label);
            return false;
        },
        minLength: 2,
        select: function (event, ui) {
            document.location = '/' + ui.item.value;
            return false;
        }

    })
    .data("autocomplete")._renderItem = function (ul, item) {
        return $("<li></li>")
			.data("item.autocomplete", item)
			.append('<a><table cellpadding="0" cellspacing="0"><tr><td><img src="' + item.thumb + '" class="thumbsmall" /></td><td><b>' + item.label + '</b><div class="small">' + item.desc + '</div></td></tr></table></a>')
			.appendTo(ul);
    };



});


function AjaxFailed(result) {
    try {
        var err = eval('(' + result.responseText + ')');
        showAjaxError('There seems to be a problem with a webservice request. If you have this problem after reloading the page, contact System One and explain the steps that lead to this problem.<br/><br/><b>Error message</b>: ' + err.Message);
    }
    catch (err) {
        showAjaxError('There seems to be a problem with a webservice request. If you have this problem after reloading the page, contact System One and explain the steps that lead to this problem.');
    };
}

function showAjaxError(str) {
    document.getElementById('dialogError').innerHTML = str;
    openJQueryDialog('dialogError');
}



//***** Product ajax manager *************************************************
function ModelAjax() { }

ModelAjax.Delete = function (modelId) {
    var response = new Object;
    response.modelId = modelId;
    $.ajax({
        async: false,
        url: 'Interface.asmx/DeleteModel',
        data: JSON.stringify(response),
        success: function (data) {
            var holder = document.getElementById(modelId);
            while (holder.hasChildNodes()) {
                holder.removeChild(holder.lastChild);
            }
        }
    });
}

ModelAjax.KeyExists = function (key, modelId) {
    var response = new Object;
    response.key = key;
    response.modelId = modelId;

    var result = undefined;
    $.ajax({
        async: false,
        url: 'Interface.asmx/KeyExists',
        data: JSON.stringify(response),
        success: function (data) {
            result = data.d;
        }
    });
    return result;
}


function PhotoAjax() { }

PhotoAjax.Delete = function (photoId) {
    var response = new Object;
    response.photoId = photoId;
    $.ajax({
        async: false,
        url: 'Interface.asmx/DeletePhoto',
        data: JSON.stringify(response),
        success: function (data) {
            var holder = document.getElementById(photoId);
            while (holder.hasChildNodes()) {
                holder.removeChild(holder.lastChild);
            }
        }
    });
}


PhotoAjax.StoreSortedList = function (ids) {
    var response = new Object;
    response.ids = ids;
    $.ajax({
        url: 'Interface.asmx/StoreSortedList',
        data: JSON.stringify(response)
    });
}
