var theChartName;

function myCharts(x)
{
	var namesCookie = getaCookie("myChartsNames");
	var codesCookie = getaCookie("myChartsCodes");
	
	if (namesCookie == "")
		top.location = "http://www.astromusings.com/my-charts/select.html";
	else
	{
		var theNames = namesCookie.split("/");
		var theCodes = codesCookie.split("/");
		
		var theName = theNames[x];
		var theCode = theCodes[x];
		
		if (theCode == "undefined")
		{
			top.location = "http://www.astromusings.com/my-charts/select.html";
		}
		else
		{
			var newCookie = "myChartsName=" + escape(theName) + "; domain=.astromusings.com; path=/";
			document.cookie = newCookie;
		
			var newCookie = "myChartsCode=" + theCode + "; domain=.astromusings.com; path=/";
			document.cookie = newCookie;
			
			document.title = theName + "'s Chart";
			
			document.getElementById("chart").src = "../cgi-bin/my-charts-display.pl";
		}
	}
}


function chartHeader()
{
	theChartName = getaCookie("myChartsName");
	document.write(theChartName + "'s Chart");
}


function setMyChartsName(theName)
{
	theChartName = theName;
}


function getaCookie(searchName)
{
	var theCookies = document.cookie.split(";");
	var theCookie, j, theCookieName, theCookieData;
	
	for (var i = 0; i < theCookies.length; i++)
	{
		theCookie = unescape(theCookies[i]);
		j = theCookie.indexOf("=")
		theCookieName = theCookie.substring(0, j);
		
		if (theCookieName.charAt(0) == " ")
		{
			theCookieName = theCookieName.substring(1, theCookieName.length);
		}
		
		if (theCookieName == searchName)
		{
			return theCookie.substring(j+1, theCookie.length);
		}
	}
	return "";
}


function cc()
{
	var newCookie = "myChartsName=; expires=1-Jan-1980 00:00:00 GMT; domain=.astromusings.com; path=/";
	document.cookie = newCookie;
	
	var newCookie = "myChartsCode=; expires=1-Jan-1980 00:00:00 GMT; domain=.astromusings.com; path=/";
	document.cookie = newCookie;
}


function checkInput()
{
	var nameField = document.getElementById("name");
	var theName = nameField.value;
	
	var passwordField = document.getElementById("password");
	var thePassword = passwordField.value;
	
	var nameMessageSpan = document.getElementById("nameMessage");
	var oldNameMessageTextNode = nameMessageSpan.childNodes[0];
	var nameMessage = "";
	
	var passwordMessageSpan = document.getElementById("passwordMessage");
	var oldPasswordMessageTextNode = passwordMessageSpan.childNodes[0];
	var passwordMessage = "";
	
	var theReturn = true;
	
	if (theName == "") {
		nameMessage = "Please enter a name for the chart (e.g. Mary or Joe).";
	}
	
	if (thePassword == "") {
		passwordMessage = "Please enter the password for the chart.";
	}
	
	var pattern = /[,;:\/]/;
	if (pattern.test(theName)) {
		nameMessage = "The name cannot include a comma, colon, semicolon or forward slash.";
	}
	
	var theCookies = getaCookie("myCharts");
	var cookieArray = theCookies.split("/");
	var theCookie, j, chartName;
	
	for (var i = 0; i < cookieArray.length; i++)
	{
		theCookie = cookieArray[i];
		j = theCookie.lastIndexOf(":");
		chartName = theCookie.substring(0,j);
		
		if (chartName == theName) {
			nameMessage = "The name " + theName + " already exists in your " + '"My Charts" menu.';
		}
	}
	
	if (nameMessage != "") {
		var newNameMessageTextNode = document.createTextNode(nameMessage);
		nameMessageSpan.replaceChild(newNameMessageTextNode, oldNameMessageTextNode);
		nameMessageSpan.style.visibility = "visible";
		document.getElementById("nameLabel").style.color = "red";
		nameField.focus();
		theReturn = false;
	}
	
	if (passwordMessage != "") {
		var newPasswordMessageTextNode = document.createTextNode(passwordMessage);
		passwordMessageSpan.replaceChild(newPasswordMessageTextNode, oldPasswordMessageTextNode);
		passwordMessageSpan.style.visibility = "visible";
		document.getElementById("passwordLabel").style.color = "red";
		if (nameMessage == "") {
			passwordField.focus();
		}
		theReturn = false;
	}
	
	return theReturn;
}


function checkInvalid()
{
	var theUserName = getaCookie("myChartsInvalid");
	
	var newCookie = "myChartsInvalid=; expires=1-Jan-1990 00:00:00 GMT; domain=.astromusings.com; path=/;";
	document.cookie = newCookie;
	
	if (theUserName != "")
	{
		var nameField = document.getElementById("name");
		nameField.defaultValue = theUserName;
		
		document.getElementById("password").focus();
		document.getElementById("passwordMessage").style.visibility = "visible";
		document.getElementById("passwordLabel").style.color = "red";
	}
}


function writeExpiredMessage()
{
	var theExpMonth = getaCookie("myChartsExpiredMonth");
	var theExpYear = getaCookie("myChartsExpiredYear");
	var newCookie = "myChartsExpiredMonth=; expires=1-Jan-1990 00:00:00 GMT; domain=.astromusings.com; path=/;";
	document.cookie = newCookie;
	var newCookie = "myChartsExpiredYear=; expires=1-Jan-1990 00:00:00 GMT; domain=.astromusings.com; path=/;";
	document.cookie = newCookie;
	
	document.write('The password you entered expired at the end of ' + theExpMonth + ', ' + theExpYear + '.');
}


function checkAdded()
{
	var theAddedName = getaCookie("myChartsAdd");
	if (theAddedName == "")
	{
		document.write("AstroMusings was unable to add the chart to your My Charts menu.<br><br>");
		document.write("In order to use the My Charts system, that is to add charts to and display charts from your My Charts menu, your browser must be set to accept cookies.<br><br>");
		document.write('After enabling cookies, return to <a href="add-chart.html">Add Chart</a>.');
	}
	else
	{
		document.write(theAddedName + "'s chart has been added to your " + '"My Charts" menu.');
		document.write("<br><br>");
		document.write('To add another chart to your "My Charts" menu: &nbsp;<a href="add-chart.html">Add Chart</a><br>');
		document.write('To delete a chart from your "My Charts" menu: &nbsp;<a href="delete.html">Delete Chart</a>');
		
		newCookie = "myChartsAdd=; expires=1-Jan-1980 00:00:00 GMT; domain=.astromusings.com; path=/";
		document.cookie = newCookie;
	}
}


function showMyChart(theURL)
{
	var temp = theURL.split(/\//);
	temp = temp[temp.length - 1];
	
	var wName = "W" + Math.round(Math.random() * (10000 - 1)) + 1;
	
	var chartWindow = window.open("http://www.astromusings.com/my-charts/mycharts2.html", wName, "height=570,width=570,scrollbars=no,directories=no,location=no,menubar=no,status=no,left=600");
	
	var newCookie = "myChartsChart=" + temp + "; domain=.astromusings.com; path=/";
	document.cookie = newCookie;
	
	var newCookie = "myChartsTitle=" + theChartName + "; domain=.astromusings.com; path=/";
	document.cookie = newCookie;
}


function setTitle()
{
	var theTitle = getaCookie("myChartsTitle");
	
	var chartsChart = getaCookie("myChartsChart");
	var pattern = /-chart.gif$/;
	
	if (!pattern.test(chartsChart))
	{
		theTitle = theTitle + "'s Chart";
	}
	
	document.title = theTitle;
	
	var newCookie = "myChartsName=; expires=1-Jan-1980 00:00:00 GMT; domain=.astromusings.com; path=/";
	document.cookie = newCookie;
	
	newCookie = "myChartsTitle=; expires=1-Jan-1980 00:00:00 GMT; domain=.astromusings.com; path=/";
	document.cookie = newCookie;
}


function myChartsChart()
{
	document.getElementById("chart").src = "../cgi-bin/my-charts-chart.pl";
}
