// JavaScript Document
var fields='';
var errors='';

function checkEmail(theFormElem) {		
	if (theFormElem.value.search('@')==-1) {
   errors=errors+'\n*Email address appears invalid.';
   return false;
	}
return true;
}

function checkRadio(theRadio) {
	var radioChkd=false;
	for(var i=0;i<theRadio.length;i++) {
		if(theRadio[i].checked==true) {
			radioChkd=true;
		}
	}	
	if(!radioChkd) {
		return false;
	} 
	return true;
}

function checkSelect(theSelect) {
	if (theSelect.selectedIndex==-1) {
		return false;
	}
	return true;
}

function checkForm(theForm) {
	if(theForm.email.value=="") {
		fields=fields+'\n*Email';
	} else {
		checkEmail(theForm.email);
	}
	if(theForm.company.value=="") {
		fields=fields+'\n*Company';
	}
	if(theForm.city.value=="") {
		fields=fields+'\n*City';
	}
	if(theForm.state.value=="") {
		fields=fields+'\n*State';
	}	
	if(!checkSelect(theForm.country)) {
		fields=fields+'\n*Country';
	}	
	if(!checkRadio(theForm.verbuild)) {
		fields=fields+'\n*Version';
	}		
	if(!checkRadio(theForm.reg_for)) {
		fields=fields+'\n*Are you registering this copy for';
	}	
	if(!checkRadio(theForm.use_for)) {
		fields=fields+'\n*What will you use EnergyPlus for:';
	}	
	if(!theForm.learn_about.value.length) {
		fields=fields+'\n*Where did you learn about EnergyPlus?';
	}
	if (theForm.previous[1].checked) {
		if (theForm.how_long.value=='') {
			fields=fields+'\n*How long have you have been using EnergyPlus?';
		}
		if (theForm.features.value=='') {
			fields=fields+'\n*What are the 5 most important features the program lacks?';
		}
		if (theForm.buildings.value=='') {
			fields=fields+'\n*Names of noteworthy buildings you have modeled with the program.';
		}
		if (theForm.eval[0].checked==true && theForm.start_design.value=='') {
			fields=fields+'\n*Estimate of total floor area modeled since you started using the program for Building Design.';
		}
		if (theForm.start_design.value!='' && (theForm.measure3[0].checked==false && theForm.measure3[1].checked==false)) {
			fields=fields+'\n*UNITS for Estimate of total floor area modeled since you started using the program for Building Design.';
		}
		if (theForm.eval[0].checked==true && theForm.year_design.value=='') {
			fields=fields+'\n*Estimate of total floor area modeled with the program in the past year for Building Design.';
		}
		if (theForm.year_design.value!='' && (theForm.measure1[0].checked==false && theForm.measure1[1].checked==false)) {
			fields=fields+'\n*UNITS for Estimate of total floor area modeled with the program in the past year for Building Design.';
		}
		if (theForm.eval[1].checked==true && theForm.start_retrofit.value=='') {
			fields=fields+'\n*Estimate of total floor area modeled since you started using the program for Retrofit.';
		}
		if (theForm.start_retrofit.value!='' && (theForm.measure4[0].checked==false && theForm.measure4[1].checked==false)) {
			fields=fields+'\n*UNITS for Estimate of total floor area modeled since you started using the program for Retrofit.';
		}
		if (theForm.eval[1].checked==true && theForm.year_retrofit.value=='') {
			fields=fields+'\n*Estimate of total floor area modeled with the program in the past year for Retrofit.';
		}
		if (theForm.year_retrofit.value!='' && (theForm.measure2[0].checked==false && theForm.measure2[1].checked==false)) {
			fields=fields+'\n*UNITS for Estimate of total floor area modeled with the program in the past year for Retrofit.';
		}
		if (!checkSelect(theForm.savings)) {
			fields=fields+'\n*Estimate of average percent energy savings realized through use of the program.';
		}
		if (theForm.other_work.value=='') {
			fields=fields+'\n*Other work that you used EnergyPlus to model?';
		}
	}
	if (fields!='' || errors!='') {
		var msg='';
		if(fields!='') msg=msg+'You must fill in the following field(s):\n'+fields;
		if(errors!='') msg=msg+'\n\nError(s):\n'+errors;
		alert(msg);	
		msg=''; errors=''; fields='';
		return false;
	}	
	theForm.email.disabled=false;
	return true;
}