﻿var msgErr = "";
var msgAlertError = "";
function ValidateForm(arrCheckList, objDivId) {

    i = 0;
    var strValidateString;
    var objVal;
    var strError;
    var arrVal;
    var IsErr = false;
    var retVal = true;
    var errDiv = "";
    msgErr = "";
    msgAlertError = "";
    if (arrCheckList.length > 0) {
        for (i = 0; i < arrCheckList.length; i++) {
            if (document.getElementById(arrCheckList[i][0]) != null) {
                if (arrCheckList[i].length >= 1) {

                    objVal = document.getElementById(arrCheckList[i][0]);

                }
                if (arrCheckList[i].length >= 2) {
                    strValidateString = arrCheckList[i][1]
                }
                if (arrCheckList[i].length >= 3) {
                    strError = arrCheckList[i][2]
                }
                if (arrCheckList[i].length >= 4) {
                    errDiv = arrCheckList[i][3]
                }
            }
            //alert(objVal);
            if (validateInput(strValidateString, objVal, strError) == false) {
                // alert("1" + errDiv);
                stuff_error_msg(strError, errDiv)
            }
            else {
                //  alert("2" + errDiv);
                if (errDiv != "") {
                    document.getElementById(errDiv).innerHTML = "";
                }
            }
        }
    }
    if (msgAlertError != "") {
        alert(msgAlertError);
    }
    if (msgErr.length > 0) {
        //showMessage(objDivId);
        retVal = false;
    }
    //    else
    //    {
    //        clearError(objDivId)
    //    }
    return retVal;
}

function stuff_error_msg(strMsg, errorDiv) {
    var i;
    if (msgErr.length > 0) {
        msgErr = msgErr + "<br/>" + strMsg;
    }
    else {
        msgErr = strMsg;
    }
    if (errorDiv != "") {
        document.getElementById(errorDiv).innerHTML = strMsg;
        //alert( document.getElementById(errorDiv).innerHTML);
    }
    else {
        if (msgAlertError.length > 0) {
            msgAlertError = msgAlertError + "\n\r" + strMsg;
        }
        else {
            msgAlertError = strMsg;
        }

    }
}

function showMessage(objDivId) {
    document.getElementById(objDivId).innerHTML = "<span class='error_td'>" + msgErr + "</span>";
}

function clearError(objDivId) {
    document.getElementById(objDivId).innerHTML = "";

}

function validateInput(strValidateStr, objValue, strError) {
    //alert(strValidateStr);
    //alert(objValue);
    //alert(strError);
    var ret = true;
    var epos = strValidateStr.search(":");
    var command = "";
    var cmdvalue = "";
    var cmdvalue1 = "";
    if (epos >= 0) {
        command = strValidateStr.substring(0, epos);
        cmdvalue = strValidateStr.substr(epos + 1);
    }
    else {
        command = strValidateStr;
    }
    //alert(command); 
    switch (command) {
        case "req":
        case "required":
            {
                ret = TestRequiredInput(objValue, strError);
                // alert(ret);
                break;
            } //case required 
        case "requiredphoneno":
            {
                ret = TestRequiredInputPhoneNumber(objValue, cmdvalue, cmdvalue1, strError)
                break;
            } //case required
        case "multirequired":
            {
                ret = TestRequiredInput(objValue, strError);
                break;
            }  //case required 
        case "dlrequired":
            {

                ret = TestRequiredSelect(objValue, strError)
                break;
            } //case required 
        case "dltimerequired":
            {
                ret = TestRequiredTimeSelect(objValue, strError)
                break;
            } //case required 
        case "filetype":
            {
                ret = filterFileType(objValue, cmdvalue, strError);
                break;
            }
        case "maxlength":
        case "maxlen":
            {
                ret = TestMaxLen(objValue, cmdvalue, strError)
                break;
            } //case maxlen 
        case "minlength":
        case "minlen":
            {
                ret = TestMinLen(objValue, cmdvalue, strError)
                break;
            } //case minlen 
        case "minlengthB":
        case "minlenB":
            {

                ret = TestMinLength(objValue, cmdvalue, strError)
                break;
            } //case minlen     
        case "alnum":
        case "alphanumeric":
            {
                ret = TestInputType(objValue, "[^A-Za-z0-9]", strError,
						objValue.name + ": Only alpha-numeric characters allowed ");
                break;
            }
        case "alnum_s":
        case "alphanumeric_space":
            {
                ret = TestInputType(objValue, "[^A-Za-z0-9\\s]", strError,
						objValue.name + ": Only alpha-numeric characters and space allowed ");
                break;
            }
        case "num":
        case "numeric":
            {
                ret = TestInputType(objValue, "[^0-9]", strError,
						objValue.name + ": Only digits allowed ");
                break;
            }
        case "dec":
        case "decimal":
            {
                ret = TestInputType(objValue, "[^0-9\\.]", strError,
						objValue.name + ": Only numbers allowed ");
                break;
            }
        case "alphabetic":
        case "alpha":
            {
                ret = TestInputType(objValue, "[^A-Za-z]", strError,
						objValue.name + ": Only alphabetic characters allowed ");
                break;
            }
        case "alphabetic_space":
        case "alpha_s":
            {
                ret = TestInputType(objValue, "[^A-Za-z\\s]", strError,
						objValue.name + ": Only alphabetic characters and space allowed ");
                break;
            }
        case "email":
            {
                //          alert("sdf");
                if(objValue.value.replace(" ","").trim()!="" && objValue.value!=" ")
                {
                ret = TestEmail(objValue, strError);
                }
                break;
            }
        case "date":
            {
                ret = validateDate(objValue);
                break;
            }
        case "lt":
        case "lessthan":
            {
                ret = TestLessThan(objValue, cmdvalue, strError);
                break;
            }
        case "gt":
        case "greaterthan":
            {
                ret = TestGreaterThan(objValue, cmdvalue, strError);
                break;
            } //case greaterthan 
        case "regexp":
            {
                ret = TestRegExp(objValue, cmdvalue, strError);
                break;
            }
        case "dontselect":
            {
                ret = TestDontSelect(objValue, cmdvalue, strError)
                break;
            }
        case "dontselectchk":
            {
                ret = TestDontSelectChk(objValue, cmdvalue, strError)
                break;
            }
        case "shouldselchk":
            {
                ret = TestShouldSelectChk(objValue, cmdvalue, strError)
                break;
            }
        case "selone_radio":
            {
                ret = TestSelectOneRadio(objValue, strError);
                break;
            }
        case "compare":
            ret = TestEqual(objValue, cmdvalue, strError)
            break;
        case "datecompare":
            ret = TestLessDate(objValue, cmdvalue, strError)
            break;
        case "futuredate":
            ret = CheckForFutureDate(objValue, strError)
            break;
        case "pastdate":
            {
                ret = CheckForPastDate(objValue, strError)
                break;
            }
        case "pastmonth":
            {

                ret = CheckForPastMonth(objValue, strError)
                break;
            }
        case "futureYear":
            {
                ret = CheckForFutureYear(objValue, strError)
                break;
            }
        case "futureDOB":
            {
                ret = CheckForDOBMoreThen18(objValue, strError)
                break;
            }
        case "lessthenzero":
            {
                ret = CheckForLessThenZero(objValue, strError)
                break;
            }
        case "url":
            {
                ret = CheckURL(objValue, strError)
                break;
            }
        case "USphone":
            {
                ret = CheckUSPhoneFormate(objValue, strError)
                break;
            }
        case "custom":
            {
                break;
            }
    } //switch 
    return ret;
}

