function exTitlWord(word)
{
	var w;

	if (word.length < 2)
		return(true);
	else if (word.length > 6)
		return(false);
	else
	{
		w = word.toUpperCase();

		if (w.substr(0, 1) == "A")
			return (w == "A" || w == "AND" || w == "AT" || w == "about" || w == "AN" || w == "AS");
		else if (w.substr(0, 1) == "B")
			return (w == "BOOK" || w == "BOOKS" || w == "BY");
		else if (w.substr(0, 1) == "D")
			return (w == "DE");
		else if (w.substr(0, 1) == "F")
			return (w == "FOR" || w == "FROM");
		else if (w.substr(0, 1) == "G")
			return (w == "GUIDE" || w == "GUIDES");
		else if (w.substr(0, 1) == "H")
			return (w == "HER" || w == "HIS" || w == "HOW");
		else if (w.substr(0, 1) == "I")
			return (w == "IN" || w == "II" || w == "IS" || w == "IT");
		else if (w.substr(0, 1) == "M")
			return (w == "ME" || w == "MY");
		else if (w.substr(0, 1) == "O")
			return (w == "OF" || w == "ON" || w == "OUR" || w == "OVER");
		else if (w.substr(0, 1) == "T")
			return (w == "THE" || w == "TO" || w == "THAT" || w == "THIS");
		else if (w.substr(0, 1) == "U")
			return (w == "UP");
		else if (w.substr(0, 1) == "V")
			return (w == "VOL");
		else if (w.substr(0, 1) == "W")
			return (w == "WITH" || w == "WHO");
		else if (w.substr(0, 1) == "Y")
			return (w == "YOU" || w == "YOUR");
		else
			return(false);
	}
}

function exPublWord(word)
{
	var w;

	if (word.length < 2)
		return(true);
	else
	{
		w = word.toUpperCase();

		if (w.substr(0, 1) == "A")
			return (w == "A" || w == "AN" || w == "AND" || w == "AT");
		else if (w.substr(0, 1) == "C")
			return (w == "CO" || w == "COMPANY");
		else if (w.substr(0, 1) == "E")
			return (w == "EDITIONS");
		else if (w.substr(0, 1) == "F")
			return (w == "FOR" || w == "FROM");
		else if (w.substr(0, 1) == "G")
			return (w == "GROUP" || w == "GUIDES");
		else if (w.substr(0, 1) == "I")
			return (w == "IN"|| w == "INTERNATIONAL");
		else if (w.substr(0, 1) == "L")
			return (w == "LTD"|| w == "LIMITED");
		else if (w.substr(0, 1) == "O")
			return (w == "OF" || w == "ON");
		else if (w.substr(0, 1) == "P")
			return (w == "PAPERBACK" || w == "PAPERBACKS" || w == "PR" || w == "PRESS" || w == "PRODUCTIONS" || 
							w == "PUBLICATIONS" || w == "PUBLISHERS" || w == "PUBLISHING");
		else if (w.substr(0, 1) == "S")
			return (w == "SONS");
		else if (w.substr(0, 1) == "T")
			return (w == "THE" || w == "TO");
		else if (w.substr(0, 1) == "U")
			return (w == "U" || w == "UK" || w == "US" || w == "USA" || w == "UNIVERSITY");
		else if (w.substr(0, 1) == "W")
			return (w == "WITH");
		else
			return(false);
	}
}

function allExcluded(textString, k)
{
	var i, textWords, text;

	text = stripString(textString);
	if (text == "") return(false);
	textWords = text.split(" ");

	if (k == 1)
	{
		for (i = 0; i < textWords.length; i++) 
			if (!exTitlWord(textWords[i]) && textWords[i] != "") return(false);
	}
	else
	{
		for (i = 0; i < textWords.length; i++) 
			if (!exPublWord(textWords[i]) && textWords[i] != "") return(false);
	}

	return(true);
}

function isTAPString(x)
{
	var i;

	if (x == "") return(true);
	for (i = 0; i < x.length; i++) if (!isLetter(x.substr(i, 1)) && x.substr(i, 1) != " ") return(false);
	return(true);
} 

function isNumberString(x)
{
	var i;

	if (x == "") return(true);

	for (i = 0; i < x.length; i++)
		if (!isDigit(x.substr(i, 1)) && x.substr(i, 1) != " ") return(false);

	return(true);
} 

function isIsbnString(x)
{
	var i, xWords, count;

	if (stripString(x) == "") 
		return(true);
	else if (!isNumberString(x.substr(0, x.length - 2)))
		return(false);
	else if (!isDigit(x.substr(x.length - 1, 1)) && x.substr(x.length - 1, 1) != "X" && x.substr(x.length - 1, 1) != "x")
		return(false);
	else
	{
		xWords = x.split(" ");
		for (i = 0, count = 0; i < xWords.length; i++) if (xWords[i] != "") count++;
	}

	return(count < 2);
} 

function formatPrice(x)
{
	var i, k, z;

	if (x == null || x == "")
		return("");
	else
	{
		for (i = 0, k = -1; i < x.length; i++)
			if (x.substr(i, 1) == ".")
			{
				k = i;
				break;
			}

		if (k == -1) 
			z = x + ".00";
		else if (k == x.length - 1)
			z = x + "00";
		else if (k == x.length - 2)
			z = x + "0";
		else
			z = x;

		return("£" + z);
	}
} 

function formatPriceB(x)
{
	var i, k, z;

	if (x == null || x == "")
		return("");
	else
	{
		for (i = 0, k = -1; i < x.length; i++)
			if (x.substr(i, 1) == ".")
			{
				k = i;
				break;
			}

		if (k == -1) 
			z = x + ".00";
		else if (k == x.length - 1)
			z = x + "00";
		else if (k == x.length - 2)
			z = x + "0";
		else
			z = x;

		return(z);
	}
} 

function formatDate(x)
{
	var month, year;

	if (x == null || x == "")
		return("");
	else
	{
		year = x.substr(0, 4);
		month = parseInt(x.substr(5, 2));

		if (month == 1)
			month = "January ";
		else if (month == 2)
			month = "February ";
		else if (month == 3)
			month = "March ";
		else if (month == 4)
			month = "April ";
		else if (month == 5)
			month = "May ";
		else if (month == 6)
			month = "June ";
		else if (month == 7)
			month = "July ";
		else if (month == 8)
			month = "August ";
		else if (month == 9)
			month = "September ";
		else if (month == 10)
			month = "October ";
		else if (month == 11)
			month = "November ";
		else if (month == 12)
			month = "December ";
		else
			month = "";

		return(month + year);
	}
} 

function aboutWindow(item)
{
	var objWin, winName, aboutFile, height;

	if (item == 0)
	{
		winName = "aboutSearching";
		aboutFile = "aboutSearching.html";
		height = 425;
	}
	else if (item == 1)
	{
		winName = "aboutOrdering";
		aboutFile = "aboutOrdering.html";
		height = 425;
	}
	else if (item == 2)
	{
		winName = "aboutMail";
		aboutFile = "aboutMail.html";
		height = 400;
	}
	else if (item == 3)
	{
		winName = "aboutCheckout";
		aboutFile = "aboutCheckout.html";
		height = 375;
	}
	else if (item == 4)
	{
		winName = "aboutProblems";
		aboutFile = "aboutProblems.html";
		height = 375;
	}

	objWin = window.open("", winName, "width=500, height=" + height + 
									", left=0, top=0, toolbar=no, status=no, resizeable=no");
	objWin.document.write(getFile(aboutFile));
	return;
}

function getText(xml, tag1, tag2)
{
	var k1, k2, text;

	text = "";

	if (xml != null && xml != "")
	{
		k1 = xml.search(tag1);
		k2 = xml.search(tag2);
		if (k1 != -1 && k2 != -1) text = xml.slice(k1 + tag1.length, k2);
	}

	return(text);
}

function getAuthors(xml)
{
	var text, more, author, authors;

	text = xml;
	more = true;
	authors = "";

	while(more)
	{
		author = getText(text, "<Author>", "</Author>") + ", ";

		if (author == ", ")
			more = false;
		else
		{
			authors += author;
			text = text.substr(text.search("</Author>") + 5, text.length);
		}
	}

	authors = authors.substring(0, authors.length - 2);
	return(authors);
}

function getAmazon(isbn)
{
	var objXml, data;

	data = "";
	objXml = new ActiveXObject("Microsoft.XMLHTTP"); 
	objXml.open("GET", "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService" + 
						"&AWSAccessKeyId=00B5KD1X16J6S3DG5PR2&Operation=ItemLookup&ResponseGroup=Large&ItemId=" + isbn, false); 
	objXml.onreadystatechange = function() {if (objXml.readyState == 4) data = objXml.responseText;}
 	objXml.send(null); 
	return(data);
}

function viewBasket()
{
	var i, k, order, item, cookieText, html, html1, html2, html3, objWin, total, postage, id, author, title, price, 
		quantity, pText, tText, pound, region, basketChars, code;

	cookieText = getCookie("bookOrder");

	if (cookieText == "")
		alert("There are no items in your basket at the moment");
	else
	{
		html = getFile("bookBasket.html");
		k = html.search("Price</strong>") + 46;
		html1 = html.substr(0, k);
		html3 = html.substr(k, html.length);

		order = cookieText.split("~");
		pound = "";

		for (i = 0, total = 0, html = html1; i < order.length; i++)
		{
			item =  order[i].split("|");
			id = item[0];
			author = item[1];
			title = item[2];
			price = item[3];
			quantity = item[4];
			if (pound == "") pound = price.substr(0, 1);
			total += (price == "") ? 0 : parseFloat(price.substr(1, price.length))*parseInt(quantity);

			html2 = '<tr><td><font size="2" face="Arial">' + id + '</font></td>' + 
							'<td><font size="2" face="Arial">' + author + '</font></td>' +
							'<td><font size="2" face="Arial">' + title + '</font></td>' +
							'<td align="center"><font size="2" face="Arial">' + quantity + '</font></td>' +
							'<td align="right"><font size="2" face="Arial">' + price + '</font></td></tr>';

			html += html2;
		}

		region = getCookie("register").split(";")[9];

		if (region == 1)
			postage = 3;
		else if (region == 2)
			postage = 7;
		else if (total > 30)
			postage = 0;
		else
			postage = 2;

		postage = 0;

		total += postage;
		pText = pound + formatPriceB(postage.toString());
		total = Math.round(100*total)/100;
		tText = pound + formatPriceB(total.toString());
		html3 = html3.replace("XXXPOSTAGEXXX", pText);
		html3 = html3.replace("XXXTOTALXXX", tText);
		html += html3;

		basketChars = "ABCDEFGHJKLMNPQRSTUWXYZ23456789";
		for (i = 0, code = ""; i < 4; i++) code += basketChars.substr(Math.floor(Math.random()*31), 1);

		objWin = window.open("", "bookBasket" + code.substr(2, code.length), 
						"width=800, height=400, left=25, top=25, toolbar=no, status=no, resizeable=yes, scrollbars");

		objWin.document.write(html);
	}

 	return;
}

function getOrder()
{
	var i, cookieText, regList, t, orderChars, code, address, newLine, objWin, html, order, item, total, postage, 
		id, price, quantity, tText, pound, region, skuList;

	html = getFile("bookOrder.html")

	cookieText = getCookie("bookOrder");

	order = cookieText.split("~");
	pound = "";

	for (i = 0, total = 0; i < order.length; i++)
	{
		item =  order[i].split("|");
		id = item[0];
		price = item[3];
		quantity = item[4];
		skuList = (i == 0) ? id + "(" + quantity + ")" : skuList + ", " + id + "(" + quantity + ")";
		if (pound == "") pound = price.substr(0, 1);
		total += (price == "") ? 0 : parseFloat(price.substr(1, price.length))*parseInt(quantity);
	}

	region = getCookie("register").split(";")[9];

	if (region == 1)
		postage = 3;
	else if (region == 2)
		postage = 7;
	else if (total > 30)
		postage = 0;
	else
		postage = 2;

	postage = 0;

	total += postage;
	total = Math.round(100*total)/100;
	tText = pound + formatPriceB(total.toString());

	html = html.replace("XXXIDXXX", skuList);
	html = html.replace("XXXTOTALXXX", tText);

	cookieText = getCookie("register");

	if (cookieText != "")
	{
		t = new Array();
		t[0] = "Mr";
		t[1] = "Mrs";
		t[2] = "Miss";
		t[3] = "Ms";
		t[4] = "Dr";
		t[5] = "Prof";
		t[6] = "Rev";
		t[7] = "Lord";
		t[8] = "Lady";
		t[9] = "Sir";

		newLine = String.fromCharCode(13);

		regList = cookieText.split(";");
		address = t[regList[0]] + ". " + regList[1] + " " + regList[2] + newLine + regList[3] + newLine;
		if (regList[4] != "") address += regList[4] + newLine;
		address += regList[5];
		if (regList[6] != "") address +=  newLine + regList[6];
		address += " " + regList[7] + newLine + regList[8];
		html = html.replace("XXXCONTACTXXX", address);
		html = html.replace("XXXDAYTELXXX", regList[10]);
		html = html.replace("XXXEVETELXXX", regList[11]);
		html = html.replace("XXXEMAILXXX", regList[12]);
	}

	orderChars = "ABCDEFGHJKLMNPQRSTUWXYZ23456789";
	for (i = 0, code = "B-"; i < 4; i++) code += orderChars.substr(Math.floor(Math.random()*31), 1);
	html = html.replace("XXXCODEXXX", code);

	objWin = window.open("bookOrder.html", "bookOrder" + code.substr(2, code.length), 
							"width=700, height=600, left=50, top=50, toolbar=no, status=no, resizeable=yes, scrollbars");

	objWin.document.write(html);

	return;
}

function getBasket(html)
{
	var i, order, item, cookieText, total, postage, id, price, tText, pound, region, skuList;

	cookieText = getCookie("bookOrder");

	order = cookieText.split("~");
	pound = "";

	for (i = 0, total = 0; i < order.length; i++)
	{
		item =  order[i].split("|");
		id = item[0];
		price = item[3];
		skuList = (i == 0) ? id : skuList + ", " + id;
		if (pound == "") pound = price.substr(0, 1);
		total += (price == "") ? 0 : parseFloat(price.substr(1, price.length));
	}

	region = getCookie("register").split(";")[9];

	if (region == 1)
		postage = 3;
	else if (region == 2)
		postage = 7;
	else if (total > 30)
		postage = 0;
	else
		postage = 2;

	postage = 0;

	total += postage;
	total = Math.round(100*total)/100;
	tText = pound + formatPriceB(total.toString());

	objWin.document.frmOrder.SKUs.value = skuList;
	objWin.document.frmOrder.Value.value = tText;

 	return;
}

function goToCheckout()
{
	var cookieText;

	cookieText= getCookie("register");

	if (cookieText == "")
		alert("You need to register before continuing with online ordering");
	else
	{
		cookieText = getCookie("bookOrder");

		if (cookieText == "")
			alert("There are no items in your shopping basket");
		else
			getOrder();
	}

	return;
}
