﻿/// <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);
        if (source.isvalid) return;
        var send2 = '{mailAddress: "' + userIdOrMail + '"}';
        source.isvalid = args.IsValid = CoreRequest('MailExists', send2, false);
        return;
    }
    else {
        source.isvalid = false;
    }
    
    
    
}