// Define a Card object
function Card (ImageName, Width, Height, SmName, SmWidth, SmHeight, TopColour, BottomColour, TopLine, BottomLine, VerseLine1, VerseLine2, VerseLine3, VerseLine4)
{
	this.ImageName = ImageName;
	this.Width = Width;
	this.Height = Height;
	this.SmName = SmName;
	this.SmWidth = SmWidth;
	this.SmHeight = SmHeight;
	this.TopColour = TopColour;
	this.BottomColour = BottomColour;
	this.TopLine = TopLine;
	this.BottomLine = BottomLine;
	this.VerseLine1 = VerseLine1;
	this.VerseLine2 = VerseLine2;
	this.VerseLine3 = VerseLine3;
	this.VerseLine4 = VerseLine4;
}

// Save a Card in a cookie
function saveCard ()
{
//	document.write  ("Saving card: ","card", "[",this.ImageName , "],[" + this.Width + "],[" + this.Height + "],[" + this.SmName + "],["  + this.SmWidth + "],[" + this.SmHeight + "],[" + this.TopColour + "],[" + this.BottomColour + "],[" + this.TopLine + "],[" + this.BottomLine + "],[" + this.VerseLine1 + "],[" + this.VerseLine2 + "],[" + this.VerseLine3 + "],[" + this.VerseLine4 + "]","<br/>");
	saveCookie ("card", "["+this.ImageName + "],[" + this.Width + "],[" + this.Height + "],[" + this.SmName + "],["  + this.SmWidth + "],[" + this.SmHeight + "],[" + this.TopColour + "],[" + this.BottomColour + "],[" + this.TopLine + "],[" + this.BottomLine + "],[" + this.VerseLine1 + "],[" + this.VerseLine2 + "],[" + this.VerseLine3 + "],[" + this.VerseLine4 + "]");
}

//Read a Card from a cookie
function getCard ()
{
	var Name = "card";
	var allCookies = document.cookie;
//	document.write ("getCard, cookies= '", allCookies, "'<br/>");
	var pos = allCookies.indexOf (Name + "=");
	if (pos != -1)
	{
		var start = pos + Name.length + 1;
		var end = allCookies.indexOf(";", start);
		if (end == -1) end = allCookies.length;
		var value = allCookies.substring (start, end);
		value = unescape (value);
//		document.write (Name,  " = '", value, "'<br/>");
		
//		var cardInfo = /^.([\w\/\.-]+)\]\,\[(\d+)\],\[(\d+)\],\[([\w\/\.-]+)\],\[(\d+)\],\[(\d+)\]\,\[(\w+)\]\,\[(\w+)\]\,\[([^\]]+)\]\,\[([^\]]+)\]\,\[([^\]]+)\]\,\[([^\]]+)\]\,\[([^\]]+)\]\,\[([^\]]+)/;

		var cardInfo = /^.([\w\/\.-]+)\]\,\[(\d+)\],\[(\d+)\],\[([\w\/\.-]+)\],\[(\d+)\],\[(\d+)\]\,\[(\w+)\]\,\[(\w+)\]\,\[([^\]]*)\]\,\[([^\]]*)\]\,\[([^\]]*)\]\,\[([^\]]*)\]\,\[([^\]]*)\]\,\[([^\]]*)/;
		
		var result = value.match (cardInfo);
		if (result != null)
		{
//			document.write ("Parsed:  0='", result [0], "', 1='", result [1], "', 2='", result [2], "', 3='", result [3], "', 4='", result [4], "', 5='", result [5], "', 6='", result [6], "', 7='", result [7], "', 8='", result [8], "', 9='", result [9], "', 10='", result [10], "', 11='", result [11], "', 12='", result [12], "', 13='", result [13], "', 14='", result [14], "' [", result.length,"]<br/>");
			if (result.length == 15)
			{
				this.ImageName = result [1];
				this.Width = result [2];
				this.Height = result [3];
				this.SmName = result [4];
				this.SmWidth = result [5];
				this.SmHeight = result [6];
				this.TopColour = result [7];
				this.BottomColour = result [8];
				this.TopLine = result [9];
				this.BottomLine = result [10];
				this.VerseLine1 = result [11];
				this.VerseLine2 = result [12];
				this.VerseLine3 = result [13];
				this.VerseLine4 = result [14];
			}
			else
			{
//				document.write ("Less than 15 items!<br/>");
				this.ImageName = "";
				this.Width = 0;
				this.Height = 0;
				this.SmName = "";
				this.SmWidth = 0;
				this.SmHeight = 0;
				this.TopColour = "";
				this.BottomColour = "";
				this.TopLine = "\240";
				this.BottomLine = "\240";
				this.VerseLine1 = "\240";
				this.VerseLine2 = "\240";
				this.VerseLine3 = "\240";
				this.VerseLine4 = "\240";
			}
		}
		else
		{
//			document.write ("card cookie is null!<br/>");
		}
	}
	else
	{
//		document.write ("No cookies!<br/>");
	}
	
	// Adjust image if it's one that has been removed
	
	if (this.ImageName == "images/flower.jpg")
	{
//		document.write ("Image is flower!<br/>");
		this.ImageName ="images/blank.gif";
		this.Width = 200;
		this.Height = 300;
		this.SmName = "images/blank.gif";
		this.SmWidth = 138;
		this.SmHeight = 207;
		this.TopColour = "black";
		this.BottomColour = "black";
	}
}

// This function updates the card display
function updateDisplay ()
{
//	document.write  ("updateDisplay: ","card", "[",this.ImageName , "],[" + this.Width + "],[" + this.Height + "],[" + this.SmName , "],["  + this.SmWidth + "],[" + this.SmHeight + "],[" + this.TopColour + "],[" + this.BottomColour + "],[" + this.TopLine + "],[" + this.BottomLine + "],[" + this.VerseLine1 + "],[" + this.VerseLine2 + "],[" + this.VerseLine3 + "],[" + this.VerseLine4 + "]","<br/>");
	document.getElementById("Left").style.background="url('"+this.ImageName+"')";
	document.getElementById("Left").style.width=this.Width;
	document.getElementById("Left").style.height=this.Height;
	document.getElementById("Right").style.width=this.Width;
	document.getElementById("Right").style.height=this.Height;
	document.getElementById("card").style.width=2*this.Width+4;
	document.getElementById("card").style.height=parseInt(this.Height)+2;
	document.getElementById("TopLine").style.height=this.Height/2;
	document.getElementById("TopLine").style.color=this.TopColour;
	document.getElementById("BottomLine").style.color=this.BottomColour;
	document.getElementById("card").style.fontSize="70%";
	
	// Update texts
	 var newText = document.createTextNode(this.TopLine);
	 var oldText = document.getElementById("TopLine").childNodes[0];
   	 document.getElementById("TopLine").replaceChild(newText, oldText);

	 var newText = document.createTextNode(this.BottomLine);
	 var oldText = document.getElementById("BottomLine").childNodes[0];
   	 document.getElementById("BottomLine").replaceChild(newText, oldText);

	 var newText = document.createTextNode(this.VerseLine1);
	 var oldText = document.getElementById("VerseLine1").childNodes[0];
	 document.getElementById("VerseLine1").replaceChild(newText, oldText);

	 var newText = document.createTextNode(this.VerseLine2);
	 var oldText = document.getElementById("VerseLine2").childNodes[0];
	 document.getElementById("VerseLine2").replaceChild(newText, oldText);

	 var newText = document.createTextNode(this.VerseLine3);
	 var oldText = document.getElementById("VerseLine3").childNodes[0];
	 document.getElementById("VerseLine3").replaceChild(newText, oldText);

	 var newText = document.createTextNode(this.VerseLine4);
	 var oldText = document.getElementById("VerseLine4").childNodes[0];
	 document.getElementById("VerseLine4").replaceChild(newText, oldText);

	// Adjust widths of divs
	var NewContentWidth = 2*this.Width+4;
	if (NewContentWidth  <= 404)
	{
		document.getElementById("ContentWidth").style.width=690;
		document.getElementById("content").style.width=492;
		document.getElementById("wrapper").style.width=970;
//		document.getElementById("EduservTop").style.paddingRight=280;
		document.getElementById("EduservTop").style.paddingRight=170;
		document.getElementById("NavigationMenu").style.paddingRight=0;
	}
	else
	{
		Adjustment = NewContentWidth - 404;
		document.getElementById("ContentWidth").style.width=690+Adjustment;
		document.getElementById("content").style.width=492+Adjustment;
		document.getElementById("wrapper").style.width=970+Adjustment;
//		document.getElementById("EduservTop").style.paddingRight=280+Adjustment;
		document.getElementById("EduservTop").style.paddingRight=170+Adjustment;
		document.getElementById("NavigationMenu").style.paddingRight=Adjustment;
	}
	
}

// This function updates the card display
function updateSmallDisplay ()
{
	// document.write  ("updateSmallDisplay: ","card", "[",this.ImageName , "],[" + this.Width + "],[" + this.Height + "],[" + this.SmName , "],["  + this.SmWidth + "],[" + this.SmHeight + "],[" + this.TopColour + "],[" + this.BottomColour + "],[" + this.TopLine + "],[" + this.BottomLine + "],[" + this.VerseLine1 + "],[" + this.VerseLine2 + "],[" + this.VerseLine3 + "],[" + this.VerseLine4 + "]","<br/>");
	document.getElementById("Left").style.background="url('"+this.SmName+"')";
	document.getElementById("Left").style.width=this.SmWidth;
	document.getElementById("Left").style.height=this.SmHeight;
	document.getElementById("Right").style.width=this.SmWidth;
	document.getElementById("Right").style.height=this.SmHeight;
	document.getElementById("card").style.width=2*this.SmWidth+4;
	document.getElementById("card").style.height=parseInt(this.SmHeight)+2;
	document.getElementById("TopLine").style.height=this.SmHeight/2;
	document.getElementById("TopLine").style.color=this.TopColour;
	document.getElementById("BottomLine").style.color=this.BottomColour;
	//document.getElementById("card").style.fontSize="65%";
	//document.getElementById("card").style.lineHeight="65%";
	document.getElementById("card").style.fontSize=(100*this.SmWidth)/this.Width + "%";
	document.getElementById("card").style.lineHeight=(100*this.SmWidth)/this.Width + "%";

	// Update texts
	 var newText = document.createTextNode(this.TopLine);
	 var oldText = document.getElementById("TopLine").childNodes[0];
   	 document.getElementById("TopLine").replaceChild(newText, oldText);

	 var newText = document.createTextNode(this.BottomLine);
	 var oldText = document.getElementById("BottomLine").childNodes[0];
   	 document.getElementById("BottomLine").replaceChild(newText, oldText);

	 var newText = document.createTextNode(this.VerseLine1);
	 var oldText = document.getElementById("VerseLine1").childNodes[0];
	 document.getElementById("VerseLine1").replaceChild(newText, oldText);

	 var newText = document.createTextNode(this.VerseLine2);
	 var oldText = document.getElementById("VerseLine2").childNodes[0];
	 document.getElementById("VerseLine2").replaceChild(newText, oldText);

	 var newText = document.createTextNode(this.VerseLine3);
	 var oldText = document.getElementById("VerseLine3").childNodes[0];
	 document.getElementById("VerseLine3").replaceChild(newText, oldText);

	 var newText = document.createTextNode(this.VerseLine4);
	 var oldText = document.getElementById("VerseLine4").childNodes[0];
	 document.getElementById("VerseLine4").replaceChild(newText, oldText);
}

new Card ("",0,0,0,0,"","","","","","","",""); // Create a dummy one so it will work in Netscape.
Card.prototype.save = saveCard;
Card.prototype.get = getCard;
Card.prototype.updateDisplay = updateDisplay;
Card.prototype.updateSmallDisplay = updateSmallDisplay;

// End of Card object

// LastPage object

function LastPage (lastPageURL,lastPageName)
{
	this.lastPageURL = lastPageURL;
	this.lastPageName = lastPageName;
	if (lastPageURL.length > 0)
	{
		saveCookie ("last", lastPageURL+"]"+lastPageName);
	}
}

function getLastPage ()
{
	var Name = "last";
	var allCookies = document.cookie;
//	document.write ("LastPage, cookies= '", allCookies, "'<br/>");
	var pos = allCookies.indexOf (Name + "=");
	if (pos != -1)
	{
		var start = pos + Name.length + 1;
		var end = allCookies.indexOf(";", start);
		if (end == -1) end = allCookies.length;
		var value = allCookies.substring (start, end);
		value = unescape (value);
//		document.write (Name,  " = '", value, "'<br/>");
		this.lastPage = value;
		
		var pageInfo = /^([^\]]+)\](.+)/;
		var result = value.match (pageInfo);
		if (result != null)
		{
			//document.write ("Parsed:  0='", result [0], "', 1='", result [1], "', 2='", result [2], "', 3='", result [3], "', 4='", result [4], "', 5='", result [5], "', 6='", result [6], "', 7='", result [7], "', 8='", result [8], "', 9='", result [9], "', 10='", result [10], "', 11='", result [11], "', 12='", result [12], "', 13='", result [13], "', 14='", result [14], "' [", result.length,"]<br/>");
			if (result.length == 3)
			{
				this.lastPageURL = result [1];
				this.lastPageName = result [2];
			}
			else
			{
//				document.write ("Less than 3 items!<br/>");
				this.lastPageURL = "index.html";
				this.lastPageName = "home";
			}
		}
		else
		{
//			document.write ("page info is null!<br/>");
		}
	}
	else
	{
//		document.write ("No cookies!<br/>");
		this.lastPageURL = "index.html";
		this.lastPageName = "home";
	}
}

new LastPage ("",""); // Create a dummy one so it will work in Netscape.
LastPage.prototype.get = getLastPage;

// End of LastPage object

// Save a long-term cookie
function saveCookie (Name, Value)
{
	var nextYear = new Date();
	nextYear.setFullYear(nextYear.getFullYear() + 1);
//document.write ("saveCookie, value ='",Value,"'<br/>");
	document.cookie = Name + "=" + escape(Value) + "; expires=" + nextYear.toGMTString();
}

// Save a temporary cookie
function saveTempCookie (Name, Value)
{
//document.write ("saveCookie, value ='",Value,"'<br/>");
	document.cookie = Name + "=" + escape(Value);
}

//Check if a Cookie exists with the specified name
function existsCookie (Name)
{
	// If no name is specified, then return true
	if (Name.length == 0) return true;
	var allCookies = document.cookie;
	var pos = allCookies.indexOf (Name + "=");
	return (pos != -1);
}

function getCookie (Name)
{
	var allCookies = document.cookie;
//	document.write ("LastPage, cookies= '", allCookies, "'<br/>");
	var pos = allCookies.indexOf (Name + "=");
	if (pos != -1)
	{
		var start = pos + Name.length + 1;
		var end = allCookies.indexOf(";", start);
		if (end == -1) end = allCookies.length;
		var value = allCookies.substring (start, end);
		value = unescape (value);
//		document.write (Name,  " = '", value, "'<br/>");
		return value;
	}
	else
	{
//		document.write ("No cookies!<br/>");
		return "";
	}
}
// Code for making a greetings card

// This function swaps background image
function swapCardBackground(ImageName,TopColour,BottomColour,Width,Height,SmName,SmWidth,SmHeight)
{
	// Adjust widths of divs
	var NewContentWidth = 2*Width+4;
	if (NewContentWidth  <= 404)
	{
		document.getElementById("ContentWidth").style.width=690;
		document.getElementById("content").style.width=492;
		document.getElementById("wrapper").style.width=970;
//		document.getElementById("EduservTop").style.paddingRight=280;
		document.getElementById("EduservTop").style.paddingRight=170;
		document.getElementById("NavigationMenu").style.paddingRight=0;
	}
	else
	{
		Adjustment = NewContentWidth - 404;
		document.getElementById("ContentWidth").style.width=690+Adjustment;
		document.getElementById("content").style.width=492+Adjustment;
		document.getElementById("wrapper").style.width=970+Adjustment;
//		document.getElementById("EduservTop").style.paddingRight=280+Adjustment;
		document.getElementById("EduservTop").style.paddingRight=170+Adjustment;
		document.getElementById("NavigationMenu").style.paddingRight=Adjustment;
	}
	
	document.getElementById("Left").style.background="url('"+ImageName+"')";
	document.getElementById("Left").style.width=Width;
	document.getElementById("Left").style.height=Height;
	document.getElementById("Right").style.width=Width;
	document.getElementById("Right").style.height=Height;
	document.getElementById("card").style.width=2*Width+4;
	document.getElementById("card").style.height=Height+2;
	document.getElementById("TopLine").style.height=Height/2;
	document.getElementById("TopLine").style.color=TopColour;
	document.getElementById("BottomLine").style.color=BottomColour;
	card.ImageName = ImageName;
	card.TopColour = TopColour;
	card.BottomColour = BottomColour;
	card.Width = Width;
	card.Height = Height;
	card.SmName = SmName;
	card.SmWidth = SmWidth;
	card.SmHeight = SmHeight;
	card.save ();
}

// This function displays text from a drop-down menu
function displayText (Line,Id)
{
   	 var TextValue = "none";
	 for (i=0;(i<document.getElementById(Id).options.length) && !document.getElementById(Id).options[i].selected;i++);
	 if (i<document.getElementById(Id).options.length)
		TextValue = document.getElementById(Id).options[i].value;
	 if (TextValue == "OwnText") {
		document.getElementById(Id+"Specify").style.display="block";
		document.getElementById(Id+"SpecifyInput").focus();
		document.getElementById(Id+"SpecifyInput").select();
		return;
	 }
	 document.getElementById(Id+"Specify").style.display="none";
	 var newText = document.createTextNode(TextValue);
	 var oldText = document.getElementById(Line).childNodes[0];
   	 document.getElementById(Line).replaceChild(newText, oldText);
   	 if (Line == "TopLine")
		 card.TopLine = newText.nodeValue;
	else
		card.BottomLine = newText.nodeValue;
	card.save ();
}

// This function changes the text in an element to content from an input line
function swapTextLine(Line,TextID)
{
   	 var TextValue = document.getElementById(TextID).value;
	 var newText = document.createTextNode(TextValue);
	 var oldText = document.getElementById(Line).childNodes[0];
   	 document.getElementById(Line).replaceChild(newText, oldText);
   	 if (Line == "TopLine")
		 card.TopLine = newText.nodeValue;
	else
		card.BottomLine = newText.nodeValue;
	card.save ();
}

// This function displays the specified verse
function displayVerse (Side, Id)
{
   	 var VerseName = "none";
	 for (i=0;(i<document.getElementById(Id).options.length) && !document.getElementById(Id).options[i].selected;i++);
	 if (i<document.getElementById(Id).options.length)
		VerseName = document.getElementById(Id).options[i].value;
	 if (VerseName== "OwnVerse") {
		document.getElementById(Id+"Input").style.display="block";
		document.getElementById(Id+"Input1").focus();
		document.getElementById(Id+"Input1").select();
		return;
	 }
	 document.getElementById(Id+"Input").style.display="none";
	for (i=1;i<5;i++)
	{
		var VerseLine = document.getElementById(VerseName+i.toString()).firstChild.nodeValue;
		var newText = document.createTextNode(VerseLine);
		var oldText = document.getElementById(Side+"Line"+i.toString()).childNodes[0];
		document.getElementById(Side+"Line"+i.toString()).replaceChild(newText, oldText);
	}
	card.VerseLine1 = document.getElementById(VerseName+"1").firstChild.nodeValue;
	card.VerseLine2 = document.getElementById(VerseName+"2").firstChild.nodeValue;
	card.VerseLine3 = document.getElementById(VerseName+"3").firstChild.nodeValue;
	card.VerseLine4 = document.getElementById(VerseName+"4").firstChild.nodeValue;
	card.save();
}


// This function displays the specified verse from input lines
function swapVerse (Side, Id)
{
	for (i=1;i<5;i++)
	{
		 var VerseLine = document.getElementById(Id+i.toString()).value;
		var newText = document.createTextNode(VerseLine);
		var oldText = document.getElementById(Side+"Line"+i.toString()).childNodes[0];
		document.getElementById(Side+"Line"+i.toString()).replaceChild(newText, oldText);
	}
	card.VerseLine1 = document.getElementById(Id+"1").value;
	card.VerseLine2 = document.getElementById(Id+"2").value;
	card.VerseLine3 = document.getElementById(Id+"3").value;
	card.VerseLine4 = document.getElementById(Id+"4").value;
	card.save();
}

function updateTextMenu (menuID, value)
{
	var found = false;
	//document.write ("menuID = '",menuID,"', value='", value,"'<br/>");
	//document.write ("first option is '",document.getElementById(menuID).options[1].value,"'<br/>");
	for (i=0;(i<document.getElementById(menuID).options.length - 1);i++)
	{
		if (document.getElementById(menuID).options[i].value == value)
		{
			found = true;
			document.getElementById(menuID).options[i].selected = true;
		}
	}
	if (!found)
	{
		//document.write ("found = '",found,"'<br/>");
		document.getElementById(menuID).options[document.getElementById(menuID).options.length - 1].selected = true;
		document.getElementById(menuID+"Specify").style.display="block";
		document.getElementById(menuID+"SpecifyInput").value=value;
	}
}

function updateVerseMenu (menuID, line1, line2, line3, line4)
{
	var found = false;
	//document.write ("menuID = '",menuID,"', line1='", line1,"'<br/>");
	//document.write ("first option is '",document.getElementById(menuID).options[1].value,"'<br/>");
	for (i=0;(i<document.getElementById(menuID).options.length - 1);i++)
	{
		var VerseID = document.getElementById(menuID).options[i].value
		//document.write ("first line of verse '", VerseID, "' is '",document.getElementById(VerseID+"1").firstChild.nodeValue,"'<br/>");
		if (document.getElementById(VerseID+"1").firstChild.nodeValue== line1)
		{
			if ((document.getElementById(VerseID+"2").firstChild.nodeValue== line2) && (document.getElementById(VerseID+"3").firstChild.nodeValue== line3) && (document.getElementById(VerseID+"4").firstChild.nodeValue== line4))
			{
				found = true;
				document.getElementById(menuID).options[i].selected = true;
			}
		}
	}
	if (!found)
	{
		//document.write ("found = '",found,"'<br/>");
		document.getElementById(menuID).options[document.getElementById(menuID).options.length - 1].selected = true;
		document.getElementById(menuID+"Input").style.display="block";
		document.getElementById(menuID+"Input1").value=line1;
		document.getElementById(menuID+"Input2").value=line2;
		document.getElementById(menuID+"Input3").value=line3;
		document.getElementById(menuID+"Input4").value=line4;
	}
}
