﻿function ValidateDate(date, format) {
    if (date.length != format.length)
        return false;
    return Date.parseLocale(date, format) != null;
}

function ValidateTime(time, format) {
    if (time.length != format.length)
        return false;
    return Date.parseLocale(time, format) != null;
}

function IsDateInFuture(dateString, format) {
    var date = Date.parseLocale(dateString, format);
    return date > new Date();
}

function CompareDateGreaterThan(isThisDate, greaterThanThisDate, format) {
    var date1 = Date.parseLocale(isThisDate, format);
    var date2 = Date.parseLocale(greaterThanThisDate, format);
    return date1 > date2;
}

function GetDaysInMonth(month, year) {
    return 32 - new Date(year, month - 1, 32).getDate();
}

function prevMonth() {
    var thisMonth = this.getMonth();
    this.setMonth(thisMonth - 1);
    if (this.getMonth() != thisMonth - 1 && (this.getMonth() != 11 || (thisMonth == 11 && this.getDate() == 1)))
    this.setDate(0);
}
function nextMonth() {
    var thisMonth = this.getMonth();
    this.setMonth(thisMonth + 1);
    if (this.getMonth() != thisMonth + 1 && this.getMonth() != 0)
    this.setDate(0);
}
Date.prototype.nextMonth = nextMonth;
Date.prototype.prevMonth = prevMonth;


function ValidateNumber(number) {
    return ValidateIsJustNumber(number);
}

function ValidateNumberWithRange(number, min, max) {
    if (!ValidateIsJustNumber(number))
        return false;

    var num = parseFloat(number);

    if (min != null && num < min)
        return false;

    if (max != null && num > max)
        return false;

    return true;
}

function ValidateIsJustNumber(number) {
    for (i = 0; i < number.length; i++) {
        if ("0123456789".indexOf(number.charAt(i)) == -1)
            return false;
    }
    return true;
}

function IsNullOrEmpty(s) {
    if (s && typeof (s).toString() == "string" && s.length > 0)
        return false;
    return true;
}

function ValidateEmailAddress(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(email))
        return true;
    return false;
}

function ParseTime(s) {

    var data = "";
    //replace commas and full stops with colon
    s = s.replace(".", ":").replace(",", ":");

    //remove any non-numeric or colon characters
    var foundColon = false;
    for (i = 0; i < s.length; i++) {
        var code = s.charCodeAt(i);
        if (code >= 48 && code <= 57)
            data += s.charAt(i);
        else if(code == 58 && !foundColon)
        {
            foundColon = true;
            data += s.charAt(i);
        }
    }

    if (data.length > 2 && data.charCodeAt(2) != 58) {
        data = data.replace(":", "");
        data = data.substr(0, 2) + ":" + data.substr(2);
    }

    if (data.length > 0 && data.charCodeAt(0) == 58)
        data = "0" + data;
    if (data.length > 1 && data.charCodeAt(1) == 58)
        data = "0" + data;

    if (data.length > 2 && !foundColon)
        data = data.substr(0, 2) + ":" + data.substr(3);

    if (data.length > 5)
        data = data.substr(0, 5);

    return data;
}

function GetCaretPosition(ctrl) {
    var CaretPos = 0; // IE Support
    if (document.selection) {
        ctrl.focus();
        var Sel = document.selection.createRange();
        Sel.moveStart('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    }
    // Firefox support
    else if (ctrl.selectionStart || ctrl.selectionStart == '0')
        CaretPos = ctrl.selectionStart;
    return (CaretPos);
}
function SetCaretPosition(ctrl, pos) {
    if (ctrl.setSelectionRange) {
        ctrl.focus();
        ctrl.setSelectionRange(pos, pos);
    }
    else if (ctrl.createTextRange) {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}

function AddTrailing(value, character, amount) {
    for (i = 0; i < amount; i++)
        value += character;
    return value;
}

function InsertStringIntoString(original, insert, index) {
    return original.substr(0, index) + insert + original.substr(index);
}

function ValidateColour(colour) {
    if (colour.length != 6)
        return false;

    var reg = /^[0-9A-Fa-f]*$/;
    if (reg.test(colour))
        return true;

    return false;
}

function GetProgressColourClass(percent) {
    if (percent < 25)
        return "FormProgressBarFillColourLT25";
    if (percent < 50)
        return "FormProgressBarFillColourLT50";
    if (percent < 75)
        return "FormProgressBarFillColourLT75";

    return "FormProgressBarFillColourLT100";
}

function ParseCustomHref(href) {
    var index = href.lastIndexOf("/");
    if (index >= 0)
        return href.substring(index + 1);
    return href;
}

function ValidateOnlyAlphanumeric(s) {
    var reg = /^[A-Za-z0-9_-]+$/;
    if (reg.test(s))
        return true;
    return false;
}
