﻿//<![CDATA[

// NOTE: Table rows with class .hidden are actually hidden in each individual
//       page where this script is called and where necessary with -$('.hidden').hide();-


displayAddressForm = function(clear) {

    if (clear) {
        $('#addressForm input').val('');
        $('input[id$=txtAddressId]').val('0');

        $("[id$='lblErrMsg']")
            .html('')
            .css('display', 'none');
        
        $('tr.hidden').hide();

        $('h3#header-title').text('Enter a new address');
        
        $('tr#address_info').show();
    } else {
        $('h3#header-title').text('Edit this address');
    
        $('tr#address_info').hide();
    }

    $('#addressBook').fadeOut(300, function() {
        $('#addressForm').fadeIn(300);
    });

    return false;
};

hideAddressForm = function() {

    var errMsg = $('p#errMsg').length > 0 ? $('p#errMsg') : $("[id$='lblErrMsg']");

    errMsg.hide();

    $('#addressForm').fadeOut(300, function() {
        $('#addressBook').fadeIn(300);
    });

    return false;
};

getAddress = function(addressId) {

    $.ajax({
        url: '/DeliveryAddress.ashx',
        cache: false,
        data: {
            method: 'getAddress',
            addressId: addressId
        },
        success: function(data) {
            var a = eval('(' + data + ')');

            $('input[id$=txtAddressId]').val(a.id);
            $('input[id$=txtFName]').val(a.firstname);
            $('input[id$=txtLName]').val(a.lastname);
            $('input[id$=txtContactNo]').val(a.contact);
            $('input[id$=txtHouseNo]').val(a.houseNo);
            $('input[id$=txtAddr1]').val(a.addr1);
            $('input[id$=txtAddr2]').val(a.addr2);
            $('input[id$=txtCity]').val(a.city);
            $('input[id$=txtCounty]').val(a.county);
            $('input[id$=txtPostcode]').val(a.postcode);

            $('tr.hidden').show();

            displayAddressForm(false);
        }
    });

    return false;
};

lookupPostcode = function() {

    var errMsg = null;

    errMsg = $('p#errMsg').length > 0 ? $('p#errMsg') : $("[id$='lblErrMsg']");

    var txtPostcode = $('input[id$=txtPostcode]');

    if (txtPostcode.val().length > 0) {

        $.ajax({
            url: '/DeliveryAddress.ashx',
            cache: false,
            data: {
                method: 'lookupPostcode',
                postcode: txtPostcode.val()
            },
            success: function(data) {
                var a = eval('(' + data + ')');

                if (a.isError == 'true') {

                    $('tr#address_info').show();
                    $('tr.hidden > td > input').each(function() {
                        $(this).val('');
                    });

                    if (errMsg != null) {
                        errMsg
                            .html('The address could not be found!<br />Please try inserting a different postcode or enter your address manually.')
                            .fadeIn(300);
                    } /*else {
                        alert('Cannot find error msg');
                    }*/
                } else {
                    $('input[id$=txtAddr1]').val(a.addr1);
                    $('input[id$=txtAddr2]').val(a.addr2);
                    $('input[id$=txtCity]').val(a.city);
                    $('input[id$=txtCounty]').val(a.county);
                    $('input[id$=txtPostcode]').val(a.postcode);

                    $('tr#address_info').hide();
                }

                $('tr.hidden').show();

                $('input[id$=txtHouseNo]')
                    .val('')
                    .focus();

                $('a#btnFind').html('Search again');
            }
        });

    } else {
        if (errMsg != null) {
            errMsg
            .html('Please enter a postcode')
            .fadeIn(300);
        } else {
            alert('Please enter a postcode');
        }
    }

    return false;
};

dispatch = function() {
    if (valForm())
        return confirm('Are you sure you want to dispatch to this address?');
    else
        return false;

};

valForm = function () {
    var emailFilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
    var errs = new Array();

    if ($("[id$='txtFName']").length > 0) {
        if ($("[id$='txtFName']").val().length < 1)
            errs[errs.length] = '<li>Please enter your first name</li>';
    }

    if ($("[id$='txtLName']").length > 0) {
        if ($("[id$='txtLName']").val().length < 1)
            errs[errs.length] = '<li>Please enter your last name</li>';
    }

    if ($("[id$='txtEmail']").length > 0) {
        if ($("[id$='txtEmail']").val().length < 1) {
            errs[errs.length] = '<li>Please enter your email address</li>';
        } else {
            if (!emailFilter.test($("[id$='txtEmail']").val())) {
                errs[errs.length] = '<li>Please enter a valid email address (e.g. myname@googlemail.com)</li>';
            } else {
                if ($("[id$='txtEmailConfirm']").val().length < 1) {
                    errs[errs.length] = '<li>Please confirm your email</li>';
                } else {
                    if ($("[id$='txtEmail']").val() != $("[id$='txtEmailConfirm']").val())
                        errs[errs.length] = '<li>The email addresses entered do not match</li>';
                }
            }
        }
    }

    if ($("[id$='txtPwd']").length > 0) {
        if ($("[id$='txtPwd']").val().length < 1) {
            errs[errs.length] = '<li>Please enter a password</li>';
        } else {
            if ($("[id$='txtPwdConfirm']").val().length < 1) {
                errs[errs.length] = '<li>Please confirm the password</li>';
            } else {
                if ($("[id$='txtPwd']").val() != $("[id$='txtPwdConfirm']").val())
                    errs[errs.length] = '<li>The passwords entered do not match</li>';
            }
        }
    }

    if ($("[id$='txtPhone']").length > 0) {
        if ($("[id$='txtPhone']").val().length < 1) {
            errs[errs.length] = '<li>Please enter your phone number</li>';
        } else {
            var phone = $("[id$='txtPhone']").val().replace(/ /g, '');

            if (phone.substring(0, 1) != '0' || phone.length < 10 || isNaN(phone))
                errs[errs.length] = '<li>Please enter a valid phone number (e.g. 0044 01494 777777)</li>';
        }
    }

    if ($("[id$='txtContactNo']").length > 0) {
        if ($("[id$='txtContactNo']").val().length < 1) {
            errs[errs.length] = '<li>Please enter the contact number</li>';
        } else {
            var phone = $("[id$='txtContactNo']").val().replace(/ /g, '');

            if (phone.substring(0, 1) != '0' || phone.length < 10 || isNaN(phone))
                errs[errs.length] = '<li>Please enter a valid contact number (e.g. 0044 01494 777777)</li>';
        }
    }

    if ($("[id$='txtPostcode']").val().length < 1) {
        errs[errs.length] = '<li>Please enter the postcode</li>';
    } else {
        //if ($('tr.hidden').css('display') != 'none') {

        if ($("[id$='txtHouseNo']").val().length < 1)
            errs[errs.length] = '<li>Please enter the house name/ number</li>';
        
        if ($("[id$='txtAddr1']").val().length < 1)
            errs[errs.length] = '<li>Please enter the first line of the address</li>';

        if ($("[id$='txtCity']").val().length < 1)
            errs[errs.length] = '<li>Please enter the town/ city</li>';

        if ($("[id$='txtCounty']").val().length < 1)
            errs[errs.length] = '<li>Please enter the county</li>';
    } //else {
    //errs[errs.length] = '<li>Please enter a valid address</li>';
    //}

    if (errs.length > 0) {
        //var error = new StringBuilder();
        //error.clear();

        var error = [];

        error.push('<ul>');

        for (var i = 0; i < errs.length; i++)
            error.push(errs[i]);

        error.push('</ul>');

        $("[id$='lblErrMsg']")
            .html(error.join(''))
            .fadeIn(500)
            .css('display', 'block');

        window.scrollTo(0, 0);

        return false;
    } else {
        return true;
    }
};

//]]>
