//
// Created on March 10, 2004
// 
// Author: Sirbu V. Dorin
// E-mail: dorin.sirbu@ist.ro
//
// © INDUSTRIAL SOFTWARE TECHNOLOGIES 2004. All rights reserved. 
//

// Retuns the current user browser language. 
function utilGetUserLanguage()
{
	var lang; 
	if (navigator.appName == 'Netscape')
		lang = navigator.language;
	else
		lang = navigator.browserLanguage;
	return lang.substring(0, 2);	
}

function utilRandomValue(low, high) 
{
    return Math.floor(Math.random() * (1 + high - low) + low);
}
  
function utilRandomValue1(high) 
{
	return 1 + Math.floor(Math.random() * high);
}

function trim(inputString) 
{
	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces it with one space. If something besides
	// a string is passed in (null, custom object, etc.) then return the input.
	if (typeof inputString != "string") 
	{ 
		return inputString; 
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") 
	{ // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") 
	{ // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) 
	{ // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function utilGetFieldValue(field)
{
   switch(field.type)
   {
      case "text" :
      case "textarea" :
      case "password" :
      case "hidden" :
         return field.value;

      case "select-one" :
         var i = field.selectedIndex;
         if (i == -1)   return "";
         // else   return (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
         else   return field.options[i].value;

      case "select-multiple" :
         var allChecked = new Array();
         for(i = 0; i < field.options.length; i++)
            if(field.options[i].selected)
               allChecked[allChecked.length] = (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
         return allChecked;

      case "button" :
      case "reset" :
      case "submit" :
         return "";

      case "radio" :
      case "checkbox" :
         if (field.checked) { return field.value; } else { return ""; }
      default :
         if(field[0].type == "radio")
         {
            for (i = 0; i < field.length; i++)
               if (field[i].checked)
                  return field[i].value;

            return "";
         }
         else if(field[0].type == "checkbox")
         {
            var allChecked = new Array();
            for(i = 0; i < field.length; i++)
               if(field[i].checked)
                  allChecked[allChecked.length] = field[i].value;

            return allChecked;
         }
         else
            var str = "";
            for (x in field) { str += x + "\n"; }
            alert("I couldn't figure out what type this field is...\n\n" + field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
         break;
   }
   return "";
}

function utilSetDroplist(drop, value)
{
	for(i = 0; i < drop.options.length; i++)
	{
		if(drop.options[i].value == value)
		{
			drop.selectedIndex = i;
			break;
		}
	}
} 