appsRepeatable = {
	j : 1,
	n : 1,
	
	init : function(id) {
		that=this;
		jQuery('#'+id).children().each(function(i, itemin) {
			if (itemin.id) {
				divTag=jQuery('#'+itemin.id);
				if (divTag.attr('class')=='zfsub') {
					divTag.children('input').eq(0).each(function(i,input) {
						if (divTag.attr('class')=='zfsub') {
							inputTag=jQuery('#'+input.id);
							inputTag.attr('pos',1);
							inputTag.attr('name',inputTag.attr('name')+'[]');
							inputTag.bind('keydown',that,function(event) {
								return event.data.tab(event);
					        });

						}
					});
				}
			}
		});
	},

	del : function(id,pos) {
		if (this.n==1) {
			alert('You can\'t remove this element');
			return;
		}
		this.n--;
		item=jQuery('#'+id);
		jQuery('#'+id).children().each(function(i, itemin) {
			if (itemin.id) {
				divTag=jQuery('#'+itemin.id);
				c=divTag.children('input').size();
				if (divTag.attr('class')=='zfsub') {
					divTag.children('input').each(function(i,input) {
						inputTag=jQuery('#'+input.id);
						if (inputTag.attr('pos')==pos || (pos==1 && i==0)) {
							if (i>0) inputTag.prev().remove();
							else inputTag.next().remove();
							inputTag.remove();
						}
					});
				}
				if (divTag.attr('class')=='zftablecontrol') {
					divTag.children('input').each(function(i,input) {
						inputTag=jQuery('#'+input.id);
						if (inputTag.attr('pos')==pos || (pos==1 && i==0)) {
							if (i>0) inputTag.prev().remove();
							else inputTag.next().remove();
							inputTag.remove();
						}
					});
				}
			}
		});

		
	},
	
	add : function(id,pos) {
		this.j++;
		this.n++;
		that=this;
		var html="";
		item=jQuery('#'+id);
		jQuery('#'+id).children().each(function(k, itemin) {
			if (itemin.id) {
				divTag=jQuery('#'+itemin.id);
				if (divTag.attr('class')=='zfsub') {
					divTag.children('input').each(function(i,input) {
						inputTag=jQuery('#'+input.id);
						if (inputTag.attr('pos')==pos || (pos==1 && i==0)) {
							newInputTag=jQuery("<input>");
							key=divTag.attr('id')+'_'+that.j;
							newInputTag.attr('id',divTag.attr('id')+'_'+that.j);
							newInputTag.attr('pos',that.j);
							newInputTag.attr('class',inputTag.attr('class'));
							newInputTag.attr('type',inputTag.attr('type'));
							newInputTag.bind('keydown',that,function(event) {
								return event.data.tab(event);
					        });

							name=inputTag.attr('name');
							newInputTag.attr('name',name);
							if (inputTag.attr('size')>0) newInputTag.attr('size',inputTag.attr('size'));
							if (inputTag.attr('maxlength')>0) newInputTag.attr('maxlength',inputTag.attr('maxlength'));
							inputTag.after(newInputTag);
							newInputTag.before(jQuery('<br />'));
							if (k==0) newInputTag.focus();
						}
					});
				}
				if (divTag.attr('class')=='zftablecontrol') {
					divTag.children('input').each(function(i,input) {
						inputTag=jQuery('#'+input.id);
						if (inputTag.attr('pos')==pos || (pos==1 && i==0)) {
							newInputTag=jQuery("<input>");
							newInputTag.attr('id',divTag.attr('id')+'_'+that.j);
							if (inputTag.attr('value')=="+") action='appsRepeatable.add(\''+id+'\','+that.j+')';
							else action='appsRepeatable.del(\''+id+'\','+that.j+')';
							newInputTag.attr('onclick',action);
							newInputTag.attr('pos',that.j);
							newInputTag.attr('class',inputTag.attr('class'));
							newInputTag.attr('value',inputTag.attr('value'));
							newInputTag.attr('type',inputTag.attr('type'));
							inputTag.after(newInputTag);
							newInputTag.before(jQuery('<br />'));
						}
					});
				}
			}
		});
	},
	
	tab: function(event) {

		var t = jQuery(event.target);
		pos=t.attr('pos');
    	 if (event.keyCode == '9' && !event.shiftKey) {
    		 p=t.parent();
    		 s=p.next();
    		 if (s.attr('class')=='zfsub') {
    			 s.children('input').each(function(i,input) {
    				 c=jQuery('#'+input.id);
    				 if (c.attr('pos')==pos) {
    					 c.focus();
    				 }
    			 });
    		 } else {
    			 s=p.parent().children().eq(0);
    			 s.children('input').each(function(i,input) {
    				 c=jQuery('#'+input.id);
    				 if (c.attr('pos')==pos) {
    					 if (c.next() && c.next().next()) c.next().next().focus();
    					 else return true;
    				 }
    			 });
    		 }
        	 return false;
    	 }
    	 if (event.keyCode == '9' && event.shiftKey) {
    		 p=t.parent();
    		 s=p.prev();
    		 if (s.attr('class')=='zfsub') {
    			 s.children('input').each(function(i,input) {
    				 c=jQuery('#'+input.id);
    				 if (c.attr('pos')==pos) {
    					 c.focus();
    				 }
    			 });
    		 } else {
    			 s=p.parent().children().eq(-4);
    			 s.children('input').each(function(i,input) {
    				 c=jQuery('#'+input.id);
    				 if (c.attr('pos')==pos) {
    					 if (c.next() && c.prev().prev()) c.prev().prev().focus();
    					 else return true;
    				 }
    			 });
    		 }
        	 return false;
    	 }
	}
};


