
function IsCustomInteger(arg_nValue,arg_nMin, arg_nMax, arg_bRequired){

	if (arg_nValue==''){return !arg_bRequired}	

	if (!CustomCompareDataType(arg_nValue,'Integer')){return false}
	return IsCustomRange(arg_nValue,arg_nMin,arg_nMax)

}

function IsCustomDecimal(arg_nValue,arg_nMin, arg_nMax, arg_bRequired){

	if (arg_nValue==''){return !arg_bRequired}
	if (!CustomCompareDataType(arg_nValue,'Double')){return false}
	return IsCustomRange(arg_nValue,arg_nMin,arg_nMax)
}

function IsCustomPercentage(arg_nValue, arg_bRequired){

	return IsCustomInteger(arg_nValue,1,100,arg_bRequired)
}

function IsCustomRange(arg_nValue,arg_nMin, arg_nMax){

	if (arg_nMin!=''){
		if (!CustomCompare(arg_nValue,arg_nMin,  'GreaterThanEqual')){return false}
	}
	
	if (arg_nMax!=''){
		if (!CustomCompare(arg_nValue,arg_nMax,  'LessThanEqual')){return false}
	}
	
	return true
}


function IsCustomRequired(arg_nValue){

	return arg_nValue!=''
}

function CustomCompareDataType(operand, datatype) {

 	return (CustomConvert(operand, datatype, '') != null)

}


function CustomCompare(operand1, operand2, operator) {

    var op1, op2;

	op1=operand1
	op2=operand2
	switch (operator) {
		case "NotEqual":
			return (op1 != op2);
		case "GreaterThan":
			return (op1 > op2);
		case "GreaterThanEqual":
			return (op1 >= op2);
		case "LessThan":
			return (op1 < op2);
		case "LessThanEqual":
			return (op1 <= op2);
		default:
			return (op1 == op2);            
    }
}

function CustomConvert(op, dataType, val) {
    function GetFullYear(year) {
        return (year + parseInt(val.century)) - ((year < val.cutoffyear) ? 0 : 100);
    }
    var num, cleanInput, m, exp;
    if (dataType == "Integer") {
        exp = /^\s*[-\+]?\d+\s*$/;
        if (op.match(exp) == null) 
            return null;
        num = parseInt(op, 10);
        return (isNaN(num) ? null : num);
    }
    else if(dataType == "Double") {

        exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + "." + "(\\d+))?\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);            
    } 
    else if (dataType == "Currency") {
        exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + "," + ")*)(\\d+)"
                        + ((val.digits > 0) ? "(\\" + "." + "(\\d{1," + val.digits + "}))?" : "")
                        + "\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        var intermed = m[2] + m[5] ;
        cleanInput = m[1] + intermed.replace(new RegExp("(\\" + "," + ")", "g"), "") + ((val.digits > 0) ? "." + m[7] : 0);
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);            
    }
    else if (dataType == "Date") {
        var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
        m = op.match(yearFirstExp);
        var day, month, year;
        if (m != null && (m[2].length == 4 || val.dateorder == "ymd")) {
            day = m[6];
            month = m[5];
            year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
        }
        else {
            if (val.dateorder == "ymd"){
                return null;		
            }						
            var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
            m = op.match(yearLastExp);
            if (m == null) {
                return null;
            }
            if (val.dateorder == "mdy") {
                day = m[3];
                month = m[1];
            }
            else {
                day = m[1];
                month = m[3];
            }
            year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
        }
        month -= 1;
        var date = new Date(year, month, day);
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
    }
    else {
        return op.toString();
    }
}

