//<!-- TOPIC Javascript Libraries -->
// Include this file after fsa.js and amessages.js

var clHandleHelp;
var clHandleCust;
var szHelpWinFeatures = "status=yes,scrollbars=yes,resizable=yes,width=400,height=375";

var clHandleExternal;
var szExternalWinFeatures = "width=800,height=600,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes,directories=yes";

function syncToLoc() {
	var i, j;
	var szVal  = document.URL
	var szVal2 = ""
	var szVal3 = ""

	if (szVal.indexOf("#") >= 0) {
		szVal = szVal.substring(0, szVal.indexOf("#"))
	}
	szVal = szVal + "?cs=true&"
	with (document.FAFSA) {
		for (i=0; i<length; i++) {
			if (elements[i].type == "hidden") {
				if (elements[i].name == "dowhat") {
					szVal = szVal + elements[i].name + "=display" + "&"
				} else {
					szVal2 = elements[i].value + ""
					szVal3 = ""
					for (j=0; j<szVal2.length; j++) {
						if (szVal2.charAt(j) == " ") {
							szVal3 = szVal3 + "+"
						} else {
							szVal3 = szVal3 + szVal2.charAt(j)
						}
					}
					szVal = szVal + elements[i].name + "=" + szVal3 + "&"
				}
			}
		}
	}
	return szVal
}

function SelectOption(clControl, szValue) {
    var bSet = false
    for ( i=0; i < clControl.length; i++ ) {
        if (clControl.options[i].value == szValue) {
            clControl.options[i].selected=1
            bSet = true
        }
    }
    if (!bSet) {
        clControl.options[0].selected=1
    }
}

function openWindow(clHandle, szFile, szName, szWinFeatures) {
	// Netscape Navigator allows to open only 100 windows at the maximum
	// recycle the existing one to avoid exceeding the limit (R.L.)
	if (clHandle) {
		if (!clHandle.closed) {   // if help window not closed
			clHandle.location.href = szFile
		} else {
			clHandle = window.open(szFile, szName, szWinFeatures)
		}
	} else {
		clHandle = window.open(szFile, szName, szWinFeatures)
	}

	// check if new window failed to open
	if (clHandle) {
		clHandle.focus();
	} else {
		alert(szHelpBrowserMsg) // this is an extremely rare case, if it ever happens.
	}
	return clHandle;		
}

function Help(szHelp, szLocale, szPath) {
	var szFile
	
	if (szPath) {
		// When provided, go to the defined help folder
		if (szHelp) {
			szFile = szPath + szHelp   // open with a given file
		} else {
			szFile = szPath + "fftoc00.htm"  // by default, open the help content file
		}
	} else {
		// By default, go to the currently cycle help folder
		if (szHelp) {
			szFile = "/fotw0809/help/" + szHelp   // open with a given file
		} else {
			szFile = "/fotw0809/help/fftoc00.htm"  // by default, open the help content file
		}
	}

	if (szLocale && szLocale.indexOf("es_ES") > -1) {
		szFile = "/es_ES" + szFile ;
	}

	openWindow(clHandleHelp, szFile, "help", szHelpWinFeatures);
}

function ConfirmKey(szValue ) {
    var bRet = true
	return bRet
}

function CloseHelp() {
	if (clHandleHelp)
	{
		clHandleHelp.close()
		clHandleHelp = null
	}
}

function Print() {
	if (window.print) {
		// NN 4.x and IE 5.x browsers use the built-in print function
		window.print();
	} else 	{
		if (document.all) {
			// IE 4.x browsers use the bultin-in WebBrowser control
			document.body.insertAdjacentHTML("beforeEnd", "<object id=\"printWB\" width=0 height=0 classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\"></object>");
			execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
			printWB.outerHTML = "";
		} else {
			// NN 3.x or other unsupported browser
			alert(szPrintMenu)
		}
	}
}

function CustServ(szLocale) {
	var szHelp;

	if (szLocale && szLocale.indexOf("es_ES") > -1) {
		szHelp = "/es_ES/fotw0809/help/cs.htm";
	} else { 
		szHelp = "/fotw0809/help/cs.htm";
	}
	
	openWindow(clHandleCust, szHelp, "CustomerService", szHelpWinFeatures);	
}


//open PIN site with requested page
function PIN(szFile) {
	openWindow(clHandleExternal, szFile, "pin", szExternalWinFeatures);	
}

//Exits from the app to index page
function FafsaHome() {
	document.FAFSA.dowhat.value = "exit"
	document.FAFSA.submit()	
}
// calls servlet with "dowhat = next"
function NextPage() {
	if (validate() == true) {
		document.FAFSA.dowhat.value = "next"
		document.FAFSA.submit()
	}
}
// calls servlet with "dowhat = prev"
function PrevPage() {
	document.FAFSA.dowhat.value = "prev"
	document.FAFSA.submit()
}
// calls servlet with "dowhat = summary"
function Summary() {
	document.FAFSA.dowhat.value = "summary"
	document.FAFSA.submit()
}
// calls servlet with "dowhat = save"
function Save() {
	document.FAFSA.dowhat.value = "save"
	document.FAFSA.submit()
}
// calls servlet with "dowhat = printit"
function FinalChk() {
	document.FAFSA.dowhat.value = "printit"
	document.FAFSA.submit()
}
// calls servlet with "dowhat = display"
function DisplayPage(id, step) {
	document.FAFSA.pageid.value = id;
	document.FAFSA.dowhat.value = "display";
	if (step && step != "") {
		document.FAFSA.currentsection.value = step;
	}
	document.FAFSA.submit();
}

// calls servlet with "dowhat = exit"
function ExitForm(iShort){
    var szMsg = "";
    if (iShort && iShort == 1) {
        szMsg = szExitFormShort;
    } else {
        szMsg = szExitFormLong;
    }
	if (confirm(szMsg)) {
		FafsaHome()
	}
}

function IndexPage(szFile, szLocale) {

	if (szLocale && szLocale.indexOf("es_ES") > -1) {
		szFile = "/es_ES/" + szFile;
	} else { 
		szFile = "/" + szFile;
	}
	
	popWindow(szFile,800,600,1);
}

function Step(iStep){
	if (iStep <= 9)
	{
		document.FAFSA.dowhat.value = "step0"+iStep	
	} else {
		document.FAFSA.dowhat.value = "step"+iStep
	}
	document.FAFSA.submit()
}

// Exit for FAA
function FAAExit(bSave){
	var szMsg = "";
	if (bSave) {
		szMsg = szFAAExitShort;
	} else {
		szMsg = szFAAExitLong;
	}
	if (confirm(szMsg))	{
		with(document.FAFSA) {
			dowhat.value = "exit"
			submit()	
		}
	}
}

// Return to FAA Menu
function FAAMenu(bSave){
	var szMsg = "";
	if (bSave) {
		szMsg = szFAAMenuLong;
	} else {
		szMsg = szFAAMenuShort;
	}	
	if (confirm(szMsg)) {
		with(document.FAFSA) {
			dowhat.value = "menu"
			submit()	
		}
	}
}

// Return to Login (for FAA)
function ReturnToLogin(bSave){
	var szMsg = "";
	if (bSave) {
		szMsg = szReturnToLoginLong;
	} else {
		szMsg = szReturnToLoginShort;
	}	
	if (confirm(szMsg)) {
		with(document.FAFSA) {
			dowhat.value = "returntologin"
			submit()	
		}
	}
}

function OpenCust(locale) {
    var szVal  = document.URL
    if (locale != null && locale.indexOf("es_ES") > -1) {
	   if(szVal.indexOf("fafsa.ed.gov") > -1) {
		window.open('https://fafsalivehelp01.ed.gov/FSAICWebAPI-ES/Chat/HtmlChatFrameSet.jsp ', 'CustomerService', 'scrollbars=yes,width=600,height=375')
	   } else {
	   window.open('http://10.33.88.9/FSAICWebAPI-ES/Chat/HtmlChatFrameSet.jsp', 'CustomerService', 'scrollbars=yes,width=600,height=375')
	   }
	} else {
	   if(szVal.indexOf("fafsa.ed.gov") > -1) {
		window.open('https://fafsalivehelp01.ed.gov/FSAICWebAPI-EN/Chat/HtmlChatFrameSet.jsp', 'CustomerService', 'scrollbars=yes,width=600,height=375')
	   } else {
       window.open('http://10.33.88.9/FSAICWebAPI-EN/Chat/HtmlChatFrameSet.jsp', 'CustomerService', 'scrollbars=yes,width=600,height=375')
       }
    }
}

function CheckBrowser(locale) {
	if (!document.getElementById) {
		if (locale && locale.indexOf("es_ES") > -1) {
			location.href = "/es_ES/unsupprt.htm";
		} else {
			location.href = "/unsupprt.htm";
		}
	}
}

function RefreshSession() {
	// timeout 25 minutes (25x60x1000 = 1500000 millisec)
	setTimeout("ConfirmRefresh()", 1500000);
}
function ConfirmRefresh() {
	var szHour;
	var szMin;
	var szSec;
	var szNoon;

	var szHour1;
	var szMin1;
	var szSec1;
	var szNoon1;
	
	// get current date and time
	var clDate = new Date();
	szHour = clDate.getHours();
	szMin = clDate.getMinutes();
	szSec = clDate.getSeconds();
	
	// add 5 minutes
	if (szMin < 55) {
		szMin1 = szMin + 5;
		szHour1 = szHour;
	} else {
		szMin1 = 5 - (59 - szMin + 1);
		szHour1 = szHour + 1;
		if (szHour1 == 24) {
			szHour1 = 0;
		}
	}
	
	// adjust hour format
	if (szHour < 12) {
		szNoon = "AM";
	} else if (szHour == 12) {
		szNoon = "PM";
	} else {
		szHour = szHour - 12;
		szNoon = "PM";
	}

	if (szHour1 < 12) {
		szNoon1 = "AM";
	} else if (szHour1 == 12) {
		szNoon1 = "PM";
	} else {
		szHour1 = szHour1 - 12;
		szNoon1 = "PM";
	}	
		
	// no change to seconds
	szSec1 = szSec;

	// format hh:mm:ss
	if (szHour < 10) {
		szHour = "0" + szHour;
	}
	if (szMin < 10) {
		szMin = "0" + szMin;
	} 
	if (szSec < 10) {
		szSec = "0" + szSec;
	}
	if (szHour1 < 10) {
		szHour1 = "0" + szHour1;
	}
	if (szMin1 < 10) {
		szMin1 = "0" + szMin1;
	} 
	if (szSec1 < 10) {
		szSec1 = "0" + szSec1;
	}
			
	var szMsg = szSessionIdleMsg;
	szMsg = szMsg + szHour + ":" + szMin + ":" + szSec + " " + szNoon
	szMsg = szMsg + szSessionExpireMsg
	szMsg = szMsg + szHour1 + ":" + szMin1 + ":" + szSec1 + " " + szNoon1
	szMsg = szMsg + szSessionRefreshMsg;
	
	if (confirm(szMsg)) {
		with(document.FAFSA) {
			dowhat.value = "display"
			submit()	
		}
	}
}

function AlertKeySize() {
	alert(szUse128Msg);
	return;
}