// Copyright (c) 2002 Life Cycle Solutions, Inc.   All Rights Reserved
///////////////////////////////////////////////////////////////////
// Utility Methods
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
function ltrim ( s ) {
   if(isNumber(s)){
       return s;
   }
   return s.replace( /^\s*/, "" )
}
///////////////////////////////////////////////////////////////////
function rtrim ( s ) {
   if(isNumber(s)){
       return s;
   }
   return s.replace( /\s*$/, "" );
}
///////////////////////////////////////////////////////////////////
function trim ( s ) {
   s = rtrim(ltrim(s));
   if(s == ' ' || s == '  '){
       s = '';
   }
   return s;
}
///////////////////////////////////////////////////////////////////
function handleCheckBox(box, formval) {
    if(box.checked){
        formval.value = 'true';
    } else {
        formval.value = 'false';
    }
}
///////////////////////////////////////////////////////////////////
function setSelectedValueOfListFromDisplay(select, display){
   var text;
   for(var i = 0; i < select.options.length; i++){
      text = select.options[i].text;
      if(display == text){
         select.selectedIndex = i;
         return true;
      }
   }
   return false;
}
///////////////////////////////////////////////////////////////////
function getSelectedValue(select){
   return select.options[select.selectedIndex].value;
   //return select.options[select.selectedIndex].text;
}

///////////////////////////////////////////////////////////////////
function getSelectedDisplay(select){
   //return select.options[select.selectedIndex].value;
   //jsDebug("display: " + select.options[select.selectedIndex].text);
   return select.options[select.selectedIndex].text;
}
///////////////////////////////////////////////////////////////////
function getValueFromDisplay(select, display){
   //return select.options[select.selectedIndex].value;
   //jsDebug("display: " + select.options[select.selectedIndex].text);
   for(var i = 0; i < select.options.length; i++){
       if(select.options[i].text == display){
           return select.options[i].value;
       }
   }
   return 'NOT FOUND';
}
///////////////////////////////////////////////////////////////////
function getDisplayFromValue(select, value){
   //return select.options[select.selectedIndex].value;
   //jsDebug("display: " + select.options[select.selectedIndex].text);
   for(var i = 0; i < select.options.length; i++){
       if(select.options[i].value == value){
           return select.options[i].text;
       }
   }
   return 'NOT FOUND';
}
///////////////////////////////////////////////////////////////////
function format(val){
   if(!val){
      return '';
   }
   if('null' == val){
       val = '';
   }
   if('undefined' == val){
       val = '';
   }
   if(isNaN(val)){
       return trim(val);
   }
   else{
      return val;
   }
}
///////////////////////////////////////////////////////////////////
function formatFloat(val){
   if(!hasContent(val)){
       return 0.0;
   }
   return parseFloat(val);
}
///////////////////////////////////////////////////////////////////
function formatInt(val){
   if(!hasContent(val)){
       return 0;
   }
   return parseInt(val);
}
///////////////////////////////////////////////////////////////////
function checkEmptyInput(input){
    if(input && input.value.length == 0){
        input.value = " ";
    }
}
///////////////////////////////////////////////////////////////////
function hasContent(val){
    if(!val || trim(val) == '' || val == 'null' || val == 'undefined'){
        return false;
    }
    return true;
}
///////////////////////////////////////////////////////////////////
function RGBIntToHex(integer) {
  hexValues = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
  hexDigit1 = Math.floor(integer / 16);
  hexDigit2 = (integer % 16);
  return hexValues[hexDigit1] + hexValues[hexDigit2];
}
///////////////////////////////////////////////////////////////////
function parseReturns(s){
    if(isNumber(s)){
        return s;
    }
    if(!s)
        return "";

    if(s.indexOf('\n') < 0){
        return s;
    } else {
        return s.substring(0, s.indexOf('\n')) + "<br>" + parseReturns(s.substring(s.indexOf('\n') + 1, s.length));
    }
}
///////////////////////////////////////////////////////////////////

