// JavaScript Document
// Jacascript code for "admissions/formfinancialaid.shtml"
var d = new Date(); var curr_date = d.getDate(); var curr_month = d.getMonth(); curr_month++;
var curr_year = d.getFullYear(); var formateddate = curr_month + "/" + curr_date + "/" + curr_year;
function fillWithDate(element) {element.value = formateddate;}
function alphaNumericCheck(elementName) {
	var regex = /^[0-9A-Za-z]+$/; //^[a-zA-z]+$/
	if(regex.test(elementName.value)) {
		return true;
	} else {
		elementName.value = '0';
		//alert("Enter only numbers.\nPlease do not enter commas or periods.");
	}
}
function numCheck(elementName) {
  if (elementName.value == "") elementName.value = "0";
  var a = elementName.value.split("");
  var b = "";
  for (var i in a)
    if (a[i].match(/\d/))
    b += a[i];
  while (b.length>1 && b.charAt(0) == "0") b=b.substr(1,b.length);
  elementName.value = b;
}
function formatNumbers(strNum) {
	var regEx2 = /(\d+)(\d{3})/;
	while(regEx2.test(strNum)){
		strNum = strNum.replace(regEx2, '$1' + ',' + '$2');
	}
	return strNum;
}
function studentIDcheck(elemName) {
	var studentIDpattern = /^[Bb0Hh]0[0-9]{7}$/;
	if (elemName.value.search(studentIDpattern) == -1) {
		alert("Your student ID is entered incorrectly.\nPlease enter your ID like: B00123456.");
		//var id_var=prompt('Your student ID is entered incorrectly.\nPlease enter your ID like: B00123456.',elemName.value);
		//elemName.value = (id_var) ? id_var : elemName.value;
		elemName.focus();
		return false;
	}
}
function emailAddrCheck(elemName) {
	var email_address_pattern = /^.+@.+\..+$/;
	if (elemName.value.search(email_address_pattern) == -1) {
		alert("Please enter your email address in the correct format.");
		//var email_var=prompt('You have missed a required field.\nPlease enter your email address.','');
		//elemName.value = (email_var) ? email_var : elemName.value;
		elemName.focus();
		return false;
	}
}
function dumbQuotes(id) {
	var replacements, regex, key, textnode, s;

	replacements = {
		"\xa0": " ",
		"\xa9": "(c)",
		"\xae": "(r)",
		"\xb7": "*",
		"\u2018": "'",
		"\u2019": "'",
		"\u201c": '"',
		"\u201d": '"',
		"\u2026": "...",
		"\u2002": " ",
		"\u2003": " ",
		"\u2009": " ",
		"\u2013": "-",
		"\u2014": "--",
		"\u2122": "(tm)"};
	regex = {};
	for (key in replacements) {
		regex[key] = new RegExp(key, 'g');
	}
	
	s = document.getElementById(id).value;
	for (key in replacements) {
		s = s.replace(regex[key], replacements[key]);
	}
	document.getElementById(id).value = s;
}
function SelectMoveRows(from,to) {
	var SS1 = document.getElementById(from);
	var SS2 = document.getElementById(to);
    var SelID, SelText;
    // Move rows from SS1 to SS2 from bottom to top
    for (i=SS1.options.length - 1; i>=0; i--) {
        if (SS1.options[i].selected == true) {
            SelID=SS1.options[i].value;
            SelText=SS1.options[i].text;
            var newRow = new Option(SelText,SelID, true, false);
            SS2.options[SS2.length]=newRow;
            SS1.options[i]=null;
        }
    }
    SelectSort(SS2);
}
function SelectSort(SelList){
    var ID, Text;
    for (x=0; x < SelList.length - 1; x++) {
        for (y=x + 1; y < SelList.length; y++) {
            if (SelList[x].text > SelList[y].text) {
                // Swap rows
                ID=SelList[x].value;
                Text=SelList[x].text;
                SelList[x].value=SelList[y].value;
                SelList[x].text=SelList[y].text;
                SelList[y].value=ID;
                SelList[y].text=Text;
            }
        }
    }
}
function selectAllOptions(selStr) {
  var selObj = document.getElementById(selStr);
  for (var i=0; i<selObj.options.length; i++) {
    selObj.options[i].selected = true;
  }
}
function setInnerHTML(id, HTML) {
	document.getElementById(id).innerHTML = HTML;
}
function subtotalA(formName) {
	formName.Subtotalincomeandassets.value = parseFloat(formName.lineone.value) + parseFloat(formName.linetwo.value);
	setInnerHTML('subTotalATxt', '<b>'+formName.Subtotalincomeandassets.value+'</b>');
	//formName.Subtotalincomeandassets.value = formatNumbers(formName.Subtotalincomeandassets.value);
}
function subtotalB(formName) {
	formName.Expensessubtracted.value = parseFloat(formName.Subtotalincomeandassets.value) - parseFloat(formName.linethree.value);
	//formName.Expensessubtracted.value = formatNumbers(formName.Expensessubtracted.value);
	setInnerHTML('subTotalBTxt', '<b>'+formName.Expensessubtracted.value+'</b>');
}
function subtotalC(formName) {
	formName.SubTotalC.value = parseFloat(formName.Expensessubtracted.value) - parseFloat(formName.TuitionAndBooks.value);
	formName.SubTotalC.value = formatNumbers(formName.SubTotalC.value);
	setInnerHTML('subTotalCTxt', '<b>'+formName.SubTotalC.value+'</b>');
}
function updateTotals(formName) {
	formName.NetAnnualIncome.value = formName.lineone.value;
	setInnerHTML('netAnnualIncomeTxt', '<b>'+formName.NetAnnualIncome.value+'</b>');
	formName.NetAssets.value = formName.linetwo.value;
	setInnerHTML('netAssetsTxt', '<b>'+formName.NetAssets.value+'</b>');
	formName.Expenses.value = formName.linethree.value;
	setInnerHTML('expensesTxt', '<b>'+formName.Expenses.value+'</b>');
	formName.TuitionAndBooks.value = formName.linefour.value;
	setInnerHTML('tuitionAndBookCostsTxt', '<b>'+formName.TuitionAndBooks.value+'</b>');
	subtotalA(formName);
	subtotalB(formName);
	subtotalC(formName);
}
function addIncome(formName, elementName) {
	numCheck(elementName);
	formName.lineone.value = parseFloat(formName.EstimatedAnnualIncome.value) +	parseFloat(formName.AnnualSupportIncome.value) + parseFloat(formName.OtherIncome.value) + parseFloat(formName.TuitionAssistance.value) + parseFloat(formName.Loans.value);
	updateTotals(formName);
}
function addAssets(formName, elementName) {
	numCheck(elementName);
	formName.linetwo.value = parseFloat(formName.Savings.value) + parseFloat(formName.Stocksbonds.value) + parseFloat(formName.Otherliquidassets.value);
	updateTotals(formName);
}
function addExpenses(formName, elementName) {
	numCheck(elementName);
	formName.linethree.value = parseFloat(formName.Tithe.value) + parseFloat(formName.Housing.value) + parseFloat(formName.Utilities.value) + parseFloat(formName.Food.value) + parseFloat(formName.Clothingandpersonal.value) + parseFloat(formName.Automobile.value) + parseFloat(formName.Insurance.value) + parseFloat(formName.Childcare.value) + parseFloat(formName.MedicalExpenses.value) + parseFloat(formName.Debtrepayment.value) + parseFloat(formName.Taxes.value) + parseFloat(formName.Miscellaneous.value);
	updateTotals(formName);
}
function addTuition(formName, elementName) {
	numCheck(elementName);
	formName.linefour.value = parseFloat(formName.Projecteddivinitytuition.value) + parseFloat(formName.Projectedjointtuition.value) + parseFloat(formName.Estimatedbooks.value);
	updateTotals(formName);
}
function updateStatus(formName) { // line 159
	if(formName.maritalStatus.options[1].selected == true) {
		formName.workingSpouse.options[2].selected = true; // line 168
		formName.SpouseSignature.disabled = true; // line 512
		formName.sigdate2.disabled = true;
		//alert("Single is slected");
	} else {
		formName.workingSpouse.options[2].selected = false;
		formName.workingSpouse.options[0].selected = true; // line 168
		formName.SpouseSignature.disabled = false; // line 512
		formName.sigdate2.disabled = false;
		//alert("Married or None is slected");
	}
}
function updateJoint(formName) {
	if(formName.jointDegree.options[1].selected == true) {
		formName.jointSchool.disabled = false; // line 199
	} else {
		formName.jointSchool.disabled = true; // line 199
	}
}
function monthToYear(monthlyExpense) {
	var yearlyExpense = parseFloat(monthlyExpense) * 12;
	document.getElementById('yearlytotal').value = yearlyExpense;
}
function switchAwardsLevel() {
	var currentAwardelem = document.getElementById('academicProgram');
	if(currentAwardelem.selectedIndex >= 3) {
		showId('doctoralAwardsFieldset');
		hideId('mastersAwardsFieldset');
	} else {
		hideId('doctoralAwardsFieldset');
		showId('mastersAwardsFieldset');
	}
}
function switchCurrentStudent() {
	var currentStudentStatus = document.getElementById('currentaid');
	if(!currentStudentStatus.checked) {
		hideId('currentStudent');
	} else {
		showId('currentStudent');
	}
}  // <---------------------------------
function switchId(id){
	var elemStyle = document.getElementById(id).style;
	var elemDisplay = elemStyle.display;
	if (elemDisplay == 'none') {return (hideAllIds() && showId(id)) ? false : true;}
	else {return (hideId(id)) ? false : true;}
}

function hideAllIds(){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){hideId(ids[i]);}
	return true;
}
function showAllIds(){
	//loop through the array and show each element by id
	for (var i=0;i<ids.length;i++){showId(ids[i]);}
	return true;
}

function hideId(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6, Modern Browsers
		document.getElementById(id).style.display = 'none';
	} else {
		if (document.layers) {document.id.display = 'none';} // Netscape 4
		else {document.all.id.style.display = 'none';} // IE 4
	}
	return true;
}

function showId(id) {
	//safe function to show an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6, Modern Browsers
		document.getElementById(id).style.display = 'block';
	} else {
		if (document.layers) {document.id.display = 'block';} // Netscape 4
		else {document.all.id.style.display = 'block';} // IE 4
	}
	return true;
} // <---------------------------------
function beginApp() {
	if(document.getElementById('closeReviewButton').style.display == 'block') {
		var navigationP = getElementsByClassName('navp');
		for (var i=0;i<navigationP.length;i++) {navigationP[i].style.display = 'block';}
		hideAllIds();
		hideId('introduction');
		hideId('closeReviewButton');
		showId('appTypeFieldset');
		setStep(2);
	} else {
		hideId('introduction');
		showId('appTypeFieldset');
		setStep(2);
	}
}
function reviewApp() {
	if(document.getElementById('closeReviewButton').style.display == 'block') {
		beginApp();
	} else {
		var navigationP = getElementsByClassName('navp');
		for (var i=0;i<navigationP.length;i++) {navigationP[i].style.display = 'none';}
		showAllIds();
		showId('closeReviewButton');
		setStep(0);
	}
}
function setStep(step, id) {
	if(id != null) {switchId(id);}
	var progressIncrements = 15; //number of steps in the process
	var stepsPercent = Math.floor((step/progressIncrements)*100); // Raw Percentage
	setInnerHTML('percentText', stepsPercent); // print percentage
	var imageWidth = 243;
	var eachPercent = (imageWidth/2)/100;
	var newPosition = (eachPercent*stepsPercent)-120; //var initial = -120;
	document.getElementById('percentProgressBar').style.backgroundPosition = newPosition+'px 0pt';
}
function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}
window.onload = function() {
	var numBoxes = getElementsByClassName('numbox');
	for (var i=0;i<numBoxes.length;i++) {
		numBoxes[i].onfocus = function () {if(this.value=='0')this.value='';};
		numBoxes[i].onchange = function () {if(this.value=='')this.value='0';};
		//numBoxes[i].onmouseover = function () {this.style.backgroundColor='#ddd'}
		//numBoxes[i].onmouseout = function () {this.style.backgroundColor='#fff'}
	}
	switchAwardsLevel();
	hideAllIds();
	hideId('closeReviewButton');
}
