if (typeof(console) == 'undefined') {
	var console = {
			log: function() {}
	};
}
getID = function (str, delimiter, pos ) {            
    var strArray = str.split( delimiter );
    if (strArray.length < pos) {
        alert("Chyba: Nemohu načíst ID");
        return false;
    } else {
        return strArray[pos];
    }
};

$(document).ready(function() {	
	//$('input.time').mask('99:99');
	$('input.date').datepicker({dateFormat:'d M, yy'});	
	$('.tooltip').tooltip({showURL: false});
	
	vehicleSinglePrice = 0;
	vehicleReturnPrice = 0;
	
	$('#one_direction').change( function() {
		if ( $(this).val() == 1 ) {
			$('#one_direction_2').show();
			$('#one_direction_1').hide();
		};
		if ( $(this).val() == 0 ) {
			$('#one_direction_1').show();
			$('#one_direction_2').hide();
		};
	});
	$('#two_direction').change( function() {
		if ( $(this).val() == 1 ) {
			$('#two_direction_2').show();
			$('#two_direction_1').hide();
		};
		if ( $(this).val() == 0 ) {
			$('#two_direction_1').show();
			$('#two_direction_2').hide();
		};
	});
	$('#waytype').change( function() {
		if ( $(this).val() == 0 ) {
			$('#oneway').show();
			$('#twoway').hide();
			$('#showSingle').attr('value','1');
			$('#showReturn').attr('value','0');
		};
		if ( $(this).val() == 1 ) {
			$('#twoway').show();
			$('#oneway').hide();
			$('#showSingle').attr('value','0');
			$('#showReturn').attr('value','1');
		};
		setPrice();
	});
		
	$('.upgrades').click( function() {
		setPrice();
	});
	$('.upgradescounter').keyup( function() {		
		setPrice();
	});
	
	$('#id_vehicle').change( function() {
		var str = $('#id_vehicle').val();
		vehicleSinglePrice = getID( str,';',1);
		vehicleReturnPrice = getID( str, ';', 2);
		setPrice();
	});
	
	setPrice = function() {
		var price = 0;
		var upgrades = 0;
		if ( $('#showSingle').val() == 1 ) {
			price = vehicleSinglePrice;
		} else {
			price = vehicleReturnPrice;
		}
		$('.upgrades:checked').each( function(i,e) {
			var tmp = $(e).val();
			tmpPrice = getID( tmp,';', 1);
			tmpCounter = $(e).parent().prev().prev().find('input').val();			
			upgrades = upgrades + (Math.round( (parseFloat(tmpPrice) * parseInt(tmpCounter)) * 100) / 100 ); 
			//console.log( tmp );
		});
		//console.log( price );
		var pricetxt = parseInt(price) + parseFloat(upgrades);
		if( upgrades == 0 ) {
			$("#pricetext").html( pricetxt + '€ ');
		} else {
			$("#pricetext").html( price + '€ + '+ upgrades +'€ = ' + pricetxt + '€' );
		}
		$("#price").attr('value', pricetxt );
	}
	
	var str = $('#id_vehicle').val();
	vehicleSinglePrice = getID( str,';',1);
	vehicleReturnPrice = getID( str, ';', 2);
	$('#showSingle').attr('value','1');
	$('#showReturn').attr('value','0');
	setPrice();

	$("#upgradestoggleclick").click( function() {
		
		if ( $("#upgradestoggle").is(":visible") ) {
			$(this).html('(show)');
			$("#upgradestoggle").slideUp('fast');
		} else {
			$(this).html('(hide)');
			$("#upgradestoggle").slideDown('fast');
		}
	});
	
	//ODESLANI!
	
	$("#signupForm").submit(function() {
		var pass = true;
		$('input.required').each(function(e,i) {
				var value = $(this).val();
				if (value == '') {
					pass = false;
					$(this).addClass('wrong');
				} else {
					$(this).removeClass('wrong');
				}
		});
		$('textarea.required').each(function(e,i) {
				var value = $(this).val();				
				if (value == '') {
					pass = false;
					$(this).addClass('wrong');
				} else {
					$(this).removeClass('wrong');
				}
		});

		/*if (!$("input[type='checkbox']").is(":checked")) {
			EFF.uiMessage('Musíte souhlasit s podmínkami!');
			return false;
		}*/
		if (pass) {			

			var data = $("#signupForm").serialize();

			//EFF.ajaxLoader("#kontaktform","show");
	        	$.ajax({
	        		url: '/templates/actions/book.send.actions.php',
	        		dataType: "json",
	        		data: data,
	        		type: 'post',
	        		error: function(xhr, msg) {
	            		EFF.uiAlert("Při odesílání dat došlo k chybě.");
	            		//EFF.ajaxLoader("#kontaktform","hide");
	            	},
	            	success: function(data) {
	            		//EFF.ajaxLoader("#kontaktform","hide");
	            		if (data.status == "ok") {
		            		EFF.uiMessage('Transportation booking was sent and received.<br/>You will shortly find a '+
		            				'confirmation<br/>email in your inbox '+ data.email +'.<br/>Please make '+
		            				'sure it does not end up in the SPAM box. The same confirmation is '+
		            				'available in the browser window. Thank you for using Taxi & Transfers.',
		            				{
		            			title: 'Confirm message',		            			
			            		close: function() {
		            				window.location = data.url;
		            				//$('#uiMessage').dialog('close');
		            			}
		            		});
	            		} else {
	            			EFF.uiAlert(data.content);
	            		}
	            	}
	        	});
		} else {
			EFF.uiAlert('Some fields are required!');
		}
		return false;
	});

	
	
});