/*
	This area holds all the core / plugin js that is used to work with 3rd party plugins or
	workarounds.
*/

$(function(){
	
	/* PNG FIX */
	$(document).pngFix();

	
	/* COLOR BOX POPUP (http://colorpowered.com/colorbox/) */
	$("a[rel='gallery']").colorbox();

	
	
	/* CUFON FONT REPLACEMENT
       Generate More (here(http://cufon.shoqolate.com/generate/) */
	Cufon.replace('.univers67condensedBold', { fontFamily: 'univers67condensedBold' });


	/* POPUP EVENT (replacement for target="_blank") */
	$( ".popup" ).live( "click", function( event ) {
		event.preventDefault();
		var sTarget = $( this ).attr( "href" );
		var iWidth = 800;
		var iHeight = 600;
		var iScrollbars = 2;
		var iLeft = (screen.width)?(screen.width-iWidth)/2:100;
		var iTop = (screen.height)?(screen.height-iHeight)/2:100;
		var sSettings='width='+iWidth+',height='+iHeight+',top='+iTop+',left='+iLeft+',scrollbars='+iScrollbars+',location=yes,directories=no,status=yes,menubar=yes,toolbar=yes,resizable=yes';
		win=window.open( sTarget, "", sSettings );
	} );

});
	







/*
*	Newsletter Form
*/
function newsletterForm(){
	//Set variables
	var forename = $("input[name='newsletter[forename]']").val();
	var surname = $("input[name='newsletter[surname]']").val();
	var email = $("input[name='newsletter[email]']").val();
	var confirm_email = $("input[name='newsletter[confirm_email]']").val();
	var reference = $("select[name='publication[reference]']").val();

		
	if(forename && surname && email && confirm_email){
		
		if(email != confirm_email){
			alert('Emails do not match!');
			return false;
		}
		
		if(validateEmail(email)){
			return true;
		}
		
		else{
			return false;
		}

	}
	else{
		alert('Please fill out all form fields');
		return false;
	}	
}





//Email Cleaner
function validateEmail(str){

		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){
			 return true;
			 
		 }
		 
		 else{
			
			if (str==null || str==""){
				alert("Please Enter your Email Address");

			}
	
			else {
				alert("'" + str + "' is not a valid email address!");
				return false;

			}
		 }	
}
