function MM_jumpMenu(targ,selObj,restore){ //v3.0
  var target = selObj.options[selObj.selectedIndex].value;
  if(target.indexOf("'")){
  	target = target.replace("'", "%27");
  }
  eval(targ+".location='"+target+"'");
  if (restore) selObj.selectedIndex=0;
}

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_jumpMenuGo(selName,targ,restore){ //v3.0
   var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}

// textarea character counter
function limitChars(textid, infodiv, limit){
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	if(textlength > limit){
		$('#' + infodiv).html('<label for="' + textid + '" class="error"><strong>You have reached the '+limit+' character limit. No further input is permitted.</strong></label>');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	} else {
		$('#' + infodiv).html((limit - textlength) + ' characters remaining');
		return true;
	}
}

$(document).ready(function(){
	// Display feedback form
	$("#pageFeedbackCont").show();
	
	// Table sorter (default sort defined in <table> tag)
	$("table").tablesorter();

	// Tool tips
	$(".line a").tipsy({gravity: 's', fade: true});
	
	// Clear common form field UI actions
	$("#subscribe-top #email").bind("click keypress", function(){
		var value = $(this).val();
		if(value == "Enter email"){
			$(this).val("");
		}
	});
	$("#searchform").submit(function() {
		var value = $("#searchText").val();
		if(value == ""){
			alert("You have not entered a search term.");
			$("#searchText").focus();
			return false;
		} else {
			return true;
		}
    });
		
	// Textarea counter (uses limitChars())
	$('textarea').keyup(function(){
		var $name = $(this).attr("id");
		var $charLimit = $(this).attr("maxlength");
		if($charLimit){
			limitChars($name, $name+'Count', $charLimit);
		}
});
		
	// Close box confirm
	$('span.close').click(function(){
		$('.confirm').hide("slow");
	});
	
	// Confirm account cancellation
	$("#cancel").bind("click keypress", function(){
        option = confirm("You are about to cancel your registration. Do you wish to continue?");
		if(option){
			return true;
		} else {
			return false;
		}
	});
	
	// Subscribe email validation
	$("#Subscribe").validate({
		rules: {
			"ResponseURL": {
				email: true,
				required: true
            }
		},
		messages: {
			"ResponseURL": "Please enter a valid email address"
		},
		errorLabelContainer: "#emailError"
	});
	
	$("#Subscribe #ResponseURL").bind("click keypress", function(){
		var value = $(this).val();
		if(value == "Enter email address"){
			$(this).val("");
		}
	});
	
	// Reset button confirmation
	$("#Reset").click(function(){
        option = confirm("Resetting the form will erase all entered text. Do you wish to continue?");
		if(option){
			validator.resetForm( );
			$("div.charcount").html("");
			if ( $("#username").length > 0 ) {
				$("#username").focus();
			} else if ( $("#feedbackName").length > 0 ) {
				$("#feedbackName").focus();
			} else if ( $("#text").length > 0 ) {
				$("#text").focus();
			} else if ( $("#feedbackText").length > 0 ) {
				$("#feedbackText").focus();
			}
		} else {
			return false;
		}
	});
	
	// Page feedback
	$("a.rate").click(function(e){
		e.preventDefault();
		//Clear previous errors
		if($("p.failure").length != 0){
			$("p.failure").remove();
		}
		target = $(this).attr("href");
		dataError = "<p class='failure'>An error has occured and your feedback has not been submitted. Please try again.</p>";
		$.get(target, function(data){
			if (data.indexOf("<p class='success'>") >= 0){
				$("#feedbackRate").fadeOut('fast', function(){
					$(data).insertAfter("p.question");
				});
			} else if (data.indexOf("Agent done") >= 0){
				$(dataError + data).insertAfter("#feedbackRate");
			} else {
				$(data).insertAfter("#feedbackRate");
			}
		});
	});
	
		
	$("form#PageFeedback").submit(function(e){
		e.preventDefault();
		//Clear previous errors
		if($("p.failure").length != 0){
			$("p.failure").remove();
		}
		//Check for input
		if(($("#comments").val().length == 0) || ($("#captchaInput").val().length != 3)){
			$("<p class='failure'>Please ensure that you've entered a comment and the requested digits from the real person check.</p>").insertAfter("form#PageFeedback input.button");
		} else {
			target = $(this).attr("action");
			data = $(this).serialize();
			dataError = "<p class='failure'>An error has occured and your feedback has not been submitted. Please try again.</p>";
			$.ajax({
			  type: 'POST',
			  url: target,
			  data: data,
			  success: function(data){
				if (data.indexOf("<p class='success'>") >= 0){
					$("#feedbackCont").fadeOut('fast', function(){
						$(data).insertAfter("form#PageFeedback label.comments");
					});
				} else {
					if(data.indexOf("Agent done") >= 0){
						data = dataError;
					}
					$(data).insertAfter("form#PageFeedback input.button");
				}
			  },
			  failure: function(){
				dataError.insertAfter("form#PageFeedback input.button");
			  },
			  dataType: 'html'
			});
		}
	})
	
	$("form#PageFeedback #comments").focus(function(){
		//if($(this).val().length>0){ 
			$("#commentsSub").slideDown("slow");
		//}
	});
	
	$("form#PageFeedback a#info").click(function(e){
		e.preventDefault();
		$("p.rpInfo").slideDown('slow');
	});
		
	// Footer publications
	$("#footer a.toggle").click(function(e){
		e.preventDefault();
		var ul = $(this).next("ul");
		var par = $(this).parent("li");
		if(par.hasClass("plus")){
			ul.slideDown("slow");
			par.removeClass("plus").addClass("minus");
		} else {
			ul.slideUp("slow");
			par.removeClass("minus").addClass("plus");
		}
	});

	$("a#allPubs").click(function(e){
		e.preventDefault();
		if($(this).html() == "Expand all"){
			$("#footer ul li ul").slideDown("slow");
			$("#footer ul li.plus").removeClass("plus").addClass("minus");
			$(this).html("Contract all");
		} else {
			$("#footer ul li. ul").slideUp("slow");
			$("#footer ul li.minus").removeClass("minus").addClass("plus");
			$(this).html("Expand all");
		}
	});
	
});

