
/* 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_attribute(array,value,id)
{
	array[id] = value;
}





/* MODIFY ATTRIBUTES
--------------------------------------------------------------------- /*
	Used to either add a new attribute or remove one. the start and end
	are invisible tags so that the attribute element can be removed with
	no messy code left behind.
--------------------------------------------------------------------- */
function modify_attributes(type,rm_id)
{	
	var extra_div 		= document.getElementById('attributes_extra');
	
	if(type == 'add')
	{
		opt_count += 1;
		extra_div.innerHTML = create_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
		attribute_name.splice(rm_id,1);
		attribute_affector.splice(rm_id,1);
		
		// reduce the total count by 1
		opt_count -= 1;
		
		extra_div.innerHTML = create_output(opt_count);
	}
}





/* CREATE OUTPUT
-------------------------------------------------------------------- /*
	Creates the output of all the available attributes. Starting and 
	ending are required for proper creating and deleting of the 
	attributes.
	
	use class="attribute" to style each element box the same
	use id="attr_" to set individual widths of element boxes
-------------------------------------------------------------------- */
function create_output(opt_count)
{
	var output = '';
		
	for(i=0;i<opt_count;i++)
	{
		
		/* set any default values
		--------------------------------------------------- */
		if(attribute_name[i] == null) attribute_name[i] = '';
		if(attribute_difference[i] == '' || attribute_difference[i] == null || attribute_difference[i] == '0')
		{
			attribute_difference[i] = '0.00';
		}
		/* --------------------------------------------------- */
		
		
		/* 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
		--------------------------------------------------- */
		attr_select = '<select name="attribute_affector[]" onChange="javascript:set_attribute(attribute_affector,this.value,'+i+');">';
		for(a=0;a<attr_affectors.length;a++)
		{
			var attr_selected = (attr_affectors[a] == attribute_affector[i]) ? 'selected' : '';
			attr_select += '<option value="'+attr_affectors[a]+'" '+attr_selected+'>'+attr_affectors[a]+'</option>';	
		}
		attr_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_attributes(\'remove\','+i+');" /></div>';
		title_rem		= '<div id="attr_title"><h4>Attribute '+(i+1)+':</h4>'+rem_div+'</div>';
		attr_name		= '<div class="attribute" id="attr_name"><p>Attribute Name:</p><input type="text" name="attribute_name[]" value="'+attribute_name[i]+'" size="30" maxlength="50" onChange="javascript:set_attribute(attribute_name,this.value,'+i+');" /></div>';
		attr_affector 	= '<div class="attribute" id="attr_affect"><p>Price Affector:</p>'+attr_select+'</div>';
		attr_diff		= '<div class="attribute" id="attr_price"><p>Price Difference:</p>$<input type="text" name="attribute_difference[]" size="10" value="'+attribute_difference[i]+'" onChange="javascript:set_attribute(attribute_difference,this.value,'+i+');"/></div>';
		
		output 			+= starting+'<div class="attribute_box" id="attr_'+bg_type+'">'+title_rem+attr_name+attr_affector+attr_diff+'</div>'+ending;
	}
	return output;
}





/* FOR ADDING OPTIONS TO PRODUCTS
------------------------------------------------------------------------------------------- */

/* ADD OPTION TO LIST
-------------------------------------------------------------------- /*
	Adds the option to the list, this is the main option and attributes
	will still need to be applied. See next function
-------------------------------------------------------------------- */
function add_option(name,type)
{	
	var field = document.getElementById(name);
		
	if(field.checked)
	{
		// add the value to the list
		if(product_options.value.indexOf(name) == -1) product_options.value += ';'+name+'|';
		if(product_options.value.indexOf(';') == 0) product_options.value = product_options.value.substring(1);
		
		// Activate all of the attributes
		field_array = eval(name+'_array');		
		for(i=0;i<field_array.length;i++)
		{
			cbox = eval('document.getElementById("'+name+'_'+field_array[i]+'")');
			if(type != 'cbx') cbox.checked = true;
			cbox.disabled = false;
			add_attribute(name,field_array[i]);
		}
	}
	else
	{			
		start = product_options.value.indexOf(name+'|');
		end = product_options.value.indexOf(';',start+2);
		
		opt_array = product_options.value.split(';');
		
		new_list = '';
		for(i=0;i<opt_array.length;i++)
		{
			split_array = opt_array[i].split('|');
			// if NOT the array we are trying to remove...
			if(split_array[0] != name)
			{
				new_list += split_array[0]+'|'+split_array[1]+';';
			}
		}
		
		new_list = new_list.substring(0,new_list.length-1);
		
		product_options.value = new_list;
		
		// Disable all of the attributes
		field_array = eval(name+'_array');
		for(i=0;i<field_array.length;i++)
		{
			cbox = eval('document.getElementById("'+name+'_'+field_array[i]+'")');
			if(type != 'cbx') cbox.checked = false;
			cbox.disabled = true;
		}
	}
	
}





/* ADD ATTRIBUTE
-------------------------------------------------------------------- /*
	Adds the attribute to the options list. An option must have 
	attributes in order for it to be displayed correctly
-------------------------------------------------------------------- */
function add_attribute(name,value)
{	
	// get the checkbox we just checked
	var cbox = document.getElementById(name+'_'+value);

	if(cbox.checked)
	{
		// find the appropriate place in the list
		name_start = product_options.value.indexOf(name);
		name_end = product_options.value.indexOf(';',name_start);
		if(name_end == -1) name_end = product_options.value.length;
		
		// get a first part and last part so that we can insert values in the middle
		first_part = product_options.value.substring(0,name_end);
		last_part = product_options.value.substring(name_end);
		
		if(first_part.indexOf(','+value+',') != -1) 		in_first 	= true;
		else if(first_part.indexOf('|'+value+',') != -1)	in_first 	= true;
		else if(first_part.indexOf(','+value+';') != -1)	in_first 	= true;
		else if(first_part.indexOf('|'+value+';') != -1)	in_first	= true;
		else												in_first 	= false;
		
		if(last_part.indexOf(','+value+',') != -1) 			in_last 	= true;
		else if(last_part.indexOf('|'+value+',') != -1)		in_last 	= true;
		else if(last_part.indexOf(','+value+';') != -1)		in_last 	= true;
		else if(last_part.indexOf('|'+value+';') != -1)		in_last 	= true;
		else												in_last 	= false;
				
		if(!in_first && !in_last)
		{
			// no comma for first attribute, put a comma between all others
			if(first_part.lastIndexOf('|') == first_part.length-1) new_first = first_part+value;
			else new_first = first_part+','+value;
		
			// save new options/attribute list
			product_options.value = new_first+last_part;
		}
	}
	else
	{
		opt_array = product_options.value.split(';');
		
		var new_list = '';
		
		for(i=0;i<opt_array.length;i++)
		{
			split_array = opt_array[i].split('|');
			split_attributes = split_array[1].split(',');
			
			var attr_list = '';
			for(a=0;a<split_attributes.length;a++)
			{
				if(split_attributes[a] != value) attr_list += ','+split_attributes[a];
			}
						
			if(attr_list.length) new_list += split_array[0]+'|'+attr_list.substring(1)+';';	
			else
			{
				document.getElementById(split_array[0]).checked = false;
				field_array = eval(split_array[0]+'_array');
				for(f=0;f<field_array.length;f++)
				{
					cbox = eval('document.getElementById("'+name+'_'+field_array[f]+'")');
					cbox.disabled = true;
				}
			}
		}
		
		new_list = new_list.substring(0,new_list.length-1);
				
		product_options.value = new_list;
	}	
}

/* ------------------------------------------------------------------------------------------- */







/* UPDATE PRICE
-------------------------------------------------------------------- /*
	This function is for use on the store side. It updates the price
	based on the options attributes and their affectors. The price must 
	be in an span element with id = 'product_price' in order for it 
	to be updated.
-------------------------------------------------------------------- */
function update_price(option_id,value)
{	
	var price_field			= document.getElementById('product_price')
	var price_text			= document.getElementById('price')
	var new_orig_price 		= original_price;
	var new_prod_options	= '';
	var price_extra			= '';
	
	if(option_id != null && value != null)
	{
		/* replace old value with new value, only if an option id is passed
		-------------------------------------------------- */
		if(option_id !== false)
		{
			var info 				= value.split(',');
			num_values[option_id] 	= info[1];
			if(info[0] != null) name_values[option_id]	= info[0];
		}
		
		/* CALCULATE OPTIONS
		-------------------------------------------------- */
		new_orig_price = calculate_options(num_values,new_orig_price);
		new_prod_options = update_options(num_values,name_values,opt_values,new_prod_options);
			
		
		/* COMMA REMOVER *
		-------------------------------------------------- */
		new_prod_options = new_prod_options.substring(1);
		/* -------------------------------------------------- */
		
		
		/* SET OPTIONS FOR POST
		-------------------------------------------------- */
		cart_options(new_prod_options);
		
		
		/* CUSTOM LETTERING *
		   add any lettering that may be in the custom letters field
		   
		   charge:BOOLEAN = whether or not we are going to charge by the letter
		-------------------------------------------------- */
		if(document.getElementById('custom_lettering') != null)
		{
			var charge		= true;
			var lettering 	= document.getElementById('custom_lettering').value;
			new_orig_price = add_lettering(lettering,new_orig_price,charge);
		}
		/* -------------------------------------------------- */
		
		
		/* update the price field and product price
		-------------------------------------------------- */	
		if(parseFloat(new_orig_price) != parseFloat(original_price))
		{
			price_extra = ' with Options';
			price_text.style.color = 'blue';
		}
		else
		{
			price_extra = '';
			price_text.style.color = '';
		}
	}
	
	new_orig_price 			= formatNumber(new_orig_price,2,',','.','','','-','')
	
	price_text.innerHTML 	= '$'+new_orig_price+price_extra;
	price_field.value		= new_orig_price;
		
}
/* -------------------------------------------------------------------- */





/* CALUCULATE OPTIONS
-------------------------------------------------------------------- */
function calculate_options(num_values,new_orig_price)
{	
	for(i=0;i<num_values.length;i++) new_orig_price = parseFloat(new_orig_price) + parseFloat(num_values[i]);
	return new_orig_price;
}
/* -------------------------------------------------------------------- */





/* UPDATE OPTIONS
-------------------------------------------------------------------- */
function update_options(num_values,name_values,opt_values,new_prod_options)
{	
	for(i=0;i<num_values.length;i++)
	{
		new_prod_options += ', '+opt_values[i].replace('_',' ')+': '+name_values[i];
	}
	return new_prod_options;
}
/* -------------------------------------------------------------------- */






/* ADD LETTERING
-------------------------------------------------------------------- */
function add_lettering(custom_letters,new_orig_price,charge)
{		
	/* variables
	-------------------------------------------------------------- */
	var price_display 	= document.getElementById('price_display');
	var price_field 	= document.getElementById('product_price');
	var opt_field		= document.getElementById('product_options');
	var new_price		= "0.00";
	/* -------------------------------------------------------------- */
			
	/* if we are charging by the letter and there are letters, then add to price
	-------------------------------------------------------------- */
	switch(charge)
	{
		case true:
			if(custom_letters.length > 0) new_price = parseFloat(new_orig_price)+parseFloat("0.47"*custom_letters.length);
			break;
		
		case 'word': break; /* use this for charging by the word */
	}
	
	
	/* add the custom text to the product options array
	-------------------------------------------------------------- */	
	opt_field.value += ', Custom Lettering: "'+custom_letters+'"';
	
	return new_price;
}
/* -------------------------------------------------------------------- */





/* ADD NEW OPTION LIST TO POST FIELD
-------------------------------------------------------------------- */
function cart_options(opt_list)
{
	prod_div = document.getElementById('product_options');
	prod_div.value = opt_list;
}
/* -------------------------------------------------------------------- */



// number formatting function
// copyright Stephen Chapman 24th March 2006, 10th February 2007
// permission to use this function is granted provided
// that this copyright notice is retained intact

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0');y.splice(z, 0, pnt); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}