
contactForm = {}

contactForm.init = function() {

	// Create the fade-out effect

	$("body").append(
		'<div id="fade-out" class="overlay"></div>');

	if ($.browser.msie && $.browser.version == '6.0') {
		$("#fade-out").css({
			height: $(document).height().toString() + 'px',
			width: $(document).width().toString() + 'px'
		});
	}

	// Create the form itself
	var offsetLeft = Math.round($(window).width() / 2) - 250;
	var offsetTop = Math.round($(window).height() / 2) - 230;
	
	$("body").append(
		'<div id="contact-form" class="overlay" style="top:' + offsetTop + 'px; left:' + offsetLeft + 'px;">' + 
			'<h1>Timebestilling</h1>' +
			'<p>Send oss noen ord om hva det gjelder, så kontakter vi deg så snart som mulig.<br/><br/></p>' +
			'<p>' +
			'Navn:<br/><input class="form" id="name" type="text" /><br/>' +
			'Telefon:<br/><input class="form" id="phone" type="text" /><br/>' +
			'Epost:<br/><input class="form" id="email" type="text" /><br/><br/>' +
			
			'Overskrift:<br/><input class="form" id="title" type="text" /><br/>' +
			'Beskjed:<br/><textarea class="form" id="message" rows="8" cols="70" ></textarea><br/>' +
			'<input id="send" type="submit" value="Send" /><input id="abort" type="button" value="Avbryt" />' +
			'</p>' +
		'</div>');

	$("#contact-form .form").click(function() {
		$($(this)).css("background","white");
		if ($(this).val().match(/^Ugyldig\s.*$/)) {
			$(this).val("");
		}
	});

	// Set up the buttons
	$("#contact-form #abort").click(function() { $("#fade-out").click(); });
	$("#contact-form #send").click(function() { 

		var valid = true;

		// Validate phone number
		validPhone = /^\s*(\+\d{2})?(\s?\d){8}\s*$/;
		if (!$("#contact-form #phone").val().match(validPhone)) {
			$("#contact-form #phone").val("Ugyldig telefonnummer").css("background", "#ee9191");
			valid = false;
		} 

		// Validate email
		validEmail = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
		if (!$("#contact-form #email").val().match(validEmail)) {
			$("#contact-form #email").val("Ugyldig epostadresse").css("background", "#ee9191");
			valid = false;
		}

		// Highlight empty fields
		$("#contact-form .form").each(function() {
			if ($(this).val() == "") {
				$(this).css("background","#ee9191");	
				valid = false;
			}
		});

		if (valid) {

			var details = {
				name : $("#name").val(),
				phone : $("#phone").val(),
				email : $("#email").val(),
				title : $("#title").val(),
				message : $("#message").val()
			}

			var offsetLeft = Math.round($(window).width() / 2) - 150;
			var offsetTop = Math.round($(window).height() / 2) - 75;
			
			$.post("/includes/email.php", details, function(data) { 
				if (data == "1") {
					$("#contact-form").html(
						'<h1>Timebestilling</h1>' +
						'<p>Takk for din bestilling! Vi vil kontakte deg så raskt som mulig.<br/><br/>' + 
						'<a id="abort">Tilbake</a></p>'
					).animate({
						height : '150px',
						left : offsetLeft,
						top : offsetTop,
						width : '300px'
					}, 'fast');

					$("#abort").click(function() {
						$("#fade-out").trigger("click");
					});
				}
			});	
		}
	});
	
	// Show the form
	$("#fade-out").fadeTo(300, 0.6, function() {
		$("#contact-form").show();
	});

	// Remove the form when clicking somewhere in the background
	$("#fade-out").click(function () {
		$(".overlay").fadeOut(100, function() { 
			$(this).remove();
		});
	});
}

