		/* console.log("This is the new calculator.js file."); */
		
		function toggleField(el,fld){
			var fm=document.calculator;
			fm.elements[fld].disabled=!el.checked;
		}

		var idealsFields={
			fieldName:"ideals",
			values:[1,2,3,4,5],
			flag:[0,0,0,0,0,0,0,0,0,1,1,0],
			mult:[0,0,0,0,0,0,0,0,0,0,2.0951,0],
			fieldLabels:[
				"The impact that our society has on the environment is so severe that there is very little that individuals can do about it.",
				"The seriousness of environmental problems is exaggerated today.",
				"Environmental problems are having a negative impact on my health today.",
				"The environmental movement is a passing fad. ",
				"I feel guilty about the impact that I have on the environment.",
				"Companies and industries are currently working very hard to make sure that we have a clean environment in my country.",
				"An environmentally friendly lifestyle is good for one's health.",
				"Owning a luxury car is a very important goal in my life.",
				"I am willing to pay more now for an energy-saving product if it will save me money over the product's life due to lower energy costs.",
				"I generally try to buy things \"used\" or pre-owned, rather than brand new.",
				"I choose to live close to the places I need to travel to on most days specifically to minimize the impact my transportation habits have on the environment.",
				"I have recently been paying more attention to news stories about the environment."
			]
		};
		var activitiesFields={
			fieldName:"activities",
			values:[1,2,3,4,5],
			flag:[1,1,1,1,1,0,1,0,3,0],
			mult:[0,0,0,0,1.6836,1.8068,0,0,1.4882,0],
			fieldLabels:[
				"Recycle materials that can be recycled", 	 	 	
				"Avoid certain products specifically because they are bad for the environment",
				"Use your own durable or reusable shopping bags, rather than accepting bags from the store or market.",
				"Wash laundry in cold water rather than warm or hot water specifically to save energy ",			
				"Keep the heating or cooling in your home or workplace at low settings to save energy ",	
				"Take public transportation to save fuel and reduce pollution",				
				"Minimize your use of fresh water",
				"Recycle electronic items like computers, mobile phones or batteries",
				"If distance allows, walk or ride a bike to your destination",
				"Choose NOT to buy a specific type of seafood or fish, specifically because of something you heard about threats to the species"
			]
		};	
		
		
		function makeRandomSet(set){
			var alp="abcdefghijklmnopqrstuvwxyz";
			var ord=[];
			while(ord.length<set.fieldLabels.length){
				var a=Math.floor(Math.random()*set.fieldLabels.length);
				var found=false;
				for(var x=0; x<ord.length; x++){
					if(ord[x]==set.fieldLabels[a]){
						found=true;
						break;
					}
				}
				if(!found){
					ord.push(set.fieldLabels[a]);
				}				
			}
			var out="";
			for(var x=0; x<ord.length; x++){
				out+="<tr>\n";
				out+='<td class="c1">'+alp.charAt(x)+')</td>\n';
				out+='<td class="c2" id="'+set.fieldName+'_'+x+'-label">'+ord[x]+'</td>';
				for(var y=0; y<set.values.length; y++){
					out+='<td class="radio"><input type="radio" name="'+set.fieldName+'_'+x+'" value="'+set.values[y]+'" flag="'+set.flag[x]+'" fac="'+set.mult[x]+'" /></td>\n';
				}
				out+='</tr>'
			}
			return out;
		}
		var counter=0;
		function getRadioValue(fld){
			for(var x=0; x<fld.length; x++){
				if(fld[x].checked){
					return parseInt(fld[x].value);
				}
			}
			return null;					
		}
		function $(a){
			return document.getElementById(a);
		}
		function setError(a){
			$(a+'-label').style.color="red";
		}
		function clearError(a){
			try{
				$(a+'-label').style.color="#333";
			}catch(e){
				alert(a);
			}
		}
		
		var errMsg="There were errors encountered while processing your Greendex.  Please review the fields in red and select or input a proper value!";
		
		
		function calculateGreendex(){
			
			var errFields=[];
			var fm=document.calculator;
			
			var tmp;
			var err=false;
			/* question 1 (1-6)*/
			var q1a=getRadioValue(fm["food-1"]);
			if(q1a==null){
				setError("food-1");
				err=true;
			}
			else{
				q1a=(q1a>=5)?-1:0;
				clearError("food-1");
			}
			var q1b=getRadioValue(fm["food-2"]);
			if(q1b==null){
				setError("food-2");
				err=true;
			}
			else{
				q1b=(q1b>=5)?1:0;
				clearError("food-2");
			}
			var q1c=getRadioValue(fm["food-3"]);
			if(q1c==null){
				setError("food-3");
				err=true;
			}
			else{
				q1c=(q1c>=5)?-1:0;
				clearError("food-3");
			}
			var q1d=getRadioValue(fm["food-4"]);
			if(q1d==null){
				setError("food-4");
				err=true;
			}
			else{
				q1d=(q1d>=5)?-1:0;
				clearError("food-4");
			}
			
			var q1e=getRadioValue(fm["food-6"]);
			if(q1e==null){
				setError("food-6");
				err=true;
			}
			else{
				q1e=(q1e>=5)?-1:0;
				clearError("food-6");
			}
			var q1f=getRadioValue(fm["food-7"]);
			if(q1f==null){
				setError("food-7");
				err=true;
			}
			else{
				q1f=(q1f>=5)?1:0;
				clearError("food-7");
			}
			var q1g=getRadioValue(fm["food-8"]);
			if(q1g==null){
				setError("food-8");
				err=true;
			}
			else{
				q1g=(q1g>=5)?1:0;
				clearError("food-8");
			}
			var q1h=getRadioValue(fm["food-9"]);
			if(q1h==null){
				setError("food-9");
				err=true;
			}
			else{
				q1h=(q1h>=5)?-1:0;			
				clearError("food-9");
			}
			
			/* question 2 (1-20)*/
			if(fm["house-rooms"].selectedIndex==0){
				setError("house-rooms");
				err=true;
			}
			else{
				var q2=parseInt(fm["house-rooms"][fm["house-rooms"].selectedIndex].value);
				clearError("house-rooms");
			}
			
			/* question 3 */
			var q3a=fm["house-features-1"].checked?1:0;
			var q3b=fm["house-features-2"].checked?1:0;
			var q3c=fm["house-features-3"].checked?1:0;
			
			/* question 4 (0-4)*/
			var q4a=getRadioValue(fm["house-install-1"]);
			if(q4a==null){
				setError("house-install-1");
				err=true;
			}
			else{
				clearError("house-install-1");
			}
			var q4b=getRadioValue(fm["house-install-2"]);
			if(q4b==null){
				setError("house-install-2");
				err=true;
			}
			else{
				clearError("house-install-2");
			}
			var q4c=getRadioValue(fm["house-install-3"]);
			if(q4c==null){
				setError("house-install-3");
				err=true;
			}
			else{
				clearError("house-install-3");
			}
			
			
			
			
			/* question 5 (1-6)*/
			var q5a=getRadioValue(fm["transportation-1-1"]);
			if(q5a==null){
				setError("transportation-1-1");
				err=true;
			}
			else{
				clearError("transportation-1-1");
			}
			
			var q5b=getRadioValue(fm["transportation-1-2"]);
			if(q5b==null){
				setError("transportation-1-2");
				err=true;
			}
			else{
				clearError("transportation-1-2");
			}
			
			/* question 6 */
			var q6a=getRadioValue(fm["transportation-2-1"]);
			if(q6a==null){
				setError("transportation-2-1");
				err=true;
			}
			else{
				q6a=q6a*0;
				clearError("transportation-2-1");
			}
			var q6b=getRadioValue(fm["transportation-2-2"]);
			if(q6b==null){
				setError("transportation-2-2");
				err=true;
			}
			else{
				q6b=q6b*0;
				clearError("transportation-2-2");
			}
			var q6c=getRadioValue(fm["transportation-2-3"]);
			if(q6c==null){
				setError("transportation-2-3");
				err=true;
			}
			else{
				q6c=q6c*0;
				clearError("transportation-2-3");
			}
			var q6d=getRadioValue(fm["transportation-2-4"]);
			if(q6d==null){
				setError("transportation-2-4");
				err=true;
			}
			else{
				q6d=q6d*-2.5;
				clearError("transportation-2-4");
			}

			
			/* question 9 */
			var q9a=parseInt(fm["members-1"].value);
			if(isNaN(q9a)){
				setError("members-1");
				err=true;
			}
			else{
				clearError("members-1");
			}
			var q9b=parseInt(fm["members-2"].value);
			if(isNaN(q9b)){
				setError("members-2");
				err=true;
			}
			else{
				clearError("members-2");
			}
			var q9c=parseInt(fm["members-3"].value);
			if(isNaN(q9c)){
				setError("members-3");
				err=true;
			}
			else{
				clearError("members-3");
			}
			var q9d=parseInt(fm["members-4"].value);
			if(isNaN(q9d)||q9d<1){
				setError("members-4");
				err=true;
			}
			else{
				clearError("members-4");
			}
			
			// start totalling
			var numPeople = q9a+q9b+q9c+q9d;
			
			if(q2/numPeople>=3){
				q2=-3;
			}
			else if(q2/numPeople>2){
				q2=-2
			}
			else if(q2/numPeople>1){
				q2=-1;
			}
			else{
				q2=1;
			}
			q3a=(q3a)?-3:0;	
			q3b=(q3b)?-2:0;
			q3c=(q3c)?-2:0;
			
			q4=q4a+q4b+q4c;
			
			q5a=(q5a==6)?-3:0;
			q5b=(q5b==6)?2:0;
			
			
			
			
		
			/* question 7 */
			var q7=[];
			for(var x=0; x<activitiesFields.fieldLabels.length; x++){
				q7[x]=getRadioValue(fm["activities_"+x]);
				if(q7[x]){
					var flag=parseInt(fm["activities_"+x][q7[x]-1].getAttribute('flag'));
					var factor=parseFloat(fm["activities_"+x][q7[x]-1].getAttribute('fac'));
					q7[x]=(q7[x]==5)?flag*factor:0;	
					clearError("activities_"+x);
				}
				else{
					setError("activities_"+x);
					err=true;
				}		
			}	
			/* question 8 */
			var q8=[]
			for(var x=0; x<idealsFields.fieldLabels.length; x++){
				q8[x]=getRadioValue(fm["ideals_"+x]);
				if(q8[x]){
					var flag=parseInt(fm["ideals_"+x][q8[x]-1].getAttribute('flag'));
					var factor=parseFloat(fm["ideals_"+x][q8[x]-1].getAttribute('fac'));
					q8[x]=(q8[x]==5)?flag*factor:0;	
					clearError("ideals_"+x);
				}
				else{
					setError("ideals_"+x);
					err=true;
				}		
			}	
			// var debug="q1a = "+q1a+"\n";
			// debug+="q1b = "+q1b+"\n";
			// debug+="q1c = "+q1c+"\n";
			// debug+="q1d = "+q1d+"\n";
			// debug+="q1e = "+q1e+"\n";
			// debug+="q1f = "+q1f+"\n";
			// debug+="q1g = "+q1g+"\n";
			// debug+="q1h = "+q1h+"\n";
			// debug+="q2 = "+q2+"\n";
			// debug+="q3a = "+q3a+"\n";
			// debug+="q3b = "+q3b+"\n";
			// debug+="q3c = "+q3c+"\n";
			// debug+="q4 = "+q4+"\n";
			// debug+="q5a = "+q3a+"\n";
			// debug+="q5b = "+q3b+"\n";
			// debug+="q6d = "+q6d+"\n";
			// alert(debug)
			var total=51.4829;
			total+=2.114*q1a;
			total+=3.2296*q1b;
			total+=2.2626*q1c;
			total+=2.1653*q1d;
			total+=2.3594*q1e;
			total+=5.75*q1f;
			total+=3.286*q1g;
			total+=1.7726*q1h;
			total+=1.4384*q3a;
			total+=1.1979*q3b;
			total+=0.9015*q3c;
			total+=1.3619*q4;
			total+=1.6481*q2;
			total+=0.8768*q5a;
			total+=1.9207*q5b;
			total+=1.1868*q6d;
			for(var x=0; x<q7.length; x++){
				total+=q7[x];
			}
			for(var x=0; x<q8.length; x++){
				total+=q8[x];
			}
			
			
			
			
			if(err){
				alert(errMsg);
			}
			else{
				$("showGreendexValue").innerHTML=Math.floor(total);
				$('postCalc').style.display="block";
				location.href=location.href+"#calculateResults";
			}
			return false;
			
		}