
$(function() {
	//for all outside links, set class to "ext" (in case we forget)
	$("a[href^=http]").not(".noext").addClass("ext");
	
	//open "ext" class links in new window ("target" is not valid XHTML Strict), and add a title if none is already specified
	$(".ext").attr("target", "_blank").not("[title]").attr("title", "External link");

	//open PDFs in a new window
	$("a[href$=pdf]").attr("target", "_blank");

	//class "first" removes the dot from the left of the first list element
	$("li:first-child").addClass("first");

	//finds all links to the current page (and all parent LI elements of them) and sets them to be active
	$("a[href=" + window.location.pathname + "]").parents("li").addClass("active");
});

function loginIress() {
	if (!$("#login-username").val() || !$("#login-password").val()) {
		alert("You must enter a user name and a password to log in.");
		return false;
	}

        var mode = 'auto';
        if ($("#loginmode").attr('checked') == true) {
            $("#product").val('html');
            mode = 'html';
        } else {
            $("#product").val('auto');
        }
	if ($("#showPopUp").val() != "no")
	{
		if ($("#loginFrom").val() == "millennium")
		{
			if (!acceptMillenniumLoginFee(mode)) return false;
		}
		else
		{
			if (!acceptLoginFee(mode)) return false;
		}
    }
    if ($("#loginFrom").val() == "millennium") {
        $("#u").val($("#login-username").val() + "@millennium");
    }
    else {
        $("#u").val($("#login-username").val() + "@minc");
    }
	
	window.open("", "webiress", "directories=no,location=no,menubar=no,scrollbars=yes,toolbar=no,resizable=yes,status=no");
	return true;
}

function loginIressDemo(product) {
	window.open("http://web.iress.com.au/secure/login.asp?u=demo@minc&p=demo56&product=" + product, "webiress", "directories=no,location=no,menubar=no,scrollbars=yes,toolbar=no,resizable=yes,status=no");
	return false;
}


function acceptLoginFee(mode) {
	var cookiename = "lastlogin_" + $("#login-username").val();	//eg "lastlogin_12345"
	if (! $.cookie(cookiename)) {	//cookie does not exist (or has expired), show warning about platform fee
		var fee_msg = "Please Note:\n";
                if (mode == 'html') {
                    fee_msg += "Usage of the htmlIRESS trading platform attracts a Software Fee of " + IressHTMLSoftwareFee + " per calendar month (incl GST).\n\n";
                }
                else
                {
                    fee_msg += "Usage of the webIRESS trading platform attracts a Software Fee of " + IressSoftwareFee + " (incl GST).\n\n";
                }
		fee_msg += "The first time you log into the platform this month, you will be charged the Software Fee. ";
		fee_msg += "For more details, see the Fees page or call us on " + ContactNumber + ".\n\n";
		fee_msg += "Click OK to accept this Fee and log in.";		
		if (! confirm(fee_msg)) return false;	//fee not accepted, exit
	}
	//create/update cookie
	var now = new Date();
	var expiry = new Date(now.getFullYear(), now.getMonth() + 1, 1);	//start of next month (will roll-over end of year)
	$.cookie(cookiename, now.getTime(), { expires: expiry });
	return true;
}

function acceptMillenniumLoginFee(mode) {
	var cookiename = "lastlogin_" + $("#login-username").val();	//eg "lastlogin_12345"
	if (! $.cookie(cookiename)) {	//cookie does not exist (or has expired), show warning about platform fee
		var fee_msg = "Please Note:\n";
        fee_msg += "Usage of the MillenniumOnline trade account platform attracts a Software Fee of $5.50 per calendar month (incl GST).\n\n";

		fee_msg += "The first time you log into the platform this month, you will be charged the Software Fee to your MillenniumPortfolios account.";
		fee_msg += " Please see the details of the Software Fee within the terms and conditions of use document available on this website.\n\n";
		fee_msg += "Click OK to accept this Fee and log in.";		
		if (! confirm(fee_msg)) return false;	//fee not accepted, exit
	}
	//create/update cookie
	var now = new Date();
	var expiry = new Date(now.getFullYear(), now.getMonth() + 1, 1);	//start of next month (will roll-over end of year)
	$.cookie(cookiename, now.getTime(), { expires: expiry });
	return true;
}