﻿// JScript File to check Validations

// Checks whether the value is empty or not
// Returns True if input is empty
function IsEmpty(value) {
    var result = false;
    if (value == "") {
        result = true;
    }
    return result;
}

// Checks whether the value is valid email or not
// Returns True if input is valid email
function IsValidEmailId(value) {
    var result = true;
    var email = value;

    if (!IsEmpty(value)) {
        var expEmail = new RegExp(/^([a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$)/i);
        var chkval = expEmail.exec(email);
        if (chkval == null) {
            result = false;
        }
    }
    else {
        result = false;
    }
    return result;
}

// Checks whether the value is string or not
// Returns True if input is string
function IsString(value) {
    var result = true;

    if (!IsEmpty(value)) {
        var exp = new RegExp("^[a-zA-Z \s]*$");
        var chkNames;
        chkNames = exp.exec(value);
        if (chkNames == null) {
            result = false;
        }
    }
    else {
        result = false;
    }
    return result;
}

// Checks whether date value is within valid range or not
// Returns true if valid input is provided.
function DateRangeCheck(value) {
    var result = true;

    if (!IsEmpty(value)) {
        if (value < 1 || value > 31) {
            result = false;
        }
    }
    else {
        result = false;
    }
    return result;
}


// Function to compare two dates
// Returns true if ToDate is greater or equal to FromDate
function CompareDates(toYear, toMonths, toDate, fromYear, fromMonths, fromDate) {
    var ToDate = new Date(toYear, toMonths - 1, toDate);
    var FromDate = new Date(fromYear, fromMonths - 1, fromDate);
    if (ToDate >= FromDate) {
        return true;
    }
    else {
        return false;
    }
}

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables
function isDateValid(dateStr) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

    // To require a 4 digit year entry, use this line instead:
    // var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?
    if (matchArray == null) {        
        return false;
    }
    day = matchArray[1]; // parse date into variables
    month = matchArray[3];
    year = matchArray[4];
    if (month < 1 || month > 12) { // check month range
        return false;
    }
    if (day < 1 || day > 31) {        
        return false;
    }
    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
       return false
    }
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !isleap)) {
            return false;
        }
    }
    return true;  // date is valid
}

// Method to check if a given date is valid or not
// da -> Day number as string
// mo -> Month number as string
// yr -> Year as string
// Returns true if date is valid
function IsValidDate(da, mo, yr) {
    if (yr.length != 4 || da.length > 2 || mo.length > 2 || yr.length == 0 || da.length == 0 || mo.length == 0) {
        return false;
    }
    if (IsNaN.test(da) || IsNaN.test(mo) || IsNaN.test(yr)) {
        return false;
    }
    var year = eval(yr);
    var month = eval(mo);
    var day = eval(da);
    if (day == 0 || month == 0 || year == 0) {
        return false;
    }
    var daysInMonth = 31;
    if (month == 4 || month == 6 || month == 9 || month == 11) {
        daysInMonth = 30;
    }
    if (month == 2) {
        daysInMonth = (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28)
    }
    if (month > 12 || day > daysInMonth) {
        return false;
    }
    return true;
}

// Method to check if a given date is valid or not.
// ddsmmm -> Date as string in format "dd mmm".
// Returns true if date is valid.
function IsValidDateDDSMMM(ddsmmm) {
    if (ddsmmm.length == 0) {
        return false;
    }
    var month = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
    var dt = ddsmmm.split(" ");
    if (!dt[1]) {
        return false;
    }
    var mm = month.indexOf(dt[1].toUpperCase());
    var dd = eval(dt[0]);
    if (mm < 0) {
        return false;
    }
    var cDate = new Date();
    var yy = cDate.getFullYear();
    if (mm < cDate.getMonth() || (mm == cDate.getMonth() && dd < cDate.getDate())) {
        yy++;
    }
    var tDate = new Date(yy, mm, dd);

    if (tDate.getFullYear() != yy || tDate.getMonth() != mm || tDate.getDate() != dd) {
        return false;
    }
    return true;
}

//Checks for textarea Validation
//Accepts a-zA-Z0-9|%-@#&/$+?' with spaces
function IsValidDescription(value) {
    var result = true;
    if (!IsEmpty(value)) {
        var exp = new RegExp("^[a-zA-Z0-9|%-@#&/$+?\n' /s']*$");
        var chkNames;
        chkNames = exp.exec(value);
        if (chkNames == null) {
            result = false;
        }
    }
    else {
        result = false;
    }
    return result;
}


function Trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function Ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}

function Rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}

function IsValidPhoneNo(value) {
    var result = true;

    if (!IsEmpty(value)) {
        var exp = new RegExp("^[+0-9-\(\)]+$");
        var chk;
        var splitItems = Trim(value).split(',');
        for (var l = 0; l < splitItems.length; l++) {
            chk = exp.test(splitItems[l]);
            if (chk == false) {
                result = false;
            }
        }
    }
    else {
        result = false;
    }
    return result;
}




