
/* CUSTOM OPTIONS
-------------------------------------------------------------------- /*

	This file contains the functions for adding additional elements
	to a form. Useful for creating custom options to be used
	in another section such as products. The element is a div holding
	any number of form elements inside. 
	
	This script is useful for creating user polls or product options.
	
	Created: 3.5.2007

-------------------------------------------------------------------- /*
-------------------------------------------------------------------- */





/* SET ATTRIBUTE
-------------------------------------------------------------------- /*
	This stores each value in an array so that when a new value is 
	added or removed, the options within each element will stay	as 
	they were (filled in, options still selected, etc.)
-------------------------------------------------------------------- */
function set_poll_option(array,value,id)
{
	array[id] = value;
}





/* MODIFY ATTRIBUTES
--------------------------------------------------------------------- /*
	Used to either add a new poll_option or remove one. the start and end
	are invisible tags so that the poll_option element can be removed with
	no messy code left behind.
--------------------------------------------------------------------- */
function modify_poll_options(type,rm_id)
{	
	var extra_div 		= document.getElementById('poll_options_extra');
	
	if(type == 'add')
	{
		opt_count += 1;
		extra_div.innerHTML = create_poll_output(opt_count);
	}
	else
	{
		last_id 	= rm_id-1;
		next_id 	= rm_id+1;
		start 		= extra_div.innerHTML.indexOf('<span style="visibility:hidden">&lt;'+rm_id+'</span>');
		end 		= extra_div.innerHTML.indexOf('<span style="visibility:hidden">'+rm_id+'&gt;</span>');
		first_part 	= extra_div.innerHTML.substring(0,start);
		last_part 	= extra_div.innerHTML.substring(end+5);
		
		// we need to remove it's values from our temp memory
		option_name.splice(rm_id,1);
		option_color.splice(rm_id,1);
		
		// reduce the total count by 1
		opt_count -= 1;
		
		extra_div.innerHTML = create_poll_output(opt_count);
	}
}





/* CREATE OUTPUT
-------------------------------------------------------------------- /*
	Creates the output of all the available poll_options. Starting and 
	ending are required for proper creating and deleting of the 
	poll_options.
	
	use class="poll_option" to style each element box the same
	use id="attr_" to set individual widths of element boxes
-------------------------------------------------------------------- */
function create_poll_output(opt_count)
{
	var output = '';
		
	for(i=0;i<opt_count;i++)
	{
		
		/* set any default values
		--------------------------------------------------- */
		if(option_name[i] == null) option_name[i] = '';
		/* --------------------------------------------------- */
		
		
		/* set a style for each row using even and odd
		--------------------------------------------------- */
		var bg_type = 'odd';
		if(i % 2) bg_type = 'even';
		/* --------------------------------------------------- */
		
		
		/* select field for the price affector
		--------------------------------------------------- */
		color_select = '<select name="poll_colors[]" id="poll_colors[]" onChange="javascript:set_poll_option(poll_colors,this.value,'+i+');">';
		for(a=0;a<option_colors.length;a++)
		{
			var color_selected = (option_colors[a] == option_color[i]) ? 'selected' : '';
			color_select += '<option value="'+option_colors[a]+'" '+color_selected+'>'+option_colors[a].replace('_',' ')+'</option>';	
		}
		color_select += '</select>';
		/* --------------------------------------------------- */
		
		
		starting 		= '<span style="visibility:hidden">&lt;'+i+'</span>';
		ending 			= '<span style="visibility:hidden">'+i+'&gt;</span>';
		
		rem_div			= '<div id="attr_remove"><input type="button" class="remove_btn" value="Remove" onClick="modify_poll_options(\'remove\','+i+');" /></div>';
		title_rem		= '<div id="attr_title"><h4>Option '+(i+1)+':</h4>'+rem_div+'</div>';
		name_div		= '<div class="attribute" id="attr_name"><p>Option Value:</p><input type="text" name="poll_options[]" id="poll_options[]" value="'+option_name[i]+'" size="30" maxlength="50" onChange="javascript:set_poll_option(option_name,this.value,'+i+');" /></div>';
		color_div 		= '<div class="attribute" id="attr_affect"><p>Option Color:</p>'+color_select+'</div>';
		
		output 			+= starting+'<div class="attribute_box" id="attr_'+bg_type+'">'+title_rem+name_div+color_div+'</div>'+ending;
	}
	return output;
}





/* RESET POLL
-------------------------------------------------------------------- /*
	used to reset the values in the poll
	results field. replaces all vote 
	totals with zero(0).
/* -------------------------------------------------------------------- */
function reset_poll()
{	
	results				= document.getElementById('poll_results');
	voter_list			= document.getElementById('poll_voters');
	rval				= results.value
	result_count		= rval.split(',').length;
	results.value		= '';
	for(i=0;i<result_count;i++) results.value += '0,';
	results.value = results.value.substring(0,results.value.length-1);
	voter_list.value	= ' ';			// needs to set as a space so that the empty list will be stored in the database (function does not store blank values)
	document.getElementById('reset_results').disabled = true;
	alert('The poll has been TEMPORARILY reset!\n Please make sure to click UPDATE or changes will be lost!');
}
