$(document).ready(function() {
	$('#submitButton').click(function(){
		$('#subscriptionForm').submit();
	});
	$('#cancelButton').click(function(){
		if (confirm('Are you sure you want to cancel the subscription process? Click OK to be taken back to the homepage.')) location.href = '/';
	});
	$('input.strSubscriptionLevelID').change(displayHardcopyOption);
	$('#intPaymentTypeID').change(displayPaymentOptions);
	displayHardcopyOption();
	displayPaymentOptions();
	$('#isValidEPUKNumber').css('display','block');
	$('#isValidEPUKNumber').click(checkEPUKNumber);
	if (($('div.container').height() + $('#containerHeader').height() + $('#containerFooter').height()) < $(window).height()) {
		$('div.container').height($(window).height() - $('#containerHeader').height() - $('#containerFooter').height() - 40);
	}
});

function displayHardcopyOption() {
	var selectedVal = $('input.strSubscriptionLevelID:checked').val();
	if (selectedVal == 'free') {
		$('#paymentOptions').prev().addClass('notVisible');
		$('#paymentOptions').addClass('notVisible');
		$('#hardCopy').addClass('notVisible');
	} else {
		$('#paymentOptions').prev().removeClass('notVisible');
		$('#paymentOptions').removeClass('notVisible');
		$('#hardCopy').removeClass('notVisible');
	}
}

function displayPaymentOptions() {
	if ($('#intPaymentTypeID').val() == 427) {
		$('tr.strPONumber').show();
	} else {
		$('tr.strPONumber').hide();
	}
}

function checkEPUKNumber() {
	var strEmailAddress = $('#strEmailAddress').val();
	var strEPUKMemNumber = $('#strEPUKMemNumber').val();
	var strSubscriptionLevelID = $('input:radio[name=strSubscriptionLevelID]:checked').val()
	var boolError = false;
	var strMessage = '';
	
	if (strEPUKMemNumber.length == 0) {
		boolError = true;
		strMessage = strMessage + 'You haven\'t entered a membership number';
	}
	
/*	if (strEmailAddress.length == 0) {
		boolError = true;
		if (strMessage.length > 0) {
			strMessage = strMessage + '\n\n';
		}
		strMessage = strMessage + 'We need you to enter a main contact email address in order to validate your membership number.';
	}*/
	
	if (boolError) {
		alert(strMessage);
	} else {
		$.ajax({
			type: "POST",
			timeout: 5000,
			data: "function=checkEPUKNumber&strEmailAddress=" + strEmailAddress + "&strEPUKMemNumber=" + strEPUKMemNumber + '&strSubscriptionLevelID=' + strSubscriptionLevelID,
			url: "/_ajax/",
			dataType: "json",
			success: showErrors,
			error: ajaxRequestFailed
		});
	}
}

function showErrors(r) {
	$('#strEPUKMemNumber').parent().prev().find('span').remove();
	$('#strEPUKMemNumber').parent().prev().append(r.MESSAGE);
	$('#subsOptions').html(r.SUBSOPTIONS);
}

function doNothing() {
}
