﻿/// <reference path="jquery/jquery-1.4.1-vsdoc.js" />
$j(document).ready(function() {

    $j("#sendUserFeedback").addClass("sendUserFeedbackLink");

    var initialiseFeedbackDialog = function initialiseUserFeedbackDialog() {
        var firstName = $j(".userFeedbackContent [id$='_FirstNameTextBox']"),
        lastName = $j(".userFeedbackContent [id$='_LastNameTextBox']"),
        emailAddress = $j(".userFeedbackContent [id$='_EmailAddressTextBox']"),
        location = $j(".userFeedbackContent [id$='_LocationDropDown']"),
        networkYes = $j(".userFeedbackContent [id$='_NetworkRadioButtonYes']"),
        networkNo = $j(".userFeedbackContent [id$='_NetworkRadioButtonNo']"),
        network = $j(".userFeedbackContent [id$='_NetworkSelector']"),
        allValidationFields = $j([]).add(firstName).add(lastName).add(emailAddress).add(location),
        form = getForm();

        if (networkNo.is(':checked')) {
            network.attr("disabled", "disabled");
        }

        var dialogSelector = "div.userFeedbackDialog",
            dialogPlaceHolder = "div#userFeedbackPlaceHolder",
            dialogContentSelector = "div.userFeedbackContent",
            dialogConfirmSelector = "div#userFeedbackConfirmDialog",
            dialogConfirmClass = "userFeedbackConfirmDialog confirmDialog";

        $j(".userFeedbackContent").dialog({
            autoOpen: false,
            draggable: false,
            resizable: false,
            zIndex: 600000,
            height: 655,
            width: 800,
            modal: true,
            dialogClass: "userFeedbackDialog dialogForm",
            close: function(event, ui) {
                closeTermsAndConditionsDialogForm();
                cleanupDialogForm(dialogPlaceHolder, dialogContentSelector);
            },
            buttons: { 'Cancel': function() {
                closeDialogForm(dialogContentSelector);
            },
                'Send feedback': function() {
                    insertLoadingImage(dialogSelector);
                    var isValid = true;
                    allValidationFields.removeClass("ui-state-error");
                    isValid = isValid && validateLocation(dialogSelector, location, "Please select a valid location");
                    isValid = isValid && checkRegexp(dialogSelector, emailAddress, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Please provide a valid email address");
                    if (isValid) {
                        submitDialogForm(dialogPlaceHolder,
                                         dialogSelector,
                                         dialogContentSelector,
                                         dialogConfirmSelector,
                                         dialogConfirmClass,
                                         null,
                                         "Sorry, we were unable to submit your feedback",
                                         getPageUrl(),
                                         getForm())
                    } else {
                        removeLoadingImage(dialogSelector);
                    }
                }
            }
        })

        initialiseTermsAndConditionsDialog();

        allValidationFields.focus(function() {
            $j(this).removeClass("ui-state-error");
        })

        networkNo.click(function() {
            network.attr("disabled", "disabled");
        })

        networkYes.click(function() {
            network.removeAttr("disabled");
        })

        var termsAndConditionsLink = $j(".userFeedbackContent div.termsAndConditionsLink span");


        termsAndConditionsLink.click(function() {
            openTermsAndConditionsDialogForm();
        })


        function validateLocation(dialogSelector, field, msg) {
            if (field.val() == "-") {
                field.addClass('ui-state-error');
                updateTips(dialogSelector, msg);
                return false;
            } else {
                return true;
            }
        }

    }


    function getForm() {
        return $j("#userFeedbackPlaceHolder #virginDialogForm");
    }

    function getPageUrl() {
        return "/templates/UserFeedbackDialog.aspx" + window.location.search;
    }

    function initialiseTermsAndConditionsDialog() {
        initialiseConfirmDialog("div#userFeedbackTermsDialog",
                                "userFeedbackTermsDialog messageDialog",
                                { height: 400,
                                    width: 300,
                                    modal: false,
                                    autoHide: false,
                                    bgiframe: true,
                                    position: 'center'
                                }
                                );

    }

    function openTermsAndConditionsDialogForm() {
        openDialog("div#userFeedbackTermsDialog");
    }

    function closeTermsAndConditionsDialogForm() {
        closeDialog("div#userFeedbackTermsDialog");
    }

    $j(".sendUserFeedbackLink").click(function() {
        openDialogForm("div#userFeedbackPlaceHolder",
                       "div.userFeedbackDialog",
                       "div.userFeedbackContent",
                       "div#userFeedbackLoadDialog",
                       "loadDialog",
                       initialiseFeedbackDialog,
                        "Sorry, but there was a problem loading the user feedback form.",
                        getPageUrl(),
                        "#userFeedbackPlaceHolder #virginDialogForm");
    });


});

