/*
	This heading must remain intact at all times.
	Copyright (c) 2009 Mark Mason. All rights reserved.

	File:	Q-Cogo-Nav.js
	Use:	To provide navigation operations for Q-Cogo, <http://www.q-cogo.com/>.
	Ver:	1.1 (Beta)

	Created by Mark Mason. Latest version available from <http://www.q-cogo.com/>.



	This file is part of Q-Cogo.
	
	Q-Cogo is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	Q-Cogo is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Q-Cogo.  If not, see <http://www.gnu.org/licenses/>.



	FUNCTION LISTING

	Emake
	ScrollHelp
	CheckAllPoints
	EditPoints
	ClearField
	SelectField
	SyncHIHT
	MoveFront
	IntersectSwap
	InvDimSwap
	InvTypeSwap
	TravDimSwap
	TravTypeSwap
*/



// *******************************************************************************************************************************

function Emake() {

// Writes a spoofed contact link
// Input:  None
// Output: The contact link, written into the document

	var MT = '&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
	var AD = '&#105;&#110;&#102;&#111;&#64;&#113;&#45;&#99;&#111;&#103;&#111;&#46;&#99;&#111;&#109;';

	var link = '<a class="submit" onmouseover="Tip(\'&#69;&#109;&#97;&#105;&#108; Q-Cogo\', BALLOON, true)" onmouseout="UnTip()" href="';
	link += MT;
	link += AD;
	link += '">';
	link += AD;
	link += '<\/a>';

	document.write(link);

}

// *******************************************************************************************************************************

function ScrollHelp(DivID) {

// Scrolls the help div to display specific items in its contents
// Input:  Element ID
// Output: The newly scrolled help div

	var Field = document.getElementById('HelpIndex');
	Field.scrollTop = document.getElementById(DivID).offsetTop - Field.offsetTop - 6;

}

// *******************************************************************************************************************************

function CheckAllPoints() {

// Checks users input when points area is blurred
// Input:  None
// Output: Nothing if points area input is acceptable, appropriate alert boxes if it is not

	var check = 1;

// Get points contents from text box (user input box)

	var PointsField = document.getElementById('pointsContentsCopy');
	var ptsCnts = PointsField.value + ' ';

// Remove special characters, leading spaces, and multiple spaces from points pontents and split into a vector

	ptsCnts = ptsCnts.replace(/\n/g,' ');
	ptsCnts = ptsCnts.replace(/\t/g,' ');
	ptsCnts = ptsCnts.replace(/\b/g,'');
	ptsCnts = ptsCnts.replace(/\v/g,'');
	ptsCnts = ptsCnts.replace(/\f/g,'');
	ptsCnts = ptsCnts.replace(/\r/g,'');
	ptsCnts = ptsCnts.replace(/\s+/g,' ');

	ptsCnts = ((ptsCnts.substring(0,1) == ' ') ? ptsCnts.substring(1) : ptsCnts);

	var pointsVector = ptsCnts.split(' ');

// Find length of points matrix, to initialize later

	var lP = ((pointsVector[0] == '') ? 0 : (pointsVector.length - 1) / 5);

// Alert if input does not appear to have the appropriate number of values (5 values per point)

	if (lP != parseInt(lP)) {

		if (PointsField.readOnly == false) {

			alert('Each point must have a point name, northing, easting, elevation, and description');

		}

		check = 0;

	}

	if (check) {

// Read data into 2D matrix

		var pointsMatrix = new Array(lP);

		for (var i=0; i<=lP; i++) {

			var tempPoint = new Array(5);
			tempPoint[0] = pointsVector[0 + i*5];
			tempPoint[1] = pointsVector[1 + i*5];
			tempPoint[2] = pointsVector[2 + i*5];
			tempPoint[3] = pointsVector[3 + i*5];
			tempPoint[4] = pointsVector[4 + i*5];

			pointsMatrix[i] = tempPoint;

		}

// Loop through matrix and check each value

		for (i=0; i<=lP-1; i++) {

			if (pointsMatrix[i][0].search(/\,/) >= 0 || pointsMatrix[i][0].search(/\-/) >= 0 || pointsMatrix[i][0].search(/\*/) >= 0) {

				check = 0;
				alert('Point names must not contain commas, asterisks or dashes');

			}

			if (check && (!CheckDecimal(pointsMatrix[i][1]) || !CheckDecimal(pointsMatrix[i][2]) || !CheckDecimal(pointsMatrix[i][3]))) {

				check = 0;

			}

			if (check) {

				for (var j=i; j<lP; j++) {

					if (j != i && FormatString(pointsMatrix[i][0], 1, 0) == FormatString(pointsMatrix[j][0], 1, 0)) {

						check = 0;
						alert('Point names must be unique');
						break;

					}

				}

			}

		}

	}

// Confirm and output, rewrite, or leave open as appropriate

	if (check && PointsField.readOnly == false) {

		if (confirm('Permanently edit points record?')) {

			OutputPoints(pointsMatrix, lP-1);
			PointsField.readOnly = true;
			PointsField.style.backgroundColor = '#ffffff';
			PointsField.style.border = '0.078em solid #666666';

// Clear point sorting labels

			ClearSort();

		}

		else {

			RewritePoints();
			PointsField.readOnly = true;
			PointsField.style.backgroundColor = '#ffffff';
			PointsField.style.border = '0.078em solid #666666';

		}

	}

	else if (!check && PointsField.readOnly == false) {

		if (confirm('Points record not changed! Revert to previous points?')) {

			RewritePoints();
			PointsField.readOnly = true;
			PointsField.style.backgroundColor = '#ffffff';
			PointsField.style.border = '0.078em solid #666666';

		}

	}

	else {

		RewritePoints();

	}

}

// *******************************************************************************************************************************

function EditPoints() {

// Opens the points area for editing
// Input:  None
// Output: Makes the points area editable at high precision, informs user

	var PrecisionField = document.getElementById('DPrecision');
	var PointsField = document.getElementById('pointsContentsCopy');
	
// Make points area high precision and change color to inform the user that it is opened for editing

	PrecisionVal = PrecisionField.value;

	PrecisionField.value = '5';
	RewritePoints();
	PrecisionField.value = PrecisionVal;

	PointsField.readOnly = false;
	PointsField.style.backgroundColor = '#f2f2ff';
	PointsField.style.border = '0.078em solid #000099';

// Focus the points area

	PointsField.focus();

}

// *******************************************************************************************************************************

function ClearField(FieldID) {

// Clears the contents of a given text field
// Input:  The html ID of the field that will be cleared
// Output: Clears the field contents, confirms with user if the field is a critical one

	var Field = document.getElementById(FieldID);

// If the requested field is the points field, then confirm clear with user

	if (FieldID == 'pointsContents') {

		var check = confirm('Delete all points?');

		if (check == 1) {

			Field.value = '';
			var FieldCopy = document.getElementById(FieldID + 'Copy');
			FieldCopy.value = '';
			SketchPoints();

		}

	}

// Otherwise, clear without confirming

	else {

		Field.value = '';

	}

}

// *******************************************************************************************************************************

function SelectField(Field) {

// Selects the contents of a given text field
// Input:  The html DOM path to the field that will be selected
// Output: Selects the field contents

// If points field chosen, make high resolution

	if (Field == document.points.pointsContentsCopy) {

		var PrecisionField = document.getElementById('DPrecision');
		var PointsField = document.getElementById('pointsContentsCopy');
	
		PrecisionVal = PrecisionField.value;

		PrecisionField.value = '5';
		RewritePoints();
		PrecisionField.value = PrecisionVal;

	}

// Focus field

	Field.focus();
	Field.select();

}

// *******************************************************************************************************************************

function SyncHIHT(FieldID) {

// Checks that the contents of an HI or HT field is acceptable, formats the input according to current precision, updates all existing HI or HT fields
// Input:  The field ID to be checked
// Output: The formatted field, or "0" if the distance cannot be parsed, along with the appropriate alert boxes

// Get value of entered field

	var Field = document.getElementById(FieldID);
	var TravDim = document.getElementById('TravDim').innerHTML;

// Form vector of HI or HT values to change

	if (FieldID.substring(0, 2) == 'HI') {

		var Fields = [document.getElementById('HI'), document.getElementById('HI2'), document.getElementById('TravHI')];

	}

	else if (FieldID.substring(0, 2) == 'HT') {

		var Fields = [document.getElementById('HT'), document.getElementById('HT2'), document.getElementById('TravHT')];

	}

// Error check input and update all fields accordingly

	if (!Field.value || !CheckDecimal(Field.value)) {

		Fields[0].value = '0';

		if (Fields[1] != null) {

			Fields[1].value = '0';

		}
	
		if (TravDim == '3D') {

			Fields[2].innerHTML = '0';

		}

	}

	else {

		var Value = FormatDecimal(Field.value, 0);
		Value = Value.replace(/\s+/g,'');

		Fields[0].value = Value;

		if (Fields[1] != null) {

			Fields[1].value = Value;

		}

		if (TravDim == '3D') {

			Fields[2].innerHTML = Value;

		}

	}

}

// *******************************************************************************************************************************

function MoveFront(DivID) {

// Apparantly moves the selected div and its corresponding label in front of the others
// Input:  The html ID of the div to be moved
// Output: Changes the z-index of all divs to move the selected div to the front

// Hide any open tooltips

	tt_HideInit();

// Create array of all existing div layer IDs

	var Divs = new Array(6);
	Divs[0] = 'points';
	Divs[1] = 'traverse';
	Divs[2] = 'inverse';
	Divs[3] = 'intersect';
	Divs[4] = 'transform';
	Divs[5] = 'area';
	Divs[6] = 'settings';
	Divs[7] = 'help';

// Loop through the array and move the selected layer to the front

	for (var i=0; i<Divs.length; i++) {

		var DivObject = document.getElementById(Divs[i]).style;
		var TabObject = document.getElementById(Divs[i] + '-tab');
		var FocusElement = document.getElementById(Divs[i] + '-tiplink');

		if (Divs[i] == DivID) {

			DivObject.zIndex = 10;
			TabObject.setAttribute('class', 'legend-selected');

			if (FocusElement!= null) {

				FocusElement.focus();

			}

		}

		if (Divs[i] != DivID) {

			DivObject.zIndex = 0;
			TabObject.setAttribute('class', 'legend');

		}

	}

}

// *******************************************************************************************************************************

function IntersectSwap() {

// Cycles the intersection fields area through all three types of intersections
// Input:  None
// Output: Changes the labels and title link of the intersect fields area

// Get elements to be changed

	var Title = document.getElementById('IntTitle');
	var FromLabel = document.getElementById('IntValueFromLabel');
	var ToLabel = document.getElementById('IntValueToLabel');

// Change the elements' inner HTML to the next values in the cycle

	var Titles = ['Brg-Brg', 'Brg-Dist', 'Dist-Dist'];
	var Froms = ['Az:', 'Az:', 'HD:'];
	var Tos = ['Az:', 'HD:', 'HD:'];

	for (var i=0; i<Titles.length; i++) {

		if (Title.innerHTML == Titles[i]) {

			break;

		}

	}

	Title.innerHTML = Titles[(i + 1) % Titles.length];
	FromLabel.innerHTML = Froms[(i + 1) % Froms.length];
	ToLabel.innerHTML = Tos[(i + 1) % Tos.length];

}

// *******************************************************************************************************************************

function InvDimSwap() {

// Changes the inverse dimension title link between 2D and 3D
// Input:  None
// Output: Changes title link of the inverse fields area

// Get element to be changed

	var Title = document.getElementById('InvDim');

// Change the elements' inner HTML to the next values in the cycle

	var Titles = ['2D', '3D'];

	for (var i=0; i<Titles.length; i++) {

		if (Title.innerHTML == Titles[i]) {

			break;

		}

	}

	Title.innerHTML = Titles[(i + 1) % Titles.length];

}

// *******************************************************************************************************************************

function InvTypeSwap() {

// Changes the inverse fields area between point to point and point to line methods
// Input:  None
// Output: Changes title link and input areas of the inverse area

// Get elements to be changed

	var Title = document.getElementById('InvType');
	var FromLabel = document.getElementById('InvFromPtLabel');
	var ToLabel = document.getElementById('InvToPtLabel');
	var Input = document.getElementById('InvPtPt');
	var PtLabel = document.getElementById('InvPtPtLabel');

// Change the elements' inner HTML to the next values in the cycle

	var Titles = ['Point', 'Line'];
	var FromLabels = ['From:', 'Start:'];
	var ToLabels = ['To:', 'End:'];
	var Inputs = ['none', 'inline'];
	var PtLabels = ['none', 'inline'];

	for (var i=0; i<Titles.length; i++) {

		if (Title.innerHTML == Titles[i]) {

			break;

		}

	}

	Title.innerHTML = Titles[(i + 1) % Titles.length];
	FromLabel.innerHTML = FromLabels[(i + 1) % FromLabels.length];
	ToLabel.innerHTML = ToLabels[(i + 1) % ToLabels.length];
	Input.style.display = Inputs[(i + 1) % Inputs.length];
	PtLabel.style.display = PtLabels[(i + 1) % PtLabels.length];

}

// *******************************************************************************************************************************

function TravDimSwap() {

// Changes the traverse title and fields between 2D and 3D
// Input:  None
// Output: Changes title link and input areas of the traverse area

// Hide any open tooltips

	tt_HideInit();

// Get elements to be changed

	var Title = document.getElementById('TravDim');
	var DistLabel = document.getElementById('TravValueDistLabel');
	var ZALabel = document.getElementById('TravValueZALabel');
	var ZAInput = document.getElementById('TravZA');
	var Span = document.getElementById('TravHIHT');

// Form inner HTML for HI and HT change boxes

	var InnerHI = '<div style=&quot;width:10.5em;&quot;><input class=&quot;short&quot; style=&quot;float:none;&quot; type=&quot;text&quot; value=&quot;&quot; onblur=&quot;SyncHIHT(\\\'HI2\\\'); tt_HideInit();&quot; id=&quot;HI2&quot;> <a class=&quot;submit&quot; href=&quot;javascript:void(0)&quot; onclick=&quot;tt_HideInit()&quot;>OK</a></div>';

	var InnerHT = '<div style=&quot;width:10.5em;&quot;><input class=&quot;short&quot; style=&quot;float:none;&quot; type=&quot;text&quot; value=&quot;&quot; onblur=&quot;SyncHIHT(\\\'HT2\\\'); tt_HideInit();&quot; id=&quot;HT2&quot;> <a class=&quot;submit&quot; href=&quot;javascript:void(0)&quot; onclick=&quot;tt_HideInit()&quot;>OK</a></div>';


// Change the elements' inner HTML to the next values in the cycle, dynamically creating the html of the input balloon

	var SpanString = ':&#160; HI=<a class="title-swap" onmouseover="Tip(\'Change HI\', BALLOON, true)" onmouseout="UnTip()"';
	SpanString += ' href="javascript:void(0)" id="TravHI"';
	SpanString += ' onclick="tt_HideInit(); Tip(\'' + InnerHI + '\', BALLOON, true, STICKY, true, DELAY, 0, EXCLUSIVE, true);';
	SpanString += ' document.getElementById(\'HI2\').value = document.getElementById(\'HI\').value;">';
	SpanString += document.getElementById('HI').value;
	SpanString += '</a>,&#160;';
	SpanString += ' HT=<a class="title-swap" onmouseover="Tip(\'Change HI\', BALLOON, true)" onmouseout="UnTip()"';
	SpanString += ' href="javascript:void(0)" id="TravHT"';
	SpanString += ' onclick="tt_HideInit(); Tip(\'' + InnerHT + '\', BALLOON, true, STICKY, true, DELAY, 0, EXCLUSIVE, true);';
	SpanString += ' document.getElementById(\'HT2\').value = document.getElementById(\'HT\').value;">';
	SpanString += document.getElementById('HT').value;
	SpanString += '</a>';

	var Titles = ['2D', '3D'];
	var Dists = ['HD:', 'SD:'];
	var Labels = ['none', 'inline'];
	var Inputs = ['none', 'inline'];
	var Spans = ['', SpanString];

	for (var i=0; i<Titles.length; i++) {

		if (Title.innerHTML == Titles[i]) {

			break;

		}

	}

	Title.innerHTML = Titles[(i + 1) % Titles.length];
	DistLabel.innerHTML = Dists[(i + 1) % Dists.length];
	ZALabel.style.display = Labels[(i + 1) % Labels.length];
	ZAInput.style.display = Inputs[(i + 1) % Inputs.length];
//	ZAInput.style.display = Inputs[(i + 1) % Inputs.length];
	Span.innerHTML = Spans[(i + 1) % Spans.length];

}

// *******************************************************************************************************************************

function TravTypeSwap() {

// Changes the traverse fields area between azimuth method and horizontal angle method
// Input:  None
// Output: Changes title link and input areas of the traverse area

// Hide any open tooltips

	tt_HideInit();

// Get elements to be changed

	var Title = document.getElementById('TravType');
	var Ang = document.getElementById('TravValueAngLabel');
	var BSLabel = document.getElementById('TravValueBSLabel');
	var BSInput = document.getElementById('TravBS');

// Change the elements' inner HTML to the next values in the cycle

	var Titles = ['Azimuth', 'Angle'];
	var Angs = ['Az:', 'HA:'];
	var Labels = ['none', 'inline'];
	var Inputs = ['none', 'inline'];

	for (var i=0; i<Titles.length; i++) {

		if (Title.innerHTML == Titles[i]) {

			break;

		}

	}

	Title.innerHTML = Titles[(i + 1) % Titles.length];
	Ang.innerHTML = Angs[(i + 1) % Angs.length];
	BSLabel.style.display = Labels[(i + 1) % Labels.length];
	BSInput.style.display = Inputs[(i + 1) % Inputs.length];

}

// *******************************************************************************************************************************

