window.onload = function() {
	
	$$('input, textarea, select').each(function(e) { 
			
		if(e.id != "unit1" && e.name != "submitSurvey") {
			e.disable(); 
			e.style.backgroundColor = "#D4D0C8";
		}
			
	});
	
};

function enable_form(s) {
	
	$$('input, textarea, select').each(function(e) { 
												
		e.enable();
		if(e.type != "button") 
			e.style.backgroundColor = "#ffffff";
		
	});
	
	// replacement ms_only
	$$('tr.replacement').each(function(e) { 
	
		if(e.hasClassName('ms_only') && s.value != "MS")
			e.parentNode.removeChild(e);
		
	});
	
	
	switch(s.value) {
		case "MS":
			building($('unit2'), "Morningside");
			size($('unit5'), "Morningside");
			$('span_campus').innerHTML = "<b>Morningside</b>";
		break;
		
		case "CUMC":
			building($('unit2'), "Medical_Center");
			size($('unit5'), "Medical_Center");
			$('span_campus').innerHTML = "<b>Medical Center</b>";
		break;
		
		case "LDEO":
			building($('unit2'), "LDEO");
			size($('unit5'), "LDEO");
			$('span_campus').innerHTML = "<b>LDEO</b>";
		break;
		
		case "NEVIS":
			building($('unit2'), "Nevis_Laboratories");
			size($('unit5'), "Nevis_Laboratories");
			$('span_campus').innerHTML = "<b>Nevis Laboratories</b>";
		break;
	}
	
	$('span_campus').innerHTML += ' &nbsp;&nbsp;&nbsp;&nbsp; -- &nbsp;<em><a href="./">Change Campus</a><em?';
	s.style.visibility = "hidden";
}


function checkForm(f) {
	
	var pass = true;
	var errmsg = "Sorry we were unable to process your request. \nRequired information is missing. \n\nPlease review all error fields marked in red. \n\n";
	
	$$(".lab_required").each(function(e) {
		
		if(e.value == "") {
			if(e.disabled != true) {
				//errmsg += ("Required: "+e.name+"\n");
				e.style.backgroundColor = "#ffcccc";
				pass = false;
			}
		} else {
			e.style.backgroundColor = "#ffffff";
		}
	
	});
	
	$$(".verify").each(function(e) {
		
		if($('noWaste').checked == false && e.checked == false) {
			if(e.disabled != true) {
				//errmsg += ("Required: "+e.name+"\n");
				e.up().style.backgroundColor = "#ffcccc";
				pass = false;
			}
		} else {
			e.up().style.backgroundColor = "#ffffff";
		}
	
	});
	
	
	f.elements["replacement_supplies"].value = "";
	$$(".replacement_supplies").each(function(e) {
		
		if(e.value != "") {
			f.elements["replacement_supplies"].value += e.name + ": " + e.value + "\n";
		}
	
	});
	
	
	f.elements["chemical_hazardous"].value = "";
	$$(".hazardous").each(function(e) {
		
		if(e.value != "") {
			f.elements["chemical_hazardous"].value += e.name + ": " + e.value + "\n";
			if(e.name == "Location of waste #")
				f.elements["chemical_hazardous"].value += "\n";
		}
	
	});
	
	if(f.elements["replacement_supplies"].value == "")
		f.elements["replacement_supplies"].value = "N/A \n";
	
	if(f.elements["chemical_hazardous"].value == "")
		f.elements["chemical_hazardous"].value = "N/A \n";
	
	
	if(pass) {
		f.submit();		
	} else {
		alert(errmsg);
		return false;
	}
}

function add() {
	if($('noWaste').checked != true && $('unit1').value != "")
		hazardous.add();
	
	if($('hazardous_clone').childNodes.length > 0)
		$('chemical_one').style.display = '';
	else
		$('chemical_one').style.display = 'none';
	
	return false;
}



function toggle_hazardous(c) {

	if(c.checked)
		$$('.hazardous').each(function(e) { 
			e.disable(); 
			e.style.backgroundColor = "#D4D0C8"; 
		});
	
	else
		$$('.hazardous').each(function(e) { 
			e.enable();  
			e.style.backgroundColor = "#ffffff";
		});

}



function building(f, campus) {
	var a;
	var c = 0;
	
	$(f).options.length = 0; // clear option list
	$(f).options[c++] = new Option("--Select--","");
	
	if(f.options)
		Buildings[campus].each(function(i) { $(f).options[c++] = new Option(i,i) });
}

function size(f, campus) {
	var a;
	var c = 0;
	
	var sindex = $(f).selectedIndex;
	var selected = $(f).options[sindex].value;
	
	$(f).options.length = 0; // clear option list
	$(f).options[c++] = new Option("--Select--","");
	
	$(f).options[c++] = new Option("1gal(3.78L)","1gal(3.78L)"); // ALL
	$(f).options[c++] = new Option("5 gal(18.9L)","5 gal(18.9L)"); // ALL
	
	if(campus == "Morningside") $(f).options[c++] = new Option("2 gal(small red)sharps box","2 gal(small red)sharps box");
	if(campus == "Morningside") $(f).options[c++] = new Option("8 gal(large red)sharps box","8 gal(large red)sharps box");
	if(campus == "Morningside") $(f).options[c++] = new Option("Biohaz grey bin","Biohaz grey bin");
	
	$(f).options[c++] = new Option(">5 gal (18.9L)",">5 gal (18.9L)"); // ALL
	$(f).options[c++] = new Option("Lecture Bottle/Cylinder","Lecture Bottle/Cylinder"); // ALL
	
	if(campus == "Morningside") $(f).options[c++] = new Option("Blue bin Contaminated Glass","Blue bin Contaminated Glass");
	
	$(f).options[c++] = new Option("Other (Describe in comments section)","Other (Describe in comments section)"); // ALL
	
	$(f).selectedIndex = 0;
	
	for(i=0; i<$(f).options.length; i++)
		if($(f).options[i].value == selected)
			$(f).selectedIndex = i;
}

//auto phone format
function format_phone(phone) {
	phoneRegex = /([0-9]{3})([0-9]{3})([0-9]{4})/;
	
	if(phone.value.length == 10)
	{
		phone.value = phone.value.replace(phoneRegex, "($1)$2-$3");
	}
}