// $Revision: 1.15 $:

var isIE = navigator.appName.toLowerCase().indexOf("microsoft internet explorer") != -1;
var isNS = navigator.appName.toLowerCase().indexOf("netscape") != -1;

function startsWith(source, target) {
   return source != null && source.indexOf(target) == 0;
}

function endsWith(source, target) {
   if(source == null || target == null)
   return false;
   var targetLength = target.length;
   var targetIndex = source.lastIndexOf(target);
   if(targetIndex == - 1)
   return false;
   return(targetIndex + targetLength) == source.length;
}

function trim(source) {
   while(source.substring(0, 1) == ' ') source = source.substring(1);
   while(source.substring(source.length - 1, source.length) == ' ') source = source.substring(0, source.length - 1);
   return source;
}

function getTokens(source, delimiter) {
   var tokens = new Array();
   if(delimiter == null) {
      tokens[tokens.length] = source;
      return tokens;
   }
   var index = - 1;
   while((index = source.indexOf(delimiter)) != - 1) {
      tokens[tokens.length] = source.slice(0, index);
      source = source.slice(index + 1);
   }
   if(source != null && trim(source) != "") tokens[tokens.length] = source;
   return tokens;
}

function getFlagName(flag) {
   if(flag == null)
   return null;
   var index = flag.indexOf("=");
   if(index == - 1)
   return null;
   return flag.slice(0, index);
}

function getFlagValue(flag) {
   if(flag == null)
   return null;
   var index = flag.indexOf("=");
   if(index == - 1)
   return null;
   return flag.slice(index + 1);
}

function setCookie(cookieName, cookieValue) {
   top.document.cookie = cookieName + "=" + cookieValue + ";";
}

function replaceLocation(newLocation) {
   location.replace(newLocation);
}

function getCookie(cookieName) {
   var cookies = getCookies();
   var tokens = getTokens(cookies, ";");
   if(tokens == null)
   return null;
   for(var i = 0; i < tokens.length; i++) {
      var cookie = tokens[i];
      cookie = trim(cookie);
      var currentCookieName = getFlagName(cookie);
      if(currentCookieName == cookieName) {
         return getFlagValue(cookie);
      }
   }
   return null;
}

function getCookies() {
   return top.document.cookie;
}

function getNextRandom() {
   return Math.floor((Math.random() * 100000));
}

function setSelectedOption(selectObj, value) {
   for(var i = 0; i < selectObj.length; i++) {
      if(selectObj.options[i].value == value) {
         selectObj.options[i].selected = "1";
         return;
      }
   }
}

function setCenterWindow(winProps) {

  var screenWidth = isIE ? document.body.offsetWidth : window.outerWidth;
  var screenHeight = isIE ? document.body.offsetHeight : window.outerHeight;
  var screenTop = isIE ? window.screenTop : window.screenY;
  var screenLeft = isIE ? window.screenLeft : window.screenX;
  var top = -1;
  var left = -1;
  var indx = 0;
  var params = winProps.split(",");

  for(indx = 0; indx < params.length; indx++) {

    heightIndx = params[indx].indexOf("height");
    widthIndx = params[indx].indexOf("width");
    if(heightIndx != -1) {
      equalIndx = params[indx].indexOf("=");
      top = (screenHeight - parseInt(params[indx].substring(equalIndx + 1))) / 2 + screenTop ;
    }
    if(widthIndx != -1) {
      equalIndx = params[indx].indexOf("=");
      left = (screenWidth - parseInt(params[indx].substring(equalIndx + 1))) / 2 + screenLeft;
    }
  }
  if(top != -1 && left != -1)
    winProps += ",top=" + top + ",left=" + left;
  return winProps;
}

function toggleElement(element, disable)  {
   try {
     element.disabled = disable;
   }
   catch(Exception) {
   }
   var children = element.childNodes;
   for(var i = 0; i < children.length; i++) {
      toggleElement(children[i], disable);
   }
} // function

function wait(waitTimeInMilliseconds) {
  var targetTime = (new Date()).getTime() + waitTimeInMilliseconds;
  while(true) {
    if((new Date()).getTime() >= targetTime) {
      break;
    }
  }
}   // function

// escape function for IBI
function IBIEscape(str)
{
  var retstr = "";

  if( str != null )
  {
    var chkstr = "";

    /* If contents of str has numeric only, str becomes numeric variable */
    /* The str.length becomes undefined means 0.  */
    str += "";
    for( var i=0; i<str.length; i++ )
    {
      chkstr = escape( str.charAt(i) );
      if( chkstr.length == 6 && chkstr.substring(0,2) == "%u" )
           retstr += escape( chkstr );   /* double escaped for tomcat */
      else retstr += chkstr;
    }
  }
  else retstr = str;

  return retstr;
}   // function

function cancelEvent(e) {
  e.returnValue = false;
  e.cancel = true;
  return false;
}
