
<%+header%> 

<script type="text/javascript">//<![CDATA[

function init_page()
{
	var firmware = document.getElementById("firmware");
	var length = firmware.options.length;
	firmware.selectedIndex = 0;
	for (i = 1; i < length; i++) {
		firmware.remove(1);
	}
	var table = document.getElementById('dot');
	for ( i=0;i<table.rows.length;i++){
		for(j=0;j<table.rows[i].cells.length;j++){
			table.rows[i].deleteCell(0);
		}
	}

	var updatebtn = document.getElementById('updatebtn');
	var checkbtn = document.getElementById('checkbtn');
	var stxhr = new XHR();
	stxhr.get('<%=luci.dispatcher.build_url("admin", "services", "sierraupdate")%>/sierraupdate_init' , null,
		function(x, mArray)
		{
		}
		);
	updatebtn.disabled = true;
	checkbtn.disabled = false;
	firmware.disabled = true;
} 

function toggleupdate()
{
	var updatebtn = document.getElementById('updatebtn');
	var firmware = document.getElementById("firmware");
	if(firmware.selectedIndex==0)
		updatebtn.disabled = true;
	else{
		updatebtn.disabled = false;
	}
} 

function check_firmware()
{
	var i=0;
	var updatebtn = document.getElementById('updatebtn');
	updatebtn.disabled=true;
	var checkbtn = document.getElementById('checkbtn');
	checkbtn.disabled = true;
	var firmware = document.getElementById("firmware");
	var length = firmware.options.length;
	firmware.selectedIndex = 0;
	for (i = 1; i < length; i++) {
		firmware.remove(1);
	}
	var table = document.getElementById('dot');
	for ( i=0;i<table.rows.length;i++){
		for(j=0;j<table.rows[i].cells.length;j++){
			table.rows[i].deleteCell(0);
		}
	}


	var channel = document.getElementById('channel');
	var channelval;
	var channelval=channel.value;
	var stxhr = new XHR();
	stxhr.get('<%=luci.dispatcher.build_url("admin", "services", "sierraupdate")%>/sierraupdate_update' + '/' + "M" + '/' + "check" + '/' + channelval + '/' + "NULL", null,
		function(x, mArray)
		{
			if (mArray.length > 0)
			{
				if(mArray.length && mArray.length==1)
				{
					alert(mArray.firmwares[0]);
				}else{
					var min = 1;
					var max=mArray.length;
					var latest=mArray.firmwares[0];
					for (i = min; i < max; i++) {
						var opt = document.createElement('option');
						opt.value = mArray.firmwares[i];
						opt.innerHTML = mArray.firmwares[i];
						firmware.appendChild(opt);
					}
					if( latest=="new"){
						firmware.selectedIndex = 1;
						updatebtn.disabled=false;
					}else{
						firmware.selectedIndex = 0;
					}
				}
			}else{
				alert("Could not finish checking.");
			}
		}
		);
	checkbtn.disabled = false;
	firmware.disabled = false;
}
function update_firmware()
{
	var channel = document.getElementById('channel');
	var channelval=channel.value;
	var firmware = document.getElementById('firmware');
	var filename=firmware.value;
	var updatebtn = document.getElementById('updatebtn');
	// var forceupgrade = document.getElementById('forceupgrade').checked;

	if (filename != "NULL") {
		firmware.disabled = true;
		var checkbtn = document.getElementById('checkbtn');
		checkbtn.disabled = true;
		channel.disabled=true;
		updatebtn.disabled=true;
		// if (forceupgrade){
		// 	upgrade="fupdate"
		// }else{
		// 	upgrade="update"
		// }
		upgrade="update"
		var stxhr = new XHR();
		stxhr.get('<%=luci.dispatcher.build_url("admin", "services", "sierraupdate")%>/sierraupdate_update' + '/' + "M" + '/' + upgrade + '/' + channelval + '/' + filename , null,
			function(x, mArray)
			{
				if (mArray && mArray.length && mArray.length > 0)
				{
					var min = 0;
					var data="";
					var max=mArray.length;
					for (i = min; i < max; i++) {
						data = data + mArray.firmwares[i] + ".";
					}
					alert(data);
				}else if (mArray && mArray.length){
					alert("Could not finish updating.");
				}  
			}
			);
	}else{
		alert("Please select Proper firmware to upload");
	}
}

XHR.poll(10, '<%=luci.dispatcher.build_url("admin", "services", "sierraupdate","sierraupdate_status" )%>', { status: 1 },
	function(x, mArray)
	{
		console.log("2:",mArray.data);
		var data="";
		if (mArray && mArray.length && mArray.length > 0)
		{
			var min = 0;
			var max=mArray.length;
			var max_p=0;
			var table = document.getElementById('dot');
			for(var i=0;i<table.rows.length;i++) {
				max_p += table.rows[i].cells.length;
			}
			for (i = max_p; i < max ; i++) {
				var row = table.insertRow(0);
				var cell = row.insertCell(0);
				cell.innerHTML = mArray.data[i];
			}
		}
	}
	); 
	//]]></script> 
	<body onload="init_page()">
		<!-- <h2><a>Download Over The Air</a></h2> -->
		<!-- <div >From Lantronix Wireless Server</div> -->
		<div id="buttoncss" align="center">
			<table class="cbi-section-table" style="width:940px; margin-left:5px">
				<tr class="cbi-section-table-descr">
					<th class="cbi-section-table-cell" style="width:100px"><%:Type of upgrade%></th>
					<th class="cbi-section-table-cell" style="width:100px"><%:Check for update%></th>
					<th class="cbi-section-table-cell" style="width:350px"><%:Available Firmwares%></th>
					<!-- <th class="cbi-section-table-cell" style="width:50px"><%:Force Upgrade%></th> -->
					<th class="cbi-section-table-cell" style="width:100px"><%:Upgrade%></th>
				</tr>
				<tr>
					<td>
						<select id="channel" onchange="init_page()" class="cbi-input-select" name="channel">
							<option value="sierra_fw" selected="selected">Firmware</option>
							<option value="volte_patches">VOLTE Patch</option>
						</select>
					</td>
					<td>
						<input  id="checkbtn" onclick="check_firmware()" type="button" value="<%:Check for update%>" class="cbi-button cbi-button-apply"/>
					</td>   
					<td>
						<select id="firmware" onchange="toggleupdate()" disabled  style="width:350px" class="cbi-input-select" name="firmware">
							<option value="NULL" selected="selected">Please Select file</option>
						</select>
					</td>
<!-- 					<td>
						<input  id="forceupgrade" type="checkbox"/>
					</td>  
 -->					<td>
						<input id="updatebtn" onclick="update_firmware()" type="button" disabled value="<%:Update now%>" class="cbi-button cbi-button-apply"/>
					</td>
				</tr>
			</table>
		</div>

		<fieldset class="cbi-section">
			<table width="100%" cellspacing="10" id="dot">
				<tr></tr>
			</table>
		</fieldset>
	</body>

<%+footer%> 

