// Import FAT

document.write('<script type="text/javascript" src="/scripts/fat.js"></script>');

// Import AJAX

document.write('<script type="text/javascript" src="/scripts/AjaxRequest.js"></script>');

// Popup handling

	var popShowing = false;

	// Show the contact box
	function showContactPop() {
	
		document.getElementById('contactpop').style.display='block';
		popShowing = true;
		
	}

	// Hide the contact box	
	function hideContactPop() {
	
		popShowing = false;
		setTimeout(function() {
	
			if (!popShowing) {
				document.getElementById('contactpop').style.display='none';

				// Restore notice				
	      	change_notice('info-icon.png', 0, 'Please try the <a href="/info">information</a> pages before sending a question!');
				
			}
		
		}, 1350);
	
	}
	
	// Select Henrik in the contact box
	function selectHenrik() {
	
		document.getElementById('popTo').value = 'henrik';
	
		document.getElementById('johannaimg').className='bordered faded';
		document.getElementById('henrikimg').className='bordered';
	
		document.getElementById('johannatitle').style.color='#CCC';
		document.getElementById('henriktitle').style.color='';
	
	}
	
	// Select Johanna in the contact box
	function selectJohanna() {
	
		document.getElementById('popTo').value = 'johanna';
	
		document.getElementById('johannaimg').className='bordered';
		document.getElementById('henrikimg').className='bordered faded';
	
		document.getElementById('johannatitle').style.color='';
		document.getElementById('henriktitle').style.color='#CCC';
	
	}
	
	// Change the notice icon and text, and optionally FAT it
	function change_notice(icon, fade, text) {
	
		var message = document.getElementById('popMessage');
		var notice = document.getElementById('popNotice');
	
		notice.style.backgroundImage = 'url(/imgs/'+icon+')';
		notice.innerHTML = text;
		
		if (fade)
			Fat.fade_element("popNotice", 30, 3000, "#FFFF33", "#FFFFEE");
		
	}
	
	// Submit the contact form
	function submitContact() {
	
		var email = document.getElementById('popFrom');
		var message = document.getElementById('popMessage');
		var to = document.getElementById('popTo');
		var from = document.getElementById('popFrom');
		var notice = document.getElementById('popNotice');
		
	
		// Validate
		
		if (document.getElementById('popMessage').value.length < 1) {
		
			// No message entered
		
      	change_notice('warning-icon.png', 1, 'You forgot to enter a message!');
			message.focus();
			
		}
		//else if (email.value && !email.value.match(/^[._a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/)) {
		
			// Not a valid e-mail address
			
      	//change_notice('warning-icon.png', 1, 'Please enter a real e-mail address, or none at all!');
			//email.focus();
			//email.select();
			
		//}
		else {
		
			// Validated - go on an send it
			
			var message = document.getElementById('popMessage');
			var notice = document.getElementById('popNotice');
			
			// Send mail

		  AjaxRequest.post({

		      'url':'/incs/contactpop.php',
		      'mess':message.value,
		      'to':to.value,
		      'from':from.value,
		      'onLoading':function() {
		      
		      	change_notice('working-icon.gif', 0, 'Sending&hellip;');
		      
		      },
		      'onError':function() {
		      
		      	change_notice('warning-icon.png', 1, 'Could not send message! Please use <a href="/feedback">ordinary e-mail</a> instead.');
		      
		      },
				'timeout':30000,
		      'onTimeout':function() {
		      
		      	change_notice('warning-icon.png', 1, 'The operation timed out. Try again or use <a href="/feedback">e-mail</a> instead.');
		      
		      },
				'onSuccess':function() {

		      	change_notice('ok-icon.png', 1, 'Your message has been sent!');
					message.value = '';
		     
				}

			});
			
		}
			
	}
