var styleID = '';
var products = new Array();
var styles = new Array();

var parent_picture;
var parent_price;
var parent_msrp;
var parent_discount;
var ADD_PRODUCT_ID = 0;

function referenceURL(url)
{
	if ( url != '') {
		if ( url.indexOf(m_base) == -1 && url.indexOf('http') == -1 ) {
			url = m_base + url;
		}
		window.location.href = url;
	}
	else
		alert("No reference URL defined for this product!");
}

function changeStyle(productID, style_id, finish)
{
	style_ID = style_id;
	ADD_PRODUCT_ID = 0;

	// alert('style ID: ' + style_ID);
	if ( finish == "" ) finish = "finish";
	
	img_path = m_base + 'images/products/';
	if ( style_ID != 'parent' ) style_ID = parseInt(style_ID);
	if ( style_ID == 'parent' ) {
		alert("Please select a " + finish + " !");
	}
	else {
		image_file = styles[style_ID][0];
		price = styles[style_ID][1];
		msrp = styles[style_ID][2];
		discount = styles[style_ID][3];			
	}
		
	if (image_file == '') image_file = 'no_image.gif';
	picture = img_path + image_file;
	if ( document.getElementById('imageID_'+productID) ) {
		document.getElementById('imageID_'+productID).src = picture; // image
	}
	document.getElementById('priceID_'+productID).innerHTML = outputMoney(price); // price
	document.getElementById('msrpID_'+productID).innerHTML = msrp; // MSRP
	document.getElementById('discountID_'+productID).innerHTML = discount; // discount	

	// check for add products
	reg = '^add_product_'+productID+'_([0-9]+)';
	if ( document.kernel_form ) {
		for (var i = 0; i < document.kernel_form.elements.length; i++) {
			el = document.kernel_form.elements[i];
			if ( el.type == 'text' && el.name.match(reg) ) {
				add_p_id = RegExp.$1;
				price = parseFloat(document.getElementById('add_product_price_'+productID+'_'+add_p_id).value);
				// alert(' add_p_id ' + add_p_id + ' price ' + price );
				AddAdditionalProducts(productID, add_p_id, price);
			}
		}
	}
}

function cart_action(action) 
{
	document.kernel_form.elements['cart__action'].value = action;
	document.kernel_form.submit();
	document.kernel_form.target = '';
	document.kernel_form.t.value = t;
}

function addToCart(id, finish)
{
	if ( finish == "" ) finish = "finish";	
	
	el = document.getElementById('drop_' + id);
	if (el == null) 
		s_id = '0';
	else
		s_id = el.options[el.selectedIndex].value;

	if ( styleID == 'parent' || s_id == 'parent' ) {
		s_id = '0';
		alert("Please select a " + finish + " !");
		return;
	}
	if ( id > 0 ) {
		document.kernel_form.elements['cart__sel_id'].value = id;
		document.kernel_form.elements['cart__ps_id'].value = s_id;
		submit_action('cart__', 'action', 'AddToCart');
	}
}

function addToCartTemp(id, style_id)
{
	if ( id > 0 ) {
		document.kernel_form.elements['cart__sel_id'].value = id;
		document.kernel_form.elements['cart__ps_id'].value = style_id;
		submit_action('cart__', 'action', 'AddToCart');
	}
}

function update_cart() 
{
	cart_action('UpdateCart');
}

function delete_cart()
{
	cart_action('DeleteCart');
}

function save_cart() 
{
	cart_action('SaveCart');
}

function make_order() 
{
	cart_action('Order');
}

function delete_item(id)
{
	var field = document.getElementById(id);
	field.value=0;
	update_cart();
}

function edit_item(id)
{
	document.kernel_form.elements['cart__edit_id'].value = id;
	submit_action('cart__', 'action', 'EditTable');
}	

function SelectStyle(productID, style_id)
{
	el = document.getElementById('drop_' + productID);
	select_selected(el, style_id); 
}

// Tables styles
var error = 0;

function addToCartProduct(id, finish)
{
	if ( id == 145 ) {
		addToCartProduct145(id, finish);
	}
	else {
		if ( finish == "" ) finish = "finish";	
			
		el = document.getElementById('drop_' + id);
		if (el == null) 
			s_id = '0';
		else
			s_id = el.options[el.selectedIndex].value;
			
		if ( styleID == 'parent' || s_id == 'parent' ) {
			s_id = '0';
			alert("Please select a "+finish + " !");
			return;		
		}
		url = add_url + "product_id=" + id;
			
		if ( s_id > 0 ) {
			// check availibility
			p_styles = styles[s_id];
			if ( p_styles[4] == 0 ) 
			{
				error = 1;
				url = error_style_url + "product_id=" + id + "&product=yes";
			}
			else 
				error = 0
			url+= "&style_id=" + s_id;
		}
		
		// check for add products			
		if ( document.kernel_form ) {
			var add_p_ids = '';
			var add_p_qtys = '';
			reg = '^add_product_'+id+'_([0-9]+)';
			for (var i = 0; i < document.kernel_form.elements.length; i++) {
				el = document.kernel_form.elements[i];
				if (el.type == "text" && el.name.match(reg)) {
					add_p_id = RegExp.$1;
					add_p_ids+= add_p_id+',';
					add_p_qtys+= el.value+',';
				}
			}
			if ( add_p_ids != '' && add_p_qtys != '' ) url+='&add_p_id='+add_p_ids+'&add_p_qtys='+add_p_qtys;
		}
		if ( error == 1 ) {
			// o_links(error_style_url);
			window.location.href = error_style_url;	
		} 	
		else {
			window.location.href = url;
		}
	}
}

function addToCartProduct145(id, finish)
{
	url = add_url + "product_id=" + id;
	el = document.getElementsByName('styles');
	for (i = 0; i < el.length; i++)
	{
		if ( el[i].checked ) {
			s_id = el[i].id;
		}
	}
	
	if ( s_id > 0 ) {
		url+= "&style_id=" + s_id;
		window.location.href = url;	
	}
	else {
		alert('Please select color');
	}
}

function GetDDValue(aDropDown)
{
	return aDropDown.value != "" ? aDropDown.value : aDropDown.innerHTML ;
}

function IsSelectStyle(an_style, t_sizes, t_finishes, t_legs, t_pockets, t_slate)
{
	c_sel = 0;
	c_style = 0;
	
	t_sizes_value = GetDDValue(t_sizes);
	t_finishes_value = GetDDValue(t_finishes);
	t_legs_value = GetDDValue(t_legs);
	t_pockets_value = GetDDValue(t_pockets);
	t_slate_value = GetDDValue(t_slate);
	
	if ( an_style[3] != "" ) {
		c_style++;
		if ( an_style[3] == t_sizes_value ) {
			c_sel++;
		}
	}

	if ( an_style[4] != "" ) {
		c_style++;
		if ( an_style[4] == t_finishes_value ) {
			c_sel++;
		}
	}

	if ( an_style[9] != "" ) {
		c_style++;
		if ( an_style[9] == t_legs_value ) {
			c_sel++;
		}
	}

	if ( an_style[5] != "" ) {
		c_style++;					
		if ( an_style[5] == t_pockets_value ) {
			c_sel++;
		}
	}

	if ( an_style[8] != "" ) {
		c_style++;					
		if ( an_style[8] == t_slate_value ) {
			c_sel++;
		}
	}	

	if ( c_sel == c_style && c_style > 0 ) 
	{
	  // alert(" Found :: c_sel " + c_sel + " c_style " + c_style + " price " + an_style[1] );
	}	
	return ( c_sel == c_style && c_style > 0 ) ? 1 : 0 ;
}

function in_my_array (aArray, aValue) 
{
  var k;
  for (var k=0, len = aArray.length; k < len; k++) {
		if (aArray[k] === aValue) {
		    return true;
		}
  }
  return false;
}    
    
function EscOptions(aOption)
{
	aOption = aOption.indexOf("'") ? '"' + aOption + '"' : "'" + aOption + "'";
	return aOption;
}

function ArrayToSelect(anArray, aSelect, sel_pos)
{
	var initial_length = aSelect.length;
	if ( typeof(sel_pos) == 'undefined' ) sel_pos = 0;

	for (i = initial_length-1; i >= 0; i--) {	aSelect.options[i] = null; }
	
	for (i = 0; i < anArray.length; i++)
	{
		cur = anArray[i];
		selected = ( i == sel_pos ) ? 1 : 0 ;
		aSelect.options[aSelect.length] = new Option(cur, cur, selected);
	}
}


function array_to_select(anArray, aSelect, sel_pos)
{
	anArray.sort(select_compare);
	
	var initial_length = aSelect.length;
	
	if ( typeof(sel_pos) == 'undefined' ) sel_pos = 0;
	
	for (var i = initial_length - 1; i >= 0; i--)
	{
		aSelect.options[i] = null;
	}

	for (var i = 0; i < anArray.length; i++)
	{
		cur = anArray[i];
		selected = ( i == sel_pos ) ? 1 : 0 ;
		aSelect.options[aSelect.length] = new Option(cur[1], cur[1], selected);
	}
}

function select_compare($a, $b)
{
	if ($a[0] < $b[0])
		return -1;
	if ($a[0] > $b[0])
		return 1;
	return 0;
}

function SetTOptions(aStyles, aSelectType, aSelectedValues, productID) 
{	
	// 3 - t_size ; 4 - t_finish ; 9 - t_legs ; 5 - t_pockets	
	aOptions = new Array();
	j = 0;
	selected_value = '';	
	for ( i = 0; i < aStyles.length; i++ ) {
		
		an_style = aStyles[i];
		sel = 0;
		option_value = '';
		
		switch(aSelectType) {
			case 't_size':
				if ( an_style[3] == aSelectedValues['t_size']  ) {
					option_value = an_style[4];
					sel = 1;
					control = 't_finish';
					if ( option_value == aSelectedValues['t_finish'] ) {
						selected_value = aSelectedValues['t_finish'];
					}
				}
			break;	
			case 't_finish':
				if ( an_style[3] == aSelectedValues['t_size'] && an_style[4] == aSelectedValues['t_finish'] ) {
					option_value = an_style[9];
					// alert(' add finish to options:' + an_style[9] + 'an_style[3]:'+ an_style[3] + ' aSelectedValues[t_size]:' + aSelectedValues['t_size'] + ' an_style[4]:' + an_style[4] + ' aSelectedValues[t_finish]:' + aSelectedValues['t_finish'] );
					sel = 1;
					control = 't_leg';
					if ( option_value == aSelectedValues['t_leg'] ) {
						selected_value = aSelectedValues['t_leg'];
					}					
				}
			break;
			case 't_leg':
				if ( an_style[3] == aSelectedValues['t_size'] && an_style[4] == aSelectedValues['t_finish'] && an_style[9] == aSelectedValues['t_leg'] ) {
					option_value = an_style[5];
					sel = 1;
					control = 't_pockets';
					if ( option_value == aSelectedValues['t_pockets'] ) {
						selected_value = aSelectedValues['t_pockets'];
					}					
				}
			break;						
		}
		
		if ( sel == 1 && option_value != '' ) {
			if ( !in_my_array(aOptions, option_value)) {
				aOptions[j] = option_value;
				j++;
			}
		}
	}
		
	if ( aOptions.length > 0 ) {
		aDropDown = document.getElementById(productID+'_'+control);
		if ( typeof(aDropDown.options) != 'undefined' ) {
			ArrayToSelect(aOptions, aDropDown);
		}
		
		if ( typeof(aDropDown.options) != 'undefined' && selected_value != '' ) {
			// alert(' set selected option value  ' + selected_value);
			select_selected(aDropDown, selected_value);
		}		
	}
}

// fix Matt Black finish : sizes
function SetTOptionsSize(aStyles, aSelectedValues, productID) 
{		
	aArray = new Array();
	j = 0;
	control = 't_size';
	sel_pos = 0;
	sel_value = '';
	 
	t_finishes = document.getElementById(productID+'_t_finish');
	aSelectedValues['t_finish'] = GetDDValue(t_finishes);
		
	for ( i = 0; i < aStyles.length; i++ ) {
		an_style = aStyles[i];
		sel = 0;
		option_value = '';
		if ( an_style[4] == aSelectedValues['t_finish'] ) {
			option_value = an_style[3];
			sel = 1;
		}
		if ( sel == 1 && option_value != '' ) {
			if ( !in_my_array(aArray, option_value)) {
				if ( aSelectedValues['t_size'] == option_value ) {
					sel_pos = j;
					sel_value = option_value;
				}
				aArray[j] = option_value;
				j++;
			}
		}
	}

	if ( aOptions.length > 0 ) {
		aDropDown = document.getElementById(productID+'_'+control);
		if ( typeof(aDropDown.options) != 'undefined' ) {		
			ArrayToSelect(aArray, aDropDown, sel_pos);
			if ( is_msie() && sel_value != '' ) {
				select_selected(aDropDown, sel_value);
			}
		}
	}
}

// fix Matt Black finish : legs
function SetTOptionsLeg(aStyles, aSelectedValues, productID) 
{	
	// alert(" Fix Legs ")
			
	aOptions = new Array();
	j = 0;
	control = 't_leg';
	
	t_finishes = document.getElementById(productID+'_t_finish');
	aSelectedValues['t_finish'] = GetDDValue(t_finishes);

	t_sizes = document.getElementById(productID+'_t_size');
	aSelectedValues['t_size'] = GetDDValue(t_sizes);
				
	for ( i = 0; i < aStyles.length; i++ ) {
		an_style = aStyles[i];
		sel = 0;
		option_value = '';
		if ( an_style[3] == aSelectedValues['t_size'] && an_style[4] == aSelectedValues['t_finish'] ) {
			option_value = an_style[9];
			sel = 1;
		}
		if ( sel == 1 && option_value != '' ) {
			if ( !in_my_array(aOptions, option_value)) {					
				aOptions[j] = option_value;
				j++;
			}
		}
	}
	
	if ( aOptions.length > 0 ) {
		aDropDown = document.getElementById(productID+'_'+control);
		if ( typeof(aDropDown.options) != 'undefined' ) {
			ArrayToSelect(aOptions, aDropDown);
		}
	}
}

// fix Matt Black finish : legs
function SetTOptionsFinish(aStyles, aSelectedValues, productID) 
{				
	aOptions = new Array();
	j = 0;
	control = 't_finish';

	t_legs = document.getElementById(productID+'_t_leg');
	aSelectedValues['t_leg'] = GetDDValue(t_legs);

	t_sizes = document.getElementById(productID+'_t_size');
	aSelectedValues['t_size'] = GetDDValue(t_sizes);

	for ( i = 0; i < aStyles.length; i++ ) {
		an_style = aStyles[i];
		sel = 0;
		option_value = '';
		if ( an_style[3] == aSelectedValues['t_size'] && an_style[9] == aSelectedValues['t_leg'] ) {
			option_value = an_style[4];
			sel = 1;
		}
		if ( sel == 1 && option_value != '' ) {
			if ( !in_my_array(aOptions, option_value)) {
				aOptions[j] = option_value;
				j++;
			}
		}
	}
	
	if ( aOptions.length > 0 ) {
		aDropDown = document.getElementById(productID+'_'+control);
		if ( typeof(aDropDown.options) != 'undefined' ) {
			ArrayToSelect(aOptions, aDropDown);
		}
	}
}
	
function SelectTableStyle(productID, selectType)
{
	price_div = document.getElementById(productID+'_priceID');
	
	t_sizes = document.getElementById(productID+'_t_size');
	t_finishes = document.getElementById(productID+'_t_finish');
	t_legs = document.getElementById(productID+'_t_leg');
	t_pockets = document.getElementById(productID+'_t_pockets');
	t_slate = document.getElementById(productID+'_t_slate');
	t_quality = document.getElementById(productID+'_t_quality');
	
	selectedValues = new Array();
		
	selectedValues['t_size'] = GetDDValue(t_sizes);
	selectedValues['t_finish'] = GetDDValue(t_finishes);
	selectedValues['t_leg'] = GetDDValue(t_legs);
	selectedValues['t_pockets'] = GetDDValue(t_pockets);

	p_styles = t_styles[productID];
	
	update_below = 0;
	
	for(selectedValue in selectedValues) 
	{
		// last without recalculate
		if ( selectType == 't_pockets' ) break;
				
		if ( selectType == selectedValue ) update_below = 1;	
	
		// update if not current
		if ( update_below == 1 ) 
		{
			SetTOptions(
				p_styles, 
				selectedValue, // selectType
				selectedValues, 
				productID
			);

			// fix Matt Black finish
			if ( selectedValue == 't_finish' && selectType == 't_finish' ) {
				// fix size				
				
				SetTOptionsSize(
					p_styles,
					selectedValues, 
					productID
				);
								
				// fix legs				
				SetTOptionsLeg(
					p_styles, 
					selectedValues, 
					productID
				);
			}
			
			// fix Legs IB #24931
			/*			
			if ( selectedValue == 't_leg' && selectType == 't_leg' ) {
				// fix size
				SetTOptionsSize(
					p_styles,
					selectedValues, 
					productID
				);
				// fix finish
				SetTOptionsFinish(
					p_styles, 
					selectedValues, 
					productID
				);
			}
			*/
		}
	}

	selected = 0;
	
	for ( i = 0; i < p_styles.length; i++ ) 
	{
		an_style = p_styles[i];
		if ( IsSelectStyle(an_style, t_sizes, t_finishes, t_legs, t_pockets, t_slate) ) {
			price = parseInt(an_style[1]);
			if ( t_quality.value == 1 ) {
				price += parseInt(premium_cloth_price);
				price = parseInt(price);
			}
			price_div.innerHTML = outputMoney(price+'');
			selected = 1;
			// change image
			image_file = m_base + 'images/products/' + an_style[0];			
			document.getElementById('imageID_'+productID).src = image_file; // image
			document.getElementById('addbuttonon').style.display = 'block';
			document.getElementById('addbuttonoff').style.display = 'none';
			break;
		}
	}

	if ( selected == 0 ) 
	{
		document.getElementById('addbuttonon').style.display = 'none';
		document.getElementById('addbuttonoff').style.display = 'block';
		price_div.innerHTML = "N/A";
	}
	
	// (#26753)
	OceansideColorElement = document.getElementById(productID + '_t_colors8');
	if ( selectType == 't_quality' ) {
		if ( t_quality.value != 1 ) {
			OceansideColorElement.disabled = true;
			if ( OceansideColorElement.checked == true ) {
				BrunswickGreenColorElement = document.getElementById(productID + '_t_colors1');
				BrunswickGreenColorElement.checked = true;
			}
		}
		else
			OceansideColorElement.disabled = false;	
	}	
}

function SelectTableStyleDisabled(productID, selectType)
{
	price_div = document.getElementById(productID+'_priceID');
	
	t_sizes = document.getElementById(productID+'_t_size');
	t_finishes = document.getElementById(productID+'_t_finish');
	t_legs = document.getElementById(productID+'_t_leg');
	t_pockets = document.getElementById(productID+'_t_pockets');
	t_slate = document.getElementById(productID+'_t_slate');
	t_quality = document.getElementById(productID+'_t_quality');
	
	selectedValues = new Array();
		
	selectedValues['t_size'] = GetDDValue(t_sizes);
	selectedValues['t_finish'] = GetDDValue(t_finishes);
	selectedValues['t_leg'] = GetDDValue(t_legs);
	selectedValues['t_pockets'] = GetDDValue(t_pockets);

	p_styles = t_styles[productID];
	
	update_below = 0;
	
	for(selectedValue in selectedValues) 
	{
		// last without recalculate
		if ( selectType == 't_pockets' ) break;
				
		if ( selectType == selectedValue ) update_below = 1;	
	
		// update if not current
		if ( update_below == 1 ) 
		{
			SetTOptions(
				p_styles, 
				selectedValue, // selectType
				selectedValues, 
				productID
			);

			// fix Matt Black finish
			if ( selectedValue == 't_finish' && selectType == 't_finish' ) {
				// fix size				
				
				SetTOptionsSize(
					p_styles,
					selectedValues, 
					productID
				);
								
				// fix legs				
				SetTOptionsLeg(
					p_styles, 
					selectedValues, 
					productID
				);
			}
		}
	}

	selected = 0;
	
	for ( i = 0; i < p_styles.length; i++ ) 
	{
		an_style = p_styles[i];
		if ( IsSelectStyle(an_style, t_sizes, t_finishes, t_legs, t_pockets, t_slate) ) {
			price = parseInt(an_style[1]);
			if ( t_quality.value == 1 ) {
				price += parseInt(premium_cloth_price);
				price = parseInt(price);
			}
			price_div.innerHTML = outputMoney(price+'');
			selected = 1;
			// change image
			image_file = m_base + 'images/products/' + an_style[0];			
			document.getElementById('imageID_'+productID).src = image_file; // image
			document.getElementById('addbuttonon').style.display = 'block';
			document.getElementById('addbuttonoff').style.display = 'none';
			break;
		}
	}

	if ( selected == 0 ) 
	{
		document.getElementById('addbuttonon').style.display = 'none';
		document.getElementById('addbuttonoff').style.display = 'block';
		price_div.innerHTML = "N/A";
	}
	
	// (#27972)
	OceansideColorElement = document.getElementById(productID + '_t_colors8');
	OceansideColorElementPhrase = document.getElementById(productID + '_t_colors8_phrase');
	if ( selectType == 't_quality' ) {
		if ( t_quality.value != 1 ) {
			OceansideColorElement.style.display = 'none';
			OceansideColorElementPhrase.style.display = 'none';
		}
		else {
			OceansideColorElement.style.display = 'inline';
			OceansideColorElementPhrase.style.display = 'inline';	
		}
	}	
}

function SelectFirstTableStyle(productID)
{	
	if ( t_styles[productID] ) p_styles = t_styles[productID];
	
	if ( p_styles.length == 0 ) return;
	
	p_styles = t_styles[productID];
	price_div = document.getElementById(productID+'_priceID');
	t_sizes = document.getElementById(productID+'_t_size');
	t_finishes = document.getElementById(productID+'_t_finish');
	t_pockets = document.getElementById(productID+'_t_pockets');
	t_slate = document.getElementById(productID+'_t_slate');
	
	an_style = p_styles[0];
	price_div.innerHTML = outputMoney(an_style[1]);
	
	if ( t_sizes.options ) {
		if ( an_style[3] != "" && t_sizes.options.length > 0 ) {
			 select_selected(t_sizes, an_style[3]);
		}
	}
	if ( t_finishes.options ) {
		if ( an_style[4] != "" && t_finishes.options.length > 0 ) {
			 select_selected(t_finishes, an_style[4]);
		}
	}
	if ( t_pockets.options ) {
		if ( an_style[5] != "" && t_pockets.options.length > 0 ) {
			 select_selected(t_pockets, an_style[5]);
		}
	}
	
	// (#26753)
	OceansideColorElement = document.getElementById(productID + '_t_colors8');
	t_quality = document.getElementById(productID+'_t_quality');
	if ( t_quality.value != 1 ) {
		OceansideColorElement.disabled = true;
		if ( OceansideColorElement.checked == true ) {
			BrunswickGreenColorElement = document.getElementById(productID + '_t_colors1');
			BrunswickGreenColorElement.checked = true;
		}
	}
	else
		OceansideColorElement.disabled = false;	
		
		
			
}

// products select ....

function addToCartTable(productID)
{
	if ( productID > 0 ) {
		t_name = document.getElementById(productID+'_product_name'); 
		t_sizes = document.getElementById(productID+'_t_size');
		t_finishes = document.getElementById(productID+'_t_finish');
		t_pockets = document.getElementById(productID+'_t_pockets');
		t_legs = document.getElementById(productID+'_t_leg');
		t_quality = document.getElementById(productID+'_t_quality');
		
		selected_color = 'Brunswick Green';
		
		for ( z = 1; z < 13; z++ ) {
			t_colors = document.getElementById(productID+'_t_colors' + z);
			if ( t_colors.checked ) selected_color = t_colors.value;
		}
				
		t_slate = document.getElementById(productID+'_t_slate');
		t_price = document.getElementById(productID+'_priceID');
				
		a_legs = t_legs.value;
		a_legs = a_legs.replace("&", "|||");

		// check availibility
		p_styles = t_styles[productID];
		url = add_url_table;		
		for ( i = 0; i < p_styles.length; i++ ) {
			an_style = p_styles[i];
			// alert("an_style[3]="+an_style[3]+" t_sizes.value="+t_sizes.value+" an_style[4]="+an_style[4]+" t_finishes.value="+t_finishes.value+" an_style[5]="+an_style[5]+" t_pockets.value="+t_pockets.value+" an_style[7]="+an_style[7]);
			if ( IsSelectStyle(an_style, t_sizes, t_finishes, t_legs, t_pockets, t_slate) ) {
				// alert(" availibility " + an_style[6] + " an_style[7] = " + an_style[7]);
				if ( an_style[6] == 0 ) { 
					error = 1;
					// url = error_style_url;
					break;
				}
				error = 0;
			}
		}
		url+= "table=" + t_name.value + "&size=" + t_sizes.value + "&finish=" + t_finishes.value;
		url+= "&legs=" + a_legs + "&pocket=" + t_pockets.value + "&clothtext=" + selected_color;
		url+= "&qual=" + t_quality.value;
		url+= "&slate=" + t_slate.value + "&price=" + ClearMoneyFormat(t_price.innerHTML) + "&c=1";

		// alert("url" + url);
		// return;

		if ( error == 1 ) {
			window.location.href = error_style_url;
		} 	
		else {	
			window.location.href = url;
		}
	}
}

/* Money Formatter */

function ClearMoneyFormat(s)
{
	s = s.replace("$", "");
	s = s.replace(",", "");
	return s;
}		

function outputMoney(number) 
{
  number=checkval(number);
  return "$"+outputDollars(Math.floor(number-0) + '') + outputCents(number - 0);
}

function checkval(mycheckval) 
{
  	mycheckval=mycheckval.replace(/,/g, '');
  	mycheckval=mycheckval.replace(/\$/g, '');
  	return (mycheckval);
}

function outputDollars(number) 
{
	if (number.length <= 3)
	  return (number == '' ? '0' : number);
	else {
    var mod = number.length%3;
    var output = (mod == 0 ? '' : (number.substring(0,mod)));
    for (i=0 ; i < Math.floor(number.length/3) ; i++) {
      if ((mod ==0) && (i ==0))
        output+= number.substring(mod+3*i,mod+3*i+3);
      else
        output+= ',' + number.substring(mod+3*i,mod+3*i+3);
    }
    return (output);
	}
}

function outputCents(amount) 
{
  amount = Math.round( ( (amount) - Math.floor(amount) ) *100);
  return (amount < 10 ? '.0' + amount : '.' + amount);
}

function flash_popup(table)
{
	settings = 'width=725,height=510,location=no,directories=no,menubar=yes,toolbar=no,status=no,scrollbars=no,resizable=yes,fullscreen=no'
	PopupWin = window.open(m_base+'build_your_own_table/flash.php?table='+table,'PopupWin', settings);
	PopupWin.focus();
}

// set functions

function addToCartProductSet(product_id)
{
	document.getElementById(product_id).checked = true;
	SetFullSet(product_id);	
	document.kernel_form.t.value = 'controls/product_set_item';
	submit_action('cart__', 'action', 'ToCartProductSet');
}

function addToCartProductOptions(product_id)
{
	document.kernel_form.t.value = 'controls/product_set_item';
	reg = 'ind_set_'+product_id;
	options_selected = 0;
	for (var i = 0; i < document.kernel_form.elements.length; i++) {
		el = document.kernel_form.elements[i];
	 	if (el.type == "checkbox" && el.name.match(reg) && el.checked == true ) {
	 		options_selected++;
	 	}
	}
	if ( options_selected > 0 )
		submit_action('cart__', 'action', 'ToCartProductOptions');
	else 
		alert('Please select at least one options!');
}

function SetFullSet(product_id)
{
	box = document.getElementById(product_id);
	if ( box.checked == true ) {
		reg = 'ind_set_'+product_id;
		for (var i = 0; i < document.kernel_form.elements.length; i++) {
			el = document.kernel_form.elements[i];
			if (el.type == "checkbox" && el.name.match(reg) ) el.checked = false;
			document.getElementById('price_'+product_id).value = '';
			document.getElementById('priceInd_'+product_id).innerHTML = '';
		}
	}
}


function SetIndSet(product_id, style_id, price) 
{
	box = document.getElementById('ind_set_'+product_id+'_'+style_id);
	var add = 0;
	if ( box.checked == true ) {
		add = 1;
		document.getElementById(product_id).checked = false;
	}
	
	price_field = document.getElementById('price_'+product_id);
	
	cur_price = parseFloat(price_field.value);
		
	if ( isNaN(cur_price) ) cur_price = 0.001;

	if ( add == 1 ) {
		price_field.value = cur_price + parseFloat(price);
	}
	else
		price_field.value = cur_price - parseFloat(price);

	document.getElementById('priceInd_'+product_id).innerHTML = outputMoney(price_field.value); 
}

function AddAdditionalProducts(product_id, add_p_id, price)
{
	box = document.getElementById('add_product_'+product_id+'_'+add_p_id);
	prev_box = document.getElementById('prev_qty_'+product_id+'_'+add_p_id);
	
	price_box = document.getElementById('priceID_'+product_id);
	
	main_price = 0;
	main_price = price_box.innerHTML.replace(/[,\$]/g, '');
	main_price = parseFloat(main_price);
	
	total = 0;
	qty = parseInt(box.value);
	prev_qty = parseInt(prev_box.value);

	if ( qty > 0 ) {
		if ( qty == prev_qty )
			total = main_price + qty * price;
		else
			total = main_price + ( qty - prev_qty ) * price;	
		prev_box.value = qty;
	}
	else {
		total = main_price - prev_qty * price;
		prev_box.value = '0';
		box.value = '0';
	}
	price_box.innerHTML = outputMoney(total.toString());
}

function changeAddStyle(productID, add_product_id, product_price)
{
	ADD_PRODUCT_ID = add_product_id;
	document.getElementById('priceID_'+productID).innerHTML = outputMoney(parseFloat(product_price).toString()); // price	
}

function addToCartProductStyles(id, finish)
{
	if ( finish == "" ) finish = "finish";
	if (style_ID == '') 
		s_id = '0';
	else
		s_id = style_ID;
		
	if ( ADD_PRODUCT_ID != id && s_id == '0' ) {
		alert("Please select a "+finish + " !");
		return;
	}
	
	if ( ADD_PRODUCT_ID > 0 ) {
		id = ADD_PRODUCT_ID;
		s_id = 0;
	}

	url = add_url + "product_id=" + id;
	if ( s_id > 0 ) {
		// check availibility
		p_styles = styles[s_id];
		if ( p_styles[4] == 0 ) {
			error = 1;
			url = error_style_url + "product_id=" + id + "&product=yes";
		}
		else 
			error = 0
		url+= "&style_id=" + s_id;
	}
	if ( error == 1 ) {
		window.location.href = error_style_url;	
	} 	
	else {
		window.location.href = url;
	}
}
