﻿var sco2009 = {}

sco2009.formValidator = function() {
	return new sco2009.formValidator.Constructor
}
sco2009.formValidator.Constructor = function() {
	this.init()
}
sco2009.formValidator.Constructor.prototype = {
	init: function() {
		this.form = $('.accreditation')
		
		this.inputs = this.form.find('input[type="text"], select, input[type="file"], input[type="radio"]')
		
		$('input[type="text"]', this.form)
			.bind('blur', 
				function(evt){
					this.value = this.value.replace(/^\s+/, '').replace(/\s+$/, '').replace('~', '').replace('(', '').replace(')', '').replace('>', '').replace('<', '').replace('/', '').replace('\\', '')
				}
			)

		this.form.bind('submit', {that: this}, function(evt) { 
			that = evt.data.that;
			$('.warn', that.form).removeClass('warn')
			if (!that.checkInputs.apply(that, [that.inputs])) {
				evt.preventDefault()
			} else {
				that.form[0].submit()
			}
		})
	},
	
	checkInputs: function(nodeset) {
		var ok = true, that = this
		nodeset.each(function(i) {
			_node = $(this)
			if (!that.checkNode(_node)) {
				ok = false
				that.warn(_node)
			}
		})
		
		if (!ok) {
			alert(this.form.find('p.err_msg').text())
		}
		return ok
	},
	
	warn: function(node) {
		var nName = node[0].nodeName;
		if (nName == 'INPUT') {
			if (node.attr('type') == 'text') {
				node.addClass('warn')
			} else if(node.attr('type') == 'radio') {
				node.parents('fieldset').find('legend').addClass('warn')
			} else if (node.attr('type') == 'file') {
				node.prev('legend').addClass('warn')
			}
		} else if (nName = 'SELECT') {
			node.parents('fieldset').find('legend').addClass('warn')
		}
	},
	
 	checkNode: function(node) {
		var nName = node[0].nodeName;
		if (nName == 'INPUT') {
			return this.checkInput(node)
		} else if (nName == 'SELECT') {
			return this.checkSelect(node)
		} 
	}, 
	
	checkInput: function(node) {
		var type = node.attr('type')
		if (type == 'text') {
			return this.checkText(node)
		} else if (type == 'radio') {
			return this.checkRadio(node)
		}
		else {
			return node.val() != ''
		}
	},
	checkText: function(node) {
		if (this.isMandatory(node)) {
			if (!this.isEmpty(node) && this.isValid(node)) {
				var tests = {
					cyr: node.hasClass('cyr'),
					lat: node.hasClass('lat'),
					email: node.hasClass('email'),
					digit: node.hasClass('digit'),
					phone: node.hasClass('phone')
				}
				
				var result = true, that = this;
				$.each(tests, function(key, val){
					if (val) {
						result = result && that.tests[key](node.val())
					}
				})
				return result
			} else {
				return false
			}
		} 
		return true;
	},
	
	isMandatory: function(node) {
		return 	node.hasClass('mandatory') || 
				(node.parents('label, fieldset, form').hasClass('mandatory') && !node.hasClass('optional'))
	},
	
	isEmpty: function(node) {
		return node.val() == '' || /^\s+?$/.test(node.val())
	},
	
	isValid: function(node) {
		return !/[\|\^\~\>\<]/.test(node.val())
	},
	
	tests: {
		word: function(val) {
			if (!val.split(' ').length == 0) {
				return false
			}
			return true;
		},
		cyr: function(val) { 
			if (!/^[А-Яа-я\.\- ]+?$/.test(val)) {
				return false
			}
			return true		
		},
		lat: function(val) { 
			if (!/^[A-Za-z\.\- ]+?$/.test(val)) {
				return false
			}
			return true
		},
		chn: function(val) {
			if (!/^[A-Za-z\.\- ]+?$/.test(val)) {
				return false
			}
			return true			
		},
		digit: function(val) { 
			if (!/^\d+?$/.test(val)) {
				return false			
			}
			return true
		},
		phone: function(val) { 
			if (!/^[\d\+\-\s]+?$/.test(val)) {
				return false			
			}
			return true
		},
		rustext: function(val) {
			if (!/^[А-Яа-я\.\,\- ]+?$/.test(val)) {
				return false
			}
			return true					
		},
		lattext: function(val) {
			if (!/^[A-Za-z\.\,\- ]+?$/.test(val)) {
				return false
			}
			return true					
		},
		rusaddress: function(val) {
			if (!/^[А-Яа-яA-Za-z\d\.\,\- ]+?$/.test(val)) {
				return false
			}
			return true								
		},
		lataddress: function(val) {
			if (!/^[A-Za-z\d\.\,\- ]+?$/.test(val)) {
				return false
			}
			return true								
		},
		pass: function(val) {
			if (!/^[A-Za-z\d]+?$/.test(val)) {
				return false
			}
			return true
		},
		email: function(val) { 
			var username_allowed = /[A-Za-z_\d\-\.]+/
			var domain_allowed = /[A-Za-z\-\d\.]+/
			var suffix_allowed = /[A-Za-z]{2,6}/

			if (val.split('@').length == 2) {
				var username_valid = username_allowed.test(val.split('@')[0]);
				var domain = val.split('@')[1].split('.')
				var suffix_valid = suffix_allowed.test(domain.pop())
				var domain_valid = domain_allowed.test(domain.join('.'))
				if (!(username_valid && suffix_valid && domain_valid)) {
					return false
				}
				return true
			} else {
				return false
			}
		}
	},
	
	checkSelect: function(node) {
		if (!(this.isMandatory(node) && this.isSelected(node)) || !this.isMandatory(node)) {
			return false			
		}
		return true
	},
	
	checkRadio: function(node) { 
		if (this.form.find('input[name="' + node.attr('name') +'"]:checked').length == 0) {
			return false
		}
		return true
	},
	
	isSelected: function(node) {
		return node.val() != 0
	}
}
 
sco2009.fieldToggler = function() {
	$('.radio_toggler label').click(function(evt){
		var toggler = $(this).parents('fieldset').find('input.toggler:checked')
		$(this).parents('label').find(':radio').attr('checked', 'checked')
		var label = $(this).parents('fieldset').find('label.toggled')
		var input = label.find('input')
		if (toggler.length) {
			label.removeClass('hidden')
			input.removeClass('volatile')
		} else {
			label.addClass('hidden')
			input.addClass('volatile')		
		}
	})
}

 
$(document).ready(function() {
	sco2009.formValidator()
	sco2009.fieldToggler()
});
