// Follows the naming scheme <Element.Name>_IdCounter
var EventTypes_IdCounter = 0;
var EventLocations_IdCounter = 0;

function buildSaveList(inElement, SaveElementName, outputId){

	var tmpIndex = inElement.selectedIndex;
	var tmpValue = inElement.value;
	var tmpText  = inElement.options[tmpIndex].text;
	var tmpForm  = inElement.form;
	var tmpName  = inElement.name;

	// Hidden Input Values
	IdCounter = eval(tmpName + "_IdCounter");
	var tmpSaveId = SaveElementName + '_' + IdCounter;
	eval(tmpName + "_IdCounter = " + tmpName + "_IdCounter + 1");
	
	var newInput = document.createElement('input');
	newInput.setAttribute('type' , 'hidden'); // change hidden to text to view output
	newInput.setAttribute('name' , SaveElementName);
	newInput.setAttribute('id'   , tmpSaveId);
	//newInput.setAttribute('value', tmpValue + ":" + tmpText);
	newInput.setAttribute('value', tmpValue);
	tmpForm.appendChild(newInput);
	
	outputRef = document.getElementById(outputId);
	
	p1 = "'" + tmpName.replace("'", "&rsquo;") 	  + "'";
	p2 = "'" + tmpText.replace("'", "&rsquo;")    + "'";
	p3 = "'" + tmpValue.replace("'", "&rsquo;")   + "'";
	p4 = "'" + tmpSaveId.replace("'", "&rsquo;")   + "'";
	
	newRemoveLink = "<a style=\"cursor:pointer;color:Red;\" onclick=\"removeSaveListItem(" + p1 + "," + p2 + "," + p3 + "," + p4 + ", this);updateEventLocationContainer('EventLocations','EventLocations', 'EventTypesSaved','StartDate', 'EndDate');\">X</a>";
	newDiv = "<div><div style=\"width:10px; float:left;\">" + newRemoveLink + "</div><div style=\"width:143px; float:right;\">" + tmpText + "</div><div style=\"clear:both;\"></div></div>";
	
	if(outputRef.innerHTML == ""){
		outputRef.innerHTML = newDiv;
	}else{
		outputRef.innerHTML = outputRef.innerHTML + newDiv;
	}

	inElement.blur();
	inElement.remove(tmpIndex);
	
}

function buildHiddenField(inElement, HiddenFieldName){
	var tmpIndex = inElement.selectedIndex;
	var tmpValue = inElement.value;
	var tmpText  = inElement.options[tmpIndex].text;
	var tmpForm  = inElement.form;
	var tmpName  = inElement.name;
	
	var mObj = document.getElementById(HiddenFieldName);
	var currentList = mObj.value;
	//alert('tmpvalue=' + tmpValue + ' and HiddenFieldName=' + HiddenFieldName);
	//alert('tmpvalue=' + tmpValue + ' and currentList=' + currentList);
	
	currentList = currentList + "," + tmpValue;
	mObj.value = currentList;
	
}

function removeByReference(ElementRef) {
	ElementRef.parentNode.removeChild(ElementRef);
}

function removeElementById(ElementId) {
	var d = document.getElementById(ElementId);
	removeByReference(d);
}


// Remove save list item and insert back into options
function removeSaveListItem(ElementName, ElementText, ElementValue, ElementId, AnchorRef){

	optionElement = document.getElementById(ElementName);
	
	// Insert back into options
	optionNew = new Option(ElementText, ElementValue);
	try{
		optionElement.add(optionNew, null); // standards compliant
	}catch(ex){
		optionElement.add(optionNew); // IE only
	}
	
	// Remove Anchor(A Tag) Link and Text within DIV
	removeByReference(AnchorRef.parentNode.parentNode);

	// Remove Input
	removeElementById(ElementId);
}

function clearSaveList(ElementID){
	//alert(ElementID);
	var mElement = document.getElementById(ElementID);
	mElement.innerHTML = "";
}