// Active Ingredient Search box
// onclick
function clearInput(el) {
 	if(el.value == 'Type name here') {
	 	el.value = '';
		el.className = '';
	}
}

// onblur
function checkInput(el) {
	if(el.value == '' || el.value == ' ') {
		el.value = 'Type name here';
		el.className = 'mnf_keywordBlank';
	} else if (el.value != 'Type name here') {
                el.className = '';
        }
}
// End Active Ingredient Search box

$(document).ready(function() {	

// Set Metadata
function cleanTitle() {
    if ($("#dc_alt_title").text()!="") {
        $("#dc_title").remove();
    } else {
        $("#dc_alt_title").remove();
    }
}

function cleanQL() {
    $("#quick_links ul li").each(function (){
        if ($(this).text()=="") {
            $(this).remove();
        }
    });

    if (!($("#quick_links ul").children("li").length>0)) {
        $("#quick_links").remove();
    }
}


    cleanTitle();
    cleanQL();    
// End Set Metadata

// Create Redirect Onclicks  
	$('a[@href*=http://],a[@href*=https://]').each(function(){
      	var link = $(this).attr("href");
		var title=$(this).text();
		var linkHTML = $(this).html();
		if ($(this).attr("title") != null) title= this.getAttribute("title");
		
		if ((link.indexOf("nps.org.au") != -1) || (link.indexOf("npsradar.org.au") != -1) || (link.indexOf("nps.clients.squiz.net") != -1) || (link.indexOf("nps.funnelback.com") != -1) || (link.indexOf("pbs.gov.au") != -1)) {
			if (link.indexOf("nimc.nps.org.au/elearning/log/login.asp") != -1) {
				title = "Log-in to National Inpatient chart training";
				$(this).html("");
				$(this).attr("href", "http://www.nps.org.au/You_are_leaving_this_site_to_national_inpatient_chart_training?link=" + encodeURIComponent(link) + "&name=" + encodeURIComponent(title));
				$(this).html(linkHTML);
			} else if (link.indexOf("nimc.nps.org.au/elearning/log/register.asp") != -1) {
				title = "Register for National Inpatient chart training";
				$(this).html("");
				$(this).attr("href", "http://www.nps.org.au/You_are_leaving_this_site_to_national_inpatient_chart_training?link=" + encodeURIComponent(link) + "&name=" + encodeURIComponent(title));
				$(this).html(linkHTML);
			}
		} else {
			if (link.indexOf("australianprescriber") != -1) {			
                                $(this).html("");
				$(this).attr("href", "http://www.nps.org.au/You_are_leaving_this_site_to_Australian_Prescriber?link=" + encodeURIComponent(link) + "&name=" + encodeURIComponent(title));
				$(this).html(linkHTML);
			} else if ((link.indexOf("nps.unisa.edu.au") != -1) || (link.indexOf("npsprescribe.lamsinternational.com/lams") != -1)) {
				title = "Log-in to the National Prescribing Curriculum";
				$(this).html("");
				$(this).attr("href", "http://www.nps.org.au/You_are_leaving_this_site_to_national_prescribing_curriculum?link=" + encodeURIComponent(link) + "&name=" + encodeURIComponent(title));
				$(this).html(linkHTML);
			} else {
				$(this).html("");
				$(this).attr("href", "http://www.nps.org.au/You_are_leaving_this_site?link=" + encodeURIComponent(link) + "&name=" + encodeURIComponent(title));
				$(this).html(linkHTML);
			}
		}
   	});
// End Create Redirect Onclicks

// Change Date Formats
   $(".iso_date").each(function (i) {                // For each element with class iso_date
       if ($(this).text()!="") {                       // Check there is text to manipulate
           var isoDate = $(this).text().split(' ')[0]; // Grab the date only from the date/time
       } else {
           return true;                                // If no text move onto next .iso_date
       }
       isoDate = isoDate.split('-');                   // Split date into 3 tokens
       $(this).text(isoDate[2]+'/'+isoDate[1]+'/'+isoDate[0]); // Set text of current element to format: 28/10/2008
   });

   var isoDateMonthText = ["","January","February","March","April","May","June","July","August","September","October","November","December"];
   var isoDateMonthTextShort = ["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

   $(".iso_date_month").each(function (i) {                // For each element with class iso_date_month
       if ($(this).text()!="") {                       // Check there is text to manipulate
           var isoDateMonth = $(this).text().split(' ')[0]; // Grab the date only from the date/time
       } else {
           return true;                                // If no text move onto next .iso_date_month
       }
       isoDateMonth = isoDateMonth.split('-');                   // Split date into 3 tokens
       if (!((isoDateMonth[1] == 10) ||(isoDateMonth[1] == 11) || (isoDateMonth[1] == 12))) {
	  isoDateMonth[1] = isoDateMonth[1].substring(1,isoDateMonth[1].length);
       }

       $(this).text(isoDateMonthText[isoDateMonth[1]]+' '+isoDateMonth[0]); // Set text of current element to format: October 2008
   });

   $(".iso_date_month_short").each(function (i) {                // For each element with class iso_date_month
       if ($(this).text()!="") {                       // Check there is text to manipulate
           var isoDateMonth = $(this).text().split(' ')[0]; // Grab the date only from the date/time
       } else {
           return true;                                // If no text move onto next .iso_date_month
       }
       isoDateMonth = isoDateMonth.split('-');                   // Split date into 3 tokens
       if (!((isoDateMonth[1] == 10) ||(isoDateMonth[1] == 11) || (isoDateMonth[1] == 12))) {
	  isoDateMonth[1] = isoDateMonth[1].substring(1,isoDateMonth[1].length);
       }

       $(this).text(isoDateMonthTextShort[isoDateMonth[1]]+' '+isoDateMonth[0]); // Set text of current element to format: October 2008
   });

   $(".iso_date_day_month").each(function (i) {                // For each element with class iso_date_day_month
       if ($(this).text()!="") {                       // Check there is text to manipulate
           var isoDateDayMonth = $(this).text().split(' ')[0]; // Grab the date only from the date/time
       } else {
           return true;                                // If no text move onto next .iso_date_day_month
       }
       isoDateDayMonth = isoDateDayMonth.split('-');                   // Split date into 3 tokens
       if (!((isoDateDayMonth[1] == 10) ||(isoDateDayMonth[1] == 11) || (isoDateDayMonth[1] == 12))) {
	  isoDateDayMonth[1] = isoDateDayMonth[1].substring(1,isoDateDayMonth[1].length);
       }

       var day = isoDateDayMonth[2];
	   if (day.indexOf("0")==0){
	   	day = day.substring(1);
	   }

       $(this).text(day+' '+isoDateMonthText[isoDateDayMonth[1]]+' '+isoDateDayMonth[0]); // Set text of current element to format: 28 October 2008
   });
// End Change Date Formats

// Change Content Type text
   var _correspondence = "Correspondence";
   var _complete = "Complete issue";
   var _review = "Review article";
   var _brief = "Brief item";
   var _web = "Web-only item";

   $(".content_type").each(function (i) {
       if ($(this).text().indexOf("Correspondence") != -1) {
           $(this).text(_correspondence);
       } else if ($(this).text().indexOf("Full") != -1) {
           $(this).text(_complete);
       } else if ($(this).text().indexOf("Review") != -1) {
           $(this).text(_review);
       } else if ($(this).text().indexOf("Brief") != -1) {
           $(this).text(_brief);
       } else if ($(this).text().indexOf("Web Only Item") != -1) {
           $(this).text(_web);
       }
   });
// End Content Type text

// PDF Gateway Cookie
function ngw()    {
    if ($("#ngw:checked"))    {
        setCookie("npsngw",1);
    }
    else    {
        setCookie("npsngw",0);
    }
}

function setCookie(c_name,value)    {
    document.cookie=c_name+ "=" +escape(value);
}

function getCookie(c_name)    {
    if (document.cookie.length>0)    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)    { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

    $("#ngw").click(ngw);

    var ck = getCookie("npsngw");
    if (ck == "1")    {
        $("#ngw").attr("checked","checked");
        var newLoc = document.getElementById("pdfurl").href;
        window.location.replace(newLoc);
    }
// End PDF Gateway Cookie

// Text resizer
// <a href="javascript:ts('body',1)">+ Larger Font</a> 
// | <a href="javascript:ts('body',-1)">+ Smaller Font</a>

var tgs = new Array( 'div','td','tr');

//Specify spectrum of different font sizes:
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;

function ts( trgt,inc ) {
	if (!document.getElementById) return
	var d = document,cEl = null,sz = startSz,i,j,cTags;
	
	sz += inc;
	if ( sz < 0 ) sz = 0;
	if ( sz > 6 ) sz = 6;
	startSz = sz;
		
	if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

	cEl.style.fontSize = szs[ sz ];

	for ( i = 0 ; i < tgs.length ; i++ ) {
		cTags = cEl.getElementsByTagName( tgs[ i ] );
		for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
	}
}

$("#text_decrease").click(function(){
	ts("column_container",-1);
});
$("#text_increase").click(function(){
	ts("column_container",1);
});
$(".text_decrease").click(function(){
 ts("inside_body_wrapper",-1);
});
$(".text_increase").click(function(){
 ts("inside_body_wrapper",1);
});

// End Text resizer

// Email Page
function emailPage() {
  var messageText = 'I thought you\'d be interested in this article on the NPS website. ' + window.location;
  var subjectText = 'NPS website: '+ document.title;
  var email = prompt("Enter your friend's email address:", '');
    window.location = 'mailto:' + email +
      '?subject=' + encodeURIComponent(subjectText) +
      '&body=' + encodeURIComponent(messageText);
  }
  
$("#email_page,.email_page").click(function(){
	emailPage();
});
// End Email Page

// Remove blank end dates
$(".end_date").each(function(){
    if ($(this).text()==" to ") {
        $(this).remove();
    }
});
// End remove blank end dates

if ($("#mnf_keyword").length > 0 ) {
    checkInput($("#mnf_keyword").get(0));
}

});