// JScript File
//Added Datefourdigit param for dd/MM/yyyy fromat message
function checkDate(field, format, Monthfourdigit, Datefourdigit, Invalidyearentry, Invalidmonthentry, Invaliddateentry, DateformatDDMONYYYY, Entertime, Valueforhours, Invalidvalueforminutes, Invalidtimeformat, EntervalidDateandTime, ValiddatemonthMMDDYYHHMM, ValiddatemonthMMDDYYHHMMTT, EntervalidDateandTime, Invalidvalueforhours, Invalidvalueforminites) {
    if (field.value != '') {
    }
    var errorMessage = ''
    var RegExPattern = ''
    if (format == 'MM/DD/YYYY')//dd/MM/yyyy
    {
        RegExPattern = /^(?=\d{2})(?:(?:(?:(?:(?:0?[13578]|1[02])(\/||\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/||\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/||\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/||\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{4}))($|\ (?=\d{2})))?$/;
        errorMessage = Monthfourdigit;
    }
    if (format == 'DD/MM/YYYY')//dd/MM/yyyy
    {
        RegExPattern = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/;
        errorMessage = Datefourdigit;
    }
    if (format == 'DD-MMM-YYYY')//dd-MMM-yyyy
    {
        return checkDateDDMMMYYYY(field);
    }
    if (field.value != '') {
        if (field.value.match(RegExPattern)) {
            var arr = field.value.split('/')
            if (arr[2] < 1900) {
                alert(errorMessage);
                field.value = "";
                return false;
            }
            return true;
        }
        else {
            alert(errorMessage);
            field.value = "";
            return false;
        }
    }
}
// validation for DD-MMM-YYYY format
function getMonth(val) {
    var monthArray = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN",
    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
    for (var i = 0; i < monthArray.length; i++) {
        if (monthArray[i] == val) {
            return (i);
        }
    }
    return (-1);
}
function checkDateDDMMMYYYY(fld) {
    var success = true;
    var mo, day, yy, testDate;
    var val = fld.value.toUpperCase();
    //alert(val);
    var re = new RegExp("[0-9]{2}[-][A-Z]{3}[-][0-9]{4}");
    //alert(re.test(val));
    if (re.test(val)) {
        var delimChar = (val.indexOf("/") != -1) ? "-" : "-";
        var delim1 = val.indexOf(delimChar);
        var delim2 = val.lastIndexOf(delimChar);
        day = parseInt(val.substring(0, delim1), 10);
        mo = getMonth(val.substring(delim1 + 1, delim2), 10);
        yy = parseInt(val.substring(delim2 + 1), 10);
        if (yy < 1900) {
            alert(DateformatDDMONYYYY);
            fld.value = "";
            success = false;
        }
        testDate = new Date(yy, mo, day);
        if (testDate.getDate() == day) {
            if (testDate.getMonth() == mo) {
                if (!testDate.getFullYear() == yy) {
                    alert(Invalidyearentry);
                    fld.value = "";
                    success = false;
                }
            }
            else {
                alert(Invalidmonthentry);
                fld.value = "";
                success = false;
            }
        }
        else {
            alert(Invaliddateentry);
            fld.value = "";
            success = false;
        }
    }
    else {
        alert(DateformatDDMONYYYY);
        fld.value = "";
        success = false;
    }
    //if (success)
    //alert(testDate.toLocaleDateString());
    //else
    //fld.focus();
    return (success);
}
//Validate date(DDMMMYYYY) and time.
function checkDateDDMMMYYYYandTime(field, dateformat, Timeformat) {
    //alert(field);
    var val = field.value;
    //alert(val);
    var date = val.substring(0, 11);
    //alert(date);
    //alert(dateformat);
    if (checkDateValue(date, dateformat)) {
        var time = val.substring(12);
        if (time == "") {
            alert(Entertime);
            return false;
        }
        else {
            return checkTimeValue(time, Timeformat);
        }
    }
    else {
        return false;
    }
}
//Validate Time    
function checkTime(field, format) {
    var TimeFormat = format;
    var errorMsg = "";
    // regular expression to match required time format 
    if (TimeFormat == "HH:mm") {
        re = /^(\d{1,2}):(\d{2})(:00)?([AP]M)?$/;
    }
    if (TimeFormat == "LT") {
        re = /^(\d{1,2}):(\d{2})(:00)? ((AM|am|PM|pm))?$/;
    }
    //re = /^(\d{1,2}):(\d{2})(:00)? ((AM|am|PM|pm))?$/;  
    if (field.value != '') {
        if (regs = field.value.match(re)) {
            if (TimeFormat == "LT") {
                // 12-hour time format with am/pm 
                if (regs[1] < 1 || regs[1] > 12) {
                    errorMsg = Valueforhours + regs[1];
                }
            }
            else if (TimeFormat == "HH:mm") {
                // 24-hour time format 
                if (regs[1] > 23) {
                    errorMsg = Valueforhours + regs[1];
                }
            }
            if (!errorMsg && regs[2] > 59) {
                errorMsg = Invalidvalueforminutes + regs[2];
            }
        }
        else {
            errorMsg = Invalidtimeformat + field.value;
        }
    }
    if (errorMsg != "") {
        alert(errorMsg);
        field.focus();
        return false;
    }
    return true;
}
function checkDateAndTime(field, dateformat, Timeformat, Monthfourdigit, Datefourdigit, Invalidyearentry, Invalidmonthentry, Invaliddateentry, DateformatDDMONYYYY, Entertime, Valueforhours, Invalidvalueforminutes, Invalidtimeformat, EntervalidDateandTime, ValiddatemonthMMDDYYHHMM, ValiddatemonthMMDDYYHHMMTT, ValiddateDateDDMMYYHHMM, ValiddateDateDDMMYYHHMMTT, EntervalidDateandTime, Invalidvalueforhours, Invalidvalueforminites) {
    if (dateformat == 'DD-MMM-YYYY') {
        return checkDateDDMMMYYYYandTime(field, dateformat, Timeformat);
    }
    else if (dateformat == 'MM/DD/YYYY') {
        var RegExPattern = "";
        var errorMessage = EntervalidDateandTime + dateformat + ' ' + Timeformat;
        var displayTimeFormat = '';
        var displayDateFormat = 'MM/dd/yyyy';
        if (Timeformat == "HH:mm") {
            displayTimeFormat = 'HH:mm';
            if (parseInt(field.value.length) != 16) {
                errorMessage = ValiddatemonthMMDDYYHHMM;
                alert(errorMessage);
                field.focus();
                return false;
            }
            RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
            errorMessage = ValiddatemonthMMDDYYHHMM;
        }
        if (Timeformat == "LT") {
            displayTimeFormat = 'hh:mm tt';
            if (parseInt(field.value.length) != 19) {
                errorMessage = ValiddatemonthMMDDYYHHMMTT;
                alert(errorMessage);
                field.focus();
                return false;
            }
            RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M)))?$/;
            errorMessage = ValiddatemonthMMDDYYHHMMTT;
        }
    }
    else if (dateformat == 'DD/MM/YYYY') {
        //Changed lert message with given arguments values by Manjula.p ON 15-Feb-2014
        var RegExPattern = "";
        var errorMessage = EntervalidDateandTime + dateformat + ' ' + Timeformat;
        var displayTimeFormat = '';
        var displayDateFormat = 'dd/MM/yyyy';
        if (Timeformat == "HH:mm") {
            displayTimeFormat = 'HH:mm';
            if (parseInt(field.value.length) != 16) {
                errorMessage = ValiddateDateDDMMYYHHMM;
                alert(errorMessage);
                field.focus();
                return false;
            }
            RegExPattern = /^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d)?\d{2})) (20|21|22|23|[0-1]?\d):[0-5]?\d$/;
            errorMessage = ValiddateDateDDMMYYHHMM;
        }
        if (Timeformat == "LT") {
            displayTimeFormat = 'hh:mm tt';
            if (parseInt(field.value.length) != 19) {
                errorMessage = ValiddateDateDDMMYYHHMMTT;
                alert(errorMessage);
                field.focus();
                return false;
            }
            RegExPattern = /^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d)?\d{2})) ((20|21|22|23|[0-1]?\d):([0-5]?\d){0,2}(\ [AP]M))?$/;
            errorMessage = ValiddateDateDDMMYYHHMMTT;
        }
    }
    var errorMessage = EntervalidDateandTime + displayDateFormat + ' ' + displayTimeFormat;
    if (field.value != '') {
        if (field.value.match(RegExPattern)) {
            var arr = field.value.split('/')
            var arrYear = arr[2].split(' ');
            if (arrYear[0] < 1900) {
                alert(errorMessage);
                field.focus();
                return false;
            }
            return true;
        }
        else {
            alert(errorMessage);
            field.focus();
            return false;
        }
    }
}
//Check Date, parameter by value
function checkDateValue(field, format) {
    //alert(field);
    // alert(format);
    if (format == 'DD-MMM-YYYY') {
        return checkDateDDMMMYYYYValue(field);
    }
    else {
        if (field != '') {
            var re = /\d{2}\/\d{2}\/\d{4}/;
            if (field.match(re)) {
            }
            else {
                alert(Monthfourdigit)
                return false;
            }
        }
        var RegExPattern = /^(?=\d{2})(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{4}))($|\ (?=\d{2})))?$/;
        var errorMessage = Monthfourdigit;
        if (field != '') {
            if (field.match(RegExPattern)) {
                var arr = field.split('/')
                if (arr[2] < 1900) {
                    alert(errorMessage);
                    //field.focus();
                    return false;
                }
                return true;
            }
            else {
                alert(errorMessage);
                //field.focus();
                return false;
            }
        }
    }
}
//Check Date DDMMMYYYY by value
function checkDateDDMMMYYYYValue(fld) {
    var success = true;
    var mo, day, yy, testDate;
    var val = fld.toUpperCase();
    var re = new RegExp("[0-9]{2}[-][A-Z]{3}[-][0-9]{4}");
    //alert(re.test(val));
    if (re.test(val)) {
        var delimChar = (val.indexOf("/") != -1) ? "-" : "-";
        var delim1 = val.indexOf(delimChar);
        var delim2 = val.lastIndexOf(delimChar);
        day = parseInt(val.substring(0, delim1), 10);
        mo = getMonth(val.substring(delim1 + 1, delim2), 10);
        yy = parseInt(val.substring(delim2 + 1), 10);
        testDate = new Date(yy, mo, day);
        if (testDate.getDate() == day) {
            if (testDate.getMonth() == mo) {
                if (!testDate.getFullYear() == yy) {
                    alert(Invaliddateentry);
                    success = false;
                }
            }
            else {
                alert(Invalidmonthentry);
                success = false;
            }
        }
        else {
            alert(Invaliddateentry);
            success = false;
        }
    }
    else {
        alert(DateformatDDMONYYYY);
        success = false;
    }
    return (success);
}
//Validate Time by value   
function checkTimeValue(field, format) {
    // alert(field);
    var TimeFormat = format;
    var errorMsg = "";
    // regular expression to match required time format 
    if (TimeFormat == "HH:mm") {
        re = /^(\d{1,2}):(\d{2})(:00)?([AP]M)?$/;
    }
    if (TimeFormat == "LT") {
        re = /^(\d{1,2}):(\d{2})(:00)? ((AM|am|PM|pm|Am|aM|Pm|pM))?$/;
    }
    //re = /^(\d{1,2}):(\d{2})(:00)? ((AM|am|PM|pm))?$/;  
    if (field != '') {
        if (regs = field.match(re)) {
            if (TimeFormat == "LT") {
                // 12-hour time format with am/pm 
                if (regs[1] < 1 || regs[1] > 12) {
                    errorMsg = Invalidvalueforhours + regs[1];
                }
            }
            else if (TimeFormat == "HH:mm") {
                // 24-hour time format 
                if (regs[1] > 23) {
                    errorMsg = Invalidvalueforhours + regs[1];
                }
            }
            if (!errorMsg && regs[2] > 59) {
                errorMsg = Invalidvalueforminites + regs[2];
            }
        }
        else {
            errorMsg = Invalidtimeformat + field;
        }
    }
    if (errorMsg != "") {
        alert(errorMsg);
        //field.focus(); 
        return false;
    }
    return true;
}
//Validate Time by value for Shift Master  
function checkTimeValueOfShift(field1, format, Invalidtimeformat) {
    var field = RemoveSpaces(field1);
    // alert(field);
    var TimeFormat = format;
    var errorMsg = "";
    var Invalidvalueforhours = "Invalidvalueforhours";
    var Invalidvalueforminites = "Invalidvalueforminites";
    // regular expression to match required time format 
    var re = '';
    //This Line add by karthikeyan for the bootstrap purpose on 25-11-2015
    //ToDo
    if (TimeFormat == 'LT') {
        TimeFormat = 'LT';
    }
    else {
        TimeFormat = 'HH:mm';
    }

    if (TimeFormat == "HH:mm") {
        //re = /^(\d{1,2}):(\d{2})(:00)?([AP]M)?$/;        
        re = /^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/;
    }
    if (TimeFormat == "LT") {
        re = /^(\d{1,2}):(\d{2})(:00)? ((AM|am|PM|pm|Am|aM|Pm|pM))?$/;
        // re = /^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/;
        //re= /^(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)$/;
    }
    regs = field.split(':');
    //re = /^(\d{1,2}):(\d{2})(:00)? ((AM|am|PM|pm))?$/;  
    // alert(re.test(field.value))
    if (re.test(field)) {
        if (TimeFormat == "LT") {
            // 12-hour time format with am/pm 
            if (regs[0] < 1 || regs[0] > 12) {
                errorMsg = Invalidvalueforhours + regs[1];
            }
        }
        else if (TimeFormat == "HH:mm") {
            // 24-hour time format 
            if (regs[0] > 23) {
                errorMsg = Invalidvalueforhours + regs[1];
            }
        }
        if (!errorMsg && regs[2] > 59) {
            errorMsg = Invalidvalueforminites + regs[2];
        }
    }
    else {
        errorMsg = Invalidtimeformat + field;
    }
    if (errorMsg != "") {
        alert(errorMsg);
        //field.focus(); 
        return false;
    }
    return true;
}
function RemoveSpaces(fieldName) {
    var s;
    if (typeof (fieldName) == 'object') {
        s = fieldName.value;
    }
    else {
        s = fieldName;
    }
    s = s.replace(/(^\s*)|(\s*$)/gi, "");
    s = s.replace(/[ ]{2,}/gi, " ");
    s = s.replace(/\n /, "\n");
    if (typeof (fieldName) == 'object') {
        fieldName.value = s;
    }
    return s;
}
///Added By Manjula.P on 07-Feb-2014 for checking time format with given format
//function CheckTimeFormat(field,TimeFormat,alertInTimeCorrectFormat,alertEnterTime)
//{
//            var regExp="^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$";
//            var length = 0;
//            if(TimeFormat != "LT")
//            {
//            length = 8;
//            regExp= "^(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?)$";
//            }
//            else
//            {
//            length = 8;
//            regExp = "^(([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))$";
//            }
//            if (field.value!="" )
//            { 
//                if(parseInt(field).value.length )!=length )
//                {
//                    field.focus();
//                    alert(alertInTimeCorrectFormat);
//                    return false;       
//                }  
//                if (field.value.search(regExp)==-1)
//                {
//                    field.focus();
//                    alert(alertInTimeCorrectFormat);
//                    return false;
//                }
//           }
//           else
//           {
//                 field.focus();
//                 alert(alertEnterTime);
//                 return false;
//           }
//}