var qq=0;
var crap=new Array();
var j=0;
var crud="";
var car=0;
var begin=0;
var end=0;
var price=0;
var stock;
var howmany;
var booky;
strt();

function strt()
{
	i=0;
	var cookie=document.cookie;
	//prompt(cookie, cookie);
	var crud="";
	var end=0;
	while (crud!="true")
	{
		begin=cookie.indexOf("[|]",end);
		end=cookie.indexOf("[|]",begin+3)
		book=cookie.substring(begin+3,end)
		if (end==-1 || end=="")
			crud="true";
		begin=cookie.indexOf("[|]",end);
		end=cookie.indexOf("[|]",begin+3)
		price=cookie.substring(begin+3,end)
		begin=cookie.indexOf("[|]",end);
		end=cookie.indexOf("[|]",begin+3)
		stock=cookie.substring(begin+3,end)
		begin=cookie.indexOf("[|]",end);
		end=cookie.indexOf("[|]",begin+3)
		howmany=cookie.substring(begin+3,end)
		if (crud !="true")
		{
			crap[j]=new person(book,price,stock,howmany);
			j++;
		}
	}
}

function delete_cookie ( cookie_name )
{
	var cookie_date = new Date ( );  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = cookie_name += "=; path='/'; expires=" + cookie_date.toGMTString();
	//alert(document.cookie);
}

function get_cookie ( cookie_name )
{
	var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
	//alert(results);
	if ( results )
		return ( unescape ( results[1] ) );
	else
		return null;
}

function person(booktit,price,stock,quantity)
{
	this.quantity=quantity
	this.booktit=booktit
	this.stock=stock
	this.price=price
}
var i=0;
var book="[|]";

function add(theform)
{
	document.cookie="cook=yay; "+"expires= "+new Date();
	var e = document.forms[theform].elements;
	var q = e.length-2;
	while(i!=q)
	{
		if (!e[i+1]) //if somehow we got beyond the last element break.
		break;
		if (e[i].type == "hidden" || e[i].type=="select-one") //if there is a checkbox checked and it is true OR if its not a checkbox, i.e., a drop down list
		{
			var si=0
			if (e[i].type=="select-one") //If the current element is a list THEN...
			{
				if (e[i].options[e[i].options.selectedIndex].value!="YM") //not "SELECT YOUR OPTION"
				{
					if (e[i+1].type=="select-one")
					{
						do_ops(theform);
					}
					else
					{
						var jmj=e[i].options[e[i].options.selectedIndex].value
						var bg=0;
						var en=0;
						var prod1=""
						bg=jmj.indexOf("|");
						prod1=jmj.substring(0,bg) //Get the product description
						bg=jmj.indexOf("|",en)+1
						en=jmj.indexOf("|",bg+1)
						var firstprice=jmj.substring(bg,en) //Get the price
						bg=jmj.indexOf("|",en)+1
						en=jmj.length
						var stock1=jmj.substring(bg,en)
						if(e[i+1].value=="")
						e[i+1].value=0
						book+=prod1+"[|]"+firstprice+"[|]"+stock1+"[|]"
						book+=e[i+1].value+"[|]";
						var msg = (e[i+1].value > 1) ? e[i+1].value + " " +prodconfirm + "s have been added to your order."
									: e[i+1].value + " " +prodconfirm + " has been added to your order.";
						//alert(msg);
					}
				}
				else si=777 //Nope, it was just the default "Click here to select options" setting
			}
			if (e[i].type!="select-one" && e[i].value!="")
			{
				if (e[i+1].type=="select-one")
				{
					do_ops(theform)
				}
				else book+=e[i].value+"[|]"+e[i+1].value+"[|]"
			}
		}
		if (e[i+1].type=="select-one")
		{
			i++;
			while(e[i].type=="select-one")
			{
				i++;
			}
			i--
		}
		i+=1;
	}
	document.cookie="cook="+book+";"
	booky=""
	if (comp_page==1)
	{
		price=0
		var timbo=0
		while (crud!="true")
		{
			cookie=document.cookie
			begin=cookie.indexOf("[|]",end);
			end=cookie.indexOf("[|]",begin+3)
			if (end==-1 || end=="")
			{
				crud="true"
				break;
			}
			booky+=cookie.substring(begin+3,end)//+"LINE_BR"
			begin=cookie.indexOf("[|]",end);
			end=cookie.indexOf("[|]",begin+3)
			var price4=cookie.substring(begin+3,end)
			parseInt(price4)
			price4--
			price4+=1
			price--;
			price++;
			price+=price4;
			begin=cookie.indexOf("[|]",end);
			end=cookie.indexOf("[|]",begin+3)
			stock=cookie.substring(begin+3,end)
			begin=cookie.indexOf("[|]",end);
			end=cookie.indexOf("[|]",begin+3)
			howmany=cookie.substring(begin+3,end)
			if (timbo==0)
			{
				timbo=1
				var f_stock=stock;
				var f_howmany=howmany
			}
		}
		document.cookie="cook=[|]"+booky+"[|]"+price+"[|]"+f_stock+"[|]"+f_howmany+"[|];"
		begin=0
		end=0
		crud="false";
	}
	while (crud!="true")
	{
		cookie=document.cookie
		begin=cookie.indexOf("[|]",end);
		end=cookie.indexOf("[|]",begin+3)
		booky=cookie.substring(begin+3,end)
		if (end==-1 || end=="")
		crud="true"
		begin=cookie.indexOf("[|]",end);
		end=cookie.indexOf("[|]",begin+3)
		price=cookie.substring(begin+3,end)
		begin=cookie.indexOf("[|]",end);
		end=cookie.indexOf("[|]",begin+3)
		stock=cookie.substring(begin+3,end)
		begin=cookie.indexOf("[|]",end);
		end=cookie.indexOf("[|]",begin+3)
		howmany=cookie.substring(begin+3,end)
		if (crud!="true")
		{
			crap[j]=new person(booky,price,stock,howmany)
			j++
		}
	}
	for (i=0; i<j; i++)
	{
		for (z=i+1; z<j; z++)
		{
			if (crap[i].stock==crap[z].stock)
			{
				var temp=parseInt(crap[z].quantity)
				var temp2=parseInt(crap[i].quantity)
				if (crap[i].booktit.indexOf("LINE_BR")=="" || crap[i].booktit.indexOf("LINE_BR")==-1)
				temp2+=temp;
				crap[z].quantity=temp
				crap[i].stock="zip";
			}
		}
	}
	document.cookie="cook=";
	book="[|]"
	for (i=0; i<j; i++)
	{
		if (crap[i].stock!="zip")
		book+=crap[i].booktit+"[|]"+crap[i].price+"[|]"+crap[i].stock+"[|]"+crap[i].quantity+"[|]"
	}
	document.cookie="cook="+book+" path='/';"
	location.href = "cart.htm";
}

function check(val,theform)
{
	var d=document.form[theform].elements;
	var nogood="false"
	for (var zit = 0; zit < val.length; zit++)
	{
		var ha = val.substring(zit, zit + 1)
		if (ha < "0" || ha > "9")
		{
			nogood="true"
			for (zit=0; zit<d.length; zit++)
			{
				if (d[zit].value==val) d[zit].focus();
			}
		}
	}
	if (nogood=="true")
	alert("Please enter a number for the quantity you require.");
}

function do_ops(theform)
{
	var e = document.forms[theform].elements;
	var jmj;
	if (e[i].type=="select-one")
	jmj=e[i].options[e[i].options.selectedIndex].value
	else
	jmj=e[i].value;
	var sag=e[i+1].options[e[i+1].options.selectedIndex].value
	var bg=0;
	var en=0;
	var prod1=""
	var prodconfirm=""
	bg=jmj.indexOf("|");
	prod1=jmj.substring(0,bg) //Get the product description
	prodconfirm=prod1
	bg=jmj.indexOf("|",en)+1
	en=jmj.indexOf("|",bg+1)
	var firstprice=jmj.substring(bg,en) //Get the price
	bg=jmj.indexOf("|",en)+1
	en=jmj.length
	var stock1=jmj.substring(bg,en)
	bg=sag.indexOf("|")
	var prod2=sag.substring(0,bg) //get the description of the option
	prod1+="LINE_BR";
	prod1+=prod2 //Product description PLUS OPTION description
	bg=sag.indexOf("|")+1
	en=sag.indexOf("|",bg)
	var secondprice=sag.substring(bg,en) //price
	bg=sag.indexOf("|",en)+1
	var stock2=sag.substring(bg,sag.length) //the stock #
	stock1+="-"+stock2
	parseInt(firstprice);
	parseInt(secondprice);
	firstprice++;
	firstprice--;
	secondprice++;
	secondprice--;
	firstprice+=secondprice;
	firstprice=Math.round(firstprice*100)/100
	i++;
	if (e[i+1].type=="select-one")
	{
		i++;
		while(e[i].type=="select-one")
		{
			sag=e[i].options[e[i].options.selectedIndex].value
			bg=sag.indexOf("|")
			prod2=sag.substring(0,bg) //get the description of the option
			prod1+="LINE_BR";
			prod1+=" "+prod2 //Product description PLUS OPTION description
			bg=sag.indexOf("|",bg)+1
			en=sag.indexOf("|",bg)
			secondprice=sag.substring(bg,en) //price
			parseInt(secondprice);
			secondprice++;
			secondprice--;
			firstprice+=secondprice;
			firstprice=Math.round(firstprice*100)/100
			bg=sag.indexOf("|",en)+1
			stock2=sag.substring(bg,sag.length) //the stock #
			stock1+="-"+stock2;
			i++;
		}
		i--
	}
	prod1+="LINE_BR";
	book+=prod1+"[|]"+firstprice+"[|]"+stock1
	book+="[|]"+e[i+1].value+"[|]"
	var bg2a=0;
	var bg3a=0;
	var prod2a=""
	bg2a=prodconfirm.indexOf("<");
	bg3a=prodconfirm.indexOf(">");
	prodconfirm=prodconfirm.substring(bg3a+1) //Get the product description
	var bg2a=0;
	var bg3a=0;
	var prod2a=""
	bg2a=prodconfirm.indexOf("<");
	bg3a=prodconfirm.indexOf(">");
	prodconfirm=prodconfirm.substring(0,bg2a) //Get the product description
	var msg = (e[i+1].value > 1) ? e[i+1].value + " " +prodconfirm + "s have been added to your order." 
				: e[i+1].value + " " +prodconfirm+ " has been added to your order.";
	//alert(msg);
}

function special()
{
	var tempthing=document.cookie
	var beginthing=tempthing.indexOf("cook=")+5
	tempthing=tempthing.substring(beginthing,tempthing.length)
	var moretemp=""
	for (var dum1=0; dum1<tempthing.length; dum1++)
	{
		if (tempthing.substring(dum1,dum1+1)==" ")
		{
			moretemp+="%20"
		}
		else
		moretemp+=tempthing.substring(dum1,dum1+1)
	}
	document.cookie="cook="+moretemp
	var special=document.cookie
	var bspecial=special.indexOf("cook=")+5
	var bend=special.indexOf(";",bspecial)
	if (bend==-1 || bend=="")
	bend=special.length
	special=special.substring(bspecial,bend)
	location.href="cart.htm?"+special
}
function fixPNG()
{
	for(var i=0; i<document.images.length; i++)
	{
		if(document.images[i].src)
		{
			var imgName = document.images[i].src.toUpperCase()
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			{
				document.images[i].runtimeStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + document.images[i].src + "', sizingMethod='image');";
				document.images[i].src='images/blank.gif';
			}
		}
	}
}
agt = navigator.userAgent.toLowerCase();
if((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1))
{
	window.attachEvent("onload", fixPNG);
}
function setActiveLink()
{
	var thisPage = window.location.toString();
	for (var i = 0; i < document.links.length; i++)
	{
		if(document.links[i].href == thisPage)
		document.links[i].className = "active";
	}
}
window.onload = setActiveLink;
function goSecure(url)
{
	blLive = (url.indexOf('thistledown') != -1);
	if (blLive)
	{
		if((url.indexOf('cart')!=-1) && (url.indexOf('https')==-1)){
			location.href = 'https://www.thistledown.on.ca/cart.htm';
			return;
		} else if (url.indexOf('www.thistledown.on.ca') == -1) {
			url = url.substring(url.lastIndexOf('/') + 1);
			location.href = 'http://www.thistledown.on.ca/' + url;
		}
	}
}
