/*
	DIYBanking.js for IBv3
	John Bishop, June 2011
*/

function goLogin(mobile) {

	// URL for login page
	sLoc = "/DIYLogin.html";

	if (mobile) {
		// Mobile client; include the m=1 URL parameter for further processing
		// 7-7-11 - JB - Modified to open in same window, not pop in new window
		sLoc=sLoc + '?m=1';
		window.location = sLoc;
	} else {
		// Full client
		loadFullScreen(sLoc);
	}
}

function DIYLoginCheck() {

	// If outage=true passed through via query string, force alertEnabled to true
	if (gup("outage") == "true") {
		alertEnabled = true;
	}

	// Check for presence of m=1 in URL string, and send to full or mobile function as appropriate
	if (gup('m') == "") {
		DIYLoginCheckIB();
	} else {
		DIYLoginCheckMobile();
	}
}

function DIYLoginCheckIB() {
	/*	Page that contains Alert page content.
		Whether this content is shown or not is based on
		the var 'alertEnabled' which is defined in DIY-switch.js.
	*/
	var alertPage =  "DIYAlert.html";

	/*	If notice is to be shown, set noticeEnabled = true.
		If set to true, contents of noticePage will be shown before user can continue to IB.
		Otherwise, set noticeEnabled = false; no notice will be shown.
	*/
	var noticeEnabled = false;
	var noticePage = "DIYNotice.html";
	
// ------------------------------------------------------------------------------	
	
	// If alertEnabled and not planned outage, show generic outage message and do not let user continue	
	if ((alertEnabled) && (!planned)) {
		document.getElementById('dynamicIB').style.display = 'none';
		document.getElementById('unplanned').style.display = 'block';
		document.getElementById('closeWin').style.display = 'block';
		return;
	}
	
	// Check alert states
	if (alertEnabled) {
		new Ajax(alertPage, {method: 'get',update: $('dynamicIB')}).request();
		if (continueToIB) {
			document.getElementById('continue').style.display = 'block';
		} else {
			document.getElementById('closeWin').style.display = 'block';
		}
		return;
	}
	
	// No outage; show notice?
	if (noticeEnabled) {
		new Ajax(noticePage, {method: 'get',update: $('dynamicIB')}).request();
		return;
	}
	
	//None of that, or anything else - onwards to IB!
	DIYAlertReturn();
}

function DIYLoginCheckMobile() {
	/*	Page that contains Alert page content.
		Whether this content is shown or not is based on
		the variables which are defined in IBSwitch.js.
	*/
	var alertPageMobile =  "DIYAlert.html";

	/*	If notice is to be shown, set noticeEnabled = true.
		If set to true, contents of noticePage will be shown before user can continue to IB.
		Otherwise, set noticeEnabled = false; no notice will be shown.
	*/
	var noticeEnabledMobile = false;
	var noticePageMobile = "DIYNotice.html";
	
// ------------------------------------------------------------------------------	

	// If alertEnabled and not planned outage, show generic outage message and do not let user continue

	if ((alertEnabledMobile) && (!plannedMobile)) {
		document.getElementById('dynamicIB').style.display = 'none';
		document.getElementById('unplanned').style.display = 'block';
		document.getElementById('closeWin').style.display = 'block';
		return;
	}
	
	// Check alert states
	if (alertEnabledMobile) {
		new Ajax(alertPageMobile, {method: 'get',update: $('dynamicIB')}).request();
		if (continueToIBMobile) {
			document.getElementById('continue').style.display = 'block';
		} else {
			document.getElementById('closeWin').style.display = 'block';
		}
		return;
	}
	
	// No outage; show notice?
	if (noticeEnabledMobile) {
		new Ajax(noticePageMobile, {method: 'get',update: $('dynamicIB')}).request();
		return;
	}
	
	//None of that, or anything else - onwards to IB!
	DIYAlertReturn();
}

function DIYAlertReturn() {
	if (gup('m') == "") {
		// Full IB site
		window.location='https://ib.mebank.com.au/ME';
	} else {
		// Mobile IB site
		window.location='https://m.mebank.com.au/Mobile';
	}
}

function loadFullScreen(page)
{
   var resize='';
   var titlebar='';
   var wvalue=screen.width - 7;
   var hvalue=screen.height - 75;
   var ibWin = window.open(page,'', 'top=0,left=0,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=' + wvalue + ',height=' + hvalue + resize + titlebar + ',hotkeys=0');  
}

// Get Url Parameter
function gup(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if(results == null) {
		return "";
	} else {
		return results[1];
	}
}
