﻿$(document).ready(function() {
    VoucherCheck();
});

function VoucherCheck() {

    var voucherCheckMobileNumberDefault = "Mobil nummer";
    $("#txtVoucherCheckMobileNumber").focus(function() {
        if ($(this).val() == voucherCheckMobileNumberDefault) {
            $(this).val("");
        }
    });
    $("#txtVoucherCheckMobileNumber").blur(function() {
        if ($(this).val() == "") {
            $(this).val(voucherCheckMobileNumberDefault);
        }
    });

    var voucherCheckCodeDefault = "Kode";
    $("#txtVoucherCheckCode").focus(function() {
        if ($(this).val() == voucherCheckCodeDefault) {
            $(this).val("");
        }
    });
    $("#txtVoucherCheckCode").blur(function() {
        if ($(this).val() == "") {
            $(this).val(voucherCheckCodeDefault);
            $(this).disabled = 'disable';
        }
    });

    $("#btnVoucherCheckButton").click(function() {
        //check for integer characters
        var cellPhoneValidator = /(^\d*$)/;
        var cellPhoneValid = cellPhoneValidator.test($("#txtVoucherCheckMobileNumber").val());

        if (cellPhoneValid && $("#txtVoucherCheckCode").val() != "") {
            $("#VoucherCheckStatus").html('Vent venligst...');

            var dataString = "CampaignID='" + $("#hdnVoucherCheckCampaignID").val() +
                            "'&CellPhoneNumber='" + $("#txtVoucherCheckMobileNumber").val() +
                            "'&VoucherCode='" + $("#txtVoucherCheckCode").val() + "'";
            $.ajax(
                {
                    type: "GET",
                    url: "http://vouchercheck.aller.dk/webservices/VoucherCheck.asmx/GetRedirectURL",
                    data: dataString,
                    contentType: "application/json; charset=utf-8",
                    dataType: "jsonp",
                    success: function(response) {
                        if (response.d.toLowerCase().indexOf('http://') > -1) {
                            window.location = response.d;
                        }
                        else {
                            $("#VoucherCheckStatus").html(response.d);
                        }

                    },
                    error: function(response) {
                        $("#VoucherCheckStatus").html('Der opstod en fejl, pr&oslash;v igen senere.<br />\n' + response);
                    }
                });

        }
        else {
            var errorMessage = "";
            if (!cellPhoneValid) {
                errorMessage += "Det indtastede mobil telefon nummer er ikke tilladt.<br />\n";
            }
            if ($("#txtVoucherCheckCode").val() == "") {
                errorMessage += "Du har ikke indtastet en kode.<br />\n";
            }
            errorMessage += "Mobil nummer må kun være tal og kode feltet må ikke være tomt.<br />\n";

            $("#VoucherCheckStatus").html(errorMessage);
        }


    });

}