/**
 * For foreignKeyControl
 */
function toggleForeign(divbase,expand,select,edit){
  var expanded = document.getElementById('expan'+divbase);
  var key = document.getElementById('drop'+divbase);
  var expand = document.getElementById(expand);
  var abutton = document.getElementById('a'+divbase);
  if (expand.value=='Y' ){
    expand.value='N';
    key.style.display="inline";
    expanded.style.display="none";
    abutton.innerHTML = edit;
  } else {
    expand.value='Y';
    key.style.display="none";
    expanded.style.display="inline";
    abutton.innerHTML = select;
  }
}

function doFKAction(action,name, table, noitem, value) {
  // TEMPORARY FIX the multiple arguments in the action string  
  actiononly= action.split('&');
  actiononly = actiononly[0];
  
  var element = document.forms[formname][name];
  var idtag = "";
  if (actiononly != "new") {
    var newid = element.options[element.selectedIndex].value;
    if (newid == "" || newid == "0") {
      alert(noitem);
      return false;
    }
    idtag = "&id=" + newid;
  }
  var or_id = "";
  if (actiononly == "delete") {
    or_id = "&or_id=" + value;
  }  
  var actionid = "";
  if (or_id== "" && value > 0) {
    actionid = "&actionid=" + value;
  }
  newwin = window.open("?action="+action+actionid+idtag+"&table="+table+"&fknewid="+name+or_id, actiononly+formname+"_"+name, "resizable,width=800,height=600,scrollbars");
  newwin.focus();
  return false;
}

/**
 * For updating FK Control
 *
 * utype == 1 => add
 * utype == 2 => change
 * utype == 3 => delete
 */ 
function updateFKControl(o_id, no_id, no_val, txt, utype){
  var message = "";
  dropdown = document.getElementById(o_id);  
  if (dropdown != null && dropdown.options) {  
    switch (utype) {
      case 2: // change
        found = false;
        for (i=0; i < dropdown.options.length; i++){
          if (dropdown.options[i].value == no_id){
            dropdown.options[i].text = no_val;
            found = true;
          }
        }
        if (found) {
          message = '<br />' + txt;
          break;
        }
      case 1: // add
        newid = dropdown.options.length;
        dropdown.options[newid] = new Option(no_val, no_id);    
        if (dropdown.options[0].selected){
          dropdown.selectedIndex = newid;
        }
        message = '<br />' + txt;
        break;
      case 3: // delete
        for (i=0; i < dropdown.options.length; i++){
          if (dropdown.options[i].value == no_id){
            dropdown.selectedIndex = no_val;
            dropdown.options[i] = null;
            message = '<br />' + txt;
          }
        }
        break;
      default:
        break;
    }
  }
  // display message
  aspan = document.getElementById(o_id + '_sp');
  if (aspan != null) {
    aspan.innerHTML = message;
  }
}

