 // javascript functions for a bookstore
  
var orders=new Array();	// unique id number for each book the customer orders

var quantity=new Array(); // quantities of each book stored in orders

var categories=new Array(); // store the names of the different categories
categories[0]=null; // not using category code 0
categories[1]="Computers";
categories[2]="Horror";
categories[3]="Sci-Fi";
categories[4]="Crime";
			
			
var books=new Array(); // create an array of books to be stored, 
					// values will be assigned in populateDB(), which will be called 
					// when the document is loaded

var imagesToggle=1; // set whether or not to display book cover images, can be changed
					// from the toggleImages() function

var layerState=0; // record whether the catalogue-> categories layer is shown or not
				// used in showCategories


// Purpose: will expose a hidden layer and move layer below it to produce a drop-down menu effect
//			also repostions the Images On/Off button, so it appears not to move
function showCategories() 
{

// get the three layers that need to be moved
lyr=document.getElementById("catalogue");
lyr2 = document.getElementById("subLinks");
lyr3 = document.getElementById("imagesButton");

	if(layerState==0)
	{
	// set the layer visible and move other layers appropriately
	lyr.style.visibility = "visible";
	lyr2.style.top = 200;
	lyr3.style.top = 145;

	layerState=1; // save layer state
	}
	else if(layerState==1)
	{
		// set the layer hidden and move other layers appropriately
		lyr.style.visibility = "hidden";
		lyr2.style.top = 75;
		lyr3.style.top= 270;

		layerState=0; // save layer state
	}

 
}

// Purpose: will update the quantity for a given book number
// Parameters: book number to be changed and new quantity/
function changeQuantity(bookNum, qty)
{
	for(i=0; i<orders.length; i++)
	{
		if(orders[i]==bookNum) // find what position bookNum is in
		{
			quantity[i]=qty;    // and update it's quantity
		}
	}
	setCookieArray("quantity", quantity);
}
			
// Purpose: add an order code to the orders array, 
//			also update the quantity for that order 
function addOrderCode(codeNumber, qty)
{
	len=orders.length;
	var codeExists=0;
	
	// check to make sure the order code hasnt already been added
	for(i=0; i<len; i++)
	{
		if(orders[i] == codeNumber)
		{
			codeExists=1;
		}
	}
    
	// if it doesnt exist, add it in
	if(!codeExists)
	{
		orders[len] = codeNumber;
		quantity[len] = qty;

		setCookieArray("codes", orders);
		setCookieArray("quantity", quantity);
	}
	else // if it does exist, display a message
	{
		alert("Selected Book has already been added to your Basket!\n" +
				"If you wish to change the quantity, please click View Basket. \n\n" +
				"Thank You!");
	}
				
}

// Purpose: Remove an order code from the orders array
function removeOrderCode(codeNumber)
{
	var temp=new Array();
	var qtemp=new Array();
	
	for(i=0; i<orders.length; i++)
	{
		if(orders[i] == codeNumber)
		{
			orders[i]='#'; // replace the order code to be removed with a #
			quantity[i]='#'; // as above in quanity
		}
		else
		{
			// move all the elements to be kept into temporary arrays
			temp[temp.length]=orders[i]; 
			qtemp[qtemp.length]=quantity[i];
		}
	}
	// copy temporary arrays back
	orders=temp;
	quantity=qtemp;
	
	// if there is no longer any books on order, delete the cookies
	if(orders.length==0)
	{
		deleteCookieArray("codes");
		deleteCookieArray("quantity");
	}
	else // if there is still books on order, update the cookies
	{
	setCookieArray("codes", orders);
	setCookieArray("quantity", quantity);
	}


}
// Purpose: will create a cookie with name=value and expires= today+expire
function setCookie(name, value, expire)
{
	if(value != '' )
	{
	expireDate = new Date();
	expireDate.setDate(expireDate.getDate() + expire) ;
	document.cookie = name + "=" + value + "; expires=" + expireDate.toGMTString();
	}
}

// Purpose: will retrieve a cookie with name
function getCookie(name) 
{

    var index = document.cookie.indexOf(name + "=");
    if (index == -1) 
		return null;

    index = document.cookie.indexOf("=", index) + 1; // first character

    var endstr = document.cookie.indexOf(";", index);

    if (endstr == -1) 
		endstr = document.cookie.length; // last character

    return unescape(document.cookie.substring(index, endstr));
  }

// Purpose: will create an 'array' within a cookie
// this is achieved by storing any array element array[i]
// as a cookie with name arrayi
function setCookieArray(name, array){
	var length=0;

	// get the array length, just using array.length in the for loop
	// seems to cause errors.
	while(array[length] != null)
		length++;

		// write each element of the array as a cookie
		for(i=0; i<length; i++)       
		{	
        data = array[i]	
        setCookie (name + i, data, 5);
        }        
 }

// Purpose: will retrieve a named array from a cookie
function getCookieArray(name)
{
    var i = 0;
	var arr=new Array();

        while (getCookie(name + i) != null)  // check to make sure cookie exists
		{
			// get all the array elements from the cookie
        arr[i] = getCookie(name + i);
        i++;  
        }
		return arr;
   }
// Purpose: will delete a cookie array
function deleteCookieArray(name)
{
	var i=0;
	while (getCookie(name + i) != null) // check to make sure the cookie exists
		{
        deleteCookie(name + i);
        i++; 
        }
}
// Purpose: delete an indiviual cookie, by setting its expiry date in the past
function deleteCookie(name)
{
	value='';
	expireDate = new Date();
	expireDate.setDate(expireDate.getDate() - 10) ; // set expiry date in the past

	document.cookie = name + "=" + value + "; expires=" + expireDate.toGMTString();
}


// Purpose: used upon document load to read in the required cookies, if they exist
function readOrderCodes() 
{
	orders=getCookieArray("codes");
	quantity=getCookieArray("quantity");
}

		

// Purpose: display a book based on it's unique number, will write
//		the book details, image of cover and current price to document

function displayBook(bookNum)
{
	var bookDetails=''; 
	
	for(i=1; i<5; i++)
		bookDetails+=books[bookNum][i];
	return bookDetails;
}

// Purpose: return a specific detail about a book
// Parameters: book number and detail number to display
function displayBookDetail(bookNum, detailNum)
{
	return books[bookNum][detailNum];
}

function getBookPrice(bookNum)
{
	return books[bookNum][2];
}

// Purpose: Remove the table named booktable
function removeTable()
{
	var mybody=document.getElementsByTagName("body").item(0);
	var mytable=document.getElementsByName("booktable").item(0);
	
	var myrow=document.getElementsByTagName("tr").item(0);

	while(myrow!=null)
	{
		var myrow=document.getElementsByTagName("tr").item(0);
		if(myrow!=null)
		{
		var row=myrow.parentNode.removeChild(myrow);
		}
	}
	if(mytable!=null)
	{
	var table=mytable.parentNode.removeChild(mytable);

	}

}

// Purpose: display the contents of the basket, using DOM

function viewBasket()
{	
	// orders array should be populated when the document loads, if it 
	// is still empty, then either there is no cookie set or the person
	// has not placed an order
	if(orders == '' || orders==null)
	{
		removeTable();
		mybody=document.getElementsByTagName("body").item(0);

		mytable = document.createElement("TABLE");
		mytable.setAttribute("name", "booktable");
		mytablebody = document.createElement("TBODY");
			
		mycurrent_row=document.createElement("TR");
		mycurrent_cell=document.createElement("TD");

		mylayer=document.createElement("DIV");
		mylayer.setAttribute("id", "content");
		 
		mylayer.style.marginTop = "200px";
		
		mylayer.style.marginLeft= "220px";
		
		header=document.createElement("H2");
		var	mytext=document.createTextNode("Your Basket is Empty");
		header.appendChild(mytext);
		mylayer.appendChild(header);

		mycurrent_cell.appendChild(mylayer);
		mycurrent_row.appendChild(mycurrent_cell);
		
		mytable.appendChild(mytablebody);
		mytable.setAttribute("border","0");
		
		
		mytable.appendChild(mylayer);
		mybody.appendChild(mytable);
	}
	else // if there IS currrent orders, display them
	{
	if(books == '' || books==null)
		populateDB(); // populate the books array with values

	removeTable(); // this will remove a table displaying books if it exists

	var mybody=document.getElementsByTagName("body").item(0);
	
		mylayer=document.createElement("DIV");
		mylayer.setAttribute("id", "content");
				 
		mylayer.style.marginTop = "200px";
		
		mylayer.style.marginLeft= "220px";
	
	mytable = document.createElement("TABLE");
	mytable.setAttribute("name", "booktable");
	mytable.setAttribute("cellpadding", "10");
	mytablebody = document.createElement("TBODY");

	mycurrent_row=document.createElement("TR");

	mycurrent_cell=document.createElement("TD");
	mycurrent_cell.setAttribute("colspan", 6 +  imagesToggle);
		mycurrent_header=document.createElement("H2");

		currenttext=document.createTextNode("Your Basket ::");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);
		
		mytablebody.appendChild(mycurrent_row);

	
	mycurrent_row=document.createElement("TR");

	if(imagesToggle)
	{
		mycurrent_cell=document.createElement("TD");
		mycurrent_row.appendChild(mycurrent_cell);
	}


		mycurrent_cell=document.createElement("TD");	
		mycurrent_cell.setAttribute("width", 150);
		currenttext=document.createTextNode("Book Title");
		mycurrent_header=document.createElement("H4");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		
		mycurrent_row.appendChild(mycurrent_cell);

		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 25);
		currenttext=document.createTextNode("Price (€)");
		mycurrent_header=document.createElement("H4");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);

		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 50);
		currenttext=document.createTextNode("Author");
		mycurrent_header=document.createElement("H4");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);


		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 120);
		currenttext=document.createTextNode("Other Details");
		mycurrent_header=document.createElement("H4");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);
		
		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 30);
		currenttext=document.createTextNode("Quantity");
		mycurrent_header=document.createElement("H4");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);
		
		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 50);
		currenttext=document.createTextNode("Total Cost (€)");
		mycurrent_header=document.createElement("H4");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);
		
		mytablebody.appendChild(mycurrent_row);

		mycurrent_form=document.createElement("FORM");
		mycurrent_form.setAttribute("name", "quantity_cost");
			
	for(j=0;j<orders.length;j++) // display all books in orders
	{
		v=orders[j];
		
	mycurrent_row=document.createElement("TR");
			
			if(imagesToggle)
		{
			mycurrent_cell=document.createElement("TD");
			
			my_image=document.createElement("IMG");
			mycurrent_cell.appendChild(my_image);
			
			mycurrent_row.appendChild(mycurrent_cell);

			my_image.setAttribute("src", books[v][5]);
			my_image.setAttribute("width", "80");
			my_image.style.visibility="visible";
		
		}
		mycurrent_cell=document.createElement("TD");

			mycurrent_input=document.createElement("INPUT");
			mycurrent_input.setAttribute("type", "hidden");
			mycurrent_input.setAttribute("name", "bookid");
			mycurrent_input.setAttribute("visibility", "hidden");
			mycurrent_input.setAttribute("value", displayBookDetail(v,0));
			
			mycurrent_cell.appendChild(mycurrent_input);

			
			mycurrent_cell.setAttribute("width", 150);
			mycurrent_cell.setAttribute("name", "bookname");
			currenttext=document.createTextNode(displayBookDetail(v,1));
			mycurrent_cell.appendChild(currenttext);
			
			mycurrent_row.appendChild(mycurrent_cell);
		
		
			mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("width", 25);
			mycurrent_cell.setAttribute("name", "bookprice");
			currenttext=document.createTextNode(displayBookDetail(v,2) );
			
			mycurrent_cell.appendChild(currenttext);
			mycurrent_row.appendChild(mycurrent_cell);

			mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("width", 50);
			currenttext=document.createTextNode(displayBookDetail(v,3));
			
			mycurrent_cell.appendChild(currenttext);
			mycurrent_row.appendChild(mycurrent_cell);

			
			mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("width", 120);
			currenttext=document.createTextNode(displayBookDetail(v,4));
			mycurrent_cell.appendChild(currenttext);
			mycurrent_row.appendChild(mycurrent_cell);
			
			mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("width", 30);
				
				mycurrent_input=document.createElement("INPUT");
				
				mycurrent_input.setAttribute("type", "text");
				mycurrent_input.setAttribute("name", "quantity");
				mycurrent_input.setAttribute("width", 30);
				mycurrent_input.setAttribute("size", 4);
				
				mycurrent_input.setAttribute("value", quantity[j]);
				
				mycurrent_input.setAttribute("onchange", "updateQuantity(" +books[v][0]+ ")" );
				
			mycurrent_cell.appendChild(mycurrent_input);				
			mycurrent_row.appendChild(mycurrent_cell);	
			
			mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("width", 50);
			
				mycurrent_input=document.createElement("INPUT");
				mycurrent_input.setAttribute("type", "text");
				mycurrent_input.setAttribute("name", "cost");
				mycurrent_input.setAttribute("width", 50);
				mycurrent_input.setAttribute("size", 6);
				mycurrent_input.setAttribute("disabled", "disabled");

			// set the value to the book price*quantity
			mycurrent_input.setAttribute("value", (getBookPrice(v)*quantity[j]).toFixed(2)); 
			
			mycurrent_cell.appendChild(mycurrent_input);

			mycurrent_input=document.createElement("INPUT");
			mycurrent_input.setAttribute("type", "button");
			mycurrent_input.setAttribute("value", "Delete");
			mycurrent_input.setAttribute("onclick", "removeOrderCode(" +orders[j]+ "), viewBasket()" );
			mycurrent_cell.appendChild(mycurrent_input);
			
			mycurrent_row.appendChild(mycurrent_cell);

		mytablebody.appendChild(mycurrent_row);
		mycurrent_row=document.createElement("TR");


		
	}
		
		mytable.appendChild(mycurrent_form);
			
			
		mytable.appendChild(mytablebody);
		mytable.setAttribute("border","0");
		
		mylayer.appendChild(mytable);
		mybody.appendChild(mylayer);
			
		
	}

} // viewBasket

// Purpose: update quantity for each book by reading new quantities
// 		from form input and updating quantity and cost boxes
//		will also remove a book from the order if the quantity
//		is set to 0.
function updateQuantity(bookNum)
{
	
		var mybody=document.getElementsByTagName("body").item(0);
		var myform=mybody.getElementsByTagName("form").item(1);
		var myinput=document.getElementsByTagName("input");

		var mycost=document.getElementsByName("cost");
		var myquan=document.getElementsByName("quantity");
		var mybookprice=document.getElementsByName("bookprice");
		var mybookid=document.getElementsByName("bookid");

		var elementPos=0;

		// find what position in the table this book occurs
		for(i=0; i<mybookid.length; i++)
		{
			if(mybookid.item(i).value==bookNum)
				elementPos=i;
		}
		// check to make sure the quantity is valid, if not give an error message
	if(myquan[elementPos].value%1 != 0)
	{
			alert("Error! " +myquan[elementPos].value+ " is not a valid quantity!");
			myquan[elementPos].value=quantity[elementPos]; // reset quantity to previous value
	}
	else if(myquan[elementPos].value <= 0)
	{
			alert("Error! " +myquan[elementPos].value+ " is not a valid quantity! \n\n If you wish to remove a book from \n\n" +
				"your order, then please use the delete button.");
			myquan[elementPos].value=quantity[elementPos]; // reset quantity to previous value
	}
	else // if quantity is valid, then update form and quantity cookie
	{		
		changeQuantity(bookNum, myquan[elementPos].value);

		setCookieArray("quantity", quantity);

		mycost[elementPos].value=myquan[elementPos].value * mybookprice[elementPos].innerHTML;
	}
		
}
// Purpose: toggle whether or not images are displayed
function toggleImages()
{
	if(imagesToggle) // if images are on, turn them off
	{
		imagesToggle=0;
			mytable=document.getElementsByTagName("table").item(0);
			// get all the img tags in the table
			coverImages=mytable.getElementsByTagName("img");
			
			for(i=0; i<coverImages.length; i++)
			{
				// set them all to hidden
				coverImages.item(i).style.visibility="hidden";
			}
	}
	else // if images are off, turn them on
		{
		imagesToggle=1;
		mytable=document.getElementsByTagName("table").item(0);
			coverImages=mytable.getElementsByTagName("img");
		
			for(i=0; i<coverImages.length; i++)
			{
				coverImages.item(i).style.visibility="visible";
			}
		}
}
// Purpose: will display a book category based on catNum, each book stores what catNum it belongs to
//			and this is checked to see if that book belongs to selected category
function displayCategory(catNum)
{	
		
	if(books == '' || books==null)
		populateDB(); // populate the books array with values, if it hasnt been done already

	removeTable(); // this will remove a table displaying books if it exists

	var mybody=document.getElementsByTagName("body").item(0);
	
		mylayer=document.createElement("DIV");
		mylayer.setAttribute("id", "content");
		 
		mylayer.style.marginTop = "200px";
		
		mylayer.style.marginLeft= "220px";
	
	mytable = document.createElement("TABLE");
	mytable.setAttribute("name", "booktable");
	mytable.setAttribute("cellpadding", 10);

	mytablebody = document.createElement("TBODY");
	
	mycurrent_row=document.createElement("TR");

	mycurrent_cell=document.createElement("TD");
	mycurrent_cell.setAttribute("colspan", 5 + imagesToggle);
		mycurrent_header=document.createElement("H2");

		currenttext=document.createTextNode("Books in Category :: " +categories[catNum]);
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);
		
		mytablebody.appendChild(mycurrent_row);

	mycurrent_row=document.createElement("TR");
	
		

	if(imagesToggle)
	{
		mycurrent_cell=document.createElement("TD");
		mycurrent_row.appendChild(mycurrent_cell);
	}
	
		mycurrent_cell=document.createElement("TD");	
		mycurrent_cell.setAttribute("width", 150);
		currenttext=document.createTextNode("Book Title");
		mycurrent_header=document.createElement("H4");
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_header.appendChild(currenttext);

		mycurrent_row.appendChild(mycurrent_cell);

		mycurrent_cell=document.createElement("TD");
		currenttext=document.createTextNode("Price (€) ");
		mycurrent_header=document.createElement("H4");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);

		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 80);
		currenttext=document.createTextNode("Author");
		mycurrent_header=document.createElement("H4");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		
		mycurrent_row.appendChild(mycurrent_cell);


		mycurrent_cell=document.createElement("TD");
		currenttext=document.createTextNode("Other Details");
		mycurrent_header=document.createElement("H4");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);
		
		mycurrent_cell=document.createElement("TD");
		currenttext=document.createTextNode("  ");
		mycurrent_cell.appendChild(currenttext);
		mycurrent_row.appendChild(mycurrent_cell);
		
		mytablebody.appendChild(mycurrent_row);
		mycurrent_row=document.createElement("TR");
			
			
		mycurrent_form=document.createElement("FORM");
		mycurrent_form.setAttribute("name", "quantity_cost");
			
	for(j=0;j<books.length;j++) 
	{
		
	if(books[j][6]==catNum) // check to see if the book corresponds to the selected category
	{
		v=j;
		
	
	mycurrent_row=document.createElement("TR");
		
			
		if(imagesToggle)
		{
			mycurrent_cell=document.createElement("TD");
			
			my_image=document.createElement("IMG");
			mycurrent_cell.appendChild(my_image);
			
			mycurrent_row.appendChild(mycurrent_cell);

			my_image.setAttribute("src", books[v][5]);
			my_image.setAttribute("width", "100");
			my_image.style.visibility="visible";
		}
			
			mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("width", 150);
			currenttext=document.createTextNode(displayBookDetail(v,1));
			mycurrent_cell.appendChild(currenttext);
			
			
			mycurrent_row.appendChild(mycurrent_cell);
		
		
			mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("name", "bookprice");
			currenttext=document.createTextNode(displayBookDetail(v,2) );
			mycurrent_cell.appendChild(currenttext);
			mycurrent_row.appendChild(mycurrent_cell);

			mycurrent_cell=document.createElement("TD");
				mycurrent_cell.setAttribute("width", 80);
			currenttext=document.createTextNode(displayBookDetail(v,3));
			mycurrent_cell.appendChild(currenttext);
			mycurrent_row.appendChild(mycurrent_cell);

			
			mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("width", 150);
			currenttext=document.createTextNode(displayBookDetail(v,4));
			mycurrent_cell.appendChild(currenttext);
			mycurrent_row.appendChild(mycurrent_cell);
			
			
			mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("width", 10);
			
				myform=document.createElement("FORM");
				myform.setAttribute("name", "addtobasket");		
				
				mycurrent_input=document.createElement("INPUT");
				mycurrent_input.setAttribute("type", "submit");



				mycurrent_input.setAttribute("onclick", "addOrderCode(" +books[v][0]+ ",1 ), displayCategory(" +books[v][6]+ ")" );
				mycurrent_input.setAttribute("value", "Add to Basket");

				myform.appendChild(mycurrent_input);
					
			mycurrent_cell.appendChild(myform);
			

		
			mycurrent_row.appendChild(mycurrent_cell);
		
		//}

		mytablebody.appendChild(mycurrent_row);
		}
	}
		
		mytable.appendChild(mycurrent_form);
			
			
		mytable.appendChild(mytablebody);
		mytable.setAttribute("border","0");
		
		mylayer.appendChild(mytable);
		mybody.appendChild(mylayer);
			
		
	//}

} // displayCategory()


// Purpose: display the checkout form using DOM
function checkout()
{
	if(orders.length==0)
	{
		alert("You have not selected any items to Purchase");
	}
	else
	{
		removeTable();

		var mybody=document.getElementsByTagName("body").item(0);
		
			mylayer=document.createElement("DIV");
			mylayer.setAttribute("id", "content");
					 
			mylayer.style.marginTop = "200px";
			
			mylayer.style.marginLeft= "220px";
				
		mytable = document.createElement("TABLE");
		mytable.setAttribute("name", "booktable");
		mytable.setAttribute("cellpadding", 10);
		mytable.setAttribute("width", 550);

		mytablebody = document.createElement("TBODY");
		
		// first row -- first name
		mycurrent_row=document.createElement("TR");
		

		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("colspan", 2);
		
			mycurrent_header=document.createElement("H3");

			currenttext=document.createTextNode("Enter Payment Details");
			mycurrent_header.appendChild(currenttext);
			mycurrent_cell.appendChild(mycurrent_header);
			mycurrent_row.appendChild(mycurrent_cell);
			mytablebody.appendChild(mycurrent_row);

		mycurrent_row=document.createElement("TR");
		
		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 80);

		myform=document.createElement("FORM");
		myform.setAttribute("name", "details");
		myform.setAttribute("action", "mailto:kd@iol.ie");
		myform.setAttribute("onsubmit", "validate()");
		
			
				myinput=document.createElement("INPUT");
				myinput.setAttribute("type", "text");
				myinput.setAttribute("name", "first");
				myinput.setAttribute("size", 15);
		
		myform.appendChild(myinput);
		currenttext=document.createTextNode("First Name: ");
		mycurrent_cell.appendChild(currenttext);

		mycurrent_cell2=document.createElement("TD");
		mycurrent_cell2.appendChild(myinput);
		mycurrent_row.appendChild(mycurrent_cell);	
		mycurrent_row.appendChild(mycurrent_cell2);	
			
		mytablebody.appendChild(mycurrent_row);

		// second row -- last name
		mycurrent_row=document.createElement("TR");
		
		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 80);	
			
				myinput=document.createElement("INPUT");
				myinput.setAttribute("type", "text");
				myinput.setAttribute("name", "last");
				myinput.setAttribute("size", 15);
		
		myform.appendChild(myinput);

		currenttext=document.createTextNode("Surname:     ");

		mycurrent_cell.appendChild(currenttext);

		mycurrent_cell2=document.createElement("TD");
		mycurrent_cell2.appendChild(myinput);
		mycurrent_row.appendChild(mycurrent_cell);	
		mycurrent_row.appendChild(mycurrent_cell2);
			
		mytablebody.appendChild(mycurrent_row);	

			
		// third row -- address
		mycurrent_row=document.createElement("TR");
		
		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 80);
		mycurrent_cell.setAttribute("valign", "top");
			
				myinput=document.createElement("TEXTAREA");
				myinput.setAttribute("name", "address");
		
		myform.appendChild(myinput);

		currenttext=document.createTextNode("Address:			");

			mycurrent_cell.appendChild(currenttext);

		mycurrent_cell2=document.createElement("TD");
		mycurrent_cell2.appendChild(myinput);
		mycurrent_row.appendChild(mycurrent_cell);	
		mycurrent_row.appendChild(mycurrent_cell2);	
			
		mytablebody.appendChild(mycurrent_row);
	  

		// fourth row -- card type
		mycurrent_row=document.createElement("TR");
		
		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 80);	

		myselect=document.createElement("SELECT");
		myselect.setAttribute("name", "cardtype");

			myoption=document.createElement("OPTION");
			myoption.setAttribute("value", "mastercard");

				optiontext=document.createTextNode("MasterCard");

			myoption.appendChild(optiontext);

		myselect.appendChild(myoption);

			myoption=document.createElement("OPTION");
			myoption.setAttribute("value", "visa");

				optiontext=document.createTextNode("Visa");

			myoption.appendChild(optiontext);

		myselect.appendChild(myoption);

		myform.appendChild(myselect);

		currenttext=document.createTextNode("Card Type:");

		mycurrent_cell.appendChild(currenttext);

		mycurrent_cell2=document.createElement("TD");
		mycurrent_cell2.appendChild(myselect);
		mycurrent_row.appendChild(mycurrent_cell);	
		mycurrent_row.appendChild(mycurrent_cell2);
			
		mytablebody.appendChild(mycurrent_row);	

		// fifth element -- expiry date
		ycurrent_row=document.createElement("TR");
		
		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 80);	

		myselect=document.createElement("SELECT");
		myselect.setAttribute("name", "cardmonth");

		for(i=1; i<13; i++)
		{
			myoption=document.createElement("OPTION");
			myoption.setAttribute("value", i);

				optiontext=document.createTextNode(i);

			myoption.appendChild(optiontext);

		myselect.appendChild(myoption);
		}
		myform.appendChild(myselect);

		myselect2=document.createElement("SELECT");
		myselect2.setAttribute("name", "cardyear");

		for(i=2005; i<2011; i++)
		{
			myoption=document.createElement("OPTION");
			myoption.setAttribute("value", i);

				optiontext=document.createTextNode(i);

			myoption.appendChild(optiontext);

		myselect2.appendChild(myoption);
		}
		

		myform.appendChild(myselect2);

		currenttext=document.createTextNode("Card Expiry:");

		mycurrent_cell.appendChild(currenttext);

		mycurrent_cell2=document.createElement("TD");
		mycurrent_cell2.appendChild(myselect);
		mycurrent_cell2.appendChild(myselect2);
		mycurrent_row.appendChild(mycurrent_cell);	
		mycurrent_row.appendChild(mycurrent_cell2);
			
		mytablebody.appendChild(mycurrent_row);	

		// sixth row -- card number
		mycurrent_row=document.createElement("TR");
		
		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 80);	
			
				myinput=document.createElement("INPUT");
				myinput.setAttribute("type", "text");
				myinput.setAttribute("name", "cardnumber");
				myinput.setAttribute("maxlength", 16);
				myinput.setAttribute("size", 16);
		
		myform.appendChild(myinput);

		currenttext=document.createTextNode("Card Number:");

		mycurrent_cell.appendChild(currenttext);

		mycurrent_cell2=document.createElement("TD");
		mycurrent_cell2.appendChild(myinput);
		mycurrent_row.appendChild(mycurrent_cell);	
		mycurrent_row.appendChild(mycurrent_cell2);
			
		mytablebody.appendChild(mycurrent_row);	

		// seventh row -- submit button
		mycurrent_row=document.createElement("TR");
		
		mycurrent_cell=document.createElement("TD");
		mycurrent_cell.setAttribute("width", 80);
			
				myinput=document.createElement("INPUT");
				myinput.setAttribute("type", "button");
				myinput.setAttribute("value", "Send Order");
				myinput.setAttribute("onclick", "validate()");
		
		myform.appendChild(myinput);

		currenttext=document.createTextNode(" ");

			mycurrent_cell.appendChild(currenttext);

		mycurrent_cell2=document.createElement("TD");
		mycurrent_cell2.appendChild(myinput);
		mycurrent_row.appendChild(mycurrent_cell);	
		mycurrent_row.appendChild(mycurrent_cell2);	
					
		mytablebody.appendChild(mycurrent_row);

		

		mytable.appendChild(mytablebody);
		mytable.appendChild(myform);
			mytable.setAttribute("border", 0);
			mylayer.appendChild(mytable);
			mybody.appendChild(mylayer); 
	}
	
}

// Purpose: gets all the data required and sends it by email
//			only called from validate, when all the required fields
//			have been check
function mailForm()
{
	address=document.getElementsByName('address').item(0).value;
	first=document.getElementsByName('first').item(0).value;
	last=document.getElementsByName('last').item(0).value;
	type=document.getElementsByName('cardtype').item(0).value;
	month=document.getElementsByName('cardmonth').item(0).value;
	year=document.getElementsByName('cardyear').item(0).value;
	cardNum=document.getElementsByName('cardnumber').item(0).value;
	order=getCookieArray("codes");
	quant=getCookieArray("quantity");

	window.location="mailto:kd@iol.ie?subject=Bookstore Order&body=" +first+ "<br>" +last+ "<br>" 
		+address+ "<br>" + type+ "<br>" + month + "<br>" + year+ "<br>" +cardNum+ "<br>" + order + "<br>" + quant;
}

// Purpose: will validate the form data when sending credit card details and name, address etc....
function validate()
{
	var firstOK;
	var lastOK;
	var addressOK;
	var cardNumOK;
	var expiryOK;
	var str='Invalid Data in form, please check the following areas: \n\n\n';

	var nameRef=/^[a-zA-Z]{1,32}$/; // create a regular expression to test the names
									// this will be only letters a-z or A-Z of length 1 or more
	var addressRef=/^[a-zA-Z0-9\s]{1,50}$/; // regular expression to test address
	var cardNumRef=/^[0-9]{16}$/; // regular expression to test card number
	var now= new Date();

	// get all the elements from the document
	address=document.getElementsByName('address').item(0).value;
	first=document.getElementsByName('first').item(0).value;
	last=document.getElementsByName('last').item(0).value;
	type=document.getElementsByName('cardtype').item(0).value;
	month=document.getElementsByName('cardmonth').item(0).value;
	year=document.getElementsByName('cardyear').item(0).value;
	cardNum=document.getElementsByName('cardnumber').item(0).value;

	// check do the elements comply with the regular expressions above
	if(nameRef.test(first))
		{
			firstOK=1;
		}
		else
		{
			firstOK=0;
		}

	if(nameRef.test(last))
		{
			lastOK=1;
		}
		else
		{
			lastOK=0;
		}

	if(addressRef.test(address))
		{ 
			addressOK=1;
		}
		else
		{
			addressOK=0;
		}
		
	if(cardNumRef.test(cardNum))
		{
			cardNumOK=1;
		}
		else
		{
			cardNumOK=0;
		}
	
	if(firstOK && lastOK && addressOK && cardNumOK)
	{
		// if all elements are ok, send the details by email
			mailForm();
	}
	else // otherwise, give an alert specifying whats wrong with the details
	{
		if(!firstOK)
		{
			str+="  First Name  \n\n";
		}
		if(!lastOK)
		{
			str+="  Last Name  \n\n";
		}
		if(!addressOK)
		{
			str+="  Address  \n\n";
		}
		if(!cardNumOK)
		{
			str+=" Card Number  \n\n";
		}
		if(year == now.getYear()+1900 && month < now.getMonth()+1)
		{
			str+=" Card is Expired!  \n\n";
		}
	

		alert(str);
	}
	
	return false; // returns false if the form is not validated and e-mailed



}
// Purpose: Will display the a random book from the array 
//			and also the number of books in the basket
function displayIndex()
{
	bookNum=Math.random()*books.length;
	bookNum=Math.round(bookNum);

	var mybody=document.getElementsByTagName("body").item(0);
	
		mylayer=document.createElement("DIV");
		mylayer.setAttribute("id", "content");
				 
		mylayer.style.marginTop = "200px";
		
		mylayer.style.marginLeft= "220px";
			
	mytable = document.createElement("TABLE");
	mytable.setAttribute("name", "booktable");
	mytable.setAttribute("cellpadding", 10);
	mytable.setAttribute("width", 550);

	mytablebody = document.createElement("TBODY");

	mycurrent_row=document.createElement("TR");
	

	mycurrent_cell=document.createElement("TD");
	mycurrent_cell.setAttribute("colspan", 2);
	
		mycurrent_header=document.createElement("H3");

		currenttext=document.createTextNode("Welcome to The Bookstore, please have a look around!");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);
		mytablebody.appendChild(mycurrent_row);

	
	mycurrent_row=document.createElement("TR");
	

	mycurrent_cell=document.createElement("TD");
	mycurrent_cell.setAttribute("colspan", 2);
	
		mycurrent_header=document.createElement("H3");

		currenttext=document.createTextNode("Feaured Book :: \"" +books[bookNum][1] +"\" ");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);
		mytablebody.appendChild(mycurrent_row);

	mycurrent_row=document.createElement("TR");
	
	mycurrent_cell=document.createElement("TD");
	mycurrent_cell.setAttribute("rowspan", 2);
		myimage=document.createElement("IMG");
		myimage.setAttribute("src", books[bookNum][5]);
		myimage.setAttribute("height", 200);

		mycurrent_cell.appendChild(myimage);
		mycurrent_row.appendChild(mycurrent_cell);
		
		mytablebody.appendChild(mycurrent_row);

	mycurrent_row=document.createElement("TR");

	mycurrent_cell=document.createElement("TD");
		mycurrent_header=document.createElement("H3");

		currenttext=document.createTextNode("Author: " +books[bookNum][3]+ ", " +books[bookNum][4]+ "   Price:  €" +books[bookNum][2]);
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);
		mytablebody.appendChild(mycurrent_row);

		mycurrent_row=document.createElement("TR");

		mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("colspan", 2);
			mycurrent_cell.setAttribute("align", "right");

			
				myform=document.createElement("FORM");
				myform.setAttribute("name", "addtobasket");		
				
				mycurrent_input=document.createElement("INPUT");
				mycurrent_input.setAttribute("type", "button");
				mycurrent_input.setAttribute("onclick", "addOrderCode(" +books[bookNum][0]+ ",1)" );
				mycurrent_input.setAttribute("value", "Add to Basket");

				myform.appendChild(mycurrent_input);
					
			mycurrent_cell.appendChild(myform);
			

		
			mycurrent_row.appendChild(mycurrent_cell);
			mytablebody.appendChild(mycurrent_row);

			mycurrent_row=document.createElement("TR");
	

			mycurrent_cell=document.createElement("TD");
			mycurrent_cell.setAttribute("colspan", 2);
	
			mycurrent_header=document.createElement("H3");

		currenttext=document.createTextNode("Currently " + orders.length +" item(s) in basket");
		mycurrent_header.appendChild(currenttext);
		mycurrent_cell.appendChild(mycurrent_header);
		mycurrent_row.appendChild(mycurrent_cell);
		mytablebody.appendChild(mycurrent_row);


		mytable.appendChild(mytablebody);
		mytable.setAttribute("border", 0);
		mylayer.appendChild(mytable);
		mybody.appendChild(mylayer); 
		
		
}// displayIndex




// Purpose: creates an array called books, which contains all the data for the books
//			called when document is loaded
function populateDB()
{

books[0]=new Array(7);
books[0][0]="0"; // the unique id code for every book
books[0][1]="The Dark Tower: The Gunslinger"; // book name
books[0][2]="12.99";	// book price
books[0][3]="Stephen King"; // book author
books[0][4]="ISBN: 1231355, Other details...."; // any other relevant details
books[0][5]="images/horror/dt1.gif";
books[0][6]="2";

books[1]=new Array(7);
books[1][0]="1"; // the unique id code for every book
books[1][1]="The Dark Tower: The Drawing of The Three"; // book name
books[1][2]="12.99";	// book price
books[1][3]="Stephen King"; // book author
books[1][4]="ISBN: 1233465, Other details...."; // any other relevant details
books[1][5]="images/horror/dt2.jpg";
books[1][6]="2";


books[2]=new Array(7);
books[2][0]="2"; // the unique id code for every book
books[2][1]="The Dark Tower: The Waste Lands"; // book name
books[2][2]="12.99";	// book price
books[2][3]="Stephen King"; // book author
books[2][4]="ISBN: 122355, Other details...."; // any other relevant details
books[2][5]="images/horror/dt3.jpg";
books[2][6]="2";

books[3]=new Array(7);
books[3][0]="3"; // the unique id code for every book
books[3][1]="The Dark Tower: Wizard and Glass"; // book name
books[3][2]="12.99";	// book price
books[3][3]="Stephen King"; // book author
books[3][4]="ISBN: 1568855, Other details...."; // any other relevant details
books[3][5]="images/horror/dt4.jpg";
books[3][6]="2";


books[4]=new Array(7);
books[4][0]="4"; // the unique id code for every book
books[4][1]="The Dark Tower: Wolves Of The Calla"; // book name
books[4][2]="12.99";	// book price
books[4][3]="Stephen King"; // book author
books[4][4]="ISBN: 1239855, Other details...."; // any other relevant details
books[4][5]="images/horror/dt5.jpg";
books[4][6]="2";


books[5]=new Array(7);
books[5][0]="5"; // the unique id code for every book
books[5][1]="The Dark Tower: Song of Susannah"; // book name
books[5][2]="12.99";	// book price
books[5][3]="Stephen King"; // book author
books[5][4]="ISBN: 12397675, Other details...."; // any other relevant details
books[5][5]="images/horror/dt6.jpg";
books[5][6]="2";


books[6]=new Array(7);
books[6][0]="6"; // the unique id code for every book
books[6][1]="The Dark Tower: The Dark Tower"; // book name
books[6][2]="12.99";	// book price
books[6][3]="Stephen King"; // book author
books[6][4]="ISBN: 12325675, The Last book in the Quest for the Elusive Dark Tower"; // any other relevant details
books[6][5]="images/horror/dt7.jpg";
books[6][6]="2";

books[7]=new Array(7);
books[7][0]="7";
books[7][1]="Windows XP for Dummies";
books[7][2]="16.99";
books[7][3]="Andy Rathbone";
books[7][4]="ISBN: 0764573235, '...sure to be a best seller!....' ";
books[7][5]="images/computer/xpdummies.jpg";
books[7][6]="1";

books[8]=new Array(7);
books[8][0]="8";
books[8][1]="PHP and MySQL Web Development";
books[8][2]="36.50";
books[8][3]="Luke Welling, Laura Thomson";
books[8][4]="ISBN: 0672326728, Appropriate for all courses in Web development that utilize PHP scripting and MySQL databases.";
books[8][5]="images/computer/phpmysql.jpg";
books[8][6]="1";

books[9]=new Array(7);
books[9][0]="9";
books[9][1]="Red Hat Fedora Linux 3 Bible";
books[9][2]="33.99";
books[9][3]="Christopher Negus";
books[9][4]="ISBN: 0764578723";
books[9][5]="images/computer/rhfedora.jpg";
books[9][6]="1";

books[10]=new Array(7);
books[10][0]="10";
books[10][1]="Google Hacks";
books[10][2]="17.50";
books[10][3]="Rael Dornfest, Tara Calishain";
books[10][4]="Publisher: O'Reilly UK,  ISBN: 0596008570";
books[10][5]="images/computer/googlehack.jpg";
books[10][6]="1";

books[11]=new Array(7);
books[11][0]="11";
books[11][1]="The Hitch Hiker's Guide to the Galaxy: A Trilogy in Five Parts";
books[11][2]="15.99";
books[11][3]="Douglas Adams";
books[11][4]="Publisher: William Heinemann, ISBN: 0434003484";
books[11][5]="images/scifi/hhgg.jpg";
books[11][6]="3";

books[12]=new Array(7);
books[12][0]="12";
books[12][1]="'Star Trek' Encyclopedia: A Reference Guide to the Future";
books[12][2]="20.00";
books[12][3]="Michael Okuda";
books[12][4]="ISBN: 0671536095";
books[12][5]="images/scifi/stency.jpg";
books[12][6]="3";

books[13]=new Array(7);
books[13][0]="13";
books[13][1]="Seven of Nine (Star Trek: Voyager S.) ";
books[13][2]="12.99";
books[13][3]="Christie Golden";
books[13][4]="ISBN: 0671024914";
books[13][5]="images/scifi/stv7of9.jpg";
books[13][6]="3";

books[14]=new Array(7);
books[14][0]="14";
books[14][1]="The Da Vinci Code";
books[14][2]="6.99";
books[14][3]="Dan Brown";
books[14][4]="ISBN: 0552149519, ";
books[14][5]="images/crime/davinci.jpg";
books[14][6]="4";

books[15]=new Array(7);
books[15][0]="15";
books[15][1]="Digital Fortress";
books[15][2]="6.99";
books[15][3]="Dan Brown";
books[15][4]="ISBN: 0552151696";
books[15][5]="images/crime/digfortress.jpg";
books[15][6]="4";

/*
books[x]=new Array(7);
books[x][0]="x";
books[x][1]=" name";
books[x][2]="price";
books[x][3]="author";
books[x][4]="other";
books[x][5]="images/";
books[x][6]="catNum";
*/





}

