//******************** FUNCTION: APPEND SELECTED COMMODITY ******************** 
function appendSelectedCommodity() 
{
  if (document.form1.listCommodity.length != 0){
    var commodityList = document.form1.listCommodity;
    var selectedCommodities = document.form1.selectCommodity;
    var elOptNew = document.createElement('option');
    elOptNew.text = commodityList.options[commodityList.selectedIndex].text;
    elOptNew.value = commodityList.options[commodityList.selectedIndex].value;
    var elSel = document.getElementById('selectCommodity');

    try {
      elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
      removeCommodityList();
    }
    catch(ex) {
      elSel.add(elOptNew); // IE only
      removeCommodityList();
    }
  }
}
//*********************** FUNCTION: APPEND COMMODITY LIST *********************** 
function appendCommodityList() 
{
  if (document.form1.selectCommodity.length != 0){
    var commodityList = document.form1.listCommodity;
    var selectedCommodities = document.form1.selectCommodity;
    var elOptNew = document.createElement('option');
    elOptNew.text = selectedCommodities.options[selectedCommodities.selectedIndex].text;
    elOptNew.value = selectedCommodities.options[selectedCommodities.selectedIndex].value;
    var elSel = document.getElementById('listCommodity');

    try {
      elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
      removeSelectedCommodity();
    }
    catch(ex) {
      elSel.add(elOptNew); // IE only
      removeSelectedCommodity();
    }
  }
}
//******************* FUNCTION: REMOVE SELECTED COMMODITY LIST ******************** 
function removeSelectedCommodity() 
{
  var elSel = document.getElementById('selectCommodity');
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}
//************************ FUNCTION: REMOVE COMMODITY LIST ************************
function removeCommodityList() 
{
  var elSel = document.getElementById('listCommodity');
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}
//******************************* FUNCTION: SEARCH ********************************* 
function search(comcode)
{
  var elSel = document.getElementById('listCommodity');
  var i;
  var found = 0;
  for (i = elSel.length - 1; i>=0; i--) 
  {
    if (elSel.options[i].value == comcode)   
    {
      elSel.options[i].selected = true;
      found = 1;
    }
  }
  if (found == 0){
    alert("Commodity code " + comcode + " not found");
  }
  if (found != 0){
   appendSelectedCommodity();
  }
}
//******************************* FUNCTION: SPLIT *********************************
function Split(comcodes){
   comcodeString = comcodes.toString()
   var elSel = document.getElementById('selectCommodity');
   var splitResult = comcodeString.split(",");
   for(i = 0; i < splitResult.length; i++){
      //alert("Element " + i + " = " + splitResult[i]); 
      search(trimAll(splitResult[i].toUpperCase()))      
   }   
   if (elSel.length < 4){
          selectAll();
   }   
   if (elSel.length > 3){
      if (maxcnt() == true){
        selectAll(); 
      } 
   }
   //validate()
//selectAll();  
}
//***************************** FUNCTION: LIST SELECT ALL *************************
function selectAll(){
  var elSel = document.getElementById('selectCommodity');
  var i;
  var found = 0;
  for (i = elSel.length - 1; i>=0; i--) {
      elSel.options[i].selected = true;
  }
}
//********************************* FUNCTION: TRIM ALL *********************************
function trimAll(sString){
  while (sString.substring(0,1) == ' ')
  {
     sString = sString.substring(1, sString.length);
  }
  while (sString.substring(sString.length-1, sString.length) == ' ')
  {
     sString = sString.substring(0,sString.length-1);
  }
  return sString;
}
    
//********************************* FUNCTION: VALIDATE *********************************  
function validate() {
	var Com = form1.selectCommodity.value        
    form1.selectCommodity.value = Com;
    if (Com == "") {
      alert("Please select a Commodity!");
      form1.txtcomcode.focus();
      return false; 
    }
    
}
//********************************* FUNCTION: MAXCNT(MESSAGE) ********************************* 
function maxcnt() {
    var answer;   
    answer = confirm("You have selected more than 3 commodities");
  return answer;
} 

