// JScript source code
var NewWindow = '';

function isIE() {
	var browserName = navigator.appName;
	
	if (-1 == browserName.indexOf("Explorer", 0)) {
		return false;
	}
	return true;
}

function ShowSingleGraph(Title, TargetURL) {
/*
	if NewWindow.document is not defined chances are you are refreshing
	the Popup and it is not in scope, it can now be referenced with
	the window Object.
*/
	if (!NewWindow.document) {
		NewWindow = window;	
	}
	if (!NewWindow.document) {
		alert("NewWindow.document still undef");
		return;
	}
	var PopupDoc = NewWindow.document;
//	PopupDoc.open();	
	PopupDoc.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">');
	PopupDoc.writeln('<html>\n<head>');
	PopupDoc.writeln('<script type="text/javascript" src="MainGraphs.js"></script>');
	PopupDoc.writeln('<meta http-equiv="Refresh" content="60;URL=javascript:ShowSingleGraph(\'' + Title + '\', \'' + TargetURL +'\')"/>');
//	PopupDoc.writeln('<meta http-equiv="Refresh" content="10;URL=http://av.com"/>');
	PopupDoc.writeln('<meta http-equiv="Pragma" content="no-cache">');
	PopupDoc.writeln('<meta http-equiv="Expires" content="-1">');
	PopupDoc.writeln('<link rel="stylesheet" type="text/css" media="all" href="w.css">');
	PopupDoc.writeln('<title>' + Title + '</title>');
	PopupDoc.writeln('</head>');
	PopupDoc.writeln('<body>');
	PopupDoc.writeln('<table>\n<tr><td>');
	PopupDoc.writeln('<img src="' + TargetURL + '">');
	PopupDoc.writeln('</td></tr>');
	if (isIE()) {
		PopupDoc.writeln('<tr><td align="center"><a href="javascript:ShowSingleGraph(\'' + Title + '\', \'' + TargetURL +'\')">Refresh</a></td></tr>');
	}
	else {
		PopupDoc.writeln('<tr><td align="center"><b>Press F5 to Refresh</b></td></tr>');
	}
//	PopupDoc.writeln('<tr><td align="center"><a href="javascript:history.go(0)">Refresh2</a></td></tr>');
	PopupDoc.writeln('</table>');
	PopupDoc.writeln('</body>\n</html>');
	
	if (isIE()){
		//This causes the page to load as with out the document.close nothing is displayed
		NewWindow.history.go(0);
		
	}
	else {
		// this crashes IE
		PopupDoc.close();
	}
}

function PopitUp(Title, TargetURL, Width, Height) {
	// the "Handle need's to be assigned to a javascript varible to keep so browsers happy
	Height = Height + 60;
	Width = Width + 25;
	var windOptions = 'top=100, left=100, menubar=yes, resizable=yes, width=' + Width + ', height=' + Height;
	NewWindow = window.open('', 'Title', windOptions);
	
	ShowSingleGraph(Title, TargetURL);
	
	// return false so onclick doesn't actually take us to the link
	return false; 
}

/******************************************************************************
Pop up the User Control Panel
******************************************************************************/
function PopupUCP(Title, TargetURL, Width, Height) {
	// the "Handle need's to be assigned to a javascript varible to keep some browsers happy"
	Height = Height + 60;
	Width = Width + 25;
	var windOptions = 'top=100,left=100,menubar=no,toolbar=yes,resizable=yes,scrollbars=yes, width=' + Width + ', height=' + Height;

	NewWindow = window.open('', 'Title', windOptions);
	if (typeof(NewWindow.document) == 'undefined') {
	    NewWindow = window;
	}
	NewWindow.location = TargetURL;
	// return false so onclick doesn't actually take us to the link
	return false; 
}

function CreateHiddenInput(name, value) {
    var hiddenInputElm;
    
    hiddenInputElm = document.createElement('input');
    hiddenInputElm.setAttribute('type', 'hidden');
    hiddenInputElm.setAttribute('name', name);
    hiddenInputElm.setAttribute('value', value);
  
    return hiddenInputElm;
}

function submitForm(cmd, e) {
    var evt = window.event?window.event:e;
    var elm;
    var node;
    var formElm;
    
    if (!e) {
        return false;
    }
    if (evt.target) {
        elm = evt.target;
    }
    else if (evt.srcElement) {
        elm = evt.srcElement
    }
    else {
        return false;
    }
    formElm = elm.form;
    formElm.appendChild(CreateHiddenInput('cmd', cmd));
    formElm.submit();
    return true; 
}

function InsertHashLink() {
	var i;
	
	allRows = document.getElementsByTagName('tr');

	for (i=0; i < allRows.length; i++)
		if(i == parseInt(allRows.length-((window.innerHeight-40)/19))) {
			rowElm = document.createElement('tr');
			rowElm.setAttribute('style', 'visibility:collapse');
			cellElm = document.createElement('td');
			tagElm = document.createElement('a');
			tagElm.setAttribute('name', 'b');
			cellElm.appendChild(tagElm);
			rowElm.appendChild(cellElm);
			allRows[i].parentNode.insertBefore(rowElm, allRows[i]);
			break;
		}
}

function SortLogs(sortBy) {
	var logstable = document.getElementById('logstable');
	var rows = logstable.tBodies[0].rows;
	var cells;
	var currentRow;
	var inputs = new Array();
	var inputIDs = new Array();
	var inputDeviceIDs = new Array();
	var inputDatasets = new Array();
	var inputDatapoints = new Array();
	var inputValues = new Array();
	var temp, tempID, tempDeviceID, tempDataset, tempDatapoint;
	var i, j, k, l, m;
	var c;

	//get fields from first row and prepare sort arrays
	cells = rows[0].cells;
	for(i=4,j=1; i<cells.length; i++,j++) {
		inputs[j] = j;
		inputIDs[j] = cells[i].id;

		if(sortBy == "input")
			inputDeviceIDs[j] = parseInt(cells[i].innerHTML);
		else if(sortBy == "deviceid") {
			for(var m=0,n=0,o=0; m<cells[i].id.length; m++) {
				if(inputIDs[j].charAt(m) == ',') {
					if(o == 0)
						inputDeviceIDs[j] = inputIDs[j].substring(0, m);
					else {
						inputDatasets[j] = inputIDs[j].substring(n, m);
						inputDatapoints[j] = inputIDs[j].substring(m+1);
					}
					n = m+1;
					o++;
				}
			}
		}

		for(k=4,l=1; (k<cells.length)&&(k<i); k++,l++) {
			if((inputDeviceIDs[j]<inputDeviceIDs[l]) || ((inputDeviceIDs[j]==inputDeviceIDs[l])&&(inputDatasets[j]<inputDatasets[l])) || ((inputDeviceIDs[j]==inputDeviceIDs[l])&&(inputDatasets[j]==inputDatasets[l])&&(inputDatapoints[j]<inputDatapoints[l]))) {
				temp = inputs[l];
				tempID = inputIDs[l];
				tempDeviceID = inputDeviceIDs[l];
				tempDataset = inputDatasets[l];
				tempDatapoint = inputDatapoints[l];
				inputs[l] = inputs[j];
				inputIDs[l] = inputIDs[j];
				inputDeviceIDs[l] = inputDeviceIDs[j];
				inputDatasets[l] = inputDatasets[j];
				inputDatapoints[l] = inputDatapoints[j];
				for(m=(j-1); m>l; m--) {
					inputs[m+1] = inputs[m];
					inputIDs[m+1] = inputIDs[m];
					inputDeviceIDs[m+1] = inputDeviceIDs[m];
					inputDatasets[m+1] = inputDatasets[m];
					inputDatapoints[m+1] = inputDatapoints[m];
				}
				inputs[l+1] = temp;
				inputIDs[l+1] = tempID;
				inputDeviceIDs[l+1] = tempDeviceID;
				inputDatasets[l+1] = tempDataset;
				inputDatapoints[l+1] = tempDatapoint;
			}
		}
	}

	//sort all rows based on the sort array
	for(i=0; i<rows.length; i++) {
		cells = rows[i].cells;

		for(j=4,k=1; j<cells.length; j++,k++)
			inputValues[k] = cells[parseInt(inputs[k]+3)].innerHTML;

		for(j=4,k=1; j<cells.length; j++,k++) {
			if(i == 0)
				cells[j].id = inputIDs[k];
			cells[j].innerHTML = inputValues[k];
		}
	}
}
