BEGIN {

	RS="&";
	FS="=";

	JQCSSPATH="/jquery.mobile-1.2.1.min.css";
	JQBASEPATH="/jquery-1.8.3.min.js";
	JQMOBILEPATH="/jquery.mobile-1.2.1.min.js";
	JQPLUGINPATH="/plugin.js";

	project_path="/usr/share/";

        constants["copyright"] = "&copy;2018 LED OCD | All rights reserved.";
	constants["titlemain"] = "LED OCD Configuration";
	constants["titlehome"] = "LED OCD Configuration";
	constants["lblsubmit"] = "Apply";

	#read the list of data files
	command=sprintf("ls -1 %sledocd_gui/data/*.csv | grep csv > /tmp/datafiles", project_path);
        system(command);
	while(( getline data<"/tmp/datafiles") > 0 ) {
		searcheol="\n";
		numlines=split(data,datapaths,searcheol);
		numdatafiles = 0;
		for(line=1;line<=numlines;line++) {
			searchfield=".";
			numfields=split(datapaths[line],fields,searchfield);
			if(numfields == 2) {
				searchfield="/";
				numfields=split(fields[1],datafiles,searchfield);
				datafilename[numdatafiles+1] = datafiles[numfields];
				datafiletype[numdatafiles+1] = "WPC";
				numlamps_ra[numdatafiles+1]=0;
				numlamps=0;
				while(( getline lampsinfo<datapaths[line]) > 0 ) {
					if(index(lampsinfo, "CAPCOM_A") > 0) {
						datafiletype[numdatafiles+1] = "CAPCOMA";
					}
					else if(index(lampsinfo, "CAPCOM_B") > 0) {
						datafiletype[numdatafiles+1] = "CAPCOMB";
					}
					searcheol="\n";
					numinfolamps=split(lampsinfo,lampinfo,searcheol);
					for(lamp=1;lamp<=numinfolamps;lamp++) {
						searchfield=",";
						numfields=split(lampinfo[lamp],fields,searchfield);
						if((numfields>=2) && (fields[1] ~ /^[0-9]+$/)) {
							lampnumber_ra[numdatafiles+1, numlamps+1] = fields[1];
							if(fields[1]+0 < 11) {
								datafiletype[numdatafiles+1] = "STERN";
							}
							lampname_ra[numdatafiles+1, numlamps+1] = fields[2];
							numlamps_ra[numdatafiles+1]++;
							lampnumber[numlamps+1] = fields[1];
							lampname[numlamps+1] = fields[2];
							numlamps++;
						}
					}
				}
				numdatafiles++;
			}
		}
	}
	close("/tmp/datafiles");

	print "Content-type: text/html";
	print "";
	print "<!DOCTYPE html>";
	print "<html>";

	print "<head>";
	printf "<title>%s</title>\n", constants["titlemain"];
	print "<meta name='viewport' content='width=device-width, initial-scale=1'>\n";
	printf "<link rel='stylesheet' href='%s'/>\n", JQCSSPATH;
	printf "<script type='text/javascript' src='%s'></script>\n", JQBASEPATH;
	printf "<script type='text/javascript' src='%s'></script>\n", JQMOBILEPATH;

	gameindex=0;
	
	port="/dev/ttyUSB0";

	profile_name[1] = "Incandes";
	profile_bright[1,1] = 30;
	profile_bright[1,2] = 40;
	profile_bright[1,3] = 50;
	profile_bright[1,4] = 60;
	profile_bright[1,5] = 70;
	profile_bright[1,6] = 80;
	profile_bright[1,7] = 84;
	profile_bright[1,8] = 84;
	profile_delay[1] = 0;
	
	profile_name[2] = "LED 25%";
	profile_bright[2,1] = 4;
	profile_bright[2,2] = 7;
	profile_bright[2,3] = 10;
	profile_bright[2,4] = 13;
	profile_bright[2,5] = 16;
	profile_bright[2,6] = 19;
	profile_bright[2,7] = 22;
	profile_bright[2,8] = 25;
	profile_delay[2] = 4;
	
	profile_name[3] = "LED 35%";
	profile_bright[3,1] = 4;
	profile_bright[3,2] = 8;
	profile_bright[3,3] = 12;
	profile_bright[3,4] = 17;
	profile_bright[3,5] = 21;
	profile_bright[3,6] = 26;
	profile_bright[3,7] = 30;
	profile_bright[3,8] = 35;
	profile_delay[3] = 4;
	
	profile_name[4] = "LED 45%";
	profile_bright[4,1] = 6;
	profile_bright[4,2] = 11;
	profile_bright[4,3] = 17;
	profile_bright[4,4] = 22;
	profile_bright[4,5] = 28;
	profile_bright[4,6] = 33;
	profile_bright[4,7] = 39;
	profile_bright[4,8] = 45;
	profile_delay[4] = 4;
	
	profile_name[5] = "LED 55%";
	profile_bright[5,1] = 8;
	profile_bright[5,2] = 14;
	profile_bright[5,3] = 21;
	profile_bright[5,4] = 28;
	profile_bright[5,5] = 34;
	profile_bright[5,6] = 41;
	profile_bright[5,7] = 48;
	profile_bright[5,8] = 55;
	profile_delay[5] = 4;
	
	profile_name[6] = "LED 70%";
	profile_bright[6,1] = 10;
	profile_bright[6,2] = 18;
	profile_bright[6,3] = 27;
	profile_bright[6,4] = 35;
	profile_bright[6,5] = 44;
	profile_bright[6,6] = 52;
	profile_bright[6,7] = 61;
	profile_bright[6,8] = 70;
	profile_delay[6] = 4;
	
	profile_name[7] = "LED 85%";
	profile_bright[7,1] = 10;
	profile_bright[7,2] = 20;
	profile_bright[7,3] = 30;
	profile_bright[7,4] = 40;
	profile_bright[7,5] = 50;
	profile_bright[7,6] = 60;
	profile_bright[7,7] = 70;
	profile_bright[7,8] = 85;
	profile_delay[7] = 4;
	
	profile_name[8] = "LED 100%";
	profile_bright[8,1] = 20;
	profile_bright[8,2] = 31;
	profile_bright[8,3] = 42;
	profile_bright[8,4] = 54;
	profile_bright[8,5] = 65;
	profile_bright[8,6] = 77;
	profile_bright[8,7] = 88;
	profile_bright[8,8] = 100;
	profile_delay[8] = 4;
	
	for (item = 1; item<=88; item++) {
		lamp_profile[item]=7;
	}

}

/^menu-lamp/ {
	#menu-lamp##
	lamp = substr($1,10)+0;
	#Profile # - xxxx
	lamp_profile[lamp] = substr($2, 9, 1);
}

/^menu-game/ { 
	val = $2;
	gsub(/\+/, " ", val);
	gsub(/\%21/, "!", val);
	gsub(/\%22/, "\"", val);
	gsub(/\%23/, "#", val);
	gsub(/\%24/, "$", val);
	gsub(/\%26/, "\\&", val);
	gsub(/\%27/, "'", val);
	gsub(/\%28/, "(", val);
	gsub(/\%29/, ")", val);
	gsub(/\%2A/, "*", val);
	gsub(/\%2B/, "+", val);
	gsub(/\%2C/, ",", val);
	gsub(/\%2F/, "/", val);
	gsub(/\%3A/, ":", val);
	gsub(/\%3B/, ";", val);
	gsub(/\%3C/, "<", val);
	gsub(/\%3D/, "=", val);
	gsub(/\%3E/, ">", val);
	gsub(/\%3F/, "?", val);
	gsub(/\%40/, "@", val);
	gsub(/\%5B/, "[", val);
	gsub(/\%5C/, "\\", val);
	gsub(/\%5D/, "]", val);
	gsub(/\%5E/, "^", val);
	gsub(/\%60/, "`", val);
	gsub(/\%7B/, "{", val);
	gsub(/\%7C/, "|", val);
	gsub(/\%7D/, "}", val);
	gsub(/\%7E/, "~", val);
	gsub(/\%25/, "%", val);
	
	for(i=1;i<=numdatafiles;i++) {
		if(datafilename[i] == val) {
			gameindex = i; 
		}
	}
}

/^txt-profile1-name/ {
	val = $2;
	gsub(/\+/, " ", val);
	gsub(/\%21/, "!", val);
	gsub(/\%22/, "\"", val);
	gsub(/\%23/, "#", val);
	gsub(/\%24/, "$", val);
	gsub(/\%26/, "\\&", val);
	gsub(/\%27/, "'", val);
	gsub(/\%28/, "(", val);
	gsub(/\%29/, ")", val);
	gsub(/\%2A/, "*", val);
	gsub(/\%2B/, "+", val);
	gsub(/\%2C/, ",", val);
	gsub(/\%2F/, "/", val);
	gsub(/\%3A/, ":", val);
	gsub(/\%3B/, ";", val);
	gsub(/\%3C/, "<", val);
	gsub(/\%3D/, "=", val);
	gsub(/\%3E/, ">", val);
	gsub(/\%3F/, "?", val);
	gsub(/\%40/, "@", val);
	gsub(/\%5B/, "[", val);
	gsub(/\%5C/, "\\", val);
	gsub(/\%5D/, "]", val);
	gsub(/\%5E/, "^", val);
	gsub(/\%60/, "`", val);
	gsub(/\%7B/, "{", val);
	gsub(/\%7C/, "|", val);
	gsub(/\%7D/, "}", val);
	gsub(/\%7E/, "~", val);
	gsub(/\%25/, "%", val);
	profile_name[1] = val;
}

/^txt-profile2-name/ {
	val = $2;
	gsub(/\+/, " ", val);
	gsub(/\%21/, "!", val);
	gsub(/\%22/, "\"", val);
	gsub(/\%23/, "#", val);
	gsub(/\%24/, "$", val);
	gsub(/\%26/, "\\&", val);
	gsub(/\%27/, "'", val);
	gsub(/\%28/, "(", val);
	gsub(/\%29/, ")", val);
	gsub(/\%2A/, "*", val);
	gsub(/\%2B/, "+", val);
	gsub(/\%2C/, ",", val);
	gsub(/\%2F/, "/", val);
	gsub(/\%3A/, ":", val);
	gsub(/\%3B/, ";", val);
	gsub(/\%3C/, "<", val);
	gsub(/\%3D/, "=", val);
	gsub(/\%3E/, ">", val);
	gsub(/\%3F/, "?", val);
	gsub(/\%40/, "@", val);
	gsub(/\%5B/, "[", val);
	gsub(/\%5C/, "\\", val);
	gsub(/\%5D/, "]", val);
	gsub(/\%5E/, "^", val);
	gsub(/\%60/, "`", val);
	gsub(/\%7B/, "{", val);
	gsub(/\%7C/, "|", val);
	gsub(/\%7D/, "}", val);
	gsub(/\%7E/, "~", val);
	gsub(/\%25/, "%", val);
	profile_name[2] = val;
}

/^txt-profile3-name/ {
	val = $2;
	gsub(/\+/, " ", val);
	gsub(/\%21/, "!", val);
	gsub(/\%22/, "\"", val);
	gsub(/\%23/, "#", val);
	gsub(/\%24/, "$", val);
	gsub(/\%26/, "\\&", val);
	gsub(/\%27/, "'", val);
	gsub(/\%28/, "(", val);
	gsub(/\%29/, ")", val);
	gsub(/\%2A/, "*", val);
	gsub(/\%2B/, "+", val);
	gsub(/\%2C/, ",", val);
	gsub(/\%2F/, "/", val);
	gsub(/\%3A/, ":", val);
	gsub(/\%3B/, ";", val);
	gsub(/\%3C/, "<", val);
	gsub(/\%3D/, "=", val);
	gsub(/\%3E/, ">", val);
	gsub(/\%3F/, "?", val);
	gsub(/\%40/, "@", val);
	gsub(/\%5B/, "[", val);
	gsub(/\%5C/, "\\", val);
	gsub(/\%5D/, "]", val);
	gsub(/\%5E/, "^", val);
	gsub(/\%60/, "`", val);
	gsub(/\%7B/, "{", val);
	gsub(/\%7C/, "|", val);
	gsub(/\%7D/, "}", val);
	gsub(/\%7E/, "~", val);
	gsub(/\%25/, "%", val);
	profile_name[3] = val;
}

/^txt-profile4-name/ {
	val = $2;
	gsub(/\+/, " ", val);
	gsub(/\%21/, "!", val);
	gsub(/\%22/, "\"", val);
	gsub(/\%23/, "#", val);
	gsub(/\%24/, "$", val);
	gsub(/\%26/, "\\&", val);
	gsub(/\%27/, "'", val);
	gsub(/\%28/, "(", val);
	gsub(/\%29/, ")", val);
	gsub(/\%2A/, "*", val);
	gsub(/\%2B/, "+", val);
	gsub(/\%2C/, ",", val);
	gsub(/\%2F/, "/", val);
	gsub(/\%3A/, ":", val);
	gsub(/\%3B/, ";", val);
	gsub(/\%3C/, "<", val);
	gsub(/\%3D/, "=", val);
	gsub(/\%3E/, ">", val);
	gsub(/\%3F/, "?", val);
	gsub(/\%40/, "@", val);
	gsub(/\%5B/, "[", val);
	gsub(/\%5C/, "\\", val);
	gsub(/\%5D/, "]", val);
	gsub(/\%5E/, "^", val);
	gsub(/\%60/, "`", val);
	gsub(/\%7B/, "{", val);
	gsub(/\%7C/, "|", val);
	gsub(/\%7D/, "}", val);
	gsub(/\%7E/, "~", val);
	gsub(/\%25/, "%", val);
	profile_name[4] = val;
}

/^txt-profile5-name/ {
	val = $2;
	gsub(/\+/, " ", val);
	gsub(/\%21/, "!", val);
	gsub(/\%22/, "\"", val);
	gsub(/\%23/, "#", val);
	gsub(/\%24/, "$", val);
	gsub(/\%26/, "\\&", val);
	gsub(/\%27/, "'", val);
	gsub(/\%28/, "(", val);
	gsub(/\%29/, ")", val);
	gsub(/\%2A/, "*", val);
	gsub(/\%2B/, "+", val);
	gsub(/\%2C/, ",", val);
	gsub(/\%2F/, "/", val);
	gsub(/\%3A/, ":", val);
	gsub(/\%3B/, ";", val);
	gsub(/\%3C/, "<", val);
	gsub(/\%3D/, "=", val);
	gsub(/\%3E/, ">", val);
	gsub(/\%3F/, "?", val);
	gsub(/\%40/, "@", val);
	gsub(/\%5B/, "[", val);
	gsub(/\%5C/, "\\", val);
	gsub(/\%5D/, "]", val);
	gsub(/\%5E/, "^", val);
	gsub(/\%60/, "`", val);
	gsub(/\%7B/, "{", val);
	gsub(/\%7C/, "|", val);
	gsub(/\%7D/, "}", val);
	gsub(/\%7E/, "~", val);
	gsub(/\%25/, "%", val);
	profile_name[5] = val;
}

/^txt-profile6-name/ {
	val = $2;
	gsub(/\+/, " ", val);
	gsub(/\%21/, "!", val);
	gsub(/\%22/, "\"", val);
	gsub(/\%23/, "#", val);
	gsub(/\%24/, "$", val);
	gsub(/\%26/, "\\&", val);
	gsub(/\%27/, "'", val);
	gsub(/\%28/, "(", val);
	gsub(/\%29/, ")", val);
	gsub(/\%2A/, "*", val);
	gsub(/\%2B/, "+", val);
	gsub(/\%2C/, ",", val);
	gsub(/\%2F/, "/", val);
	gsub(/\%3A/, ":", val);
	gsub(/\%3B/, ";", val);
	gsub(/\%3C/, "<", val);
	gsub(/\%3D/, "=", val);
	gsub(/\%3E/, ">", val);
	gsub(/\%3F/, "?", val);
	gsub(/\%40/, "@", val);
	gsub(/\%5B/, "[", val);
	gsub(/\%5C/, "\\", val);
	gsub(/\%5D/, "]", val);
	gsub(/\%5E/, "^", val);
	gsub(/\%60/, "`", val);
	gsub(/\%7B/, "{", val);
	gsub(/\%7C/, "|", val);
	gsub(/\%7D/, "}", val);
	gsub(/\%7E/, "~", val);
	gsub(/\%25/, "%", val);
	profile_name[6] = val;
}

/^txt-profile7-name/ {
	val = $2;
	gsub(/\+/, " ", val);
	gsub(/\%21/, "!", val);
	gsub(/\%22/, "\"", val);
	gsub(/\%23/, "#", val);
	gsub(/\%24/, "$", val);
	gsub(/\%26/, "\\&", val);
	gsub(/\%27/, "'", val);
	gsub(/\%28/, "(", val);
	gsub(/\%29/, ")", val);
	gsub(/\%2A/, "*", val);
	gsub(/\%2B/, "+", val);
	gsub(/\%2C/, ",", val);
	gsub(/\%2F/, "/", val);
	gsub(/\%3A/, ":", val);
	gsub(/\%3B/, ";", val);
	gsub(/\%3C/, "<", val);
	gsub(/\%3D/, "=", val);
	gsub(/\%3E/, ">", val);
	gsub(/\%3F/, "?", val);
	gsub(/\%40/, "@", val);
	gsub(/\%5B/, "[", val);
	gsub(/\%5C/, "\\", val);
	gsub(/\%5D/, "]", val);
	gsub(/\%5E/, "^", val);
	gsub(/\%60/, "`", val);
	gsub(/\%7B/, "{", val);
	gsub(/\%7C/, "|", val);
	gsub(/\%7D/, "}", val);
	gsub(/\%7E/, "~", val);
	gsub(/\%25/, "%", val);
	profile_name[7] = val;
}

/^txt-profile8-name/ {
	val = $2;
	gsub(/\+/, " ", val);
	gsub(/\%21/, "!", val);
	gsub(/\%22/, "\"", val);
	gsub(/\%23/, "#", val);
	gsub(/\%24/, "$", val);
	gsub(/\%26/, "\\&", val);
	gsub(/\%27/, "'", val);
	gsub(/\%28/, "(", val);
	gsub(/\%29/, ")", val);
	gsub(/\%2A/, "*", val);
	gsub(/\%2B/, "+", val);
	gsub(/\%2C/, ",", val);
	gsub(/\%2F/, "/", val);
	gsub(/\%3A/, ":", val);
	gsub(/\%3B/, ";", val);
	gsub(/\%3C/, "<", val);
	gsub(/\%3D/, "=", val);
	gsub(/\%3E/, ">", val);
	gsub(/\%3F/, "?", val);
	gsub(/\%40/, "@", val);
	gsub(/\%5B/, "[", val);
	gsub(/\%5C/, "\\", val);
	gsub(/\%5D/, "]", val);
	gsub(/\%5E/, "^", val);
	gsub(/\%60/, "`", val);
	gsub(/\%7B/, "{", val);
	gsub(/\%7C/, "|", val);
	gsub(/\%7D/, "}", val);
	gsub(/\%7E/, "~", val);
	gsub(/\%25/, "%", val);
	profile_name[8] = val;
}

/^p1-b1/ {
	profile_bright[1,1] = $2;
}

/^p1-b2/ {
	profile_bright[1,2] = $2;
}

/^p1-b3/ {
	profile_bright[1,3] = $2;
}

/^p1-b4/ {
	profile_bright[1,4] = $2;
}

/^p1-b5/ {
	profile_bright[1,5] = $2;
}

/^p1-b6/ {
	profile_bright[1,6] = $2;
}

/^p1-b7/ {
	profile_bright[1,7] = $2;
}

/^p1-b8/ {
	profile_bright[1,8] = $2;
}

/^p1-delay/ {
	profile_delay[1] = $2;
}

/^p2-b1/ {
	profile_bright[2,1] = $2;
}

/^p2-b2/ {
	profile_bright[2,2] = $2;
}

/^p2-b3/ {
	profile_bright[2,3] = $2;
}

/^p2-b4/ {
	profile_bright[2,4] = $2;
}

/^p2-b5/ {
	profile_bright[2,5] = $2;
}

/^p2-b6/ {
	profile_bright[2,6] = $2;
}

/^p2-b7/ {
	profile_bright[2,7] = $2;
}

/^p2-b8/ {
	profile_bright[2,8] = $2;
}

/^p2-delay/ {
	profile_delay[2] = $2;
}

/^p3-b1/ {
	profile_bright[3,1] = $2;
}

/^p3-b2/ {
	profile_bright[3,2] = $2;
}

/^p3-b3/ {
	profile_bright[3,3] = $2;
}

/^p3-b4/ {
	profile_bright[3,4] = $2;
}

/^p3-b5/ {
	profile_bright[3,5] = $2;
}

/^p3-b6/ {
	profile_bright[3,6] = $2;
}

/^p3-b7/ {
	profile_bright[3,7] = $2;
}

/^p3-b8/ {
	profile_bright[3,8] = $2;
}

/^p3-delay/ {
	profile_delay[3] = $2;
}

/^p4-b1/ {
	profile_bright[4,1] = $2;
}

/^p4-b2/ {
	profile_bright[4,2] = $2;
}

/^p4-b3/ {
	profile_bright[4,3] = $2;
}

/^p4-b4/ {
	profile_bright[4,4] = $2;
}

/^p4-b5/ {
	profile_bright[4,5] = $2;
}

/^p4-b6/ {
	profile_bright[4,6] = $2;
}

/^p4-b7/ {
	profile_bright[4,7] = $2;
}

/^p4-b8/ {
	profile_bright[4,8] = $2;
}

/^p4-delay/ {
	profile_delay[4] = $2;
}

/^p5-b1/ {
	profile_bright[5,1] = $2;
}

/^p5-b2/ {
	profile_bright[5,2] = $2;
}

/^p5-b3/ {
	profile_bright[5,3] = $2;
}

/^p5-b4/ {
	profile_bright[5,4] = $2;
}

/^p5-b5/ {
	profile_bright[5,5] = $2;
}

/^p5-b6/ {
	profile_bright[5,6] = $2;
}

/^p5-b7/ {
	profile_bright[5,7] = $2;
}

/^p5-b8/ {
	profile_bright[5,8] = $2;
}

/^p5-delay/ {
	profile_delay[5] = $2;
}

/^p6-b1/ {
	profile_bright[6,1] = $2;
}

/^p6-b2/ {
	profile_bright[6,2] = $2;
}

/^p6-b3/ {
	profile_bright[6,3] = $2;
}

/^p6-b4/ {
	profile_bright[6,4] = $2;
}

/^p6-b5/ {
	profile_bright[6,5] = $2;
}

/^p6-b6/ {
	profile_bright[6,6] = $2;
}

/^p6-b7/ {
	profile_bright[6,7] = $2;
}

/^p6-b8/ {
	profile_bright[6,8] = $2;
}

/^p6-delay/ {
	profile_delay[6] = $2;
}

/^p7-b1/ {
	profile_bright[7,1] = $2;
}

/^p7-b2/ {
	profile_bright[7,2] = $2;
}

/^p7-b3/ {
	profile_bright[7,3] = $2;
}

/^p7-b4/ {
	profile_bright[7,4] = $2;
}

/^p7-b5/ {
	profile_bright[7,5] = $2;
}

/^p7-b6/ {
	profile_bright[7,6] = $2;
}

/^p7-b7/ {
	profile_bright[7,7] = $2;
}

/^p7-b8/ {
	profile_bright[7,8] = $2;
}

/^p7-delay/ {
	profile_delay[7] = $2;
}

/^p8-b1/ {
	profile_bright[8,1] = $2;
}

/^p8-b2/ {
	profile_bright[8,2] = $2;
}

/^p8-b3/ {
	profile_bright[8,3] = $2;
}

/^p8-b4/ {
	profile_bright[8,4] = $2;
}

/^p8-b5/ {
	profile_bright[8,5] = $2;
}

/^p8-b6/ {
	profile_bright[8,6] = $2;
}

/^p8-b7/ {
	profile_bright[8,7] = $2;
}

/^p8-b8/ {
	profile_bright[8,8] = $2;
}

/^p8-delay/ {
	# the +0 coverts to a number, which seems necessary for this one value
	profile_delay[8] = $2+0;
}


END {
	
	
	print "<script type='text/javascript'>";
	print "</script>";
	printf "<script type='text/javascript' src='%s'></script>\n", JQPLUGINPATH;
	print "</head>";

	print "<body>";
	print "<form id='config-form' data-ajax='false' action='ledocd.html' method='post'/>";

	#############################################################################
	# Home
	#############################################################################
	
	print "<div data-role='page' id='home' data-theme='b'>";
		print "<div data-role='header' data-theme='b'>";
			printf "<a href='ledocd.html' class='button' data-icon='home' data-theme='b'>%s</a>\n", "Home";
			printf "<h1>%s</h1>\n", constants["titlehome"];
			printf "<a href='#' data-icon='check' data-theme='b' class='submitButton'>%s</a>\n", constants["lblsubmit"];
		print "</div>"; #end header

		print "<div data-role='content'>";

			printf "<p>%s</p>\n", "Game Select";
			printf "<select name='menu-game' id='menu-game' class='gameMenu'>"
			for(i=1; i<=numdatafiles; i++) {
				selected = "";
				if(gameindex==0 && datafilename[i]=="-Undefined Stern")
				{
					gameindex=i;
				}
				if(i==gameindex) {
					selected = "selected='selected'";
				}
				printf "<option %s>%s</option>", selected, datafilename[i]
			}
			printf "</select>"
			printf "<input type='hidden' name='txt-gametype' id='txt-gametype' value='%s'/>\n", datafiletype[gameindex];

			printf "<tr><td>Port</td><td width="150"><input type='text' name='txt-port' id='txt-port' value='%s'/></td></tr>\n", port;
			printf "<input type='button' data-theme='b' value='%s' class='testConnectionButton'/>\n", "Version";

			printf "<input type='button' data-theme='b' value='Read' class='readButton'/>\n";
			printf "<a href='#profiles' data-role='button' data-icon='arrow-r' data-iconpos='right'>Profiles</a>\n";
			printf "<a href='#lamps' data-role='button' data-icon='arrow-r' data-iconpos='right'>Lamps</a>\n";

			#printf "<input type='button' data-theme='b' value='Export...' class='exportButton'/>\n";
			#printf "<input type='button' data-theme='b' value='Import...' class='importButton'/>\n";
			printf "<input type='button' data-theme='b' value='Send' class='sendButton'/>\n";
			printf "<input type='button' data-theme='b' value='Save' class='saveButton'/>\n";
			#printf "<input type='button' data-theme='b' value='Manual Test' class='manualTestButton'/>\n";
			printf "<input type='button' data-theme='b' value='Passthrough' class='passthroughButton'/>\n";
			printf "<input type='button' data-theme='b' value='Normal OCD' class='normalOCDButton'/>\n";
			printf "<input type='button' data-theme='b' value='Checkerboard' class='checkButton'/>\n";

		print "</div>"; #end content

		print "<div data-role='footer' data-theme='b'>";
			printf "<h6>%s</h6>\n", constants["copyright"];
		print "</div>"; #end footer
	print "</div>"; #end page

	#############################################################################
	# Lamp Settings
	#############################################################################
	
	printf "<div data-role='page' id='lamps'>\n";
		printf "<div data-role='header' data-theme='b'>\n";
			printf "<a href='ledocd.html' class='button' data-icon='home' data-theme='b'>%s</a>\n", "Home";
			printf "<h1>%s</h1>\n", constants["titlehome"];
			printf "<a href='#' data-icon='check' data-theme='b' class='submitButton'>%s</a>\n", constants["lblsubmit"];
		printf "</div>\n"; #end header
		printf "<div data-role='content'>\n";
			printf "<h4>Lamp Settings</h4>\n";
				for (lamp=1; lamp<=numlamps_ra[gameindex]; lamp++) {
					if(length(lampname_ra[gameindex, lamp]) > 1) {
						#printf "<p>Lamp %s - %s</p>\n", lampnumber_ra[gameindex, lamp], lampname_ra[gameindex, lamp];
						printf "<label for='menu-lamp%s' class='select'>Lamp %s - %s</label>", lampnumber_ra[gameindex, lamp], lampnumber_ra[gameindex, lamp], lampname_ra[gameindex, lamp];
						printf "<select name='menu-lamp%s' id='menu-lamp%s' data-theme='b'>", lampnumber_ra[gameindex, lamp], lampnumber_ra[gameindex, lamp];
							for (profile=1; profile<=8; profile++) {
								selected= "";
								if (profile == lamp_profile[lampnumber_ra[gameindex, lamp]+0]) {
									selected = "selected='selected'";
								}
								printf "<option %s>Profile %s - %s</option>", selected, profile, profile_name[profile];
							}
						printf "</select>\n"
					}
				}
		printf "</div>\n"; #end content
		printf "<div data-role='footer' data-theme='b'>\n";
			printf "<h6>%s</h6>\n", constants["copyright"];
		printf "</div>\n"; #end footer
	printf "</div>\n"; #end page

	#############################################################################
	# Profile Settings
	#############################################################################
	
	printf "<div data-role='page' id='profiles'>\n";
		printf "<div data-role='header' data-theme='b'>\n";
			printf "<a href='ledocd.html' class='button' data-icon='home' data-theme='b'>%s</a>\n", "Home";
			printf "<h1>%s</h1>\n", constants["titlehome"];
			printf "<a href='#' data-icon='check' data-theme='b' class='submitButton'>%s</a>\n", constants["lblsubmit"];
		printf "</div>\n"; #end header
		printf "<div data-role='content'>\n";
			printf "<h4>Profile Settings</h4>\n";
                                printf "<div data-role='collapsible-set' data-iconpos='right' data-theme='b'>"
 				for (profile=1; profile<=8; profile++) {
			  	printf "<div data-role='collapsible' data-iconpos='right' data-inset='false' data-theme='b'>"
    			printf "<h2>Profile %s - %s</h2>", profile, profile_name[profile];
						printf "<tr><td>Name</td><td width="150"><input type='text' name='txt-profile%s-name' id='txt-profile%s-name' value='%s'/></td></tr>\n", profile, profile, profile_name[profile];
						for (bright=1; bright<=8; bright++) {
							printf "<div id='profile%s-b%s-slider'>\n", profile, bright;
							printf "<label for='profile%s-b%s-slider'>B%s:</label>\n", profile, bright, bright;
							printf "<input type='range' name='p%s-b%s' id='p%s-b%s' data-highlight='true' value='%s' min='0' max='100' data-track-theme='b'/>\n", profile, bright, profile, bright, profile_bright[profile, bright];
							printf "</div>\n";
						}
						printf "<div id='profile%s-delay-slider'>\n", profile;
						printf "<label for='profile%s-delay-slider'>DL:</label>\n", profile;
						printf "<input type='range' name='p%s-delay' id='p%s-delay' data-highlight='true' value='%s' min='0' max='9' data-track-theme='b'/>\n", profile, profile, profile_delay[profile];
						printf "</div>\n";
				  print "</div>"
				}
                                  print "</div>"
		printf "</div>\n"; #end content
		printf "<div data-role='footer' data-theme='b'>\n";
			printf "<h6>%s</h6>\n", constants["copyright"];
		printf "</div>\n"; #end footer
	printf "</div>\n"; #end page


        #############################################################################
        # Busy Dialog
        #############################################################################

	printf "<div data-role='page' id='busyDialog' data-role='page' data-theme='b'>"
		printf "<div data-role='header' data-backbtn='false' data-close-btn='none' data-theme='b'>"
			printf "<h1>Busy</h1>"
		printf "</div>"
		printf "<div data-role='content' data-theme='b'>";
			printf "The software is working on your request.  Please be patient.";
		printf "</div>";
	printf "</div>";

	#############################################################################

	print "</form>";
	print "</body>";
	print "</html>";
}
