function checkDob(date) {

    var currentDate = new Date();
    var currentYear = currentDate.getYear();
    var currentMonth = currentDate.getMonth();
    var currentDay = currentDate.getDate();

    if(currentYear<1000) currentYear+=1900;

    var yr = date.substring(6);
    var mon = date.substring(0,2);
    var day = date.substring(3,5);

    if((currentYear - yr) ==17){
        if((currentMonth - mon) == 0){
            if((currentDay - day) >= 0){
                return true; 
            }else if((currentDay - day) < 0){
                return false; 
            }
        }else if ((currentMonth - mon) > 0){
            return true; 
        } else if ((currentMonth - mon) < 0){
            return false; 
        }
    } else if ((currentYear - yr) < 17){
        return false; 
    } else if ((currentYear - yr) > 17){
        return true;
    }
}

function checkDob20(date) {

    var currentDate = new Date();
    var currentYear = currentDate.getYear();
    var currentMonth = currentDate.getMonth();
    var currentDay = currentDate.getDate();

    if(currentYear<1000) currentYear+=1900;

    var yr = date.substring(6);
    var mon = date.substring(0,2);
    var day = date.substring(3,5);

    if((currentYear - yr) ==20){
        if((currentMonth - mon) == 0){
            if((currentDay - day) >= 0){
                return true; 
            }else if((currentDay - day) < 0){
                return false; 
            }
        }else if ((currentMonth - mon) > 0){
            return true; 
        } else if ((currentMonth - mon) < 0){
            return false; 
        }
    } else if ((currentYear - yr) < 20){
        return false; 
    } else if ((currentYear - yr) > 20){
        return true;
    }
}

function utiCheckForm(form) {
	if (form.custom2 && form.custom2.value == "") {
       alert("Please enter your date of birth.");
       return (false);
    }
	var dateSyntax = /^\d\d\/\d\d\/\d\d\d\d$/;
	if (!dateSyntax.test(form.custom2.value))
	{
		alert("Please enter Date of Birth in mm/dd/yyyy format.")
		form.custom2.focus();
		return (false);
	}
    if (!checkDob(form.custom2.value)) {
        alert("Applicant must be 17 years or older.");
        return (false);
    }
	if(form.military && form.military.value!="" && form.military.value!="---") {
		if (form.militaryMonth && form.militaryMonth.value == "") {
          alert("Please enter your military month.");
          return (false);
        }
        if (form.militaryYear && form.militaryYear.value == "") {
          alert("Please enter your military year.");
          return (false);
        }
        if (form.militaryInstallation && (form.militaryInstallation.value == "" || form.militaryInstallation.value == "N/A")
			&& form.militaryInstallationInput && form.militaryInstallationInput.value == "") {
          alert("Please enter your military installation.");
          return (false);
        }	
		if (form.militaryMonth) {
			form.custom5.value = form.militaryMonth.value + "|" + form.militaryYear.value + "|";
			if(form.militaryInstallation.value != "" && form.militaryInstallation.value != "N/A") 
				form.custom5.value = form.custom5.value + "YES|" + form.militaryInstallation.value;
			else 
				form.custom5.value = form.custom5.value + "NO|" + form.militaryInstallationInput.value;
		}
	}
    if (form.custom4 && form.custom4.value == "") {
        alert("Please indicate how you hear about us.");
        return (false);
    }
	if (!checkDob20(form.custom2.value)) {
		if (form.highSchoolMonth && form.highSchoolMonth.value == '') {
		    alert("Please specify Month of High School Graduation.");
			return (false);
		}
		if (form.highSchoolYear && form.highSchoolYear.value == '') {
		    alert("Please specify Year of High School Graduation.");
			return (false);
		}
		if (form.highSchoolState && form.highSchoolState.value == '') {
		    alert("Please specify State of the last High School you attended.");
			return (false);
		}
		if (form.highSchool && form.highSchool.value == '') {
		    alert("Please specify High School Name.");
			return (false);
		}
		if (form.highSchool && form.highSchool.value == 'other' && form.otherHighSchool && form.otherHighSchool.value == '') {
		    alert("Please specify High School Name under 'If High School is not listed, enter here'.");
			return (false);
		}
		form.custom3.value = form.highSchoolMonth.value + "|" + form.highSchoolYear.value + "|" + form.highSchoolState.value;
		if(form.highSchool && form.highSchool.value == 'other')
			form.custom6.value = "other|"+form.otherHighSchool.value;
		else
			form.custom6.value = form.highSchool.value;
	}
	else
		form.custom3.value = "";
    return true;
}

function utiProgramChange(form)
{
}

function utiMilitaryChange(form)
{
	if (form.military && form.military.value!="" && form.military.value!="---") {
		document.getElementById("militaryDiv1").style.display = "block";
		document.getElementById("militaryDiv2").style.display = "block";
		document.getElementById("militaryDiv3").style.display = "block";
		document.getElementById("militaryDiv4").style.display = "block";
    }
    else {
		document.getElementById("militaryDiv1").style.display = "none";
		document.getElementById("militaryDiv2").style.display = "none";
		document.getElementById("militaryDiv3").style.display = "none";
		document.getElementById("militaryDiv4").style.display = "none";
    }
}

function popHighSchoolSection(form)
{
	var dateSyntax = /^\d\d\/\d\d\/\d\d\d\d$/;
	if (dateSyntax.test(form.custom2.value))
	{
	    if (!checkDob(form.custom2.value)) {
		    alert("Applicant must be 17 years or older.");
			form.custom2.focus();
			return;
		}
		if (!checkDob20(form.custom2.value)) {
			document.getElementById("highSchoolDiv1").style.display = "block";
			document.getElementById("highSchoolDiv2").style.display = "block";
			document.getElementById("highSchoolDiv3").style.display = "block";
			document.getElementById("highSchoolDiv4").style.display = "block";
			document.getElementById("highSchoolDiv5").style.display = "block";
		}
		else {
			document.getElementById("highSchoolDiv1").style.display = "none";
			document.getElementById("highSchoolDiv2").style.display = "none";
			document.getElementById("highSchoolDiv3").style.display = "none";
			document.getElementById("highSchoolDiv4").style.display = "none";
			document.getElementById("highSchoolDiv5").style.display = "none";
		}
    }
}

function handleHttpResponse2(form) {
   
   if(http.readyState == 4){
      var respStr = http.responseText;
	  var arr = Array();
	  arr = respStr.split("\n");
	  
	  if(respStr){
	 	updateHighSchools(arr,form);
	  }
   }
}

function updateHighSchools(a, form) {
	
	if(a.length != 0) {
		for(var i=form.highSchool.options.length-1;i>=0;i--) {
			form.highSchool.remove(i);
		}	
		
		for(var i=0;i<a.length;i++){
			if(a[i]){
				var arr = Array();
				arr = a[i].split(":");
    			var opt = document.createElement("OPTION");
				opt.value = arr[0];
				opt.text = arr[1];
				form.highSchool.options.add(opt);
			}
		}
	}
}

function loadHighSchools(form) {
  if (form.highSchoolState && form.highSchoolState.value == "") {
      alert("Please select high school state first.");
      return (false);
  }
  var url = '/common/uti/getHighSchools.jsp?highSchoolState=' + form.highSchoolState.value;

  url += "&sid="+Math.random();
  http = getHttpRequestObj();
  http.onreadystatechange = function() {
	  handleHttpResponse2(form);
  }
  http.open('GET', url, true);
  http.send(null);
}
