var useAttachs = false;

function GetFillerUri() {
    try {
        if (fillerUri != undefined) {
            return fillerUri;
        }
    } catch (err) { }

    return "http://localhost/eGO.Filler.WS/FillerService.svc/rest/";
}

$(document).ready(function () {
    var uri = GetFillerUri();

    $("#showXml").click(function () {
        ShowData(uri);
    });

    $("#showXmlWithAttachments").click(function () {
        ShowDataWithAttachments(uri);
    });

    $("#saveForm").click(function () {
        SaveXml(uri);
    });

    $("#saveFormAs").click(function () {
        SaveXmlAs();
    });

    $("#loadForm").click(function () {
        var xmlData = localStorage.getItem(GetFormStorageKey());
        var xmlDoc = $.parseXML(xmlData);
        var indexes = Array();

        if(XPathUsed()) {
            LoadXPathXml(xmlDoc.childNodes[0], indexes);
            return;
        }

        LoadXml(xmlDoc.childNodes[0], indexes);
    });

    $("#loadFromFile").click(function () {
        LoadFromFile();
    });

    $("#closeForm").click(function () {
        CloseForm(uri);
    });

    $("#printForm").click(function () {
        PrintForm();
    });

    $("#validateForm").click(function () {
        ValidateForm();
    });

    $("#validateFormWithXsd").click(function () {
        ValidateFormWithXsd(uri);
    });

    $("#validateFormWithServer").click(function () {
        ValidateFormWithServer(uri);
    });

    $("#clearForm").click(function () {
        ClearForm(this.form);
    });

    $("#cancelForm").click(function () {
        CancelForm();
    });

    $("#saveAndCloseForm").click(function () {
        SaveAndCloseForm(uri);
    });

    $("#disableForm").click(function () {
        DisableForm();
    });

    $("#enableForm").click(function () {
        EnableForm();
    });

    $("#validateFormAndClose").click(function () {
        ValidateFormAndClose(uri);
    });

    $("#validateFormAndNotClose").click(function () {
        validateFormAndNotClose(uri);
    });

    if (useAttachs) {
        ShowSavedForms();
        InitializeAttachmentFields();
    }
});

function InitializeAttachmentFields() {
    $('input:file').each(function () {
        document.getElementById($(this).attr('id')).addEventListener('change', HandleFileSelect, false);
    });
}