function validateStep2(form){
	with(form){ 
		if(!validateDropDown(custom1)){
			alert("Please select your School of Interest.");
			form.custom1.focus();
			return false;
		}
		if(!validateDropDown(programId)){
			alert("Please select your Program of Interest.");
			form.programId.focus();
			return false;
		}		
		if(!validateRadioButtonCondition(military)){
			alert("Please indicate your military nature.");
			return false;
		}
		if ( ! validateStartDate123Months ('custom12') ) {
		return false;
	}
	if ( ! validateCommentBox ('comments') ) {
		return false;
	}
	if(! validateJoiningReason ('reasonForJoining') ) { return false; } if(! validateContactQuestion ('contactForQuality') ) { return false;}if(! validateMotivationQuestion ('motivationForQuality') ) {
			return false;
	}
	}
	if(!validateSchoolFormCO(form)){
		return false;
	}
	if(!validateStartDate(form)){
		return false;
	}
	with (form) {
		var programText = programId.options[programId.selectedIndex].text;
	        var educationLevelVal = eduLevel;
	        var age = Age;
        	var schoolVal = custom1.value;
		if ( ((programText.indexOf("Certificate")>=0 || programText.indexOf("EdS")==0 || programText.indexOf("PhD")==0 ) && (educationLevelVal<13)) || ((programText.indexOf("MS")==0 || programText.indexOf("MBA")==0) && (educationLevelVal == "9" || educationLevelVal<8)) ) {
			alert("Based on your current education level you do not qualify for the program/degree you have selected. Please select an Associate or Bachelor program from the Program list.");
			return (false);
		}
        	var sProgram = programValues[programId.value];
	        if(sProgram == 353 || sProgram == 368 || sProgram == 370 || sProgram == 622) {
        		alert("Thank you for your interest in Capella University. The specialization you have selected is for current K-12 professionals interested in extending their education with a graduate degree. Please be aware that this specialization does not lead to initial teacher licensure.");
        	}
	}
	if( educationLevelVal < 7 ) {
		with(form) {
			if(!validateRadioButton(military,0)) {
				 alert("Thank You for your interest in Capella University. Students applying for any program must atleast have either 90+ quarter credits or 60+ semester credits or Active Military Status.");
				 return false;
			}
		}
	}
	return true;
}

function validateStartDate(form) {
	with(form){	
		if(!validateDropDown(custom2) || !validateDropDown(custom3)){
			alert("Please provide when do you want to start");
			return false;
		}
		var currentDate = new Date();
		var currentMonth = currentDate.getMonth();
		var currentYear = currentDate.getFullYear();
		var enteredMonth = custom2.selectedIndex - 1;
		var enteredYear = parseInt(custom3.options[custom3.selectedIndex].value);
		if(enteredYear < currentYear) {
			alert('Your expected start date should be ahead of current date');
			return false;
		}
		else if((enteredYear == currentYear) && (enteredMonth < currentMonth)) {
			alert('Your expected start date should be ahead of current date');
			return false;
		}
	}
	return true;
}

function validateStep1(form) {
	if(!validateSchoolFormCO(form)){
		return false;
	}
	with(form){
		if(!validateDropDown(educationLevel)){
			alert("Please select your education level.");
			return false;
		}	
		if(!validateDropDown(age)){
			alert("Please select your age.");
			return false;
		}					
	}
	return true;
}


function addPrograms(schoolValue, selectedCode) {
    if (schoolValue == "") {
        schoolValue = document.getElementById("custom1").value;
    } else {
        document.getElementById("custom1").value = schoolValue;
    }
    var programNode = document.getElementById("programId");
    for (var i = 1; i < programNode.options.length;) {
        if (programNode.options[i].value == "") {
            i++;
        } else {
            programNode.remove(i);
        }
    }
    
    if(document.getElementById("custom1").selectedIndex == 0)
    	return;
    for (var i = 0; i < programCodes[schoolValue].length; i++) {
        addOption(programNames[schoolValue][i], programCodes[schoolValue][i], programNode);
        if (selectedCode == programNode.options[i+1].value) {
            programNode.options[i+1].selected = true;
        }
    }
}

function addOption(selectbox,text,value )
{
	var optn = document.createElement("option");
	optn.text = text;
	optn.value = value;
	optn.title = text;
	selectbox.options.add(optn);
}

function validateThankYou(form) {
	with(form){
		if(!validateDropDown(educationLevel)){
			alert("Please select your education level.");
			return false;
		}
		if(!validateDropDown(custom1)){
			alert("Please select the school you are interested in.");
			return false;
		}	
		if(!validateDropDown(programId)){
			alert("Please select a degree program you are interested in.");
			return false;
		}	
		if(!validateDropDown(age)){
			alert("Please select your age.");
			return false;
		}					
		if(!validateRadioButtonCondition(military)){
			alert("Please indicate your military nature.");
			return false;
		}	
		if(!validateStartDate(form)){
			return false;
		}   
		
        var programText = programId.options[programId.selectedIndex].text;
        var educationLevelVal = educationLevel.value;
        var schoolVal = custom1.value;

		if ( ((programText.indexOf("Certificate")>=0 || programText.indexOf("EdS")==0 || 
		programText.indexOf("PhD")==0 ) && (educationLevelVal<13)) ||
		((programText.indexOf("MS")==0 || programText.indexOf("MBA")==0) && 
		(educationLevelVal == "9" || educationLevelVal<8)) ) {
			alert("Based on your current education level you do not qualify for the program/degree you have selected. Please select an Associate or Bachelor program from the Program list.");
			return (false);
		}		
        	var sProgram = programValues[programId.value];
	        if(sProgram == 353 || sProgram == 368 || sProgram == 370 || sProgram == 622) {
        		alert("Thank you for your interest in Capella University. The specialization you have selected is for current K-12 professionals interested in extending their education with a graduate degree. Please be aware that this specialization does not lead to initial teacher licensure.");
		}
	}
	if( educationLevelVal < 7 ) {
		with(form) {
			if(!validateRadioButton(military,0)) {
				 alert("Thank You for your interest in Capella University. Students applying for any program must atleast have either 90+ quarter credits or 60+ semester credits or Active Military Status.");
				 return false;
			}
		}
	}
	return true;
}

function onProgramChangeStep1(programId) {
	if(selectedProgramId == -1) {
		document.getElementById('custom1').selectedIndex = 0;
		return;
	}
	
	var schoolOfInterest = document.getElementById('custom1');
	var selectedSchool = "";
	
	for(var i = 1; i < schoolOfInterest.options.length; i++) {
		if(selectedSchool != "") break;
		
		var schoolName = schoolOfInterest[i].value;
		var programOptions = programCodes[schoolName];
		for(var j = 0; j < programOptions.length; j++) {
			if(programOptions[j] == selectedProgramId) {
				selectedSchool = i;
				schoolOfInterest.selectedIndex = i;
				break;
			}
		}
	}
	if(schoolOfInterest.selectedIndex>0)
	addPrograms(schoolOfInterest.options[schoolOfInterest.selectedIndex].value, selectedProgramId);
}

function onProgramChange(selectedProgramId) {
	if(selectedProgramId == -1) {
		document.getElementById('custom1').selectedIndex = 0;
		return;
	}
	
	var schoolOfInterest = document.getElementById('custom1');
	var selectedSchool = "";
	
	for(var i = 1; i < schoolOfInterest.options.length; i++) {
		if(selectedSchool != "") break;
		
		var schoolName = schoolOfInterest[i].value;
		var programOptions = programCodes[schoolName];
		for(var j = 0; j < programOptions.length; j++) {
			if(programOptions[j] == selectedProgramId) {
				selectedSchool = i;
				schoolOfInterest.selectedIndex = i;
				break;
			}
		}
	}
	if(schoolOfInterest.selectedIndex>0)	
		addPrograms(schoolOfInterest.options[schoolOfInterest.selectedIndex].value, selectedProgramId);
}

var programNames = new Array();
var programIds = new Array();

function checkEducationLevelThankYou(eduValue){
	populateOnlineProgramsThankYou(eduValue);
	return true;
}

function populateOnlineProgramsThankYou(eduLevel)
{
	var campusId = document.getElementById('campusId').value;
	var schoolId = document.getElementById('schoolId').value;
	var url = '/gotoLink.htm?jsp=/colleges/getPrograms.jsp&campus='+campusId+'&educationLevel='+eduLevel+'&schoolId='+schoolId;
	http=getHttpRequestObj();
	if (http==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	http.onreadystatechange = function() {
								if(http.readyState == 4){
									var responseString = http.responseText;
									handleHttpResponseGetPrograms(responseString);
								}
							};
	http.open('GET', url, true);
	http.send(null);

}
function handleHttpResponseGetPrograms(responseString) {
	// code for IE
	var xmlObject;
	if (window.ActiveXObject) {
	  xmlObject=new ActiveXObject("Microsoft.XMLDOM");
	  xmlObject.async="false";
	  xmlObject.loadXML(responseString);
	 }
	// code for Mozilla, Firefox, Opera, etc.
	else {
		xmlObject = (new DOMParser()).parseFromString(responseString, "text/xml");
	}
	var locationsElements = xmlObject.getElementsByTagName('program');
	
	programNames.splice(0,programNames.length);
	programIds.splice(0,programIds.length);
	
	
	for(var i = 0; i < locationsElements.length; i++) {
		var locationElement = locationsElements[i];
		programNames.push(locationElement.getAttribute('name'));
		programIds.push(locationElement.getAttribute('id'));
		programAOI.push(locationElement.getAttribute('aoi'));
		programValues[locationElement.getAttribute('id')] = locationElement.getAttribute('value');
	}
	populateProgramsForAOIThankYou();
}

function onEducationChange() {
	removeAllOptions(document.getElementById('programId'));
	addOption(document.getElementById('programId'), 'Select Program', '-1');
	for(var i = 0; i < programNames.length; i++) {
		addOption(document.getElementById('programId'), programNames[i], programIds[i]);
	}
}
var aoiNames = new Array();
var aoiProgramNames = new Array();
var aoiProgramIds = new Array();
	
function populateProgramsForAOIThankYou() {
	// Remove all elements from the array
	aoiNames.splice(0, aoiNames.length);
	aoiProgramNames.splice(0, aoiProgramNames.length);
	aoiProgramIds.splice(0, aoiProgramIds.length);
	
	aoiNames.push('--------- Select One ---------');
	
	var programsArray = new Array();
	programsArray.push('--------- Select a Program ---------');
	aoiProgramNames.push(programsArray);
	
	programsArray = new Array();
	programsArray.push('-1');
	aoiProgramIds.push(programsArray);
	
	// Add all AOIs and associate the programs with the area of interest
	for(var i = 0; i < programNames.length; i++) {
		var aoi = programAOI[i];
		var aoiIndex = getIndexOfElementInArray(aoi, aoiNames);
			
		if(aoiIndex == -1) {
			aoiNames.push(aoi);
			programsArray = new Array();
			programsArray.push('--------- Select a Program ---------');
			aoiProgramNames.push(programsArray);
			
			programsArray = new Array();
			programsArray.push('-1');
			aoiProgramIds.push(programsArray);
			aoiIndex = aoiNames.length - 1;
		}
		
		programsArray = aoiProgramNames[aoiIndex];
		programsArray.push(programNames[i]);
		
		programsArray = aoiProgramIds[aoiIndex];
		programsArray.push(programIds[i]);
	}
	
	removeAllOptions(document.getElementById('custom1'));
	addOption(document.getElementById('custom1'), aoiNames[0], '');
	for(var i = 1; i < aoiNames.length; i++) {
		addOption(document.getElementById('custom1'), aoiNames[i], aoiNames[i]);
	}
	document.getElementById('custom1').selectedIndex = 0;
	onAOIChange(document.getElementById('custom1'));
}

function onAOIChange(aoiHandler) {
	
	removeAllOptions(document.getElementById('programId'));
	
	var aoiSelectedIndex = aoiHandler.selectedIndex;
	
	var names = aoiProgramNames[aoiSelectedIndex];
	var ids = aoiProgramIds[aoiSelectedIndex];
	
	for(var i = 0; i < names.length; i++) {
		addOption(document.getElementById('programId'), names[i], ids[i]);
	}
}

function selectProgramAndAOI(program) {
	if(program == '') {
		return;
	}
	
	// There is some programId value. Just iterate over the whole list of aoi and program and select the appropriate.
	for(var i = 0; i < aoiNames.length; i++) {
		for(var j = 0; j < aoiProgramIds[i].length; j++) {
			if(aoiProgramIds[i][j] == program) {
				document.getElementById('custom1').selectedIndex = i;
				onAOIChange(document.getElementById('custom1'));
				document.getElementById('programId').selectedIndex = j;
				return;
			}
		}
	}
}

function onProgramChange(programId) {
	selectProgramAndAOI(clickedProgramId);
}
