jQuery(function($){
            
             /* Multi download */
             var form = $('#orderForm');
             $(form).validate({
                //debug: true,
                errorClass: 'validate-error',
                errorElement: 'span',
                submitHandler: function(form){
                       
                        var containing = $('#orderForm').find('input').filter(function(){ return $(this).val() > 0 && $(this).hasClass('requiresOne');}).size();
                        
                        if(containing > 0){
                            form.submit();
                        }
                        else{
                            $('#noneSelected').fadeOut(100, function(){
                                $('#noneSelected').html('Du har ikke valgt nogle materialer. Angiv venligst ovenfor, hvad du vil bestille. (kun tal)').fadeIn(300);
                            });
                        }
                },  
                rules: {
                    zipcode: {
                        required: true,
                        range: [100, 9998]
                    }
                },
                messages: {
                    company: 'Udfyld firma',
                    name: 'Udfyld navn',
                    address: 'Udfyld adresse',
                    zipcode: 'Udfyld postnr. og by',
                    city: 'Udfyld postnr. og by',
                    email: 'Udfyld e-mail',
                    digits: 'Ugyldigt antal',
                    requiresOne : 'Ugyldigt antal'
                },
                groups: {
                    username: 'zipcode city'
                },
                errorPlacement: function(error, element) {
                    if (element.attr('name') == 'zipcode' || element.attr('name') == 'city') {
                        error.insertAfter('#city');
                    } else if (element.hasClass('requiresOne')) {
                        $('#validate-error-container').html(error);
                    } else {
                        error.insertAfter(element);
                    }
                    error.fadeIn(300);
                }
            });
            

			 /* IHC */

           
			$("#ihccuponform").validate({
			rules: {
						name:"required",
						address:"required",
						zipcode:"required",
						city:"required",
						companyname:"required",
						companyattname:"required",
						companyaddress:"required",
						companyzipcode:"required",
						companycity:"required"
						},
				messages: {
						name: 'Udfyld navn',
						address: 'Udfyld adresse',
						zipcode: 'Udfyld postnr. og by',
						city: 'Udfyld postnr. og by',
						companyname: 'Udfyld firmanavn',
						companyattname: 'Udfyld navn',
						companyaddress: 'Udfyld adresse',
						companyzipcode: 'Udfyld postnr. og by',
						companycity: 'Udfyld postnr. og by'
						},
				 ignore: ".ignore",
				 groups: {
                    zipcity: 'zipcode city',
					companyzipcity: 'companyzipcode companycity'
				 },
				errorClass: 'validate-error',
				errorElement: 'span',
				errorPlacement: function(error, element) {
				 if (element.attr("name") == "zipcode" || element.attr("name") == "city" ){
					  error.insertAfter("#city");
				 }
				 else if (element.attr("name") == "companyzipcode" || element.attr("name") == "companycity" ){
					  error.insertAfter("#companycity");
				 }
				 else{
				   error.insertAfter(element);
				 }
				}


			}); 

            
            
            /*Single download*/
            
              $('#ordersingelbrochure').validate({
                //debug: true,
                errorClass: 'validate-error',
                errorElement: 'span',
                submitHandler: function(form){
                form.submit();
				},
                rules: {
                    zipcode: {
                        required: true,
                        range: [100, 9998]
                    }
                },
                messages: {
                    company: 'Udfyld firma',
                    name: 'Udfyld navn',
                    address: 'Udfyld adresse',
                    zipcode: 'Udfyld postnr. og by',
                    city: 'Udfyld postnr. og by',
                    email: 'Udfyld e-mail',
                    digits: 'Ugyldigt antal',
                    requiresOne : 'Ugyldigt antal'
                },
                groups: {
                    username: 'zipcode city'
                },
                errorPlacement: function(error, element) {
                    if (element.attr('name') == 'zipcode' || element.attr('name') == 'city') {
                        error.insertAfter('#city');
                    } else if (element.hasClass('requiresOne')) {
                        $('#validate-error-container').html(error);
                    } else {
                        error.insertAfter(element);
                    }
                    error.fadeIn(300);
                }
            });
            /* add special rule for amount fields */
            /*$(".requiresOne").rules('add', {
                digits: true,
                min: 1,
                messages: {
                    digits: 'Ugyldigt antal',
                    min: 'Indtast minimum 1'
                }
            });
            */
            
            
            /*course validation*/
            
                $('.courseForm').each(function(){
                var thisForm = $(this);
                $(this).validate({
                    errorClass: 'validate-error',
                    errorElement: 'span',
                    submitHandler: function(form){
                        var containing = $(form).find('input.input').filter(function(){
                            return $(this).val() > 0;
                        }).size();
				var conamount = $(form).find('input.input').filter(function(){
                            return $(this).val();
                        }).val();

                        $(form).find('.errors').fadeOut(100, function(){
                            if (containing > 0 && conamount < 100) {
                                form.submit();
                            } else {
					if (conamount >= 100) {
						$(this).html('<span class="validate-error">Du kan maks tilmelde 99 personer..</span>').fadeIn(300);
					}
					if (containing <= 0) {
                                $(this).html('<span class="validate-error">Du har ikke valgt nogle kurser..</span>').fadeIn(300);
					}
                            }
                        });
                    },
                    errorPlacement: function(error, element) {
                        $(thisForm).find('.errors').html(error);
                    }
                });
            });
            /*$('.form .input').rules('add', {
                digits: true,
                min: 1,
                messages: {
                    digits: 'Ugyldigt antal',
                    min: 'Indtast minimum 1'
                }
            });   */
            
            
            
            /*Course cart validation*/
            
            function checkInputBoxes(){
            var show = false;
            var inputs = $('.form table .input');
            if (inputs.size() < 1) {
                $('#submit .button input').attr({disabled:'disabled'});
            } else {
                $('#submit .button input').removeAttr('disabled');
                inputs.each(function(){
                    if ($(this).val() > 9) {
                        show = true;
                    }
                });
            }
            if (show) {
                $('#infoBox').slideDown(250);
            } else {
                $('#infoBox').slideUp(250);
            }
        }
	  
	  function repostCourseCart(action, uduItem){
		var submitURL = window.location.href;
		var submitParams = "cartaction=" + action;
		if (uduItem) {
			submitParams = submitParams + "&courseid=" + uduItem.find('input.courseid').val();
			submitParams = submitParams + "&amount=" + uduItem.find('input.amount').val();
			submitParams = submitParams + "&title=" + uduItem.find('.title').html();
			//alert(submitParams);
		}
		
		$.post(submitURL + submitParams);
		
		return false

	  }
        function checkZebra(){
            $('table tr').removeClass('odd').each(function(i){
                if (i % 2) {
                    $(this).addClass('odd');
                }
            });
        }
        function checkUndoBox(){
            if (undoItem) {
                $('#undoBox').find('.title').html(undoItem.find('.title').html());
                $('#undoBox').slideDown(250);
            } else {
                $('#undoBox').slideUp(250);
            }

        }
        function assignDeleteEvent(elm){
            elm.click(function(){
                var row = $(this).parents('tr:first');
		    var curRow = $(this).parents('tr:first');
                row.children('td').fadeOut(250, function(){
                    undoItem = row.clone();
                    row.remove();
                    checkUndoBox();
                    checkInputBoxes();
                    checkZebra();			  
                });
			repostCourseCart('remove_single', curRow);
            });
        }
        function assignChangeEvent(elm){
            elm.keyup(function(){
                checkInputBoxes();
            });
        }
        var undoItem = undefined;
            
         checkInputBoxes();
            $('#undo').click(function(){
                if (undoItem) {
                    $('.form table').append(undoItem);
                    undoItem.children('td').fadeIn(250);
                    checkInputBoxes();
                    checkZebra();
                    assignDeleteEvent(undoItem.find('.delete img'));
                    assignChangeEvent(undoItem.find('input.input'));
			  repostCourseCart('add', undoItem);
                    undoItem = undefined;
                    checkUndoBox();			  
                } else {
                    alert('nothing to undo!');
                }
                return false;
            });
            $('.form .delete').each(function(){
                assignDeleteEvent($(this).find('img'));
            });
            $('.form input.input').each(function(){
                assignChangeEvent($(this));
            });

			var Mthisform = $('#courseCartForm');

			$(Mthisform).validate({
                errorClass: 'validate-error',
                errorElement: 'span',
				submitHandler: function(Mthisform){
                       var containing = $(Mthisform).find('input.input').filter(function(){
                            return $(this).val() > 0;
                        }).size();
                       var conamount = $(Mthisform).find('input.input').filter(function(){
                            return $(this).val();
                        }).val();
    				
				 if (containing > 0 && conamount < 100) {
                                Mthisform.submit();
                           } else {
					if (conamount >= 100) {
						$('#validate-error-container').html('<span class="validate-error">Du kan maks tilmelde 99 personer..</span>').fadeIn(300);
					}
					if (containing <= 0) {
                              	$('#validate-error-container').html('<span class="validate-error">Du har ikke valgt nogle kurser..</span>').fadeIn(300);
					}                                
                           }

                },
                errorPlacement: function(error, element) {
                    $('#validate-error-container').html('Ugyldigt tal').fadeIn(250);
                }
            });



			$("#addmoreC").click(function(){
				var submitURL = $("#addmoreC").attr('href');
				$('#courseCartForm').attr("action",submitURL);

				var containingback = $('#courseCartForm').find('input.input').filter(function(){
				return $(this).val() > 0;
				}).size();

				if(containingback == 0){
					$("input[name='cartaction']").attr("value","clear");
				}
				document.courseCartForm.submit();
				return false
			});
			
			
			/*Course signup details*/
			
			   $('#courseForm').validate({
                //debug: true,
                errorClass: 'validate-error',
                errorElement: 'span',
                rules: {
                    zipcode: {
                        required: true,
                        range: [100, 9998]
                    }
                },
                messages: {
                    company: 'Udfyld firma',
                    name: 'Udfyld navn',
                    address: 'Udfyld adresse',
                    zipcode: 'Udfyld postnr. og by',
                    city: 'Udfyld postnr. og by',
                    email: 'Udfyld e-mail'
                },
                groups: {
                    username: 'zipcode city'
                },
                errorPlacement: function(error, element) {
                    if (element.attr('name') == 'zipcode' || element.attr('name') == 'city') {
                        error.insertAfter('#city');
                    } else if (element.hasClass('participant') && element.next().size() > 0 ) {
                        error.insertAfter(element.next());
                    } else {
                        error.insertAfter(element);
                    }
                    error.fadeIn(300);
                }
            });

            $.validator.addClassRules({
                participant: {
                    partify: true
                }
            });
            $.validator.addMethod('partify', function(value, element) {
                return value.length > 1;
            }, 'Indtast navn på deltager');
            
            
            $('#gmapsearchform').validate({
                submitHandler: function(form) {
                var gmapsearchfield = $('.gmtxtfield').val();
                var gmapPostURL = $("#gmapPostURL").attr("value");
                $('#gmapsearchform').attr({action:gmapPostURL}); 
                               if(gmapsearchfield){
                                    
                                    try {
                                      pageTracker._trackEvent('spot installatør søgning', 'søgning', gmapsearchfield);
                                    } catch(err) {}
                                    
                                    form.submit();
                               }
                               else{
                                $('.gmtxtfield').val('Adresse, by, post nr.');
                                $('.gmtxtfield').css("color","red");

                                $('.gmtxtfield').click(function() {
                                    $('.gmtxtfield').css("color","#000");
                                    $('.gmtxtfield').val('');    
                                });
                               }
                }
            });



 
        });
