function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function chkSendFriendForm() {
	var errArr = new Array();
	if (!$('#sendFriendTo').val().length || !isValidEmail($('#sendFriendTo').val())) {
		errArr[errArr.length] = 'Please enter a valid email address for your recipient.';
	}
	if (!$('#sendFriendFrom').val().length) {
		errArr[errArr.length] = 'Please add your name so your friend knows who it\'s from!';
	}
	if ($('#sendFriendFrom').val().length && !isValidEmail($('#sendFriendFrom').val())) {
		errArr[errArr.length] = 'Please ensure your email address is a valid email address';
	}
	if (errArr.length) {
		alert('Please complete the following:\n\n- '+errArr.join('\n- '));
		return false
	} else {
		$.get('/_ajax/sendToAFriend.cfm',{
			rand:Math.random(),
			sendFriendTo:$('#sendFriendTo').val(),
			sendFriendFrom:$('#sendFriendFrom').val(),
			sendFriendName:$('#sendFriendName').val(),
			sendFriendPageTitle:$('h1').text(),
			sendFriendPage:location.href
		},function(data) {
			$('#emailFriendThanksLink').click();	
		});	
	}
}	

function isValidEmail(email) {
		var emailRegExp = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,4}/ 
		var result = email.match(emailRegExp);
		if ((result && result[0].length != email.length) || !result) {
			return false
		} else {
			return true
		}
	}
	
function chkContactForm() {
	var errArr = new Array();
	if (!$('#title option:selected').length || !$('#title').val().length) {
		errArr[errArr.length] = "Title";
	}
	if (!$('#firstname').val().length) {
		errArr[errArr.length] = "Firstname";
	}
	if (!$('#lastname').val().length) {
		errArr[errArr.length] = "Lastname";
	}
	if (!$('#email').val().length || !isValidEmail($('#email').val())) {
		errArr[errArr.length] = "Email must be a valid email address";
	}
	if (!$('#telephone').val().length) {
		errArr[errArr.length] = "Telephone";
	}
	
	/*if (!$('#agree_dataProtection-yes:checked').length) {
		errArr[errArr.length] = "Please agree with the Data Protection Policy before continuing";
	}*/
	
	$.get('/_ajax/check_captcha.cfm',{rand:Math.random(),captcha:$('#captcha').val(),challenge:cpt},function(data) {
		//arrResponse = data.split('\n');
		if (data != 'pass') {
			errArr.push('You didn\'t enter the security words correctly. Please try again with the new words displayed below.');
			reCaptcha();
		} 
		if (errArr.length) {
			alert('Please complete the following:\n\n- '+errArr.join('\n- '));
		} else {
			$.post('/_ajax/contact_save.cfm',{
				   	rand:Math.random(),
					title:$('#title').val(),
					firstname:$('#firstname').val(),
					lastname:$('#lastname').val(),
					email:$('#email').val(),
					telephone:$('#telephone').val(),
					schoolName:$('#schoolName').val(),
					address_1:$('#address_1').val(),
					postcode:$('#postcode').val(),
					require_presentation:$('#require_presentation-yes:checked').length,
					require_trial:$('#require_trial-yes:checked').length,
					MIS_used:$('#MIS_used').val(),
					num_pupils:$('#num_pupils').val(),
					newsletter_subscribed:$('#newsletter_subscribed-yes:checked').length,
					enquiry:$('#contactEnquiry').val(),
					contact:0
					
					
				},function(data) {
				//alert('Thank you!\n\nWe\'ll respond to your enquiry shortly.');
				window.location.href='/page/thank-you';
			});
		}
	});
}	

var page = new Object();
	page.pageLink = encodeURIComponent(location.href);
	page.pageTitle = document.title;
	page.pageTitleRaw = document.title;
	page.pageTitle = encodeURIComponent(page.pageTitle);
	page.twitterLink = 'http://twitter.com/share?url='+page.pageLink+'&text='+page.pageTitle;
	page.fbLink = 'http://www.facebook.com/sharer.php?u='+page.pageLink+'&t='+page.pageTitle;
	page.linkedinLink = 'http://www.linkedin.com/shareArticle?mini=true&ro=false&trk=bookmarklet&title='+page.pageTitle+'&url='+page.pageLink;
	
	
	
function reCaptcha() {
	var nowDate = new Date();
	var imgName = encodeURIComponent(location.host) + nowDate.getTime() + '.jpgx';
	cpt = imgName;
	document.getElementById('captchaImg').src = 'http://www.opencaptcha.com/img/'+imgName;
}

function removeBlockQuoteParas(blockquote) {
	var contentArr = new Array();
	$('p',blockquote).each(function() {
		contentArr[contentArr.length] = $(this).html();
	});
	$('p',blockquote).eq(0).before(contentArr.join('<br /><br />'));
	$('p',blockquote).each(function() {
		$(this).remove();							
	});
	
}

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-24224349-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


$(document).ready(function(){
	// add a "rel" attrib if Opera 7+
	if(window.opera) {
		if ($("a.bookmark").attr("rel") != ""){ 
			$("a.bookmark").attr("rel","sidebar");
		}
	}

	$("a.bookmark").click(function(event){
		event.preventDefault(); 
		var url = location.href;
		var title = document.title;

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; 
		} else { 
			 alert('Unfortunately, this browser does not support the requested action, please bookmark this page manually.');
		}

	});
	if ($('.social').length) {
		$('#facebookLink').attr({'href':page.fbLink,'target':'_blank'});
		$('#twitterLink').attr({'href':page.twitterLink,'target':'_blank'});
		$('#linkedinLink').attr({'href':page.linkedinLink,'target':'_blank'});
	}
	if ($.fancybox) {
		$('#emailFriendLink,#emailFriendThanksLink').fancybox({titleShow:false});	
	}
	//$('#emailPageDocTitle strong').text($('h1').text());
	$('#emailPageDocTitle strong').text('iTRACK Primary - '+pt);
	$('#sendFriendPageTitle').val('iTRACK Primary - '+pt);
	//$('#sendFriendPageTitle').val(page.pageTitle);
	$('#sendFriendPage').val(page.pageLink);
	$("a[href^='http:']:not([href*='" + window.location.host + "'])").live('click', function(){
		$(this).attr('target','_blank'); 
 	});
	$('img[alt]').not('img[title]').each(function() {
		$(this).attr('title',$(this).attr('alt'));											  
	});
	if ($.fancybox) {
		$('.youtube,#demoVideo').click(function() {
				$.fancybox({
					titleShow:false,
					'href':this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
					'type': 'swf',
					'swf'			: {
						'wmode'		: 'transparent',
						'allowfullscreen'	: 'true'
					}
				});	
				return false;
		});
	}
	$('blockquote').each(function() {
		removeBlockQuoteParas(this);
	});
});

