function businessNameFieldKeyUp() {
  var rowDisplayValue = ($('business_name').value != '') ? '' : 'none';
  var rows = document.getElementsByClassName('signup_business_only_row');
  
  for (rowIndex = 0; rowIndex < rows.length; rowIndex++) {
    row = rows[rowIndex];
    
    row.style.display = rowDisplayValue;
  }
}

function emailFieldKeyUp() {
  if (!haveModifiedUsername) {
    // base default username off email address, removing the @ symbol and stripping illegal chars
    if($('username')) {
	    username = $('email').value.replace(/^([^@]*).*$/, '$1').replace(/[^a-zA-Z0-9-\._]/g, '');
	    username = username.toLowerCase();
	    $('username').value = username;
	  }
  }
}

var haveModifiedUsername = false;

function usernameFieldKeyUp() {
  haveModifiedUsername = true;
}

function passwordFieldKeyUp() {
  if ($('password_two').value == '') {
    $('invalid_passwords_msg').innerHTML = '';
    return;
  }
  
  var passwordsDontMatch = ($('password_one').value != $('password_two').value);
  
  $('invalid_passwords_msg').innerHTML = passwordsDontMatch ? '<em style="color: #a00;">Your passwords don\'t match</em>' : '<em style="color: #0a0;">Password accepted</em>';
}

function getNextAddressIdx() {
	var i=0;
	while($('idaccount_holder_address_'+i))
		i++;
	return i;	
}

function addAddress() {
	var i=getNextAddressIdx();

	if(i>5) {
		alert('Sorry, the maximum number of outlets is 6');
		return;
	}
	
  new Ajax.Request('signup.addaddress.'+i, {
  method: 'get',
  onSuccess: function(i, transport) {
    if(transport.responseText=='')
      return;
    addAddressCallback(i,transport.responseText);
  }.bind(this,i)
 });
	
}

function addAddressCallback (id,html) {
	$('add_address_row').insert({ before: html});
}


function accountModifyMultipleOutletsChanged() {
	var value=$('multiple_outlets').getValue();
  switch(value) {
	  case '1':
		  $('add_address_row').show();
		  $('main_address_heading').show();
		  if(getNextAddressIdx()==0)
		    addAddress();
		  break;
	  default:
		  $('add_address_row').hide();
		  $('main_address_heading').hide();
		  break;
  }
  elements=$$('.account_holder_address_row');
	elements.each(function(el) {
		(value==1) ? el.show() : el.hide() ;
	});
}

function accountModifyWholesalerChanged() {
	var value=$('wholesaler').getValue();
	
	//Hide and disable multiple outlets if wholesaler is on, otherwise show
	if(value=='1') $('multiple_outlets').setValue(0);
	accountModifyMultipleOutletsChanged();
  elements=$$('.multiple_outlets');
	elements.each(function(el) {
		(value=='1') ? el.hide() : el.show() ;
	});
}

function getLocationIdx(element) {
  var idbits=/_([0-9]*)$/.exec(element.id);
	if(!idbits)
    return false;
  return idbits[1];
}

function locationPostcodeChanged(el) {
  // do nothing if it isn't 4 characters
  if (el.getValue().length != '4')
    return;
    // update region selcet boxes based on the new postcode
  updateLocationRegions(el);
}

function updateLocationRegions(el) {
  var idx=getLocationIdx(el);
  var postcode = el.value;
  var idregion = $('idregion_'+idx).getValue();

  //this.fetchDataCallBack.bind(this, el, source, dataReadyCallback));

  sendAjaxMessage('signup', 'fetchRegionsAndSubregions', {'postcode':postcode, 'idregion':idregion}, this.updateLocationRegionsCallback.bind(this,idx) );
}

function updateLocationRegionsCallback(idx, response) {

  // update idregion select
  var regionsEl = $('idregion_'+idx);
  regionsEl.options.length = 0;
 
  for (var i = 0; i < response.regions.length; i++) {
    var idregion = response.regions[i].idregion;
    var title = response.regions[i].title;
    
    regionsEl.options[regionsEl.options.length] = new Option(title, idregion);
    if (idregion == response.idregion)
      regionsEl.options[regionsEl.options.length-1].selected = true;
  }
  
  // update idsubregion select
  var subregionsEl = $('idsubregion_'+idx);
  subregionsEl.options.length = 0;
  
  for (var i = 0; i < response.subregions.length; i++) {
    var idsubregion = response.subregions[i].idsubregion;
    var title = response.subregions[i].title;
    
    subregionsEl.options[subregionsEl.options.length] = new Option(title, idsubregion);
  }
  
  // update visibility of the select
  updateLocationRegionsVisible(idx);
}


function updateLocationRegionsVisible(idx) {
  if (document.getElementById('idregion_'+idx).length == 0) {
    x$(document).byClassName('valid_postcode_only_'+idx).hide();
  } else {
    x$(document).byClassName('valid_postcode_only_'+idx).show();
  }
}


function setupAccountModifyEvents() {
  $('wholesaler').observe('change', function(evnt) { accountModifyWholesalerChanged() } );
  $('multiple_outlets').observe('change', function(evnt) { accountModifyMultipleOutletsChanged() } );
  accountModifyWholesalerChanged();
}

Event.observe(window, 'load', function() {
	setupAccountModifyEvents();
});



