function getCookieVal (offset) 
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function removeCookie(name)
{
	document.cookie = name + "=0; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function getCookie (name) 
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) 
  {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) return(getCookieVal (j));
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return 0;
}

function FixCookieDate (date) // fix date on Mac
{
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

function setCookie (name,value,expire) 
{
	  outstr = name + "=" + escape (value) + "; path=/;";
		document.cookie = outstr;
}

function thankyouWindow()
{
		var theThankyouWindow = open("", "mWin", "width=237,height=165");
		theThankyouWindow.document.open();
		theThankyouWindow.document.writeln("<html><head><title>Item added to quote</title><script>function exit(){this.close();}</script></head>");
		theThankyouWindow.document.writeln("<body text='#00000' bgcolor='#F1EDE4'><span style='font-family: Arial, Helvetica, sans-serif; font-size: 10pt;'><b>Your selected items have been added to your quote.<br><br><a href=\"javascript:window.opener.document.location='view-quote.php';this.close();\">View Quote</a><br><br><a href='javascript:window.opener.history.go(-1);this.close();'>Continue Selecting</a><br></span></body></html>");
		theThankyouWindow.document.close();
}


function additemtoquote()
{
	var numberofproducts = parseInt(getCookie("MERTONnumberofitems"));
	if(isNaN(numberofproducts)) numberofproducts = 0;
	thequan = document.addtoquote.quantity.value;
	thepricingunit = document.addtoquote.unit.value;

	if(parseInt(thequan)>0)
	{
		thetotalprice = 0;
		priceperunit = 0;

		for(i=matrixcount;i>=0;i--)
		{
			if(bands[i]==0)//single band
			{
				priceperunit = prices[0];
				fullpriceperunit = fullprices[0];
				thefactor = factors[0];
				break;
			}
			else if(thequan>bands[i]) 
			{
				priceperunit = prices[i];
				fullpriceperunit = fullprices[i];
				thefactor = factors[i];
				break;
			}
		}
		if(isNaN(priceperunit)){priceperunit = prices[0];}
		if(isNaN(thefactor)){priceperunit = factors[0];}
		thetotalprice = thequan * (priceperunit/thefactor);

		var orderstring = "" + productcode + "|" + productname +"|" + thetotalprice + "|" + thepricingunit+ "|" +priceperunit +"|" + fullpriceperunit + "|" + thequan + "|";

		setCookie("MERTONbasketitem" + numberofproducts,orderstring,"false");
		numberofproducts++;
		setCookie("MERTONnumberofitems",numberofproducts,"false");
		thankyouWindow();
	}


}

	
function docalcwin(lstart)
{
	open('timbercalc.php?lstart='+lstart, 'tcWin', 'width=330,height=300,scrollbars=yes');
}

//long lengths

function docalcwin2(lstart,lend)
{
	open('timbercalc2.php?start='+lstart+'&end='+lend, 'ltcWin', 'width=330,height=300,scrollbars=yes');
}



function format2(number)
{
	if(number=='CALL')return 'CALL';
	if(number<1 && number>0) string = " 0" + number;
	else string = " " + number;
	if (string.indexOf('.') == -1)
	{
		return string + '.00';
	}
	point = string.length - string.indexOf('.');
	if (point > 3)
	{
		return string.substring(0, string.length - point + 3);
	}
	else if (point == 2)
	{
		return string + '0';
	}
	return string;
}

function additemtoquote2()
{
	var numberofproducts = parseInt(getCookie("MERTONnumberofitems"));
	if(isNaN(numberofproducts)) numberofproducts = 0;
	thequan = document.addtoquote.quantity2.value;
	theunit = document.addtoquote.unit.value;
	thesurcharge = document.addtoquote.surcharge.value;

	if(parseInt(thequan)>0)
	{
		thetotalprice = 0;
		priceperunit = 0;

		for(i=matrixcount;i>=0;i--)
		{
			if(bands[i]==0)//single band
			{
				priceperunit = prices[0];
				thefactor = factors[0];
				break;
			}
			else if(thequan>bands[i]) 
			{
				priceperunit = prices[i];
				thefactor = factors[i];
				break;
			}
		}

		if(isNaN(priceperunit)){priceperunit = prices[0];}
		if(isNaN(thefactor)){priceperunit = factors[0];}
//		thetotalprice = thequan * (priceperunit/thefactor);
		if(thefactor==1)
		{
			thepricingunit = ""+theunit;
		}
		else
		{
			//thepricingunit = ""+thefactor+ ""+theunit;
			thepricingunit = ""+thefactor;
		}
	
		if(isNaN(priceperunit)){priceperunit = prices[0];}

//		priceperunit = (1+(thesurcharge/100)) * (priceperunit/thefactor);

		priceperunit = (1+(thesurcharge/100)) * (priceperunit);///thefactor);


		thetotalprice = thequan * (priceperunit/thefactor);
		var orderstring = "" + productcode + "|" + productname +" (long lengths)|" + thetotalprice + "|" + theunit + "|" + thepricingunit+ "|" +priceperunit +"|" + thequan + "|";
		setCookie("MERTONbasketitem" + numberofproducts,orderstring,"false");
		numberofproducts++;
		setCookie("MERTONnumberofitems",numberofproducts,"false");
		thankyouWindow();
	}


}

function dodummy()
{
	additemtoquote();
	return false;
}