	var count = null;
	var minCount = null;
	var allow  = true;
	var keepCount = 0;
	itemArray = new Array();
	errorMsgArray = new Array();
	var itemCount = 0;
	
	function initLoad(countAmount,countMinimum)
	{
		// Please note, for unlimited amount of selections, please make count Amount = -1
		count = countAmount;
		minCount = countMinimum;
	}
	
	function addItem(itemID,itemErrorMessage)
	{
		itemArray[itemCount] = itemID;
		errorMsgArray[itemCount] = itemErrorMessage;
		itemCount++;
	}
	
	function checkCount()
	{
		if(count == 0)
		{
			alert("You've reached the maximum amount you may select");
			allow = false;
		}else
		{
			allow = true;
		}
	}
	
	function moveAccross(id,tablename,formname) 
	{
			var selectedrow = document.getElementById(id);
			addRowToTable(selectedrow.id,selectedrow.innerHTML,tablename,formname);
	}
	
	
	function checkForID(id,table)
	{
		if((id == null) || (id == '0')) 
			return true;
		else
		{
			var selectedid = document.getElementById("cur"+id);
			if(selectedid == null) 
				return false;
			else			
				return true;
		}
	}
	
	function removeItem(itemId)
	{
	   row = document.getElementById(itemId);
	   parent = row.parentNode;
	   parent.removeChild(row);
	   if(parent.rows.length==1)
	   {
		   grandparent = parent.parentNode;
		   grandparent.removeChild(parent);
	   }
	} 
		
	function removeRowWithID( theRowID,tableid,formid )
	{
		// remove table row
		var rowToDelete = document.getElementById(theRowID);
		var parent = rowToDelete.parentNode;	
		parent.removeChild(rowToDelete);
		// remove hidden field
		var rowToDelete = document.getElementById(theRowID);
		var parent = rowToDelete.parentNode;
		parent.removeChild(rowToDelete);
		count++;
		keepCount--;
	}
	
	function addRow(theTable)
	{
		return theTable.insertRow(theTable.rows.length);
	}
	
	function addCell(tr)
	{
		td = tr.insertCell(tr.cells.length);
		return td;
	}
	
	function createInput(name, value, type, id)
	{
	if(!type)
		{
			type = "text";
		}
		field = document.createElement('input');
		field.type=type;
		field.name = name + "[]";
		field.value = value;
		field.id = id;
		return field;
	}

	
	function addRowToTable(ids,innerHTMLs,tablename,formid)
	{
		// Add to table
		checkCount();
		if(checkForID(ids,tablename)) return;
		if((count!='')&&(allow==true))
		{
			count--;
			keepCount++;
			var table = document.getElementById( tablename );
			var newRow = addRow(table);
			var newCol = addCell(newRow);
			newCol.id = 'cur'+ids;
			newCol.innerHTML = innerHTMLs;
			newCol.onclick = function (evt) { removeRowWithID(this.id,tablename,formid); }
			// Add Hidden field
			var forms = document.getElementById(formid);
			var newHid = createInput('list', ids, 'hidden', 'cur'+ids);
			forms.appendChild( newHid );
		}
	}
	
	function requiredField(fieldId)
	{
		objitem = document.getElementById(fieldId);
		if((objitem.value == "")||(objitem.value==null))
		{
			objitem.focus();
			return false;
		}else
			return true;
	}
	
	function submitForm(formName)
	{
		for (x=0; x<itemCount; x++)
		{
			if(requiredField(itemArray[x]) == false) 
			   {
					alert(errorMsgArray[x]);
					return;
			   }
		} 
		if(minCount > keepCount) 
			alert("At least " + minCount + " item(s) need to be selected");
		else
		{
			var form = document.getElementById(formName);
			form.submit();
		}
	}
	
	function submitForm2(formName)
	{
		for (x=0; x<itemCount; x++)
		{
			if(requiredField(itemArray[x]) == false) 
			   {
					alert(errorMsgArray[x]);
					return;
			   }
		} 
		
		var form = document.getElementById(formName);
		form.submit();
	}

