var join_validation; // created in init_join_form
var captcha_shown = false;

function do_join() {
	if (!join_validation.validate()) {
		return false;
	}

	$('process').show();
	$('submit_button').hide();

	var tmp = $(join_validation.form.name).serialize(true);
    var tmpargs = $H(tmp);

    new Ajax.Request('/a_join.php', {
		parameters: tmpargs,
		onSuccess: function(resp) { 
			process_response(resp);
        }
    });
}

//RWH
function uni2ent(srcTxt) {
  var entTxt = '';
  var c, hi, lo;
  var len = 0;
  for (var i=0, code; code=srcTxt.charCodeAt(i); i++) {
    var rawChar = srcTxt.charAt(i);
    // needs to be an HTML entity
    if (code > 255) {
      // normally we encounter the High surrogate first
      if (0xD800 <= code && code <= 0xDBFF) {
        hi  = code;
        lo = srcTxt.charCodeAt(i+1);
        // the next line will bend your mind a bit
        code = ((hi - 0xD800) * 0x400) + (lo - 0xDC00) + 0x10000;
        i++; // we already got low surrogate, so don't grab it again
      }
      // what happens if we get the low surrogate first?
      else if (0xDC00 <= code && code <= 0xDFFF) {
        hi  = srcTxt.charCodeAt(i-1);
        lo = code;
        code = ((hi - 0xD800) * 0x400) + (lo - 0xDC00) + 0x10000;
      }
      // wrap it up as Hex entity
      c = "&#x" + code.toString(16).toUpperCase() + ";";
    }
    else {
      c = rawChar;
    }
    entTxt += c;
    len++;
  }
  return entTxt;
}

function process_response(resp) {
	$('process').hide();
	$('submit_button').show();
	var response = resp.responseJSON;
	if (!response) {
		show_error('main', 'Error Contacting Server.  Please Try Again');
		return false;
	}
	if (response.captcha) {
		if (typeof pageTracker != 'undefined' && !captcha_shown) {
			pageTracker._trackEvent("Form_"+join_validation.form.name, 'Action::captcha_shown', 'join');
			captcha_shown = true;
		}
		var div=$('captcha_top');
		if (!div) {
			show_error('main', 'Captcha Missing Error');
			return false;
		}
		div.show();
		div.update(response.captcha);
		try {
			$('captcha').focus();
		}
		catch(e) {
		}

	}
	if (response.errors) {
		for (var i=0; i<response.errors.length; ++i) {
			var error = response.errors[i];
			show_error(error.field, error.error);
		}
		return false;
	}
	else if (response.redirect) {
		if (typeof pageTracker != 'undefined') {
			pageTracker._trackEvent("Form_"+join_validation.form.name, 'Success');
		}
		$('submit_button').hide();
		window.location.href = response.redirect;
		return true;
	}
	else if (!response.captcha) {
		show_error('main', 'Incorrect Response From Server.  Please Try Again');
		return false;
	}
}

function show_error(field, error) {
	join_validation.drawFail('other', $(field), error);
}

function enter_submit(e) {
	if ($('submit_button').visible && (window.event && window.event.keyCode == 13 || e && e.which == 13)) {
		do_join();
	}
	return true;
}   

function update_captcha(id) {
	if (!id) {
		return false;
	}
	if (typeof pageTracker != 'undefined') {
		pageTracker._trackEvent('join_form', 'Action::captcha_refresh');
	}
    new Ajax.Updater(id, '/a_getcaptcha.php');
}

function init_join_form() {
	join_validation = new Validation(join_form_name, {useTitles:true, onSubmit:false});
}

document.observe('dom:loaded', init_join_form);
