// Includes the following functions:
//    clearDefault(field)
//    selectRadio(fieldText, fieldRadio)
//    hasOptions(srcList)
//    sortSelect(srcList)
//    copyToList(formName,a,b,move)
//    copyToListSubmit(formName,to)



function clearDefault(field) {
	if (field.defaultValue == field.value) {
		field.value = ""
	}
}

function selectRadio(fieldText, fieldRadio) {
   if (fieldText.value != "") {
      fieldRadio.checked = true;
   }
}

function hasOptions(srcList) 
//usage if (!hasOptions(srcList))
{
   if (srcList!=null && srcList.options!=null) 
   { 
      return true; 
   }
	return false;
}

function sortSelect(srcList) 
{
	var tmpList = new Array();
   if (!hasOptions(srcList)) 
   { 
      return; 
   }
	for (var i=0; i<srcList.options.length; i++) 
   {
		tmpList[tmpList.length] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected);
	}
	if (tmpList.length==0) 
   { 
      return;
   }
	tmpList = tmpList.sort( 
		function(a,b) 
      { 
			if ((a.text+"") < (b.text+"")) 
         { 
            return -1;
         }
			if ((a.text+"") > (b.text+"")) 
         { 
            return 1; 
         }
			return 0;
		} 
	);

	for (var i=0; i<tmpList.length; i++) 
   {
		srcList.options[i] = new Option(tmpList[i].text, tmpList[i].value, tmpList[i].defaultSelected, tmpList[i].selected);
	}
}

function copyToList(formName,a,b,move) 
{
  aList = eval('document.' + formName + '.' + a);
  bList = eval('document.' + formName + '.' + b);
  btnAtoB = eval('document.' + formName + '.btnAtoB');
  btnBtoA = eval('document.' + formName + '.btnBtoA');
  aMax = eval('document.' + formName + '.maxA');
  aMax = parseInt(aMax.value);
  bMax = eval('document.' + formName + '.maxB'); 
  bMax = parseInt(bMax.value);
  
  if (move == 'AtoB')
  {
     fromList = aList;
     toList = bList;
     toMax = bMax;
  }
  else if (move == 'BtoA')
  {
     fromList = bList;
     toList = aList;
     toMax = aMax;
  }

  if (toList.options.length > 0 && toList.options[0].value == 'temp') 
  {
    toList.options.length = 0;
  }
  var sel = false;
  for (i=0;i<fromList.options.length;i++) 
  {
    var current = fromList.options[i];
    if (current.selected) 
    {
      sel = true;
      if ((current.value == 'temp') || (toList.options.length >= toMax)) 
      {
        current.selected = false;
        
      } 
      else 
      {
         txt = current.text;
         val = current.value;
         toList.options[toList.length] = new Option(txt,val);
         fromList.options[i] = null;
         i--;
      }
    }
  }
  sortSelect(toList);
  
  if (aList.options.length >= aMax)
  {
     btnBtoA.disabled = true;
  }
  else
  {
     btnBtoA.disabled = false;
  }
  
  if (bList.options.length >= bMax)
  {
     btnAtoB.disabled = true;
  }
  else
  {
     btnAtoB.disabled = false;
  }
}

function copyToListSubmit(formName,to) 
{
  List = eval('document.' + formName + '.' + to);
  if (List.length && List.options[0].value == 'temp') 
  {
     return;
  }
  for (i=0; i < List.length; i++) 
  {
     List.options[i].selected = true;
  }
}

// The following two scrips work with getParam to populate Fields in a form 
// both scripts will look for var paramIdx array... an array of the indexes
// returned by getParams
// var paramIdx = ['a', 'b', 'c']

// inputParams populates form fields from the http post
// works with pre-existing text, hidden and select fields only 
// REQUIRES paramIdx (above) and var inputId array
// inputId is an array of form field ids to be populated, 
// order of field ids should correspond to parameters in paramIdx
// var inputIdx = ['a-input', 'b_input', 'c_input']
function inputParams() {
   var prm;
   var fld;
   var params = getParams();
   if(params) {      
       for (i in paramIdx) {
       // if parameter contains a value and corresponding input field exists
         if(params[paramIdx[i]] && document.getElementById(inputId[i])) {    
           var prm = params[paramIdx[i]];
           var fld = document.getElementById(inputId[i]);
           if (fld.type=='text'||'hidden') {   
              //decodePostStr uses decodeURIComponent to remove encoded characters from post
              fld.value = decodePostStr(prm); 
            } else if (fld.type=='select') {    
               for (k=0;k<fld.length;k++) {                  
                  if(prm == fld.options[k].value) {                   
                     fld.options[k].selected=true;
                     break;                  
                  }
               }
            }               
         }
      }      
   } 
}

// createHiddenFields creates hidden field in an existing form
// and populates values from http post
// The fields mimic the parameters passed in get Params
// REQUIRES paramId (above) an var frmId.  frmId is the id for the form
// in which the hidden fields will be added.
function makeFlds() {
   var params = getParams();
   if(params) {
   for(i in paramIdx) {
       if(params[paramIdx[i]]) {   
         var prm = params[paramIdx[i]];
         //decodePostStr uses decodeURIComponent to remove encoded characters from post
         var prm = decodePostStr(prm);   
         var input = document.createElement("input");
         input.setAttribute("type", "hidden");
         input.setAttribute("name", paramIdx[i]);
         input.setAttribute("id", paramIdx[i]);
         input.setAttribute("value",prm);
         document.getElementById(frmId).appendChild(input);
      }   
   }}
} 

// end
   

