// JavaScript Document
$(window).load(function(){
	$('.nivoSlider').nivoSlider({
        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
        animSpeed:350, // Slide transition speed
        pauseTime:4000, // How long each slide will show
        controlNav:true, // 1,2,3... navigation
        prevText: '', // Prev directionNav text
        nextText: '', // Next directionNav text
		afterLoad: function(){ // Triggers when slider has loaded
			$('.nivoSlider').each(function() {
				var h = $(this).find('img:first').height();
				var w = $(this).find('img:first').width();
			
				$(this).height(h).width(w)				
					   .children('p a, p img').unwrap()
					   .find('&nbsp;').replaceWith('');				
			});
		}
    });
});

$(document).ready(function() {

		
	$(".userName").focus(function() {
	 if($(".userName").val()=="Kunde login"){
			 $(".userName").val("");
		 }
	});
	$(".userPassword").focus(function() {
	 if($(".userPassword").val()=="Kodeord"){
			 $(".userPassword").val("");
		 }
	});
	$(".userName").blur(function() {
	 if($(".userName").val()==""){
			 $(".userName").val("Kunde login");
		 }
	});
	$(".userPassword").blur(function() {
	 if($(".userPassword").val()==""){
			 $(".userPassword").val("Kodeord");
		 }
	});
	$(".productGroup:nth-child(3n)").css("marginRight","0");
	$('.buyButton').click(function(){
		if($(this).prev().val()==0){
			$.fancybox(
				'<div class="message"><h1>Angiv venligst antal</h1><br />Angiv venligst et antal på 1 eller højere for varen du ønsker at bestille</div>',
				{
					'width':350,
					'height':350,
					'padding':25
				}		
			);
			return false;
		}
		else{
			$.fancybox(
				'<div class="message"><h1>Varen er blevet tilføjet kurven</h1><br />Din vare er blevet tilføjet din indkøbskurv, find den i venstre side under menuen</div>',
				{
					'width':350,
					'height':350,
					'padding':25
				}
			);
		}
	});
	$('#otherDeliveryAddress').click(function () {
    	$('.couldReq').toggleClass('req', $('#otherDeliveryAddress:checked').length);
	});
	// Replace value in search field
	$('.replaceInput').each(function(){
		var thisVal = $(this).attr('value');							
		$(this).focus(function(){
			if($(this).attr('value')==thisVal){
				$(this).attr('value','');
			}
		}).blur(function(){
			if($(this).attr('value')==''){
				$(this).attr('value',thisVal);
			}
		});														
	});

});
function validateForm() {
	var bolSubmitForm = true;
	$(".req").each(function(){
		error = false;
		if($(this).hasClass("text")){
			if(this.value.match(/[A-Za-z0-9]/gi)==null){
				error = true;
			}
		}
		if($(this).hasClass("email")) {
			if(this.value.match(/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/gi)==null){
				error = true;
			}
		}
		if($(this).hasClass("isNumber")) {
			if(isNaN(this.value) || this.value==''){
				error = true;
			}
		}
		
		if(error!=false){
			$(this).addClass("error");
			bolSubmitForm = false;
		}
		else if($(this).hasClass("error")) {
			$(this).removeClass("error");
		}
		else {
			if(bolSubmitForm) bolSubmitForm = true;
		}
	});

	if(bolSubmitForm) {
		return true;
	}
	else {
		displayError();
		return false;
	}
}

var ErrorMsgId = 0;
function displayError() {
	var str = '';
	$(".req.error").each(function(){
		str = str+'<li>'+$(this).attr("alt")+'</li>';
	});
	$.fancybox(
		'<div class="message"><h1>Manglende information</h1><br />Der opstod følgende fejl ved indsendelse af ordren<br /><br /><ul>'+str+'</ul></div>',
		{
			'width':350,
			'padding':25
		}		
	);
}
