// set up variables
var halfOpacity = 0.5;
var opaque = 1;
var transparent = 0;
var zeroHeight = 0;
var fullHeight = 550;

window.addEvent('domready',function() {
	
	// set up open & close
	var salesEnquiry = $('SalesEnquiry');	
	var closeForm = $('SalesEnquiry');
	

	// click to open lightbox
	salesEnquiry.addEvent('click', function(e) {
		
		// stops link being followed
		e.stop();
		
		//set up overlay & click to close lightbox
		var createSalesOverlay = new Element ('div', {
				'id': 'salesOverlay',
				'opacity': 0,
				'events': {
					'click': function() {
						
						//remove overlay
						var hideOverlay = new Fx.Morph('salesOverlay', {duration: 1000});
							hideOverlay.start({
								'opacity': [halfOpacity, transparent]
							});
							
						//remove form
						var hideLightbox = new Fx.Morph('formHolder', {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
							hideLightbox.start({
								'opacity': [opaque, transparent]
							}).chain(function(){
									$('salesOverlay').destroy();
									$('formHolder').destroy();																 
							});
					}
				}
		});
		
		var createForm = new Element ('div', {
				'id': 'salesForm',
				'opacity': 1
		});
		
		var formHolder = new Element ('div', {
				'id': 'formHolder',
				'opacity': 0
			
		});
		
		function checkWholeForm(theForm) {
    		var why = "";
		    why += isEmpty(theForm.name.value, 'Name');
		    why += isEmpty(theForm.company.value, 'Company');
		    why += isEmpty(theForm.postcode.value, 'Postcode');
		    why += isEmpty(theForm.email.value, 'Email');
		    why += checkEmail(theForm.email.value);

		    if (why != "") {
		       alert(why);
		       return false;
		    }
			return true;
		}
		
		
		// set up close button
		var closeForm = new Element('a', {
				'class': 'formClose',
				'id': 'formClose',
				'title': 'Close',
    			'events': {
        			'click': function(){
						
						//remove overlay
						var hideOverlay = new Fx.Morph('salesOverlay', {duration: 1000});
							hideOverlay.start({
								'opacity': [halfOpacity, transparent]
							});
							
						//remove form
						var hideLightbox = new Fx.Morph('formHolder', {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
							hideLightbox.start({
								'opacity': [opaque, transparent]
							}).chain(function(){
									$('salesOverlay').destroy();
									$('formHolder').destroy();																 
							});
					}
				}
		});
		
		var CreatePrint = new Element('a', {
		
		'class': 'print',
		'id': 'print',
		'href': '#',
		'events': {
			'click': function(){
					window.print();	
			}
			}
		
		});
		
		
	 
	// inject into DOM						
	createSalesOverlay.injectInside(document.body);
	formHolder.injectInside(document.body);
	createForm.injectInside('formHolder');
	closeForm.injectAfter('salesForm');
	
	// this loads a local file with the from on it - if it's hosted externally, this will change to a page with an iframe
	createForm.load("sales_form.html");
																
	// fade it all in
	$('salesOverlay').tween('opacity', [transparent, halfOpacity]);
	
	var revealLightbox = new Fx.Morph('formHolder', {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
		revealLightbox.start({
				'opacity': [transparent, opaque]
		});
		
	});
	
	


									
});