/*
|--------------------------------------------------------------------------
| Amedia Creative Frontend Framework > Form Validation
|--------------------------------------------------------------------------
|
| @package		Amedia Creative
| @subpackage	Frontend Framework
| @company		Amedia Creative, Inc.
| @phone		310/651/8733
| @fax			310/388/1210
| @author		Joey Avino / Bart Jedrychowski
| @email		joey@amediacreative.com / bart@amediacreative.com
| @link			http://www.amediacreative.com
| @copyright	2008 Amedia Creative, Inc.
| @requires 	mootools.1.2.js
|
*/

/*
|--------------------------------------------------------------------------
| Amedia Creative Frontend Framework > Form Validation > Core JavaScript
|--------------------------------------------------------------------------
|
*/

	var aui_validation = new Class({

		initialize: function(err_css_class) {

			this.error_css_class = err_css_class;
			this.val_event = 'change';
			this.default_gtp = 3;
			this.val_form = false;
			this.callback = false;
			//this.val_add_stars();

		},





		/**
		* Function adds stars to required fields.
		*
		* @name 	Vall Add Stars
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		val_add_stars: function(where) {

			where.getElements('.val_star').each(function(el) {

				var holder = null;

				var gtp = this.default_gtp-1;

				if (el.hasClass('val_dropdown'))
					gtp = gtp -1;

				if (el.hasClass('multiselect_checkbox'))
					holder = new Element('div',{'class':'val_req_star'});

				if (holder == null)
						holder = new Element('span',{'class':'val_req_star'});

				holder.appendText('*');
				var parent = this.get_to_parent(el,gtp);
				parent.grab(holder,'bottom');

			}.bind(this));
		},





		/**
		* Function to validate a phone number
		*
		* @name 	Val Phone
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		val_phone: function(str) {

			var obj = $(str);
			obj.addEvent(this.val_event, function(_this) { 
					  
				_this.check_phone(obj);

			}.pass(this));
		},





		/**
		* Function that will check a phone number.
		*
		* @name 	Val Email
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		check_phone: function(obj) {

			var tmp = obj.get('value').trim();

			if (obj.hasClass('val_nr') && tmp.length == 0)
				return true;

			var parent = this.get_to_parent(obj,this.default_gtp);
			var valid = tmp.test(/(\+)?([-\._\(\) ]?[\d]{3,20}[-\._\(\) ]?){2,10}/);
			this.switch_error_class(valid,parent);
			return valid;

		},





		/**
		* Function that will validate a minimum length.
		*
		* @name 	Val Length
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		val_minlen: function(str, min) {

			var obj = $(str);
			obj.addEvent(this.val_event, function(_this) {

				_this.check_minlen(obj, min);

			}.pass(this));	
		},





		/**
		* Function which checks mimimum length.
		*
		* @name 	Check Minlen
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		check_minlen: function(obj, min) {

			var tmp = obj.get('value').trim();

			if (obj.hasClass('val_nr') && tmp.length == 0)
				return true;

			var parent = this.get_to_parent(obj,this.default_gtp);

			var valid = (tmp.length >= min);
			this.switch_error_class(valid,parent);

			return valid;

		},





		/**
		* Function that will validate email address.
		*
		* @name 	Val Email
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		val_email: function(str) {

			var obj = $(str);
			obj.addEvent(this.val_event, function(_this) {

				_this.check_email(obj);

			}.pass(this));	
		},





		/**
		* Function that will ensure passwords match and are a min length of 4.
		*
		* @name 	Val Password
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino
		* @email	joey@amediacreative.com
		*/ 
		val_password: function(str, min) {

			var obj = $(str);
			obj.addEvent(this.val_event, function(_this) {

				_this.check_password(obj, min);

			}.pass(this));	
		},	





		/**
		* Function that will check an email address.
		*
		* @name 	Check Email
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		check_email: function(obj) {

			var tmp = obj.get('value').trim();

			if (obj.hasClass('val_nr') && tmp.length == 0)
				return true;

			var parent = this.get_to_parent(obj, this.default_gtp);
			var valid = tmp.test(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			this.switch_error_class(valid,parent);
			return valid;

		},





		/**
		* Function that will check password rules.
		*
		* @name 	Check Password
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino
		* @email	joey@amediacreative.com
		*/ 
		check_password: function(obj, min) {

            var valid = true;
            var valid1 = true;
            var valid2 = true;
            var input = $$('.val_password_'+min);

            var pass = input[0].get('value').trim();
            var repass = input[1].get('value').trim();

            if (pass.length > 0 && pass.length < min) {

                valid = false;
                valid1 = false;

            }

            if (repass.length > 0 && pass != repass) {

                valid = false;
                valid2 = false;

            }

            if (valid && pass.length == 0) {

                valid = false;
                valid1 = false;

            }

            var parent1 = this.get_to_parent(input[0], this.default_gtp);
            var parent2 = this.get_to_parent(input[1], this.default_gtp);

            this.switch_error_class(valid1, parent1);
            this.switch_error_class(valid2, parent2);


        },





		/**
		* Function that will validate a URL.
		*
		* @name 	Val URL
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		val_url: function(str) {

			var obj = $(str);

			obj.addEvent(this.val_event, function(_this) {
  
				_this.check_url(obj);

			}.pass(this));	
		},





		/**
		* Function that will validate email address.
		*
		* @name 	Check URL
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		check_url: function(obj) {

			var parent = this.get_to_parent(obj,this.default_gtp);
			var url = obj.get('value').trim();

			var valid = true;

			if (url != 'http://' &&  url != '') {
				valid = url.test(/^(http:\/\/)(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/); }

			if (url == 'http://')
				valid = false;

			this.switch_error_class(valid,parent);
			return valid;

		},





		/**
		* Function that will validate a dropdown
		*
		* @name 	Val Dropdown
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		val_dropdown: function(str) {

			var obj = $(str);
			obj.addEvent(this.val_event, function(_this) {

				_this.check_dropdown(obj);

			}.pass(this));	
		},





		/**
		* Function that will check a dropdown.
		*
		* @name 	Check Dropdown
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		check_dropdown: function(obj) {

			var tmp = obj.get('value').trim();

			if (obj.hasClass('val_nr') && tmp.length == 0)
				return true;

			var parent = this.get_to_parent(obj,this.default_gtp-1);
			var valid = (tmp.length >= 1);
			this.switch_error_class(valid,parent);
			return valid;
		},





		/**
		* Function that will validate a file input.
		*
		* @name 	Val File
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		val_file: function(str) {

			var obj = $(str);
			obj.addEvent(this.val_event, function(_this) {

				_this.check_file(obj);

			}.pass(this));	
		},





		/**
		* Function that will check a file input.
		*
		* @name 	Check File
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		check_file: function(obj) {

			var tmp = obj.get('value').trim();

			if (obj.hasClass('val_nr') && tmp.length == 0)
				return true;

			var parent = this.get_to_parent(obj,this.default_gtp-1);
			var valid = (tmp.length >= 1);
			this.switch_error_class(valid,parent);
			return valid;
		},





		/**
		* Function that will validate a radio button.
		*
		* @name 	Val Radio
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		val_radio: function(str) {

			var obj = $(str);
			obj.addEvent(this.val_event, function(_this) {
	  
				_this.check_radio(obj);

			}.pass(this));	
		},	





		/**
		* Function that will validate check a radio button.
		*
		* @name 	Check Radio
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		check_radio: function(obj) {

			var parent = this.get_to_parent(obj,this.default_gtp);
			var radios = parent.getElements('input');
			var valid = false;

			radios.each(function(radio) {
	
				if (radio.get('checked') == true)
					valid = true;

			});

			this.switch_error_class(valid,parent);
			return valid;

		},





		/**
		* Function that will validate a form in total.
		* If there's an error, it will not let the user submit a form and display an alert.
		*
		* @name 	Val Fform
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 


		val_fform: function(obj, cl) {

			var val = this;
			obj.addEvent('submit', function(e) {

				var valid = aui_check_form(obj, cl, val);

				if (valid)
					return val._callback(e,obj);
				
				alert('Please check your entries.');
				e.stop();
				
				
			}); 
		},





		/**
		* Function that will validate a form in total.
		*
		* @name 	Val Callback
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		_callback: function(e, obj) {
			
			if (this.callback) {
				e.stop();
				return eval(this.callback);
			}	
			return;
					
		},





		/**
		* Function that will toggle the error class if validation is false.
		*
		* @name 	Val Email
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		switch_error_class: function(tf,target) {

			if (tf)
				target.removeClass(this.error_css_class);
			else
				target.addClass(this.error_css_class);

		},





		/**
		* Function that sets a location to the parent.
		*
		* @name 	Get To Parent
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino / Bart Jedrychowski
		* @email	joey@amediacreative.com / bart@amediacreative.com
		*/ 
		get_to_parent: function(p, c) {

			for (var i=0; i<c; i++)
				p = p.getParent();

			return p;

		}
	});





	/**
	* Function that checks the form as a whole
	*
	* @name 	AUI Check Form
	* @access 	public
	* @param 	var-type none
	* @return 	none
	* @author 	Joey Avino / Bart Jedrychowski
	* @email	joey@amediacreative.com / bart@amediacreative.com
	* @notes	Philip [2/15/09]: added K as a counter to fix overwrite problem.
	*/ 
	var aui_check_form = function(form,check_list,obj) {

		var valid = new Array();
		var k = 0;
		
		for (var i=0, c=check_list.length; i<c;i++) {
			
				var els = form.getElements(check_list[i]);

				els.each(function(el, j) {
					
					valid[k] = aui_router('check',check_list[i],el,obj);
					k++;
				});
		}

		var flag = true;

		for (var i=0,c=valid.length;i<c;i++) {

			if (valid[i] == false) {
				flag = false;
			}

		}

		return flag;

	}





	/**
	* Function that will initialze the validation and the fields requested.
	*
	* @name 	Init Val
	* @access 	public
	* @param 	var-type none
	* @return 	none
	* @author 	Joey Avino / Bart Jedrychowski
	* @email	joey@amediacreative.com / bart@amediacreative.com
	*/ 
	function init_val(check_list, form) {

		var validation = new aui_validation('val_error');
		validation.val_fform(form, check_list);

		//validation.val_add_stars(form);
		if (arguments[2] != '')
			validation.callback = arguments[2];

		for (var i=0, c=check_list.length; i<c; i++) {

			var els = form.getElements(check_list[i]);

			els.each(function(el) {

				aui_router('val', check_list[i], el, validation);

			});
		}
	}





	/**
	* Function that routes the form to the proper validation methods.
	*
	* @name 	Init Val
	* @access 	public
	* @param 	var-type none
	* @return 	none
	* @author 	Joey Avino / Bart Jedrychowski
	* @email	joey@amediacreative.com / bart@amediacreative.com
	*/ 
	var aui_router = function(vc, str, el, v) {

		if (vc == 'val' || vc == 'check') {

			var user_func = '';

			if (str.substr(0,13) == '.val_min_len_')
				user_func += 'v.'+vc+'_minlen(el, '+str.substr(str.lastIndexOf("_")+1)+')';

			if (str == '.val_phone')
				user_func = 'v.'+vc+'_phone(el)';

			if (str == '.val_email')
				user_func = 'v.'+vc+'_email(el)';

			if (str == '.val_dropdown')
				user_func = 'v.'+vc+'_dropdown(el)';

			if (str == '.val_url')
				user_func = 'v.'+vc+'_url(el)';

			if (str == '.val_file')
				user_func = 'v.'+vc+'_file(el)';

			if (str.substr(0,14) == '.val_password_')
				user_func += 'v.'+vc+'_password(el, '+str.substr(str.lastIndexOf("_")+1)+')';

			return (user_func != '') ? eval(user_func): false;

		}
	}





	/**
	* Function that will toggle the classes of a submit buttion.
	*
	* @name 	Submit Toggle
	* @access 	public
	* @param 	var-type none
	* @return 	none
	* @author 	Joey Avino / Bart Jedrychowski
	* @email	joey@amediacreative.com / bart@amediacreative.com
	*/ 
	function submit_toggle() {

		var start_event = 'mouseenter';
		var end_event = 'mouseleave';
		var b = $$('input.submit');

		b.each(function(el) {

			el.addEvent(start_event,function() {
		 
				this.set('class','submit_over');

			});

			el.addEvent(end_event,function() {

				this.set('class','submit');

			});
		});
	}





	/**
	* Function that will create a custom dropdown.
	* The label needs to have an id _label after it.
	*
	* @name 	Dropdown
	* @access 	public
	* @param 	var-type none
	* @return 	none
	* @author 	Joey Avino / Bart Jedrychowski
	* @email	joey@amediacreative.com / bart@amediacreative.com
	*/
	function dropdown(obj) { // Newer version to be inserted later

		var select = $(obj);
		$(obj + '_label').set('text', select.options[select.selectedIndex].text);

		select.addEvent('change', function() {

			$(obj + '_label').set('text', select.options[select.selectedIndex].text);

		});
	}





	/**
	* Function that will create a custom file input
	* The label needs to have an id _label after it.
	*
	* @name 	Dropdown
	* @access 	public
	* @param 	var-type none
	* @return 	none
	* @author 	Joey Avino
	* @email	joey@amediacreative.com
	*/
	function file(obj) { // Newer version to be inserted later

		var select = $(obj);
		//$(obj + '_label').set('text', select.options[select.selectedIndex].text);

		//select.addEvent('change', function() {

			//$(obj + '_label').set('text', select.options[select.selectedIndex].text);

		//});
	}





	/**
	* Function that toggles the div focus classes.
	*
	* @name 	Div Toggle
	* @access 	public
	* @param 	var-type none
	* @return 	none
	* @author 	Joey Avino / Bart Jedrychowski
	* @email	joey@amediacreative.com / bart@amediacreative.com
	*/ 
	function div_toggle_input(obj, sub_div, css_left, css_mid, css_right, _focus) {
 
		var start_event = 'click';

		var list = $$(obj);

		list.each(function(element) {

			var sub_element = element.getElement(sub_div);

			if (sub_element) {

				var _left = sub_element.getElement('.left');
				var _mid = sub_element.getElement('.mid');
				var _right = sub_element.getElement('.right');

				if (_focus == true)
					var _input = _mid.getElement(sub_div.substr(1));

				element.addEvent(start_event, function() { 

					//Remove the focus class on click from another input.
					list.each(function(el) {

						var sub_element = el.getElement(sub_div);

						if (sub_element) {

							var _left = sub_element.getElement('.left');
							var _mid = sub_element.getElement('.mid');
							var _right = sub_element.getElement('.right');

							if (_focus == true)
								var _input = _mid.getElement(sub_div.substr(1));

							if (_left.hasClass(css_left))
								_left.removeClass(css_left);

							if (_mid.hasClass(css_mid))
								_mid.removeClass(css_mid);
			
							if (_right.hasClass(css_right))						
								_right.removeClass(css_right);

							if (_focus == true && _input.hasClass(sub_div.substr(1) + '_focus'))
								_input.removeClass(sub_div.substr(1) + '_focus');

						}
					});
				
					//Set the current input with the CSS class.
					_left.toggleClass(css_left);
					_mid.toggleClass(css_mid);
					_right.toggleClass(css_right);

					if (_focus == true)
						_input.toggleClass(sub_div.substr(1) + '_focus');

				});
			}
		});
	}





	/**
	* Function to activate the multiadd once it's been initialized
	*
	* @name 	New Multiadd Class
	* @access 	public
	* @param 	var-type none
	* @return 	none
	* @author 	Joey Avino
	* @email	joey@amediacreative.com
	*/
	var Multiadd = new Class({

		/**
		* Function to activate the multiadd
		*
		* @name 	Display Carousel
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino
		* @email	joey@amediacreative.com
		*/
		Implements: Options, options: {

			add_link: 'add_input_link',
			event: 'click',
			id_input: 'article_links',
			id_ol_list: 'article_links_list',
			val_type: '.val_min_len_3',
			form: 'my_form',
			hlite_color: '#fff2b6',
			input_name: 'myname[]'

		},

		initialize: function(options) {

			this.i = 1;

			this.setOptions(options);
			this.add_link = $(this.options.add_link);
			this.id_input = $(this.options.id_input);
			this.id_ol_list = $(this.options.id_ol_list);
			this.val_type = this.options.val_type;
			this.form = $(this.options.form);
			this.event = this.options.event;
			this.input_name = this.options.input_name;

			this.add_link.addEvent(this.options.event, this.f_add.bind(this));

		},





		/**
		* Function to add the item to the list.
		*
		* @name 	Form Add
		* @access 	public
		* @param 	var-type none
		* @return 	none
		* @author 	Joey Avino
		* @email	joey@amediacreative.com
		*/
		f_add: function() {

			var val = $(this.id_input).get('value');

			var check_list = new Array();
				check_list[0] = this.val_type;

			init_val(check_list, this.form);

			var li = new Element('li', {id: 'item-'+this.i, text:val});
			var a = new Element('a', {id:'item_remove-'+this.i, 'class':'btn-img remove', text:'X '});
			var input = new Element('input', {'name': this.input_name, 'value':val, 'style': 'display:none;'});
			a.inject(li, 'top');
			input.inject(li, 'bottom');
			this.id_input.set('value', '');
			this.id_ol_list.adopt(li);

			$(a).addEvent(this.event, function(e) {

				li.erase('html');
				li.setStyle('display', 'none');

			});

			li.highlight(this.hlite_color);
			this.i++;

		}
	});
