//this function includes all necessary js files for the application  
function include(file)  
{  
  
  var script  = document.createElement('script');  
  script.src  = file;  
  script.type = 'text/javascript';  
  script.defer = true;  
  
 document.getElementsByTagName('head').item(0).appendChild(script);  
   
}  

function docLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "doc") 
     anchor.target = "_blank"; 
 } 
} 
window.onload = docLinks;

//Some functions that should be supported by javascript by default!
function IsEmail(e) {
	if (e.match(/^(?:([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?))$/)) {
		return true;
	} else {
		return false;
	}
}

//JQuery stuff:
$(function() {
	//Handle email image links:
	$('.emailimg').click(function() {
		var src = $(this).attr('src');
		src = src.replace(/.+text=(.+)&text2=(.+)/g, "$1@$2");
		document.location = 'mailto:' + src;
	});
	$('.sphighlight').animate({opacity:0.6}, 'slow').animate({opacity:1.0}, 'slow').animate({opacity:0.4}, 'slow').animate({opacity:1.0}, 'slow');
	// Table sorters:
	$("#sepccertmemberstable").tablesorter();
	//Handle feedback
	//vars
	var $wrap = $('#Feedback');
	var $btnOpen = $('#Feedback a.feedbackbutton');
	var $input = $('#FeedbackInput');
	var $btnCancel = $('#FeedbackInputCancel');
	var $btnSend = $('#FeedbackInputSend');
	var $fldEmail = $('#FeedbackInputEmail');
	var $fldMsg = $('#FeedbackInputMessage');
	var $status = $('#FeedbackStatus');
	
	//Display the feedback area
	$wrap.show('fast');
	//Listeners
	$btnOpen.click(function() {
		$input.show('fast');
		return false;
	});
	$btnCancel.click(function() {
		$input.hide('fast'); 
	});
	$btnSend.click(function() {
		//vars
		var action = 'feedback';
		var url = '/inc/office.php';
		var email = $fldEmail.val();
		var msg = jQuery.trim($fldMsg.val());
		var $error = false;
		var $emailWarning = $('#FeedbackInputEmailWarning');
		var $msgWarning = $('#FeedbackInputMsgWarning');
		if (email && email.length > 0 && !IsEmail(email)) {
			if ($emailWarning.html() == null || $emailWarning.html() == "") {
			  $fldEmail.after('<p id="FeedbackInputEmailWarning">Sorry, your email address was not recognised</p>');
			} else {
			  $emailWarning.animate({opacity:0.2});
			  $emailWarning.animate({opacity:1.0});
			}
			$error = true;
			//return;
		} else {
			if ($emailWarning.html()) $emailWarning.html("");
		}
		if (!msg) {
			if ($msgWarning.html() == null || $msgWarning.html() == "") {
			  $fldMsg.after('<p id="FeedbackInputMsgWarning">Please enter a message before submitting</p>');
			} else {
			  $msgWarning.animate({opacity:0.2});
			  $msgWarning.animate({opacity:1.0});
			}
			$error = true;
			//return;
		} else {
		  if ($msgWarning.html()) $msgWarning.html("");
		}
		if ($error === true) return;
		//Signal wait
		$status.html('<img src="/images/style/ajax-loader.gif" />');
		$.post(url,{a:action,e:email,m:msg},
		function(data) {
			//End wait
			$status.html('<p>Feedback submitted - thank you for your time</p>');
			$wrap.animate({opacity: 0.0}, 3000);
		});
		$input.hide(); 
	});
});