var isSubappBusy;
var warnType = "";
var lockType = "";
var displayType = "";
var displayPhrase = "";
var displayPhrase2= "";
var LOCK = "lock";
var WARN = "warn";
var logOffWhenCancelled = "false";
var suppressWarn = "false";
var suppressLock = "false";
var isE2e = false;

function isSubappBusy()
{
	return ((displayPhrase != null) && (displayPhrase != ""));
}

function confirmGo(sprWarn, sprLock)
{
  suppressWarn = sprWarn;
  if(typeof suppressWarn == 'undefined')
    suppressWarn = "false";

suppressLock = sprLock;
  if(typeof suppressLock == 'undefined')
    suppressLock = "false";

  return ConfirmGo()
}

function ConfirmGo()
{
	var subappBusy = isSubappBusy();

	if (subappBusy)
	{
		if (lockType == LOCK && suppressLock == "false")
		{
			alert (displayPhrase);
			return false;
		}
		else if (warnType == WARN && suppressWarn == "false" )
		{
			if (!confirm(displayPhrase))
			{
				if(logOffWhenCancelled == "true")
				{
					logOff();
				}
				return false;
			}
			else
			{
				if (displayPhrase2 != "")
				{
					if (!confirm(displayPhrase2))
					{
						if(logOffWhenCancelled == "true")
						{
							logOff();
						}
					    	return false;
					}
				}
			}
		}
		else
		{
		    if(suppressWarn == "false" && suppressLock == "false")
			alert("Dialog Error: ConfirmGo 'Type' not alert or confirm!");
		}
	}
	//setSubappBusy("","","");
	return true;
}

function setSubappBusy(dType, phrase1, phrase2)
{
	if (dType == "")
	{
		lockType = dType;
		warnType = dType;
		displayPhrase = "";
		displayPhrase2 = "";
	}
	else
	{
		if(dType == WARN)
			warnType = dType;
		else if(dType == LOCK)
			lockType = dType;

		displayPhrase = phrase1;
		displayPhrase2 = phrase2;
		logOffWhenCancelled = "false";
	}
}

function setSubappBusy2(dType, phrase1, phrase2, logOffIfCancelled)
{
	if (dType == "")
	{
		lockType = dType;
		warnType = dType;
		displayPhrase = "";
		displayPhrase2 = "";
		logOffWhenCancelled = "false";
	}
	else
	{
		if(dType == WARN)
			warnType = dType;
		else if(dType == LOCK)
			lockType = dType;

		displayPhrase = phrase1;
		displayPhrase2 = phrase2;
		logOffWhenCancelled = logOffIfCancelled;
	}
}

function submitLinkPostForm(formName)
{
	var myForm = document.forms[formName];
	myForm.submit();
}

function submitLinkPostForm2(formName, actionURL)
{
	var myForm = document.forms[formName];
	myForm.action=actionURL;
	myForm.submit();
}

function encryptE2e(form)
{
    if (!document.e2e)
        return;
        
    e2eapplet = document.e2e;
    if (form.pin) {
        enc = e2eapplet.encryptPIN(form.pin.value,eid);
        if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 5)
            form.pin.maxLength = enc.length();
        form.pin.value = enc;
    }
        
    if (form.pwd) {
        enc = e2eapplet.encryptPassword(form.pwd.value,eid);
        if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 5)
            form.pwd.maxLength = enc.length();
        form.pwd.value = enc;
    }
}

function validateToken(form) {
	return true;
}

function validateCredential(form) {
    
    var isValid = true;
    var focusField = null;
    var i = 0;
    var NO_SELECTION = '-1';
    var fields = new Array();
    oRequired = new credentials();
    for (x in oRequired) {
        var field = form[oRequired[x][0]];
        if(typeof field != 'undefined') {
            if(field.type != null) {
                if(field != null && field.disabled == false) {
                    if (field.type == 'text' ||
                        field.type == 'password') {
                  

                        if (field.type == 'text' || field.type == 'password') {
                            if (field.value == '' || field.value == null) {
                                focusField = field;
                                fields[i++] = oRequired[x][1];
                                isValid = false;
                                break;
                            }
                        }
                    }
                }
            }
	}
    
    }   


    if (fields.length > 0 && focusField != null) {
       alert(fields.join('\n'));
       focusField.focus();
    }

    if (isValid && document.getElementById("reentry"))
        disablePinPad();
        
    if (isValid && (document.getElementById("vkb") || document.getElementById("largevkb")))
        hideVkb('done');

    if (isValid && isE2e)
        encryptE2e(form);

    return isValid;
}


function validateRequired(form) {
    var isValid = true;
    var focusField = null;
    var i = 0;
    var NO_SELECTION = '-1';
    var fields = new Array();
    oRequired = new required();
    for (x in oRequired) {
        var field = form[oRequired[x][0]];
        if(typeof field != 'undefined') {
            if(field.type != null) {
                if(field != null &&  field.disabled == false && (typeof field.readOnly == 'undefined' || field.readOnly == false) ) {
                    if (field.type == 'text' ||
                        field.type == 'textarea' ||
                        field.type == 'file' ||
                        field.type == 'select-one' ||
                        field.type == 'radio' ||
                        field.type == 'password') {

                        var value = NO_SELECTION;
                        // get field's value
                        if (field.type == "select-one") {
                            var si = field.selectedIndex;
                            if (si >= 0) {
                                value = field.options[si].value;
                            }
                        }
                        else {
                            value = field.value;
                        }
			if (field.type == "select-one" && value == NO_SELECTION) {
				if (i == 0) {
                                   focusField = field;
                               }
                               fields[i++] = oRequired[x][1];
                               isValid = false;
                               break;
                        }



                        if (field.type == 'text' 
							|| field.type == 'password'
							|| field.type == 'textarea') {
                            if (field.value == '' || field.value == null) {
                                focusField = field;
                                fields[i++] = oRequired[x][1];
                                isValid = false;
                                break;
                            }
                        }
                    }
                }
            }
            else {
                var checked = false;
                var skipped = true;
                var focusElem;
                for(j=0;j<field.length;j++) {
                    temp = field[j];
                    if (temp == null
                    || temp.disabled == true
                    || typeof  temp.readOnly == 'undefined'
                    || temp.readOnly == true) {
                        continue;
                    }
                    else {
                        skipped = false;
                        if (temp.type == 'radio') {
                                focusElem=temp;
                                if(temp.checked) {
                                checked=true;
                            }
                        }

                        if(temp.type == 'checkbox') {
                            focusElem=temp;
                            if(temp.checked) {
                                checked=true;
                            }
                        }
                        if(temp.type == 'text') {
                            focusElem=temp;
                            if(temp.value !='') {
                                checked=true;
                            }
                        }
                    }
                }

                if(checked==false && !skipped) {
                    focusField=focusElem;
                    fields[i++] = oRequired[x][1];
                    isValid = false;
                    break;
                }
            }
        }
    }

    if (fields.length > 0 && focusField != null) {
       focusField.focus();
       alert(fields.join('\n'));
    }

    return isValid;
}

function getCookie(name)
{
var c=""+document.cookie;
var index = c.indexOf(name + "=");
if (index == -1) return null;
var endstr = c.indexOf(";", index);
if (endstr == -1) endstr = c.length;
return unescape(c.substring(index+name.length+1, endstr));
}

function setCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + value + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

function isEmpty(s){ return ((s == null) || (s.length == 0)) }

function isWhitespace (s)
{
   var i;
   var whitespace = " \t\n\r";
   if (isEmpty(s)) return true; // Is s empty?

   for (i = 0; i < s.length; i++)
   {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
   }

   return true;
}

function displayHelp(helpURL, windowName, openWinOptions)
{
	var anchor = "";
	if(!isWhitespace(applicationID))
	{
		if(helpURL.indexOf("#") != -1)
		{
			anchor = helpURL.substring(helpURL.indexOf("#") + 1);
		}
		if(helpURL.indexOf("?") != -1)
			helpURL += "&";
		else
			helpURL += "?";

		helpURL += "citigroup.jfp.applicationID=" + applicationID;
		if(!isWhitespace(screenID))
				helpURL += "&citigroup.jfp.screenID=" + screenID;

		if(!isWhitespace(anchor))
			helpURL += "#" + anchor;
	}
	showPopup_W_XY(helpURL, windowName,openWinOptions, 160, 80);
}

function OnClickHandler() {
	var el=null;
	var flag=true;

	el=event.srcElement;
	while (flag && el) {
		if (el.tagName=="A") {
			flag=false;
			if (el.protocol=="javascript:" || el.href.indexOf("javascript:")!=-1) {
				execScript(unescape(el.href),"javascript");
				window.event.returnValue=false;
			}
		}
		else
			el=el.parentElement;
	}
}
if ((navigator.appName.indexOf("Microsoft")!=-1) && (navigator.appVersion.substring(0,1)>"3"))
	if (navigator.userAgent.indexOf('Mac') == -1)
		document.onclick=OnClickHandler;

function NS6OnClickHandler(event) {
    var srcElement=event.target;
    if (srcElement.nodeType==1 || srcElement.nodeType==3)
        srcElement=srcElement.parentNode;
    if (srcElement.tagName=="A") {
        if (lockType==LOCK) {
            alert(displayPhrase);
            return false;
        }
    }
    return true;
}
if (navigator.appName.indexOf("Netscape")!=-1  && document.getElementById  && navigator.userAgent.indexOf("Safari") == -1)
    document.onclick = NS6OnClickHandler;

if(window.location.protocol.indexOf("https") != -1)
	setCookie("JSESSIONID", getCookie("JSESSIONID"), null, "/", null, true);
	
if (document.captureEvents) 
	{//non IE
    	if (Event.MOUSEDOWN)
		{//NS 4, NS 6+, Mozilla 0.9+
		document.captureEvents(Event.MOUSEDOWN);
		document.onmousedown = winMouseDown;
		}
	}

var _evt = null;
function winMouseDown(e)
{
_evt = e;
}

function winSize() 
{
var wW = -1;
var wH = -1;
if (typeof(window.innerWidth) == 'number') 
	{//Non-IE
	wW = window.innerWidth;
	wH = window.innerHeight;
	} 
else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) 
	{//IE 6+ in 'standards compliant mode'
	wW = document.documentElement.clientWidth;
	wH = document.documentElement.clientHeight;
	} 
else if (document.body && (document.body.clientWidth || document.body.clientHeight)) 
	{//IE 4 compatible
	wW = document.body.clientWidth;
	wH = document.body.clientHeight;
	}
var win = new Object();
win.x = wW;
win.y = wH;
return win;
}

function popupWinSize(winProp)
{
var wW = -1;
var wH = -1;
var props = winProp.split(',');
for (var i = 0; i < props.length; i++)
	{
	var nv = props[i].split('=');
	if (nv.length == 2)
		{
		var n = nv[0].replace(/^\s+/, '').replace(/\s+$/, '');
		var v = nv[1].replace(/^\s+/, '').replace(/\s+$/, '');
		if (n.toLowerCase() == "width" && !isNaN(parseInt(v,10))) wW = parseInt(v,10);	
		else if (n.toLowerCase() == "height" && !isNaN(parseInt(v,10))) wH = parseInt(v,10);
		}
	}
var win = new Object();
win.x = wW;
win.y = wH;
return win;
}

function getClickPos()
{
var mouseX = 0;
var mouseY = 0;
if (!_evt) _evt=window.event||arguments.callee.caller.arguments[0];
if (_evt)
	{
	if (typeof(_evt.pageX) == 'number' ) 
		{
		mouseX = _evt.pageX;
		mouseY = _evt.pageY;
		}
	else if(typeof(_evt.clientX) == 'number')
		{
		mouseX = _evt.clientX;
		mouseY = _evt.clientY;
		}
	}
var mouse = new Object();
mouse.x = mouseX;
mouse.y = mouseY;
return mouse;	
}

function showPopup_W_XY(url, wn, winProp, X, Y)
{
if (navigator.userAgent.indexOf("Safari") != -1)
  {
  window.open (url, wn, winProp + ',screenX='+X+',left='+X+',screenY='+Y+',top='+Y);
  return;
  }
var mouse = new Object();
mouse.x = X;
mouse.y = Y;
mouse.offsetX = 0;
mouse.offsetY = 0;
doPopup(url, wn, winProp, mouse, 'W');
}

function showPopup_L_XY(url, wn, winProp, X, Y)
{
if (navigator.userAgent.indexOf("Safari") != -1)
  {
  window.open (url, wn, winProp + ',screenX='+X+',left='+X+',screenY='+Y+',top='+Y);
  return;
  }
var mouse = getClickPos();
mouse.offsetX = X;
mouse.offsetY = Y;
doPopup(url, wn, winProp, mouse, 'L');
}

function showPopup(url, wn, winProp)
{
if (navigator.userAgent.indexOf("Safari") != -1)
  {
  window.open (url, wn, winProp);
  return;
  }
var mouse = getClickPos();
mouse.offsetX = 25;
mouse.offsetY = 0;
doPopup(url, wn, winProp, mouse, 'L');	
}

function doPopup(url, wn, winProp, mouse, type)
{  
var x = 0;
var y = 0;
var offsetX = 0;
var offsetY = 0;
url+="";
wn+="";
winProp+="";
if (winProp == null) winProp = "";
if (document.getElementById) 
	{
	if (isNaN(window.screenX))
		{// IE6
		x=mouse.x+window.screenLeft
		y=mouse.y+window.screenTop
		}
	else 
		{// Mozilla Firefox 0.9
		if (type=='L')
			{
			offsetX = window.pageXOffset;
			offsetY = window.pageYOffset;
			}
		x=mouse.x+window.screenX+(window.outerWidth-window.innerWidth)-offsetX;
		y=mouse.y+window.screenY+(window.outerHeight-24-window.innerHeight)-offsetY;
		}
	}
else if (document.all) 
	{
	x=mouse.x+window.screenLeft
	y=mouse.y+window.screenTop	
	}
else if (document.layers) 
	{// NS 4.7x
	if (type=='L')
		{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
		}	
	x=mouse.x+window.screenX+(window.outerWidth-window.innerWidth)-offsetX;
	y=mouse.y+window.screenY+(window.outerHeight-24-window.innerHeight)-offsetY;
	}

x += mouse.offsetX;
y += mouse.offsetY;
if (x<0) x=0;
if (y<0) y=0;
var popupWin = popupWinSize(winProp);
if (popupWin.x < 0 || popupWin.y < 0)
	{
	var win = winSize();
	if (popupWin.x < 0) popupWin.x = win.x;
	if (popupWin.y < 0) popupWin.y = win.y;	
	}
if (screen && screen.availHeight)
	{
	if ((y + popupWin.y) > screen.availHeight)
		y = screen.availHeight - popupWin.y;
	}
if (screen && screen.availWidth)
	{
	if ((x + popupWin.x) > screen.availWidth)
		x = screen.availWidth - popupWin.x;
	}

window.open (url, wn, winProp + ',screenX='+x+',left='+x+',screenY='+y+',top='+y);
}

function linkParentAndCloseSelf(url){
 self.opener.location = url;
  window.close();
}

function trim(str){
    var reTrimRight = / +$/;
    var reTrimLeft = /^ +/;
    str = str.replace( reTrimRight, "" );
    str = str.replace( reTrimLeft, "" );
	return str;
}
