//-------------------------------------------------------------------------
// Global javascript file which is needed on every page. 
//-------------------------------------------------------------------------

/**
 * Registration form submission
 */
 
function register () {
	
	var f = $("#keepinformed form");
	
	var params = "email=" + f.find("[@name=email]").fieldValue()
			   + "&format=" + f.find("[@name=format]").fieldValue()
			   + "&renderAs=JSON";
	
	// Request
	 $.ajax({
	   type: 		"POST",
	   url: 		"register.php",
	   dataType: 	"json",
	   data:		params,
	   async:		true,
	   success:		function (response) {
		   
		   // If warnings are present, show the first warning
		   if (response.hasWarnings == "true") {
			   alert(response.warnings[0][1]);
		   }
		   // Otherwise, no warnings - show success message!
		   else {
				alert("Thank you for registering!"); 
				$("#keepinformed form").find("[@name=email]").val("");
		   }
		   
	   }
	});

	return false;
	
}

/**
 * Determine whether large or regular fonts are being used.
 */

function checkFonts () {
	
	// Check if a large font cookie exists
	if (document.cookie.indexOf("fontsize=large") > -1) {
		largeFonts();	
	}
	// Otherwise, default to regular fonts
	else {
		defaultFonts();	
	}
	
}

/**
 * Enable the large fonts CSS file and set a cookie for large fonts.
 */

function largeFonts () {
	
	// Enable large CSS
	$(".css-large")
	.attr("disabled", false)
	.attr("rel", "stylesheet");
	
	setFontCookie("large");

}

/**
 * Disable the large fonts CSS file and set a cookie for small fonts.
 */

function defaultFonts () {

	$(".css-large")
	.attr("disabled", true)
	.attr("rel", "alternate");
	
	setFontCookie("small");	
	
}

/**
 * Sets a font size cookie with the specified value.
 */
 
function setFontCookie (size) {
	
	// Set cookie
	var d = new Date();
	d.setTime(d.getTime() + (365 * 24 * 60 * 60 * 1000)); // 1 year
	document.cookie = "fontsize=" + size + "; "
					+ "expires=" + d.toGMTString() + "; "
					+ "path=/";	
	
}

/**
 * This function is bound to an AJAX request and will parse the RSS XML
 * and populate the whats new box with the latest 3 RSS items.
 */

function loadRSS (e) {

	$("#whatsnew").append(e);
	
}

/**
 * Replaces all external links with a confirm dialog before redirecting.
 */

function confirmExternalLinks () {
	
	if (document.getElementsByTagName) {
	
		var anchors = document.getElementsByTagName("A");
		
		var baseurl = new String(document.location);
			baseurl = baseurl.substr(0, baseurl.indexOf(".au/") + 4);

		// For each achor in the document, check if it begins with
		// http:// (which assumes it is external) and if it does
		// replace the contents if it's not for a human services agency.
		for (var i = 0; i < anchors.length; i++) {
			
			var href = anchors[i].getAttribute("href");
			var replace;
			
			if (href && href != "undefined") {

				href = new String(href);

				if (href.indexOf("http://") > -1 && href.indexOf(baseurl) == -1) {
					
					// Check for .gov.au for government message
				if ( href.indexOf(".gov.au")> -1 ){
				
						if ( (href.indexOf("http://www.accesscard.gov.au") == -1) && (href.indexOf("http://www.jca.gov.au") == -1) && (href.indexOf("http://myaccount.humanservices.gov.au") == -1) )
						{
					
							anchors[i].onclick = function () {
							var message = "You are now leaving the Department of Human Services website.\n"
										+ "The site you are entering is maintained and funded by "
										+ "the Australian Government.\n"
										+ "Please click:\n"
										+ "- OK to proceed or\n"
										+ "- CANCEL to return to the current page";
										
							return confirm(message);
							}
						}
					
					
				}
					// Any non-government sites have a generic message
				else 
				{
						
						anchors[i].onclick = function () {
							var message = "You are now leaving the Department of Human Services website.\n"
										+ "The site you are entering is NOT maintained and funded by "
										+ "the Australian Government.\n"
										+ "Please click:\n"
										+ "- OK to proceed or\n"
										+ "- CANCEL to return to the current page";
										
							return confirm(message);
						}						
						
					}
				
				}
				
			}
			
		}
	
	}
	
	/*
	$("a[@href]:contains('.gov.au')").each(function () {
		
		$(this).click(function(){
			var msg = "You have clicked on .gov.au link that will open in a new window";
			return confirm(msg);
						   });
						  
	});
	
	$("a[@href]").each(function () {
			var url = $(this).attr('href');
			//alert(url);
				if ((url.indexOf('www.humanservices') == -1)
				&& (url.indexOf('.gov.au') == -1)
				&& (url.indexOf('#') == -1)
				&& (url.indexOf('/') > -1))
				{				
					$(this).click(function(){
					var msg = "You are leaving ";
					return confirm(msg);
			   });
			}		
			
	});

	$("a[@href]").each(function () {
								 
		var href = $(this).attr("href");
		
		if
								 
								 
	});
	*/	

}


/**
 * jQuery page initialising
 */

$(document).ready(function () {


	//---------------------------------------------------------------------
	// Search form
	//---------------------------------------------------------------------
	
	$(".action-search").click(function () {
		$("#search form").submit();											
	});	

	//---------------------------------------------------------------------
	// Registration form
	//---------------------------------------------------------------------	

	$("#keepinformed .action-register").click(register);
	$("#keepinformed form").submit(register);
	
	//---------------------------------------------------------------------
	// Load what's new
	//---------------------------------------------------------------------
	
	 $.ajax({
	   type: 		"GET",
	   url: 		"http://www.humanservices.gov.au/rss/humanservices.rss.html",
	   dataType: 	"html",
	   async:		false,
	   success:		loadRSS
	 });
	
	//---------------------------------------------------------------------
	// CSS font size changer
	//---------------------------------------------------------------------
	
	checkFonts();
	
	$(".action-font-increase").click(largeFonts);
	$(".action-font-decrease").click(defaultFonts);
	
	//---------------------------------------------------------------------
	// Warnings for external sites
	//---------------------------------------------------------------------
	
	confirmExternalLinks();

});