/*
 *  @author: Thomas Pink | Netural Communication | t.pink@netural.com
 *  @version: 1.1
 */
(function($){
    var _this = null;
    var _errorboxname = ".errormsg";
    var methods = {
        init: function(options){
            var _form = $(this);
            return _form.each(function(){
                $(this).find(".submit").bind("click.formvalidator", function() {
					methods.check($(this).attr("rel"));
				});
            });
            
        },
        destroy: function(){
        
            return this.each(function(){
                $(this).find(".submit").unbind('.formvalidator');
            });
        },
        check: function(_id){
			_form = $("#"+_id);
			var iserror = false;
            var errorbox = _form.find(_errorboxname);
            errorbox.children().hide();
            _form.siblings('form.dateselector').removeClass('error');
            _form.find(".required").each(function(i){
                $(this).parent().removeClass("error");
                if($(this).hasClass("checkbox")) {
                    if($(this).hasClass("checked") === false){
                        $(this).addClass("error");
                        errorbox.find('p[rev="' + $(this).attr("id") + '"][rel="empty"]').show();
                        iserror = true;
                    }
                } else if($(this).hasClass("select-box")) {
					var _box = $(this);
					_box.removeClass("error");
					_box.find("select > option").each(function() {
						if($(this).hasClass("not-selectable") && $(this).attr("selected") == "selected") {
							_box.addClass("error");
                        	errorbox.find('p[rev="' + _box.find("select").eq(0).attr("id") + '"][rel="empty"]').show();
                        	iserror = true;			
						}
					});
				} else if (!$(this).val()) {
                    $(this).parent().addClass("error");
                    errorbox.find('p[rev=' + $(this).attr("id") + '][rel="empty"]').show();
                    iserror = true;
                } else {
                    if ($(this).hasClass("email") && !/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/.test($(this).val())) {
                        $(this).parent().removeClass("error").addClass("error");
                        errorbox.find('p[rev="' + $(this).attr("id") + '"][rel="notvalid"]').show();
                        iserror = true;
                    }
                }
            });
            
            if (iserror) {
                errorbox.find('p[rev="all"][rel="all"]').show();
                //DOKA SPECIFIC -> TRAINING FORMS
                if(_form.siblings('form.dateselector').length !== 0){
                    if(_form.siblings('form.dateselector').find('.form-row .custom-radio input:checked').length === 0){
                        _form.siblings('form.dateselector').addClass('error');
                    }
                }

                return false;
            } else {
            	//Doka spamprotection
				if(($("input#spamVal1").length!=0) && ($("input#spamVal2").length!=0)) {
					if(($("input#spamVal1").val()=="Doka") && ($("input#spamVal2").val()=="")) {
						_form.submit();
                		return true;
					} else {
						return false;				
					}
				}
				else {
					_form.submit();
                	return true;
				}                
            }
        }
    };
    
    $.fn.formvalidator = function(method){
    
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else 
            if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            }
            else {
                $.error('Method ' + method + ' does not exist on jQuery.formvalidator');
            }
        
    };
    
})(jQuery);

