$(document).ready(function(){

	$('#v_call_atv,#v_consultation,#v_info_email,#v_info_post').change(function(){
		var v_call_atv = $('#v_call_atv:checked').length;
		var v_consultation = $('#v_consultation:checked').length;
		var v_info_email = $('#v_info_email:checked').length;
		var v_info_post = $('#v_info_post:checked').length;

		if ($('#v_address_1').val() == '') {
			$('#v_house,#v_address_1,#v_address_2,#v_town,#v_county,#postcode,#v_consult_date,#v_consult_time,#v_call,#v_info').parent().hide();
		}

		if (v_consultation || v_info_post) {
			//$('#v_house').parent().show();
		}
		if (v_info_post) {
			//$('#v_address_1,#v_address_2,#v_town,#v_county').parent().show();
		}
		if (v_consultation || v_info_email || v_info_post) {
			$('#postcode').parent().show();
		}
		if (v_consultation) {
			$('#v_consult_date,#v_consult_time').parent().show();
		}
		if (v_call_atv) {
			$('#v_call').parent().show();
		}
		if (v_call_atv || v_consultation) {
			$('#v_info').parent().show();
		}
	});
	if ($('#v_call_atv:checked,#v_consultation:checked,#v_info_email:checked,#v_info_post:checked').length == 0) {
		$('#v_consultation').click();
	}
	$('#v_consultation').change();
	
	$('#postcode').blur(function(){
		$v_address_1 = $('<select/>',{'id':'v_address_1','name':'v_address_1','disabled':'disabled'}).css({'width':'107px'}).append($('<option/>',{'value':''}).html('Loading...'));
		$('#v_address_1').replaceWith($v_address_1);
		$v_address_1.parent().show();
		$.getJSON('/contact/postcode.asp',{'postcode':$(this).val()},function(data) {
			// Remove Loading
			$v_address_1.empty();
			if (typeof data.error != 'undefined') {
				$v_address_1.replaceWith($('<p/>',{'id':'v_address_1'}).html(data.message));
			}
			if (typeof data.Address1 != 'undefined') {
				// Add Entries
				$v_address_1.append($('<option/>',{'value':''}).html('Please Select...'));
				$.each(data.Address1, function(i,item){
					$v_address_1.append($('<option/>',{'value':item}).html(item));
				});
				$v_address_1.append($('<option/>',{'value':''}).html('Not Listed - Enter Manually'));
				// Ready To Select
				$v_address_1.removeAttr('disabled').change(function(){
					if ($(this).val() == '') {
						$v_address_1.replaceWith($('<input/>',{'id':'v_address_1','name':'v_address_1','type':'text'}));
					}
					$('#v_address_2').val(((typeof data.Address2 != 'undefined' && $(this).val() != '') ? data.Address2 : '')).parent().show();
					$('#v_town').val(((typeof data.Town != 'undefined' && $(this).val() != '') ? data.Town : '')).parent().show();
					$('#v_county').val(((typeof data.County != 'undefined' && $(this).val() != '') ? data.County : '')).parent().show();
				});
			}
		});
		
	});

	$('#v_consult_date').datepicker({
		dateFormat:'dd/mm/yy',
		beforeShow:function(input,inst){
			setTimeout(function() {
				$('#ui-datepicker-div').css({'left':'5px','width':'15em'});
			}, 100);
		},
		beforeShowDay:function(date){
			return ((date.getDay() == 0) ? Array(false,'','*Please note that we are unable to take Sunday appointments') : Array(true,'',''));
		}
	});

});
