﻿
function valPasswordIsValid(el) {
    if (!/(?!^[0-9]*$)(?!^[a-zA-Z@!,.]*$)^([a-zA-Z0-9@!,.]{6,100})$/.test(el.value)) {
        el.errors.push("The password is not valid.");
        return false;
    } else {
        return true;
    }
}

function valUrlCharacters(el) {
    if (!/^[a-zA-Z0-9_-]*$/.test(el.value)) {
        el.errors.push("Only letters, numbers, underscores and hypens are allowed.");
        return false;
    } else {
        return true;
    }
}

function valCaptcha(el) {
    var number = parseInt(el.value, 10);

    if (number == NaN || number < 25832 || (number - 25832) % 3559 != 0) {
        el.errors.push("Please enter the number exactly as given.");
        return false;
    }
    return true;
}

//////////////////////////////////////////////////////////////////////
/////                                                            /////
/////                      Date Validation                       /////
/////                                                            /////
//////////////////////////////////////////////////////////////////////

var dtCh = "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}

function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31;
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30; }
        if (i == 2) { this[i] = 29; }
    }
    return this;
}

function valIsDate(el) {
    var date = getDate(el);
    return date != -1;
}

function valDateNotInFuture(el) {
    var date = getDate(el);
    if (date == -1) return false;
    if (date == null) return true;

    if (date > new Date()) {
        el.errors.push("The date cannot be in the future.");
        return false;
    }

    return true;
}

function valDateNotInPast(el) {
    var date = getDate(el);
    if (date == -1) return false;
    if (date == null) return true;

    if (date < new Date()) {
        el.errors.push("The date cannot be in the past.");
        return false;
    }

    return true;
}

function getDate(el) {
    var strDate = el.value;

    if (strDate.toLowerCase() == "dd/mm/yyyy") {
        return null;
    }

    var daysInMonth = DaysArray(12);
    var pos1 = strDate.indexOf(dtCh);
    var pos2 = strDate.indexOf(dtCh, pos1 + 1);

    if (pos1 == -1 || pos2 == -1) {
        el.errors.push("The date should be of the form : DD/MM/YYYY.");
        return -1;
    }

    var strDay = strDate.substring(0, pos1);
    var strMonth = strDate.substring(pos1 + 1, pos2);
    var strYear = strDate.substring(pos2 + 1);

    var month = parseInt(strMonth, 10);
    var day = parseInt(strDay, 10);
    var year = parseInt(strYear, 10);

    if (year < 1000) {
        year += 2000;
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        el.errors.push("Please enter a valid month.");
        return -1;
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        el.errors.push("Please enter a valid day.");
        return -1;
    }
    if ((strYear.length != 4 && strYear.length != 2) || year == 0 || year < minYear || year > maxYear) {
        el.errors.push("Please enter a valid year between " + minYear + " and " + maxYear + ".");
        return -1;
    }
    if (strDate.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(strDate, dtCh)) == false) {
        el.errors.push("Please enter a valid date.");
        return -1;
    }
    return new Date(year, month, day);
}
