// ID of HTML control that contains the name of the field for which a value is being selected
var POPUP_LIST_FIELD = "popuplistfield";
// suffix for the name of the HTML control for which a value has been selected
var DISPLAY_SUFFIX = "Display";
// the ID of the table containing the list
var LIST_TABLE_ID = "listtable";
var ROW_PREFIX = "row";

// set the name of the text field for which a value is to be selected from a popup list
function setPopupListTextField(textfieldName) {
   document.getElementById(POPUP_LIST_FIELD).value = textfieldName;   
}

// function to ensure that at only one checkbox has been selected
function checkNumberofSelectedItems() {
	
	if (opener.document.getElementById('listitemflag').value == "true") {
		checkSelection();		
		executeOpenerScriptAddToTable();	
	} else {	
   		if (checkSelection()) {
      		if (count == 1) {
	     		executeOpenerScript();
	  		} else {
	     		alert("Only One item can be selected");
	  		}
		}
	} 
	// reset the listitemflag to 'false'
	opener.document.getElementById('listitemflag').value = "false";
}

// function to ensure that at least one checkbox has been selected
function checkSelection() {
   count = 0;
   for (var i=0; i < document.forms[0].elements.length; i++) {
      if (document.forms[0].elements[i].checked) {
	     count++;
	  }
   }
   if (count == 0) {
      alert("Select an item");
	  return false;
   }
   return true;
}

function executeOpenerScript() {
	var button = opener.parent.document.getElementById('btn').click();
}

// Function to add a checked row to an HTML field
function addSelectedObjectToTextField() {
   // The name of the field to which the value is to be added. This value is set
   // before the popup window is opened.		
   var fieldName = document.getElementById(POPUP_LIST_FIELD).value;
   // the field to which the checked value is to be added
   var field = document.getElementById(fieldName);
   // loop through all the controls in the popup window and find the one that is checked
   for (i = 0; i < listwindow.document.forms[0].elements.length; i++) {
      if (listwindow.document.forms[0].elements[i].checked) {
	  	 // set the value of the hidden field which is the value of the checked element
		 document.getElementById(fieldName).value = listwindow.document.forms[0].elements[i].value;
		 // set the value to be displayed, the value of the second column of row containing the checked item
		 setDisplayField(fieldName, listwindow.document.forms[0].elements[i].value);
	  }
   }
   listwindow.close();
}

// set the value of the field to be displayed on the screen
function setDisplayField(field, ID) {
	// the table containing the results
	var theTable = listwindow.document.getElementById(LIST_TABLE_ID);
	var theRow = theTable.rows[ROW_PREFIX + ID];
	
	if (document.getElementById(field + DISPLAY_SUFFIX) != null) {
	   document.getElementById(field + DISPLAY_SUFFIX).value = theRow.cells[1].innerHTML;
	} 
	if (document.getElementById(field + DISPLAY_SUFFIX + "1") != null) {
	   document.getElementById(field + DISPLAY_SUFFIX + "1").value = theRow.cells[2].innerHTML;
	} 
	if (document.getElementById(field + DISPLAY_SUFFIX + "2") != null) {
	   document.getElementById(field + DISPLAY_SUFFIX + "2").value = theRow.cells[3].innerHTML;
	} 
	if (document.getElementById(field + DISPLAY_SUFFIX + "3") != null) {
	   document.getElementById(field + DISPLAY_SUFFIX + "3").value = theRow.cells[4].innerHTML;
	} 
	if (document.getElementById(field + DISPLAY_SUFFIX + "4") != null) {
	   document.getElementById(field + DISPLAY_SUFFIX + "4").value = theRow.cells[5].innerHTML;
	} 
	if (document.getElementById(field + DISPLAY_SUFFIX + "5") != null) {
	   document.getElementById(field + DISPLAY_SUFFIX + "5").value = theRow.cells[6].innerHTML;
	} 
	if (document.getElementById(field + DISPLAY_SUFFIX + "6") != null) {
	   document.getElementById(field + DISPLAY_SUFFIX + "6").value = theRow.cells[7].innerHTML;
	} 
	if (document.getElementById(field + DISPLAY_SUFFIX + "7") != null) {
	   document.getElementById(field + DISPLAY_SUFFIX + "7").value = theRow.cells[8].innerHTML;
	} 
}
