﻿$(function() {
    $('#online_quote').dialog({ modal: true, width: 425, autoOpen: false});

    $('#right_hand_menu > img:first').click(function() {
        $('#quote_tabs').tabs({ disabled: [1] });
        $('#online_quote').dialog('open');
    });
});

function ValidateFormPart1() {
    Page_ClientValidate('Step1');

    if (Page_IsValid) {
        SetUpAccordion('step2_accordion');
        $('#quote_tabs').tabs('option', 'disabled', []).tabs('select', 1).tabs('option', 'disabled', [0]);
    }
}

function StepBack() {
    $('#quote_tabs').tabs('option', 'disabled', []).tabs('select', 0).tabs('option', 'disabled', [1]);
}

function CalculateAlreadyCleaned() {
    ValidateForm();

    if (Page_IsValid) {
        //make ajax call to a web service to calculate the amount
        var _JsonData = '{ aEnquirer: {' +
                    'Email: \'' + $('#ctl00_EnquirerEmail').val() + '\', ' +
                    'Name: \'' + $('#ctl00_EnquirerName').val() + '\', ' +
                    'Tel: \'' + $('#ctl00_EnquirerTel').val() + '\'' +
                '}, ' +
                    'aExistingContractor: { ' +
                        'NumHoursPerCleaner: ' + $('#ctl00_NumHoursEach').val() + ', ' +
                        'NumCleaners: ' + $('#ctl00_NumCleaners').val() + ', ' +
                        'NumDaysPerWeek: ' + $('#ctl00_NumDaysPerWeek').val() +
                    '}' +
                '}';

        //Change cursor on page to hour glass
        $('body').css('cursor', 'wait');

        $.ajax({
            type: "POST",
            url: "SiteActions.asmx/GetQuotationForClientWithExistingCleaners",
            data: _JsonData,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function(obj) {
                $('#online_quote').dialog('close');
                $('#existing_cleaning_quote_price').dialog({ autoOpen: false, modal: true });
                $('#existing_cleaning_quote_price > #price').text('Unfortunately we were unable to calculate a price.  However, we would still love the opportunity of providing a quotation so one of our professional and friendly representatives ' +
                    'will contact you soon to arrange for a site visit after which we will be able to provide you with an exact price for the work that will be involved in cleaning your premises.');
                $('#existing_cleaning_quote_price').dialog('open');

                //change the cursor back
                $('body').css('cursor', 'auto');
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.debug(XMLHttpRequest);

                //change the cursor back
                $('body').css('cursor', 'auto');
            }
        });
    }
}

function ValidateForm() {
    //use the visibility of the accordion content to work out
    //which group to volidate
    if ($('#step2_accordion > .accordion_content:first:visible').size() != 0) {
        Page_ClientValidate('AlreadyCleanedGroup');
    } else {
        Page_ClientValidate('NewCleanGroup');
    }
}

function CalculateCleanCost() {
    ValidateForm();

    if (Page_IsValid) {
        //make ajax call to a web service to send out email

        var _JsonData = '{ aEnquirer: {' +
                    'Email: \'' + $('#ctl00_EnquirerEmail').val() + '\', ' +
                    'Name: \'' + $('#ctl00_EnquirerName').val() + '\', ' +
                    'Tel: \'' + $('#ctl00_EnquirerTel').val() + '\'' +
                '}, ' +
                    'aNewCleaningRequirement: { ' +
                        'FloorArea: ' + ($('#ctl00_SquareMetres').val() == '' ? 0 : $('#ctl00_SquareMetres').val()) + ', ' +
                        'NumRooms: ' + $('#ctl00_NumRooms').val() + ', ' +
                        'NumFloors: ' + $('#ctl00_NumFloors').val() + ', ' +
                        'NumToilets: ' + $('#ctl00_NumToilets').val() +
                    '}' +
                '}';

        //Change cursor on page to hour glass
        $('body').css('cursor', 'wait');

        $.ajax({
            type: "POST",
            url: "SiteActions.asmx/GetQuotationForClientWithoutCleaners",
            data: _JsonData,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function(obj) {
                $('#online_quote').dialog('close');
                $('#existing_cleaning_quote_price').dialog({ autoOpen: false, modal: true });
                $('#existing_cleaning_quote_price > #price').text('Unfortunately we were unable to calculate a price.  However, we would still love the opportunity of providing a quotation so one of our professional and friendly representatives ' +
                    'will contact you soon to arrange for a site visit after which we will be able to provide you with an exact price for the work that will be involved in cleaning your premises.');
                $('#existing_cleaning_quote_price > span:first').text('');
                $('#existing_cleaning_quote_price > #price_note').text('');
                $('#existing_cleaning_quote_price').dialog('open');

                //change the cursor back
                $('body').css('cursor', 'auto');
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.debug(XMLHttpRequest);
                //change the cursor back
                $('body').css('cursor', 'auto');
            }
        });
    }
}
