﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

$(document).ready(function () {
    $('.CustomerType').change(function () {
        HandleBusinessFields();
    });
});

function RegTrack() {
    if (Page_IsValid) return;
    var emospro = {};
    var csid = '0';
    if ($("#csid input").length == 1 && $("#csid input").val() != '') {
        csid = "SID" + $("#csid input").val();
    }
    var pageId = $("#EcondaHelperPageId").html();
    var content = $("#EcondaHelperContent").html();
    var langId = $("#EcondaHelperLangId").html();
    var sideId = $("#EcondaHelperSideId").html();
    emospro.content = content;
    emospro.pageId = pageId;
    emospro.siteid = sideId;
    emospro.langid = langId;
    emospro.countryid = sideId;
    emospro.register = [[csid, '2']];
    window.emosPropertiesEvent(emospro);
}

function HandleBusinessFields() {
    if ($('.CustomerType').val() == "20") {
        BusinessCustomer();
    }
    else if ($('.CustomerType').val() == "40") {
        ClubCustomer();
    }
    else if ($('.CustomerType').val() == "50") {
        AdministrationCustomer();
    }
    else if ($('.CustomerType').val() == "10") {
        PrivateCustomer();
    }
}

function ValidationAnchor() {
    if (Page_Validators.length == 0) return;
    var vGroup = Page_Validators[0].validationGroup;
    if (!Page_IsValid) {
        TrackData(vGroup, 'SaveClickInvalid');
        console.log(vGroup + " 'SaveClickInvalid'");
        $.each(Page_Validators, function (index, value) {
            if (!value.isvalid) {
                var i = value.controltovalidate.split('_');
                var input = i[i.length - 1];
                TrackData(value.validationGroup, 'SaveClickInvalidDetails', input);
                console.log(value.validationGroup + " 'SaveClickInvalidDetails' " + input);
            }
        });
        $("#ErrorMessage").show();
        window.location.href = '#info';
    }
    else {
        TrackData(vGroup, 'SaveClickValid');
        console.log(vGroup + " 'SaveClick'");
    }
}

function PrivateCustomer() {
    $('.AdditionalBusinessData').hide();
    $('.BranchField').hide();
    ResetCompanyValidator(true);
    ResetVatNoValidator(true);
    ResetBranchOfBusinessValidator(true);
    if ($("body").hasClass("ESES")) {
        $("#formrowNifCif").show();
        $("#hintPhoneNumber").css("margin-top", "252px");
    }
}

function BusinessCustomer() {
    $('.AdditionalBusinessData').show();
    $('.BranchField').show();
    ResetCompanyValidator(false);
    ResetVatNoValidator(false);
    ResetBranchOfBusinessValidator(false);
    $('.companyText').show();
    $('.clubText').hide();
    $('.administrationText').hide();
    if ($("body").hasClass("ESES")) {
        $("#formrowNifCif").hide();
        $("#formrowNifCif").val('');
        $("#hintPhoneNumber").css("margin-top", "222px");
    }
}

function ClubCustomer() {
    $('.AdditionalBusinessData').show();
    $('.BranchField').hide();
    ResetCompanyValidator(false);
    ResetVatNoValidator(true);
    ResetBranchOfBusinessValidator(true);
    $('.companyText').hide();
    $('.clubText').show();
    $('.administrationText').hide();
    if ($("body").hasClass("ESES")) {
        $("#formrowNifCif").hide();
        $("#formrowNifCif").val('');
        $("#hintPhoneNumber").css("margin-top", "222px");
    }
}

function AdministrationCustomer() {
    $('.AdditionalBusinessData').show();
    $('.BranchField').hide();
    ResetCompanyValidator(false);
    ResetVatNoValidator(true);
    ResetBranchOfBusinessValidator(true);
    $('.companyText').hide();
    $('.clubText').hide();
    $('.administrationText').show();
    if ($("body").hasClass("ESES")) {

        $("#formrowNifCif").hide();
        $("#formrowNifCif").val('');
        $("#hintPhoneNumber").css("margin-top", "222px");
    }
}

function ResetCompanyValidator(bool) {
    if ($('.CvCompany').length != 0 && $('.TbCompany').length != 0) {
        $('.CvCompany')[0].isvalid = bool;
        if (bool) {
            $('.TbCompany').val('');
        }
        ResetValidationSymbol($('.TbCompany')[0].id);
    }
}

function ResetVatNoValidator(bool) {
    if ($('.CvVatNo').length != 0 && $('.TbVatNo').length != 0) {
        $('.CvVatNo')[0].isvalid = bool;
        if (bool) {
            $('.TbVatNo').val('');
        }
        ResetValidationSymbol($('.TbVatNo')[0].id);
    }
}

function ResetBranchOfBusinessValidator(bool) {
    if ($('.CvBranchOfBusiness').length != 0 && $('.BranchOfBusiness').length != 0) {
        $('.CvBranchOfBusiness')[0].isvalid = bool;
        if (bool) {
            $('.BranchOfBusiness').val('');
        }
        ResetValidationSymbol($('.BranchOfBusiness')[0].id);
    }
}

function TrackData(category, action, label, value) {
    if (label != '' && value != '') {
        _gaq.push(['_trackEvent', category, action, label, value]);
    }
    else if (label != '') {
        _gaq.push(['_trackEvent', category, action, label]);
    }
    else {
        _gaq.push(['_trackEvent', category, action]);
    }
}


$(document).ready(function () {
    $('input.PasswordToValidate').keyup(function () {
        var o = $('input.PasswordToValidate');
        if (o.length > 0) {
            ValidatePassword(o[0].value);
        }
    });
    $('.ShowInfoCustomerType').focus(function () {
        $("#hintCustomerType").fadeIn("slow");
    });
    $('.ShowInfoCustomerType').blur(function () {
        $("#hintCustomerType").hide();
    });
    $('.ShowInfoCompany').focus(function () {
        $("#hintCompany").fadeIn("slow");
    });
    $('.ShowInfoCompany').blur(function () {
        $("#hintCompany").hide();
    });
    $('.ShowInfoVatNo').focus(function () {
        $("#hintVatNo").fadeIn("slow");
    });
    $('.ShowInfoVatNo').blur(function () {
        $("#hintVatNo").hide();
    });
    $('.ShowInfoBranchOfBusiness').focus(function () {
        $("#hintBranchOfBusiness").fadeIn("slow");
    });
    $('.ShowInfoBranchOfBusiness').blur(function () {
        $("#hintBranchOfBusiness").hide();
    });
    $('.ShowInfoSalutation').focus(function () {
        $("#hintSalutation").fadeIn("slow");
    });
    $('.ShowInfoSalutation').blur(function () {
        $("#hintSalutation").hide();
    });
    $('.ShowInfoFirstName').focus(function () {
        $("#hintFirstName").fadeIn("slow");
    });
    $('.ShowInfoFirstName').blur(function () {
        $("#hintFirstName").hide();
    });
    $('.ShowInfoLastName').focus(function () {
        $("#hintLastName").fadeIn("slow");
    });
    $('.ShowInfoLastName').blur(function () {
        $("#hintLastName").hide();
    });
    $('.ShowInfoStreet').focus(function () {
        $("#hintStreet").fadeIn("slow");
    });
    $('.ShowInfoStreet').blur(function () {
        $("#hintStreet").hide();
    });
    $('.ShowInfoZip').focus(function () {
        $("#hintZip").fadeIn("slow");
    });
    $('.ShowInfoZip').blur(function () {
        $("#hintZip").hide();
    });
    $('.ShowInfoCity').focus(function () {
        $("#hintCity").fadeIn("slow");
    });
    $('.ShowInfoCity').blur(function () {
        $("#hintCity").hide();
    });
    $('.ShowInfoTaxnumber').focus(function () {
        $("#hintTaxnumber").fadeIn("slow");
    });
    $('.ShowInfoTaxnumber').blur(function () {
        $("#hintTaxnumber").hide();
    });
    $('.ShowInfoPhoneNumber').focus(function () {
        $("#hintPhoneNumber").fadeIn("slow");
    });
    $('.ShowInfoPhoneNumber').blur(function () {
        $("#hintPhoneNumber").hide();
    });
    $('.ShowInfoTaxnumber2').focus(function () {
        $("#hintTaxnumber2").fadeIn("slow");
    });
    $('.ShowInfoTaxnumber2').blur(function () {
        $("#hintTaxnumber2").hide();
    });
    $('.ShowInfoEmail').focus(function () {
        $("#hintMail").fadeIn("slow");
    });
    $('.ShowInfoEmail').blur(function () {
        $("#hintMail").hide();
    });
    $('.ShowInfoRepeatEmail').focus(function () {
        $("#hintRepeatMail").fadeIn("slow");
    });
    $('.ShowInfoRepeatEmail').blur(function () {
        $("#hintRepeatMail").hide();
    });
    $('.ShowInfoUsername').focus(function () {
        $("#hintUsername").fadeIn("slow");
    });
    $('.ShowInfoUsername').blur(function () {
        $("#hintUsername").hide();
    });
    $('.ShowInfoPassword').focus(function () {
        $("#hintPassword").fadeIn("slow");
    });
    $('.ShowInfoPassword').blur(function () {
        $("#hintPassword").hide();
    });
    $('.ShowInfoRepeatPassword').focus(function () {
        $("#hintRepeatPassword").fadeIn("slow");
    });
    $('.ShowInfoRepeatPassword').blur(function () {
        $("#hintRepeatPassword").hide();
    });
    $('.ShowInfoCurrentPassword').focus(function () {
        $("#hintCurrentPassword").fadeIn("slow");
    });
    $('.ShowInfoCurrentPassword').blur(function () {
        $("#hintCurrentPassword").hide();
    });
    $('.ShowInfoBirthdayDay').focus(function () {
        $("#hintBirthdayDay").fadeIn("slow");
    });
    $('.ShowInfoBirthdayDay').blur(function () {
        $("#hintBirthdayDay").hide();
    });
    $('.ShowInfoBirthdayMonth').focus(function () {
        $("#hintBirthdayMonth").fadeIn("slow");
    });
    $('.ShowInfoBirthdayMonth').blur(function () {
        $("#hintBirthdayMonth").hide();
    });
    $('.ShowInfoBirthdayYear').focus(function () {
        $("#hintBirthdayYear").fadeIn("slow");
    });
    $('.ShowInfoBirthdayYear').blur(function () {
        $("#hintBirthdayYear").hide();
    });
    $('.ShowInfoDepositor').focus(function () {
        $("#hintDepositor").fadeIn("slow");
    });
    $('.ShowInfoDepositor').blur(function () {
        $("#hintDepositor").hide();
    });
    $('.ShowInfoBank').focus(function () {
        $("#hintBank").fadeIn("slow");
    });
    $('.ShowInfoBank').blur(function () {
        $("#hintBank").hide();
    });
    $('.ShowInfoAccountNumber').focus(function () {
        $("#hintAccountNumber").fadeIn("slow");
    });
    $('.ShowInfoAccountNumber').blur(function () {
        $("#hintAccountNumber").hide();
    });
    $('.ShowInfoBankCodeNumber').focus(function () {
        $("#hintBankCodeNumber").fadeIn("slow");
    });
    $('.ShowInfoBankCodeNumber').blur(function () {
        $("#hintBankCodeNumber").hide();
    });
    $('.ShowInfoCustomerNumber').focus(function () {
        $("#hintCustomerNumber").fadeIn("slow");
    });
    $('.ShowInfoCustomerNumber').blur(function () {
        $("#hintCustomerNumber").hide();
    });
});

// Anrede validieren
function ValidateSalutation(source, args) {
    source.isvalid = args.IsValid = SalutationValidation(args.Value)
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function SalutationValidation(v) {
    return $.sync(val + 'ValidateSalutation', '{salutationId: "' + encodeURI(v) + '"}');
}

// Vornamen validieren
function ValidateName(source, args) {
    source.isvalid = args.IsValid = NameValidation(args.Value);
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function NameValidation(v) {
    if (v == undefined) return false;
	 v = $.trim(v);
    return $.sync(val + 'ValidateName', '{name: "' + encodeURI(v) + '"}');
}

// Bankleitzahl validieren
function ValidateBankCodeNumber(source, args) {
    source.isvalid = args.IsValid = BankCodeNumberValidation(args.Value);
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function BankCodeNumberValidation(v) {
    return $.sync(val + 'ValidateBankCodeNumber', '{bcn: "' + encodeURI($.trim(v)) + '"}');
}

// Kontonummer validieren
function ValidateAccountNumber(source, args) {
    source.isvalid = args.IsValid = AccountNumberValidation(args.Value);
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function AccountNumberValidation(v) {
    return $.sync(val + 'ValidateAccountNumber', '{an: "' + encodeURI($.trim(v)) + '"}');
}

// Bank validieren
function ValidateBank(source, args) {
    source.isvalid = args.IsValid = BankValidation(args.Value);
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function BankValidation(v) {
    if (v == undefined) return false;
    return $.sync(val + 'ValidateBank', '{name: "' + encodeURI(v) + '"}');
}

// Kontoinhaber validieren
function ValidateDepositor(source, args) {
    source.isvalid = args.IsValid = DepositorValidation(args.Value);
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function DepositorValidation(v) {
    if (v == undefined) return false;
    return $.sync(val + 'ValidateDepositor', '{name: "' + encodeURI(v) + '"}');
}

// Email validieren
function ValidateEmail(source, args) {
    source.isvalid = args.IsValid = EmailValidation(args.Value);
    // Da die Validität auch von der Funktion MailIsNotInDataBase geprüft wird muss das Symbol an dieser Stelle nicht ausgegeben werden
    // SetValidationSymbol(source.controltovalidate, source.isvalid);
}

// Email validieren
function ValidateEmailWithValidationSymbol(source, args) {
    source.isvalid = args.IsValid = EmailValidation(args.Value);
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function EmailValidation(v) {
    return $.sync(val + 'ValidateEmail', '{mail: "' + encodeURI(v) + '"}');
}

// Ruft per AJAX eine Servermethode auf, welche überprüft, ob die Email-Adresse in der Datenbank existiert
function MailIsInDataBase(source, args) {
    var o = $.sync(val + 'MailExists', '{mailAddress: "' + encodeURI(args.Value) + '"}');
    source.isvalid = args.IsValid = o != null && o == false;
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

// Ruft per AJAX eine Servermethode auf, welche überprüft, ob die Email-Adresse noch nicht in der Datenbank existiert
function MailIsNotInDataBase(source, args) {
    var o = $.sync(val + 'MailExists', '{mailAddress: "' + encodeURI(args.Value) + '"}');
    source.isvalid = args.IsValid = o != null && o == false;
    if (!source.isvalid) {
        if (EmailValidation(args.Value)) {
            $("#hintMailWrong").hide();
            $("#hintMailExists").show();
        }
        else {
            $("#hintMailWrong").show();
            $("#hintMailExists").hide();
        }
        $("#hintMailCurrent").hide();
        $("#hintMail").addClass("hintError");
    }
    else {
        $("#hintMailWrong").hide();
        $("#hintMailExists").hide();
        $("#hintMailCurrent").show();
        $("#hintMail").removeClass("hintError");
    }
    if ($("input.RepeatedMail") != null && $("input.RepeatedMail").val() != '') {
        CompareMailAgain($("input.RepeatedMail"), args.Value);
    }
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

// Ruft per AJAX eine Servermethode auf, welche überprüft, ob die Email-Adresse noch nicht in der Datenbank existiert
function MailIsNotInDataBaseForUser(source, args) {
    source.isvalid = args.IsValid = $.sync(val + 'MailIsNotInDataBaseForUser', '{mail: "' + encodeURI(args.Value) + '"}');
    if (!source.isvalid) {
        if (EmailValidation(args.Value)) {
            $("#hintMailWrong").hide();
            $("#hintMailExists").show();
        }
        else {
            $("#hintMailWrong").show();
            $("#hintMailExists").hide();
        }
        $("#hintMailCurrent").hide();
        $("#hintMail").addClass("hintError");
    }
    else {
        $("#hintMailWrong").hide();
        $("#hintMailExists").hide();
        $("#hintMailCurrent").show();
        $("#hintMail").removeClass("hintError");
    }
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

// Ruft per AJAX eine Servermethode auf, welche überprüft, ob die Email-Adresse noch nicht in der Datenbank existiert
function CompareMail(source, args) {
    var o = $("input.EmailToCompare");
    source.isvalid = args.IsValid = o.length > 0 && args.Value != "" && o[0].value == args.Value;
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function CompareMailAgain(repeatedMailElement, mail) {
    var bool = $(repeatedMailElement).val() == mail;
    if ($(".CvRepeatMail") != null && $(".CvRepeatMail").length != 0) {
        $(".CvRepeatMail")[0].isvalid = bool;
    }
    SetValidationSymbol($(repeatedMailElement)[0].id, bool);
}

// Vergleicht die beiden eingegeben Mails miteinander und berücksichtigung ob die eingegebene Email bereits dem Kunden gehört
function CompareMailIfChanged(source, args) {
    var o = $("input.EmailToCompare");
    var b = $.sync(val + 'MailIsOwnedByCustomer', '{mail: "' + encodeURI(o[0].value) + '"}');
    if (b) {
        source.isvalid = args.IsValid = true;
        SetValidationSymbol(source.controltovalidate, source.isvalid);
    }
    else {
        CompareMail(source, args);
    }
}

// Passwort validieren
function ValidatePasswordMatch(source, args) {
    source.isvalid = args.IsValid = $.sync(val + 'ValidatePassword', '{password: "' + encodeURI(args.Value) + '"}');
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function ComparePassword(source, args) {
    var o = $("input.PasswordToValidate");
    source.isvalid = args.IsValid = o.length > 0 && args.Value != "" && o[0].value == args.Value;
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function ComparePasswordIfChanged(source, args) {
    var o = $("input.PasswordToValidate");
    source.isvalid = args.IsValid = o[0].value == args.Value;
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

// Postleitzahl validieren
function ValidateZip(source, args) {
    var v = ZipValidation(args.Value);
    var b = !IsZipBlacklisted(args.Value);
    if (!b) {
        $("#hintZip").addClass("hintError");
        $("#hintZip").show();
        $("#hintZipInfo").hide();
        $("#hintZipBlacklisted").show();
    }
    else {
        $("#hintZip").removeClass("hintError");
        $("#hintZip").show();
        $("#hintZipBlacklisted").hide();
    }
    source.isvalid = args.IsValid = v && b;
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function ZipValidation(v) {
    return $.sync(val + 'ValidateZip', '{zip: "' + encodeURI(v) + '"}');
}

function IsZipBlacklisted(v) {
    return $.sync(val + 'IsZipBlacklisted', '{zip: "' + encodeURI(v) + '"}');
}

// Ort validieren
function ValidateCity(source, args) {
    source.isvalid = args.IsValid = CityValidation(args.Value);
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function CityValidation(v) {
    if (v == undefined) return false;
    return $.sync(val + 'ValidateCity', '{city: "' + encodeURI(v) + '"}');
}

// Steuernummer validieren
function ValidateTaxnumberBusiness(source, args) {
    var o = $(".CustomerType");
    if (o == null) {
        source.isvalid = args.IsValid = true;
    }
    else {
        source.isvalid = args.IsValid = $.sync(val + 'ValidateTaxnumberBusiness', '{taxnumber: "' + encodeURI(args.Value) + '", customertypId: "' + encodeURI($(".CustomerType")[0].value) + '"}');
        SetValidationSymbol(source.controltovalidate, source.isvalid);
    }
}

// Steuernummer validieren
function ValidateTaxnumberPrivate(source, args) {
    var o = $(".CustomerType");
    if (o == null) {
        source.isvalid = args.IsValid = true;
    }
    else {
        source.isvalid = args.IsValid = $.sync(val + 'ValidateTaxnumberPrivate', '{taxnumber: "' + encodeURI(args.Value) + '", customertypId: "' + encodeURI($(".CustomerType")[0].value) + '"}');
        SetValidationSymbol(source.controltovalidate, source.isvalid);
    }
}

// Straße validieren
function ValidateStreet(source, args) {
    var v = StreetValidation(args.Value);
    var b = StreetBlacklistValidation(args.Value);
    if (!b) {
        $("#hintStreet").addClass("hintError");
        $("#hintStreet").show();
        $("#hintStreetInfo").hide();
        $("#hintStreetBlacklisted").show();
    }
    else {
        $("#hintStreet").removeClass("hintError");
        $("#hintStreetInfo").show();
        $("#hintStreetBlacklisted").hide();
    }
    source.isvalid = args.IsValid = v && b;
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function StreetValidation(v) {
    if (v == undefined) return false;
    return $.sync(val + 'ValidateStreet', '{street: "' + encodeURI(v) + '"}');
}

function StreetBlacklistValidation(v) {
    return $.sync(val + 'StreetIsNotBlacklisted', '{street: "' + encodeURI(v) + '"}');
}

function IsValidCustomerTypeId(source, args) {
    source.isvalid = args.IsValid = $.sync(val + 'IsValidCustomerTypeId', '{customerTypId: "' + encodeURI(args.Value) + '"}');
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

// Telefonnummer validieren
function ValidatePhoneCountry(source, args) {
    source.isvalid = args.IsValid = PhoneCountryValidation(args.Value);
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function PhoneCountryValidation(v) {
    return $.sync(val + 'ValidatePhoneCountry', '{number: "' + encodeURI(v) + '"}');
}

function ValidatePhoneCity(source, args) {
    source.isvalid = args.IsValid = PhoneCityValidation(args.Value);
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function PhoneCityValidation(v) {
    return $.sync(val + 'ValidatePhoneCity', '{number: "' + encodeURI(v) + '"}');
}

function ValidatePhoneNumber(source, args) {
    $("#" + source.controltovalidate).val($("#" + source.controltovalidate).val().replace(/\s/g, ""));
    source.isvalid = args.IsValid = PhoneNumberValidation($("#" + source.controltovalidate).val());
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function PhoneNumberValidation(v) {
    return $.sync(val + 'ValidatePhoneNumber', '{number: "' + encodeURI(v) + '"}');
}

function ValidatePhone(source, args) {
    var phoneCountry = "";
    if ($('.phoneCountry') != null) {
        phoneCountry = $('.phoneCountry').val();
    }
    var phoneCity = "";
    if ($('.phoneCity') != null) {
        phoneCity = $('.phoneCity').val();
    }
    var phoneNumber = "";
    if ($('.phoneNumber') != null) {
        phoneNumber = $('.phoneNumber').val();
    }
    var cv1 = $(".CvPhoneCountry");
    var cv2 = $(".CvPhoneCity");
    var cv3 = $(".CvPhoneNumber");
    if (cv1.length == 0 || cv2.length == 0 || cv3.length == 0) {
        source.isvalid = args.IsValid = false;
    }
    else {
        source.isvalid = cv1[0].isvalid = cv2[0].isvalid = cv3[0].isvalid = args.IsValid = $.sync(val + 'ValidatePhoneCountry', '{number: "' + encodeURI(phoneCountry) + '"}') && $.sync(val + 'ValidatePhoneCity', '{number: "' + encodeURI(phoneCity) + '"}') && $.sync(val + 'ValidatePhoneNumber', '{number: "' + encodeURI(phoneNumber) + '"}');
    }
    SetValidationSymbol(GetIdOfLastChild(source.controltovalidate), source.isvalid);
}


function ValidateUsername(source, args) {
    source.isvalid = args.IsValid = UsernameValidation(args.Value);
    // Da die Validität auch von der Funktion UserIdIsNotInDataBase geprüft wird muss das Symbol an dieser Stelle nicht ausgegeben werden
    // SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function UsernameValidation(v) {
    return $.sync(val + 'ValidateUsername', '{username: "' + encodeURI(v) + '"}');
}

function ValidateOtherPhoneCountry(source, args) {
    if (args.Value == '') {
        source.isvalid = args.IsValid = true;
    }
    else {
        ValidatePhoneCountry(source, args);
    }
}

function ValidateOtherPhoneCity(source, args) {
    if (args.Value == '') {
        source.isvalid = args.IsValid = true;
    }
    else {
        ValidatePhoneCity(source, args);
    }
}

function ValidateOtherPhoneNumber(source, args) {
    if (args.Value == '') {
        source.isvalid = args.IsValid = true;
    }
    else {
        ValidatePhoneNumber(source, args);
    }
}

function ValidateCompanyName(source, args) {
    var o = $(".CustomerType");
    if (o == null) {
        source.isvalid = args.IsValid = false;
    }
    source.isvalid = args.IsValid = $.sync(val + 'ValidateCompanyName', '{name: "' + encodeURI(args.Value) + '", customertypId: "' + encodeURI($(".CustomerType")[0].value) + '"}');
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function ValidateBranchOfBusiness(source, args) {
    var o = $(".CustomerType");
    if (o == null) {
        source.isvalid = args.IsValid = false;
    }
    source.isvalid = args.IsValid = $.sync(val + 'ValidateBranchOfBusiness', '{value: "' + encodeURI(args.Value) + '", customertypId: "' + encodeURI($(".CustomerType")[0].value) + '"}');
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function ValidateVatNumber(source, args) {
    var o = $(".CustomerType");
    if (o == null) {
        source.isvalid = args.IsValid = false;
    }
    else {
        source.isvalid = args.IsValid = $.sync(val + 'ValidateVatNumber', '{number: "' + encodeURI(args.Value) + '", customertypId: "' + encodeURI($(".CustomerType")[0].value) + '"}');
        SetValidationSymbol(source.controltovalidate, source.isvalid);
    }
}

function ValidateVatNumberEs(source, args) {
    if (args.Value == null || args.Value.length == 0) {
        source.isvalid = args.IsValid = true;
    }
    else {
        source.isvalid = args.IsValid = $.sync(val + 'ValidateVatNumber', '{number: "' + encodeURI(args.Value) + '", customertypId: "' + encodeURI($(".CustomerType")[0].value) + '"}');
    }
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

// Ruft per AJAX eine Servermethode auf, welche überprüft, ob die UserId noch nicht in der Datenbank existiert
function UserIdIsNotInDataBase(source, args) {
    var o = $.sync(val + 'UserIdExists', '{userId: "' + encodeURI(args.Value) + '"}');
    source.isvalid = args.IsValid = o != null && o == false;
    if (!source.isvalid) {
        if (UsernameValidation(args.Value)) {
            $("#hintUsernameExists").show();
            $("#hintUsernameWrong").hide();
        }
        else {
            $("#hintUsernameExists").hide();
            $("#hintUsernameWrong").show();
        }
        $("#hintUsernameCurrent").hide();
        $("#hintUsername").addClass("hintError");
    }
    else {
        $("#hintUsernameExists").hide();
        $("#hintUsernameWrong").hide();
        $("#hintUsernameCurrent").show();
        $("#hintUsername").removeClass("hintError");
    }
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

// Ruft per AJAX eine Servermethode auf, welche überprüft, ob die UserId noch nicht in der Datenbank existiert
function UserIdIsNotInDataBaseForUser(source, args) {
    source.isvalid = args.IsValid = $.sync(val + 'UserIdIsNotInDataBaseForUser', '{userid: "' + encodeURI(args.Value) + '"}');
    if (!source.isvalid) {
        if (UsernameValidation(args.Value)) {
            $("#hintUsernameExists").show();
            $("#hintUsernameWrong").hide();
        }
        else {
            $("#hintUsernameExists").hide();
            $("#hintUsernameWrong").show();
        }
        $("#hintUsernameCurrent").hide();
        $("#hintUsername").addClass("hintError");
    }
    else {
        $("#hintUsernameExists").hide();
        $("#hintUsernameWrong").hide();
        $("#hintUsernameCurrent").show();
        $("#hintUsername").removeClass("hintError");
    }
    SetValidationSymbol(source.controltovalidate, source.isvalid);
}

function GetIdOfLastChild(id) {
    var element = $('#' + id).parent();
    if (element.length == 0) {
        return id;
    }
    var node = $(element).children("input.last");
    if (node == null || node == undefined || node.length == 0) {
        node = $(element).children("select.last");
        if (node == null || node == undefined || node.length == 0) return id;
    }
    return node[0].id;
}

function ValidatePassword(password) {
    // var value = $.sync(val + 'ValidatePassword', '{password: "' + password + '"}');
    var value = ValidationGrade(password);
    $('#pwvi').removeClass();
    $('#pwvi').addClass("pwLevel" + value);
}

function ValidationGrade(password) {
    var v = 1;
    if (password.length == 0 || password.match(/[\s]/)) {
        return v - 1;
    }
    if (password.length < 4) {
        return v;
    }
    if (password.length < 6) {
        return v + 1;
    }
    if (password.match(/[a-z]/)) {
        v++;
    }
    if (password.match(/[A-Z]/)) {
        v++;
    }
    if (password.match(/[0-9]/)) {
        v++;
    }
    if (password.match(/[\!|\"|\#|\$|\%|\&|\'|\(|\)|\*|\+|\,|\-|\.|\/|\:|\;|\<|\=|\>|\?|\@|\[|\\|\]|\^|_|\`|\{|\||\}|\~]/)) {
        v++;
    }
    return v;
}

function SetValidationSymbol(id, valid) {
    var o = $("#" + id);
    var symbol = $("#" + id + "~ span.spacer");
    if (symbol != null) {
        $(symbol).remove();
    }
    var spanId = ResetValidationSymbol(id);
    if (valid) {
        $(o).after('<span id="' + spanId + '" class="valid">&nbsp;</span>');
    }
    else {
        $(o).after('<span id="' + spanId + '" class="invalid">&nbsp;</span>');

    }
    // Tooltip ausblenden wenn vorhanden (wird nur bei Warnungen eingeblendet -> SetWarning)
    $("#" + id.replace(/\-/g, "") + "ToolTip").hide();
}

function SetWarning(id, ttText) {
    var o = $("#" + id);
    var symbol = $("#" + id + "~ span.spacer");
    if (symbol != null) {
        $(symbol).remove();
    }
    var spanId = ResetValidationSymbol(id);
    $(o).after('<span id="' + spanId + '" class="warningError">&nbsp;</span>');
    var g = id.replace(/\-/g, "");
    var tt = $("#" + g + "ToolTip");
    if ($(tt).length == 0) return;
    $(tt).html(ttText);
    $(tt).show();
    var t = -tt[0].offsetHeight - 10;
    $(tt).css("margin-top", t + "px");
}

function ResetValidationSymbol(id) {
    var spanId = id + "_validationsymbol";
    if ($("#" + spanId) != null) {
        $("#" + spanId).remove();
    }
    return spanId;
}

function UserIdOrMailIsInDataBase(source, args) {
    var userIdOrMail = args.Value;
    var f = userIdOrMail.search("\"");
    if (f != -1) {
        source.isvalid = args.IsValid = false;
        return;
    }
    if (userIdOrMail.length > 0) {
        source.isvalid = args.IsValid = $.sync(val + 'UserIdExists', '{userId: "' + encodeURI(userIdOrMail) + '"}');
        //GenerateSuggestions();

        if (source.isvalid) return;
        source.isvalid = args.IsValid = $.sync(val + 'MailExists', '{mailAddress: "' + encodeURI(userIdOrMail) + '"}');
        return;
    }
    else {
        source.isvalid = false;
    }
}

function IsCurrentUserId(source, args) {
    var userid = args.Value;
    if (userid.length > 0) {
        source.isvalid = args.IsValid = $.sync(val + 'IsCurrentUserId', '{userid: "' + encodeURI(userid) + '"}');
    }
    else {
        source.isvalid = args.IsValid = false;
    }
}

function ValidateBirthDay(source, args) {
    var day = '';
    if ($("select.DdlBday") != null) {
        day = $("select.DdlBday").val();
    }
    var month = '';
    if ($("select.DdlBmonth") != null) {
        month = $("select.DdlBmonth").val();
    }
    var year = '';
    if ($("select.DdlByear") != null) {
        year = $("select.DdlByear").val();
    }
    var cv1 = $(".CvDdlDay");
    var cv2 = $(".CvDdlMonth");
    var cv3 = $(".CvDdlYear");
    if (cv1.length == 0 || cv2.length == 0 || cv3.length == 0) {
        source.isvalid = args.IsValid = false;
    }
    else {
        source.isvalid = cv1[0].isvalid = cv2[0].isvalid = cv3[0].isvalid = args.IsValid = $.sync(val + 'ValidateBirthDay', '{day: "' + encodeURI(day) + '", month: "' + encodeURI(month) + '", year: "' + encodeURI(year) + '"}');
    }
    SetValidationSymbol(GetIdOfLastChild(source.controltovalidate), source.isvalid);
}

/* prüft die Eingaben, wenn ein Fehlerbericht abgesendet wird */
function ValidateFeedbackMailE(source, arguments) {
    var mail = $("#" + source.controltovalidate);
    if ($('.FeedbackErrorYes > input[type=checkbox]').is(':checked')) {
        if ($.sync(val + 'IsValidMail', '{mail: "' + encodeURI($(mail).val()) + '"}')) {
            arguments.IsValid = true;
        }
        else {
            arguments.IsValid = false;
        }
    }
    else {
        arguments.IsValid = true;
    }
}


function ValidateOtherBranch(source, args) {
    source.isvalid = args.IsValid = true;
}


