jQuery("html").addClass("js");

jQuery.noConflict();

jQuery(function ($) {
    Engine = {
        fixes: {
        // JavaScript Fixes :P
    },
    ui: {
        placeholder: function () {
            // $('input[placeholder], textarea[placeholder]').placeholder();
            $('#global-password-clear').show();
            $('#global-password').hide();

            $('#global-password-clear').focus(function () {
                $('#global-password-clear').hide();
                $('#global-password').show();
                $('#global-password').focus();
            });
            $('#global-password').blur(function () {
                if ($('#global-password').val() == '') {
                    $('#global-password-clear').show();
                    $('#global-password').hide();
                }
            });
            $('#global-internetnumber').each(function () {
                var default_value = this.value;
                $(this).focus(function () {
                    if (this.value == default_value) {
                        this.value = '';
                    }
                });
                $(this).blur(function () {
                    if (this.value == '') {
                        this.value = default_value;
                    }
                });
            });
        },
        bannercycle: function () {
            $('#banner').cycle({
                fx: 'fade',
                pause: true,
                timeout: 10000
                // choose your transition type, ex: fade, scrollUp, shuffle, etc...
                // See http://jquery.malsup.com/cycle/lite/ for features
            });
        },
        zebratable: function () {
            $("table.tablecolors tr:even").addClass("even");
            $("table.tablecolors tr:odd").addClass("odd"); //This is not required - you can avoid this if you have a table background
        },
        tooltip: function () {
            $(".info img[title]").tooltip();
        },
        popup: function () {
            $("a[rel='plantegning']").colorbox();
            $("a[rel='map']").colorbox({ iframe: true, innerWidth: 700, innerHeight: 500 });
            // $("a[rel='example1']").colorbox();
            // $("a[rel='example2']").colorbox({ transition: "fade" });
            // $("a[rel='example3']").colorbox({ transition: "none", width: "75%", height: "75%" });
            // $("a[rel='example4']").colorbox({ slideshow: true });
            // $(".example5").colorbox();
            // $(".example6").colorbox({ iframe: true, innerWidth: 425, innerHeight: 344 });
            // $(".example7").colorbox({ width: "80%", height: "80%", iframe: true });
            //$(".example8").colorbox({ width: "50%", inline: true, href: "#inline_example1" });
        },
        showhide: function () {
            if (window.location.href.indexOf('afd=') != '-1') {
                $('.departmentAppartments').css('display', 'block');
                $('.departmentInfo').css('display', 'block');
                $('.showInfo').remove();
                $('.showAppartments').remove();
            };
            $('.departmentShow span.showInfo').click(function () {
                //$(".departmentShow span.showInfo").toggleClass("show");
                $(this).toggleClass("show");
                $(this).next('.departmentInfo').slideToggle('slow', function () {
                    return false;
                });
            });
            $('.departmentShow span.showAppartments').click(function () {
                //$(".departmentShow span.showAppartments").toggleClass("show");
                $(this).toggleClass("show");
                $(this).parents('.department').find('.departmentAppartments').slideToggle('slow', function () {
                    return false;
                });
            });

            $('.faqWord h2.word').click(function () {
                $(this).toggleClass("active");
                $(this).next('.faqAnswer').slideToggle('slow', function () {
                    return false;
                });
            });

        },
        formvalidation: function () {
            if ($("input[name=SubmitForm]").length > 0) {

                $("input[name=SubmitForm]").parent().submit(function () {
                    // find the fields to validate and validate them
                    formValidationArray = [];
                    $('li.required').each(function (index) {
                        if ($(this).find('input:not([type=checkbox])').length > 0) {
                            if ($(this).find('input').val() != "") {
                                formValidationArray.push('true');
                            } else {
                                formValidationArray.push('false');
                            }
                        }
                        if ($(this).find('textarea').length > 0) {
                            if ($(this).find('textarea').val() != "") {
                                formValidationArray.push('true');
                            } else {
                                formValidationArray.push('false');
                            }
                        }
                    });

                    // loop through the fields and mark them if failed to validate and form the alert
                    var i = 0;
                    var validationMessage = 'Du mangler at udfylde disse felter:\n';
                    var fieldName = '';
                    for (i = 0; i < formValidationArray.length; i++) {
                        jq = 'li.required:eq(' + i + ')';
                        if (formValidationArray[i] == 'true') {
                            $(jq).removeClass('failed');
                        } else {
                            fieldName = $(jq).find('label').html();
                            validationMessage += '- ' + fieldName + '\n';
                            $(jq).addClass('failed');
                        }
                    }

                    // determin if the required form fields are valid
                    if ($("li.failed").length > 0) {
                        alert(validationMessage);
                        return false;
                    } else {
                        return true;
                    }

                });
            }
        }
    }
}
})

/*	On WINDOW loaded */
jQuery(window).load(function ($) {
    Engine.ui.placeholder();
    Engine.ui.bannercycle();
    Engine.ui.zebratable();
    Engine.ui.tooltip();
    Engine.ui.popup();
    Engine.ui.formvalidation();
});

/*	On DOCUMENT loaded */
jQuery(document).ready(function ($) {
    Engine.ui.showhide();
    $('.faqAnswer').attr('style', 'display:none;');
});

/*	DW javascript */
function isRadioButtonChecked(theOption, theForm) {
    if (theForm.elements[theOption].checked) {
        return true;
    }
    else {
        for (i = 0; i < theForm.elements[theOption].length; i++) {
            if (theForm.elements[theOption][i].checked) {
                return true;
            }
        }
    }
    return false;
}

function validateEmail(email) {
    var regExp = /^[\w\-_]+(\.[\w\-_]+)*@[\w\-_]+(\.[\w\-_]+)*\.[a-z]{2,4}$/i;
    return regExp.test(email);
}
/* Extranet form login */
function checkform() {
    if (document.ExtUserForm.ForgotPassword.checked) {
        if ((document.ExtUserForm.ForgotPassword.checked) && (document.ExtUserForm.Username.value.length <= 0)) {
            alert('To recieve your password, please specify username.');
            document.ExtUserForm.Username.focus();
            return false;
        }
    }
    else {
        if (document.ExtUserForm.Username.value.length <= 0) {
            alert('Specify username');
            document.ExtUserForm.Username.focus();
            return false;
        }

        if (document.ExtUserForm.Password.value.length <= 0) {
            alert('Specify password');
            document.ExtUserForm.Password.focus();
            return false;
        }
    }

    return true;
}

function ToggleForgotPassword() {
    var uform = document.forms["ExtUserForm"];
    uform.elements["Password"].disabled = uform.elements["ForgotPassword"].checked;
    uform.elements["Username"].innerText = "E-mail address";
}
/* Create user extension */
function randomPassword(length) {
    chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    pass = "";
    for (x = 0; x < length; x++) {
        i = Math.floor(Math.random() * 62);
        pass += chars.charAt(i);
    }
    return pass;
}
function formSubmit() {
    document.UserManagementEditForm.UserManagement_Form_NewPassword.value = randomPassword(8);
    document.UserManagementEditForm.UserManagement_Form_NewPasswordConfirm.value = document.UserManagementEditForm.UserManagement_Form_NewPassword.value;
    document.UserManagementEditForm.UserManagement_Form_Fax.value = document.UserManagementEditForm.UserManagement_Form_NewPassword.value;
    document.UserManagementEditForm.UserManagement_Form_UserName.value = document.UserManagementEditForm.UserManagement_Form_Email.value;
    document.UserManagementEditForm.UserManagement_Form_Name.value = document.UserManagementEditForm.UserManagement_Form_Name1.value + " " + document.UserManagementEditForm.UserManagement_Form_Name2.value;
    if (document.UserManagementEditForm.iAmHCP.checked)
        document.UserManagementEditForm.submit();
    return false;
}

