/**
 * Horde Form Sections Javascript Class
 *
 * Provides the javascript class for handling tabbed sections in Horde Forms.
 *
 * Copyright 2003-2004 Marko Djukic <marko@oblo.com>
 *
 * See the enclosed file COPYING for license information (LGPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * $Horde: horde/templates/javascript/form_sections.js,v 1.6 2004/03/16 19:20:36 chuck Exp $
 *
 * @author  Marko Djukic <marko@oblo.com>
 * @version $Revision: 1.6 $
 * @package Horde_Form
 */
function Horde_Form_Sections(instanceName, openSection)
{
    /* Set up this class instance for function calls from the page. */
    this._instanceName = instanceName;

    /* Store the currently open section in a variable, as well as the
     * cookie. */
    this._openSection = openSection;

    /* The cookie name we'll use. */
    this._cookieName = this._instanceName + '_open';

    this.getThis = function()
    {
        return this;
    }

    this.toggle = function(sectionId)
    {
        /* Get the currently open section object. */
        openSectionId = this._get();
        if (document.getElementById('_section_' + openSectionId)) {
            document.getElementById('_section_' + openSectionId).style.display = 'none';
            document.getElementById('_tab_' + openSectionId).className = 'tab';
            document.getElementById('_tabLink_' + openSectionId).className = 'tab';
        }

        /* Get the newly opened section object. */
        if (document.getElementById('_section_' + sectionId)) {
            document.getElementById('_section_' + sectionId).style.display = 'block';
            document.getElementById('_tab_' + sectionId).className = 'tab-hi';
            document.getElementById('_tabLink_' + sectionId).className = 'tab-hi';
        }

        /* Store the newly opened section. */
        this._set(sectionId);
    }
	
	this.toggleSwitch = function(sectionId)
    {
		/* Get the currently open section object. */
        openSectionId = this._get();
        if (document.getElementById('_section_' + openSectionId) && (openSectionId != sectionId)) {
            document.getElementById('_section_' + openSectionId).style.display = 'none';
        }
        /* Get the newly opened section object. */
        if (document.getElementById('_section_' + sectionId)) {
			if(openSectionId != sectionId | (document.getElementById('_section_' + sectionId).style.display != 'block'))
            	document.getElementById('_section_' + sectionId).style.display = 'block';
			else {
				document.getElementById('_section_' + sectionId).style.display = 'none';
			}
        }
        /* Store the newly opened section. */
        this._set(sectionId);
    }
	
	this.toggleOn = function(sectionId)
    {
		/* Get the currently open section object. */
        openSectionId = this._get();
        if (document.getElementById('_section_' + openSectionId) && (openSectionId != sectionId)) {
            document.getElementById('_section_' + openSectionId).style.display = 'none';
			//var parent = document.getElementById('_section_' + openSectionId).parentNode;
			//parent.id = 'x';
        }
        /* Get the newly opened section object. */
        if (document.getElementById('_section_' + sectionId)) {
           	document.getElementById('_section_' + sectionId).style.display = 'block';
			//var parent = document.getElementById('_section_' + openSectionId).parentNode;
			//parent.id = 'current';
        }
        /* Store the newly opened section. */
        this._set(sectionId);
    }

    this._get = function()
    {
        var dc = document.cookie;
        var prefix = this._cookieName + '=';
        var begin = dc.indexOf('; ' + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0) {
                return this._openSection;
            }
        } else {
            begin += 2;
        }

        var end = dc.indexOf(';', begin);
        if (end == -1) {
            end = dc.length;
        }

        return unescape(dc.substring(begin + prefix.length, end));
    }

    this._set = function(sectionId)
    {
        var cookieValue = this._cookieName + '=' + escape(sectionId);
        cookieValue += ';DOMAIN=mail.inac.sk;PATH=/horde2;';
        document.cookie = cookieValue;
        this._openSection = sectionId;
    }

    this._set(openSection);
}

//Functions for manipulation with SELECT field
function parseInt2( str )
{
   return str.substring(str.indexOf("_",0)+1);      
        
}

function codeSelected(select_object)
{
  codedString = '';
  for (i=0; i<select_object.options.length; i++)
  {
    codedString = codedString + ',' + parseInt2(select_object[i].value);
  }
  return codedString.substring(1);
}


// Add new item to select field
function addItemToSelect(select_field, value, description)
{
	var i, j, len;
	
	for (i=0; i<select_field.options.length; i++)
	{
		if (select_field.options[i].value == value)
		{
			break;
		}
		else
		{
			if (parseInt(select_field.options[i].value) > parseInt(value))
			{
				len = select_field.options.length;
				for (j=len-1; j>=i; j--)
				{
					select_field.options[j+1] = new Option(select_field.options[j].text, select_field.options[j].value);
					//select_field.options[j+1] = select_field.options[j];
				}
				select_field.options[i] = new Option(description, value);
				break;
			}
			
		}
	}
	
	if (i == select_field.options.length)
	{
		select_field.options[i] = new Option(description, value);
	}
}

// Remove field from select box
function removeItemFromSelect(select_field, index)
{
	select_field.options[index] = null;
}


// Move all selected fields from one select box to another
function moveItemsFromSelectToSelect(select_field_from, select_field_to)
{
	var i;
	
	unselectAllItems(select_field_to);

for (i=select_field_from.options.length-1; i>=0; i--)
	{
		if (select_field_from.options[i].selected)
		{
			addItemToSelect(select_field_to, 
							select_field_from.options[i].value, 
							select_field_from.options[i].text);
		}
	}
for (i=select_field_from.options.length-1; i>=0; i--)
	{
		if (select_field_from.options[i].selected)
		{
			removeItemFromSelect(select_field_from, i); 
		}
	}
}

// Select all fields in the select box
function selectAllItems(select_field)
{
	if (typeof(select_field) != "undefined") {
		for (i=0; i<select_field.options.length; i++){
			select_field.options[i].selected = true;
		}
	}
}

function unselectAllItems(select_field)
{
	if (typeof(select_field) != "undefined") {
//	if(isObject(select_field)){
		for (i=0; i<select_field.options.length; i++)
		{
			select_field.options[i].selected = false;
		}
	}
}

// move all selected items up
function moveUp(select_field)
{
	var i, new_option;
		
for (i=1; i<=select_field.options.length-1; i++)
	{
		if ((select_field.options[i].selected) && (!(select_field.options[i-1].selected)))
		{
					new_option = new Option(select_field.options[i-1].text, select_field.options[i-1].value);
//					select_field.options[i-1] = select_field.options[i];
					select_field.options[i-1] = new Option(select_field.options[i].text, select_field.options[i].value);
					select_field.options[i-1].selected = true;
					select_field.options[i] = new_option;  		
		}
	}
}

// move all selected items down
function moveDown(select_field)
{
	var i, new_option;
		
for (i=select_field.options.length-2; i>=0; i--)
	{
		if ((select_field.options[i].selected) && (!(select_field.options[i+1].selected)))
		{
					new_option = new Option(select_field.options[i+1].text, select_field.options[i+1].value);
//					select_field.options[i+1] = select_field.options[i];
					select_field.options[i+1] = new Option(select_field.options[i].text, select_field.options[i].value);
					select_field.options[i+1].selected = true;
					select_field.options[i] = new_option;  		
		}
	}
}

// checks max allowed input count for positions, categories, education_levels
function checkMaxInput(from_field, to_field, max, alert_msg)
{
	selected = to_field.options.length;
	to_move = 0;
	for (i=0; i<from_field.options.length; i++)
	{
		if (from_field.options[i].selected)
		{
			to_move++;
		}
	}

	if (selected+to_move > max)
	{
	  window.alert(alert_msg+' '+max);
		return false;
	}
	return true;
}

