function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}

/**
 *	Function in the works to display number of chars when typing out
 */
function checkMaxChars(element, counter_container, max) {
	var textarea = element.value;
	var counter = $(counter_container);
	var truncated_text = textarea;
	
	if(textarea.length > max) {
		truncated_text = textarea.substring(0, max);
		element.value = truncated_text;
	}
	
	counter.innerHTML = truncated_text.length;
}


/**
 * Sets the billing info the same as the account info
 */
function sameAsAccountInfo()
{
  if ( $('same_as_account_info').checked )
  {
    setSameBilling();
  } else {
  	clearBillingInfo();
  }
}

function clearBillingInfo() {
  var b_first_name     = $('billing_first_name');
  var b_last_name      = $('billing_last_name');
  var b_address1       = $('billing_address1');
  var b_address2       = $('billing_address2');
  var b_city           = $('billing_city');
  var b_state_province = $('billing_state_province');
  var b_postal_code    = $('billing_postal_code');
  
  b_first_name.value = '';
  b_last_name.value = '';
  b_address1.value = '';
  b_address2.value = '';
  b_city.value = '';
  b_state_province.value = '';
  b_postal_code.value = '';
}

function setSameBilling()
{
  var a_first_name		= $('first_name');
  var a_last_name		= $('last_name');
  var a_address1		= $('address1');
  var a_address2		= $('address2');
  var a_postal_code		= $('postal_code');
  var a_city			= $('city');
  var a_state_province	= $('state_province');
  
  var b_first_name     = $('billing_first_name');
  var b_last_name      = $('billing_last_name');
  var b_address1       = $('billing_address1');
  var b_address2       = $('billing_address2');
  var b_city           = $('billing_city');
  var b_state_province = $('billing_state_province');
  var b_postal_code    = $('billing_postal_code');

  // Set billing info same as account info
  if (a_first_name.value     != '') { b_first_name.value     = a_first_name.value;     }
  if (a_last_name.value      != '') { b_last_name.value      = a_last_name.value;      }
  if (a_address1.value       != '') { b_address1.value       = a_address1.value;       }
  if (a_address2.value       != '') { b_address2.value       = a_address2.value;       }
  if (a_city.value           != '') { b_city.value           = a_city.value;           }
  if (a_state_province.value != '') { b_state_province.value = a_state_province.value; }
  if (a_postal_code.value    != '') { b_postal_code.value    = a_postal_code.value;    }
}

function showProcessingMessage(container) {
	$(container).innerHTML = '<p class="processingMessage">Processing...</p>';
}

function messageHover(val) {
	$(val).style.backgroundColor = '#ffffff';
}

function messageHoverOff(val) {
	$(val).style.backgroundColor = '#dddddd';
}

function sendMessage(url) {
	window.open(url, 'sendMessage', 'height=550, width=700')
}

function displayElement(element) {
	$(element).style.display = '';
}

function displayNoneElement(element) {
	$(element).style.display = 'none';
}
