/********************************************************************
*-------------------------------------------------------------------
* Licensed Materials - Property of IBM
*
* 5724-A18
*
* (c) Copyright IBM Corp. 2000
*
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*
*-------------------------------------------------------------------*/


/****************************************************************************
* Open a new window.
*
* url - The URL of the help file.
* windowName - The title of the window.
* offset     - The offset in pixels from the parent window
* features   - The various attributes of the window to be created.
*
* Returns the opened window
****************************************************************************/
function openWindow(url, windowName, offset, features) 
{
//  if (features == null)
    features = "resizable=yes,scrollbars=yes,menubar=yes, copyhistory=no"; 
  
  if (offset == null)
    offset = 0;

  offset += 20;

  if(navigator.appName == "Netscape")
  {
    if(parseInt(navigator.appVersion) > 3)
    {
      // set width and height
      if (features.indexOf(",width=") < 0)
        features += ",width=" + top.innerWidth + ",height=" + top.innerHeight; 
  
      // set window position
      features += ",screenX=" + (top.screenX+offset) + ",screenY=" + (top.screenY+offset);
    }
    else
    {
      // set width and height
      if (features.indexOf(",width=") < 0)
        features += ",width=" + top.width + ",height=" + top.height; 
  
      // set window position not available in version 3 and less
    }
  }// END if
  
  else
  {
    // set width and height
    if (features.indexOf(",width=") < 0)
    {
      var width=",width=", height=",height=";
  
      if (screen.width < 750)
        width += screen.availWidth;
      else
        width += "750";
      
      if (screen.height < 500)
        height += screen.availHeight;
      else
        height += "500";
      features += width + height; 
    }
    
    // No way to query current window position in IE...
    features += ",left=" + (screen.availLeft+offset) + ",top=" + (screen.availTop+offset);

  }// END else

  var target = window.open( "", windowName, features); 

  target.location.href = url;
  // focus function is not working under IE4 with Service pack 2
  // it will crash, we need to detect the specific IE version 'MSIE 4.01' - d48999
  // we only use focus() function for all browser except IE 4.01
  var navVersion = navigator.appVersion
  if ( navVersion.indexOf("MSIE 4.01") == -1 )
  {  
    target.focus();
  }

  return target;

}// END openWindow

/****************************************************************************
* Determines whether an object has been assigned a value.
*
* Returns TRUE if it has been assigned a value; FALSE otherwise
****************************************************************************/
function defined(obj)
{
  var str = typeof obj;
  
  if((str == "undefined") || (obj == null))
    return false;
  else
    return true;
}// END defined

/****************************************************************************
* Returns the type of object.
****************************************************************************/
function objectType(obj)
{
 // 
 // alert( "The function Util::objectType() is deprecated!"
 //      + "It fails in IE and thus must not be used.\n\n"
 //      + "If you see this message open a defect immediately.");

  var constructor = (String)(obj.constructor);
  var tokens = constructor.split(" ");

  for(var i=0; i<tokens.length; i++)
  {
    if(tokens[i].indexOf("function") != -1)
    {
      var end = tokens[i+1].indexOf("(");

      if(end == -1)
        return(tokens[i+1]);
      else
        return(tokens[i+1].substring(0, end));
    }
  }

}// END objectType

/****************************************************************************
* Trim the leading and trailing blank of the input string 'word'
*
* Returns a string
****************************************************************************/
function trim(inword)
{
   word = inword.toString();
   var i=0;
   var j=word.length-1;
   while(word.charAt(i) == " ") i++;
   while(word.charAt(j) == " ") j--;
   if (i > j) {
		return word.substring(i,i);
	} else {
		return word.substring(i,j+1);
	}
}

/****************************************************************************
* Check if the input string 'id' is empty
*
* Returns true or false
****************************************************************************/
function isInputStringEmpty(id)
{
  return !id.match(/[^\s]/);
}


/****************************************************************************
* This will alert the user with the error/warning message 'message' and
* highlight the text field 'field'.
*
* 
****************************************************************************/
function reprompt(field, message) {
    window.status = "";
    alert(message);
    field.focus();
    field.select();
    
}

/****************************************************************************
* This will remove the Return \r \n chars from a string and
* replace them by " " char.
*
* 
****************************************************************************/
function removeReturnChar(word)
{
   var i=0;
   var j=word.length;
   var result="";

   for (i=0; i<j; ++i) {
      if((word.charAt(i) == "\r") || (word.charAt(i) == "\n" )) {
         result += " ";
      } else {
         result += word.charAt(i);
      }
   }

   return result;
}


/**************************************************************************
* This will return the index position that a char in a given string       
* return "false" if couldn't found, otherwise it returns the first invalid
* character.
*
***************************************************************************/
function find(givenString, searchChars)
{
  for ( var i=0; i< searchChars.length; i++)
  {
   var achar = searchChars.substring(i, i+1);
   
   if ( givenString.indexOf(achar) != -1 )
     return achar;
  }
   return "false";
}


/*********************************************************************
* Return the number of occurrence of a given char
**********************************************************************/
function numOfOccur(givenString, searchChars)
{
  var count = 0;

  for (var i=0; i< givenString.length; i++)
  {
     if ( givenString.charAt(i) == searchChars.charAt(0) ) 
       count ++;
  }

  return count;
}


function convertFromTextToHTML(obj)
{
   var string = new String(obj);
   var result = "";

   for (var i=0; i < string.length; i++ ) {
      if (string.charAt(i) == "<")       result += "&lt;";
      else if (string.charAt(i) == ">")  result += "&gt;";
      else result += string.charAt(i);
   }
   return result;
}

function convertFromHTMLToText(obj)
{
   /* add some padding to the string so we don't 
      require bounds checking */
   var string = new String(obj + "_____");
   var result = "";

   for (var i=0; i < string.length; ) {
      if (string.charAt(i) == "&") {
         if (string.substring(i, i+4) == "&lt;") {
            result += "<";
            i += 4;
         }
         else if (string.substring(i, i+4) == "&gt;") {
            result += ">";
            i += 4;
         }
         else {
            result += string.charAt(i);
            i++;
         }
      }
      else {
         result += string.charAt(i);
         i++;
      }
   }
   return result.substring(0, result.length-5);
}


function isEmpty(id) {
   return !id.match(/[^\s]/);
}


//////////////////////////////////////////////////////////
// This function will check whether or not the time has 
// a valid format. hh:mm 
//
// by Tony Mao
// Input: time
// Return code = "true", time has a right format
// Return code = "false", time format is wrong
//
//////////////////////////////////////////////////////////
function validTime(time1) {
   var delimiter = ":"; 
   var hh, mm;
   var time1Length;
   var hhlength;
   var mmlength;

   time1Length = time1.length;

   if (time1 == "" || time1.indexOf(delimiter) == -1 ||  time1Length > 5 ) return false;
   
   hh = time1.substring(0,time1.indexOf(delimiter));
   mm = time1.substring(time1.indexOf(delimiter) + 1);

   hhlength = hh.length;
   mmlength = mm.length;

   if (hhlength <1 || hhlength >2 || mmlength <1 || mmlength >2) return false;
   if (hh=="" || mm == "" ) return false;
   if (isNaN(hh) || isNaN(mm) ) return false;

   if ( parseInt(hh) > 23 || parseInt(hh) < 0 ) return false;
   if ( parseInt(mm) > 59 || parseInt(mm) < 0 ) return false;
  
   return true;
}


//////////////////////////////////////////////////////////
// This function will limit a text area input box to a
// specified size.  just use the onKeyUp and onKeyDown events
// with the text area box.
// call with ::
//    arg1=this.form.<myTextAreaName>
//    arg2=<maximumSizeForTextArea>
// by Glen Shortliffe
//////////////////////////////////////////////////////////
function limitTextArea(field, maxlimit) {
    if (field.value.length > maxlimit) { 
       //  field is too long... chop it down
       field.value = field.value.substring(0, maxlimit);
    }
}


//////////////////////////////////////////////////////////
// This function will validate a typical name field 
// and restrict it to pretty much alphanumerics.
// All special characters are invalid with some exceptions
// " ", "-", "_", and "." are all valid
// The string can also not start with a leading number
// The string can also not be empty
// call with ::
//    arg1=<myInputString>
//
// return true is name is valid, false otherwise
// by Glen Shortliffe
//////////////////////////////////////////////////////////
function isValidName(myString) {
    var invalidChars = "~!@#$%^&*()+=[]{};:,<>?/|`"; // invalid chars
    invalidChars += "\t\'\"\\"; // escape sequences
    var invalidStartChars = "0123456789";
    
    // if the string is empty it is not a valid name
    if (isEmpty(myString)) return false;
 
    // look for presence of invalid characters.  if one is
    // found return false.  otherwise return true
    for (var i=0; i<myString.length; i++) {
      if (invalidChars.indexOf(myString.substring(i, i+1)) >= 0) {
        return false;
      }
    }
    
    // look for a leading number... and disallow it.
    var startChar = myString.substring(0,1);
    for (var i=0; i<invalidStartChars.length; i++) {
      if (invalidStartChars.indexOf(startChar) >= 0) {
        return false;
      }
    }
    
    return true;
}

//////////////////////////////////////////////////////////
// This function will dump out the content of a javascript object
// This is useful for debugging purposes.
// In your code you can do this:
// alert(dumpObject(myBigFatObject));
// 
// This will dump out the objects contents 
// arg1 = the object you want info on...
// return a string which you can send to alert()
// by Glen Shortliffe
//////////////////////////////////////////////////////////
function dumpObject(what) {
   var output = '';
   for (i in what) {
       if (typeof what[i] == 'object') {
           output += dumpObject(what[i]);
       }
       else
           output += i + ' = ' + what[i] + '\n';
   }
   return output;
}

// function to clone a javascript object
function cloneObject(what) {
    for (i in what) {
       if (typeof i == 'object') {
          this[i] = new cloneObject(what[i]);
       }
       else {
          this[i] = what[i];
       }
    }
}


  /****************************************************************************
  ****************************************************************************/
//////////////////////////////////////////////////////////
// This function will validate a positive integer quantity
//
// arg1 = the object you want info on...
// Return true is this input arg is a valid positive int, 
// otherwise return false...
//
// by Glen Shortliffe
//////////////////////////////////////////////////////////
function isValidPositiveInteger(myString) {
    var validChars = "0123456789";
    
    // if the string is empty it is not a valid integer
    if (isEmpty(myString)) return false;
    
    // look for non numeric characters in the input string
    for (var i=0; i<myString.length; i++) {
      if (validChars.indexOf(myString.substring(i, i+1)) == "-1") {
        return false;
      }
    }
    // look for bad leading zeroes in the input string
    if (myString.length > 1 && myString.substring(0,1) == "0") {
       return false;
    }
    
    return true;
}





