/* 
DIYBanking.js
Cam Morton 1-JUL-2003
Determines the pages to display, manages cookies and alerts for DIY Banking
*/
// Crucially, the DIY-switch.js must be included before this, as boolean flag "showAlert" needs to be defined as either true or false.
var alertPage =  "DIYAlert.html";

var showFeature = false; // don't display the New Feature page
//var showFeature = true; // uncomment to display the New Feature page if customer hasn't seen it before
var newFeaturePage = "DIYNewFeature.html";
var featureDate = '24/11/2006'; // the date a new feature was added / security alert

var showNotice = false; // don't display notice page
//var showNotice = true;
var noticePage = "DIYNotice.html";

// setter methods - required because this value is derived from the SSI on the DIYLogin page
function setDateNow(d) {
	dateNow = d;
}
function setTimeNow(t) {
	timeNow = t;
}

function compareDate(d1, d2) {
       var dt1 = new Date(d1);
       var dt2 = new Date(d2);
	  return dateDiff(dt1,dt2);
} 
	    
function dateDiff(d1,d2) 
{
	diffMilli = d2.getTime()-d1.getTime();
	return Math.round(diffMilli/(1000*60*60*24));
}

// set the window opener properties and call the login handler page
function goLogin()
{
   var loginWin;   
   var HOST_NAME = "www.membersequitybank.com.au";
   //msgbox('here!');
   // if it is an external request - direct user to www.membersequitybank.com.au      
   if (window.top.location.hostname) {
      temp_HOST_NAME = window.top.location.hostname.toUpperCase();
      // if it is not an external call - assign to the machine name
      if (temp_HOST_NAME.indexOf("MEMBERSEQUITY") ==  -1) {
         HOST_NAME=temp_HOST_NAME;
         //HOST_NAME="mewebtest5";
      }                       
   }

   var bklWidth = 940; //desired size for Banklink window
   var bklHeight = 580;
   var sLeft = 0;
   var sTop = 0;
   var sScreenX = 0;
   var sScreenY = 0;
   var bestLeft = 0;

   if ((screen.Width >=0) && (screen.Height >=0)) {
      var sWidth = screen.Width - 10;
      var sHeight = screen.Height - 55;
   }
   else if ((screen.availWidth >= 0) && (screen.availHeight >= 0)) {
      var sWidth = screen.availWidth - 10;
      var sHeight = screen.availHeight - 25;
   }
   if (bklWidth < sWidth) {
      bestLeft = (Math.floor(sWidth / 2)) - (Math.floor(bklWidth / 2)) - 8;
      if (bestLeft > 0) { //sanity check
         sLeft = bestLeft
         sScreenX = bestLeft;
      }
      sWidth = bklWidth;
   }
   if (bklHeight < (sHeight - 10)) {
      sTop = 10;
      sScreenY = 10;
      sHeight = bklHeight;
   }
   sOptions = "toolbar=no,status=yes,location=no,menubar=no,directories=no,scrollbars=yes,resizable=yes,";
   sOptions = sOptions + "screenX=" + sScreenX + ",screenY=" + sScreenY + ",";
   sOptions = sOptions + "left=" + sLeft + ",top=" + sTop + ",";
   sOptions = sOptions + "height=" + sHeight + ",width=" + sWidth;        
   sLoc = "http://" + HOST_NAME + "/DIYLogin.html";
   sTitle = "ME_Bank_Internet_Banking"; 
   //msgbox(sLoc+sTitle+sOptions);
   loginWin = window.open(sLoc,sTitle,sOptions);
}

function DIYLoginCheck() {
	// if there is a pending outage or alert - show the alert page
	if (showAlert) {
		goToPage(alertPage);
	}else {
		if (showFeature) {
		DIYFeatureCheck();
		} else {
			if (showNotice) {
				goToPage(noticePage);
			} else {
				DIYLogin();
			}
		}
	}
}

// once the alert page has been shown - check for a new feature
function DIYAlertReturn() {
	DIYFeatureCheck();
}

// check for cookies relating to new features
function DIYFeatureCheck() {

	if (showFeature) {
		// call cookieCutter utility
		var cookie = GetCookie('lastFeatureDate');	
	
		// if a cookie - check the featureDate
		if (null != cookie) {
			// get the feature date out of the cookie
			var cookieLastFeatureDate = GetCookie("lastFeatureDate");		
			
			// if the date stored in the cookie is less than the newest feature - call new feature screen		
			if (null == cookieLastFeatureDate || (compareDate(cookieLastFeatureDate,featureDate)>0 )) {
				// call new Feature
				DIYNewFeature();
				
			} else { // if the cookie's date is the same			
				// log in
				DIYLogin();
			}
		} else { // no cookie has been set				 
			// call new feature page - which in turn this calls DIY Login		
			DIYNewFeature();
		}
	} else {
		DIYLogin();
	}	
}

// once the outage and feature have been checked - access web banking via the URL Forward
function DIYLogin() {
	goToPage('/webBanking');
}

// calls the new feature page
function DIYNewFeature() {
	// create the cookie values
	expire = setExpire();	
	SetCookie ("lastFeatureDate", featureDate , expire);	
	
	goToPage(newFeaturePage);
}

// once returned from the new feature page - check if there is a customer notice
function DIYNewFeatureReturn() {

	// now log in
	DIYLogin();
}


// Generic page handler
function goToPage(page) {
	document.location.href=page;
}

// Cookie Utils
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
	i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
return null;
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function setExpire() {	
	var expire = new Date();
	var theFuture = expire.getTime() + (365*24*60*60*1000);
	expire.setTime(theFuture);
	return expire;
}
