$(document).ready(function() {
	$('#check_adsl_form').submit(checkADSL);
	$('#check_adsl_button').click(function(evt){ 
		evt.preventDefault();
		$('#check_adsl_form').submit(); 
	});
	$('#line_number').focus(function() {
		if ($(this).val() == 'Enter your phone number...') {
			$(this).val('');
		}
	});
	$('#line_number').val('Enter your phone number...');
});

function checkADSL(evt) {
	try {
		var value = $('#line_number').val();

		if (value != '') {
			value = value.replace(/^\+/, '0').replace(/[^0-9]/g, '');
			$('#line_number').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_adsl_button').attr('disabled', 'disabled');
			$('#line_number').val('Checking your line - please wait...');
			$('#line_number').attr('disabled', 'disabled');
			$.ajax({
				url: 'backend.php?action=api',
				type: 'GET',
				data: { mode: 'json', q: 'validate/adsl_line/' + urlencode(value) },
				dataType: 'json',
				success: function(result) {
					if (result && result.result) {
						alert('Your Line supports ADSL');
						//TODO Leaving these here for now, but they are offer code wizard specific
						$('#firstCol').height('663px');
						$('#lineSpeedDiv').show();
						$('#sub').show();
					} else if (result == null) {
						alert('Could not check your Line at the moment. Please try again later.');
					} else {
						alert('Your Line does not support ADSL');
					}
					$('#line_number').val(value);
				},
				complete: function() {
					$('#check_adsl_button').removeAttr('disabled')
					$('#line_number').removeAttr('disabled')
				},
				error: function() {
					$('#check_adsl_button').removeAttr('disabled')
					$('#line_number').removeAttr('disabled')
				},
				timeout: 10000
			});
		}
	} catch(e) {
		alert('checkADSL: ' + e);
	}
	return false;
}

