/* 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() {
	var $ = jQuery.noConflict();
	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() {
	var $ = jQuery.noConflict();
	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) {
	var $ = jQuery.noConflict();
	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_selectField : 12/08/09
- replace <select> with dropdown box.
- multiple fields supported
*/
var RX_selectField = function(Q) {
	var $ = jQuery.noConflict();
	var c_href = 'js-',
			nodes = false;

	this.init = function() {
		nodes = Q.form.find(Q.parent);
		if (nodes.size() < 1) { return false; }

		_selectToList();
		_hookEventsToList();

		_toggleList();

		// outside click
		$(document).click(function(e) {
			if ($(e.target).parents('div.'+Q.c_node).size() == 0) { _hideAllLists(); }
		});
	}
	//

	// copy <select><option value="val">text</option></select> TO <ul><li><a href="val">text</li></ul>
	function _selectToList() {
		var ax = '<li><a href="#',
				bx = '">',
				cx = '</a></li>',
				dx = '</ul>',
				i;

		nodes.each(function() {
			var n = $(this),
					m = n.find('select.'+Q.c_node).children('option'),
					j = '&gt;',
					tx = '<ul>';

			if (m.size()>0) {
				m.each(function() {
					if ($(this).val() == -255) {
						j = $(this).html();
					} else {
						tx += ax + c_href + $(this).val() + bx + $(this).html() + cx;
					}

				});
				tx += dx;

				i = n.find('div.'+Q.c_node);
				i.append(tx);
				i.find(Q.n_txt).html(j);

				n.addClass(Q.c_toggle);
			}
		});
	}
	//

	// hook event to new created list
	function _hookEventsToList() {
		nodes.each(function() {
			var n = $(this),
					m = n.find('div.'+Q.c_node),
					o = m.children('ul');

			m.click(function(e) {
				var i = $(e.target),
						j, k;

				if (i.is('a')) {
					j = i.attr('href').substr(4);
					k = n.children('select');

					o.find('a[href*="#'+ c_href + k.val() +'"]').removeClass(Q.c_sel)
					k.val(j);
					i.addClass(Q.c_sel);

					m.find(Q.n_txt).html(i.text());

					_hideList(n);

					return false;
				}
			});

		});
	}
	//

	// hide current list
	function _hideList(node) {
		var i = node.children('div.'+Q.c_node);
		i.find('ul').hide('fast');
		i.find('a[href*="#'+Q.act_tgl+'"]').removeClass(Q.c_sel);
	}
	//

	// toggle list on and off
	function _toggleList() {
		nodes.children('div.'+Q.c_node).each(function() {
			var n = $(this);
			n.find('a[href*="#'+Q.act_tgl+'"]').click(function() {
				if ($(this).hasClass(Q.c_sel)) {
					$(this).removeClass(Q.c_sel);
					n.find('ul').hide('fast');
				} else {
					_hideAllLists();
					$(this).addClass(Q.c_sel);
					n.find('ul').show('normal');
				}

				return false;
			});
		});
	}
	//

	// hide all lists
	function _hideAllLists() {
		var i = nodes.children('div.'+Q.c_node);
		i.find('ul').hide('fast');
		i.find('a[href*="#'+Q.act_tgl+'"]').removeClass(Q.c_sel);
	}
	//
}
//

jQuery(document).ready(function($) {
	$('.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
	});

	var rx_selectField = new RX_selectField({
		form : $('.rx-auto'), 		// form / main parent
		parent : '.rx-js-select', 	// contaner for select and its replacement
		c_node : 'rx-select', 			// class for select and div for replacement
		c_toggle : 'rx-select-on',	// adds this class on success replacement
		c_sel : 'rx-selected',      // generic "selected" class
		act_tgl : 'rx-toggle',      // arrow - toggler
		n_txt : 'p em'              // node to display current selected item
	});
 	rx_selectField.init();

});

/* */
