﻿// Handles Validation
/*
jQuery.fn.extend({
    AddValidation: function(options) {
        var opts = jQuery.extend({ name: '', empty: true, isInteger: false, isDecimal: false, isEmail: false, isCurrency: false }, options)
        return this.each(function() { jQuery(this).data('valid', opts); });
    },
    Validate: function(options) {
        var opts = jQuery.extend({ requireOne: false, errorDivId: '', errorDivWrapperId: '', errorClass: '', requireOneField: false }, options);
        var _errors = [];
        var em = {
            integer: ' must be an integer.',
            decimal: ' must be a decimal.',
            required: ' is required.',
            email: ' is not a valid email.',
            invalid: ' contains invalid characters',
            requireOne: 'The form requires at least one field.'
        }
        for (i = 0; i < this.length; i++) {
            var _item = jQuery(this[i]);
            var _itemData = _item.data('valid');
            if (!_itemData) { continue; }
            _item.removeClass(opts.errorClass);
            if (!_itemData.empty) {     // check empty
                if (!_item.val()) { _errors.push(_itemData.name + em.required); continue; }
            }
            if (_itemData.isInteger) {  // check Integer
                if (!/^\s*(\+|-)?\d+\s*$/.test(_item.val())) { _errors.push(_itemData.name + em.numeric); continue; }
            }
            if (_itemData.isDecimal) {  // check decimal
                if (!/^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/.test(_item.val())) { _errors.push(_itemData.name + em.decimal); continue; }
            }
            if (_itemData.isEmail) {    // check email
                if (!/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/.test(_item.val())) { _errors.push(_itemData.name + em.email); continue; }
            }
            if (_itemData.isCurrency) { // check currency
                if (!/^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/.test(_item.val())) { _errors.push(_itemData.name + em.currency); continue; }
            }
        }
        if (opts.requireOne) {
            var _count = 0;var _i = 0;
            for (i = 0; i < this.length; i++) {
                switch (this[i].type) {
                    case 'text':
                        (!jQuery(this[i]).val()) ? _i += 1 : false; _count += 1;break; 
                    case 'select-one':
                        (this[i].selectedIndex < 1) ? _i += 1 : false; _count += 1;break;
                    case 'select-multiple':
                        (this[i].selectedIndex < 0) ? _i += 1 : false; _count += 1;break;
                    default: break;
                }
            }(_i == _count) ? _errors.push(em.requireOne) : false;
        }
        if (_errors.length > 0) {
            var _html = 'Errors Found<br /><ul>'
            jQuery(_errors).each(function(i, n) { _html += '<li>' + n + '</li>'; });
            _html += '</ul>'
            jQuery(opts.errorDivId).html(_html);
            jQuery(opts.errorDivWrapperId).show();
        } else {
            jQuery(opts.errorDivId).html('');
            jQuery(opts.errorDivWrapperId).hide();
        } return (_errors.length > 0) ? false : true;
    }
});
*/
function global_requireOne(options) {
    var _c = [];
    var options = jQuery.extend({
        query: '',
        errorQuery: ''
    }, options);

    jQuery(options.errorQuery).hide();

    jQuery(options.query).each(function() {
        jQuery(':input, :select', this).each(function() {
            switch (this.type) {
                case 'text':
                    if (this.value != '') {  } break;
                case 'password':
                    if (this.value != '') {  } break;
                case 'select-one':
                    if (this.selectedIndex > 0) {  } break;
                case 'select-multiple':
                    if (this.selectedIndex > -1) {  } break;
            }
        });
    });

    if (_c.length < 1) { jQuery(options.errorQuery).show(); } 
    return (_c.length > 0) ? true : false;
}

function validate_require_one(wrapperId) {
    var _length = jQuery('input:text,select', wrapperId).length;
    var _errorCount = 0;
    jQuery('input:text,select', wrapperId).each(function() {
        if (jQuery(this).hasClass('exclude_validate')) {  }
        switch (this.type) {
            case 'text':
                if (this.value == '') { _errorCount += 1; } break;
            case 'password':
                if (this.value == '') { _errorCount += 1; } break;
            case 'select-one':
            case 'one':
                if (this.selectedIndex < 1) { _errorCount += 1; } break;
            case 'select-multiple':
            case 'multiple':
                if (this.selectedIndex < 0) { _errorCount += 1; } break;
        }
       
    });
    return (_errorCount < _length);
}