﻿/// <reference path="jquery-1.6.min.js" />

// attach a submit handler to the form
$("#loginForm").submit(function (event) {
    // stop form from submitting normally
    event.preventDefault();

    // get some values from elements on the page:
    var $form = $(this),
            name = $form.find('input[name="username"]').val(),
            password = $form.find('input[name="password"]').val(),
            omgeving = $form.find("select[name='omgeving']").val(),
            omgevingNaam = $form.find("select[name='omgeving'] option:selected").text()
            url = $form.attr('action');

    // Send the data using post and alert the results
    $.post(url, { username: name, password: password, omgeving: omgeving },
            function (data) {
                if (data == "True") {
                    if (omgevingNaam == "Haarlem")
                        $form.attr("Action", "https://agentdesktop.2contact.com/Login.aspx");
                    else if (omgevingNaam == "'s Hertogenbosch")
                        $form.attr("Action", "https://denbosch.2contact.com/login.aspx");
                    else if (omgevingNaam == "Extranet")
                        $form.attr("Action", "https://extranet.2contact.com/login.aspx");
                    $form.unbind('submit').submit();
                }
                else
                    alert("Ongeldige inloggegevens");
                    }
            );
        }
);

        $(document).ready(function () {
            var nameInput = $("#loginForm").find('input[name="username"]');
            nameInput.bind('focus', function () {
                if ($(this).val() == "Gebruikersnaam")
                    $(this).val('')
            });
            nameInput.bind('blur', function () {
                if(!$(this).val())
                    $(this).val('Gebruikersnaam');
            });
            var passwordInput = $("#loginForm").find('input[name="password"]');
            passwordInput.bind('focus', function () {
                if ($(this).val() == "Wachtwoord")
                    $(this).val('')
            });
            passwordInput.bind('blur', function () {
                if (!$(this).val())
                    $(this).val('Wachtwoord');
            });
        });
