// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var $CE = function(tagName, attributes, styles){ //short for create element
      var el = document.createElement(tagName);
      if (attributes)
            $H(attributes).each(function(pair){
                  eval("el." + pair.key + "='" + pair.value + "'");
            });
      if (styles)
            $H(styles).each(function(pair){
                  el.style[pair.key] = pair.value;
            });

      return $(el);
};

Element.addMethods({
	//removes any child noes from the element
	//example: <div id="myDiv"><b>hello</b></div>
	//         $('myDiv').clearChildren();
	//     ==> <div id="myDiv"></div>
	clearChildren: function(element) {
		element = $(element);
		$A(element.childNodes).each(function(e){
			  e.parentNode.removeChild(e);
		});
		return element;
	},
	//method that creates a new element and appends to the current element
	// example: <div id="myDiv">Please</div>
	//          $('myDiv').append('A',{href:'otherpage.html', className:'red'}).update('Continue...');
	//     ==>  <div id="myDiv">Please<a href="otherpage.html" class="red">Continue...</a></div>
	append: function(element, tagName, attributes, styles) {
		element = $(element);
		var newEl = $CE(tagName, attributes, styles);
		element.appendChild(newEl);
		return newEl;//<-- this one returns the new element
	},
	//appends a text node to the element
	// example: <div id="myDiv"><b>hello</b></div>
	//          $('myDiv').appendText(', John');
	//      ==> <div id="myDiv"><b>hello</b>, John</div>
	appendText: function(element, text){
		element = $(element);
		var t = document.createTextNode(text);
		element.appendChild(t);
		return element;
	  }
});

/*
   This allows us to build AutoCompleters using siblings instead of ids
   Because we use policy[] notation we have duplicate named objects, 
   Also we have repeating partials that are dynamic, so ids are out of the
   Question. This model uses siblings instead:

   1) Hook this up by attaching to the onfocus event. The first focus event
      will build the auto completer and delete the focus event
   2) This code is slightly unsafe as it relies on nextSibling. You cannot
      have any whitespace (even \n) between the text field and the complete
      options div 
*/
function build_Autocompleter(field, values, options) {            
  field.onfocus = null;
  if (!options) options = {};
  field.auto_completer = new Autocompleter.Local(field, field.nextSibling, values, options);    
}  

function update_Autocompleter(field, values) {
  if (!values) values = [];
  if (field.auto_completer) {
    field.auto_completer.options.array = values;
  } else {
    build_Autocompleter(field, values);    
  }
}

function toggleCardInfo(){
  if($('payment[typed]').value.match(/^online/i)){
    Element.show('card_information');
  }else{
    Element.hide('card_information');
  }
}

function showHideBoatLength(typeSelect){
    var type = typeSelect.value;
}
