//
// Form javascript functions
//
// $Id: form.js,v 1.1.1.1 2005/07/13 11:29:37 magnusr Exp $
//

//
// RADIO BUTTON
//

function getSelectedRadioValue(radio)
{
	for (var i=0; i<radio.length; i++)
	{
		if ( radio[i].checked )
		{
			return radio[i].value;
		}
	}
}

function formFieldExists(formName, fieldName)
{
	var f = document.adForm;
	var fields = f.elements;
	for (var i=0;i<fields.length; i++)
	{
		var field = fields[i];
		if ( field.name == fieldName )
			return true
	}
	return false;
}

//
//
// LISTBOX
//
//

// Returns the number of items in a listbox
// @param the listbox
function countAdded(select)
{
    return ( select.length );
}

// Returns true/false if an option exists in a listbox.
// @param select, the listbox to check
// @param option, the option to check for.
function optionAdded(select, option)
{
   if ( select.options.length == 0 )
      return false;

   var length = select.options.length;
   var optionValue = option.value;
   for (var i=0; i<length; i++)
   {
      var opt = select.options[i];
      if ( opt.value == optionValue )
         return true;
   }
   return false;
}

// Inserts an option into a listbox.
// @param select, the listbox to insert into.
// @param option, the option to insert.
function insertOption(select, option)
{
   //debugSelect(select);
   if ( select.options.length == 0 )
        select.options[0] = option;
   else
        select.options[select.options.length] = option;
}

function getLastOptionValue(select)
{
    var len = select.options.length;
    if ( len == 0 )
        return ( 0 );
    else
    {
        var opt = select.options[(len-1)];
        return ( opt.value );
    }
}

// Replaces the text on the option.
function replaceOption(select, option)
{
   //debugSelect(select);
   var length = select.options.length;
   var optionValue = option.value;
   for (var i=0; i<length; i++)
   {
      var opt = select.options[i];
      if ( opt.value == optionValue )
         opt.text = option.text;
   }
}

// Removes options from a listbox by matching the values
// in a listbox with values stored in an array
// @param toRemove, array containing values which should be removed.
// @param from, the listbox to remove values from
function removeOptions(toRemove, from)
{
   // Removing options is a bit weird...
   var j = 0;
   var i;
   var length;
   var option;
   var index;
   var optionValue;
   var valueToRemove;

   while ( j < toRemove.length )
   {
      valueToRemove = toRemove[j++];
      i = 0;
      length = from.options.length;
      // Loop through the entire select to find the sucker...
      while ( i < length )
      {
         option      = from.options[i++];
         index       = option.index;
         optionValue = option.value;
         // Check if the option matches the one to remove
         if ( optionValue == valueToRemove )
         {

            from.options[index] = null;
            break;
         }
      }

   }

}

function clearSelect(select)
{
    select.options.length = 0;
}

function addOptions(select, options)
{
     for (var i=0; i<options.length; i++)
     {
     	var option = options[i];
     	insertOption(select, option);
     }
}

// Returns the number of selected items in a listbox
// @param select, the listbox to count selected items for.
function countSelected(select)
{
   var count = 0;
   for (var i=0; i<select.options.length; i++)
   {
      if ( select.options[i].selected )
         count = count + 1;
   }
   return count;
}

// Returns the first selected Option in a list
// @param select the listbox
// @return Option
function getSelectedOption(select)
{
    //alert(select + " " + select.name);
    for ( var i=0; i<select.options.length; i++)
    {
        if ( select.options[i].selected )
            return ( select.options[i] );
    }
}

function getSelectedOptionValue(select)
{
    //alert(select + " " + select.name);
    for ( var i=0; i<select.options.length; i++)
    {
        if ( select.options[i].selected )
            return ( select.options[i].value );
    }

	return select.options[0].value;
}

function setSelectedOptionValue(select, value)
{
    for ( var i=0; i<select.options.length; i++)
    {
		var opt = select.options[i];
        if ( opt.value == value )
            select.options[i].selected = true;
		else
		    select.options[i].selected = false;

    }
}

function debugSelect(select)
{
    var msg = "Contents of select: '"+select.name+"'\n\n";
    for ( var i=0; i<select.options.length; i++)
    {
        var option = select.options[i];
        msg += "Id: " + option.value + " Text: " + option.text + "\n";
    }
    alert(msg);
}

// Checks if a select listbox is empty or not
function isEmpty(select)
{
    if ( select.options.length > 0 )
        return ( false );
    else
        return ( true );
}

// Returns a string with the contents of the listbox on the form:
// 'xxx;xxx', where xxx is the value of the option. Values are
// separated with semicolons;
function packListBox(select)
{
    var list = "";
    for ( var i=0; i<select.options.length; i++)
    {
        var option = select.options[i];
        if ( list == "" )
            list = option.value;
        else
            list += LISTBOX_VALUE_SEP + option.value;
    }
    return ( list );
}

// Returns a string with the contents of the listbox on the form:
// 'xxx=yyy', where xxx is the value of the option and
// yyy is the text of the option. Values are
// separated with semicolons;
function packListBoxWithText(select)
{
    var list = "";
    for ( var i=0; i<select.options.length; i++)
    {
        var option = select.options[i];
        if ( list == "" )
            list = option.value + "=" + option.text;
        else
            list += LISTBOX_VALUE_SEP + option.value + "=" + option.text;
    }
    return ( list );
}

//
// FORM
//

function getForm(formName)
{
    return ( eval("document."+formName) );
}

function checkInteger(field)
{
    var value = field.value;
    if ( value == "" )
        return ( true );
    if ( ((value/value) != 1 ) && (value != 0) )
    {
        return ( false );
    }
    return ( true );
}

function isFieldEmpty(field)
{
    if ( field.value == "" )
        return ( true );
    else
        return ( false );
}

function doCheck(cb)
{
    if ( cb.checked )
        cb.value = "true";
    else
    {
        cb.value = "false";
    }
}


// Packs all checkboxes on a form to a form-field
// for later processing.
// @param formName, name of form containing the form-field
// @param field, name of form-field that should be filled with checkbox info.
function packCheckBoxes(formName, field)
{
	var f = getForm(formName);
    var field = eval("document."+formName+"."+field);
    var theCheckbox = "";

    for (var i=0; i<f.elements.length; i++)
    {
        if ((f.elements[i].type == 'checkbox'))
        {
            var cb = f.elements[i];
            var name = cb.name;
            if ( cb.checked )
              theCheckbox = name + "=true";
            else
              theCheckbox = name + "=false";

            if ( field.value == "" )
                field.value = theCheckbox;
            else
                field.value += LISTBOX_VALUE_SEP + theCheckbox;

        }
    }
}

function packSelectedCheckBoxes(formName, field, separator)
{
	var f = getForm(formName);
    var field = eval("document."+formName+"."+field);
	field.value = "";
    var theCheckbox = "";

    for (var i=0; i<f.elements.length; i++)
    {
        if ((f.elements[i].type == 'checkbox'))
        {
            var cb = f.elements[i];
            var name = cb.name;
            if ( cb.checked )
			{
            	if ( field.value == "" )
                	field.value = name;
            	else
                	field.value += separator + name;
			}

        }
    }
}

// Sets checkboxes in either true or false mode.
function doSelect(form, field)
{
   f=eval("document." + form);
   var len = f.elements.length;
   var current = f.currentSelection.value;
   for(i=0;i<len;i++)
   {
      if ( f.elements[i].name == field )
      {
         if ( current == "true" )
         {
            f.elements[i].checked = false;
            f.currentSelection.value = "false";
         }
         else
         {
            f.elements[i].checked = true;
            f.currentSelection.value = "true";
         }
      }
   }
}

/**
 * Script for always selecting the first
 * text-input (if any) on a page
 */
function setInputFocus()
{
    if(document.forms[0])
    {
    	var myform = document.forms[0];
	    for (var i=0; i< myform.elements.length; i++)
      	{
        	if(myform.elements[i].type == 'text')
          	{
            	var field = myform.elements[i];
              	if(field.disabled != true)
				{
                	field.focus();
				}
              	break;
          	}
      	}
    }
}


function trimAll(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 trimLeft(str)
{
	// remove all spaces on left
	var newstr = str.replace(/[ ]*/, "");
	return (newstr);
}

function trimRight(str)
{
	// remove all spaces on right
	var newstr = str.replace(/[ ]*$/, "");
	return (newstr);
}

function trimBoth(str)
{
	// reomve all spaces on both side
	var newstr = trimRight(trimLeft(str));
	return (newstr);
}