// JavaScript Document
var digiPortal = {	
	formID: 'digiPortalF',
	formOBJ: '',
	
	selectID: 'ApplicantQualificationMethod',
	selectOBJ: '',
	
	dropDownIDARR: new Array('HouseholdIncome','ProgramParticipation','SchoolAffiliation'),
	
	init: function() {	
	
		if(document.getElementById(digiPortal.formID) && document.getElementById(digiPortal.selectID)) {
			//alert('found form and select');
			
			//set form and select dropdown objects
			digiPortal.formOBJ = document.getElementById(digiPortal.formID);
			digiPortal.selectOBJ = document.getElementById(digiPortal.selectID);
			
			//if onload current option selected is valid (not empty) show correct drop down
			if(digiPortal.selectOBJ.selectedIndex != -1 && digiPortal.selectOBJ.options[digiPortal.selectOBJ.selectedIndex].value != "") {
				digiPortal.toggleDropDowns();
			}
			
			//add onchange to qualification select			
			addEvent(digiPortal.selectOBJ,'change',digiPortal.toggleDropDowns,false);
		}
		
	},
	
	toggleDropDowns: function(e) {
		
		//cycle through array of drop downs, display/hide accordingly		
		for(i = 0; i < digiPortal.dropDownIDARR.length; i++) {
			//selected value matches dropdownID array? display drop down
			if(digiPortal.dropDownIDARR[i] == digiPortal.selectOBJ.options[digiPortal.selectOBJ.selectedIndex].value) {				
				document.getElementById(digiPortal.dropDownIDARR[i]).style.display = '';
			} 
			//no match, hide drop down
			else {
				document.getElementById(digiPortal.dropDownIDARR[i]).style.display = 'none';
			}
		}			
	}	
}

var formOthers = {
	otherDivAppendStr: "_other",
	otherInputAppendStr: "Other",
	
	otherDivAppendRGX: /_other/,
	otherInputAppendRGX: /Other/,
	
	init: function() {
		if(!document.getElementById || !document.getElementsByTagName) {
			return;
		}
		
		allDivsARR = document.getElementsByTagName('div');
		
		for(i=0;i < allDivsARR.length; i++) {
			currentDiv = allDivsARR[i];
			
			if(currentDiv.id.match(formOthers.otherDivAppendRGX) && document.getElementById(currentDiv.id.replace(formOthers.otherDivAppendRGX,'')) && (document.getElementById(currentDiv.id.replace(formOthers.otherDivAppendRGX,'')).nodeName == 'SELECT' || document.getElementById(currentDiv.id.replace(formOthers.otherDivAppendRGX,'')).type == 'radio' || document.getElementById(currentDiv.id.replace(formOthers.otherDivAppendRGX,'')).type == 'checkbox')) {				
				//get element to add event to
				evElement = document.getElementById(currentDiv.id.replace(formOthers.otherDivAppendRGX,''));
				evType = evElement.nodeName? 'change' : 'click';	
				
				//
				formOthers.toggleOther(evElement);
					
				addEvent(evElement,evType,formOthers.toggleOther,false);				
			}
		}
	},	
	
	toggleOther: function(e) {
		var local_target;
		
		if(!findTarget(e) && e.nodeName == 'SELECT') {
			local_target = e;
		} else {
			local_target=findTarget(e);
		}
		
		if(!local_target) return;
		
		//check for the word "Other" in value of an option if select
		if(local_target.nodeName == 'SELECT') {			
			if(local_target.options[local_target.selectedIndex].value.toLowerCase() == 'other' || local_target.options[local_target.selectedIndex].firstChild.nodeValue.toLowerCase() == 'other') {				
				//show text field
				Element.show(local_target.id + formOthers.otherDivAppendStr);
			}
			else {
				//clear field and hide field
				document.getElementById(local_target.id + formOthers.otherDivAppendStr).firstChild.value = '';
				Element.hide(local_target.id + formOthers.otherDivAppendStr);
			}			
		} else if(local_target.nodeName == 'INPUT' && local_target.type == 'radio') {
			
		} else if(local_target.nodeName == 'INPUT' && local_target.type == 'checkbox') {
			
		}
	}
}

addEvent(window, 'load', formOthers.init, false);
addEvent(window, 'load', digiPortal.init, false);