window.addEvent('domready', function(){
	// check to see whether the user has a "landing page" cookie
	var landingPage = Cookie.read('landingPage', {
		path: '/'
	});
	if ( ! landingPage || landingPage == null ){
		// there is no cookie. write one down (in case the user wants to register)
		Cookie.write('landingPage', window.location, {
			path: '/'
		});
	}
	
	// check to see whether the registration form is present on this page
	var registrationForm = document.id('customerRegistrationForm');
	if ( registrationForm ){
		var input = registrationForm.getElement('input[name=landingPage]');
		if ( input ){
			input.set('value', Cookie.read('landingPage', { path: '/' } ) );
		}
	}
	
	var wrapper = document.id('downloadTermsAndConditions');
	if ( wrapper ){
		var declineButton = document.id('downloadTermsAndConditionsDecline');
		declineButton.addEvent('click', function(event){
			event.preventDefault();
			wrapper.setStyle('display', 'none');
		});
		document.getElements('a[href*=requireTandC]').each(function(element){
			element.addEvent('click', function(event){
				event.preventDefault();
				
				wrapper.setStyle('display', 'block');
				var windowDimensions = window.getSize();
				var wrapperDimension = wrapper.getSize();
				wrapper.setStyles({
					left: (windowDimensions.x / 2) - (wrapperDimension.x / 2)+"px",
					top: (windowDimensions.y / 2) - (wrapperDimension.y / 2)+"px"
				});
				
				var agreeButton = document.id('downloadTermsAndConditionsAgree');
				agreeButton.removeEvents();
				agreeButton.set('href', element.get('href'));
				agreeButton.addEvent('click', function(event){
					event.preventDefault();
					wrapper.setStyle('display', 'none');
					Cookie.write('agreedToTandCs', 'true', {
						path: '/'
					});
					
					var uri = new URI(element.get('href'));
					uri.setData({});			
					uri.go();
				});
			});
		});
	}
	
	/*
	var registrationForm = document.id('customerRegistrationForm');
	if ( registrationForm ){
		registrationForm.getElements('input[type=radio]').each(function(element){
			element.addEvent('click', function(event){
				if ( document.id(event.target).get('value') == 'new' ){
					var newFieldset = document.id('newCustomer').setStyle('display', 'block');
				} else {
					var newFieldset = document.id('newCustomer').setStyle('display', 'none');
				}
			});
		});
	}
	*/
	
	var url = window.location.toString();
	if ( url.indexOf('isNew=') > 0 ){
		// show a "thank you" modal
		var wrapper = document.id('registrationConfirmation');
		wrapper.setStyle('display', 'block');
		var windowDimensions = window.getSize();
		var wrapperDimension = wrapper.getSize();
		wrapper.setStyles({
			left: (windowDimensions.x / 2) - (wrapperDimension.x / 2)+"px",
			top: (windowDimensions.y / 2) - (wrapperDimension.y / 2)+"px",
			'z-index': 10000000
		});
		aElement = wrapper.getElement('a');
		
		var urlPos = url.indexOf('url=');
		if ( urlPos > 0 ){
			url = url.substr(urlPos + 4);
			
			aElement.set('href', url);
		} else {
			wrapper.getElement('p').set('html', 'Thank you for registering. Please click on OK to close this window');	
		}
		
		aElement.addEvent('click', function(){
			wrapper.setStyle('display', 'none');
		});
	}
});
