/**************************************************
 * NAR sign-in functions.
 */
function narSignIn() {
	var pin1 = document.getElementById("pin1").value;
	var pin2 = document.getElementById("pin2").value;
	var pin3 = document.getElementById("pin3").value;
	var aeroplanNumber = pin1 + pin2 + pin3;
	var pwd = document.getElementById("pin").value;
	var rememberChecked = document.getElementById("remember").checked;
	processRememberNumber(pin1, pin2, pin3, rememberChecked);
	 if(document.location.protocol.toLowerCase() =='http:')  
               document.location.href = document.location.href.replace('http:','https:');
 	document.forms.login.submit();	
}

/*
   Redirects the user to the relevant URL when Password Help is clicked on 
   signin
*/
function passwordRecovery(what) {
          var cust1 = document.getElementById("pin1").value;
          var cust2 = document.getElementById("pin2").value;
          var cust3 = document.getElementById("pin3").value;
        
          if(what == 'help') {
          if ((cust1.length == 3) && (cust2.length == 3) && (cust3.length == 3)) {
              document.forms.login.action = "/password_request/process.do";
          }
          else {
              document.forms.login.action = "/password_request.do";
              }
          }
          else {
              document.forms.login.action = "/enroll.do?ghostcard=true&register=true";
          }
              
          document.forms.login.submit();        
}

/*
   Returns a printable error message for signIn given an error object.

   var error - error object with error messages
*/
function getSignInErrorMessage(error) {
	var errorMessage = "";
	if (error.validationErrors['j_username']) {
		errorMessage = error.validationErrors['j_username'];
	}
	else if (error.validationErrors['j_password']) {
		errorMessage = error.validationErrors['j_password'];
	}
	return errorMessage;
}

/*
   Process remember number.  Set all three parts of pin number into cookie if
   remember number checkbox is checked, otherwise clear all three parts of pin
   number from cookie.

   var pin1 - first part of pin number
   var pin2 - second part of pin number
   var pin3 - third part of pin number
   var rememberChecked - remember number checkbox is checked
*/
function processRememberNumber(pin1, pin2, pin3, rememberChecked) {
	if (rememberChecked) {
		var today = new Date();
		// expire date is set at 10 years to conform to aeroplan.com's cookie expiry date
		var expireDate = new Date(today.getTime() + 3600000 * 24 * 3650);
		setCookie("aeroplan_memberno_1", pin1, expireDate, "/");
		setCookie("aeroplan_memberno_2", pin2, expireDate, "/");
		setCookie("aeroplan_memberno_3", pin3, expireDate, "/");
	}
	else {
		cleanCookie("aeroplan_memberno_1", "/");
		cleanCookie("aeroplan_memberno_2", "/");
		cleanCookie("aeroplan_memberno_3", "/");
	}
}

/*
   Fill pin number textboxes and check associated remember number checkbox if
   all three parts of pin number exist in cookie, otherwise do nothing.
*/
function initRememberNumber() {
	var cookie_pin1 = readCookie("aeroplan_memberno_1");
	var cookie_pin2 = readCookie("aeroplan_memberno_2");
	var cookie_pin3 = readCookie("aeroplan_memberno_3");
	if ((cookie_pin1 != null) && (cookie_pin2 != null) && (cookie_pin3 != null)) {
		document.getElementById("pin1").value = cookie_pin1;
		document.getElementById("pin2").value = cookie_pin2;
		document.getElementById("pin3").value = cookie_pin3;
		document.getElementById("remember").checked = true;
	}
}



// Cookie management javascript functions, received from Aeroplan (April 23, 2008)

/* --------------------------------------------------------------- */
/* Tools for cookie management */
/* --------------------------------------------------------------- */

/* 20080521 - added variable path to cleanCookie function */
function cleanCookie(name, path) {
	date = new Date();
	date.setFullYear(date.getFullYear()-1);
	setCookie(name,null,date,path);
}

/* --------------------------------------------------------------- */

function setCookie(name, value) {
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	
	document.cookie = name + "=" + escape(value) +
	((expires == null) ? "" : ("; expires="+expires.toGMTString())) +
	((path == null) ? "" : ("; path="+path)) +
	((domain == null) ? "" : ("; domain="+domain)) +
	((secure == true) ? "; secure" : "");
}

/* --------------------------------------------------------------- */

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

/* --------------------------------------------------------------- */

function readCookie(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;
}

// Copied from "http://www.aeroplan.com/static/js/autotab.js" to handle auto-tabbing when entering aeroplan number
// Detection of Browser should be replaced by Detection of
// Objects as per Coding Standards

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {

    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

    if (input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        var index = getIndex(input) + 1;
        index = index % input.form.length;
        
        while ((index < input.form.length) && (input.form[index].type == "hidden")) {
            index = index + 1;
        }

        
        if (index >= 0 && input.form[index] != null && input.form[index].type != "hidden" && !input.form[index].disabled)
        {
          input.form[index].focus();
        }
    }

    function containsElement(arr, ele) {
        var found = false, index = 0;
        while(!found && index < arr.length)
            if(arr[index] == ele)
                found = true;
            else
                index++;
        return found;
    }

    function getIndex(input) {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
            if (input.form[i] == input)index = i;
            else i++;
        return index;
    }

    return true;
}
    
/* --------------------------------------------------------------- */
/* Creates an IFrame shim under <DIV> elements that need to be
/* displayed above any HTML <SELECT> menus.  With IE6 all windowed 
/* controls (like SELECT) taking Z-index precedence over any 
/* non-windowed element.
/* --------------------------------------------------------------- */
function IsIE() {
	return ( navigator.appName=="Microsoft Internet Explorer" );
}

function HidePopupDiv(divID, shimID) {
    var divPopup;
    divPopup=document.getElementById(divID);
	divPopup.style.display = "none";
		
	var iframe;
	iframe = document.getElementById(shimID);

    if (IsIE()) {
        iframe.style.display="none";
    }
}
	
function ShowPopupDiv(divID, shimID) {
    var divPopup=document.getElementById(divID);
	var iFrame=document.getElementById(shimID);

    if (!IsIE()) {
    	//Just display the div
		divPopup.style.display = "block";
        return;
    }

    // increase default zIndex of div so that DIV appears before IFrame
    divPopup.style.zIndex=100;
	divPopup.style.display = "block";
		
//    iFrame.setAttribute("src", "javascript:'<html></html>';");

    // match IFrame position with divPopup
    iFrame.style.position="absolute";
    iFrame.style.left = divPopup.offsetLeft + 'px';
    iFrame.style.top = divPopup.offsetTop + 'px';
    iFrame.style.width = divPopup.offsetWidth + 'px';
    iFrame.style.height = divPopup.offsetHeight + 'px';
	iFrame.style.zIndex = 1;
	iFrame.style.display = "block";
}
