if (typeof(console) == 'undefined') {
	var console = {
			log: function() {}
	};
}
getID = function (str, pos, delimiter ) {            
    var strArray = str.split( delimiter );
    if (strArray.length < pos) {
        alert("Chyba: Nemohu načíst ID");
        return false;
    } else {
        return strArray[pos];
    }
};

//#ci2c1w1d0
//console.log( window.location.hash );
var type = window.location.hash;
var re = new RegExp( '([cp])i([0-9]+)c([01]+)w([01]+)d([01]+)' );
var ma = re.exec( type );
var attr = [];
if (ma == null) {
 // alert("No match");
} else {
 // var s = "Match at position " + ma.index + ":\n";
  for (i = 0; i < ma.length; i++) {
 //   s = s + ma[i] + "\n";
    if ( i != 0 ) {
    attr[i-1] = ma[i];
    }
  }  
 //console.log( s );
}
//console.log( attr );


$(document).ready(function() {	
	$('input.time').mask('99:99 a*');
	$('input.wtime').mask('99:99');
	$('input.date').datepicker({dateFormat:'d M, yy'});	
	$('.tooltip').tooltip({showURL: false});
		
	setPrice = function() {
		var value = $("#transfer").val();
		var value = value.split( ';' );		
		var perKM = 0;
		if ( $('#car').val() == 1 ) perKM = PRICE_KM_MINIBUS; else perKM = PRICE_KM_SEDAN;
		var distance = value[1];		
		if ( $('#way').val() == 1 && $('#day').val() == 1 ) distance = distance * 2;
		var discount = value[2];
		var fees = 0;		
		var showFees = 0;	
		//console.log( $('#showWait').val(), 'showwait');
		if ( $('#showWait').val() == 1) {
			var strtime = $('#cts_wait').val();
			var arraytime = strtime.split(':');
			if ( arraytime.length == 2 ) {
				var minutes = ((parseInt(arraytime[0]*60)) + parseInt(arraytime[1]));					
				var fees = Math.round( ONE_MINUTE_WAITING * minutes );
				var showFees = 1;
			} 
		}
		if ( $('#showWait').val() == 0 && $('#showTwoWaySameDay').val() ==1) {
			 distance = distance *2;
		}
				
		price = Math.round(( ( (perKM * distance) - (perKM * distance * discount) ) * PERMANENTLY_DISCOUNT ));
		
		if (price == 0) {
			pricetext = 'on request';
		} else {
			var pricetext = price + '€ ';
			var pricevalue = price;
			if (showFees == 1) {
				pricetext += '+ ' + fees + '€ ';
				pricevalue += fees;
			}
		}
		$("#pricetext").html(pricetext );
		$("#sumprice").attr('value',pricevalue);
		$("#price").attr('value',pricetext);
		//console.log('setprice');
	};

	// 0 = c|p city or package
	// 1 = ID city or package
	// 2 = 0|1 sedan or bus
	// 3 = 0|1 way one or two
	// 4 = 0|1 day return way same or diferent	
	
	setValue = function( id, value) {
		//console.log( 'setValue', id, value);
		$('#'+id).attr('value', value);
	};
	$("#way_cont").hide();
	$('#wait_cont').hide();
	$('#day_cont').hide();
	$('#two_way').hide();
	setValue('showWay', 0);
	setValue('showWait', 0);
	setValue('showday', 0);
	setValue('showTwoway', 0);
	
	
	checkSelect = function( skipAutoaddress ) {
		var value = $("#transfer").val();
		var value = value.split( ';' );
	
		if (!skipAutoaddress) {
			$(".autoaddress2").attr('value', value[4] );
			$(".autoaddress1").attr('value', 'Prague' );
			$(".autoaddresscity2").attr('value', value[4] );
			$(".autoaddresscity1").attr('value', 'Prague' );
		}
				
		$("#hasreturn").attr('value', (value[3]));
		
		if ( value[3] == 0) { //neni return transfer
			
			$('#city_oneway').hide();
			$('#city_twoway_sameday').hide();
			$('#city_twoway_diffday').hide();
			$('#package').show();
			
			$('#showOneWay').attr('value',0);
			$('#showTwoWaySameDay').attr('value',0);
			$('#showTwoWayDiffDay').attr('value',0);
			$('#showPackages').attr('value',1);
			
			setValue('showTwoway', 0);
			$('#two_way').slideUp('fast');
			$("#way option:first").attr('selected', true);
			$("#way_cont").slideUp('fast');
			setValue('showWay', 0);
			$('#day option:eq(0)').attr('selected', true);
			$('#day_cont').slideUp('fast');
			setValue('showDay', 0);
			$('#wait').attr('value','00:00');
			$('#wait_cont').slideUp('fast');
			setValue('showWait', 0);			
			
		} else { //je moznost return transferu
			
			$('#city_oneway').hide();
			$('#city_twoway_sameday').hide();
			$('#city_twoway_diffday').hide();
			$('#package').hide();
			$('#showOneWay').attr('value',0);
			$('#showTwoWaySameDay').attr('value',0);
			$('#showTwoWayDiffDay').attr('value',0);
			$('#showPackages').attr('value',0);
			
			$("#way_cont").slideDown('fast');	
			setValue('showWay', 0);
			//$('#showOneWay').attr('value',1);
			
			
			$("#way option").eq( attr[3] ).attr('selected', true );			
			if ( $('#way').val() == '0' ) { //one way transfer
				$('#city_oneway').show();
				$('#showOneWay').attr('value',1);
				//console.log('zjisti typ');
				
				$('#day option:eq(0)').attr('selected', true);
				$('#day_cont').slideUp('fast');
				//setValue('showDay', 0);
				$('#wait').attr('value','00:00');
				$('#wait_cont').slideUp('fast');
				setValue('showWait', 0);
				$('#two_way').slideUp('fast');
				//setValue('showTwoway', 0);
			} else {
				$('#two_way').slideDown('fast');
				setValue('showTwoway', 1);
				if ( value[1] * 2 > MAX_IN_ONE_DAY) { // je zvolen two way, ale cesta je moc dlouha, takze navrat jiny den a bez cekani
					$('#city_twoway_diffday').show();
					$('#showTwoWayDiffDay').attr('value',1);
					$('#day option:eq(1)').attr('selected', true);
					$('#day_cont').slideDown('fast');
					//setValue('showDay', 1);
					$('#day').attr('disabled', 'disabled');
					$('#wait').attr('value','00:00');
					$('#wait_cont').slideUp('fast');
					setValue('showWait', 0);
				} else {		// navrat muze byt ten samy den, pak se ale ceka, jinak jiny den a bez cekani			
					$('#day_cont').slideDown('fast');
					//setValue('showDay', 1);
					$('#day').attr('disabled', false);
					$("#day option").eq( attr[4] ).attr('selected', true );
					if ( $('#day').val() == 0 ) {
						$('#city_twoway_sameday').show();
						$('#showTwoWaySameDay').attr('value',1);
						//$('#wait').attr('value','00:30');
						//$('#wait_cont').slideDown('fast');
						var re1=/Prag/g;
						var str = $('input[name=cts_pickupaddress]').val();
						var result1= str.match(re1);
						//console.log('resutl:',result1);
						//console.log('value cts_pickupaddress',str);
						if (result1 != null) {
							//console.log( 'nenasel sem v pickup address');
							setValue('showWait', 1);
						} else {
							setValue('showWait', 0);	
						}
					} else {
						//console.log('Return diff day');
						//console.log($('#city_twoway_diffday').size());
						$('#city_twoway_diffday').show();
						$('#showTwoWayDiffDay').attr('value',1);
						//$('#city_twoway_diffday').show();
						//$('#wait').attr('value','00:30');
						//$('#wait_cont').slideUp('fast');
						setValue('showWait', 0);
					}
				}				
			}
			
		}
		setPrice();
		
		
	};	
	
	setDefault = function() {
		$('#car option[value^="'+attr[2]+'"]').attr('selected', true);
		$('#transfer option[value^="'+attr[0]+attr[1]+';"]').attr('selected', true);		
		$('#transfer_text').attr('value', $('#transfer option:selected').text() );
		checkSelect( false);
	};
	setDefault();
	
	$("#car").change(function() {				
		checkSelect( false);
	});
	$("#transfer").change(function() {				
		$('#transfer_text').attr('value', $('#transfer option:selected').text() );
		checkSelect( false);
	});
	$("#way").change(function() {	
		attr[3] = $(this).val();
		checkSelect( false);
	});
	$("#day").change(function() {
		attr[4] = $(this).val();
		checkSelect(false);
	});
	$("#cts_wait").blur(function() {
		setPrice(false);		
	});
	
	$("#cts_returnaddress").change(function() {
		if ($(this).val() == 1) {
			$(".cts_returnaddress_detail").fadeIn();
		} else {
			$(".cts_returnaddress_detail").fadeOut();
		}
	});
	
	$(".switchimg").click( function() {
		var o = $('fieldset:visible');
		var tmp = $(".autoaddress1:first",o).val();
		//console.log( tmp );
		//console.log($('.autoaddress2:first',o).val());
		$(".autoaddress1",o).attr('value', $('.autoaddress2:first',o).val() );
		$(".autoaddress2",o).attr('value',tmp );		
		var tmp = $(".autoaddresscity1:first",o).val();	
		$(".autoaddresscity1",o).attr('value', $('.autoaddresscity2:first',o).val() );
		$(".autoaddresscity2",o).attr('value',tmp );
		//setPrice();
		checkSelect(true);
		//return true;
	});

	//ODESLANI!
	
	$("#signupForm").submit(function() {
		var pass = true;
		var msg = true;
		$('fieldset:visible input.required').each(function(e,i) {
				var value = $(this).val();
				if (value == '') {
					pass = false;
					$(this).addClass('wrong');
					//console.log( $(this).attr('name') );
				} else {
					$(this).removeClass('wrong');					
				}
		});
		$('fieldset:visible textarea.required').each(function(e,i) {
				var value = $(this).val();				
				if (value == '') {
					pass = false;
					$(this).addClass('wrong');
					//console.log( $(this).attr('name') );
				} else {			
					$(this).removeClass('wrong');
				}
		});
		
		var address1 = $('fieldset:visible input[name$=pickupaddress]').val();
		var address1check = $('fieldset:visible input[name$=pickupaddresscity]').val();
		var address2 = $('fieldset:visible input[name$=dropoffaddress]').val();
		var address2check = $('fieldset:visible input[name$=dropoffaddresscity]').val();

		$('fieldset input[name$=dropoffaddress]').removeClass('wrong');
		$('fieldset input[name$=pickupaddress]').removeClass('wrong');
		if ( typeof(address1) != 'undefined' ) {
			if ( address1 == address1check ) {
				EFF.uiAlert('You must specify pick up point!');
				$('fieldset:visible input[name$=pickupaddress]').addClass('wrong');
				pass = false;
				msg = false;
			}
		}
		
		if (typeof(address2) != 'undefined') {
			if ( address2 == address2check ) {
				EFF.uiAlert('You must specify drop off point!');
				$('fieldset:visible input[name$=dropoffaddress]').addClass('wrong');
				pass = false;
				msg = false;
			}
		}

		/*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") {
	            			//$('#kontaktform').clearForm();
	            			//$('#captchaimg').attr("src",'/views/templates/actions/captcha.actions.php?'+Math.random(100,999));
		            		EFF.uiMessage(data.uimessage,
		            				{
		            			title: 'Confirm message',		            			
			            		close: function() {
		            				window.location = data.url;
		            				//$('#uiMessage').dialog('close');
		            			}
		            		});
	            		} else {
	            			EFF.uiAlert(data.content);
	            			//$('#captchaimg').attr("src",'/views/templates/actions/captcha.actions.php?'+Math.random(100,999));
	            		}
	            	}
	        	});
		} else {
			if (msg) {
				EFF.uiAlert('Some fields are required!');
			}
		}
		return false;
	});

	
	
});
