/**
 * Generic repeating form element script, inspired by wForms @ formassembly.com
 * originally licensed under under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
 * 
 * <form action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
 * 	<fieldset>
 * 		<fieldset class="sg-repeat">
 * 			<input class="sg_repeating_id" type="hidden" name="random_id" value="5" />
 * 			<input class="sg_repeating_action" type="text" name="action[0]" value="" />
 * 			<fieldset>
 * 				<legend>User Details</legend>
 * 					<label for="uname" class="error">Username <em>*</em></label> 
 * 					<input id="uname" type="text" name="uname[0]" value="" class="error" />
 * 					<label for="email">Email Address </label>
 * 					<input id="email" type="text" name="email[0]" value="" />
 * 			</fieldset>
 * 			<a class="sg-repeat-link" href="#">Add Another Name</a><br/>
 * 		</fieldset>
 * 	</fieldset>
 * 	<input type="submit" name="submit" value="Submit" id="submit" />
 * </form>
 *
 */

jQuery.sage = {
	form : {
		repeating : {
			duplicate : function (e) {
				var clean = ''; // temp clean up var
				var counter = 0; // prime counter
				
				// prepare duplicate
				var container = $(e).parent();
				var copy = container.cloneWithEvents(true); // duplicate tree
				
				// get the last count
				var count = jQuery.sage.form.repeating.get_count(container.find("input:first").get(0));
				// if the count is larger than counter, set counter to it incremented, else increment counter
				counter = (count > counter) ? (count + 1) : (counter + 1);
				
				// process duplicate children
				jQuery.sage.form.repeating.process_children(copy.get(0), counter);

				// remove add link from original
				container.find('.sg-repeat-link').remove();

				// append remove link
				container.append('<a href="#" class="sg-remove-link">Remove</a>');
				
				// attach event to links
				container.find('.sg-remove-link').click(function () { jQuery.sage.form.repeating.remove(this); return false; });
				
				// append to container
				container.parent().append(copy);
			},
			
			remove : function (e) {
				var id = $(e).parent().find('.sg_repeating_id');
				var action = $(e).parent().find('.sg_repeating_action');
				
				if(id.attr("name") && id.val() > 0) {
					action.val("delete");
					$(e).parent().hide();
				} else {
					$(e).parent().remove();
				} 
			},
			
			get_count : function (e) {
				var count = 0;
				
				if ($(e).attr("name").indexOf('[') > -1) {
					var data = $(e).attr("name").split('[');
					count = data[1].substring(0, data[1].length-1);
				}
				
				return parseInt(count);
			},
			
			process_children : function (e, counter) {
				$(e).children().each(function() {
					var node = $(this);
					
					// handle on a per element basis
					switch (this.tagName.toUpperCase())
					{
						case 'TEXTAREA':
							node.html(""); // clear value
						
						default:
							// process name attribute
							if (node.attr("name")) {
								clean = node.attr("name").split('[');
								clean = clean[0] + '[' + counter + ']';
								node.attr("name", clean);
							}
							// process for attribute
							if (node.attr("for")) {
								clean = node.attr("for").split('[');
								clean = clean[0] + '[' + counter + ']';
								node.attr("for", clean);
							}
							// clear value on the right types
							if (node.attr("name") && (node.attr("name").indexOf('action') == -1) && node.attr("value") && this.tagName.toUpperCase() != 'OPTION' && (node.attr("type") && node.attr("type").toUpperCase() != 'RADIO' && node.attr("type").toUpperCase() != 'CHECKBOX' && node.attr("type").toUpperCase() != 'HIDDEN')) {
								node.val('');
							}
					}
			
					// process its children
					jQuery.sage.form.repeating.process_children(this, counter);
				});
			}
		}
	}
};