// JavaScript Document

// Create a library for the hypnobooks
// default shipping is currently set to 15 for US Airmail. Change this number
// if you change the default shipping type
hypno = new library('hypno',15);
leader = new library('leader',15);

// for the HYPNOBOOKS library ...
// add products to the library. Needs stock number, product name, product price
// items can be added in any order - though the order in which they appear here
// is the order in which they'll appear in the drop down lists
hypno.add("hyp01","Sleeping like a baby",29.95);
hypno.add("hyp02","Rising above pain",29.95);
hypno.add("hyp03","Growing in self esteem",29.95);
hypno.add("hyp04","Riding the waves",29.95);
hypno.add("hyp05","Hypnosis as a psychotherapy tool",24.95);
hypno.add("hyp06","Harnessing the Torrent",29.95);
hypno.add("hyp07","Journey to Healing",29.95);
hypno.add("hyp08","Diverting from Depression",29.95);
hypno.add("hyp09","Flight to Delight",29.95);
hypno.add("hyp10","Overcoming Anxiety",29.95);

// for the LEADERSHIP library ...
// add products to the library. Needs stock number, product name, product price
// items can be added in any order - though the order in which they appear here
// is the order in which they'll appear in the drop down lists
leader.add("ldr01","Leadership: How to get the Very Best...",49.95);
leader.add("ldr02","Leadership: Trainers Handbook",34.95);
// put a dividing line in here
leader.add("ldr03","Leading Psychotherapy Groups: Becoming an expert group therapist",275.00);
leader.add("ldr04","Leading Psychotherapy Groups: Administrators Handbook",150.00);
// don't touch anything below this line!!!

myformcgi = "http://localhost/cgi-bin/lsl/";

function library(name,shipping)
{
	this.product	= new Array();
	this.products	= 0;
	this.total		= 0;
	this.name		= name;
	this.shipping	= shipping;
	this.add		= add_product;
	this.droplist	= make_droplist;
	this.checklist	= make_checklist;
	this.totalcost	= total_cost;
}

function change_totalcost_display(lib)
{
	total = lib.total + lib.shipping;
	if(lib.total == 0)	{ total = 0; }
	display = "$" + total + ".00 (AUD)";
	if(document.layers)
	{
		document.layers.pricebox.document.write(display);
	}
	else
	{
		if(document.all)
		{
			document.all.pricebox.innerHTML = display;
		}
		else
		{
			elid = document.getElementById("pricebox");
			elid.title = display;
			elid.firstChild.data = display;
		}
	}
	document.orderform.totalprice.value = total;
}

function total_cost()
{
	this.total = 0;
	for(f=0;f<library.products;f++)
	{
		if(library.product[f].checked)
		{	library.total += library.product[f].price;		}
	}
	return library.total;
}

function reset_products(library)
{
	for(f=0;f<library.products;f++)
	{
		library.product[f].checked = false;
		elname = 'si_' + library.product[f].stockid;
		el = document.getElementById(elname);
//		alert("Looking for element " + elname + ": " + el);
		el.checked = 0;
	}
	library.totalcost();
	change_totalcost_display(library);
}

function change_checkbox(formel)
{
	// this line gives us a variable called 'library' that points to the current library
	eval("library = " + formel.form.lsl_libname.value);
	stockid = formel.name.substring(3);
	for(f=0;f<library.products;f++)
	{
		if(library.product[f].stockid == stockid)
		{	library.product[f].checked = !library.product[f].checked;		}
	}
	library.totalcost();
	change_totalcost_display(library);
}

function change_shipping(formel)
{
	// this line gives us a variable called 'library' that points to the current library
	eval("library = " + formel.form.lsl_libname.value);
//	alert("You changed the shipping cost to " + formel.value);
	myshipidx = formel.value.indexOf("_");
//	alert("_ is located at " + myshipidx);
	myship = formel.value.substr((myshipidx+1));
//	alert("shipping value = " + myship);
	library.shipping = (1 * myship);
	library.totalcost();
	change_totalcost_display(library);
}

function make_checklist()
{
	document.write('<input type="hidden" name="lsl_libname" value="' + this.name + '">');
	total_list = "";
	for(f=0;f<this.products;f++)
	{
		text = this.product[f].name + " ($" + this.product[f].price + ")";
		cb = '<input type="checkbox" name ="si_' + this.product[f].stockid + '" ';
		cb = cb + 'id ="si_' + this.product[f].stockid + '" value="' + this.product[f].stockid;
		cb = cb + '" onClick="change_checkbox(this);">';
		document.writeln(cb + text + '&nbsp;&nbsp;&nbsp;&nbsp;</input><br>');
		total_list = total_list + this.product[f].stockid + ":";
	}
	total_list = total_list.substr(0,total_list.length-1);
	document.write('<input type="hidden" name="lsl_total" value="' + total_list + '">');
}

function make_droplist(name,price)
{
	document.write('<select id="' + name + '" name="' + name + '">');
	for(f=0;f<this.products;f++)
	{
		text = this.product[f].name;
		if(price)
		{ text = text + " ($" + this.product[f].price + ")"; }
		document.writeln('<option value="' + this.product[f].stockid + '">' + text + '&nbsp;&nbsp;&nbsp;&nbsp;</option>');
	}
	document.write('</select>');
}

function add_product(stockid,name,price)
{
	temp = new product(stockid,name,price);
	this.product[this.products] = temp;
	this.products++;
}

function product (stockid,name,price)
{
	this.stockid	= stockid;
	this.name		= name;
	this.price		= price;
	this.checked	= false;
	return this;
}

function setformdest(formname,cgi_name)
{
	theform = document.getElementById(formname);
	if(theform)
	{
		theform.action = myformcgi + cgi_name + '.cgi';
	}
	else
	{
		alert("ERROR:\nInvalid form name supplied.\nForm will not work!");
	}
}

