window.addEvent( 'domready', function() {
	var aBookmarks = $$( 'a.bookmark' );
	aBookmarks.each( function( item, index ) {
		item.addEvent( "click", function( event ) {
			
			var sLocation = window.location.href;
			var sTitle = 'Armstrong Watson - ' + $$( '#content' ).getFirst( 'h2' ).get('text');
			
			
			if( document.all && !window.opera ){
				window.external.AddFavorite( sLocation, sTitle );
				return false;
			} else {
				alert( "Please press CTRL + D to bookmark this page" );
			}
			
			event.stop();
		});
	});
	
	
	
	// Set up any accordians on the page
	var aAccordians = $$( 'div.accordion' );
	aAccordians.each( function( item, index ) {
		var oAccordian = new Accordion( item, 'div.toggler', 'div.element', {
			opacity: false,
			onActive: function(toggler, element){
				//toggler.setStyle('color', '#41464D');
			},
			onBackground: function(toggler, element){
				//toggler.setStyle('color', '#528CE0');
			}
		});
	});
	
	// Add a confirmation popup to any element with the confirm class
	var aConfirm = $$( '.confirm' );
	aConfirm.each( function( item, index ) {
		
		item.addEvent( 'click', function( event ) {
			var bConfirm = confirm( "Are you sure?" )
			
			if( !bConfirm ) {
				Event.stop( event );
			}
		});
	});
	
	// Collects a list of anchors which have class="ajax".
	// When one of these link are clicked the page content is updated through ajax rather than refreshing the whole page
	var ajaxAnchors = document.getElements( 'a.ajax' );
	ajaxAnchors.each( function( item, index ){
		item.addEvent( "click", function( event ) {
			$( 'content' ).load( item.getProperty( 'href' ) );
			event.stop();
		});
	});
	
	// Add a popup to links
	var aConfirm = $$( '.throwpop' );
	aConfirm.each( function( item, index ) {
		
		item.addEvent( 'click', function( event ) {
			Event.stop( event );
			
			var sTarget = event.target.getProperty( "href" );
			
			var w = 800;
			var h = 600;
			var LeftPosition=(screen.width)?(screen.width-w)/2:100;
			var TopPosition=(screen.height)?(screen.height-h)/2:100;
			var scroll = 1;
			
			var sSettings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=yes,directories=no,status=yes,menubar=yes,toolbar=yes,resizable=yes';
			
			win=window.open( sTarget, "", sSettings );
		});
	});
	
	
	
	// Add tool tips
	var oTipAccordian = new Tips( '.tip_accordian' );
	$$( '.tip_accordian' ).store( 'tip:text', '<p>To make an accordion, you must wrap the <br />content in --accordion-- and --/accordion--.</p><p>Any content inside these two tags which <br />has been given the HEADING 2 style will<br />become a toggler, whilst any other content <br />will be displayed in the accordion body.</p><p>--accordion--<br />&nbsp;Heading 2<br />&nbsp;Lorem Ipsum<br />--/accordion--</p>' );
	
	oTipAccordian.addEvent('show', function(tip){
		tip.fade('in');
	});

	oTipAccordian.addEvent('hide', function(tip){
		tip.fade('out');
	});
	
	// Make sure that clicking on tool tips does nothing
	var aTips = $$( '.tooltip' );
	aTips.each( function( item, index ) {
		
		item.addEvent( 'click', function( event ) {
			Event.stop( event );
		});
	});
	
	
	

	
});



//EMAIL VALIDATION FOR DOWNLOADS

//Shows the form
function dataCapture(download){
	
	$('download_url').set('value', download);
	trigger = $('email_login').get('value');

	if(trigger == 'yes'){
		window.open(download,"File Download","menubar=1,resizable=1,width=350,height=250");
		
	}
	else{
			$('data_collection').setStyle('visibility','visible');
			$('dataForm').setStyle('visibility','visible');
	}


}

//Hides the form
function close_box(){
	$('data_collection').setStyle('visibility','hidden');
	$('dataForm').setStyle('visibility','hidden');

}

//Validates the email and sends to sql
function validateEmail(){
		
		str = $("email_address").get('value');
		downloadurl = $("download_url").get('value');
		
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		var error_check = false;
		
		if (str.indexOf(at)==-1){
		   error_check = true;

		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   error_check = true;

		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    error_check = true;

		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    error_check = true;
	
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    error_check = true;
	
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    error_check = true;

		 }
		
		 if (str.indexOf(" ")!=-1){
		    error_check = true;

		 }
		 
		 if(error_check == false){
			 //Downloads the PDF
			 //window.open(downloadurl,"File Download","menubar=1,resizable=1,width=350,height=250");
			document.location.href=downloadurl;
			//Turns the popup of
				$('email_login').set('value', 'yes');
				
			//Sends the Ajax request to the forms controller
			var myRequest = new Request({method: 'post', url: '/form/addemail'});
				myRequest.send('url=' + str);
				close_box();
				return true;
				
			 
		 }
		 
		 else{
		
			if (str==null || str==""){
				alert("Please Enter your Email Address");

			}
	
			else {
				alert(str + " is not a valid email address!");

			}
		 }

	return false;
	
}


