var HIDE_FORM = false;

$(document).ready(function(){
	if(!HIDE_FORM && $("div.booking a:first").length > 0){
		var onclick = $("div.booking a:first").attr("onclick").toString();
		// onclick = function onclick(event) { bookCourse(4) };
		var id = extractNumber(onclick);
		if(!isNaN(id)){
			bookCourse(id);
		}
	}
});

function extractNumber(price) {  
   return parseFloat(price.replace(/\./g, '').replace(/,/g,'.').replace(/[^\d\.]/g,''), 10);
}

function bookCourse(id)
{
	$("div.booking a").remove();
	$.ajax({
		type: "POST",
		url: themePath+"/elements/booking/booking.php",
		data: "id="+id,
		success: function(html){
			$("div.booking").append(html);
		}
	});	
}

function onBookingSubmit(form)
{
	$("div.booking").append('<div class="status" style="height:'+$(form).height()+'px">Vennligst vent...</div>');
	$(form).children().hide();
	/*var data = "";
	$(":input", form).each(function(i){
		if(this.type == 'checkbox')
		{
			data += $(this).attr("name")+"="+this.checked;
		}
		else
		{
			data += $(this).attr("name")+"="+$(this).val();
		}
		data += "&";
	});
	$(":textarea", form).each(function(i){
		data += $(this).attr("name")+"="+$(this).val();
		data += "&";
	});*/
	$.ajax({
 			url: themePath+"/elements/booking/submitbooking.php",
 			type: "POST",
 			data: $("div.booking").find("form").serialize(),
		cache: false,
		success: function(errors){
        	errors = $.trim(errors);
        	if(errors == ""){
				$("div.booking div.status").html('Takk for din p&aring;melding!');
        	}
        	else{
				$("div.booking div.status").html(errors);
        	}
		},
        error: function(XMLHttpRequest, textStatus, errorThrown){
        	alert(XMLHttpRequest+" "+textStatus+" "+errorThrown);
        }
	});
}

