// Adds a function to the window onunload event
function addUnloadEvent(func) {
  var oldOnunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  }
  else {
    window.onunload = function() {
      oldOnunload();
      func();
    }
  }
}

$(function() {
				
	//show secondary email radios if secondary contact is not empty
	if ($('#secEmail').val() != '') {
		$('#correspondenceSelection').show();
	}
	
	$('#secEmail').keyup(function() {
			if ($(this).val().replace(/ /g, '') != '') {
				$('#correspondenceSelection').show();
			} else {
				$('#correspondenceSelection').hide();
			}
		});	
	
					 
					 
	// zebra striping tables
	$("table tr:nth-child(even)").addClass("alt");
	
	// add a class "last" to the last div with a class "section" (to remove the bottom border)
	$("div.section:last").addClass("last");
	
	// open external link in a new window - needs rel="external"
	$("a[rel=external]").click(function(){
		window.open(this.href);
		return false;
	});
	
	// open PDF file in a new SIZED window - needs rel="pdf"
	$("a[rel=pdf]").click(function(){
		window.open(this.href,'outline','height=560,width=760,top=0,left=0,resizable=yes');
		return false;
	});
	
	// open modular in a new SIZED window - needs rel="modular"
	$("a[rel=modular]").click(function(){
		window.open(this.href,'modular','height=720,width=960,top=0,left=0,resizable=yes');
		return false;
	});
	
	// open modular in a new SIZED window, with scroll - needs rel="modular"
	$("a[rel=modularAllOptions]").click(function(){
		var winmao = window.open(this.href,'modular','height=720,width=980,top=0,left=0,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,status=yes,menubar=yes');
		if (winmao) winmao.focus();
		return false;
	});
	
	//find print links and write the js link
	$("p.print").html('<a href="javascript:window.print();">Print / Imprimer</a>');
	
	// Focus Student Id field if 'Yes' radio button is clicked
	$("input#id").focus(function(){
		$("input#has-yes").attr("checked", true);
	});
	
	//hide the price if the course isn't running
	if (document.getElementById("not-running")) {
		$("span#price").hide();
	}

	$("#privateOnly").hide();
	
	if (document.getElementById('scope')) {
		if (document.getElementById('scope').checked) {
			$("#privateOnly").show();
		}
	}

	$("#scope").click(function(){
		if (this.checked) {
			$("#privateOnly").show();
		} else {
			$("#privateOnly").hide();
		}
	})
	
	
	//show PO field if PO option is selected
	var payVal = $('#payId').val();	
	if (payVal == '5') {
		$('#poField').show();
	} else {
		$('#poField').hide();
	}

	$("#payId").change(function() {
		if ($(this).val() == '5') {
			$('#poField').show();
		} else {
			$('#poField').hide();
		}
	});	
	
});








/*		|JQUERY EXTENSIONS			
			......................................................................		*/
jQuery.fn.fadeToggle = function(speed, easing, callback) { 
	return this.animate({opacity: 'toggle'}, speed, easing, callback); 
}; 

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
};

jQuery.fn.blindToggle = function(speed, easing, callback) {
  var h = this.height() + parseInt(this.css('paddingTop')) + parseInt(this.css('paddingBottom'));
  return this.animate({marginTop: parseInt(this.css('marginTop')) < 0 ? 0 : -h}, speed, easing, callback);  
};