﻿/// <reference path="jquery-1.3.2.js"/>

//$(document).ready(function() {
//    //validateForm();
//    //Diese Funktion markiert ein nicht valides Feld mit einem roten Rand
//    $(".inputValidation").blur(function(e) {
//        //Damit diese Javascript funktioniert ist es wichtig, dass der RequiredFieldValidator mit folgender Id bennant wird:
//        //[ID der zu validierendenen Textbox oder DropDownList] + "Rfv" (-> Steht für RequiredFieldValidator
//        //Für RegularExpressionValidator verhält sich das genauso, nur das statt einem "Rfv" ein "Rev" angehängt wird
//        //Der zu validierende Textbox oder DropDownList muss die CSS-Klasse inputValidation gegeben werden. Außerdem muss um die Textbox
//        //bzw. DropDownList ein DIV oder SPAN gelegt werden.
//        validate(this);
//    });

//    //    function FullInputValidation() {
//    //        var rfv = $(".RFV");
//    //        var valid = true;
//    //        for (var i = 0; i <= rfv.length; i++) {
//    //            if (!rfv[i].isValid) {
//    //                valid = false;
//    //            }
//    //        }
//    //        if (valid) {
//    //            $("#FullInputValidationDIV").show;
//    //        }
//    //        else {
//    //            $("#FullInputValidationDIV").hide;
//    //        }
//    //    }
//});

//function validate(element) {        //ID des RFVs
//    var rfvId = "#" + element.id + "Rfv";
//    //ID des REVs
//    var revId = "#" + element.id + "Rev";
//    var selector = "#" + element.id;
//    //valid = true => bleibt so sollte zu diesem Element keine Validatoren gehören
//    var valid = true;
//    //RFV-Element anhand der ID selektieren
//    var rfvElement = $(rfvId)[0];
//    if (rfvElement != null) {
//        valid = rfvElement.isvalid;
//    }
//    //REV-Element anhand der ID selektieren
//    var revElement = $(revId)[0];
//    if (revElement != null) {
//        valid = valid && revElement.isvalid;
//    }
////    //Höhe und breide auslesen

//    //Elternelement selektieren (sollte ein span oder div sein)
//    var parentElement = $(selector).parent();
//    if (!valid) {
//        //nicht valide

//        //Elternelement validationError-Klasse hinzufügen
//        if (!parentElement.hasClass("validationError"))
//        {
//         //   parent.addClass("validationError");
//            parentElement.addClass("validationError");
//        }
//    }
//    else
//    {
//        //valide
//        
//        //Elternelement validationError-Klasse entfernen
//        if (parentElement.hasClass("validationError"))
//        {
//            parentElement.removeClass("validationError"); 
//        }
//    }
//}

////Wird nach einem Button Click aufgerufen
//function validateForm() {
//    var txb = $(".inputValidation");
//    for (var i = 0; i <= txb.length; i++) {
//        validate(txb[i]);
//    }
//}

// Ruft per AJAX eine Servermethode auf, welche überprüft, ob die Email-Adresse in der Datenbank existiert
function MailIsInDataBase(source, args) {
    var mailAddress = args.Value;
    var f = mailAddress.search("\"");
    if (f != -1) {
        source.isvalid = args.IsValid = false;
        return;
    }
    if (mailAddress.length > 0) {
        var send = '{mailAddress: "' + mailAddress + '"}';

        source.isvalid = args.IsValid = CoreRequest('MailExists', send, false);

        return;

        //        $.ajax({async: false, type: "POST", url: "/CMSPages/WebService.asmx/MailExists", data: send, contentType: "application/json; charset=utf-8", dataType: "json",

        //            success: function(data)
        //            {
        //                source.isvalid = data.d;
        //                args.IsValid = data.d;
        //            },
        //            error: function(x)
        //            {
        //                var blubb;
        //                alert("MailIsInDataBase Fehler:" + x.responseText);
        //            }
        //        });
    }
    else {
        source.isvalid = false;
    }
}

// Ruft per AJAX eine Servermethode auf, welche überprüft, ob die Email-Adresse noch nicht in der Datenbank existiert
function MailIsNotInDataBase(source, args) {
    var mailAddress = args.Value;
    var f = mailAddress.search("\"");
    if (f != -1) {
        source.isvalid = args.IsValid = false;
        return;
    }
    if (mailAddress.length > 0) {
        var send = '{mailAddress: "' + mailAddress + '"}';

        source.isvalid = args.IsValid = !CoreRequest('MailExists', send, false);

        return;
    }
    else {
        source.isvalid = false;
    }
}

// Ruft per AJAX eine Servermethode auf, welche überprüft, ob die UserId noch nicht in der Datenbank existiert
function UserIdIsNotInDataBase(source, args) {
    var userId = args.Value;
    var f = userId.search("\"");
    if (f != -1) {
        source.isvalid = args.IsValid = false;
        return;
    }
    if (userId.length > 0) {
        var send = '{userId: "' + userId + '"}';

        source.isvalid = args.IsValid = !CoreRequest('UserIdExists', send, false);

        return;
    }
    else {
        source.isvalid = false;
    }
}

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) {
        var send = '{userId: "' + userIdOrMail + '"}';
        source.isvalid = args.IsValid = CoreRequest('UserIdExists', send, false);
        //GenerateSuggestions();

        if (source.isvalid) return;
        var send2 = '{mailAddress: "' + userIdOrMail + '"}';
        source.isvalid = args.IsValid = CoreRequest('MailExists', send2, false);
        return;
    }
    else {
        source.isvalid = false;
    }
}

var _PassStore = null;
var _Mail = null;
var _MailStore = null;
var _Pass = null;

function setPass(arguments) {
    _PassStore = arguments.Value;
}

function setMail(arguments) {
    _Mail = arguments.Value;
}

function Company(source, arguments) {
    arguments.IsValid = false;
    arguments.IsValid = ((arguments.Value.length >= 2));
}

var _passSource = null;
var _pssarguments = null;
function validateUserName(source, arguments) {

    arguments.IsValid = false;
    arguments.IsValid = ((arguments.Value.length >= 3) && (arguments.Value.match(/^[a-zA-Z0-9]+$/)));
    UserIdIsNotInDataBase(source, arguments)
    if (!arguments.IsValid) {
        GenerateSuggestions();
        _passSource = source;
        _pssarguments = arguments;
    }
}

function Selected(e) {
    //    $('.user_name .textfeld').val(e.currentTarget.innerText);
    $('.user_name .textfeld').val(e.currentTarget.innerHtml);
    //    _pssarguments.Value = e.currentTarget.innerText;
    $('#suggestion').hide();

    $("#" + $(_passSource).attr("controltovalidate")).parents("div.StylingContainer").removeClass("Invalid");
    $($(_passSource).parents("div.validators").parents("div")).children("img").attr("style", "display:block");
    $($(_passSource).parents("div.validators")).attr("style", "display:none");
    _pssarguments.IsValid = true;
    _pssarguments.Value = e.currentTarget.innerText;
}

var _sourceEM = null;
var _argumentsEM = null;

function validateEmail(source, arguments) {
    arguments.IsValid = false;

    arguments.IsValid = (arguments.Value.match(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/));

    if (_MailStore !== null && _MailStore !== undefined && _MailStore !== "")
        arguments.IsValid = arguments.Value == _MailStore;

    if (!arguments.IsValid) {
        $('.mail_Repeat .textfeld').val("");
        arguments.IsValid = (arguments.Value.match(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/));
        if (_argumentsEM != null)
            EmailRepeat(_sourceEM, _argumentsEM);
    }
    setMail(arguments);
}

function EmailRepeat(source, arguments) {
    if (arguments == null || arguments == undefined)
        arguments = _argumentsEM;

    arguments.IsValid = false
    var val = arguments.Value;
    var val2 = $('.mail .textfeld').val();

    arguments.IsValid = val == val2;

    if (!arguments.IsValid) {
        $('.mail_Repeat .textfeld').val("");
        $("#" + $(_sourceEM).attr("controltovalidate")).parents("div.StylingContainer").addClass("Invalid");
        $($(_sourceEM).parents("div.validators").parents("div")).children("img").attr("style", "display:none");
    }
    _sourceEM = source;
    _argumentsEM = arguments;
    _MailStore = arguments.Value;
}

var _sourcePW = null;
var _argumentsPW = null;

function validatePassword(source, arguments) {

    arguments.IsValid = false;

    arguments.IsValid = ((arguments.Value.length >= 4) && (arguments.Value.length <= 20))
    if (_Pass !== null && _Pass !== undefined && _Pass !== "")
        arguments.IsValid = (arguments.Value == _Pass);

    if (!arguments.IsValid) {
        $('.pass_Repeat .textfeld').val("");
        arguments.IsValid = ((arguments.Value.length >= 4) && (arguments.Value.length <= 20));
        if (_argumentsPW != null)
            PassRepeat(_sourcePW, _argumentsPW);
    }
    setPass(arguments);
}

function PassRepeat(source, arguments) {

    if (arguments == null || arguments == undefined)
        arguments = _argumentsPW;

    arguments.IsValid = false;

    var val = arguments.Value;
    var val2 = $('.passw .textfeld').val();

    arguments.IsValid = val == val2 && ((arguments.Value.length >= 4) && (arguments.Value.length <= 20));

    if (!arguments.IsValid) {
        $('.pass_Repeat .textfeld').val("");
        $("#" + $(_sourcePW).attr("controltovalidate")).parents("div.StylingContainer").addClass("Invalid");
        $($(_sourcePW).parents("div.validators").parents("div")).children("img").attr("style", "display:none");
    }
    _sourcePW = source;
    _argumentsPW = arguments;
    _Pass = arguments.Value;
}


var IsAktiv = false;
var Test = false;
var IsTrue = new Array(false, false, false);

$(document).ready(function() {

    $('.TxtPhone01').unbind('change');
    $('.TxtPhone01').bind('change', Validierung);

    $('.TxtPhone02').unbind('change');
    $('.TxtPhone02').bind('change', Validierung);

    $('.TxtPhone03').unbind('change');
    $('.TxtPhone03').bind('change', Validierung);
});


function Validierung() {

    if (!check1($('.TxtPhone01'))) {
        $('.TxtPhone01').addClass("red");
        IsTrue[0] = false;
    }
    else {
        $('.TxtPhone01').removeClass("red");
        IsTrue[0] = true;
    }

    if (!check($('.TxtPhone02'))) {
        $('.TxtPhone02').addClass("red");
        IsTrue[1] = false;
    }
    else {
        $('.TxtPhone02').removeClass("red");
        IsTrue[1] = true;
    }

    if (!check($('.TxtPhone03'))) {
        $('.TxtPhone03').addClass("red");
        IsTrue[2] = false;
    }
    else {
        $('.TxtPhone03').removeClass("red");
        IsTrue[2] = true;
    }

    valid = IsTrue[0] && IsTrue[1] && IsTrue[2];

    if (!valid) {
        $('.PhoneFehler').attr('Style', 'display:block');
        $('.ValidPic').attr('Style', 'display:none');
    }
    else {
        $('.PhoneFehler').attr('Style', 'display:none');
        $('.ValidPic').attr('Style', 'display:block');
    }
}

function check(elem) {
    if (elem != null && elem != undefined) {
        var value = $(elem).val();
        if (value != null && value != undefined) {
            if (value.match(/^(\d)([\d|\s]{1,49})/)) {
                return true;
            }
            else {
                return false;
            }
            return false;
        }
        return false;
    }
    return false;
}


function check1(elem) {
    if (elem != null && elem != undefined) {
        var value = $(elem).val();
        if (value != null && value != undefined) {
            if (value.match(/^(\+|00)[0-9]{1,4}/)) {
                return true;
            }
            else {
                return false;
            }
            return false;
        }
        return false;
    }
    return false;
}


function Company(source, arguments) {
    arguments.Value = $('.TxtCompanyDiv .textfeld').val();
    if (arguments.Value != null && arguments.Value != undefined) {

        if (arguments.Value.length >= 2) {
            arguments.IsValid = true;
        }
        else {
            arguments.IsValid = false;
        }
        if (!arguments.IsValid) {
            $("#" + $(source).attr("controltovalidate")).parents("div.StylingContainer").addClass("Invalid");
            $($(source).parents("div.validators").parents("div")).children("img").attr("style", "display:none");
        } else {
            $("#" + $(source).attr("controltovalidate")).parents("div.StylingContainer").removeClass("Invalid");
            $($(source).parents("div.validators").parents("div")).children("img").attr("style", "display:block");
        }
    }
}

function OtherBranch(source, arguments) {
    arguments.Value = $('.TxtDepartment .textfeld').val();
    if (arguments.Value != null && arguments.Value != undefined) {

        if (arguments.Value.length >= 2) {
            arguments.IsValid = true;
        }
        else {
            arguments.IsValid = false;
        }
        if (!arguments.IsValid) {
            $("#" + $(source).attr("controltovalidate")).parents("div.StylingContainer").addClass("Invalid");
            $($(source).parents("div.validators").parents("div")).children("img").attr("style", "display:none");
        } else {
            $("#" + $(source).attr("controltovalidate")).parents("div.StylingContainer").removeClass("Invalid");
            $($(source).parents("div.validators").parents("div")).children("img").attr("style", "display:block");
        }
    }
}

function ValidateChangingDate(sender, args) {
    var day = $("select.DdlDayChangingDate").val();
    var month = $("select.DdlMonthChangingDate").val();
    var year = $("select.DdlYearChangingDate").val();

    var d = new Date();
    var now = new Date(d.getFullYear(), d.getMonth(), d.getDate());
    var selectedDate = new Date(year, month - 1, day);
    var value = selectedDate >= now;

    args.IsValid = value;
}