jQuery(document).ready(function() {
	
	// remove outdated courses from view
	
	// course info
	jQuery(".courseinfo").live("click", function() {
		
		jQuery(this).find(".coursetxt").toggle("fast");
		jQuery(".post select").toggle();
		
		return false;
	});
	
	jQuery("img.alignleft").live("click", function() {
		return false;
	});
	
	// striped table
	jQuery("#courseTable tr:odd").addClass("odd");
	
	// course – ajax
	jQuery("#kursoversikt a").live("click", function() {		
		jQuery("#kurset .entry").fadeOut();
		
		jQuery("#kurset").fadeOut("fast").load(this.href + " .entry", function() {
			jQuery("#kurset").fadeIn();
		
			updateCourses();
			jQuery.scrollTo("#kurset");
		});			

		return false;
	});
	
	
	// check fields
	jQuery("#sendMail").submit(function() {
		if (jQuery("#navn").val() == "" || jQuery("#phone").val() == "" || jQuery("#email").val() == "") {
			alert("Du må fylle inn både navn, telefonnummer og e-post");
			return false;
		} else {
			
	        var name = jQuery('input[name=name]');   
	        var phone = jQuery('input[name=phone]');   
	        var email = jQuery('input[name=email]');    
	
			var data = 'name=' + name.val() + '&email=' + email.val() + '&phone=' + phone.val();
			
			jQuery('.loader').show(); 
		   
			
			 jQuery.ajax({   
	            url: "/wp-content/themes/Zeke2Col/sendmail.php",    
	            type: "POST",           
	            data: data,        
	            cache: false,

	            success: function (html) {		
	                if (html==1) {   
	                    jQuery('#sendMail').fadeOut('fast', function() {
		                    jQuery('.done').fadeIn('slow');   
		
						});
	                } 
	            },
				error: function (html) {
					alert(html);
				}
	        });   
	
			return false;
		}
	});
	
	updateCourses();
});
	
	
function updateCourses() {			
	jQuery('.cform option').each(function(i, selected){  
		
		re = /\d{2}\.\d{2}\.\d{2,4}/
		str = jQuery(this).val();			
	
		today = new Date();
	
		isMatch = str.match(re);
	
		if (isMatch != null) {
			isMatch = isMatch.toString()
			matchArray = isMatch.split(".");
		
			if (matchArray[2].length == 2) matchArray[2] = "20" + matchArray[2];
		
			// yy mm dd
			courseDate = new Date();
			courseDate.setFullYear(matchArray[2], matchArray[1]-1, matchArray[0])
		
			if (courseDate < today) {
				jQuery(this).remove();					
			}		
		}
	
	});  
}