//*********Data Structures*******************//
		//************style array********************//
		var style = new Array();
		style[0]=new Array('None'); //none
		style[1]=new Array('20 degrees', '30 degrees', '40 degrees', '50 degrees', '60 degrees'); //lightbanks
		style[2]=new Array('20 degrees', '30 degrees', '40 degrees', '50 degrees', '60 degrees'); //lowel
		//style[3]=new Array('20 degrees', '40 degrees', '60 degrees'); //westcott
		//style[4]=new Array('20 degrees', '30 degrees', '40 degrees', '50 degrees'); //butterfly & overhead
		style[3]=new Array('20 degrees', '30 degrees', '40 degrees', '50 degrees'); //butterfly & overhead
		//style[5]=new Array('30 degrees', '40 degrees', '50 degrees'); //kino flo
		
		
		//*****available width and height array*************//
		var wh = new Array();
		wh[0]=new Array(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 12.0, 20.0); //none
		wh[1]=new Array(1.0, 2.0, 3.0, 4.0, 5.0, 6.0); //lightbanks
		wh[2]=new Array(1.0, 2.0, 3.0, 4.0, 5.0, 6.0); //lowel
		//wh[3]=new Array(4.0, 6.0); //westcott
		//wh[4]=new Array(4.0, 6.0, 8.0, 12.0, 20.0); //butterfly
		wh[3]=new Array(4.0, 6.0, 8.0, 12.0, 20.0); //butterfly
		//wh[5]=new Array(4.0, 6.0); //kinoflo
		
		//**********cell depth array*******************//
		var cell_depth = new Array();
		cell_depth[0] = 0; //none
		cell_depth[1] = 0.166666; //lightbanks
		cell_depth[2] = 0.166666; //lowel
		//cell_depth[3] = 0.166666; //westcott
		//cell_depth[4] = 0.25; //butterfly
		cell_depth[3] = 0.25; //butterfly
		//cell_depth[5] = 0.25; //kinoflo
		
		//**********cell width array****************//
		var cell_width = new Array();
		cell_width[0] = new Array(0); //none
		cell_width[1] = new Array(0.060833, 0.096110, 0.140000, 0.198333, 0.288333); //lightbanks
		cell_width[2] = new Array(0.060833, 0.096110, 0.140000, 0.198333, 0.288333); //lowel
		//cell_width[3] = new Array(0.060833, 0.140000, 0.288333); //westcott
		//cell_width[4] = new Array(0.090991, 0.144166, 0.210000, 0.297500); //butterfly
		cell_width[3] = new Array(0.090991, 0.144166, 0.210000, 0.297500); //butterfly
		//cell_width[5] = new Array(0.144166, 0.210000, 0.297500); //kinoflo
		
		//***********Functions****************************//
		//*********function to creat corresponding info for a model****//
		function populateByModel()
		{
			var box = document.Form_Model.box_models;
			var number = box.selectedIndex;
			//alert("index= "+number);
			//if (!number) return;
			var list1 = style[number]; //styles processing
			var box1 = document.Form_Model.box_styles; 
			box1.options.length = 0;
			for(i=0;i<list1.length;i++)			
			box1.options[i] = new Option(list1[i],list1[i]); //styles ready
			
			var list2 = wh[number]; //widths processing
			var box2 = document.Form_Model.box_widths; 
			box2.options.length = 0;
			for(i=0;i<list2.length;i++)			
			box2.options[i] = new Option(list2[i],list2[i+1]); //widths ready
			
			var list3 = wh[number]; //heights processing
			var box3 = document.Form_Model.box_heights; 
			box3.options.length = 0;
			for(i=0;i<list3.length;i++)			
			box3.options[i] = new Option(list3[i],list3[i+1]); //heights ready
			
			calculate();
			
		}
		
		//***********function to add 0.5' offset horizontally for meter***//
		function horizontal_add()
		{
			var typed = parseFloat(Form_Meter["meter_hori"].value);
			if (typed>999.5){
			alert("All the horizontal offsets above 999.5 will be set to 999.5.");
			Form_Meter["meter_hori"].value=999.5;
			} else if (typed<0){
			alert("All the horizontal offsets under 0 will be set to 0.");
			Form_Meter["meter_hori"].value=0;}
			else {
			Form_Meter["meter_hori"].value=typed+0.5;
			}
			calculate();	
		}
		
		//***********function to minus 0.5' offset horizontally for meter***//
		function horizontal_minus()
		{
			var typed = parseFloat(Form_Meter["meter_hori"].value);
			if (typed>10000){
			alert("All the horizontal offsets above 999.5 will be set to 999.5.");
			Form_Meter["meter_hori"].value=999.5;
			} else if (typed<0.5){
			alert("All the horizontal offsets under 0 will be set to 0.");
			Form_Meter["meter_hori"].value=0;}
			else
			Form_Meter["meter_hori"].value=typed-0.5;		
			calculate();
		}
		
		
		//***********function to add 0.5' offset vertically for meter***//
		function vertical_add()
		{
			var typed = parseFloat(Form_Meter["meter_vert"].value);
			if (typed>999.5){
			alert("All the vertical offsets above 999.5 will be set to 999.5.");
			Form_Meter["meter_vert"].value=999.5;
			} else if (typed<0){
			alert("All the vertical offsets under 0 will be set to 0.");
			Form_Meter["meter_vert"].value=0;}
			else
			Form_Meter["meter_vert"].value=typed+0.5;		
			calculate();
		}
		
		//***********function to minus 0.5' offset vertically for meter***//
		function vertical_minus()
		{
			var typed = parseFloat(Form_Meter["meter_vert"].value);
			if (typed>10000){
			alert("All the vertical offsets above 999.5 will be set to 999.5.");
			Form_Meter["meter_vert"].value=999.5;
			} else if (typed<0.5){
			alert("All the vertical offsets under 0 will be set to 0.");
			Form_Meter["meter_vert"].value=0;}
			else
			Form_Meter["meter_vert"].value=typed-0.5;		
			calculate();
		}
		
		//***********function to add 0.5' offset distance for meter***//
		function distance_add()
		{
			var typed = parseFloat(Form_Meter["meter_dist"].value);
			if (typed>999.5){
			alert("All the distance offsets above 999.5 will be set to 999.5.");
			Form_Meter["meter_dist"].value=999.5;
			} else if (typed<0.5){
			alert("All the distance offsets under 0.5 will be set to 0.5");
			Form_Meter["meter_dist"].value=0.5;}
			else
			Form_Meter["meter_dist"].value=typed+0.5;		
			calculate();
		}
		
		//***********function to minus 0.5' offset distance for meter***//
		function distance_minus()
		{
			var typed = parseFloat(Form_Meter["meter_dist"].value);
			if (typed>10000){
			alert("All the distance offsets above 999.5 will be set to 999.5.");
			Form_Meter["meter_dist"].value=999.5;
			} else if (typed<1){
			alert("All the distance offsets under 0.5 will be set to 0.5");
			Form_Meter["meter_dist"].value=0.5;}
			else
			Form_Meter["meter_dist"].value=typed-0.5;
			calculate();		
		}
		
		//*******************function to change distance along with diffusure width**************//
		function change_dist()
		{	
			var model=Form_Model["box_models"].selectedIndex;
			var diffuser_width = wh[model][Form_Model["box_widths"].selectedIndex];
			Form_Meter["meter_dist"].value=Math.round(diffuser_width*3*10)/10;
			calculate();
					
		}
		
		
		//******Main Function to Calculate Relative Illuminance***//
		//*********************************************************//
		function calculate()
		{
			var EggDiffReflect=0.1; //Eggcrate diffuse reflectance (based on physical measurements)
			var df_ff;               // Diffuser form factor, from CalcDiffuserFormFactor() below
			var diff_illum;           // Relative diffuser illuminance: f-stop
			var ec_ff;                // Eggcrate form factor, from CalcEggcrateFormFactor() below
			var egg_illum;            // Relative eggcrate form factor: f-stop
			
			//get model index
			var model=Form_Model["box_models"].selectedIndex;
			//alert("modelindex="+model);
			
			//get style index
			var style=Form_Model["box_styles"].selectedIndex;
			//alert("styleindex="+style);
			//get cell depth
			var cell_d = cell_depth[model];
			//alert("cell depth= "+cell_d);
			//get cell width
			var cell_w = cell_width[model][Form_Model["box_styles"].selectedIndex];	
			//alert("cell_width= "+cell_w);
			//get diffuser width
			var diffuser_width = wh[model][Form_Model["box_widths"].selectedIndex];
			//alert("diffuser_width= "+diffuser_width);
			//get diffuser height
			var diffuser_height = wh[model][Form_Model["box_heights"].selectedIndex];
			//alert("diffuser_height= "+diffuser_height);
			//get number of horizontal cells
			var num_h_cells = Math.floor(diffuser_width/cell_w);
			//alert("number of hori cells= "+num_h_cells); // REMEMBER TO ACCOUNT FOR NO COVERING
			//get number of vertical cells
			var num_v_cells = Math.floor(diffuser_height/cell_w);
			//alert("number of vert cells= "+num_v_cells); // REMEMBER TO ACCOUNT FOR NO COVERING
			//get horizontal offset
			var offset_h = parseFloat(Form_Meter["meter_hori"].value);
			if (offset_h<0){
			alert("All the horizontal offsets under 0 will be set to 0.");
			offset_h = 0;
			Form_Meter["meter_hori"].value = 0;
			}	else if (offset_h>999.5){
			 	alert("All the horizontal offsets above 999.5 will be set to 999.5.");
				offset_h = 999.5;
				Form_Meter["meter_hori"].value = 999.5;
				}
			
			//alert("hori offset"+offset_h);
			//get vertical offset
			var offset_v = parseFloat(Form_Meter["meter_vert"].value);
			if (offset_v<0){
			alert("All the vertical offsets under 0 will be set to 0.");
			offset_v = 0;
			Form_Meter["meter_vert"].value = 0;
			}	else if (offset_v>999.5){
			 	alert("All the vertical offsets above 999.5 will be set to 999.5.");
				offset_v = 999.5;
				Form_Meter["meter_vert"].value = 999.5;
				}
			//alert("vert offset="+offset_v);
			//get distance
			var offset_d = parseFloat(Form_Meter["meter_dist"].value);
			if (offset_d<0.5){
			alert("All the distance offsets under 0.5 will be set to 0.5");
			offset_d = 0.5;
			Form_Meter["meter_dist"].value = 0.5;
			}	else if (offset_d>999.5){
			 	alert("All the distance offsets above 999.5 will be set to 999.5.");
				offset_d = 999.5;
				Form_Meter["meter_dist"].value = 999.5;
				}
			//alert("distance="+offset_d);
			
			//**********put metric processing here in the metric calc page******//
			//***********************************************************//
			//*******************************************************//
			
			
			//start calculating
			// Calculate diffuser form factor
			df_ff = CalcDiffuserFormFactor(offset_h, offset_v, diffuser_width, diffuser_height, offset_d);
			
			// Calculate relative diffuser illuminance (f-stop)
			diff_illum = Math.log(Math.max(df_ff, 1.0e-12)) / Math.log(2.0);
			//alert("before diff_illum="+diff_illum);
			diff_illum = Math.round(10*diff_illum)/10; 
			//alert("after diff_illum="+diff_illum);

			// Display relative diffuser form factor illuminance
			//Form_Result["ri_diffuser"].value = diff_illum;

			if (model > 0)	// Eggcrate ?
			{
				// Calculate eggcrate form factor
				//alert("you are inside (model>0) block now?");
				ec_ff = CalcEggcrateFormFactor(cell_w, offset_d, cell_d, offset_v, num_v_cells, offset_h, num_h_cells) + EggDiffReflect * df_ff;
				//alert("egg ff="+ec_ff);

				// Calculate relative diffuser illuminance (f-stop)
				egg_illum = Math.log(Math.max(ec_ff, 1.0e-12)) / Math.log(2.0);
				egg_illum = Math.round(10*egg_illum)/10; 

				// Display relative diffuser form factor illuminance
				//Form_Result["ri_eggcrate"].value = egg_illum; 
				Form_Result["ri_eggcrate"].value = Math.round(10*(egg_illum - diff_illum))/10; // relative change
			}
			else
			{
				// Reset relative eggcrate illuminance
				Form_Result["ri_eggcrate"].value = 0; //relative now may 01, 2006
			}
			
			
			//alert("right?");
			//alert("calcformfactor="+CalcEggcrateFormFactor(cell_w, offset_d, cell_d, offset_v, num_v_cells, offset_h, num_h_cells));
		}
		
		//***********eggcrate form factor******************//
		function CalcEggcrateFormFactor(m_cell_width, m_dist, m_cell_depth, m_vert, m_num_vert, m_horz, m_num_horz)
		{
			var ff = 0.0;	// Form factor
			var c_hoff;		// Cell horizontal offset
			var c_voff;		// Cell vertical offset
			var half_width;	// Cell half-width
			var max_off;		// Maximum cell offset
			var vc_height;	// Visible cell height
			var vc_hoff;		// Visible cell horizontal offset
			var vc_voff;		// Visible cell verticall offset
			var vc_width;	// Visible cell width
			var h_index;		// Cell index (horizontal)
			var v_index;		// Cell index (vertical)
			
			// Calculate cell half-width
			half_width = m_cell_width / 2.0;
			//alert("cell width half="+half_width);

			// Calculate maximum cell offset
			max_off = (m_cell_width * (m_dist - m_cell_width) / m_cell_depth) - half_width;
			//alert("max_off="+max_off);

			// Calculate cell vertical offset
			c_voff = m_vert - half_width * (m_num_vert - 1);

			for (v_index = 0; v_index < m_num_vert; v_index++)
			{
				// Calculate cell vertical offset
				c_hoff = m_horz - half_width * (m_num_horz - 1);

				// Check for cell visibility
				if ((c_voff < max_off) && (c_voff > -max_off))
				{
					for (h_index = 0; h_index < m_num_horz; h_index++)
					{
						// Check for cell visibility
						if (c_hoff < max_off && c_hoff > -max_off)
						{
							// Calculate visible cell width
							vc_width = m_cell_width - (Math.abs(c_hoff) - half_width) * m_cell_depth / (m_dist - m_cell_depth);

							// Calculate visible cell height
							vc_height = m_cell_width - (Math.abs(c_voff) - half_width) * m_cell_depth / (m_dist - m_cell_depth);

							// Calculate visible cell horizontal offset
							vc_hoff = Math.abs(c_hoff) + half_width - vc_width /2.0;

							// Calculate visible cell vertical offset
							vc_voff = Math.abs(c_voff) + half_width - vc_height /2.0;

							// Calculate cell form factor
							ff += CalcDiffuserFormFactor(vc_hoff, vc_voff, vc_width, vc_height, m_dist);
						}

						c_hoff += m_cell_width;
					}
				}

				c_voff += m_cell_width;
			}

			return ff;
		
		
		
		}
		
		
		//*************function calcdiffuser form factor****************/
		function CalcDiffuserFormFactor(h_offset, v_offset, width, height, dist)
		{
			var ff = 0.0;	// Form factor
			var max_horz;	// Maximum horizontal offset
			var max_vert;	// Maximum vertical offset
			var min_horz;	// Minimum horizontal offset
			var min_vert;	// Minimum vertical offset

			// Calculate offsets (first quadrant)
			max_horz = Math.max(h_offset + width / 2.0, 0.0);
			max_vert = Math.max(v_offset + height / 2.0, 0.0);
			min_horz = Math.max(h_offset - width / 2.0, 0.0);
			min_vert = Math.max(v_offset - height / 2.0, 0.0);

			// Calculate partial form factor (first quadrant)
			if ((max_horz > 0.0) && (max_vert > 0.0))
				ff += CalcPartialFormFactor(max_horz, min_horz, max_vert, min_vert, dist);
				
			
			// Calculate offsets (second quadrant)
			min_horz = Math.max(-h_offset - width / 2.0, 0.0);
			max_vert = Math.max(v_offset + height / 2.0, 0.0);
			max_horz = Math.max(-h_offset + width / 2.0, 0.0);
			min_vert = Math.max(v_offset - height / 2.0, 0.0);
			
			// Calculate partial form factor (second quadrant)
			if ((max_horz > 0.0) && (max_vert > 0.0))
				ff += CalcPartialFormFactor(max_horz, min_horz, max_vert, min_vert, dist);

			// Calculate offsets (third quadrant)
			min_horz = Math.max(-h_offset - width / 2.0, 0.0);
			min_vert = Math.max(-v_offset - height / 2.0, 0.0);
			max_horz = Math.max(-h_offset + width / 2.0, 0.0);
			max_vert = Math.max(-v_offset + height / 2.0, 0.0);

			// Calculate partial form factor (third quadrant)
			if ((max_horz > 0.0) && (max_vert > 0.0))
				ff += CalcPartialFormFactor(max_horz, min_horz, max_vert, min_vert, dist);

			// Calculate offsets (fourth quadrant)
			max_horz = Math.max(h_offset + width / 2.0, 0.0);
			min_vert = Math.max(-v_offset - height / 2.0, 0.0);
			min_horz = Math.max(h_offset - width / 2.0, 0.0);
			max_vert = Math.max(-v_offset + height / 2.0, 0.0);

			// Calculate partial form factor (fourth quadrant)
			if ((max_horz > 0.0) && (max_vert > 0.0))
				ff += CalcPartialFormFactor(max_horz, min_horz, max_vert, min_vert, dist);

			return ff;
		}
		
		//******************function partial form factor****************/
		function CalcPartialFormFactor(max_horz, min_horz, max_vert, min_vert, dist)
		{
			var ff1, ff2, ff3, ff4;	// Partial form factors

			ff1 = CalcFormFactor(max_horz, max_vert, dist);
			ff2 = CalcFormFactor(min_horz, max_vert, dist);
			ff3 = CalcFormFactor(max_horz, min_vert, dist);
			ff4 = CalcFormFactor(min_horz, min_vert, dist);

			return (ff1 - ff2 - ff3 + ff4);
		}
		
		//****************function calc form factor**********/
		// Calculate form factor from differential surface parallel to a finite
		// rectangular surface:
		//
		//				----------------
		//			  /               /
		//          /	   A2       /  dist_y
		//        /				  /
		//       ----------------
		//       |	  dist_x
		//		 |
		//       | dist_z
		//       |
		//       |
		//       ---
		//     /  / dA1
		//    ---
		function CalcFormFactor(x_dist, y_dist, z_dist )
		{
			var x, y;	// Temporary variables
			var fx, fy;	// Temporary variables
			var ff;		// Form factor

			x = x_dist / z_dist;
			y = y_dist / z_dist;

			fx = Math.sqrt(1 + x * x);
			fy = Math.sqrt(1 + y * y);

			ff = (1.0 / (2.0 * Math.PI)) * ((x * Math.atan(y / fx) / fx) + (y * Math.atan(x /fy) / fy));
			
			return ff;
		}
		