/////////////////////
//Generic Functions//
/////////////////////

//Add key=value to an existing URL
function AppendUrl( url, key, val )
{
    var qsSym = '?';
    if ( url.indexOf( '?' ) != -1  )
    {
        qsSym = '&';
    }
    url += qsSym + key + '=' + val;
    return url;
}

//Return a key=val&key1=val1 string from all inputs/selects/textareas inside an element
function FormElementsFromId()
{
    this.ValueString = '';
    this.FormElements = new Array();
    
    //Private variables
    this._input_objs =  null;
    this._select_objs = null;
    this._textarea_objs = null;
    this._objs_id = new Array();
    this._objs_val = new Array();
    this._elem_id = null;
    this._elem_obj = null;

    //Set Container Id - Main()
    this.SetId = function( elem_id )
    {
        this._elem_obj = document.getElementById( elem_id );
        this._FormElements();
        
        return true;
    }
    //Capture Form Elements
    this._FormElements = function()
    {
        if ( this._elem_obj )
        {
            var y = 0;
            
            this._input_objs = this._elem_obj.getElementsByTagName( 'input' );
            this._select_objs = this._elem_obj.getElementsByTagName( 'select' );
            this._textarea_objs = this._elem_obj.getElementsByTagName( 'textarea' );
            
            for( var i = 0; i < this._input_objs.length; i++ )
            {
                if ( this._input_objs[ i ].id != '' || this._input_objs[ i ].name != '' )
                {
                  if ( this._input_objs[ i ].id != '' )
                  {
                      this._objs_id[ y ] = this._input_objs[ i ].id;
                  }
                  else
                  {
                      this._objs_id[ y ] = this._input_objs[ i ].name;
                  }
                  this._objs_val[ y ] = this._input_objs[ i ].value;
                  this.FormElements[ y ] = this._input_objs[ i ];
                  y++;
                }
            }
            
            for( var i = 0; i < this._select_objs.length; i++ )
            {
                if ( this._select_objs[ i ].id != '' || this._select_objs[ i ].name != '' )
                {
                  if ( this._select_objs[ i ].id != '' )
                  {
                      this._objs_id[ y ] = this._select_objs[ i ].id;
                  }
                  else
                  {
                      this._objs_id[ y ] = this._select_objs[ i ].name;
                  }
                  this._objs_val[ y ] = this._select_objs[ i ].value;
                  this.FormElements[ y ] = this._select_objs[ i ];
                  y++;
                }
            }
            
            for( var i = 0; i < this._textarea_objs.length; i++ )
            {
                if ( this._textarea_objs[ i ].id != '' || this._textarea_objs[ i ].name != '' )
                {
                  if ( this._textarea_objs[ i ].id != '' )
                  {
                      this._objs_id[ y ] = this._textarea_objs[ i ].id;
                  }
                  else
                  {
                      this._objs_id[ y ] = this._textarea_objs[ i ].name;
                  }
                  this._objs_val[ y ] = this._textarea_objs[ i ].value;
                  this.FormElements[ y ] = this._textarea_objs[ i ];
                  y++;
                }
            }
        }
    
        var sym = '';
        for( var i = 0; i < this._objs_id.length; i++ )
        {
            this.ValueString += sym + this._objs_id[ i ] + '=' + escape( this._objs_val[ i ] );
            sym = '&';
        }
        
        return true;
    }
} 
//////////////
//Ajax Tools//
//////////////
//Sequence Asyncronous Processes
var tools_array = new Array();
var tools_enumerator = 0;

function Sequence_AddTool( cmd )
{
    if ( cmd != null )
    {
        tools_array[ this.tools_enumerator ] = cmd;
        tools_enumerator++;
    }
    return true;
}
function Sequence_Start()
{
    tools_enumerator = 0;
    Sequence_LoadNext();
    return true;
}
function Sequence_LoadNext()
{
    eval( tools_array[ tools_enumerator ] );
    tools_enumerator++;
    return true;
}

//Load a Tool
function Load_Tool( tool_url, tool_id, pallet_id, sync_cmd )
{
    if ( tool_url != null && tool_id != null && pallet_id != null )
    {
        var target = null;
        if ( !document.getElementById( tool_id ) )
        {
            var elem = new CreateHtmlElement();
            elem.elementTag = 'div';
            elem.elementId = tool_id;
            elem.parentId = pallet_id;
            elem.elementClass = 'tool';
            target = elem.Run();
        }
        
         var ajax = new AjaxNow();
         ajax.id = tool_id;
         ajax.statusId = 'status';
         ajax.url = AppendUrl( tool_url, 'tool_enum', tools_enumerator );
         ajax.async = true;
         if ( sync_cmd != null )
         {
            ajax.callBack = sync_cmd;
         }
         ajax.indicator = '<img src="./../../base/img/ajax_spinner.gif"/>';
         ajax.Run();
         
         return true;
    }
    else
    {
        return false;
    }
}

////////////
//Controls//
////////////

//Transfer Select Options
function TransferOption( source_id, target_id )
{
	var s = document.getElementById( source_id );
	var t = document.getElementById( target_id );
	
	if ( s && t )
	{
		if ( s.selectedIndex < 0 )
		{
			alert( 'Please make a selection' );
		}
		else
		{
			s.selectedIndex = s.selectedIndex; //Ensure a single selection
			t.options[ t.options.length ] = s.options[ s.selectedIndex ]; //Move option
			s.options[ s.selectedIndex ] = null; //Delect source selection
			t.selectedIndex = -1; //Select none
		}
	}
}

//Select all options
function SelectAllToInput( source_id, target_id )
{
	var s_obj = document.getElementById( source_id );
	var t_obj = document.getElementById( target_id );
	
	if ( s_obj && t_obj )
	{
		var strVal = '';
		var sym = '';
		
		for( var i = 0; i < s_obj.options.length; i++ )
		{
			strVal += sym + s_obj.options[i].value;
			sym = ',';
		}
		
		t_obj.value = strVal;
	}
	return true;
}

//////////////
//Validation//
//////////////

//Alert if not a number
function ValidateNumber( obj )
{
	var orgBgColor = obj.style.backgroundColor;
	if ( obj.value.IsNaN )
	{
		obj.style.backgroundColor = '#f00';
	}
	else
	{
		obj.style.backgroundColor = orgGbColor;
	} 
}