﻿function checkDateformat(fld, labelerrormessage,lblalertmessage) {
    var mo, day, yr;
    var entry = fld.value;
    var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
    if (entry != "") {
        var valid = (reLong.test(entry)) || (reShort.test(entry));
        if (valid) {
            var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
            var delim1 = entry.indexOf(delimChar);
            var delim2 = entry.lastIndexOf(delimChar);
            mo = parseInt(entry.substring(0, delim1), 10);
            day = parseInt(entry.substring(delim1 + 1, delim2), 10);
            yr = parseInt(entry.substring(delim2 + 1), 10);
            ////alert(testDate.getFullYear());
            ////alert(testDate.getFullYear());
            //alert(yr);
            // handle two-digit year
            if (yr < 100) {
                var today = new Date();
                // get current century floor (e.g., 2000)
                var currCent = parseInt(today.getFullYear() / 100) * 100;
                // two digits up to this year + 15 expands to current century
                var threshold = (today.getFullYear() + 15) - currCent;
                if (yr > threshold) {
                    yr += currCent - 100;
                } else {
                    yr += currCent;
                }
            }
            var testDate = new Date(yr, mo - 1, day);
            if (testDate.getDate() == day) {
                if (testDate.getMonth() + 1 == mo) {

                    if (testDate.getFullYear() == yr) {
                        labelerrormessage.innerText = "";
                        // fill field with database-friendly format

                        fld.value = mo + "/" + day + "/" + yr;

                    } //if (testDate.getFullYear() == yr)
                    else {
                        fld.value = "";
                        lblalertmessage.innerText = "";
                        labelerrormessage.innerText = "";
                        labelerrormessage.innerText = "There is a problem with the year entry.";
                        ////alert("There is a problem with the year entry.");
                        fld.focus();
                    } //if (testDate.getMonth() + 1 == mo)
                } else {
                fld.value = "";
                lblalertmessage.innerText = "";
                    labelerrormessage.innerText = "";
                    labelerrormessage.innerText = "There is a problem with the month entry.";
                    ////alert("There is a problem with the month entry.");
                    fld.focus();
                }
            } else {
            fld.value = "";
            lblalertmessage.innerText = "";
                labelerrormessage.innerText = "";
                labelerrormessage.innerText = "There is a problem with the date entry.";
                ////alert("There is a problem with the date entry.");
                fld.focus();
            }
        }
        else {
            fld.value = "";
            lblalertmessage.innerText = "";
            labelerrormessage.innerText = "";
            labelerrormessage.innerText = "Please enter a valid date.";
            ////alert("Incorrect date format. Enter as mm/dd/yyyy.");
            fld.focus();
        }
    }
    else {
    }
    return true;
}
function checkReportDateformat(fld, labelerrormessage) {
    var mo, day, yr;
    var entry = fld.value;
    var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
    if (entry != "") {
        var valid = (reLong.test(entry)) || (reShort.test(entry));
        if (valid) {
            var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
            var delim1 = entry.indexOf(delimChar);
            var delim2 = entry.lastIndexOf(delimChar);
            mo = parseInt(entry.substring(0, delim1), 10);
            day = parseInt(entry.substring(delim1 + 1, delim2), 10);
            yr = parseInt(entry.substring(delim2 + 1), 10);
            ////alert(testDate.getFullYear());
            ////alert(testDate.getFullYear());
            //alert(yr);
            // handle two-digit year
            if (yr < 100) {
                var today = new Date();
                // get current century floor (e.g., 2000)
                var currCent = parseInt(today.getFullYear() / 100) * 100;
                // two digits up to this year + 15 expands to current century
                var threshold = (today.getFullYear() + 15) - currCent;
                if (yr > threshold) {
                    yr += currCent - 100;
                } else {
                    yr += currCent;
                }
            }
            var testDate = new Date(yr, mo - 1, day);
            if (testDate.getDate() == day) {
                if (testDate.getMonth() + 1 == mo) {

                    if (testDate.getFullYear() == yr) {
                        labelerrormessage.innerText = "";
                        // fill field with database-friendly format

                        fld.value = mo + "/" + day + "/" + yr;

                    } //if (testDate.getFullYear() == yr)
                    else {
                        fld.value = "";
                        labelerrormessage.innerText = "";
                        labelerrormessage.innerText = "There is a problem with the year entry.";
                        ////alert("There is a problem with the year entry.");
                        fld.focus();
                    } //if (testDate.getMonth() + 1 == mo)
                } else {
                    fld.value = "";
                    labelerrormessage.innerText = "";
                    labelerrormessage.innerText = "There is a problem with the month entry.";
                    ////alert("There is a problem with the month entry.");
                    fld.focus();
                }
            } else {
                fld.value = "";
                labelerrormessage.innerText = "";
                labelerrormessage.innerText = "There is a problem with the date entry.";
                ////alert("There is a problem with the date entry.");
                fld.focus();
            }
        }
        else {
            fld.value = "";
            labelerrormessage.innerText = "";
            labelerrormessage.innerText = "Please enter a valid date.";
            ////alert("Incorrect date format. Enter as mm/dd/yyyy.");
            fld.focus();
        }
    }
    else {
    }
    return true;
}




// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

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 trim(s) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
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++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone) {
    var bracket = 3
    strPhone = trim(strPhone)
    if (strPhone.indexOf("+") > 1) return false
    if (strPhone.indexOf("-") != -1) bracket = bracket + 1
    if (strPhone.indexOf("(") != -1 && strPhone.indexOf("(") > bracket) return false
    var brchr = strPhone.indexOf("(")
    if (strPhone.indexOf("(") != -1 && strPhone.charAt(brchr + 2) != ")") return false
    if (strPhone.indexOf("(") == -1 && strPhone.indexOf(")") != -1) return false
    s = stripCharsInBag(strPhone, validWorldPhoneChars);
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateForm(Phone, labelerrormsg) {
    //    var Phone=document.frmSample.txtPhone

    //          if ((Phone.value == null) || (Phone.value == "")) {
    //              labelerrormsg.innerText = "";
    //              //labelerrormsg.innerText = "Please Enter your Phone Number";
    //              //Phone.focus()
    //              return false
    //          }
    if (checkInternationalPhone(Phone.value) == false) {
        labelerrormsg.innerText = "";
        labelerrormsg.innerText = "Please Enter a Valid Phone Number";
        Phone.value = ""
        //Phone.focus();
        return false
    }
    mask(Phone.value, Phone, '3,7', '-', labelerrormsg)
}

function mask(str, textbox, loc, delim, labelerrormessage) {
    var totalvalue = str;
    var locs = loc.split(',');

    for (var i = 0; i <= locs.length; i++) {
        for (var k = 0; k <= str.length; k++) {
            if (k == locs[i]) {
                if (str.substring(k, k + 1) != delim) {
                    str = str.substring(0, k) + delim + str.substring(k, str.length)
                }
            }
        }
    }
    if (str.length == 12) {
        textbox.value = str;
        labelerrormessage.innerText = "";
        return true
    }
    else {
        textbox.value = "";
        labelerrormessage.innerText = "";
        labelerrormessage.innerText = "Please Enter Ten Digit Phone Number";
        // textbox.focus();
        return false;
    }

}
