function rInit() {

  var stateValue='';

  if (document.rform.resultState)
     stateValue = document.rform.resultState.value;

  if (stateValue == 'closeState') {
    rClose();                  
  }
  else
  if (stateValue == 'spawnPopupState') {
    rSpawnPopup();                  
  } 
  else
  if (stateValue == 'confirmState') {
    rRefreshTreeAndHeader();                  
  } 
  else
  if (stateValue == 'refreshHeaderAndTreeState') {
    rRefreshTreeAndHeader();                  
  } 
  else
  if (stateValue == 'exitState') {
    // do nothing
  }   
}   

if ( window.onload != null ) {
  var old= window.onload;
  window.onload = function(e) {
    old(e);
    rInit();
  }
}
else {
  window.onload = rInit;
}

function rClose() {
  var rand = Math.floor(Math.random()*(10001))
  opener.location.href="Rule.uix?rand="+rand;
  window.close();
}

function rCancel() {
  submitForm('rform',1,{'event':'cleanup'});
}

function rSpawnPopup() {
  var url = document.rform.PopupURL.value;
  var height = document.rform.PopupHeight.value;
  var width = document.rform.PopupWidth.value;
  var windowName = document.rform.PopupWindowName.value;
  window.open(url, windowName, 'resizable,status,scrollbars,width=' + width +"," + 
              "height=" + height).focus(); 
}

function rRefreshTreeAndHeader() {          
  if (parent.header) {
    parent.header.location.href=getBaseURL(parent.header.location.href);
  }

 if (parent.rulesetTree) {
    parent.rulesetTree.location.href=getBaseURL(parent.rulesetTree.location.href);
  }
  else
    if (parent.tree) {
      parent.tree.location.href=getBaseURL(parent.tree.location.href);
    }
    else
      if (parent.customTree) {
        parent.customTree.location.href=getBaseURL(parent.customTree.location.href); 
      } 
}

function getBaseURL(url) {
   var index = url.indexOf('?');
   if (index > 0) {
     return url.substring(0, index);  
   }
   else
     return url;
} 
              

function interceptEnterKey(e, uixEventName){
  if (document.all) {
    if(window.event.keyCode==13) {
      if (uixEventName != '')
        mySubmit('rform', uixEventName);
      // do not display key pressed<
      return false;
    }
  }
  else { 
    if (e) 
      if (e.which == 13){
        if (uixEventName != '')
          mySubmit('rform', uixEventName);
        // do not display key pressed
        return false; 
      }
  }
  // display key pressed
  return true;
}


function mySubmit(formName, eventName) {
  submitForm(formName,1,{'event':eventName});
}