/******************************************************************/
//Validation Core

function validationtype(type, code, arg1, arg2) {
    type = type.toUpperCase();
    switch (type) {
        case 'EMPTY': { return isEmpty(code, arg1, arg2); break }
        case 'EMAIL': { return isEmail(code, arg1); break }
        case 'NUMERIC': { return isNumeric(code, arg1); break }
        case 'FILE': { return checkFileFormat(code, arg1, arg2); break }
        case 'MATCH': { return isMatch(code, arg1, arg2); break }
    }
}

/******************************************************************/
// Empty Checking

function isEmpty(code, arg1, arg2) {
    var errortitle = "";
    if (arg1.length <= 1) {
        if (arg1.length == 0) {
            errortitle = arg2;
        }
    } else {
        for (i = 0; i <= arg1.length - 1; i++) {
            checkfieldvalue = fieldval[i];
            checkfieldtitle = fieldtit[i];
            if (checkfieldvalue.length == 0) {
                if (errortitle == "") {
                    errortitle = checkfieldtitle;
                } else {
                    errortitle = errortitle + ", " + checkfieldtitle;
                }
            }
        }
    }
    if (errortitle != "") {
        return getErrorString(code, errortitle)
    } else {
        return getErrorString('1000', '')
    }
}

/******************************************************************/
//Match Checking

function isMatch(code, arg1, arg2) {
    if (arg1 != arg2) {
        return getErrorString(code, '')
    } else {
        return getErrorString('1000', '')
    }
}

/******************************************************************/
//This Function Validate the Image Format

function checkFileFormat(code, arg1, arg2) {
    var inc = 0;
    lo1 = arg1;
    logo1 = arg1.lastIndexOf(".");
    logole1 = arg1.length;
    le1 = logole1 - logo1;
    k1 = logo1;
    l1 = logole1;
    b1 = lo1.substring(k1, l1);
    b1 = b1.toLowerCase();
    for (i = 0; i <= filetype.length - 1; i++) {
        checkfiletype = filetype[i].toLowerCase();
        if (checkfiletype == b1) {
            inc = inc + 1;
        }
    }
    if (inc == 0) {
        return getErrorString(code, '');
    } else {
        return getErrorString('1000', '');
    }
}

/******************************************************************/
//This Function Checks the Numeric Values

function isNumeric(code, arg1) {
    value = arg1;
    if (isNaN(value)) {
        return getErrorString(code, '');
    } else {
        return getErrorString('1000', '');
    }
}

/******************************************************************/
//This Function Checks the Email

function isEmail(code, arg1) {
    var str = arg1;
    var str1 = str.toLowerCase();
    var i, m = 0, j = 0, k = 0, p = 0;
    var len = str1.length;
    var len1 = len - 1;
    for (i = 0; i <= len; i++) {
        if (str1.charAt(i) == "@") {
            j = j + 1;
            if (i == 0 || i == len1) {
                j = 10;
            }
        }
        if (str1.charAt(i) == ".") {
            k = k + 1;
            if (i == 0) {
                p = 1;
            }
            if (i == len1) {
                m = 0;
            } else {
                m = 1;
            }
        }
        if (str1.charAt(i) == "." && str1.charAt(i + 1) == "@") {
            return getErrorString(code, '');
        }
        if (str1.charAt(i) == "." && str1.charAt(i + 1) == ".") {
            return getErrorString(code, '');
        }
        if (str1.charAt(i) == "@" && str1.charAt(i + 1) == ".") {
            return getErrorString(code, '');
        }
    }
    if (j != 1 || (k = 0 && k > 2) || m != 1 || p == 1) {
        return getErrorString(code, '')
    } else {
        return getErrorString('1000', '');
    }
}

/******************************************************************/
//Trim : Remove the Left and Right Leading Spaces

function Trim(TRIM_VALUE) {
    if (TRIM_VALUE.length < 1) {
        return "";
    }
    TRIM_VALUE = RTrim(TRIM_VALUE);
    TRIM_VALUE = LTrim(TRIM_VALUE);
    if (TRIM_VALUE == "") {
        return "";
    } else {
        return TRIM_VALUE;
    }
}

/******************************************************************/
//Right Trim : This Function remove the Right Leading Spaces

function RTrim(VALUE) {
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if (v_length < 0) {
        return "";
    }
    var iTemp = v_length - 1;
    while (iTemp > -1) {
        if (VALUE.charAt(iTemp) == w_space) {
        } else {
            strTemp = VALUE.substring(0, iTemp + 1);
            break;
        }
        iTemp = iTemp - 1;
    }
    return strTemp;
}

/******************************************************************/
//Left Trim : This Function remove the Left Leading Spaces

function LTrim(VALUE) {
    var w_space = String.fromCharCode(32);
    if (v_length < 1) {
        return "";
    }
    var v_length = VALUE.length;
    var strTemp = "";
    var iTemp = 0;
    while (iTemp < v_length) {
        if (VALUE.charAt(iTemp) == w_space) {
        } else {
            strTemp = VALUE.substring(iTemp, v_length);
            break;
        }
        iTemp = iTemp + 1;
    }
    return strTemp;
}

/******************************************************************/
//Error Codes

function getErrorString(Codeid, arg1) {
    messageid = parseInt(Codeid);
    switch (messageid) {
        case 1000: { ErrResult = ''; return ErrResult; break }
        case 1001: { ErrResult = '* Mandatory Field Missing : ' + arg1; return ErrResult; break }
        case 1002: { ErrResult = 'Invalid Email'; return ErrResult; break }
        case 1003: { ErrResult = 'Invalid Number'; return ErrResult; break }
        case 1004: { ErrResult = 'Invalid Image'; return ErrResult; break }
        case 1005: { ErrResult = 'Does not Match'; return ErrResult; break }
    }
}

/******************************************************************/