//JS contain all common functions used in Nelonen EI2
/**
 * Clones elements. Used with form elements where you can optionally have several 
 * copies of the same elements
 *
 * Expects to be called from a link that's inside a <p> that's inside a <div>
 * that's inside the element to be cloned. The calling <div> will be removed
 * from the clone, and replaced with an element for removing the clone.
 *
 * The containing div should have id "rowContainer-n"
 * 
 * Set second parameter 'noreset' to 1 if dropdown selection should be 
 * reseted in the clone.
 *
 */

function addRow(je, reset) {
	// get a handle for the "add" link
	var callerId = je.parent().parent().attr('id');

	// get the name of the group
	var groupName = callerId.substr(1+callerId.indexOf('-'));
	groupName = groupName.substr(0,groupName.indexOf('-'));

	// Find the right element to copy and get the html
	var html = je.parent().parent().parent().html();

	// get the largest number of a rowContainer
	var rowContainer = je.parent().parent().parent();
	var number = 0;
	rowContainer.parent().find('.rowContainer').each(function(){
		var id = $(this).attr('id');
		var rowNumber = parseInt(id.substring((groupName+'-rowContainer-').length));
		if(rowNumber > number) {
			number = rowNumber;
		}
	});

	// Increment the number in the id's of the cloned elements
	number++;

	// TODO: This is legacy code, the elements should be named to work with the replaces below
	html = html.replace(/rowArea-([0-9]+)/g,"rowArea-"+number);
	html = html.replace(/rowArea\[([0-9]+)\]/g,"rowArea["+number+"]");
	html = html.replace(/rowItem-([0-9]+)/g,"rowItem-"+number);
	html = html.replace(/rowItem\[([0-9]+)\]/g,"rowItem["+number+"]");
	
	// TODO: Should rename all fields to work like this:
	// For example 'specialities-year-row[1]'
	html = html.replace(/-([a-zA-z]+)-row(-?)(\[?)([0-9]+)(\]?)/g,"-$1-row$2$3"+number+"$5");

	// fixes doubleSelect js
	html = html.replace(/rowItem_([0-9]+)/g,"rowItem_"+number);

	// Create the id and copy the class attributes for the new rowContainer
	var eId = groupName+'-rowContainer-'+number;
	var eClass = rowContainer.attr('class');

	// rename the "Lisää" element
	html = html.replace(/\-addRowLink\-([0-9]+)/,"-removeRowLink-"+number);

	html = '<div id="'+eId+'" class="'+eClass+'">' + html + '</div>';
	je.parent().parent().parent().parent().append(html);// 

	// Change the "poista" link html
	$('#'+'former-'+groupName+'-removeRowLink-'+number+' p').html('<a href="#" onclick="removeRow($(this));return false;">Poista</a>');
	
	
	// reset selections in new row?
	if(reset) {
		// No selections in the new row
// 		$('#'+groupName+'-rowItem-'+number+'-area').val(-1);
// 		$('#'+groupName+'-rowItem-'+number).html('');
// 	
// 		// And with the new syntax, in specialities
// 		$('#'+groupName+'-rowContainer-'+number+' select').val(-1);
// 		$('#'+groupName+'-rowContainer-'+number+' input').val('');
// 		$('#'+groupName+'-rowContainer-'+number+' .main p').html('');		
// 	
// 		// Special treatment for the doctorInfo form:
// 		$('#otherServiceProviderCity-rowItem-'+number).val(-1);
// 		$('#otherServiceProvider-rowItem-'+number).val('');
	}
	
}

function removeRow(je) {
	je.parent().parent().parent().remove();
}

