function utf8_decode (str_data) {
    // Converts a UTF-8 encoded string to ISO-8859-1  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/utf8_decode
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
    var tmp_arr = [];
        i = 0;
        ac = 0;
        c1 = 0;
        c2 = 0;
        c3 = 0;
 
    str_data += '';
 
    while (i < str_data.length) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if (c1 > 191 && c1 < 224) {
            c2 = str_data.charCodeAt(i + 1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i + 1);
            c3 = str_data.charCodeAt(i + 2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
 
    return tmp_arr.join('');
}

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}

jQuery(document).ready( function($) {
	
	
	$('#nav_main_ul > li a[title="Mina sidor"]').parent().addClass('current');
	/*function input_values()
	{	
		i=0;
		var inputBox = $('#search_field,#time,#date,#email,#password');
		inputBox.focus(function(){
			if ($(this).val() != "") {
				var theValue = $(this).val();
				$(this).attr('temp', theValue)
				if(i == 0)
				$(this).val('');
				else
				{
					$(this).val(theValue);
				}
				
			};
		
		});
		
		inputBox.blur(function(){
			i++;
			console.log("blur");
			if ($(this).val() == "") {
				var theValue = $(this).attr('value');
				$(this).val(theValue);
			}else
			{
				var theValue = $(this).attr('value');
				$(this).val(theValue);
			}
			
		});
	}
	input_values();*/
	//Legacy

	
	//from shop_js.js //Converted to jquery from javascript - DH 2011-06-21
	function update_end(before_date)
	{		
		var sel_date = $('#main form#choice_form input.start_date').val();
		var skriv = $('#error_index_message');
		var array = sel_date.split("-");
		
		if(array[0].length == 4 && array[1].length == 2 && array[2].length == 2)
		{
			go = 1;
			skriv.html('');
		}
		else
		{
			skriv.html("<div><p>Datum m&aring;ste skrivas i &Aring;&Aring;&Aring;&Aring;-MM-DD format!</p></div>");
			$('#main form#choice_form input.start_date').val('before_date');
		}
	
		var array = sel_date.split("-");
		var year = parseInt(array[0]);
		if(array[1].charAt(0) == 0)
		{
			array[1] = array[1].replace("0","");
		}
		var month = parseInt(array[1]);
		
		var char_string = array[2].charAt(0);
		
		if(char_string == "0")
		{
			var temp_day = array[2].substring(1, 2);
			var day = parseInt(temp_day);
		}
		else
		{
			var day = parseInt(array[2]);
		}
		
		month = month -1;
		
		var now=new Date(year, month, day);
		var newdate=new Date(year, month, day);
		var newtimems=newdate.getTime()+(31*24*60*60*1000);
		newdate.setTime(newtimems);
		
		year = newdate.getFullYear();
		month = newdate.getMonth() +1;
		day = newdate.getDate();
		
		
		if(month < 10)
		{
			month = month.toString();
			month = "0" + month;
		}
		
		if(day < 10)
		{
			day = day.toString();
			day = "0" + day;
		}
		
		$('#main form#choice_form input.start_date').val(year + "-" + month + "-" + day);
	}
	
	function check_form(){
	  var go = 1;

	 	if ($("#route_id").val() != 'all') {
	 		if ($("#start_city").val() == "" || $("#start_city").val() == "0") {
	 			alert(utf8_decode("Du har glömt att välja en startort!"));
		 		return false;
		 		go = 0;
	 		}
	 		if ($("#destination_city").val() == "" || $("#destination_city").val() == "0") {
	 			alert(utf8_decode("Du har glömt att välja en slutort!"));
	 			go = 0;
		 		return false;
		 	}
			if($("#route_id").val() == "" && ($("#product_id").val() == 14 || $("#product_id").val() == 12)) {
				$("#route_id").attr("value", "1")
			}
	 	}
		if(go == 1){
			$("#proceed_form").submit();
		}
	} 

		//Studerandevarning
	$('#product_id option').click(function(){
		var theValue = $(this).val();
		var thisMessage = utf8_decode('Periodkort för skolungdom och Studerande med 25 % på ordinarie pris.\nDu som reser med Studerandekort skall visa giltigt Mecenat- CSN- el Studerande kort tillsammans med giltig fotolegitimation.\nReser du som skolungdom skall du kunna styrka din ålder med giltig legitimation');
		if (theValue == '11' || theValue == '14'){
		
				// $('<div class="dialog"></div>')
				// .html(thisMessage)
				// .dialog({
				// 	title: 'Viktigt!',
				// 	dialogClass: 'dialog',
				// 	hide: 'fade',
				// 	autoOpen: true,
				// 	modal: true,
				// 	buttons: { "OK": function() { $(this).dialog("close").remove(); } }
				// });
			alert(thisMessage);
		}
	});
		
		function check_choice(){
			var choice_option = $("#make_choice").val();
			var product_id = $("#product_id").val();
			var flag = 0;
		
			if (product_id == '') 
			{
				// $('<div class="dialog"></div>')
				// .html('Du måste välja Buss eller Tåg innan du kan gå vidare!')
				// .dialog({
				// 	title: 'Viktigt!',
				// 	dialogClass: 'dialog',
				// 	hide: 'fade',
				// 	autoOpen: true,
				// 	modal: true,
				// 	buttons: { "OK": function() { $(this).dialog("close").remove(); } }
				// });
				var thisMessage = utf8_decode('Du måste välja Buss eller Tåg innan du kan gå vidare!');
				alert(thisMessage);
			
			}
			else 
			{
				if (choice_option == 3) {
					//$('form#choice_form').attr('action','/card/cart');
					$('form#choice_form').attr('action','/map/show');
					$("form#choice_form #direct").val("direct");
					$("form#choice_form #route_id").val("all");
					$("form#choice_form").submit();
				}
				if (choice_option == 1) {
					$('form#choice_form').attr('action','/map/show');
					//$('form#choice_form').attr('action','/card/map');
					$("form#choice_form").submit();
				}
				if(choice_option == 4)
				{
					if (product_id == 9 || product_id == 11) {
						$('form#choice_form').attr('action','/card/cart');
						$("form#choice_form #direct").val("direct");
						$("form#choice_form #route_id").val("city-card");
						$("form#choice_form").submit();
					}
					else
					{
					// $('<div class="dialog"></div>')
					// .html('Detta kort kan endast köpas som Period Buss eller Period Buss (student)!')
					// .dialog({
					// 	title: 'Viktigt!',
					// 	dialogClass: 'dialog',
					// 	hide: 'fade',
					// 	autoOpen: true,
					// 	modal: true,
					// 	buttons: { "OK": function() { $(this).dialog("close").remove(); } }
					// });
					var thisMessage = utf8_decode('Detta kort kan endast köpas som Period Buss eller Period Buss (student)!');
					alert(thisMessage);
					
					}
				}
				//else if((product_id == 12 || product_id == 14) && choice_option == 1)
			//	{
				//	$("#error_choice_message").html("<p class=\"error_mess\">Zoner/linjer: Endast sträcka eller länskort kan väljas för t&aring;g-kort!</p>");
				//	flag = 1;
			//	}
				if (choice_option == 2 && flag == 0) {
					$("form#choice_form").submit();
				}
			}
			
		}
		
		function add_cart(verify)
		{
			$('#proceed_form').attr('action','/card/cart');
			
			$('.order_direct').attr('value', 'direct');

			var go = 1;
			if (verify != 1) {
			 	if ($('#start_city').val() == "" || $('#start_city').val() == "0") {
			 		alert(utf8_decode("Du har glömt att välja en startort!"));
			 		return false;
			 		go = 0;
			 	}
			 	if ($('#destination_city').val() == "" || $('#destination_city').val() == "0") {
			 		alert(utf8_decode("Du har glömt att välja en slutort!"));
			 		return false;
			 		go = 0;
			 	}
			}
			
			if(go == 1)	{
				$("form#proceed_form").submit();
			}
			return false;
		}
		
		
	//Display the chosen zones
	$('.cart_prod_zones a').click(function(e){
		var theZones = $(this).attr('title');
		e.preventDefault;
			$('<div class="dialog"></div>')
					.html(theZones)
					.dialog({
						title: 'Valda zoner!',
						dialogClass: 'dialog',
						hide: 'fade',
						autoOpen: true,
						modal: true,
						buttons: { "OK": function() { $(this).dialog("close").remove(); } }
					});
		return false;
	});
	
	//Current
	
	//submit form when product id has changed
	//$('#choice_form select#product_id').change(function(){
	//	$('#choice_form').submit();
	//});
	
	//make a choice and run the check of what choice has been made
	$('#choice_form select#make_choice').change(function(){
		check_choice();
	});
	
	//update amount
	$('#choice_form #start_date').change(function(){
		var thisVal = $(this).val();
		update_end(thisVal);
	});
	
	//when choosing a route, submit the form
	$('#route_form select#route_id').live('change', function(){
		$('#route_form').submit();
	});
	
	//proceedform submit
	$('.proceed_form_submit').click(function(){
		$('form#proceed_form_discount').submit();
	});

	$('#proceed_form a.map_lines').click(function(e){
		e.preventDefault();
		$('form#proceed_form').attr('action','/map/show');
		//$('form#proceed_form').attr('action','/card/map');
		return check_form();

	});
	$('#proceed_cart').click(function(e){
		e.preventDefault();
		add_cart(0);
	});


	//orderform submit
	$('#continue_to_register').click(function(e){
		e.preventDefault();
		$('#order_form').submit();
		return false;
	});
	$('#confirm_payment_button').click(function(){
		return false;
	});
	$('#confirm_payment_submit').click(function(){
		$('#confirm_payment').submit();
		return false;
	});

	//print
	$('.print_window').click(function(){
		window.print(); 
		return false;
	});
	
	/*Popups*/
	//Print content
	$('.popup_content').click(function(){
window.open('../popup.php?content=print','popup','width=774,height=800,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); 
		return false;
	});
	//show conditions
	$('.popup_conditions').click(function(){
window.open('../popup.php?content=conditions','popup','width=774,height=800,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); 
		return false;
	});
	//show discount info
	$('.popup_discount').click(function(){	window.open('../popup.php5?content=discount','popup','width=774,height=800,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); 
		return false;
	});
	//show period info
	$('.popup_period').click(function(){	window.open('../popup.php5?content=period','popup','width=774,height=800,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); 
		return false;
	});

	$('#mapwrapper').appendTo('#content');
	


});
