$(document).ready(function() {
	$('#check_email_form').submit(checkEMAIL);
	$('#check_email_button').click(function(evt){ 
		evt.preventDefault();
		$('#check_email_form').submit(); 
		});
	$('#email_address').focus(function() {
		if ($(this).val() == 'yourname@' + bisp_domain) {
			$(this).val('@' + bisp_domain);
		}
	});
	$('#email_address').val('yourname@' + bisp_domain);
});

function checkEMAIL(evt) {
	try {
		var value = $('#email_address').val();

		if (value != '') {
			//value = value.replace(/^\+/, '0').replace(/[^0-9]/g, '');
			$('#email_address').val(value);
			//$('#internet_AdslPhone_status').html('&nbsp;&nbsp;<img style="vertical-align: middle;" src="/library/images/loading1.gif" alt="Loading, please wait..." title="Checking, please wait..." />');
			$('#check_email_button').attr('disabled', 'disabled');
			$('#email_address').val('Checking - please wait...');
			$('#email_address').attr('disabled', 'disabled');
			$.ajax({
				url: 'backend.php',
				type: 'GET',
				data: { action: 'api', q: 'mailbox/check_available/' + urlencode(value) },
				dataType: 'json',
				success: function(result) {
					if (result.result) {
						alert('Email available!');
						//TODO Leaving these here for now, but they are offer code wizard specific
						//$('#firstCol').height('663px');
						//$('#lineSpeedDiv').show();
						//$('#sub').show();
					} else {
						alert('Email address not available!');
					}
					$('#email_address').val(value);
				},
				complete: function() {
					$('#check_email_button').removeAttr('disabled')
					$('#email_address').removeAttr('disabled')
				},
				error: function() {
					$('#check_email_button').removeAttr('disabled')
					$('#email_address').removeAttr('disabled')
				},
				timeout: 10000
			});
		}
	} catch(e) {
		alert('checkEMAIL: ' + e);
	}
	return false;
}

