﻿window.isScriptLiveMode = true;


$(document).ready(function() {

    var elementsToStyle = $('input[type="text"],select,textarea');

    if (elementsToStyle != null && elementsToStyle.length) {

        $('input[type="text"],select,textarea').addClass("idleField");
        $('input[type="text"],select,textarea').focus(function() {
            $(this).removeClass("idleField").addClass("focusField");
            if ($(this).hasClass("removeDefault")) {
                if (this.value == this.defaultValue) {
                    this.value = '';
                }
            }
            if (this.value != this.defaultValue) {
                this.select();
            }
        });
        $('input[type="text"],select,textarea').blur(function() {
            $(this).removeClass("focusField").addClass("idleField");
            if ($.trim(this.value) == '') {
                this.value = (this.defaultValue ? this.defaultValue : '');
            }
        });

    }

    var textElements = $('input[type="text"]');

    if (textElements != null && textElements.length) {
        $('input[type="text"]').focus(function() {
            if ($(this).hasClass("removeDefault") && this.value == this.defaultValue) this.value = '';
        });
        $('input[type="text"]').blur(function() {
            if (this.value == '') this.value = (this.defaultValue ? this.defaultValue : '');
        });
    }

});


function ToggleOwnPrice(hide) {
    if (hide) {
        $("[id$='_divOwnPrice']").hide("normal");
    }
    else {
        $("[id$='_divOwnPrice']").show("normal");
    }
}


function SwitchDelivery(isDelivery) {
    $('#pnlDeliveryPanel').toggle("normal");
    if (isDelivery) {
        $("input[id$='_txtPostcode']").removeAttr('disabled').css('background', '#EFF5FF');
    }
    else {
        $("input[id$='_txtPostcode']").attr('disabled', 'disabled').css('background', '#DEE4EE');
    }
}

function InvokeCalendar() {
    $("[id$='_datepicker']").click();
}


function CheckPostcode() {

    var ajaxUrl = window.isScriptLiveMode
        ? 'http://' + window.location.host + '/Tulips/SpecialPages/WebMethods.asmx/CheckPostcode'
        : 'http://' + window.location.host + '/Tulips2/Tulips/SpecialPages/WebMethods.asmx/CheckPostcode';

    $.ajax({
        type: 'POST',
        url: ajaxUrl,
        data: '{"postcode" : "' + $("[id$='_txtPostcode']").val() + '"}',
        dataType: 'json',
        async: true,
        contentType: 'application/json; charset=utf-8',
        beforeSend: function() {
            $('#resultDiv').empty();
            $('#postcodeSpinner').show();
        },
        complete: function() {
            $('#postcodeSpinner').hide();
        },
        success: function(result) {
            $('#resultDiv').empty();
            var response = (typeof result.d) == 'string' ? eval('(' + result.d + ')') : result.d;
            $('#resultDiv').text(response.Value);
        },
        error: function(xhr, status, error) {
            $('#resultDiv').html('Error occured.');
        }
    });
}


function GetContactEmail() {
    $.ajax({
        type: 'POST',
        url: 'Tulips/SpecialPages/WebMethods.asmx/GetContactEmail',
        data: "{}",
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        async: true,
        success: function(result) {
            $('#contact_email').html(result.d);
        }
    });
}


function DisableClosedDays(calendarDate) {
    for (i = 0; i < disabledDays.length; i++) {
        if (calendarDate.getDate() == disabledDays[i][0]
            && calendarDate.getMonth() + 1 == disabledDays[i][1]
            && calendarDate.getFullYear() == disabledDays[i][2]) {
            return [false, ''];
        }
    }
    return [true, ''];
}


function SetPredefinedDate(datetype) {
    var today = new Date();
    var d = new Date();
    if (datetype == 1) {
        d.setDate(today.getDate() + 1);
    }
    else if (datetype == 0) {
        d.setDate(today.getDate() + 2);
    }

    $("[id$='_datepicker']").val((d.getDate()).toString() + '/' + (d.getMonth() + 1).toString() + '/' + d.getFullYear().toString());
}


function DebugArrays() {
    alert("Themes = " + arrThemes + "\r\n" + "Varieties = " + arrVarieties + "\r\n" + "Ocassions = " + arrOcassions + "\r\n" + "Types = " + arrTypes);
}


function ItemIndexInArray(arr, item) {
    var index = -1;
    for (i = 0; i < arr.length; i++) {
        if (arr[i].key1 == item) {
            index = i;
            break;
        }
    }

    return index;
}


