var Region = "";
var DefaultWorldMapInstructions = ":: Select a region of the world, chart type & chart brand";
var DefaultInstructions = ":: Now select a chart type & chart brand from the panel on the left";
var DefaultSubRegionInstructions = ":: Select a sub region on the map";
var DefaultSelectChartInstructions = ":: Click on a Chart Reference to add it to your Chart Selection";
var DefaultSelectChartReference = "Please select a Chart Reference";
var DefaultNoMap = "<p align='left'>Currently there are no charts online for this manufacturer. Please go back and select another region or manufacturer.</p><p align='left'><a href='#' onclick='history.back();return false;'><b>Go back</b></a></p>";
var WorldRegions = new Array('Europe','Iceland', 'Americas', 'Asia', 'Africa');

function ShowChartSelector(Show){
	if (Show){
		parent.document.getElementById("ChartSelector").width = 125;
		parent.document.getElementById("Map").width = 470;
		parent.document.getElementById("ChartSelector").style.display = "inline";
	}else{
		parent.document.getElementById("ChartSelector").width = 0;
		parent.document.getElementById("Map").width = 600;
		parent.document.getElementById("ChartSelector").style.display = "none";
	}
}

function ChartFinderNavigation(Data){
	// Changes info on the top bar
	parent.document.getElementById("ChartFinderNavigation").innerHTML = Data;
}

function ChartFinderStatus(Data){
	// Changes info on the bottom bar
	parent.document.getElementById("ChartFinderStatus").innerHTML = Data;
}

function ShowLayer(LayerName, Hide){
	// Shows or hides a layer
	if (Hide){
		if (Region != LayerName) document.getElementById(LayerName).style.visibility = "hidden";
	}else{
		document.getElementById(LayerName).style.visibility = "visible";
	}
}

function SelectRegion(RegionName, RegionText){
	// For World Map page on click region
	Region = RegionName;
	
	for (var i=0;i<WorldRegions.length;i++){
		ShowLayer(WorldRegions[i], true);
	}
	ShowLayer(RegionName, false);
	
	ChartFinderNavigation(RegionText); 
	ChartFinderStatus(DefaultInstructions); 
	document.getElementById("WorldMapSelection").style.display = "block";
}

function NoMap(){
	document.getElementById("CurrentMap").innerHTML = DefaultNoMap;

}

function ShowCharts(ChartType){
	// Show/Hide Chart Manufacturers
	if (ChartType == 'Paper'){
		document.getElementById("ChartManufacturerElectronic").style.display = "none";
		document.getElementById("ChartManufacturerPaper").style.display = "inline";
	}else{
		document.getElementById("ChartManufacturerElectronic").style.display = "inline";
		document.getElementById("ChartManufacturerPaper").style.display = "none";
	}
}

function Next(Manufacturer, ChartType){
	// Goes to Next map screen
	if (Region == "") {alert("Please select a region on the map first"); return false;}
	document.location.href = ChartType.toLowerCase() + "_" + Manufacturer.toLowerCase() + "_" + Region.toLowerCase() + ".asp";
}

function SelectSubRegion(){
	// Function called on every sub region selection page
	ChartFinderStatus(DefaultSubRegionInstructions);
	PageNavigation();
}

function SelectChart(){
	// Function called on every chart selection page
	ChartFinderStatus(DefaultSelectChartInstructions);
	PageNavigation();
	ShowChartSelector(true);
	DrawCords();
}

function PageNavigation(){
	// Gets current page and displays navigation tree at top
	var NavText = "";
	var Link = "world_map.asp";
	
	var PageName = GetPageName();
	PageName = PageName.replace(".asp","");
	var PageNameArray = PageName.split("_");
	var LinkBuild = PageNameArray[0] + "_" + PageNameArray[1];
	
	for (var i=0;i<PageNameArray.length;i++){
		if (i >=2) {
			LinkBuild += "_" + PageNameArray[i];
			Link = LinkBuild + ".asp";
		}
		NavText = NavText + "<a href='/chartfinder/" + Link + "' target='Map'>" + Capitalise(PageNameArray[i]) + "</a> &gt; ";	
	}
	
	NavText = NavText.substring(0,(NavText.length-6))
	
	ChartFinderNavigation(NavText);
}

function GetPageName(){
	// Gets current page name
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	return(sPage);
}

function Capitalise(a) {
    var pattern = /(\w)(\w*)/; // a letter, and then one, none or more letters 
    a = a.split(/\s+/g); // split the sentence into an array of words

    for (i = 0 ; i < a.length ; i ++ ) {
        var parts = a[i].match(pattern); // just a temp variable to store the fragments in.

        var firstLetter = parts[1].toUpperCase();
        var restOfWord = parts[2].toLowerCase();

        a[i] = firstLetter + restOfWord; // re-assign it back to the array and move on
    }
    
    return a.join(' '); // join it back together
}

// References/Co-ordinates

function addCord(Chart) {
    var defaultSelected = true;
    var selected = true;
	var ChartArray = Chart.split("::")
	var ChartProductID = ChartArray[0];

	var optionName = new Option(Chart, ChartProductID, defaultSelected, selected)
	var ChartSelectorList = window.parent.frames["ChartSelector"].document.FormChartSelector.adminidselect;
    ChartSelectorList.options[ChartSelectorList.length] = optionName;
	DrawCords();
}

function deleteCord(Cord){
	var ChartSelectorList = window.parent.frames["ChartSelector"].document.FormChartSelector.adminidselect;
	var opt = ChartSelectorList.options;
	opt[Cord] = null;
	DrawCords();
}

function clearCord(){
	var ChartSelectorList = window.parent.frames["ChartSelector"].document.FormChartSelector.adminidselect;
	ChartSelectorList.options.length = 0;
	DrawCords();
}

function allCordSelect(){
	var ChartSelectorList = window.parent.frames["ChartSelector"].document.FormChartSelector.adminidselect;
	if (ChartSelectorList.length && ChartSelectorList.options[0].value == 'temp') return;
	for (i=0;i<ChartSelectorList.length;i++){
		ChartSelectorList.options[i].selected = true;
	}
	DrawCords();
}

function DrawCords(){
	// Draws reference table in Chart Selector
	var ChartSelector = window.parent.frames["ChartSelector"].document.getElementById("ChartSelectorData");
	var ChartSelectorList = window.parent.frames["ChartSelector"].document.FormChartSelector.adminidselect;
	var ChartSelectorBuyNow = window.parent.frames["ChartSelector"].document.FormChartSelector.BuyNow;
	var ChartData = "";
	
	for (var i=0;i<ChartSelectorList.length;i++){
		ChartData += "<tr align='left' valign='top'><td>" + ChartSelectorList.options[i].text + "</td><td><a href='#' onClick='deleteCord(" + i + "); return false;' title='Delete " + ChartSelectorList.options[i].text + "'>X</a></td></tr>";
	}
	
	if (ChartData == ""){
		ChartData = DefaultSelectChartReference;
		ChartSelectorBuyNow.disabled = true;
	}else{
		ChartData = '<table width="99%" border="0" cellspacing="0" cellpadding="2">' + ChartData + '</table>';
		ChartSelectorBuyNow.disabled = false;
	};
	ChartSelector.innerHTML = ChartData;

}

// Validate submit
function SubmitChartSelector(thatForm){
	for (i=0;i<thatForm.adminidselect.length;i++){
		thatForm.adminid.value += thatForm.adminidselect[i].value + ",";	
	}
	
	if (thatForm.adminid.value.length > 0) thatForm.adminid.value = thatForm.adminid.value.substring(0,thatForm.adminid.value.length-1);
	return true;
}