// functions for dealing with joining the site

function jnr_domain_from_name(name) {
  // full clean without spaces
  var stripped = name.gsub(/[^a-zA-Z0-9- ]+/, '');
  stripped = stripped.gsub(' ', '-');
  stripped = stripped.gsub('--+', '-');
  stripped = stripped.gsub(/^-/, '');
  stripped = stripped.gsub(/-$/, '');
  stripped = stripped.toLowerCase();

  return stripped;
}

var jnr_last_checked = 'nada';

function jnr_update_site_location() {
  if (jnr_last_checked != $('un_field').value)
    jnr_domain_mark('nothing');

  var name = $('un_field').value;
  var stripped = jnr_domain_from_name(name);

  // just pretend like they haven't done anything if there's no good chars to work with
  if (stripped == '')
    stripped = 'username';

  $('new-home').innerHTML = 'http://' + stripped + '.photoswarm.com'
}

function jnr_check_domain() {
  // do the normal html updating
  jnr_update_site_location();
  
  var name = $('un_field').value;
  jnr_last_checked = name;
  var stripped = jnr_domain_from_name(name);

  if (stripped != '') {
    jnr_domain_mark('checking');
    new Ajax.Request('/signup/check_subdomain.json?domain=' + encodeURIComponent(stripped), {
        method    : 'get'
      , onSuccess : jnr_check_domain_onSuccess
      , onFailure : jnr_check_domain_onFailure
    });
  } else
    jnr_domain_mark('invalid');
}

function jnr_check_domain_onSuccess(t) {
  var result = t.responseJSON;
  
  if (result.domain_exists)
    jnr_domain_mark('exists');
  else
    jnr_domain_mark('free');
}

function jnr_check_domain_onFailure() {
  jnr_domain_mark('nothing');
}

function jnr_domain_mark(mark_type) {
  // hide any displayed icons
  $$('.un-status').invoke('hide');
  
  // just display the appropriate one
  switch(mark_type) {
    case 'checking':
      $('status-checking').show();
      break;
    case 'free':
      $('status-available').show();
      break;
    case 'exists':
      $('status-unavailable').show();
      break;
    case 'error':
      $('status-error').show();
      break;
    case 'nothing':
      $('status-nothing').show();
      break;
    case 'valid':
      $('status-valid').show();
      break;
    default:
      $('status-invalid').show();
  }
}




// DOMAIN STUFF

var last_req_id = 0;
function check_domain() {
  last_req_id++;
  var req_id = last_req_id;
  
  var domain = $('domain_field').value;
  var tld = $('tld_field').value;

  var a = new Ajax.Request('/my_account/upgrade/check_domain.json',
  {
    asynchronous  : true, 
    parameters    : Object.toQueryString({domain:domain, tld:tld}),
    onSuccess     : function(t) { check_domain_suc(t, req_id); },
    onFailure     : function(t) { check_domain_fail(req_id); }
  });
}

function check_domain_suc(t, req_id) {
  var result = t.responseJSON;
  
  if (last_req_id != req_id)
    return;
  
  if (result.success)
    if (result.available)
      jnr_domain_mark('free');
    else
      jnr_domain_mark('exists');
  else if (result.error == 'bad_domain')
    jnr_domain_mark('invalid');
  else
    jnr_domain_mark('error');
}
function check_domain_fail(req_id) {
  if (last_req_id != req_id)
    return;

  jnr_domain_mark('error');
}


var run_auto_check = null;

function jnr_auto_check() {
	jnr_domain_mark('checking');
	
	// check to see if the domain is available
	check_domain();
}

jnr_last_checked_dom = 'nada';
// called whenever the domain changes
function jnr_update_domain() {
  var name = $('domain_field').value;
	var tld = $('tld_field').value;

	// keep a track on whether it's actually changed
	if (jnr_last_checked_dom == (name + tld))
		return;
	jnr_last_checked_dom = (name + tld);

	// did they even enter a domain?
	if (name.length == 0) {
		jnr_domain_mark('nothing');
		return;
	}

	// is it valid?
	if (/^([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])|[a-zA-Z0-9]$/.match(name) == false || name != name.gsub(' ', '')) {
		// nope
		jnr_domain_mark('invalid');
		return;
	} else {
		jnr_domain_mark('valid');
	}

	// update the internal text
  var full_dom = 'www.' + name + tld;
  $$('.sel-dom').each(function(elm){
		elm.innerHTML = full_dom;
	});

	// set up the stuff to check the availibility of the domain in a second
	if (run_auto_check)
		clearTimeout(run_auto_check);
	run_auto_check = setTimeout(jnr_auto_check, 800);
}
