function showImage(aImage, aLayer) {
	document[aLayer].src = "../img/programs/" + aImage;
}

function hideImage(aImage, aLayer) {
	document[aLayer].src = "../img/programs/" + aImage;
}

function SymbolShow(aImage, aLayer, aLevel) {
	document[aLayer].src = aLevel + "img/" + aImage;
}

function SymbolHide(aImage, aLayer, aLevel) {
	document[aLayer].src = aLevel + "img/" + aImage;
}

function overImage(aImage, aLayer, aFolder) {	
	document[aLayer].src = "../../img/faculty/nav/" + aFolder + "/" + aImage;
}

function outImage(aImage, aLayer, aFolder) {	
	document[aLayer].src = "../../img/faculty/nav/" + aFolder + "/" + aImage;
}

function printShow(aImage, aLayer) {
	document[aLayer].src = "../img/buttons/" + aImage;
}

function printHide(aImage, aLayer) {
	document[aLayer].src = "../img/buttons/" + aImage;
}

function checkcvv(cvv){
	re = /^\d{3}$/;
	return re.test(cvv);
}

function checkForm(){	
	if (document.scholarship.fname.value.length <= 2) {
		alert("Please input your first name correctly!"); document.scholarship.fname.focus(); return false;
	}
	else if (document.scholarship.lname.value.length <= 2) {
		alert("Please input your last name correctly!"); document.scholarship.lname.focus(); return false;
	}
	else if (document.scholarship.dob.value.length < 10){
		alert("Please enter your date of birth correctly!"); document.scholarship.dob.focus(); return false;
	}	
	else if (document.scholarship.address.value.length < 5) {
		alert("Please enter your address correctly!"); document.scholarship.address.focus(); return false;
	}
	else if (document.scholarship.city.value.length < 3) {
		alert("Please enter city correctly!"); document.scholarship.city.focus(); return false;
	}
	else if (document.scholarship.country.value.length < 2) {
		alert("Please enter country correctly!"); document.scholarship.country.focus(); return false;
	}
	else if (checkEmail(document.scholarship.email.value) == false) {
		alert("Please enter your email address correctly!"); document.scholarship.email.focus(); return false;
	}
	else if (document.scholarship.instrument.value.length < 4) {
		alert("Please choose your principal instrument!"); document.scholarship.instrument.focus(); return false;
	}
	else if (document.scholarship.program.value == "N/A") {
		alert("Please choose which program you are entering!"); document.scholarship.program.focus(); return false;
	}
	else if (document.scholarship.highest.value.length <= 2) {
		alert("Please input your highest education!"); document.scholarship.highest.focus(); return false;
	}
	else if (document.scholarship.graddate.value.length < 10){
		alert("Please enter your graduation date!"); document.scholarship.graddate.focus(); return false;
	}
	else return true;
}

function appsForm() {
	if (document.onlineapps.fname.value.length <= 1) {
		alert("Please input your first name correctly!"); document.onlineapps.fname.focus(); return false;
	}
	else if (document.onlineapps.lname.value.length <= 1) {
		alert("Please input your last name correctly!"); document.onlineapps.lname.focus(); return false;
	}
	else if (checkEmail(document.onlineapps.email.value) == false){
		alert("Please enter your email address correctly!"); document.onlineapps.email.focus(); return false;
	}
	else if (document.onlineapps.gender.value.length < 4){
		alert("Please enter your gender!"); document.onlineapps.gender.focus(); return false;
	}
	else if (document.onlineapps.dob.value.length < 10){
		alert("Please enter your date of birth correctly!"); document.onlineapps.dob.focus(); return false;
	}
	else if (document.onlineapps.address.value.length < 5) {
		alert("Please enter your address correctly!"); document.onlineapps.address.focus(); return false;
	}
	else if (document.onlineapps.city.value.length < 3) {
		alert("Please enter city correctly!"); document.onlineapps.city.focus(); return false;
	}
	else if (document.onlineapps.country.value.length < 2) {
		alert("Please enter country correctly!"); document.onlineapps.country.focus(); return false;
	}
	else if (document.onlineapps.period.value.length < 0) {
		alert("Please enter how long you have been playing an instrument / singing!"); document.onlineapps.period.focus(); return false;
	}
	else if (document.onlineapps.ename.value.length < 0) {
		alert("Please enter emergency contact name correctly!"); document.onlineapps.ename.focus(); return false;
	}
	else if (document.onlineapps.eaddress.value.length < 0) {
		alert("Please enter address correctly!"); document.onlineapps.eaddress.focus(); return false;
	}
	else if (document.onlineapps.ecity.value.length < 0) {
		alert("Please enter city correctly!"); document.onlineapps.ecity.focus(); return false;
	}
	else if (document.onlineapps.ecountry.value.length < 0) {
		alert("Please enter country correctly!"); document.onlineapps.ecountry.focus(); return false;
	}
	else if (document.onlineapps.ssn.value.length > 0) {
		if (document.onlineapps.hphone.value.length >= 1) {
			if (checkPhone(document.onlineapps.hphone.value) == false) {
				alert("Please input your phone number correctly!"); document.onlineapps.hphone.focus(); return false;
			}
		}
		if (document.onlineapps.cphone.value.length >= 1) {
			if (checkPhone(document.onlineapps.cphone.value) == false) {
				alert("Please input your cellphone number correctly!"); document.onlineapps.cphone.focus(); return false;
			}
		}
		if (document.onlineapps.zip.value.length >= 1) {
			if (checkZip(document.onlineapps.zip.value) == false) {
				alert("Please enter your zip code correctly!"); document.onlineapps.zip.focus(); return false;
			}
		}
	}
	else if (checkEmail(document.onlineapps.eemail.value) == false) {
		alert("Please enter your email address correctly!"); document.onlineapps.eemail.focus(); return false;
	}
	else return true;
}

function contactForm(){	
	if (document.contact.fname.value.length <= 2) {
		alert("Please input your first name correctly!"); document.contact.fname.focus(); return false;
	}
	else if (document.contact.lname.value.length <= 2) {
		alert("Please input your last name correctly!"); document.contact.lname.focus(); return false;
	}
	else if (checkEmail(document.contact.email.value) == false) {
		alert("Please enter your email address correctly!"); document.contact.email.focus(); return false;
	}
	else if (document.contact.email.value < 1) {
		alert("Please enter your email address correctly!"); document.contact.email.focus(); return false;
	}
	else if (document.contact.phone.value.length >= 1) {
		if (checkPhone(document.contact.phone.value) == false) {
			alert("Please input your phone number correctly!"); document.contact.phone.focus(); return false;
		}
	}
	else return true;
}

function valbutton() {
	// place any other field validations that you require here
	// validate myradiobuttons
	myOption = -1;
	myOption2 = -1;
	myOption3 = -1;
	myOption4 = -1;
	myOption5 = -1;
	myOption6 = -1;
	
	for (i = document.onlineapps.resident.length-1; i > -1; i--) {
		if (document.onlineapps.resident[i].checked) {
			myOption = i; i = -1;
		}
	}
	
	for (i = document.onlineapps.pickup.length-1; i > -1; i--) {
		if (document.onlineapps.pickup[i].checked) {
			myOption2 = i; i = -1;
		}
	}
	
	for (i = document.onlineapps.housing.length-1; i > -1; i--) {
		if (document.onlineapps.housing[i].checked) {
			myOption3 = i; i = -1;
		}
	}
	
	for (i = document.onlineapps.type.length-1; i > -1; i--) {
		if (document.onlineapps.type[i].checked) {
			myOption4 = i; i = -1;
		}
	}
	
	for (i = document.onlineapps.programs.length-1; i > -1; i--) {
		if (document.onlineapps.programs[i].checked) {
			myOption5 = i; i = -1;
		}
	}
	
	for (i = document.onlineapps.department.length-1; i > -1; i--) {
		if (document.onlineapps.department[i].checked) {
			myOption6 = i; i = -1;
		}
	}

	if (myOption == -1) {
		alert("Are you U.S. Citizen or Residents?"); return false;
	}
	
	if (myOption2 == -1) {
		alert("Will you need to be picked up from the airport when you arrive in the U.S.?"); return false;
	}
	
	if (myOption3 == -1) {
		alert("Will you need housing assistance?"); return false;
	}
	
	if (myOption4 == -1) {
		alert("which housing option would you prefer?"); return false;
	}
	
	if (myOption5 == -1) {
		alert("Which program are you entering?"); return false;
	}
	
	if (myOption6 == -1) {
		alert("which department are you choosing?"); return false;
	}

	document.onlineapps.submit(); // this line submits the form after validation
}

function validateForm() { //v4.0
	var today = new Date();
	var month = document.emailform.exp_mo;
	var mindex = month.selectedIndex;
	var exp_month = month.options[mindex].value;
	
	var year = document.emailform.exp_yr;
	var yindex = year.selectedIndex;
	var exp_year = year.options[yindex].value;
	
	if (selectedTypeCard() == -1){
		alert("Please choose Debit or Credit Card you want to use."); return false; 
	}
	else if (document.emailform.cardtype.selectedIndex == 0){
		alert("Please choose type of Credit/Debit card you want to use."); return false;
	}
	else if (document.emailform.cardnumber.value.length == 0){
		alert("You must enter your credit card number"); return false;
	}
	else if (checkNumber() == false ){
		alert("Your credit card number is invalid"); return false;
	}
	else if (exp_year < (today.getFullYear())){
		alert("The year is invalid"); return false;
	}
	else if (checkcvv(document.emailform.cvv.value) == false){
		alert("You must enter your credit card security code"); return false;
	}
	else if (document.emailform.nameoncard.value.length == 0){
		alert("Please input name on your credit card."); return false;
	}
	else if (document.emailform.address.value.length == 0){
		alert("Please input correct billing address."); return false;
	}
	else if (document.emailform.city.value.length == 0){
		alert("Please input correct city."); return false;
	}
	else if (document.emailform.state.value.length == 0){
		alert("Please input correct state."); return false;
	}
	else if (document.emailform.country.value.length == 0){
		alert("Please input correct country."); return false;
	}
	else if (document.emailform.authorize.checked == false){
		alert("Please check the credit card authorization agreement."); return false;
	}
	else if (exp_year == (today.getFullYear())){
		if (exp_month < (today.getMonth() + 1)){
			alert("The month is invalid"); return false;
		}
	}
	else return true;	
}

function luhn(num) {
   var luhnTotal=0;
   for (i=num.length-1; i>=0; i--) {
      luhnTotal += parseInt(num.charAt(i));
      i--;
      num2 = new String(num.charAt(i)*2);
      for (j=0; j < num2.length; j++) {
         luhnTotal += parseInt(num2.charAt(j));
      }
    }
   return (luhnTotal % 10 == 0);
}

function selectedTypeCard() {
	type = -1;
	for (i = 0; i < 2; i++) {
		if (document.emailform.dorc[i].checked) type = i;
	}
	return type;
}

function checkNumber(){
	wsre = /\s/g
	cnum = document.emailform.cardnumber.value.replace(wsre, "");

	$id = document.emailform.cardtype.selectedIndex;
	switch($id){		
		case 1: re = /^6011\d{12}$/; break;
		case 2: re = /^5[1-5]\d{14}$/; break;
		case 3: re = /^4(\d{12}|\d{15})$/; break;
	}

	pattern = re.test(cnum);
	luhnCheck = luhn(cnum);
	return (pattern && luhnCheck);
}

function selectedTypeCard() {
	type = -1;
	for (i = 0; i < 2; i++) {
		if (document.emailform.dorc[i].checked) type = i;
	}
	return type;
}

function checkPhone(phone) {
	var re = /\d{3}\-\d{3}\-\d{4}/;
	return (re.test(phone));
}

function checkZip(zip) {
	var re = /^\d{5}([\-]\d{4})?$/;
	return (re.test(zip));
}

function checkEmail(email) {
	re = /^\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,8}$/;
	return re.test(email);
}
