﻿
var isPassValid = false;
var isMailValid = false;
var mail;
var pass;

function ValidatePass(source, arguments) {

    /*pass = arguments.Value;

    if (arguments.Value.length > 5)
        arguments.IsValid = true;
    else
        arguments.IsValid = false;

    isPassValid = arguments.IsValid;*/

    // allow ONLY alphanumeric keys, no symbols or punctuation
    // this can be altered for any "checkOK" string you desire

    pass = arguments.Value;

    if (pass.length < 6) {
        arguments.IsValid = false;
        isPassValid = arguments.IsValid;
        return;
    }
    
    var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    
    var allValid = true;
    for (i = 0; i < pass.length; i++) {
        ch = pass.charAt(i);
        for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
            break;
        if (j == checkOK.length) {
            allValid = false;
            break;
        }
    }
    
    if(!allValid)
        arguments.IsValid = false;
    else
        arguments.IsValid = true;

    isPassValid = arguments.IsValid;
}

function ValidateEmail(source, arguments) {

    mail = arguments.Value;

    if (echeck(arguments.Value) == true)
        arguments.IsValid = true;
    else
        arguments.IsValid = false;

    isMailValid = arguments.IsValid;
}


function echeck(str) {

    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false
    }

    if (str.indexOf(" ") != -1) {
        return false
    }

    return true
}

function EmailMatch(source, arguments) {

    if (isMailValid) {
        if (arguments.Value == mail)
            arguments.IsValid = true;
        else
            arguments.IsValid = false;
    }
}

function PassMatch(source, arguments) {

    if (isPassValid) {
        if (arguments.Value == pass)
            arguments.IsValid = true;
        else
            arguments.IsValid = false;
    }
}
