﻿
//// This is the initialising function that prepares the page to do client
//// side validation while allowing for ASP.NET forms

function ValidateAndSubmit(evt) {
    // Ascend from the button that triggered this click event 
    //  until we find a container element flagged with 
    //  .validationGroup and store a reference to that element.
    var $group = $(evt.currentTarget).parents('div[class^=vGroup]');

    var isValid = true;

    // Descending from that .validationGroup element, find any input
    //  elements within it, iterate over them, and run validation on 
    //  each of them.
    $group.find(':input').each(function(i, item) {
        if (!$(item).valid())
            isValid = false;
    });

    // If any fields failed validation, prevent the button's click 
    //  event from triggering form submission.
    if (!isValid)
        evt.preventDefault();
}

// Custom Validation Extentions

$.validator.addMethod(
        "regex",
        function (value, element, regexp) {
            var re = new RegExp(regexp);
            if (!(value == '')) {
                return re.test(value);
            }
            else
                return true;
        },
        "Please check your input."
);

$.validator.addMethod(
	"dateUK",
	function(value, element) {
	    var check = false;
	    var re = /^\d{1,2}\/\d{1,2}\/\d{2,4}$/;
	    if (!(value == '')) {
	        if (re.test(value)) {
	            var adata = value.split('/');
	            var gg = parseInt(adata[0], 10);
	            var mm = parseInt(adata[1], 10);
	            var aaaa = parseInt(adata[2], 10);
	            if (aaaa < 100) {
	                aaaa = aaaa + 2000;
	            }
	            var xdata = new Date(aaaa, mm - 1, gg);
	            if ((xdata.getFullYear() == aaaa) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == gg))
	                check = true;
	            else
	                check = false;
	        } else
	            check = false;
	    } else
	        check = true;
	    return check; //this.optional(element) ||
	},
	"Please enter a correct date"
);

	$.validator.addMethod(
    "passwordstrength",
    function(value, element, minscore) {
        var check = false;
        var test;
        test = passwordStrength(value);
        if (test.strength >= minscore) {
            check = true;
        }
        return check;
    },
    "Password not strong enough."
);

$.validator.addMethod(
    "takeawaysingleoption",
    function (value, element) {
        var check = false;
        
        if (value>0) {
            check = true;
        }
        return check;
    },
    "You must Select an Item"
);

// Password stength

var shortPass = 'Too Short'
var badPass = 'Strength: Weak'
var goodPass = 'Strength: Ok'
var strongPass = 'Strength: Excellent'

function passwordStrength(password) {
    score = 0
    
    //password < 4
    if (password.length < 4) { return {"strength": 1, "msg": shortPass} }

    //password length
    score += password.length * 4
    score += (checkRepetition(1, password).length - password.length) * 1
    score += (checkRepetition(2, password).length - password.length) * 1
    score += (checkRepetition(3, password).length - password.length) * 1
    score += (checkRepetition(4, password).length - password.length) * 1

    //password has 3 numbers
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/)) score += 5

    //password has 2 sybols
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5

    //password has Upper and Lower chars
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) score += 10

    //password has number and chars
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) score += 15
    //
    //password has number and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)) score += 15

    //password has char and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)) score += 15

    //password is just a nubers or chars
    if (password.match(/^\w+$/) || password.match(/^\d+$/)) score -= 10

    //verifing 0 < score < 100
    if (score < 0) score = 0
    if (score > 100) score = 100

    if (score < 34) return { "strength": 2, "msg": badPass }
    if (score < 68) return { "strength": 3, "msg": goodPass }
    return { "strength": 4, "msg": strongPass }
}

// checkRepetition(1,'aaaaaaabcbc')   = 'abcbc'
// checkRepetition(2,'aaaaaaabcbc')   = 'aabc'
// checkRepetition(2,'aaaaaaabcdbcd') = 'aabcd'

function checkRepetition(pLen, str) {
    res = ""
    for (i = 0; i < str.length; i++) {
        repeated = true
        for (j = 0; j < pLen && (j + i + pLen) < str.length; j++)
            repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + pLen))
        if (j < pLen) repeated = false
        if (repeated) {
            i += pLen - 1
            repeated = false
        }
        else {
            res += str.charAt(i)
        }
    }
    return res
}

