
/// <reference path="../ComplexService/ComplexService.aspx" />

//defined in SimpleService.aspx
//var serviceSelectId = '<%=serviceSelect.ClientID %>';


function pageLoad()
{
    // dodaje metodę obsługującą do przycisku "Zapisz"
    $addHandler($get("businessEntitySaveButton"),"click",businessEntitySave);
    
    // dodaje metodę obsługującą do przycisku "Zapisz"
    $addHandler($get("businessEntityMutiValueGenericSaveButton"),"click",businessEntitySave);

    // dodaje metodę obsługującą do przycisku "Zapisz"
    $addHandler($get("businessEntityGenericSaveButton"),"click",businessEntitySave);

    // dodaje metodę obsługującą do przycisku "Wyczyść"
    $addHandler($get("businessEntityClearButton"),"click",businessEntityClear);

    // dodaje metodę obsługującą do przycisku "Zapisz"
    $addHandler($get("vendorEntityGenericSaveButton"),"click",vendorEntitySave);
    
    // dodaje metodę obsługującą do przycisku "Zapisz"
    $addHandler($get("vendorEntitySaveButton"),"click",vendorEntitySave);

    // dodaje metodę obsługującą do przycisku "Wyczyść"
    $addHandler($get("vendorEntityClearButton"),"click",vendorEntityClear);

    // dodaje metodę obsługującą do przycisku "Zapisz"
    $addHandler($get("customerEntityGenericSaveButton"),"click",customerEntitySave);
    
    // dodaje metodę obsługującą do przycisku "Zapisz"
    $addHandler($get("customerEntitySaveButton"),"click",customerEntitySave);

    // dodaje metodę obsługującą do przycisku "Wyczyść"
    $addHandler($get("customerEntityClearButton"),"click",customerEntityClear);
    

}


function onFailed(error)
{
    alert("Wystąpił błąd:\n"  + error.get_message());
}


function businessEntitySave(e)
{

    var variousEntities = new ServiceData.Complex.VariousEntities();
    var entities = new Array(1);

    // brak informacji o typie
    /*
    var entity = {"Address":$get("businessEntityAddress").value, "City":$get("businessEntityCity").value,
                    "State":$get("businessEntityState").value, "Zip":$get("businessEntityZip").value};
    */

    // informacje o typie ułatwiają pracę mechanizmu serializacji
    ///*
    var entity = new ServiceData.Complex.BusinessEntity();
    entity.Address = $get("businessEntityAddress").value;
    entity.City = $get("businessEntityCity").value;
    entity.State = $get("businessEntityState").value;
    entity.Zip = $get("businessEntityZip").value;
    //*/

    entities[0] = entity;
    variousEntities.Entities = entities;

    var service;
    switch(getServiceSelectText())
    {
        case "WCF Service":
            switch(e.target.id)
            {
                case "businessEntityMutiValueGenericSaveButton":
                    tempuri.org.ComplexService.Save2(variousEntities,"My String",variousEntities,1,genericBusinessEntityOnSuccess,onFailed);
                    break;
                case "businessEntityGenericSaveButton":
                    tempuri.org.ComplexService.Save(variousEntities,genericBusinessEntityOnSuccess,onFailed);
                    break;
                case "businessEntitySaveButton":
                    tempuri.org.ComplexService.SaveBusiness(entity,businessEntityOnSuccess,onFailed);
                    break;
            }
            break;
        case "Internal WCF Service":
            service = new ServiceCommunication.ComplexWCFService();
            switch(e.target.id)
            {
                case "businessEntityMutiValueGenericSaveButton":
                        service.save2(variousEntities,"My String",variousEntities, 1, genericBusinessEntityOnSuccess,onFailed);
                        break;
                case "businessEntityGenericSaveButton":
                    service.save(variousEntities,genericBusinessEntityOnSuccess,onFailed);
                    break;
                case "businessEntitySaveButton":
                    service.saveBusiness(entity,businessEntityOnSuccess,onFailed);
                    break;
            }
            break;
    } 

}

function businessEntityClear()
{
    $get("businessEntityId").innerHTML = "";
    $get("businessEntityAddress").value = "";
    $get("businessEntityCity").value = "";
    $get("businessEntityState").value = "";
    $get("businessEntityZip").value = "";
}

function genericBusinessEntityOnSuccess(result)
{
    businessEntityOnSuccess(result.Entities[0]);
}


function businessEntityOnSuccess(result)
{
    $get("businessEntityId").innerHTML = result.Id;
}

function customerEntitySave(e)
{


    var variousEntities = new ServiceData.Complex.VariousEntities();
    var entities = new Array(1);


    // brak informacji o typie
    /*
        var entity = {"FirstName":$get("customerEntityFirstName").value,"LastName":$get("customerEntityLastName").value, 
                        "Address":$get("customerEntityAddress").value, "City":$get("customerEntityCity").value,
                        "State":$get("customerEntityState").value, "Zip":$get("customerEntityZip").value};
    */

    // informacje o typie ułatwiają pracę mechanizmu serializacji
    ///*
    var entity = new ServiceData.Complex.CustomerEntity();
    entity.FirstName = $get("customerEntityFirstName").value;
    entity.LastName = $get("customerEntityLastName").value;
    entity.Address = $get("customerEntityAddress").value;
    entity.City = $get("customerEntityCity").value;
    entity.State = $get("customerEntityState").value;
    entity.Zip = $get("customerEntityZip").value;
    //*/

    entities[0] = entity;
    variousEntities.Entities = entities;
    

    var service;
    switch(getServiceSelectText())
    {
        case "WCF Service":
            if(e.target.id === "customerEntitySaveButton")
            {
                tempuri.org.ComplexService.SaveCustomer(entity,customerEntityOnSuccess,onFailed);
            }
            else
            {
                tempuri.org.ComplexService.Save(variousEntities,genericCustomerEntityOnSuccess,onFailed);
            }
            break;
        case "Internal WCF Service":
            service = new ServiceCommunication.ComplexWCFService();
            if(e.target.id === "customerEntitySaveButton")
            {
                service.saveCustomer(entity,customerEntityOnSuccess,onFailed);
            }
            else
            {
                service.save(variousEntities,genericCustomerEntityOnSuccess,onFailed);
            }
            break;
    } 
}

function customerEntityClear()
{
    $get("customerEntityId").innerHTML = "";
    $get("customerEntityApprovalDate").innerHTML = "";
    $get("customerEntityFirstName").value = "";
    $get("customerEntityLastName").value = "";
    $get("customerEntityAddress").value = "";
    $get("customerEntityCity").value = "";
    $get("customerEntityState").value = "";
    $get("customerEntityZip").value = "";
}


function genericCustomerEntityOnSuccess(result)
{
    customerEntityOnSuccess(result.Entities[0]);
}

function customerEntityOnSuccess(result)
{
    $get("customerEntityId").innerHTML = result.Id;
    $get("customerEntityApprovalDate").innerHTML = result.ApprovalDate;
    $get("customerEntityFirstName").value = result.FirstName;
    $get("customerEntityLastName").value = result.LastName;
    $get("customerEntityAddress").value = result.Address;
    $get("customerEntityCity").value = result.City;
    $get("customerEntityState").value = result.State;
    $get("customerEntityZip").value = result.Zip;
}


function vendorEntitySave(e)
{


    var variousEntities = new ServiceData.Complex.VariousEntities();
    var entities = new Array(1);


    // brak informacji o typie
    /*
    var entity = {"VendorName":$get("vendorEntityVendorName").value, 
                    "Address":$get("vendorEntityAddress").value, "City":$get("vendorEntityCity").value,
                    "State":$get("vendorEntityState").value, "Zip":$get("vendorEntityZip").value};
                    
    */

    // informacje o typie ułatwiają pracę mechanizmu serializacji
    ///*                  
    var entity = new ServiceData.Complex.VendorEntity();
    entity.VendorName = $get("vendorEntityVendorName").value;
    entity.Address = $get("vendorEntityAddress").value;
    entity.City = $get("vendorEntityCity").value;
    entity.State = $get("vendorEntityState").value;
    entity.Zip = $get("vendorEntityZip").value;
    //*/
    
    entities[0] = entity;
    variousEntities.Entities = entities;
    

    var service;
    switch(getServiceSelectText())
    {
        case "WCF Service":
            if(e.target.id === "vendorEntitySaveButton")
            {
                tempuri.org.ComplexService.SaveVendor(entity,vendorEntityOnSuccess,onFailed);
            }
            else
            {
                tempuri.org.ComplexService.Save(variousEntities,genericVendorEntityOnSuccess,onFailed);
            }
            break;
        case "Internal WCF Service":
            service = new ServiceCommunication.ComplexWCFService();
            if(e.target.id === "vendorEntitySaveButton")
            {
                service.saveVendor(entity,vendorEntityOnSuccess,onFailed);
            }
            else
            {
                service.save(variousEntities,genericVendorEntityOnSuccess,onFailed);
            }
            break;
    } 
}

function vendorEntityClear()
{
    $get("vendorEntityId").innerHTML = "";
    $get("vendorEntityVendorName").value = "";
    $get("vendorEntityCreditRating").innerHTML = "";
    $get("vendorEntityAddress").value = "";
    $get("vendorEntityCity").value = "";
    $get("vendorEntityState").value = "";
    $get("vendorEntityZip").value = "";
}

function genericVendorEntityOnSuccess(result)
{
    vendorEntityOnSuccess(result.Entities[0]);

}

function vendorEntityOnSuccess(result)
{
    $get("vendorEntityId").innerHTML = result.Id;
    $get("vendorEntityVendorName").value = result.VendorName;
    $get("vendorEntityCreditRating").innerHTML = result.CreditRating;
    $get("vendorEntityAddress").value = result.Address;
    $get("vendorEntityCity").value = result.City;
    $get("vendorEntityState").value = result.State;
    $get("vendorEntityZip").value = result.Zip;

}

function getServiceSelectText()
{
    var serviceSelector = $get(serviceSelectId);
    return serviceSelector.options[serviceSelector.selectedIndex].text;
}
