jQuery().ready(function($) {
/* rx.library * rx-form.js : 12/08/09 */

// RX_autoHide : 12/08/09
// - hides default value on :focus
// - restores default value if unchanged on :blur
jQuery.fn.RX_autoHide = function() {
	function populateElement(selector, defvalue) {
		$(selector).focus(function() {
			if ($(selector).val() == defvalue) {
				$(selector).val('');
			}
		});

		$(selector).blur(function() {
			if ($.trim($(selector).val()) == '') {
				$(selector).val(defvalue);
			}
		});
	};

	return this.each(function() {
		populateElement($(this), $(this).val());
	});
}
//

var global_formError = false;

// RX_formValidate : 12/08/09
// - simple form validator
jQuery.fn.RX_formValidate = function() {
	function validateNoempty(text) {
    if (!text || text.length < 3) { return false;	}
		return true;
	}
	//

	function validateText(text) {
    if (!validateNoempty(text)) { return false; }
		var re_text = /[^A-Za-z0-9\.,\s]/;
		if (text.match(re_text) != null ) { return false; }
		return true;
	}
	//

	function validateEmail(email) {
		if (!validateNoempty(email)) { return false; }

		var splitted = email.match("^(.+)@(.+)$");
		if (splitted == null) return false;
		if (splitted[1] != null ) {
			var regexp_user=/^\"?[\w-_\.]*\"?$/;
			if (splitted[1].match(regexp_user) == null) { return false; }
		}

		if(splitted[2] != null) {
			var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
			if (splitted[2].match(regexp_domain) == null) {
				var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
				if (splitted[2].match(regexp_ip) == null) { return false; }
			}
			return true;
		}
		return false;
	}
	//

	function validateSelect(select) {
		if (select != -255) {
			return true;
		}
		return false;
	}
	//

	function validateInteger(number) {
		var numRegExp  = /\d+$/;
	  return numRegExp.test(number);
	}
	//

	return this.each(function() {
		$(this).click(function(e) {
			e.preventDefault();
			error = false;

			function set_error(node) {
				node.animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
				error = true;
			}

			function set_errorSelect(node) {
				if (node.parents('.rx-js-select').hasClass('rx-select-on') ) {
					node.parents('.rx-select-on').animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
					error = true;
				} else {
					set_error(node);
				}
			}

			$(this).parents('form').find('.v-noempty, .v-text, .v-email, .v-select, .v-integer').each( function() {
				if ($(this).hasClass('v-noempty')) {
					if (!validateNoempty($(this).val())) {
						set_error($(this));
					}
				} else if ($(this).hasClass('v-text')) {
					if (!validateText($(this).val())) {
						set_error($(this));
					}
				} else if ($(this).hasClass('v-email')) {
					if (!validateEmail($(this).val())) {
						set_error($(this));
					}
				} else if ($(this).hasClass('v-select')) {
					if (!validateSelect($(this).val())) {
						set_errorSelect($(this));
					}
				} else if ($(this).hasClass('v-integer')) {
					if (!validateInteger($(this).val())) {
						set_error($(this));
					}
				}
			});

			global_formError = error;

			if (!error) {
				$(this).parents('form').submit();
			}

		});

	});
	//
}
//

// RX_limitChars : 12/08/09
// - limits number of chars for input field
jQuery.fn.RX_limitChars = function(Q) {
	this.each(function() {
		var ax = $(this).attr('class').match(/lim\-\d{2,4}/),
				i, j, k;

		if (!ax) { return; }
		k = ax.toString().substr(4);

		$(this).keydown(function() {
			if ($(this).val().length >= k) {
				$(this).val( $(this).val().substr(0, k-1) );
			}
		});

		i = $(this);
		j = i.parents(Q.par).find(Q.cnt);

		setInterval( function() { j.html(k - i.val().length); }, 200 );

	});
}
//


	$('.rx-auto input, .rx-auto textarea').RX_autoHide();

	$('.rx-validate a, a.rx-validate').RX_formValidate();

	$('.v-limit').RX_limitChars({
		par : 'p', // closest parent to limited field and counter
		cnt : 'em' // counter tag
	});
});

/* */
