function resetDivs(){
     if(arguments.length){
          //reset a specific div
          for(var i = 0; i < arguments.length; i++){
               if(document.getElementById(arguments[i])){
                    document.getElementById(arguments[i]).innerHTML = "";
               }
          }
     }else {
          //just reset all the divs
          if(document.getElementById('resultDiv1')){
               document.getElementById('resultDiv1').innerHTML = "";
          }
          if(document.getElementById('resultDiv2')){
               document.getElementById('resultDiv2').innerHTML = "";
          }
          if(document.getElementById('resultDiv3')){
               document.getElementById('resultDiv3').innerHTML = "";
          }
		  if(document.getElementById('resultDiv4')){
               document.getElementById('resultDiv4').innerHTML = "";
          }
     }
}

function resetForm(){
     document.factbook.reset();
}

function in_year(year) {	
	var years=new Array("20083","20073","20063","20053","20043", "20033", "20023");
	var x;
	var found=0;
	for (x in years) {
		if(years[x] == year) {
			found=1;
		}	
	}
	return found;
}

function select_year(view) {
	var html = '';
	if(view == "1" || view == "2" || view == "3") {
		html += '<select name=\"year\" id=\"year\" onChange=\"resetDivs(\'resultDiv2\', \'resultDiv3\', \'resultDiv4\'); \n';
		html += 'show_options(\'' + view + '\', document.factbook.year.value); return true; \"> \n';
		html += '<option value=\"\" selected=\"selected\">Choose Year</option> \n';
		html += '<option value=\"20083\">2008-2009</option> \n';
		html += '<option value=\"20073\">2007-2008</option> \n';
		html += '<option value=\"20063\">2006-2007</option> \n';
		html += '<option value=\"20053\">2005-2006</option> \n'; 
		html += '<option value=\"20043\">2004-2005</option> \n';
		html += '<option value=\"20033\">2003-2004</option> \n'; 
		html += '<option value=\"20023\">2002-2003</option> \n';
  		html += '</select> \n';
	} else {
		html += '<p>Please select a section from the drop down menu</p>';	
	}
	if(document.getElementById('resultDiv1')){
    		document.getElementById('resultDiv1').innerHTML = html;
	} else {
		document.write('<p>There was an error processing your request. Please <a href="http://www.nyu.edu/ir/factbook/view.tables.html">try again</a>.</p>');
	}
}

function show_options(view, year, deselect) {
	var html = '';
	if(in_year(year) == 1) {
		if ( view == "1" ) {
    		html += '<select id=\"factbookpage\" name=\"factbookpage\" \n';
		html += 'onChange=\"resetDivs(\'resultDiv3\', \'resultDiv4\'); \n';
		html += 'process_fact_submenu(\'' + view + '\', \'' + year + '\', document.factbook.factbookpage.value); return true;\"> \n';
		html += '<option value=\"\" selected=\"selected\">Choose a subsection</option> \n';
		html += '<option value=\"1\">Total University</option> \n';
		html += '<option value=\"2\">Total Undergraduate</option> \n';
		html += '<option value=\"3\">Traditional Baccalaureate</option> \n';
		html += '</select> \n';
    	}
    	else if ( view == "2" ) {
    		html += '<select id=\"demographicspage\" name=\"demographicspage\" \n';
     		html += 'onChange=\"resetDivs(\'resultDiv3\', \'resultDiv4\'); \n';
		html += 'process_demo_submenu(\'' + view + '\', \'' + year + '\', document.factbook.demographicspage.value); return true;\"> \n';
		html += '<option value=\"\" selected=\"selected\">Choose a subsection</option> \n';
     		html += '<option value=\"1\">Enrollment</option> \n';
     		html += '<option value=\"2\">Faculty</option> \n';
     		html += '<option value=\"3\">Degrees Conferred</option> \n';
     		html += '<option value=\"4\">Employees</option> \n';
     		html += '<option value=\"5\">Tuition and Fees</option> \n';
     		html += '</select> \n';
    	}
    	else if ( view == "3" ) {
		var checked = '';
		if(deselect != "1") {
			checked = "checked";
		}
		html += '<br><p><input type=\"checkbox\" name = \"deselect\" value = \"1\" \n';
		html += 'onClick=\"resetDivs(\'resultDiv3\', \'resultDiv4\'); \n';
		html += 'show_options(\'' + view + '\', \'' + year + '\', document.factbook.deselect.value); return true;\"> Click to deselect all checkboxes</p>\n';

		html += '<p>Subsections:</p><div><p> \n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"a\" ' + checked + ' > General Information <br> \n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"b\" ' + checked + ' > Enrollment and Persistence <br> \n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"c\" ' + checked + ' > First-Time, First-Year (Freshman) Admission <br>\n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"d\" ' + checked + ' > Transfer Admission <br> \n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"e\" ' + checked + ' > Academic Offerings and Policies <br> \n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"f\" ' + checked + ' > Student Life <br> \n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"g\" ' + checked + ' > Annual Expenses <br> \n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"h\" ' + checked + ' > Financial Aid <br> \n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"i\" ' + checked + ' > Instructional Faculty and Class Size <br> \n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"j\" ' + checked + ' > Degrees Conferred Associate and Bachelor&#39;s Degrees Conferred <br> \n';
		html += '<input type = \"checkbox\" name = \"cdspage\" value = \"k\" ' + checked + ' > Common Data Set Definitions <br> \n';
		html += '</p></div><input type=\"submit\" value=\"Submit\">\n';
    	} else {
			html += '<p>Please select a section from the drop down menu</p>';	
    	}
	} else {
		html += '<p>Please select a year from the drop down menu</p>';	
	}
	
	if(document.getElementById('resultDiv2')){
     	document.getElementById('resultDiv2').innerHTML = html;
    } else {
		document.write('3 There was an error processing your request. Please <a href="http://www.nyu.edu/ir/factbook/view.tables.html">try again</a>.');
	}
}

function process_demo_submenu(view, year, demographicspage) {
    var html = '';
   
	if(year == "20083" && demographicspage == "3") {
		if(demographicspage == "3") {
			html += '<p>Degrees Conferred information will be available in Fall 2009.</p>';	
		} 
	} 	
	else {
      		if(demographicspage == "1" || demographicspage == "2" || demographicspage == "3") {
       		html += '<select name=\"dsubmenu\" id=\"dsubmenu\" width=\"200\" \n';
			html += 'onChange=\"resetDivs(\'resultDiv4\'); \n';
			html += 'process_demo_submenu2(\'' + view + '\', \'' + year + '\',  \'' + demographicspage + '\', document.factbook.dsubmenu.value); return true;\"> \n';
 	 		html += '<option value=\"\" selected=\"selected\">Choose a View</option> \n';
        		html += '<option value=\"1\">Male</option> \n';
        		html += '<option value=\"2\">Female</option> \n';
        		html += '<option value=\"3\">Total</option> \n';
        		html += '</select> \n';
		} else if (demographicspage == "4" || demographicspage == "5") {
			html += '<br><input type=\"submit\" value=\"Submit\">\n';
		} else {
			html += '<p>Please select a subsection from the drop down menu</p>';	
		}
	}
	if(document.getElementById('resultDiv3')){
     		document.getElementById('resultDiv3').innerHTML = html;
	} else {
		document.write('15 There was an error processing your request. Please <a href="http://www.nyu.edu/ir/factbook/view.tables.html">try again</a>.');
	}
}

function process_demo_submenu2(view, year, demographicspage, dsubmenu) {
    var html = '';
    if(dsubmenu == "1" || dsubmenu == "2" || dsubmenu == "3") {
   	    html += '<br><input type=\"submit\" value=\"Submit\">\n';
	} else {
		html += '<p>Please select a view from the drop down menu</p>';	
	}
	if(document.getElementById('resultDiv4')){
     		document.getElementById('resultDiv4').innerHTML = html;
	} else {
		document.write('15 There was an error processing your request. Please <a href="http://www.nyu.edu/ir/factbook/view.tables.html">try again</a>.');
	}
}



function process_fact_submenu(view, year, factbookpage) {
	var html = '';

    if ( factbookpage == "1" ) {
		html += '<br><p>Topics:</p><div><p> \n';
		html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"1\" checked> Enrollment  <br>\n';
      		html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"3\" checked> Graduation Rate <br> \n';
		html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"4\" checked> Retention Rate<br> \n';
		html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"5\" checked> Degrees Conferred <br>\n';
		html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"6\" checked> Employee Demographics <br>\n';
		html += '</p></div><input type=\"submit\" value=\"Submit\">\n';
    }
    else if ( factbookpage == "2" ) {
		html += '<br><p>Topics:</p><div><p> \n';
       	html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"2\" checked> Admissions <br> \n';
       	html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"1\" checked> Enrollment <br>\n';
       	html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"3\" checked> Graduation Rate <br>\n';
		html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"4\" checked> Retention Rate <br>\n';
		html += '</p></div><input type=\"submit\" value=\"Submit\">\n';
    }
    else if ( factbookpage == "3" ) {
		html += '<br><p>Topics:</p><div><p> \n';
        	html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"2\" checked> Admissions  <br>\n';
        	html += '<input type = \"checkbox\" name = \"fsubmenu\" value = \"1\" checked> Enrollment <br>\n';
		html += '</p></div><input type=\"submit\" value=\"Submit\"> \n';
    } else {
		html += '<p>Please select a subsection from the drop down menu</p>';	
    }
	if(document.getElementById('resultDiv3')){
     		document.getElementById('resultDiv3').innerHTML = html;
	} else {
		document.write('15 There was an error processing your request. Please <a href="http://www.nyu.edu/ir/factbook/view.tables.html">try again</a>.');
	}
}
