$(function() {
	$("#short_date").datepicker();
	$("#short_time").timepickr();
	$('#shortBook').click(function() {
		var check = chechShortBook();
		if(check) $("form").attr("action", "/book_a_minicab/planner.html").submit();
	});
	$('#clearForm').click(function() {
		$('#frm')[0].reset();
	});
	$('input[name=short_type]:radio').click(function() {
		changeFromTo();
	});
});

function chechShortBook(){
	if($('#short_from').val() == "From Postcode"){
		alert("Please enter from postcode!");
		return false;
	}
	if($('#short_to').val() == "To Postcode"){
		alert("Please enter to postcode!");
		return false;
	}
	if($('#short_date').val() == "Pick a Date..."){
		alert("Please select a date!");
		return false;
	}
	if($('#short_time').val() == "Collection Time"){
		alert("Please select a time!");
		return false;
	}
	if($('#short_passangers').val() == "Number of Passengers"){
		alert("Please select a number of passengers!");
		return false;
	}
	
	return true;
}

function chechBook(){
	if($('#name').val() == ""){
		alert("Please enter your name!");
		$('#name').focus();
		return false;
	}
	if($('#surname').val() == ""){
		alert("Please enter to surname!");
		$('#surname').focus();
		return false;
	}
	if($('#email').val() == ""){
		alert("Please enter your email!");
		$('#email').focus();
		return false;
	}
	if($('#passangers').val() == "Number of Passengers"){
		alert("Please select a number of passengers!");
		$('#passangers').focus();
		return false;
	}
	if($('#collect_date').val() == "Pick a Date..."){
		alert("Please select a date!");
		$('#collect_date').focus();
		return false;
	}
	if($('#collect_time').val() == "Collection Time"){
		alert("Please select a time!");
		$('#collect_time').focus();
		return false;
	}
	if($('#collect_postcode').val() == ""){
		alert("Please enter collection postcode!");
		$('#collect_postcode').focus();
		return false;
	}
	if($('#destination_postcode').val() == ""){
		alert("Please enter destination postcode!");
		$('#destination_postcode').focus();
		return false;
	}
	
	return true;
}

function changeFromTo(){
	var val = $('input:radio[name=short_type]:checked').val();
	if(val == "minicab"){
		$('#short_leaving_from').html('');
		$('#short_going_to').html('');
		var leaving_from = addTextBox("short_from", "short_from", "From Postcode", '175');
		leaving_from.onclick = function() { 
			if(leaving_from.value == "From Postcode") leaving_from.value = "";
		};
		leaving_from.onblur = function() { 
			if(leaving_from.value == "") leaving_from.value = "From Postcode";
		};
		$('#short_leaving_from').append(leaving_from);
		var going_to = addTextBox("short_to", "short_to", "To Postcode", '175');
		going_to.onclick = function() {  
			if(going_to.value == "To Postcode") going_to.value="";
		};
		going_to.onblur = function() { 
			if(going_to.value == "") going_to.value = "To Postcode";
		};
		$('#short_going_to').append(going_to);
		
	}
	if(val == "airport"){
		$('#short_going_to').html('');
		$('#short_leaving_from').html('');
		var leaving_from = addSelectObject("short_from", "short_from", airports);
		$('#short_leaving_from').append(leaving_from);
		var going_to = addSelectObject("short_to", "short_to", airports);
		$('#short_going_to').append(going_to);
	}
}

function addSelectOption(selectbox, value, text) {
	var opt = document.createElement('OPTION');
	opt.value = value;
	opt.innerHTML = text;
	selectbox.appendChild(opt);
}
function addSelectObject(obj_id, obj_name, data) {
	var obj = document.createElement('SELECT');
	obj.id = obj_id;
	obj.className = "bkFrmCombo";
	obj.name = obj_name;
	for( var prop in data ) {
		addSelectOption(obj, data[prop], data[prop]);
	}
	return obj;
}

function addTextBox(id, name, val, size) {
	var inp = document.createElement("INPUT");
	inp.type= 'text';
	inp.value = val;
	inp.style.width = size+"px";
	inp.name = name;
	inp.id = id;
	inp.className = "bkFrmText";
	return inp;
}
