function asp_id_match( element, id ) {
  var selector = "[id$='_" + id + "']";
  if( !element )
    return $$(selector)[0];
  else
    return element.select( selector )[0];
}


// name is one of 'grip' or 'shaft'
function shaft_or_grip_click(e) {
  var li = $(e.target).up_inclusive('li');
  
  if( !li )
    return;
  var name, sel_name, sel_value;
  var fieldset = li.up('fieldset');
  switch( fieldset.id ) {
    case 'shafts': 
      name = 'shaft';
      sel_name = 'flex';
      sel_value = cur_flex;
      break;
    case 'grips':
      name = 'grip';
      sel_name = 'grip_size';
      sel_value = cur_grip_size;
      break;
     case 'shaft_bands':
			name = 'shaft_band';
			break;
  }
  if( !name )
    return;

  var label = li.down('label');

  if( li.hasClassName('selected') )
    return;
      
  li.up('ul').select('li').invoke('removeClassName', 'selected');
  li.addClassName('selected');
  
  var input = li.down("input[type='radio']")
  input.checked = true;
  
  if(sel_name)
  {
		sel_active = document.forms["FMain"][sel_name];
		sel_active.options.length = 0;
		
		sel_options = eval(sel_name + input.value);
		if(!sel_options)
				return;
		
		for( j = 0; j < sel_options.length; j++)
		{
			sel_active.options[j] = new Option(sel_options[j], sel_options[j]);
			if(sel_options[j] == sel_value)
				sel_active.options[j].selected = true
			
		}
  }
  
  // Enable select box
  
  /*
  var sel_all = fieldset.select('select');
  sel_all.invoke('removeClassName', 'selected');
  sel_all.invoke('disable');
  var sel_active = fieldset.select('select.' + name + input.value);
  sel_active.invoke('addClassName', 'selected');
  sel_active.invoke('enable');
  */  
  
  fieldset.select('img').invoke('removeClassName', 'selected');
  fieldset.select('img.' + name + input.value).invoke('addClassName', 'selected');
  
  
}

// Add functions to the validations array, and they will be called to validate
// the contents of the page.
// Return nothing if the content passes the validation, otherwise return the error message.
// The function is passed one parameter, state which is the serialized version of the form.
// Do not put side effects in the function.
// Compatibility note:  This doesn't work on Opera, so we lose all client side validation prompts.
// Safari 2.0 requires us to bind the event directly.
var validations = []
function validate(e) {
  if( !validations || validations.length == 0 )
    return;
  var form = $('FMain');
  if( !form || !form.serialize )
    return;
  var state = form.serialize(true);
  var results = validations.invoke('call', null, state).flatten().compact();
  if( results.length > 0 ) {
    results.pluck('element').compact().invoke('addClassName', 'error');
    var result = "The page has not been filled out properly, the following problems were found:\n\n" +
      results.map(function(result){ return "  *  "+result.message }).join("\n");
    // IE fails to do this
    try {
      e.returnValue = result;
    } catch(e) {
    }
    return result;
  }
}
window.onbeforeunload = validate;

function form_change(e) {
  if( !validations || validations.length == 0 )
    return;
  var form = $('FMain');
  if( !form || !form.serialize )
    return;
  var state = form.serialize(true);
  var nextback = $('nextback');
  var next = asp_id_match( nextback, 'next' );
  var results = validations.invoke('call', null, state).flatten().compact();
  var marked = $$('.error');
  if( marked.length > 0 ) {
    marked.invoke('removeClassName', 'error');
    results.pluck('element').compact().invoke('addClassName', 'error');
  }
  if( results.length > 0 ) {
    next.addClassName('disabled');
  } else {
    next.removeClassName('disabled');
  }
}
function nextback() {
  var nextback = $('nextback');
  if( !nextback )
    return;
  var form = nextback.up('form');
  if( !form )
    return;
  form.getElements().invoke( 'observe', 'change', form_change );
  form_change();
}
Event.observe(window, 'load', nextback);

function link_click_internal(e) {  
  var link = $(e.target).up_inclusive('a.post');
  if( !link )
    return;
  
  form = link.up('form') || $(document.body).down('form');
  if( !form )
    return;

  e.stop();
  
  if( link.className.indexOf('wedge_remove') > -1 )
	if( !confirm('Are you sure you want to remove this wedge from your cart?') )
		return;
  
  var added_params = $H(link.href.toQueryParams());
  if( added_params )
  {
	added_params.each(function(pair) {
		if( !form[pair.key] ) {
		form.insert( new Element("input", {name: pair.key, value: pair.value, type:'hidden'} ) );
		} else {
		form[pair.key].value = pair.value;
		}
	});
  }	
  form.action = link.href;
  // exception occurs in some browsers if user cancels leaving the page.
  try {
    form.submit();
  } catch (ee) {
  }   
}
// TODO: external links have to be detected, and then cause the form to post asynchronously.
function link_click_external(e) {  
  var link = $(e.target).up_inclusive('a');

  var form = link.up('form') || $(document.body).down('form');
  form.request();
}

function grab_links() {
  Event.observe(document.body, "click", link_click_internal);
}

function cart_setup()
{
	if( !$('cart_list') )
		return;
	$$('#cart_list li a.wedge').invoke( 'observe', 'click', cart_toggle_wedge );
	
	//$$('#cart_list li a.remove, #cart_list li a.edit').invoke( 'observe', 'click', cart_modify_wedge );
	
	
}

function cart_toggle_wedge(e)
{
	var li = $(e.target).up_inclusive('li');
	if( !li )
		return;
			
	e.stop();
	
	if(li.className.indexOf('open') >= 0)
		li.removeClassName('open');
	else	
		li.addClassName('open');
}

function cart_modify_wedge(e)
{
	var link = $(e.target);
	if(!link)
		return;
		
	e.stop();	
		
	
	
	var query_params = $H(link.href.toQueryParams());
	if(!query_params)
		return;
				
	$('FMain').submit();	
}

function toggle_popup(elementId) {
  var popup = $(elementId);
  
  if( popup.className == "show" )
	popup.className = "";
  else
	popup.className = "show";
	
  return false;		
}

Event.observe(window,"load",grab_links);
Event.observe( window, "load", cart_setup );

