// krypterad epostadress till klarskrift
function eposttext(myarray, uttext){var encryptedemail=''; //variable to contain encrypted email 
	for (var i=0; i<myarray.length; i++)
		encryptedemail+=String.fromCharCode(myarray[i]);
	encryptedemail='<strong><a href="mailto:'+encryptedemail+'">'+uttext+'</a></strong>';
	return encryptedemail;
}
function epost(myarray) {var encryptedemail=''; //variable to contain encrypted email 
	for (var i=0; i<myarray.length; i++)
		encryptedemail+=String.fromCharCode(myarray[i]);
		encryptedemail='mailto:'+encryptedemail;
	return encryptedemail; 
}
function epostadr(myarray) {var encryptedemail=''; //variable to contain encrypted email 
	for (var i=0; i<myarray.length; i++)
		encryptedemail+=String.fromCharCode(myarray[i]);
	return encryptedemail; 
}
	var order=[111,114,100,101,114,64,109,121,103,103,110,97,116,46,115,101];
	var gh=[105,110,102,111,64,103,104,110,101,116,46,115,101];
	var test=[116,101,115,116,64,109,121,103,103,110,97,116,46,115,101];
var bw=[98,101,110,103,116,46,119,105,116,116,101,115,106,111,64,116,101,108,105,97,46,99,111,109]
$(function() {
	// alla sidor
	// ta bort test om javascript äv aktiverad
	$("p.utanJs").hide();	
	// lägg till uppdateringsdatum					 
	var datum = new Date(document.lastModified)
	datum= $.formatDate(datum,"d MMMM yyyy");
	$("li#datum span").html(datum);							 
	// läs cookie och om den finns visa menyfliken #nav-sid4
	var value = $.readCookie('myggnatse');
  if( value ) {
		$('#nav-sid4').show(); 
		} else {
			$('#nav-sid4').hide();	
		}
	
	// gör epostlänk
	$("p#epost1").html(eposttext(bw, "Firma Bengt Wittesj&ouml;"));						
// tabell ändra färg udda fält
	$('tr:odd').addClass('udda');
// tabell för saljaresidan	och ordersidan
// vit färg vid hovring	
	$('table tbody tr').hover(function(){
		$(this).addClass('over');
	}, function(){
		$(this).removeClass('over');
	})
//  lägg focus på sökfältet													 
	var $inputTxt = $('#sok');
	if( $inputTxt.val() == '' ) {
		$inputTxt.focus(); // focus the username input by default
	}			

// filtrera vid klick på ort
	var t = $('#myTable');
	function toggla(myText) {
	if (myText == '') {
		$('#btnVisa').attr('disabled', 'disabled');
		$("#sok").attr('value', ''); // töm text när btnVisa trycks ner
	}
	else {
		 $('#btnVisa').removeAttr('disabled');
	}
	}
	$('#lista li a').click(function() {
		$(this).attr('href','#top');
		var filterText = $(this).text();
		$.uiTableFilter(t, filterText, 'Ort');
		toggla(filterText)
	}) ;	
	$('#btnVisa').click(function() {
	$.uiTableFilter( t, '');
	toggla('');
	})
	$('#btnSok').click(function() {
	var filterText = $("#sok").attr('value');	
	$.uiTableFilter( t, filterText);
	toggla(filterText);
	})
	//fånga upp Enter-tryckning
	$("#sok").keydown(function(event){ 
		if (event.which == '13'){
			var filterText = $("#sok").attr('value');	
			$.uiTableFilter( t, filterText);
			toggla(filterText);
		}
  });
	//bildsidan bildvisare 
	var mySida= $('body').hasClass('bildsida');
	if (mySida) {
		$("a[rel='example1']").colorbox({opacity:0.6, transition:"elastic"});
		$("a[rel='example2']").colorbox({opacity:0.6, transition:"elastic"});
		$(".example8").colorbox({width:"70%", inline:true, href:"#inline_example1",opacity:0, transition:"elastic", speed:200});
	}
	//ordersidan: 
	//$('table.ljus tr').addClass('udda'); // två ljusa fält nära varandra i tabellen
	//$('table.mork tr').addClass('normal');  // ett mörkare fält efter dessa
	//gör cookie och validera mm
	var cl= $('body').hasClass('ordersida');
	if (cl) {
    $.setCookie('myggnatse', 'order', {duration: 365 /* in days*/});
// skriv ut		
		$('#skriv_ut').click(function() {
    window.print();
   });
// snygga till legendrubriker
		$('fieldset').each(function() {
		var heading = $('legend', this).remove().text();
		$('<h3></h3>')
		.text(heading)
		.prependTo( this );
//lägg till action för formuläret
	$("#mottagare").attr('value', epostadr(order)); // epostadr cgi-action för formuläret
// toggle optional fakturaadress
  var fakturaadress = $("div.f_div");
  var toggleCheck = $("input.toggleCheck");
  toggleCheck.is(":checked")
  ? fakturaadress.hide()
	: fakturaadress.show();
  $("input.toggleCheck").click(function() {
		if (this.checked == true) {
			fakturaadress.slideUp("medium");
			$("order_form").valid();			
		} else {
			fakturaadress.slideDown("medium");			
		}
  });
// visa fakturadress vid rensa	
	$('#rensa').click(function(){
		fakturaadress.slideDown("medium");												 
});
// form lägg focus på första fältet													 
	var $inputTxt = $('#rekt_enkel');
	if( $inputTxt.val() == '' ) {
		$inputTxt.focus(); // focus the username input by default
	}			
//avaktivera och göm fälten vid klick i checkbox#samma
	// find the checkbox and listen for the change
	$('#samma').change(function(){
	if( this.checked ){
			$("#print").val("X")
		// find all text inputs inside fakturaadress and disable them
		$('#faktura input:text').attr('disabled','disabled');
		$('#faktura .fstar').hide(); // ta bort obligatorisk märkning
	} else {
		// find all text inputs inside billingInfo and enable them
		$('#faktura input:text').removeAttr('disabled');
		$('#faktura .fstar').show();
		$("#print").val("")
	}
	}).trigger('change'); // close change() then trigger it once	
// validera form on keyup and submit
	var validator = $("#order_form").validate();
// ta bort validering vid reset
	$("#rensa").click(function() {
    validator.resetForm();
   });
// lägg till regler
	$("#postnr").rules("add", {
		minlength: 5,
		messages: {
   	minlength: jQuery.format("Ska vara {0} siffror utan mellanslag") }
	});
	$("#f_postnr").rules("add", {
		minlength: 5,
		messages: {
   	minlength: jQuery.format("Ska vara {0} siffror utan mellanslag") }
	});
	});
	}
	$(document).ready(function(){
   $("#click").bind('click', function(){
      $("#div_to_popup").bPopup();
      return false
   });
});
});		// $ slut	

/* jQuery CooQuery Plugin v2 (minified) - http://cooquery.lenonmarcel.com.br/
Copyright 2009, 2010 Lenon Marcel
Dual licensed under the MIT and GPL licenses.
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html */
(function($){$.setCookie=function(name,value,options){if(typeof name==='undefined'||typeof value==='undefined')
return false;var str=name+'='+encodeURIComponent(value);if(options.domain)str+='; domain='+options.domain;if(options.path)str+='; path='+options.path;if(options.duration){var date=new Date();date.setTime(date.getTime()+options.duration*24*60*60*1000);str+='; expires='+date.toGMTString();}
if(options.secure)str+='; secure';return document.cookie=str;};$.delCookie=function(name){return $.setCookie(name,'',{duration:-1});};$.readCookie=function(name){var value=document.cookie.match('(?:^|;)\\s*'+name.replace(/([-.*+?^${}()|[\]\/\\])/g,'\\$1')+'=([^;]*)');return(value)?decodeURIComponent(value[1]):null;};$.CooQueryVersion='v 2.0';})(jQuery);
/*
$.formatDate(date,pattern) Formats the passed date according to the supplied pattern. The tokens that are substituted in the pattern are as follows:
yyyy: the 4-digit year, yy: the 2-digit year, MMMM: the full name of the month, MMM: the abbreviated name of the month, MM: the month number as a 0-filled, 2-character field, M: the month number, dd: the day in the month as a 0-filled, 2-character field, d: the day in the month
EEEE: the full name of the day of the week, EEE: the abbreviated name of the day of the week, a: the meridium (AM or PM), HH: the 24-hour clock hour in the day as a 2-character, 0-filled field, 
H: the 24-hour clock hour in the day, hh: the 12-hour clock hour in the day as a 2-character, 0-filled field, h: the 12-hour clock hour in the day, mm: the minutes in the hour as a 2-character, 0-filled field, m: the minutes in the hour, ss: the seconds in the minute as a 2-character, 0-filled field, s: the seconds in the minute, S: the milliseconds in the second as a 3-character, 0-filled field, Parameters, date (Date) The date to be formatted., pattern (String) The pattern to format the date into. Any characters not matching pattern, tokens are copied as-is to the result. ReturnsThe formatted date.
*/
(function($){$.toFixedWidth = function(value,length,fill) {var result = value.toString();if (!fill) fill = '0';var padding = length - result.length;if (padding < 0) {result = result.substr(-padding);}else {for (var n = 0; n < padding; n++)result = fill + result;}return result;};})(jQuery);(function($){$.formatDate = function(date,pattern) {var result = [];while (pattern.length > 0) {$.formatDate.patternParts.lastIndex = 0;var matched = $.formatDate.patternParts.exec(pattern);if (matched) {result.push($.formatDate.patternValue[matched[0]].call(this,date));pattern = pattern.slice(matched[0].length);} else {result.push(pattern.charAt(0));pattern = pattern.slice(1);}}return result.join('');};$.formatDate.patternParts =/^(yy(yy)?|M(M(M(M)?)?)?|d(d)?|EEE(E)?|a|H(H)?|h(h)?|m(m)?|s(s)?|S)/;$.formatDate.monthNames = [
'januari','februari','mars','april','maj','juni','juli','augusti','september','oktober','november','december'];$.formatDate.dayNames = ['Söndag','Måndag','Tisdag','Onsdag','Torsdag','Fredag','Lördag'];$.formatDate.patternValue = {yy: function(date) {return $.toFixedWidth(date.getFullYear(),2);},yyyy: function(date) {return date.getFullYear().toString();},MMMM: function(date) {return $.formatDate.monthNames[date.getMonth()];},MMM: function(date) {return $.formatDate.monthNames[date.getMonth()].substr(0,3);},MM: function(date) {return $.toFixedWidth(date.getMonth() + 1,2);},M: function(date) {return date.getMonth()+1;},dd: function(date) {return $.toFixedWidth(date.getDate(),2);},d: function(date) {return date.getDate();},EEEE: function(date) {return $.formatDate.dayNames[date.getDay()];},EEE: function(date) {return $.formatDate.dayNames[date.getDay()].substr(0,3);},HH: function(date) {return $.toFixedWidth(date.getHours(),2);},H: function(date) {return date.getHours();},hh: function(date) {var hours = date.getHours();return $.toFixedWidth(hours > 12 ? hours - 12 : hours,2);},h: function(date) {return date.getHours() % 12;},mm: function(date) {return $.toFixedWidth(date.getMinutes(),2);},m: function(date) {return date.getMinutes();},ss: function(date) {return $.toFixedWidth(date.getSeconds(),2);},s: function(date) {return date.getSeconds();},S: function(date) {return $.toFixedWidth(date.getMilliseconds(),3);},a: function(date) {return date.getHours() < 12 ? 'AM' : 'PM';}};})(jQuery);	


