var selectedProgramId;
var aoiNames = new Array();
var aoiProgramNames = new Array();
var aoiProgramIds = new Array();

function validateRequiredStep2(form) {
	with(form) {
		// Area of interest is required.
		if(custom2.selectedIndex == 0) {
			alert('Please enter an area of interest');
			return false;
		}
		
		// Program is required.
		if(programId.selectedIndex == 0) {
			alert('Please enter a degree program you are interested in');
			return false;
		}
		
		// ProgramId should be a positive integer
		if(!(parseInt(programId.options[programId.selectedIndex].value) > 0)) {
			alert('Please enter a degree program you are interested in');
			return false;
		}
		
		// Address 1 is required
		if(address1.value == null || address1.value == '') {
			alert('Please provide your street address');
			return false;
		}
		
		// City is required
		if(city.value == null || city.value == '') {
			alert('City is required');
			return false;
		}
		
		// State is required
		if(state.selectedIndex == 0) {
			alert('State is required');
			return false;
		}
		
		// Either of the three phones is required
		if((homePhone.value == null	|| homePhone.value == '') &&
			(workPhone.value == null || workPhone.value == '') &&
			(otherPhone.value == null || otherPhone.value == '')) {
			alert('Please provide atleast one valid phone number');
			return false;
		}
		
		// Email address is required
		if(email.value == null || email.value == '') {
			alert('Please provide your email address.');
			return false;
		}
	}

	
	return true;
}

function validatePreferedPhone(form, customField) {
	with(form) {
		var preferedPhoneVal = customField.options[customField.selectedIndex].value;
		
		if((homePhone.value == null || homePhone.value == '') && preferedPhoneVal == 'home') {
			alert('Please enter home phone first');
			return false;
		}
		if((workPhone.value == null || workPhone.value == '') && preferedPhoneVal == 'work') {
			alert('Please enter work phone first');
			return false;
		}
		if((otherPhone.value == null || otherPhone.value == '') && preferedPhoneVal == 'mobile') {
			alert('Please enter mobile phone first');
			return false;
		}
	}
	return true;
}

function validateMaskStep2(form) {
	with(form) {
		var phoneRegEx = /^(\s*(\d{3}|\(\s*\d{3}\s*\))\s*[-.]?\s*(\d{3}|\(\s*\d{3}\s*\))\s*[-.]?\s*(\d{4}|\(\s*\d{4}\s*\))\s*)$/;
		var emailRegEx = /^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/;
 		// Match phone numbers
		var streetRegEx = /^(.+\s.+)$/;
 		if(homePhone.value != null && homePhone.value != '' && !phoneRegEx.test(homePhone.value)) {
			alert('Please provide your 10-digit home phone number.');
			return false;
		}
		if(workPhone.value != null && workPhone.value != '' && !phoneRegEx.test(workPhone.value)) {
			alert('Please provide your 10-digit work phone number.');
			return false;
		}
		if(otherPhone.value != null && otherPhone.value != '' && !phoneRegEx.test(otherPhone.value)) {
			alert('Please provide your 10-digit other phone number.');
			return false;
		}
		
		// Match email
		if(email.value != null && email.value != '' && !emailRegEx.test(email.value)) {
			alert('Please provide your email address, i.e. yourname@email.com');
			return false;
		}
		
		if(address1.value != null && address1.value != '' && !streetRegEx.test(address1.value)) {
			alert('Please provide your valid street address.');
			return false;
		}
	}
	return true;
}

function validateStep2(form){
	
	if(!validateRequiredStep2(form)) {
		return false;
	}
	if(!validateMaskStep2(form)) {
		return false;
	}
	with(form) {
		
		if(!validateProgram(form)) {
			return false;
		}
		
		if(!validateDropDown(custom9)) {
			alert('Please select the prefered phone to call you');
			return false;
		}
		
		if(!validatePreferedPhone(form, custom9)) {
			return false;
		}
		
		if(validateDropDown(custom10)) {
			if(!validatePreferedPhone(form, custom10)) {
				return false;
			}
		}
			
		if(!validateRadioButtonCondition(citizenship)) {
			alert('Please select if you are a US citizen');
			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(!validateCheckBox(canContact)){
			alert('Please check the box acknowledging that you understand you will be contacted by a University of Phoenix representative.');
			return false;
		}
		var programValue = programValues[programId.options[programId.selectedIndex].value];
		if(programValue == 'BSN' || programValue == 'MSN' || programValue == 'SNC'
			|| programValue == 'MSN/ED' || programValue == 'MSN/MHA' || programValue == 'MSN/MBA/HC'
			|| programValue == 'MSN/NPFT' || programValue == 'MSN/FNP' || programValue == 'LPN/BSN'
			|| programValue == 'LVN/BSN') {
			if(!validateRegisteredNurse(custom8)) {
				return false;
			}
		}
		
		if(homePhone.value == null || homePhone.value == '') {
			if(workPhone.value != null && workPhone.value != '') {
				homePhone.value = workPhone.value;
				return true;
			}
			if(otherPhone.value != null && otherPhone.value != '') {
				homePhone.value = otherPhone.value;
				return true;
			}
		}

		
	}
	return true;
}

function validateThankYou(form) {
	with(form) {
		if(!validateEducationLevel(educationLevel)) {
			return false;
		}
		if(custom2.options[custom2.selectedIndex].value == '') {
			alert('Please choose an area of interest');
			return false;
		}
		if(!validateDropDown(programId)) {
			alert('Please choose a program');
			return false;
		}
		if(programId.options[programId.selectedIndex].value == '-1') {
			alert('Please choose a program');
			return false;
		}
		if(!validateProgramThankYou(form)) {
			return false;
		}
		if(!validateRadioButtonCondition(citizenship)) {
			alert('Please select if you are a US citizen');
			return false;
		}
		var programValue = programValues[programId.options[programId.selectedIndex].value];
		if(programValue == 'BSN' || programValue == 'MSN' || programValue == 'SNC'
			|| programValue == 'MSN/ED' || programValue == 'MSN/MHA' || programValue == 'MSN/MBA/HC'
			|| programValue == 'MSN/NPFT' || programValue == 'MSN/FNP' || programValue == 'LPN/BSN'
			|| programValue == 'LVN/BSN') {
			if(!validateRegisteredNurse(custom8)) {
				return false;
			}
		}
		
		if(!validateCheckBox(canContact)){
			alert('Please check the box acknowledging that you understand you will be contacted by a University of Phoenix representative.');
			return false;
		}
	}
	return true;
}

function validateStep1(form) {
	if(!validateUopolSchoolFormCO(form)){
		return false;
	}
	with(form) {
		if(!validateEducationLevel(educationLevel)) {
			return false;
		}
		if(!validateRadioButtonCondition(custom1)) {
			alert('Please specify whether you are interested in online or campus-based learning');
			return false;
		}
	}
	return true;
}

function populateProgramsForAOI() {
	// 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 Program and Learning Format ---------');
	aoiProgramNames.push(programsArray);
	
	programsArray = new Array();
	programsArray.push('-1');
	aoiProgramIds.push(programsArray);
	
	aoiNames.push('Show All');
	
	programsArray = new Array();
	programsArray.push('--------- Select Program and Learning Format ---------');
	aoiProgramNames.push(programsArray);
	
	programsArray = new Array();
	programsArray.push('-1');
	aoiProgramIds.push(programsArray);
	
	// Add all AOIs
	for(var i = 0; i < programNames.length; i++) {
		var aoi = programAOI[i];
		if(aoi != '-1' && aoi != '') {
			var aoiIndex = getIndexOfElementInArray(aoi, aoiNames);
			
			if(aoiIndex == -1) {
				aoiNames.push(aoi);
				programsArray = new Array();
				programsArray.push('--------- Select Program and Learning Format ---------');
				aoiProgramNames.push(programsArray);
				
				programsArray = new Array();
				programsArray.push('-1');
				aoiProgramIds.push(programsArray);
			}
		}
	}
	
	for(var i = 0; i < programNames.length; i++) {
		var aoi = programAOI[i];
		
		if(aoi == '-1') {
			// It means that type of program has changed and this need to be added to all the aois.
			for(var j = 1; j < aoiNames.length; j++) {
				aoiProgramNames[j].push(programNames[i]);
				aoiProgramIds[j].push('-1');
			}
			
		}
		else {
			if(aoi != '') {
				var aoiIndex = getIndexOfElementInArray(aoi, aoiNames);
				var programs = aoiProgramNames[aoiIndex];
				programs.push(programNames[i]);
				
				programs = aoiProgramIds[aoiIndex];
				programs.push(programIds[i]);
			}
			aoiIndex = 1;
			var programs = aoiProgramNames[aoiIndex];
			programs.push(programNames[i]);
			
			programs = aoiProgramIds[aoiIndex];
			programs.push(programIds[i]);
		}
	}
	
	var custom1Val = document.getElementById('custom1').value;
	if(custom1Val == 'Campus') {
		for(var i = 1; i < aoiNames.length; i++) {
			var pgNames = aoiProgramNames[i];
			var pgIds = aoiProgramIds[i];
			
			for(var j = 1; j < programIds.length - 2; j++) {
				if(pgIds[j] == '-1' && pgIds[j+1] == '-1' && pgNames[j].toLowerCase().indexOf("campus programs") >= 0) {
					pgIds.splice(j, 1);
					pgNames.splice(j,1);
				}
			}
		}
	}

	removeAllOptions(document.getElementById('custom2'));
	addOption(document.getElementById('custom2'), aoiNames[0], '');
	for(var i = 1; i < aoiNames.length; i++) {
		addOption(document.getElementById('custom2'), aoiNames[i], aoiNames[i]);
	}
	onAOIChange(document.getElementById('custom2'));
}

function onAOIChange(aoiHandler) {
	hideLevel('noCampus');
	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]);
	}
	
	showHideNoCampusProgramText(aoiHandler);
}

function validateEducationLevel(educationLevel) {
	if(!validateDropDown(educationLevel)) {
		alert('Please select your education Level');
		return false;
	}
	if(educationLevel.options[educationLevel.selectedIndex].value == 'N' ) {
		alert('Your Student Information Form indicates that you do not meet the admissions requirements');
		return false;
	}
	return true;
}


function validateProgram(form) {
	with(form) {
		var progVal = programValues[programId.options[programId.selectedIndex].value];
		var stateVal = state.options[state.selectedIndex].value;
		
		if (progVal.indexOf('MAED') == 0 && (stateVal == 'AR' || stateVal == 'KY' || stateVal == 'MN')) {
			alert('At this time, this program is not offered in your area. Please select a different Program of Interest. Thank you');
			return (false);
        }
        
        if (zipVal.length > 5 && (progVal == 'MAED/SPE' || progVal == 'MAED/TED-E' || progVal == 'MAED/TED-S')) {
			alert('Sorry, we can no longer accept Canadian students for this program');
			return (false);
  		}
		
		if (zipVal.length == 5 && progVal == 'MM-I') {
			alert('Sorry, we can no longer accept US students for this program');
			return (false);
  		}
  		
  		if ((eduVal == '5' || eduVal == '6' || eduVal == '7')
				&& (progVal.indexOf('M') == 0 || progVal.indexOf('D') == 0 || progVal.indexOf('E') == 0)) {
			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);
  		}
		
		if ((eduVal == '8' || eduVal == '9') && (progVal.indexOf('D') == 0 || progVal.indexOf('E') == 0)) {
			alert('Based on your current education level you do not qualify for the program/degree you have selected. Please select an Associate, Bachelor, or Masters program from the Program list.');
			return (false);
		}
  
  		if (progVal == 'NHCE' && (eduVal != '8' && eduVal != '9' && eduVal != '10')) {
			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);
		}
    }
    return true;
}

function validateProgramThankYou(form) {
	with(form) {
		var progVal = programValues[programId.options[programId.selectedIndex].value];
		var eduVal = educationLevel.options[educationLevel.selectedIndex].value;
		if (progVal.indexOf('MAED') == 0 && (stateVal == 'AR' || stateVal == 'KY' || stateVal == 'MN')) {
			alert('At this time, this program is not offered in your area. Please select a different Program of Interest. Thank you');
			return (false);
        }
        
        if (zipVal.length > 5 && (progVal == 'MAED/SPE' || progVal == 'MAED/TED-E' || progVal == 'MAED/TED-S')) {
			alert('Sorry, we can no longer accept Canadian students for this program');
			return (false);
  		}
		
		if (zipVal.length == 5 && progVal == 'MM-I') {
			alert('Sorry, we can no longer accept US students for this program');
			return (false);
  		}
  		
  		if ((eduVal == '5' || eduVal == '6' || eduVal == '7')
				&& (progVal.indexOf('M') == 0 || progVal.indexOf('D') == 0 || progVal.indexOf('E') == 0)) {
			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);
  		}
		
		if ((eduVal == '8' || eduVal == '9') && (progVal.indexOf('D') == 0 || progVal.indexOf('E') == 0)) {
			alert('Based on your current education level you do not qualify for the program/degree you have selected. Please select an Associate, Bachelor, or Masters program from the Program list.');
			return (false);
		}
  
  		if (progVal == 'NHCE' && (eduVal != '8' && eduVal != '9' && eduVal != '10')) {
			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);
		}
    }
    return true;
}

function validateRegisteredNurse(regsiteredNurse) {
	if(!validateCheckBox(regsiteredNurse)) {
		alert('Thank you for your interest. This program requires an RN license. Please select another program.');
		return false;
	}
	return true;
}

function populateProgramSpecifcData(programId) {
	hideLevel('registerednurse1');
	var programValue = programValues[programId.options[programId.selectedIndex].value];
	if(programValue == 'BSN' || programValue == 'MSN' || programValue == 'SNC'
		|| programValue == 'MSN/ED' || programValue == 'MSN/MHA' || programValue == 'MSN/MBA/HC'
		|| programValue == 'MSN/NPFT' || programValue == 'MSN/FNP' || programValue == 'LPN/BSN'
		|| programValue == 'LVN/BSN') {
		showLevel('registerednurse1');
	}
}
function onProgramChange(prgId) {
	populateAOIForProgram(prgId);
	populateProgramSpecifcData(document.getElementById('programId'));
}

function populateAOIForProgram(prgId) {
	for(var i = aoiProgramIds.length - 1; i >= 0; i--) {
		var programs = aoiProgramIds[i];
		
		for(var j = 0; j < programs.length; j++) {
			if(programs[j] == prgId) {
				document.getElementById('custom2').selectedIndex = i;
				onAOIChange(document.getElementById('custom2'));
				document.getElementById('programId').selectedIndex = j;
				return;
			}
		}
	}
}

function showHideNoCampusProgramText(aoiHandler) {
	if(aoiHandler.selectedIndex == 0) {
		return;
	}
	var programHandler = document.getElementById('programId');
	var custom1Val = document.getElementById('custom1').value;
	var showMessage = true;
	if(custom1Val == 'Campus') {
		for(var i = 1; i < programHandler.options.length; i++) {
			if(programHandler.options[i].value == '-1' && programHandler.options[i].text.toLowerCase().indexOf("campus programs") >= 0) {
				showMessage = false;
			}
		}
	}
	else {
		showMessage = false;
	}
	if(showMessage) {
		showLevel('noCampus');
	}
}

function populateCampusPrograms()
{
	document.getElementById('custom1').value = 'Campus';
	var zip = document.getElementById('zipCode').value;
	var schoolId = document.getElementById('schoolId').value;
	var eduLevel = document.getElementById('educationLevel').value;
	var url = '/gotoLink.htm?jsp=/colleges/uopol/getPrograms.jsp&zipCode='+zip+'&campus=Campus'+'&schoolId='+schoolId+'&educationLevel='+eduLevel;
	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 populateCampusProgramsThankYou(eduLevel)
{
	document.getElementById('custom1').value = 'Campus';
	var zip = document.getElementById('zipCode').value;
	var schoolId = document.getElementById('schoolId').value;
	var url = '/gotoLink.htm?jsp=/colleges/uopol/getPrograms.jsp&zipCode='+zip+'&campus=Campus'+'&schoolId='+schoolId+'&educationLevel='+eduLevel;
	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 populateOnlinePrograms()
{
	
	var zip = document.getElementById('zipCode').value;
	var schoolId = document.getElementById('schoolId').value;
	var eduLevel = document.getElementById('educationLevel').value;
	document.getElementById('custom1').value = 'Online';
	var url = '/gotoLink.htm?jsp=/colleges/uopol/getPrograms.jsp&zipCode='+zip+'&campus=Online'+'&schoolId='+schoolId+'&educationLevel='+eduLevel;
	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);
	programAOI.splice(0,programAOI.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');
	}
	populateProgramsForAOI();
    if(selectedProgramId != -1 && selectedProgramId != 0) {
    	onProgramChange(selectedProgramId);
	}
}
function checkEducationLevel(eduValue){
	if(eduValue == '1'){
		alert("UOP currently does not accept students with this education level.");
		document.getElementById('educationLevel').options[0].selected = true;
		return false;
	}
	return true;
}

function checkEducationLevelThankYou(eduValue){
	if(eduValue == 'N'){
		alert("UOP currently does not accept students with this education level.");
		document.getElementById('educationLevel').options[0].selected = true;
		return false;
	}else{
		populateCampusProgramsThankYou(eduValue);
	}
	return true;
}
