function nextQ(field) {
    if(field == window.document.form1.EDUCATION_01) {
        validate_EDUCATION_01(window.document.form1);
        initWALPROGRAM_01();
    } else if(field == window.document.form1.WALPROGRAM_01) {
        initDynamicQuestions();
    }
    return;
}

function delayer() {
    submitForm();
}

function countryRedirect() {
    hide("qDiv_QUESTIONS");
    hide("qDiv_EDUCATION_01_REDIRECT");
    show("qDiv_COUNTRY_01_REDIRECT");
    setTimeout('delayer()', 9000);
}

function educationRedirect() {
    hide("qDiv_QUESTIONS");
    hide("qDiv_COUNTRY_01_REDIRECT");
    show("qDiv_EDUCATION_01_REDIRECT");
    setTimeout('delayer()', 9000);
} 
 
function validate_COUNTRY_01(form) {
    var value = getFieldValueLB(form.COUNTRY_01);
    if(!validateIsRequired(value)) {
        alert("Please select a country.");
        form.COUNTRY_01.focus();
        return false;
    }

    var csOnValue = getFieldValueTF(form.cson);
    if(csOnValue != null && csOnValue.toLowerCase() == "true" && value == "International") {
        countryRedirect();    
    }

    return true;
}

function validate_EDUCATION_01(form) {
    if(!validate_COUNTRY_01(form)) {
        return false;
    }
    var value = getFieldValueLB(form.EDUCATION_01);
    if(!validateIsRequired(value)) {
        alert("Please select the highest level of education you have achieved.");
        form.EDUCATION_01.focus();
        return false;
    }

    /*var csOnValue = getFieldValueTF(form.cson)
    if(csOnValue != null && csOnValue.toLowerCase() == "true") {
        if(value == "17000005" || value == "17000006" || value == "17000007")
            educationRedirect();    
    }*/

    return true;
}

function initDynamicQuestions() {
    initTEACHINGCERT_01();
    initLICENSEDNURSE_01();
    initAGE_24();
    initDEGREEIN_01();
    initCOMP_DEGREE_01();
    
    return true;
}
function getSelectedOption(field){
   var optField = null;
   if (field && field.options) {
     optField = field.options[field.selectedIndex];
   }
   
   return optField;
}
function ask_TEACHINGCERT_01(form) {
     var optField = getSelectedOption(form.WALPROGRAM_01);
     if (optField && optField.className && optField.className.indexOf("edu") > -1) {
       return true;
     }

     return false;
}

function ask_LICENSEDNURSE_01(form) {
     var optField = getSelectedOption(form.WALPROGRAM_01);
     if (optField && optField.className && optField.className.indexOf("nurs") > -1) {
       return true;
     }

     return false;
}

function ask_AGE_24(form) {
     var optField = getSelectedOption(form.WALPROGRAM_01);
     if (optField && optField.className && optField.className.indexOf("age24") > -1) {
       return true;
     }

     return false;
}
function ask_COMP_DEGREE_01(form) {
     var optField = getSelectedOption(form.WALPROGRAM_01);
     if (optField && optField.className && optField.className.indexOf("comp") > -1) {
       return true;
     }

     return false;
}
function ask_MATH_COURSE_01(form) {
     var value = getFieldValueRB(form.COMP_DEGREE_01);
     if (value == 'No') {
       return true;
     }

     return false;
}


function ask_DEGREEIN_01(form) {
    return false;
}

function validate_TEACHINGCERT_01(form) {
    if(ask_TEACHINGCERT_01(form)) {
        var value = getFieldValueRB(form.TEACHINGCERT_01)
        if(!validateIsRequired(value)) {
            alert("Please indicate do you have a current teaching certificate.");
            return false;
        }
    }
    return true;
}

function validate_LICENSEDNURSE_01(form) {
    if(ask_LICENSEDNURSE_01(form)) {
        var value = getFieldValueRB(form.LICENSEDNURSE_01)
        if(!validateIsRequired(value)) {
            alert("Please indicate do you have a valid RN license");
            return false;
        }
    }
    return true;
}
function validate_AGE_24(form) {
    if(ask_AGE_24(form)) {
        var value = getFieldValueRB(form.AGE_24)
        if(!validateIsRequired(value)) {
            alert("Please indicate that you are at least 24 years old or not.");
            return false;
        }
    }
    return true;
}

function validate_DEGREEIN_01(form) {
    if(ask_DEGREEIN_01(form)) {
        var value = getFieldValueTF(form.DEGREEIN_01)
        if(!validateIsRequired(value)) {
            alert("Please indicate what was your undergraduate degree in.");
            form.DEGREEIN_01.focus();
            return false;
        }
    }
    return true;
}
function validate_COMP_DEGREE_01(form) {
    if(ask_COMP_DEGREE_01(form)) {
        var value = getFieldValueRB(form.COMP_DEGREE_01)
        if(!validateIsRequired(value)) {
            alert("Please indicate that you have an undergraduate degree in Electrical or Computer Engineering or Computer Science or not.");
            return false;
        }
    }
    return true;
}

function validate_MATH_COURSE_01(form) {
    if(ask_MATH_COURSE_01(form)) {
        var value = getFieldValueRB(form.MATH_COURSE_01)
        if(!validateIsRequired(value)) {
            alert("Please indicate that you have successfully completed one year of college-level calculus and a college course in differential equations and linear algebra or not.");
            return false;
        }
    }
    return true;
}


function selectValue(field, value) {
    for (var i = 0; i < field.options.length; i++) {
        if (field.options[i].value == value) {
            field.options[i].selected = true;
            return;
        }
    }
    return;
}

var eduNames = ["ged", "hs", "diploma", "college", "as", "bs", "ms", "phd", "abovePhd"];
var aosNames = ["art", "bus", "cj", "nurs", "edu", "hhs", "tech", "misc"];

var eduLevels = new Array();
for (i=0;i<eduNames.length;++i) {
	eduLevels[eduNames[i]] = i;
}

var aosLevels = new Array();
for (j=0;j<aosNames.length;++j) {
  aosLevels[aosNames[j]] = j;
}

function disableFilteringPrograms(eduLevel, aos, selectField) {
	if (selectField && selectField.options) {
	  if (selectField.children) {//there is no selectField.children for example in firefox
	    if (!selectField.allChilds) { //in allChilds we save beginning values
	        var allSetChilds =  selectField.children;
	        var allCloneChilds = new Array();
	        for (i=0;i<allSetChilds.length;++i) {
	          allCloneChilds[i] = allSetChilds[i].cloneNode(true);
	        }
	    	selectField.allChilds = allCloneChilds;
	    }
	    //remove all children from select
	    var childNodes = selectField.children;
	    for (var k=childNodes.length-1;k>=0;--k) {
	    	selectField.removeChild(childNodes[childNodes.length-1]);
	    }
	    //add saving beginning children
	    var allChs = selectField.allChilds;
	    for(k=0;k<allChs.length;++k) {
	      selectField.appendChild(allChs[k]);
	    }
	   }
	    
		var optArray = selectField.options;
		
	    var higherLevels = eduNames.slice(parseInt(eduLevels[eduLevel]) + 1);//find levels names higher than eduLevel
		
		for (var i=0;i<optArray.length;++i) {
		  if (optArray[i].className) {
		    var isDisable = "block";
		    for (var j=0;j<higherLevels.length;++j) {
		    	if (optArray[i].className.indexOf(higherLevels[j]) > -1) {
		    		isDisable = "none";
		    	}
		    }
		    if (aos) {
		      var isContainAos = false; //if there is aos and option has class but doesn't contain such aos then isDisable will be none
		      for(m in aos) {
		        if (optArray[i].className.indexOf(aos[m]) > -1) { 
		          isContainAos = true;
		          break;
		        }
		      } 
		      if (!isContainAos) {
		          isDisable = "none";
		      }
		      
		    }
		    
		    if (optArray[i].parentNode && optArray[i].parentNode.nodeName == "OPTGROUP") {
		    	optArray[i].parentNode.style.display = isDisable;
		    }
		    optArray[i].style.display = isDisable;
		 }
		}
		
		//remove childs that has diplay == "none", because of bug in ie(if option has display='none' it shows it)
		var childs = selectField.children;
		if (childs) {
		  for (var i=0;i<childs.length;++i) {
		    if (childs[i].style.display == "none") {
			  selectField.removeChild(childs[i]);
			  if (i> 0) {
			    i--;
			  }
		    }
		  }
		}
		selectField.selectedIndex = 0;
	}
}

/*
function disableNonAosPrograms(aos, selectField) {
	if (selectField && selectField.options) {
		var optArray = selectField.options;

		for (var i=0;i<optArray.length;++i) {
		  isDisable = "none";
		  if (optArray[i].className && optArray[i].className.indexOf(aos) > -1) { //show option if in class it contains aos
		      isDisable = "block";
		  }
		  if (optArray[i].parentNode && optArray[i].parentNode.nodeName == "OPTGROUP") {
		    optArray[i].parentNode.style.display = isDisable;
		  }
		  //if (!optArray[i] || !optArray[i].style) 
		  //alert(i + " " + optArray[i].name);
		  optArray[i].style.display = isDisable;
	   }
	}
}
*/

function initWALPROGRAM_01() {
    var eduField = window.document.form1.EDUCATION_01;
    var eduValue = getFieldValueLB(eduField);
    if(eduValue == null || eduValue == "") {
        return;
    }
    var optObj = getSelectedOption(eduField);
	if (optObj && optObj.className) {
	    var classNames = optObj.className.split(" ");
	    var eduLevel = null;
	    var aos = new Array();
	    for (j in classNames) {
	      if (aosLevels[classNames[j]] != null && aosLevels[classNames[j]] != 'undefined') {
	          aos[aos.length] = classNames[j];
	          //disableNonAosPrograms(classNames[j], window.document.form1.WALPROGRAM_01);
	      }
	      else if (eduLevels[classNames[j]] != null && eduLevels[classNames[j]] != 'undefined') {
	        eduLevel = classNames[j];
	        //disableHigherEduLevelPrograms(classNames[j], window.document.form1.WALPROGRAM_01);
	      }
	    }
	    disableFilteringPrograms(eduLevel, aos, window.document.form1.WALPROGRAM_01);
    	
    }
   
    return;
}


function initTEACHINGCERT_01() {
    var eduField = window.document.form1.EDUCATION_01;
    var eduValue = getFieldValueLB(eduField);
    if(eduValue == null || eduValue == "") {
        hide("qDiv_TEACHINGCERT_01");
        return;
    }

    if(ask_TEACHINGCERT_01(window.document.form1)) {
        show("qDiv_TEACHINGCERT_01");
    } else {
        hide("qDiv_TEACHINGCERT_01");
    }
}

function initLICENSEDNURSE_01() {
    var eduField = window.document.form1.EDUCATION_01;
    var eduValue = getFieldValueLB(eduField);
    if(eduValue == null || eduValue == "") {
        hide("qDiv_LICENSEDNURSE_01");
        return;
    }

    if(ask_LICENSEDNURSE_01(window.document.form1)) {
        show("qDiv_LICENSEDNURSE_01");
    } else {
        hide("qDiv_LICENSEDNURSE_01");
    }
}
function initAGE_24() {
    var eduField = window.document.form1.EDUCATION_01;
    var eduValue = getFieldValueLB(eduField);
    if(eduValue == null || eduValue == "") {
        hide("qDiv_AGE_24");
        return;
    }

    if(ask_AGE_24(window.document.form1)) {
        show("qDiv_AGE_24");
    } else {
        hide("qDiv_AGE_24");
    }
}


function initDEGREEIN_01() {
    var eduField = window.document.form1.EDUCATION_01;
    var eduValue = getFieldValueLB(eduField);
    if(eduValue == null || eduValue == "") {
        hide("qDiv_DEGREEIN_01");
        return;
    }

    if(ask_DEGREEIN_01(window.document.form1)) {
        show("qDiv_DEGREEIN_01");
    } else {
        hide("qDiv_DEGREEIN_01");
    }
}

function initCOMP_DEGREE_01() {
    var eduField = window.document.form1.EDUCATION_01;
    var eduValue = getFieldValueLB(eduField);
    if(eduValue == null || eduValue == "") {
        hide("qDiv_COMP_DEGREE_01");
        return;
    }

    if(ask_COMP_DEGREE_01(window.document.form1)) {
        show("qDiv_COMP_DEGREE_01");
    } else {
        hide("qDiv_COMP_DEGREE_01");
    }
}

var monthString = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");
var monthString2 = new Array ("01","02","03","04","05","06","07","08","09","10","11","12");

function initStartDate() {
    var field = window.document.form1.START_DATE_01;
    if(field != undefined && field != null) {
        var value = getFieldValueLB(field);

        field.enabled = true;
        field.length=0;
        field.options[field.length] = new Option("select one", "");    

        var today = new Date();
        var month = today.getMonth();
        var year = today.getYear();
        var displayMonth;
        var displayYear = (year < 1000) ? year + 1900 : year;
        for(i = 0; i < 24; i++) {
            if(i + month <= 11) {
                displayMonth = i + month;
            } else if(i + month <= 23) {
                displayMonth = i + month - 12;
                if(displayMonth == 0) {
                    displayYear += 1;
                }
            } else {
                displayMonth = i + month - 24;
                if(displayMonth == 0) {
                    displayYear += 1;
                }
            }

            field.options[field.length] = new Option(monthString[displayMonth] + " " + displayYear, monthString2[displayMonth] + "/01/" + displayYear);    
        }
        selectValue(field,value);
    }
}
