
function RepeatableInput_addNew()
{
	if( this.maximum > 0 && this.inputs.length == this.maximum )
	{
			alert( "You have reached the maximum amount of entries." );
			return;
	}
	
	var obj_copy = this.divSource.cloneNode( true );
	obj_copy.id = 'repeaterNode_' + this.index;
	for( var i = 0; i < obj_copy.childNodes.length; i++ )
	{
		var obj_node = obj_copy.childNodes[i];
		if( obj_node.id )
			obj_node.id = obj_node.id + "_" + this.index;
		if( obj_node.name )
			obj_node.name = obj_node.name + "_" + this.index;
	}

	obj_copy.style.display = '';
	this.currentIndex = this.index;
	this.index++;
	this.inputs[this.inputs.length] = obj_copy;
	this.render();
}
function RepeatableInput_remove( obj_index ) // 0 based index
{
  var obj_parent = obj_index.parentNode;
	var str_nameArr = obj_parent.id.split("_");
	var int_index = parseInt( str_nameArr[1] );
	for( var i in this.inputs )
	{
		if( this.inputs[i] == obj_parent )
		{
			this.inputs.splice( i, 1 );
			this.containerDiv.removeChild( obj_parent );
			break;
		}
	}

  this.render();
}
function RepeatableInput_render()
{
//	this.containerDiv.innerHTML = "";
	for( var i in this.inputs )
	{		
		if( this.inputs[i] && typeof(this.inputs[i]) == "object" )
			this.containerDiv.appendChild( this.inputs[i] );
	}
}
function RepeatableInput_setMaximum( int_max )
{
	this.maximum = int_max;
}
function RepeatableInput_child_Serialize()
{
	var arr_str = new Array();
	
	for( var i = 0; i < this.inputs.length; i++ )
	{
		var obj_nodes = this.inputs[i].childNodes;
		var dte_childBirthDate = null
		if( obj_nodes[1].type && obj_nodes[1].type == 'text' )
			dte_childBirthDate = obj_nodes[1].value;
		else
			dte_childBirthDate = obj_nodes[3].value;
		if( dte_childBirthDate.length > 0 )
			arr_str[arr_str.length] = dte_childBirthDate;
	}
	
	return arr_str.join( "|" );
}
function RepeatableInput_child_Unserialize( str_data )
{
	var arr_data = str_data.split( "|" );
	for( var i = 0; i < arr_data.length; i++ )
	{
		var str_child = arr_data[i];
		this.addNew();
		var obj_newEntry = document.getElementById( "repeaterNode_" + this.currentIndex );

		if( obj_newEntry.childNodes[1].type && obj_newEntry.childNodes[1].type == 'text' )
			obj_newEntry.childNodes[1].value = str_child;
		else
			obj_newEntry.childNodes[3].value = str_child;
	}
}
function RepeatableInput( str_divId, str_repeatId )
{
  this.inputs = new Array();
	this.index = 0;
  this.div = document.getElementById( str_divId );
	this.containerDiv = document.createElement( "div" );
	this.containerDiv.style.width = this.div.style.width;

  this.divSource = document.getElementById( str_repeatId );
	this.currentIndex = -1;
	this.maximum = 0; // no limit...
	
	this.div.appendChild( this.containerDiv );
}
RepeatableInput.prototype.addNew = RepeatableInput_addNew;
RepeatableInput.prototype.remove = RepeatableInput_remove;
RepeatableInput.prototype.render = RepeatableInput_render;
RepeatableInput.prototype.setMaximum = RepeatableInput_setMaximum;
RepeatableInput.prototype.Serialize = RepeatableInput_child_Serialize;
RepeatableInput.prototype.Unserialize = RepeatableInput_child_Unserialize;

