﻿/// <reference path="assets/js/jquery-1.4.1.js" />

    $.fn.serializeObject = function() {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            alert("in serializeObject");
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

    $(document).ready(function(){
        $("a.lnkEula").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'speedIn': 200,
            'speedOut': 200,
            'autoDimensions': false,
            'height':   400,
            'width':    500
        });

        $signUpInputs = $('.signUpInput');
        $txtCustomerCode = $('.txtCustomerCode').eq(0);
        $tboxEmail = $('.tboxEmail').eq(0);
        $tboxFirstName = $('.tboxFirstName').eq(0);
        $tboxLastName = $('.tboxLastName').eq(0);
        $tboxPhone = $('.tboxPhone').eq(0);

        $txtCustomerCode.bind('blur', function(){
            findByCustomerCode();
        });

        $tboxEmail.bind('blur', function(){
            checkUsernameAvailability();
        });
    });


    var previousCustomerCode = '',
        previousUsername = '',
        $signUpInputs = null,
        $txtCustomerCode = null;
        $tboxEmail = null,
        $tboxFirstName = null,
        $tboxLastName = null,
        $tboxPhone = null;

    function findByCustomerCode() {
        customerCode = $txtCustomerCode.val().trim();

        if (customerCode != previousCustomerCode && customerCode != '') {
            previousCustomerCode = customerCode; // do not call again for the same code

            $.ajax({
                url: 'default.aspx/FindUserByCustomerCode',
                data: '{ "dmsCustomerId": "' + customerCode + '" }',
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                success: function(response) {
                    disableSignUpInputs();

                    var Customer = response.d;
                    if (Customer != null) {
                        $tboxEmail.val(Customer.CustomerEmail);
                        $tboxFirstName.val(Customer.FirstName);
                        $tboxLastName.val(Customer.LastName);
                        $tboxPhone.val(Customer.CustomerVoicePhone);

                        if (Customer.CustomerEmail == '') { 
                            $tboxEmail.removeAttr('disabled');
                        }

                        if (Customer.CustomerVoicePhone == '' ||
                            Customer.CustomerVoicePhone == '0')
                        {
                            $tboxPhone.removeAttr('disabled').val('');
                        }

                        $('#divClearForm').show();
                        $('#hdnFoundCustomer').val('true');
                    } else {
                        alert("We cannot match the customer code you entered. " +
                            "Please check that you entered it correctly or enter the required contact information and try again.");
                        
                        enableSignUpInputs();
                        clearSignUpInputs();
                        $('#hdnFoundCustomer').val('false');
                    }
                }
            });
        }
    }

    function disableSignUpInputs() {
        $signUpInputs.attr('disabled', true);
    }

    function enableSignUpInputs() {
        $signUpInputs.removeAttr('disabled');
    }

    function clearSignUpInputs() {
        // not customer code
        $tboxEmail.removeAttr('disabled').val('');
        $tboxFirstName.removeAttr('disabled').val('');
        $tboxLastName.removeAttr('disabled').val('');
        $tboxPhone.removeAttr('disabled').val('');
    }

    function checkUsernameAvailability() {
        var username = $tboxEmail.val();

        if (username != '' && username != previousUsername) {
            previousUsername = username;

            $.ajax({
                url: 'default.aspx/CheckUsernameAvailability',
                data: '{ "username": "' + username + '" }',
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    var isAvailable = response.d;

                    $('#emailAvailability')
                        .html(isAvailable ? 'Available' : 'Unavailable')
                        .css({
                            'background-color': (isAvailable ? 'green' : 'red')
                         });
                }
            });
        }
    }
