#!/Lighty2Go/Python27/python
# -*- coding: utf-8 -*-

#import datetime
#now = datetime.datetime.now()
#import cgi
#import sys
import os


def mk_home_link ():
	text = '''
	<p>
	<a href="//localhost/Lighty2Go/HTDOCS/mobiledevice/mdt.php">
	Return to Mobile Device Home Page</a><br>
	</p>'''
	return text
#	href="http://xena.sdct.nist.gov/federated-testing/mdt-start.html">
#	<a href="//localhost/mdt-start.html">

s_quest = {
1:('Acquisition',(0,'Acquire All'),(1,'Disrupted')),
2:('Equipment/User Data',(2,'Service Provider Name (SPN)'),(3,'ICCID'),(4,'IMSI'),(5,'MSISDN')),
3:('PIM Data',(6,'Abbreviated Dialing Numbers (ADNs)'),(7,'Last Numbers Dialed (LNDs)'),(8,'SMS Messages'),(9,'EMS Messages')),
4:('Location Related Data',(10,'LOCI'),(11,'GPRSLOCI')),
6:('Case File Data Protection',(15,'Modify Case Data')),
7:('Password Protected SIM/UICC Acquire',(16,'Acquisition of Protected SIM/UICC')),
8:('PIN/PUK Attempts',(17,'PIN attempts reported'),(18,'PUK attempts reported')),
9:('Non-ASCII Character',(19,'Non-ASCII characters')),
10:('Hashing',(20,'Hashes reported for acquired data objects'))
}


p_quest = {
1:('Acquisition ',(0,'Acquire All'),(1,'Disrupted')),
2:('Reporting',(2,'Preview-Pane'),(3,'Generated Reports')),
3:('Equipment/User Data',(4,'IMEI'),(5,'MEID/ESN'),(6,'MSISDN/MIN')),
4:('PIM Data',(7,'Contacts'),(8,'Calendar'),(9,'Memos/Notes')),
5:('Stand-alone Data Files',(18,'Audio'),(19,'Graphic'),(20,'Video'),(33,'Documents')),
6:('Call Logs',(10,'Incoming'),(11,'Outgoing'),(12,'Missed')),
7:('SMS/EMS Messages',(13,'Incoming'),(14,'Outgoing')),
8:('MMS Messages',(15,'Audio'),(16,'Graphic'),(17,'Video')),
9:('Location Data',(29,'Coordinates/Geo-tagged ')),
10:('Browser/Email Data',(26,'Visited Sites'),(27,'Bookmarks'),(28,'Email')),
11:('Social Media Data',(22,'Application 1, e.g., Facebook'),(23,'Application 2, e.g., Twitter'),(24,'Application 3, e.g., LinkedIn'),(25,'Application 4, e.g., Instagram')),
12:('Other Applications of Interest',(21,'Other Applications of Interest')),
13:('Non-Latin Character',(30,'Reported in native format')),
14:('Hashing',(31,'Case File/Individual Files')),
15:('Case File Data Protection',(32,'Modify Case Data'))
}

init_options = '''
	<td>
	<input #P# type="radio" name="%(q)s%(id)s" value="populated">
	Populated</td>
	<td>
	<input #O# type="radio" name="%(q)s%(id)s" value="omitted">
	Omitted</td>
'''
io = {}
init_options_p = init_options.replace('#P#','checked=checked').replace('#O#','')
init_options_o = init_options.replace('#O#','checked=checked').replace('#P#','')
io['populated'] = init_options_p
io['omitted'] = init_options_o
result_options = '''
	<td>
	<input #OK# type="radio" name="%(q)s%(id)s" value="ok">
	as Expected </td>
	<td>
	<input #P# type="radio" name="%(q)s%(id)s" value="part">
	Partial </td>
	<td>
	<input #NO# type="radio" name="%(q)s%(id)s" value="no">
	Not as Expected </td>
	<td>
	<input #N# type="radio" name="%(q)s%(id)s" value="na">
	N/A </td>
	'''
ck = 'checked=checked'
result_options_ok = result_options.replace('#OK#',ck).replace('#NO#','').replace('#P#','').replace('#N#','')
result_options_no = result_options.replace('#OK#','').replace('#NO#',ck).replace('#P#','').replace('#N#','')
result_options_p = result_options.replace('#OK#','').replace('#NO#','').replace('#P#',ck).replace('#N#','')
result_options_na = result_options.replace('#OK#','').replace('#NO#','').replace('#P#','').replace('#N#',ck)
ro = {}
ro['ok'] = result_options_ok
ro['no'] = result_options_no
ro['part'] = result_options_p
ro['na'] = result_options_na
data_elements = {
1:('PIM Data: Contacts/Address Book Entries',(0,'Regular Length'),(1,'Maximum Length'),
	(2,'Special Character'),(3,'Blank Name'),(4,'Regular Length, email'),
	(5,'Regular Length, graphic'),(6,'Regular Length, Address'),
	(7,'Non-Latin Entry'),(8,'Contact Groups'),(9,'Deleted Entry')),
2:('PIM Data: Calendar, Memos',(10,'Regular Length'),
	(11,'Maximum Length'),(12,'Special Character'),
	(13,'Blank Entry'),(14,'Deleted Entry')),
3:('Stand-alone data files',(39,'Audio'),(40,'Graphic'),(41,'Video'),(55,'Documents'),
	(42,'Audio – Deleted'),(43,'Graphic - Deleted'),(44,'Video - Deleted'),(56,'Documents - Deleted')),
4:('Call Logs',(15,'Incoming'),(16,'Outgoing'),(17,'Missed'),
	(18,'Incoming – Deleted'),(19,'Outgoing – Deleted'),(20,'Missed   - Deleted')),
5:('SMS/EMS Messages',(21,'Incoming SMS – Read'),(22,'Incoming SMS – Unread'),
	(27,'Incoming SMS – Deleted'),(24,'Incoming EMS – Read'),(25,'Incoming EMS – Unread'),
	(29,'Incoming EMS – Deleted'),(23,'Outgoing SMS'),(31,'Outgoing Group SMS'),
	(28,'Outgoing SMS – Deleted'),(26,'Outgoing EMS'),(54,'Outgoing Group EMS'),
	(30,'Outgoing EMS – Deleted')
	),
6:('MMS Messages',(32,'Incoming Audio'),(33,'Incoming Graphic'),
	(34,'Incoming Video'),(35,'Outgoing Audio'),(36,'Outgoing Graphic'),
	(37,'Outgoing Video')),
7:('Location Data',(48,'GPS Coordinates'),(49,'Geo-tagged Data')),
8:('Browser/Email Data',(45,'Visited Sites'),(46,'Bookmarks'),(47,'Email')),
9:('Social Media Data',(50,'Application 1, e.g., Facebook'),(51,'Application 2, e.g., Twitter'),(52,'Application 3, e.g., LinkedIn'),(53,'Application 4, e.g., Instagram')),
10:('Other Applications of Interest',(38,'Other Applications of Interest'))
}

sim_elements = {
1:('Abbreviated Dialing Numbers (ADN)',(0,'Regular Length'),(1,'Maximum Length'),(2,'Special Character'),
	(3,'Blank Name'),(4,'Non-ASCII Entry')),
2:('Call Logs',(5,'Last Numbers Dialed (LND)')),
3:('Text Messages',(6,'Incoming SMS - Read'),(7,'Incoming SMS - Unread'),
	(8,'Incoming SMS - Non-ASCII'),(9,'Incoming SMS - Deleted'),(10,'Incoming EMS - Read'),
	(11,'Incoming EMS - Unread'),(12,'Incoming EMS - Non-ASCII'),(13,'Incoming EMS - Deleted'))
}



def mk_question (q,ix,options,extra,default,p,add_notes,suffix,ccc):
	print '<tr>'
	row = q[ix][0]
	subs = q[ix][1:]
	n = len(subs)

	plus = extra
	if add_notes:
		print '<td rowspan=%d>%s</td>'%(n+1,row)
	else:
		print '<td rowspan=%d>%s</td>'%(n,row)
	new_row = ''
	for sub in subs:
		if new_row != '': print new_row
		print '<td>%s</td>'%(sub[1])
		xx = "%02d"%(sub[0])
		zz = default+xx
		if zz in p:
			alt = options[p[zz]]%{'q':default,'id':'%(id)s'}
			plus = alt
		print plus%{'id':xx}
		new_row = '</tr><tr>'
	print '</tr>'
	if add_notes:
		print '<tr>'
		print '<td>Notes:</td><td colspan=4>'
		ixc = "comm_%s%02d"%(suffix,ix)
		print '<textarea rows=5 cols=60 wrap=soft name="comm_%s%02d" >'%(suffix,ix)
		if ixc in ccc: print ccc[ixc]
		print '</textarea></td>'
		print '</tr>'


device_results_text = '''
<h2>Record Device Results</h2>
Select the results obtained for acquiring the device.
'''

sim_results_text = '''
<h2>Record SIM/UICC Results</h2>
Select the results obtained for acquiring the SIM/UICC.
'''

setup_device_text = '''
<h2>Device Setup</h2>
Select the data elements that have been populated to the device. (The default
setting is <b>populated</b> so you only need to click on <b>omitted</b> elements.)
'''

setup_sim_text = '''
<h2>SIM/UICC Setup</h2>
Select the data elements that have been populated to the SIM/UICC in the device.
(The default
setting is <b>populated</b> so you only need to click on <b>omitted</b> elements.)
'''

def gen_lists(do_sim,p,com,is_init):
# do == True if phone has a sim card
# p is dictionary of values already entered
# com is comments already entered
# is_init is True for population data, False for results
	d_keys = data_elements.keys()
	d_keys.sort()
	p_keys = p_quest.keys()
	p_keys.sort()
	sim_keys = sim_elements.keys()
	sim_keys.sort()
	s_keys = s_quest.keys()
	s_keys.sort()
	if is_init:
		print setup_device_text
		print '<table>'
		print "<caption>"
		print "Device Data Elements Populated"
		print "</caption>"
		for ix in d_keys:
			mk_question (data_elements,ix,io,init_options_p%{
				'q':'dev_init_','id':'%(id)s'},'dev_init_',p,False,'',com)
		print '</table>'
	
	else:
		print device_results_text
		print '<br>'
		print '<table>'
		print "<caption>"
		print "Device Data Elements Acquired"
		print "</caption>"
		for ix in p_keys:
			mk_question (p_quest,ix,ro,result_options_na%{
				'q':'dev_data_','id':'%(id)s'},'dev_data_',p,True,'md_',com)
		print '</table>'

	if not do_sim: return
	if is_init:
		print '<hr>'
		print setup_sim_text
		print '<br>'
		print '<br>'
		print '<table>'
		print "<caption>"
		print "SIM/UICC Card Data Elements Populated"
		print "</caption>"
		for ix in sim_keys:
			mk_question (sim_elements,ix,io,init_options_p%{
				'q':'sim_init_','id':'%(id)s'},'sim_init_',p,False,'',com)
		print '</table>'
	
	else:
		print sim_results_text
		print '<br>'
		print '<table>'
		print "<caption>"
		print "SIM/UICC Card Data Elements Acquired"
		print "</caption>"
		for ix in s_keys:
			mk_question (s_quest,ix,ro,result_options_na%{
				'q':'sim_data_','id':'%(id)s'},'sim_data_',p,True,'sc_',com)
		print '</table>'
def gen_one_list(do_sim,p):

	if not do_sim: return
	print '<br>'
	print '<hr>'
	print '<br>'
	sim_keys = sim_elements.keys()
	sim_keys.sort()
	print '<table>'
	print "<caption>"
	print "SIM/UICC Card Data Elements Populated"
	print "</caption>"
	for ix in sim_keys:
		mk_question (sim_elements,ix,io,init_options_p%{
			'q':'sim_init_','id':'%(id)s'},'sim_init_',p,False,'',{})
	print '</table>'
	
#	mess = ""
#	if (f.toolname.value == "") mess = "Need to enter tool name\n"
#	if (f.toolversion.value == "") mess = "Need to enter tool version\n"
#	alert(mess)
#	return false
#	if (mess == "") return true
css_head = '''

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CFTT Federated Testing CD - test a  Mobile Device tool</title>
<link rel="stylesheet" type="text/css" href="../css/reset.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/main_style.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/layout.css" media="screen" />
<script>
function ck_mobile(f)
{
	var r =f.elements['selected_device'];
	if (r[r.length-1].checked) {
		mess = ''
		if (f.model.value == '') mess += '\\nYou need to enter phome model'
		if (f.make.value == '') mess += '\\nYou need to enter phome make'
		if (f.os_version.value == '') mess += '\\nYou need to enter phome OS version'
		if (f.firmware.value == '') mess += '\\nYou need to enter phome firmware'
		if (mess == '') return true
		alert (mess)
		return false
	}
	return true
}
function ck_selection(f)
{
#	ok = false
	ok = true
	var r =f.elements['selected_device'];
	for (ix = 0, lx = r.length; ix < lx; ix++){
		if (r[ix].checked) {ok = true}
	}
	if (ok) return true
	alert('No device selected, You have to select a device');
	alert(r)
	return false;
}
function ck_selection1(f)
{
	mess = 'fix this mess';
	if (f.selected_device.value == '')
	{
		alert('You need to select a device');
		return false;
	}
	mess += ':\\n'+f;
	mess += ':\\n'+f.selected_device[0].value;
	alert(mess+': '+f.selected_device.value);
	return false;
}
function ck_tool (f)
{
	mess = '';
	if (f.toolname.value == '') {mess += 'You need to enter tool name.';};
	if (f.toolversion.value == '') {mess += '\\nYou need to enter tool version.';};
	if (mess == '' ) {return true;};
	alert(mess);
	return false;
}
</script>
<style>
	table, th, td {
	   border-collapse: collapse;
	   border: 1px solid black;
	   } 
	tr {
	   border-collapse: collapse;
	   border: 2px solid black;
	   } 
</style>
</head>
<body id="menu1">
	<div id="mainWrapper">
		<div id="topBannerContainer">
			<div id="logoContainer">
  				<img src="/images/banner.png" alt="CFTT Federated Testing - NIST">
			</div>
            <!-- Menu Starts-->
			<div id="menuContainer">

 	<ul id="menu">
	 <li><a id="menu1nav" href="http://localhost/Federated_Testing_Home_Page.php">Home</a></li>
	 <li><a id="menu3nav" href="http://localhost/about.php">About</a></li>
	 <li><a id="menu3nav" href="http://localhost/mobiledevice/mobile-definitions.html">Glossary of Mobile Device Terms</a></li>
	 <li><a id="menu2nav" href="FAQ.php">FAQs</a></li>
	 <li><a id="menu4nav" href="http://localhost/contacts.php">Contacts</a></li>
	</ul>

    		</div>
            <!-- Menu Ends-->
		</div>
		<!-- TopBanner Ends-->
	  
		<!-- Main Content Starts-->	
		<div id="subContentContainer">
		  <div id="leftContent">
		    <div id="leftLinks">
<div>
	<ul>
	 <h1>Select a Mobile Device Testing Task</h1>
	<a style="font-size:1.15em;" href="http://localhost/Lighty2Go/HTDOCS/mobiledevice/mdt.php" 
	class="html"><li id="function1nav" class="leftNavBottom">
	Mobile Device Home <img src="/images/home-16x16.png" alt="Home"
	width="16" height="16"></li></a>

	<a style="font-size:1.15em;" href="http://localhost/Lighty2Go/HTDOCS/diskimaging/formatyourthumb.php"
	 class="html"><li id="function1nav" class="leftNavBottom">Format FT-LOGS Flash Drive
	</li></a>

	<a style="font-size:1.15em;" href="http://localhost/Lighty2Go/cgi-bin/ft_mdt_record_tool.py"
	class="html"><li id="function1nav" class="leftNavBottom">
	Enter tool name and version</li></a>


	<a style="font-size:1.15em;" href="http://localhost/Lighty2Go/cgi-bin/ft_mdt_edit_devices.py"
	 class="html"><li id="function1nav" class="leftNavBottom">
	Create/Edit Mobile Device List</li></a>

	 <a style="font-size:1.15em;"
	href="http://localhost/Lighty2Go/cgi-bin/ft_mdt_select_setup.py"
	class="html"><li id="function1nav" class="leftNavBottom">
	Record Device Setup</li></a>

	 <a style="font-size:1.15em;"
	href="http://localhost/Lighty2Go/cgi-bin/ft_mdt_select_results.py"
	class="html"><li id="function1nav" class="leftNavBottom">
	Record Device Test Results</li></a>

	<a style="font-size:1.15em;"
	href="http://localhost/Lighty2Go/cgi-bin/ft_mdt_prt_test_report.py"
	class="html"><li id="function1nav"
	class="leftNavBottom">Generate a Test Report</li></a>
	</ul>




		    </div>
		    </div>
		  </div>			
		  <div id="rightContent">
<!--		    <div id="breadcrumb"><a href="../Federated_Testing_Home_Page.php">Home</a> > Test a disk imaging tool</div>-->
		    <div id="breadcrumb"><a href="../Federated_Testing_Home_Page.php" class="nav">Home</a> > Mobile Device Home</div>
			

<h1>Mobile Device Testing</h1>
<div style="padding: 1em 1em 0.5em 1em; background: #eeeeee url("images/ui-bg_highlight-soft_100_eeeeee_1x100.png") 50% top repeat-x;">
'''

css_tail_base = '''
</div>		        
		</div>
		<div class="push"></div>
	</div>
    <!-- Footer Starts-->
    <!-- Footer Ends-->

	<div class="footer"><div class="footerContentsWrapper"><p>The National Institute of Standards and Technology (NIST) is an agency of
 the U.S. Commerce Department.</p><br><br><p class="links"><a href="/Federated_Testing_Home_Page.php">Home</a>
 &nbsp;/&nbsp; <a href="/about.php">About</a> &nbsp;/&nbsp;
<a href="/mobiledevice/mobile-definitions.html">Glossary of Mobile Device Terms</a>
&nbsp;/&nbsp; <a href="/FAQ.php">FAQs</a> &nbsp;/&nbsp;
<a href="/contacts.php">Contacts</a><br/><br/>Federated Testing Version:
%s<br>Release Date: %s</p></div></div>    <!-- Footer Ends-->

'''
ft_date = ''
ft_version = ''
n = 0
if os.access('/Lighty2Go/HTDOCS/include/global.php',os.F_OK):
	php_file = open('/Lighty2Go/HTDOCS/include/global.php','rt')
	for line in php_file:
		n += 1
		if line.strip().startswith('$release_date'):
			ft_date = line.split('"')[1]
		if line.strip().startswith('$version'):
			ft_version = line.split('"')[1]
		if ft_date != '' and ft_version != '': break
else: ft_date = 'No Access'
if ft_date == '': ft_date = 'Unknown'
if ft_version == '': ft_version = 'Unknown %d'%(n)
css_tail = css_tail_base%(ft_version,ft_date)

if __name__ == '__main__':
	p = {}
	p['dev_data_00'] = 'ok'
	p['dev_data_01'] = 'part'
	p['dev_data_02'] = 'no'
	p['sim_init_00'] = 'populated'
	p['sim_init_01'] = 'omitted'
	p['dev_init_00'] = 'populated'
	p['dev_init_01'] = 'omitted'
#	gen_one_list(True,p)
	gen_lists(True,p,{})
