var allCampusNames = new Array();
var allCampusIds = new Array();
var campusProgramNames = new Array();
var campusProgramValues = new Array();
var campusProgramIds = new Array();
var programDegree = '';

function validateStep2(form){
  with(form) {
    if(schoolShortName == 'argosy' && argosyOnlineCampus == 'false') {
      if(!validateDropDown(custom2)) {
	alert("Please select a location");
	return false;
      }
    }
    
    if(!validateSuoSchoolFormCO(form)){
    	return false;
  	} 
  	
    if(!validateProgramSpecificInputs(form)) {
      return false;
    }
    if(!validateProgramSpecificValidations (form)) {
      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;
		}
		
		if(schoolShortName == 'argosy' && argosyOnlineCampus == 'false') {
       
  	  if((custom5.value=='' || custom5.value==null) || (custom6.value=='' || custom6.value==null)){
  		alert('please specify when you finished your degree.');
  	    return false;
  	}  
  }
    } 
    if(schoolShortName == 'ai'){
      if(!validateCheckBox(canContact)){
	alert('Please check the box acknowledging that you understand you will be receiving a phone call from an Admissions Representative of The Art Institute - ' + aiCampusName + '.');
	return false;
      }     
    } else if(schoolShortName == 'aio'){
      if(!validateCheckBox(canContact)){
	alert('Please check the box acknowledging that you understand you will be receiving a phone call from an Admissions Representative of The Art Institute of Pittsburgh - Online Division.');
	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;
		}
  return true;
}

function validateStep1(form) {
  if(!validateSuoSchoolFormCO(form)){
    return false;
  }
  with(form){
    if(schoolShortName == 'argosy') {
      if(!validateDropDown(educationLevel)){
	alert("Please fill your Education Level");
	return false;
      }
    }
    if(!validateDropDown(highSchoolGradYear)){
      if(schoolShortName == 'argosy' && argosyOnlineCampus == 'false') {
	alert('Please select date of highest degree completed');
	return false;
      }
      else if(schoolShortName == 'argosy' && argosyOnlineCampus == 'true') {
	alert('Please select your high school graduation year');
	return false;
      }
      else {
	alert('Please select your high school graduation year');
	return false;
      }
    }
    if (schoolShortName == 'aio' && document.getElementById('highSchoolGradYear').value > (currYear - 4)) {
      alert("The Art Institute Online only accepts applications for people who have completed their high school education on or before : " + (currYear - 4));
      return false;
    }
  }
  return true;
}

function validateProgramSpecificValidations(form) {
  with(form) {
    var prgValue = programIdValueMapping[programId.options[programId.selectedIndex].value];
	programDegree = programIdDegreeMapping [programId.options[programId.selectedIndex].value]; 
    var highSchoolValue = highSchoolGradYear;
    if (isNaN (parseInt(highSchoolValue))){
      highSchoolValue = parseInt(highSchoolGradYear.options[highSchoolGradYear.selectedIndex].value);
    }
    
  
 	if(schoolShortName == 'aio') 
 	{
    	if(prgValue != null && (prgValue == 'FRM' || prgValue == 'ADV') && highSchoolValue > (currYear - 5)) 
    	{
      		alert("To be eligible for the Advertising or Fashion & Retail Management program, you must have completed your high school education prior to " + (currYear - 5));
      		return false;
	  	}
	    else if(highSchoolValue > (currYear - 4)) 
	    {
	      alert("The Art Institute Online only accepts applications for people who have completed their high school education prior to " + (currYear - 4));
	      return false;
	    }
  	} else
  	if (schoolShortName == 'argosy' && argosyOnlineCampus == 'true' && programDegree != '' && programDegree != null) 
  	{
	    if ( programDegree == 4 && highSchoolValue > (currYear - 2)) 
	    {
		    alert("The Argosy University only accepts applications for Bachelors Programs from those who have completed their High School Education in or before : " + (currYear - 2));
	      	return false;
	    }else
	    if ( programDegree == 5 && highSchoolValue > (currYear - 4)) 
	    {
	      	alert("The Argosy University only accepts applications for Masters Programs from those who have completed their High School Education in or before : " + (currYear - 4));
	      	return false;
	    }else
	    if ( (programDegree == 6 || prgValue.indexOf("EdS") == 0) && highSchoolValue > (currYear - 6)) 
	    {
	      	alert("The Argosy University only accepts applications for Doctorate Programs from those who have completed their High School Education in or before : " + (currYear - 6));
	      	return false;
	    }
  	}
  }
  return true;	
}

function validateThankYou(form) {
  with(form){
  
    if(schoolShortName == 'argosy') {
      if(!validateDropDown(educationLevel)){
	alert("Please fill your Education Level");
	return false;
      }
    }
    
    if(schoolShortName == 'argosy' && argosyOnlineCampus == 'false') {
    if(document.getElementById('custom2') != null) {
	      if(!validateDropDown(custom2)) {
			alert("Please select a location");
			return false;
	      }
	    }
	 if((custom5.value=='' || custom5.value==null) || (custom6.value=='' || custom6.value==null)){
  		alert('please specify when you finished your degree.');
  	    return false;
  	}         
    }
    
    if(!validateDropDown(highSchoolGradYear)){
      if(schoolShortName == 'argosy' && argosyOnlineCampus == 'false') {
	alert('Please select your high school graduation year');
	return false;
      }
      else if(schoolShortName == 'argosy' && argosyOnlineCampus == 'true') {
	alert('Please select your high school graduation year');
	return false;
      }
      else {
	alert('Please select your high school graduation year');
	return false;
      }
    }
    if(programId.options[programId.selectedIndex].value == '-1') {
      alert('Please choose a program');
      return false;
    }
    if(!validateDropDown(programId)){
      alert("Please select a degree program you are interested in.");
      return false;
    } 
    
    var programValue = programIdValueMapping[programId.options[programId.selectedIndex].value];

    if(!validateProgramSpecificInputs(form)) {
      return false;
    }
    if(!validateProgramSpecificValidations(form)) {
      return false;
    }
    if(schoolShortName == 'ai'){
      if(!validateCheckBox(canContact)){
	alert('Please check the box acknowledging that you understand you will be receiving a phone call from an Admissions Representative of The Art Institute - ' + aiCampusName + '.');
	return false;
      }     
    } else if(schoolShortName == 'aio'){
      if(!validateCheckBox(canContact)){
	alert('Please check the box acknowledging that you understand you will be receiving a phone call from an Admissions Representative of The Art Institute of Pittsburgh - Online Division.');
	return false;
      }     
    }
  }
  return true;
}

function onProgramChange(programId) {
  hideLevel('div1');
  hideLevel('div2');
  hideLevel('div3');
  hideLevel('span1');
  
  var programValue = programIdValueMapping[programId.options[programId.selectedIndex].value];
  if(programValue != null && programValue != "") {
    if(programValue == 'NRBA') {
      showLevel('div1');
    }
    else
    if(programValue == 'FRM') {
      showLevel('div2');
    }
    else
    if(programValue == 'PSY-BA' || (programValue.indexOf("BA") != -1 && programValue.indexOf("Psy") != -1)) {
      showLevel('div3');
    }
    else
    if(programValue == 'WSB'){
      showLevel('span1');
    }
  }
  validateProgramSpecificValidations (document.forms[0]);
}

function onProgramChanged(programId) {
  hideLevel('div11');
  hideLevel('div12'); 
  hideLevel('div21');
  hideLevel('div22'); 
  hideLevel('div31');
  hideLevel('div32'); 
  hideLevel('span1');

  var programValue  = programIdValueMapping  [programId.options[programId.selectedIndex].value];

  if(programValue != null && programValue != "") {
    if(programValue == 'NRBA') {
      showLevel('div11');
      showLevel('div12');
    }
    else
    if(programValue == 'FRM') {
      showLevel('div21');
      showLevel('div22');
    }
    else
    if(programValue == 'PSY-BA'|| (programValue.indexOf("BA") != -1 && programValue.indexOf("Psy") != -1)) {
      showLevel('div31');
      showLevel('div32');
    }
    else
    if(programValue == 'WSB'){
      showLevel('span1');
    }
  } 
  validateProgramSpecificValidations (document.forms[0]);
}

function onProgramChangeStep2(programId){
  hideLevel('span1');
  var programValue = programIdValueMapping[programId.options[programId.selectedIndex].value];
  if(programValue != null && programValue != "") {
    if(programValue == 'WSB'){
      showLevel('span1');
    }
  }
}

function validateProgramSpecificInputs(form) {
  with(form) {
    var programValue = programIdValueMapping[programId.options[programId.selectedIndex].value];
    if(programValue != null && programValue != "")  {
      if(programValue == 'NRBA') {
	if(!validateRadioButtonCondition(license)) {
	  alert('Please specify whether you have a valid, unencumbered RN License');
	  return false;
	}
	if(!validateRadioButton(license, 0)) {
	  alert('To apply this program, you must have a valid, unencumbered RN License');
	    return (false);
	}
	
      }
      else if(programValue == 'FRM') {
	if(!validateRadioButtonCondition(age)) {
	  alert('Please specify whether you are age 25 or older');
	  return false;
	}
	if(!validateRadioButton(age, 0)) {
	  alert('To apply this program, you must be age 25 or older');
	    return (false);
	}
      }
      else if(programValue == 'PSY-BA'|| (programValue.indexOf("BA") != -1 && programValue.indexOf("Psy") != -1)) {
	if(!validateRadioButtonCondition(credits)) {
	  alert('Please specify whether you have minimim of 36 credit hours to transfer');
	  return false;
	}
      }
    }
  }
  return true;
}

function populateArgosyCampusLocationPrograms() {
  var argosySchoolId = document.getElementById('schoolId').value;
  
  var url = '/gotoLink.htm?jsp=/colleges/suo/getCampuses.jsp&schoolId=' + argosySchoolId + "&educationLvl=" + educationLvl;
  
  http = getHttpRequestObj();
  http.onreadystatechange = handleHttpResponseGetCampuses;
  http.open('GET', url, true);
  http.send(null);
}

function handleHttpResponseGetCampuses() {
  if(http.readyState == 4){
    var responseString = http.responseText;
    var xmlObject;
    // code for IE
    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('location');
    for(var i = 0; i < locationsElements.length; i++) {
      var locationElement = locationsElements[i];
      var campusName = locationElement.getAttribute('name');
      if(campusName == 'Washington') {
	campusName = 'Washington D.C.';
      }
      else if(campusName == 'Honolulu') {
	campusName = 'Hawaii';
      }
      else if(campusName == 'San Bernardino') {
	campusName = 'Inland Empire';
      }
      allCampusNames.push(campusName);
      allCampusIds.push(locationElement.getAttribute('id'));
    }
    
    getProgramsForCampus(0);
  }
}

function getProgramsForCampus (campusNumber) {

	if(document.getElementById('educationLevel') != null) {	
		educationLvl = document.getElementById('educationLevel').value; 	
	}
  var url = '/gotoLink.htm?jsp=/colleges/suo/getPrograms.jsp&campusId=' + allCampusIds[campusNumber] + "&educationLvl=" + educationLvl;
  http = getHttpRequestObj();
  http.onreadystatechange = function() {
		if(http.readyState == 4){
		  var responseString = http.responseText;
		  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');
		  
		  var programNames = new Array();
		  var programValues = new Array();
		  var programIds = new Array();
		  
		  for(var i = 0; i < locationsElements.length; i++) {
		    var locationElement = locationsElements[i];
		    programNames.push(locationElement.getAttribute('name'));
		    programValues.push(locationElement.getAttribute('value'));
		    programIds.push(locationElement.getAttribute('id'));
		  }
		  
		  campusProgramNames.push(programNames);
		  campusProgramValues.push(programValues);
		  campusProgramIds.push(programIds);
		  
		  if(campusNumber == allCampusIds.length - 1) {
		    populateLocations();
		    selectProgramAndLocation();
		    return;
		  }
		  getProgramsForCampus(campusNumber + 1);
		}
	      };
  http.open('GET', url, true);
  http.send(null);
}

function handleHttpResponseGetPrograms(campusNumber, responseString) {
  // code for IE
}

function populateLocations() {
  for(var i = 0; i < allCampusNames.length; i++) {
    addOption(document.getElementById('custom2'), 'Argosy University, ' + allCampusNames[i] + ' Campus', allCampusIds[i]);
  }
}

function selectProgramAndLocation() {
  var selectedProgram = parseInt(selectedProgramId);
  if(selectedProgram > -1) {
    // search for the rite campus and program
    for(var i = 0; i < allCampusNames.length; i++) {
      var programsForCampus = campusProgramIds[i];
      for(var j = 0; j < programsForCampus.length; j++) {
	if(selectedProgramId == programsForCampus[j]) {
	  document.getElementById('custom2').selectedIndex = i + 1;
	  populateProgramsForLocation(i);
	  
	  for(var k = 0; k < document.getElementById('programId').options.length; k++) {
	    if(selectedProgramId == document.getElementById('programId').options[k].value) {
	      document.getElementById('programId').selectedIndex = k;
	    }
	  }
	  return;
	}
      }
    }
  }
}

function populateProgramsForLocation(locationIndex) {
  removeAllOptions(document.getElementById('programId'));
  addOption(document.getElementById('programId'), "Select One", "-1");
  
  if(locationIndex == -1) {
    return;
  }
  var programNames = campusProgramNames[locationIndex];
  var programIds = campusProgramIds[locationIndex];
  var programValues = campusProgramValues[locationIndex];
  
  if(programNames != null)
  {
	  for(var i = 0; i < programNames.length; i++) {
	    addOption(document.getElementById('programId'), programNames[i], programIds[i]);
	  }

	  for(var i = 0; i < programNames.length; i++) {
	    programIdValueMapping[programIds[i]] = programValues[i];
	  }
  }
}

function onLocationChange(location) {
  populateProgramsForLocation(document.getElementById('custom2').selectedIndex - 1);
}

function onLocationChangeThankYou(location) {
  getProgramsForCampusThankYou(document.getElementById('custom2').selectedIndex - 1);
}

function getProgramsForCampusThankYou (campusNumber) {

	try {
		if(document.getElementById('educationLevel')){
			educationLvl = document.getElementById('educationLevel').options[educationLevel.selectedIndex].value;
		}
	}
	catch (ex) {}

  var url = '/gotoLink.htm?jsp=/colleges/suo/getPrograms.jsp&campusId=' + allCampusIds[campusNumber] + "&educationLvl=" + educationLvl;
  http = getHttpRequestObj();
  http.onreadystatechange = function() {
		if(http.readyState == 4){
		  var responseString = http.responseText;
		  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');
		  
		  var programNames = new Array();
		  var programValues = new Array();
		  var programIds = new Array();
		  campusProgramNames.splice(0, campusProgramNames.length);
		  campusProgramValues.splice(0, campusProgramValues.length);
		  campusProgramIds.splice(0, campusProgramIds.length);		  
		  
		  for(var i = 0; i < locationsElements.length; i++) {
		    var locationElement = locationsElements[i];
		    programNames.push(locationElement.getAttribute('name'));
		    programValues.push(locationElement.getAttribute('value'));
		    programIds.push(locationElement.getAttribute('id'));
		  }
		  
		  campusProgramNames[campusNumber] = programNames;
		  campusProgramValues[campusNumber] = programValues;
		  campusProgramIds[campusNumber] = programIds;
		  
			try {
				if(document.getElementById('custom2').selectedIndex){
					populateProgramsForLocation(document.getElementById('custom2').selectedIndex - 1);
				}
			}
			catch (ex) {}		  
		  
		  populateProgramsForLocation(document.getElementById('custom2').selectedIndex - 1);
		  
		  /*if(campusNumber == allCampusIds.length - 1) {
		    populateLocations();
		    selectProgramAndLocation();
		    return;
		  }*/
		  //getProgramsForCampus(campusNumber + 1);
		}
	      };
  http.open('GET', url, true);
  http.send(null);
}

function locationReselect()
{
	if(document.getElementById('custom2') != null) {
		document.getElementById('custom2').selectedIndex = 0;
	}
}

function getCampuses() {
	var argosySchoolId = document.getElementById('schoolId').value;
	xmlHttp =getHttpRequestObj()
    if (xmlHttp==null)
    {
        alert("Browser does not support HTTP Request")
        return
    }
    var url = '/gotoLink.htm?jsp=/colleges/suo/getCampusForSchool.jsp?schoolId=' + argosySchoolId;
    xmlHttp.onreadystatechange = eduLevelChanged
    xmlHttp.open('GET', url, true)
    xmlHttp.send(null)
}

function eduLevelChanged() {
	 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	if(document.getElementById("locationDiv") != null) {
			document.getElementById("locationDiv").innerHTML="";
		    document.getElementById("locationDiv").innerHTML=
			"<select id=\"custom2\" name=\"custom2\" class=\"TribalFusion\" onchange=\"getPrograms()\">" + xmlHttp.responseText + "</select>";
		}
	
	 }
}

function getPrograms() {
	if(document.getElementById('educationLevel') == null) {
		return;
	}
	if(document.getElementById('custom2') == null) {
		return;
	}
	var educationLevel = document.getElementById('educationLevel').value;
	var campus = document.getElementById('custom2').value;
	xmlHttp =getHttpRequestObj()
    if (xmlHttp==null)
    {
        alert("Browser does not support HTTP Request")
        return
    }
    var url = '/gotoLink.htm?jsp=/colleges/suo/getProgramsForCampus.jsp?campusId=' + campus + '&educationLevel=' + educationLevel;
    xmlHttp.onreadystatechange = locationChanged
    xmlHttp.open('GET', url, true)
    xmlHttp.send(null)
}

function locationChanged() {
	 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	if(document.getElementById("programDiv") != null) {
			document.getElementById("programDiv").innerHTML="";
		    document.getElementById("programDiv").innerHTML=
			"<select id=\"programId\" name=\"programId\" class=\"TribalFusion\" onchange=\"onProgramChange(this)\">" + xmlHttp.responseText + "</select>";
		}
	
	 }
}
