PFR.Validators = {
    Helper: {
        showValidationError: function (elm, message) {
            var id = $(elm).attr('id');
            var errorDiv = document.getElementById(id + '-error');
            if (!errorDiv) {
                errorDiv = $('<span id="' + id + '-error" class="error"></span>');
                $(elm).after(errorDiv);
            }
            $(errorDiv).html(message);
        },
        clearValidationError: function (elm) {
            var id = $(elm).attr('id');
            var errorDiv = document.getElementById(id + '-error');
            if (!errorDiv) {
                return;
            }
            $(errorDiv).html('');
        },
        insureElm: function (elm) {
            if (typeof elm != 'string') return elm;
            if (elm.length == 0) return null;
            var obj = $((elm.charAt(0) == '#' ? '' : '#') + elm);
            if (obj.length) return obj;
            obj = $('input[name=' + elm + ']');
            return obj;
        }

    },
    NonEmpty: function (elm, args) {
        elm = PFR.Validators.Helper.insureElm(elm);
        args = $.extend({}, {
            trim: true,
            message: 'Please enter a value'
        }, args);
        var val = $(elm).val();
        if (args.trim) {
            val = $.trim(val);
        }
        if (val.length > 0) {
            PFR.Validators.Helper.clearValidationError(elm);
            return true;
        }
        PFR.Validators.Helper.showValidationError(elm, args.message);
        return false;
    },
    Text: function (elm, args) {
        elm = PFR.Validators.Helper.insureElm(elm);
        args = $.extend({}, {
            trim: true,
            message: 'Please enter a valid value',
            required: true,
            minLength: 1,
            insureNumber : true,
            insureNotSpecialChar : true
        }, args);
	   if (args.preCondition && !args.preCondition()) {
            PFR.Validators.Helper.clearValidationError(elm);
            return true;
        }
        var val = $(elm).val();
        if (args.trim) {
            val = $.trim(val);
        }
        if(!args.insureNumber) {
        	PFR.Validators.Helper.showValidationError(elm,args.message)
        	return false;   	
        }
        if(!args.insureNotSpecialChar) {
         	PFR.Validators.Helper.showValidationError(elm,args.message)
        	return false;       	
        }
        if (!args.required && val.length == 0) {
            PFR.Validators.Helper.clearValidationError(elm);
            return true;
        }
        if (val.length >= args.minLength && (typeof args.maxLength == 'undefined' || val.length <= args.maxLength)) {
            PFR.Validators.Helper.clearValidationError(elm);
            return true;
        }
       /* if(val.length < args.minLength && typeof args.maxLength != 'undefined') {
        	PFR.Validators.Helper.showValidationError(elm, args.message);
        	return false;
        	if(val.length > args.maxLength ) {
         		PFR.Validators.Helper.showValidationError(elm, args.message);
        		return false;       		
        	}
        }*/
        PFR.Validators.Helper.showValidationError(elm, args.message);
        return false;
    },
    defaultSelected : function(elm) {
    	 elm = PFR.Validators.Helper.insureElm(elm);
    	 var elmVal = $(elm).val();
    	 var elmDefault = elmVal == 0;
    	 if(elmDefault) {
    	 	$(elm).addClass('redErrorBorder');
    	 	PFR.Validators.Helper.showValidationError(elm, "");
    	 	return false;
    	 }else {
    	 	PFR.Validators.Helper.clearValidationError(elm);
    	 	$(elm).removeClass('redErrorBorder');
    	 	return true;
    	 }
    },
    validateZip : function(elm,args){
		elm = PFR.Validators.Helper.insureElm(elm);
		var elmVal = $(elm).val();
		var lengthIsFive = elmVal.length == 5;
        args = $.extend({}, {
            trim: true,
            message: 'Please enter a valid zip',
            required: true,
            minLength: 1
        }, args);
        if (args.preCondition && !args.preCondition()) {
            PFR.Validators.Helper.clearValidationError(elm);
            return true;
        }
    	 if(!lengthIsFive) {
    	 	PFR.Validators.Helper.showValidationError(elm, args.message);
    	 	return false;
    	 }else {
    	 	PFR.Validators.Helper.clearValidationError(elm);
    	 	return true;
    	 }	
    },
    Custom: function (elm, args) {
        elm = PFR.Validators.Helper.insureElm(elm);
        args = $.extend({}, {
            trim: true,
            message: 'Please enter a valid value',
            validator: function () { return false; }
        }, args);
        var val = $(elm).val();
        if (args.trim) {
            val = $.trim(val);
        }
        if (args.validator(val, args)) {
            PFR.Validators.Helper.clearValidationError(elm);
            return true;
        }
        PFR.Validators.Helper.showValidationError(elm, args.message);
        return false;
    },
    Number: function (elm, args) {
        elm = PFR.Validators.Helper.insureElm(elm);
        args = $.extend({}, {
            trim: true,
            message: 'Please enter a valid value',
            minValue: Number.MIN_VALUE,
            maxValue: Number.MAX_VALUE,
            allowDecimal: true,
            allowZero: true
        }, args);
        var val = $(elm).val();
        if (args.trim) {
            val = $.trim(val);
        }

        var num = parseFloat(val);
        if ((args.allowDecimal || val.toString().indexOf('.') == -1)
			&& !isNaN(val)
			&& val >= args.minValue
			&& val <= args.maxValue
			&& (args.allowZero || val != 0)
		) {
            PFR.Validators.Helper.clearValidationError(elm);
            return true;
        }
        PFR.Validators.Helper.showValidationError(elm, args.message);
        return false;

    },
    RegEx: function (elm, args) {
        elm = PFR.Validators.Helper.insureElm(elm);
        args = $.extend({}, {
            trim: true,
            message: 'Please enter a valid value'
        }, args);
        var val = $(elm).val();
        if (args.trim) {
            val = $.trim(val);
        }
        if (typeof args.expression == 'string') {
            args.expression = new RegExp(args.expression);
        }
        if (args.expression.test(val)) {
            PFR.Validators.Helper.clearValidationError(elm);
            return true;
        }
        PFR.Validators.Helper.showValidationError(elm, args.message);
        return false;

    },
    Email: function (elm, args) {
        elm = PFR.Validators.Helper.insureElm(elm);
        args = $.extend({}, {
            trim: true,
            message: 'Please enter a valid email address'
        }, args);
        var val = $(elm).val();
        if (args.trim) {
            val = $.trim(val);
        }
        if (/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(val)) {
            PFR.Validators.Helper.clearValidationError(elm);
            return true;
        }
        PFR.Validators.Helper.showValidationError(elm, args.message);
        return false;

    }
};

