



jQuery(document).ready(function($){

  $('#forgot-password').click(function(){
     $('.login-form').toggle();
     $('.recover-password').toggle(); 
  });

if($('fieldset.flygalt').length){
$('fieldset.flygalt').find('input[type=radio]').click(function(){
$("input[type=radio]").not( $(this).parents('fieldset').find('input') ).attr('checked', false);
});

$('fieldset.flygalt input.aButton').click(function(){
	$('fieldset.flygalt').removeClass('error');
	var checkedBox = 0;
		var is_checked = true;
		$(this).parent('fieldset.flygalt').find('input[type=radio]').each(function(){
			if($(this).is(':checked')){
				checkedBox += 1;
			}
		});
		if (checkedBox==2){
			$(this).parent('fieldset').removeClass('error');
$('#fading, #popuppen, #waiting').show();
		}else{
			alert('Välj ett utrese- och ett hemresedatum.');
			$(this).parent('fieldset').addClass('error');
			return false;
		}
});
}


if($('fieldset#flygmat').length){

	$('fieldset#flygmat').find('select').change(function(){
		if($(this).find("option:selected").attr('title') != "0"){
			$(this).removeClass('error');
		}
		
	});

}



/*
$('input.waiting').click(function(){
//$('#popuppen, #waiting, #fading').show();	
});
*/
$('#sortby').change(function(){
$('#popuppen').html("<div id='fading' style='display: block;'></div><div class='popupruta' id='waiting' style='display: block; margin-top: -56px; margin-left: -140px;'><img src='/img/ajax-loader.gif' alt='Progress'><p>Vänligen vänta</p></div>");
$('#popuppen').show();
$('#waiting img').attr('src', '/img/ajax-loader.gif');
});

$("[name=kombodatum]").click(function() {
$('td.bluemark').removeClass("bluemark");
$(this).closest('tr').find('td:first-child').addClass("bluemark");
});

$('#slider1').each(function(){ 
	var s1tid1 = parseInt($(".s1tid1").val());
	var s1tid2 = parseInt($(".s1tid2").val());
  $("#slider1").slider({
        range: true,
        min: parseInt($(".s1mintid").val()),
        max: parseInt($(".s1maxtid").val()),
		step: 60,
        values: [s1tid1, s1tid2],
        slide: slideTime1
 });
	  $(".s1tid1").val($("#slider1").slider("values", 0));
	  $(".s1tid2").val($("#slider1").slider("values", 1));
});

$('#slider2').each(function(){ 
	var s2tid1 = parseInt($(".s2tid1").val());
	var s2tid2 = parseInt($(".s2tid2").val());
  $("#slider2").slider({
        range: true,
        min: parseInt($(".s2mintid").val()),
        max: parseInt($(".s2maxtid").val()),
		step: 60,
        values: [s2tid1, s2tid2],
        slide: slideTime2
 });

});
$('#slider3').each(function(){ 
	var reglage1 = parseInt($(".reg1").val());
	var reglage2 = parseInt($(".reg2").val());
  $('#slider3').slider({
        range: true,
		min: parseInt($(".minprice").val()),
		max: parseInt($(".maxprice").val()),
		step: 500,
        values: [reglage1, reglage2],
        slide: function(event, ui) {

			$(".reg1").val(ui.values[0]);
			$(".reg2").val(ui.values[1]);
	
			$("#pricerange").text(ui.values[0] +"kr"+ " - " + ui.values[1] + "kr");

		}
   });
   	
  	  $(".reg1").val($("#slider3").slider("values", 0));
	  $(".reg2").val($("#slider3").slider("values", 1));


	  $("#pricerange").text($("#slider3").slider("values", 0) +"kr"+" - "+ $("#slider3").slider("values", 1) + "kr");

});

$("#submitbutton").click(function() {
$(':submit', this).click(function() {
        return false;

});
if ($('#resevillkor:checked').length) {
	$(this).submit();
}else{
$(this).parent('form').children('#resevillkor, a').css('border', '1px solid #EF3C41');
return false;
}

});





$("#imgbtnSend2").click(function() {

var errors = 0;
var texten = "";
var avbest = "";
var flygmat = "";
var transfer = "";
var avbesterr = 0;
	$("#fading, #popuppen, #waiting").show();
$('#popup').html("<h2>Vi vill uppmärksamma dig på att ni valt bort ett eller flera tillval</h2><div id='avbest'></div><div id='flygmat'></div><div id='transfer'></div><div id='left' class='control'></div><div id='right' class='control'></div>");
if ($('input.avb:checkbox:not(:checked)').length > 0) { 
errors = errors + 1;
	avbesterr = avbesterr + 1;
}

if(avbesterr > 0){
	$('#avbest').load('/smallinfo.html?'+ (new Date()).getTime() + ' #avbestallningsf', function() {
		var barn = "";
		if($('.chd').length){barn = ', '+$('.chd').val()+':-/barn';}
		var text = '<p><strong>Pris:</strong> ' + $('.adt').val() + ':-/vuxen'+ barn + '</p>';
		$('#avbestallningsf div').append(text);
		errors = errors + 1;
	});	
}
if ($('[name=transfer]').length) {
	if( $('[name=transfer]:checked').attr('alt') == 0){
		errors = errors + 1;
		$('#transfer').load('/smallinfo.html?'+ (new Date()).getTime() + ' #transfertext', function() {
			var transfertext = '<p><strong>Pris:</strong> '+ $('input[name=transfer]').parent('li:nth-child(2)').text() +'</p>';
			$('div#transfertext').append(transfertext);
		});
	}
}
if ($('fieldset#flygmatfs').length) {
if($('fieldset#flygmatfs').find('[alt=toggler_hide]').is(':checked')){
			$('#flygmat').load('/smallinfo.html?'+ (new Date()).getTime() + ' #flygmat');
			errors = errors + 1;
}else if($('fieldset#flygmatfs').find('[alt=toggler_show]').is(':checked')){
		$('fieldset#flygmatfs').find('option:selected').each(function(){
			if ($(this).attr('title') == 0){
				$(this).parent().addClass('error');
				$('#popuppen').hide();
			}else{
				$(this).parent().removeClass('error');
				$('#flygmatfs ul.errorlist').hide();
			}

		});


		if($('fieldset#flygmatfs').find('select').hasClass('error')){
			$('#flygmatfs ul.errorlist').show();
			alert('Ange typ av flygmat för varje resenär - väljer ni flygmat måste hela sällskapet ha någon typ av flygmat.');
			$('#popuppen, #waiting, #fading').hide();
			return false;
		}
}
}

$('#right').load('/smallinfo.html #rightbutton', function() {
	$('#rightbutton input').click(function(){
		$("#popup").hide();
	$("#waiting, #fading, #popuppen").hide();
	$('#tillaggblock').submit();
	});	
});
$('#left').load('/smallinfo.html #leftbutton', function() {
	$('#leftbutton input').click(function(){
	$("#popup").hide();
	$("#waiting, #fading, #popuppen").hide();
	});		
});

if(errors == 0){
	$('#popup').hide();
	$("#popuppen, #waiting").hide();
	$('#tillaggblock').submit();

}else{
	$("#waiting").hide();
	$('#popup').show();
	$('#popup').center();
}
});
if($('a[name=visa]').length){
$('#comment').css({position:"fixed",top:"0px"})
window.location.hash = 'visa';
}

moveScroller(); 

function moveScroller() {

   var a = function() {
   	var b = $(window).scrollTop();
   	var d = $("#commentWrapper").offset().top;

   	var c = $("#comment");
c.css({position:"relative",top: d})
     if (b>d) {
       c.css({position:"fixed",top:"0px"})     
     } else {
	if (b<=d) {
         c.css({position:"relative",top:""})
	}     
     }
};
if($('#comment').length){
$(window).scroll(a);
}
} 

if($('#showcareof').is(':checked')){
$("#careof").show();
$("#showcareof").hide();
}else{
$("#careof").hide();
}
$('#showcareof').click(function() {
    $("#careof").show(this.checked);
	$("#showcareof").hide();
});

 if(!$("ul.menu17").length > 0){
checkCurrent();
 }
function checkCurrent() {

  // Display "Found" if any divs in the page have a class called "summary"
  if(!$("ul.menu17").length > 0){
	  $('.tab_content').show();  
  }else if ( $('li').hasClass('current')){
	var activeTab = $('li.current').find("a").attr("href");
	$(activeTab).addClass('active');  
  }else{
	$(activeTab).removeClass('active'); 
  }
  
}

$('div.nittio select').change(function(){
var val = $(this).val();
var spelm = $(this).attr("name").split('_');
var uid = spelm[1];
	if(val != "UM" && val != "UF" && val != "C"){
		$('div.agebox'+uid+' select').removeAttr('disabled').removeClass('error');
		$('div.requiredagebox'+uid+' select').attr('disabled', true);
		$('div.requiredagebox'+uid+' select').attr('name', 'null');
		$('div.agebox'+uid+' select#year').attr('name', 'year_'+uid);
		$('div.agebox'+uid+' select#month').attr('name', 'month_'+uid);
		$('div.agebox'+uid+' select#day').attr('name', 'day_'+uid);
		$('div.requiredagebox'+uid).hide();
		$('div.agebox'+uid).show();
		$('.agebox'+spelm[1]).find('select').val('');
	}else{
		$('div.agebox'+uid).hide();
		$('div.requiredagebox'+uid).show();
		$('div.requiredagebox'+uid+' select').removeAttr('disabled');
		$('div.agebox'+uid+' select').attr('disabled', true);
		$('div.agebox'+uid+' select').attr('name', 'null');
		$('div.requiredagebox'+uid+' select#year').attr('name', 'year_'+uid);
		$('div.requiredagebox'+uid+' select#month').attr('name', 'month_'+uid);
		$('div.requiredagebox'+uid+' select#day').attr('name', 'day_'+uid);
		$('.requiredagebox'+spelm[1]+' #year').val($('.agebox'+spelm[1]+' #year').val());
		$('.requiredagebox'+spelm[1]+' #month').val($('.agebox'+spelm[1]+' #month').val());
		$('.requiredagebox'+spelm[1]+' #day').val($('.agebox'+spelm[1]+' #day').val());
		
	}



});

if($('#huvudblock').length){
$('#huvudblock').atvalidator({}, {
	'.required' : 'required',
	'#gatuadress' : 'minlength_adress(3)',
	'#kundort' : 'minlength_ort(1)',
	'#epost' : 'email',
	'#bek_epost' : 'match(email)',
	'#agentmobil' : 'number',
	'#postnr' : 'rangelength(4, 5), number',
	'#wish' : 'maxlength(150)',
	'#sex' : 'sex',
	'#mottagare' : 'minlength_namn(2)',
	'#fornamn' : 'fornamn(2)',
	'#bedwish' : 'bedwish',
	'#efternamn' : 'surname(2)'

	
});
}
$(".view-code2").click(function() {
	var rel = $(this).attr("rel");
	$(".tool_block[id=" + rel + "], .view-code2[id=" + rel + "]").toggle();
	$("a.view-code2[rel=" + rel + "]").parent().toggle();
}); 

$(".visa").click(showHide);

$("#bek_epost").bind('paste', function(e) {
        var el = $(this);
        setTimeout(function() {
        $(el).val('');
        });
});

if($('fieldset * input[alt=toggler_show]').is(':checked')){
		$("#blk-3").show();
		
	}else{
		$("#blk-3").hide();
}

if($('fieldset.hyrbil * input[alt=toggler_show]').is(':checked')){
		$("#blk-4").show();
		
	}else{
		$("#blk-4").hide();
}

	
	
	$("[alt=toggler_hide]").change(function(){
		if (!($(this).is('checked'))) {
			$("#blk-"+$(this).val()).hide();
			$("#blk-"+$(this).val()).find('select').val(0);
			calculateForm();
		}
	});
	$("[alt=toggler_show]").change(function(){
		if (!($(this).is('checked'))) {
			$("#blk-"+$(this).val()).show();
		}
	});
	

if($(".summarybooking").length > 0){
calculateForm();
}
 $(".pricevalue").click(function(){
 calculateForm();
 });
$('#blk-3 select').change(function() {
 calculateForm();
});


 $(".showloader").click(function(){
$("#loader").show();
 });



    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#waiting').height() + 80) / 2;
    var popMargLeft = ($('#waiting').width() + 80) / 2;

    //Apply Margin to Popup
    $('#waiting').css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });



	$(".parentCheckBox").click(
		function() {
			$(this).parents('ul:eq(0)').find('.childCheckBox').attr('checked', this.checked);
		}
	);
//clicking the last unchecked or checked checkbox should check or uncheck the parent checkbox
	$('.childCheckBox').click(
		function() {
			if ($(this).parents('ul:eq(0)').find('.parentCheckBox').attr('checked') == true && this.checked == false && $(this).parents('ul:eq(0)').find('.parentCheckBox').attr('checked') != false){
				$(this).parents('ul:eq(0)').find('.parentCheckBox').attr('checked', false);
			}

			if (this.checked == true) {
				var flag = true;
				$(this).parents('ul:eq(0)').find('.childCheckBox').each(
					function() {if (this.checked == false){flag = false;}}
				);
				$(this).parents('ul.filter:eq(0)').find('.parentCheckBox').attr('checked', flag);
			}else{
				$(this).parents('ul.filter:eq(0)').find('.parentCheckBox').attr('checked', false);
			}
		}
	);



/*  Tillval  */
$("dd:last-child .markall").click(function() {
			 calculateForm();
			$('.forsakring dl dd:last-child input').attr('checked', this.checked);
});
$("dd:nth-child(2) .markall").click(function() {
			 calculateForm();
			$('.forsakring dl dd:nth-child(2) input').attr('checked', this.checked);
});

	//$('.forsakring dl dd:first-child, .forsakring dl.sum dd:first-child').find('.childcheck').attr('checked', this.checked);
//clicking the last unchecked or checked checkbox should check or uncheck the parent checkbox

	$('.forsakring dl dd:last-child input').click(
		function() {
			calculateForm();
			if ($('.forsakring dl dd:last-child input').attr('checked') == true && this.checked == false && $('.forsakring dl dd:last-child input').attr('checked') != false){
				$("dd:last-child .markall").attr('checked', false);
			}

			if (this.checked == true) {
				var flag = true;
				$('.forsakring dl dd:last-child input').each(
					function() {if (this.checked == false && !$(this).hasClass('markall')){flag = false;}}
				);
				$("dd:last-child .markall").attr('checked', flag);
			}else{
				$("dd:last-child .markall").attr('checked', false);
			}
		}
	);
	$('.forsakring dl dd:nth-child(2) input').click(
		function() {
			 calculateForm();
			if ($('.forsakring dl dd:nth-child(2) input').attr('checked') == true && this.checked == false && $('.forsakring dl dd:nth-child(2) input').attr('checked') != false){
				$("dd:nth-child(2) .markall").attr('checked', false);
			}

			if (this.checked == true) {
				var flag = true;
				$('.forsakring dl dd:nth-child(2) input').each(
					function() {if (this.checked == false && !$(this).hasClass('markall')){flag = false;}}
				);
				$("dd:nth-child(2) .markall").attr('checked', flag);
			}else{
				$("dd:nth-child(2) .markall").attr('checked', false);
			}
		}
	);


/* Bookingform leftbox */

if (!$('.bokningsform .x-vux').is(':disabled')) { 
	  	$('#showform').hide();
		$('#submit_btn').show();
		$('.bokningsform *').find('input:not(.dateinput, .dateinput2), select, textarea, a, img').removeAttr('disabled');
}else{
$('#showform').show();
$('#submit_btn').hide();

}


$('#showform').click(function(){
	$('#showform').hide();
	$('#submit_btn').show();
	$('#imgbtnSend2').hide();
	$('.bokningsform *').find('input, select, textarea, a, img').removeAttr('disabled');
});

/*bokningen stegen */
$('#tillaggblock').find('input, select, textarea, a, img').removeAttr('disabled');
if (!$('#huvudblock #fornamn').is(':disabled')) { 
	  	$('#changefields').hide();
		$('#sendinfo').show();
		$('#imgbtnSend2').hide();
		$('#huvudblock').find('input, select, textarea, a, img').removeAttr('disabled');

}else{
$('#changefields').show();
$('#sendinfo').hide();
$('#imgbtnSend2').show();


}






//tillaggblock

$('#changefields').click(function(){
	$('#changefields').hide();
	$('#imgbtnSend2').hide();
	$('#sendinfo').show();
	$('#huvudblock').find('input, select, textarea, a, img').removeAttr('disabled');
	$('#tillaggblock').find('input, select, textarea, a, img').attr("disabled","disabled");
});




$('html').click(function() {
 $(".datepicker").hide(); 
 });

 $('.datepicker, .dateinput, .dateinput2').click(function(event){
     event.stopPropagation();
 });





	$('#resvag').change(function() {
		if($('#resvag').val()== "enkel"){
			$("#tab2 .date2").hide();
		}else{
			$("#tab2 .date2").show();
		}

	});

$(".codefield").hide();
if($(".visakod").is(":checked")){
$(".codefield").show();
}
$(".visakod").click(function(){
if($(".visakod").is(":checked")){
$(".codefield").show();
}else{
$(".codefield").hide();
}
});

	
$("#scrollChange").click(changeScrollHeight);



	
	$("#clear").click(function() {
		$(":input").unautocomplete();
	});



$(".button").click(validate);
$('select').removeClass('errorfield');
$('input').removeClass('errorfield');


$("input.dateinput, input.dateinput2").click(function(event) {
	var position = $(this).position();
	var selected = $("select.xort option:selected").val();
	if (!$(this).is(':disabled')) { 
		if (selected != "Välj resmål"){
			$('#xort_error').hide();
			$(".datepicker").show().html("<img src='/img/smalloader.gif' class='smalloader' alt=''/>");
			var cal = $("input[name=sprak]").val()+"|"+$(this).attr("id")+"|"+$("select[name=avrort]").val()+"|"+$("select[name=ort]").val()+"|"+$("input[name=datum]").val();
			$(".datepicker").css( {top:position.top-5, left: position.left+75}).load('/cgi-bin/wspd_cgi.sh/pop_nykalender.p?id='+cal, function(){
				$(".smalloader").hide();
			});

		}else{
			$('#xort_error').show();
		}
	}

});
$("img.dateinput, img.dateinput2").click(function(event) {
	var position2 = $(this).position();
	var selected = $("select.xort option:selected").val();
	if (!$('input.dateinput').is(':disabled')) { 
		if (selected != "Välj resmål"){
			$('#xort_error').hide();
			$(".datepicker").show().html("<img src='/img/smalloader.gif' class='smalloader' alt='' />");
			var cal = $("input[name=sprak]").val()+"|"+$(this).attr("id")+"|"+$("select[name=avrort]").val()+"|"+$("select[name=ort]").val()+"|"+$("input[name=datum]").val();
			$(".datepicker").css( {top:position2.top-4, left: position2.left+17}).load('/cgi-bin/wspd_cgi.sh/pop_nykalender.p?id='+cal, function(){
				$(".smalloader").hide();
			});
		}else{
			$('#xort_error').show();
		}
	}
});



$("ul.tabs li").click(function() {
	$("ul.tabs li").removeClass("current"); 
	$(this).addClass("current"); 
	$('input.visakod').attr('checked', false);
	$('#from').attr('checked', true);
	$('#blk-1').show();
	$('#blk-2').hide();

	$('select').removeClass('errorfield');
	$('input').removeClass('errorfield');
	$('.error').hide();
	$(".datepicker div").remove();
	$(".alder-barn select").remove();
	$(".antal-baby select option").remove();
	$(".antal-barn select").html("<option value='0'>Antal barn (2-17 &aring;r)</option>");
	$(".antal-baby select").html("<option value='0'>0 sp&auml;dbarn (0-1 &aring;r)</option>");
	$(".codefield").hide();
	$('input.visakod').attr('checked', false);

	$(".tab_content").hide(); 
	var activeTab = $(this).find("a").attr("href");
	$(".datepicker").hide();

	if(!(activeTab == "tab2")){
	$(".date2").show();
	}

	if($("#resvag option:checked").val()=='enkel'){
		$("#tab2 .date2").hide();
	}else{
		$("#tab2 .date2").show();
	}

	$(activeTab).show();
	//$(activeTab+" div.alder-barn select").remove();
	var barn = 8-$(activeTab+" .x-vux :selected").val();
	for(var j = 1; j <= barn; j++) {
		$("<option />", {value: j}).appendTo('.antal-barn select#barn'+j).html(j+" barn");
	}
	var babypervuxen = $(activeTab+" .x-vux :selected").val();
	for(var g = 1; g <= babypervuxen; g++) {
		$("<option />", {value: g}).appendTo('.antal-baby select').html(g+" sp&auml;dbarn (0-1 &aring;r)");
	}
return false;
});

$('.x-vux').each(function(index) {
	$(this).change(function() {
	var id2 = $(this).closest("div.tab_content").attr("id");
			$(".alder-barn select").remove();
			$(".antal-barn select option").remove();
			$(".antal-baby select option").remove();

			$(".antal-barn select").html("<option value='0'>Antal barn (2-17 &aring;r)</option>")
			$(".antal-baby select").html("<option value='0'>0 sp&auml;dbarn (0-1 &aring;r)</option>")
			var barn = 8-$("#"+id2+" .x-vux :selected").val();

			for(var j = 1; j <= barn; j++) {
			$("<option />", {value: j}).appendTo('.antal-barn select').html(j+" barn");
			}
			var babypervuxen = $("#"+id2+" .x-vux :selected").val();
			for(var g = 1; g <= babypervuxen; g++) {
			$("<option />", {value: g}).appendTo('.antal-baby select').html(g+" sp&auml;dbarn (0-1 &aring;r)");
			}
			return false;
	});
 });



$('.x-avrort').change(function() {
$("select.xort option").remove();
var loadpage = "/flygplatser/"+$(this).val()+".html";
	$(".xort").load(loadpage, function() {
		
	});
});







// Ta bort selectboxar om barn sätts till 0
$('.antal-barn').each(function(index) {

	$(this).change(function() {
	var i = 0;
	i = $(this).val();
		if(i === "0"){
		$(".alder-barn select").remove();
		}
	var num = parseInt($(this).val(), 10);
	var container = $('<div />');
	for(var i = 1; i <= num; i++) {
			 $("<select />", {
				 id: "barn" + i,
				 name: "barn" + i
			 }).appendTo(container).html("<option value='0'>&Aring;lder barn "+i+" (vid hemresan)</option>");	
			$('.alder-barn').html(container);
			var num2 = 17;
			for(var j = 2; j <= num2; j++) {
			$("<option />", {value: j}).appendTo(".alder-barn select#barn" + i).html(j+" &aring;r");
			}
	}
	});
 });


});

function calculateForm() {
		var total_extra=0;
		var total_base=0;
		$('input.pricevalue:checked:enabled').each(function(index) {
			total_extra += parseInt($(this).attr('alt'));
		});
		$('span.pricevalue').each(function(index) {
			total_extra += parseInt($(this).text());
		});
		$('input[type=text].pricevalue').each(function(index) {
			total_extra += parseInt($(this).val());
		});
		$('select.pricevalue').each(function(index) {
			total_extra += parseInt($(this).find("option:selected").attr("title"));	
		});

		$('span.pricevalue_base').each(function(index) {
			total_base += parseInt($(this).text());
		});
		$(".pricevalue_extra").html(total_extra);
		$(".totalsum").html(total_extra+total_base);
}


function calculateSum() {
 
        var sum = 0;
        //iterate through each textboxes and add the values
        $(".pricevalue").each(function() {
 
            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }
 
        });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#totalsum").html(sum.toFixed(2));
    }



function changeOptions(){
	var max = parseInt(window.prompt('Please type number of items to display:', jQuery.Autocompleter.defaults.max));
	if (max > 0) {
		$("#suggest1").setOptions({
			max: max
		});
	}
}

function changeScrollHeight() {
    var h = parseInt(window.prompt('Please type new scroll height (number in pixels):', jQuery.Autocompleter.defaults.scrollHeight));
    if(h > 0) {
        $("#suggest1").setOptions({
			scrollHeight: h
		});
    }
}




function showHide() {
	var idelm = $(this).attr("id");
	var spelm = idelm.split('_');
	var uid = spelm[1];
	var theparent = $(this).parents().eq(1).attr("class");
	if($('#block_'+ uid).is(':visible')) {
		$(".show #"+idelm).parent('.show').show();
		$(".hide #"+idelm).parent('.hide').hide();
		$("#block_"+ uid).hide();
		$('#visa_'+ uid).show();
		$('#dolj_'+ uid).hide();
		$(this).closest('tr').removeClass('active');
		$("#block_"+ uid).closest('tr').removeClass('active');

	}else{
		if ($(this).parent('td')) {
			$(this).closest('tr').addClass('active');
			$("#block_"+ uid).closest('tr').addClass('active');
		}

		$(".hide #"+idelm).parent('.hide').show();
		$(".show #"+idelm).parent('.show').hide();
		$("#block_"+ uid).show();	
		$('#visa_'+ uid).hide();
		$('#dolj_'+ uid).show();

	}

}

function slideTime1(event, ui){
	var minutes0 = parseInt(ui.values[0] % 60);
	var hours0 = parseInt(ui.values[0] / 60 % 24);
	var minutes1 = parseInt(ui.values[1] % 60);
	var hours1 = parseInt(ui.values[1] / 60 % 24);
    $("#time1").text(getTime(hours0, minutes0) + ' - ' + getTime(hours1, minutes1));
	$("input.s1tid1").val(ui.values[0]);
	$("input.s1tid2").val(ui.values[1]);
}
function slideTime2(event, ui){
	var minutes0 = parseInt(ui.values[0] % 60);
	var hours0 = parseInt(ui.values[0] / 60 % 24);
	var minutes1 = parseInt(ui.values[1] % 60);
	var hours1 = parseInt(ui.values[1] / 60 % 24);
    $("#time2").text(getTime(hours0, minutes0) + ' - ' + getTime(hours1, minutes1));
	$("input.s2tid1").val(ui.values[0]);
	$("input.s2tid2").val(ui.values[1]);
}
function getTime(hours, minutes) {
	var time = null;
	minutes = minutes + "";
	var timnolla = "";
	var minnolla = "";
	if(hours < 10){
	timnolla = "0";
	}
	if(minutes < 10){
	minnolla = "0";
	}
	var skrivut = timnolla + hours + ":" + minnolla + minutes;

return skrivut;
}


function findValueCallback(event, data, formatted) {
		$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	}
	
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}


function parseDate(str) {
    var mdy = str.split('-')
    return new Date(mdy[0], mdy[1], mdy[2]);
}

function daydiff(first, second) {
    return (second-first)/(1000*60*60*24)
}

function goToByScroll(id){
       $('html, body').animate({
		   scrollTop: $("#"+id).offset().top - 236
		},'slow');
}

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight())- 700) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}

function validate(){ 


var errorlist = 0;

	var id = $(this).closest("div.tab_content").attr("id");
	var differans = (daydiff(parseDate($("#"+id+" input.dateinput").val()), parseDate($("#"+id+" input.dateinput2").val())));	

	

       var avreseort = $("#"+id+" select.x-avrort").val();  
       if (avreseort == "") {  
       $("label#x-avrort_error").show();  
       $("select.x-avrort").focus().addClass("errorfield");
	   errorlist = errorlist+1;
     }else{
		$("label#x-avrort_error").hide();
		$("select.x-avrort").removeClass('errorfield');
	 }
       var destination = $("#"+id+" select.xort").val();
	   var selected = $("#"+id+" select.xort option:selected").val();

       if (destination == "" || selected == "Välj resmål") {  
       $("label#xort_error").show();  
       $("select.xort").focus().addClass("errorfield");
	   errorlist = errorlist+1;
     }else{
		$("label#xort_error").hide();
		$("select.xort").removeClass('errorfield');
	 }
	 if($("#"+id+" #resvag option:checked").val()!='enkel'){
		if (differans > 60) {
			$("label#datemax_error").show();  
			$("input.x-datumh").focus().addClass("errorfield");  
			$("input.x-datum").focus().addClass("errorfield"); 
			errorlist = errorlist+1;
		}else{
			$("label#datemax_error").hide(); 
			$("input.x-datumh").removeClass("errorfield");  
			$("input.x-datum").removeClass("errorfield"); 
		}
		/*
		if ((differans < 2) && (differans > -1)) {
			$("label#datemin_error").show();
			$("input.x-datum").focus().addClass("errorfield"); 
			$("input.x-datumh").focus().addClass("errorfield");
			errorlist = errorlist+1;
		
		}else{
			$("label#datemin_error").hide();
			$("input.x-datumh").removeClass("errorfield");  
			$("input.x-datum").removeClass("errorfield"); 
		}*/
		if (differans < 0) {
			$("label#date_error").show();  
			$("input.x-datum").focus().addClass("errorfield"); 
			$("input.x-datumh").focus().addClass("errorfield"); 
			errorlist = errorlist+1;
		}else{
			$("label#date_error").hide(); 
			$("input.x-datumh").removeClass("errorfield");  
			$("input.x-datum").removeClass("errorfield"); 
		}
		
	}else{
			$("label#date_error").hide(); 
			$("input.x-datumh").removeClass("errorfield");  
			$("input.x-datum").removeClass("errorfield"); 
	}

	
	for(var i = 1; ($("#"+id+" select#barn"+i).val()== "0")&&i<9; i++) {
		$("div.alder-barn div select#barn"+i).focus().addClass("errorfield");
		$("label#alder_error").show();
		errorlist = errorlist+1;
	}
	for(var i = 1; !($("#"+id+" select#barn"+i).val()== "0")&&i<9; i++) {
		$("div.alder-barn div select#barn"+i).removeClass("errorfield");
		$("label#alder_error").hide();
	}


    if (($("#"+id+" select.x-vux").val() == "0")&&($("#"+id+" select.antal-barn").val() == "0")) {  
		$("label#faresande_error").show();  
		$("select.x-vux").focus().addClass("errorfield");  
		$("select.antal-barn").focus().addClass("errorfield");
		
		errorlist = errorlist+1;
     }else{
		$("label#faresande_error").hide();  
		$("select.x-vux").removeClass("errorfield");  
		$("select.antal-barn").removeClass("errorfield"); 
		
	 }

if(errorlist==0){



var string = $("form#hof0").serialize();
var url = "/cgi-bin/wspd_cgi.sh/b_pkt_resultat.p?"+string;
$('#fading, #popuppen, #waiting').show();
$(location).attr('href',url);


}
return false;  
}

