/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*		Shade Cloth Calculator - January 11, 2007					 *   
*		Last Updated - July 31, 2007 by Philip Renich				 *
*		JavaScript programming by Philip Renich - www.elfboy.com	 *
*		For International Greenhouse Company - www.igcusa.com		 *
*	-------------------------------------------------------------	 *
*		This program calculates the cost of a shade cloth based		 *
*		upon user chosen cloth type, dimensions, and other			 *
*		options. The result is displayed via DOM manipulation.		 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// Setup all the costs of the different types of cloth and materials
// Wholesale prices with markup added once the base prices are computed.
wholesale = new Object
wholesale.alum30 = .19
wholesale.alum40 = .19
wholesale.alum50 = .19
wholesale.alum60 = .21
wholesale.alum70 = .23
wholesale.black30 = .066
wholesale.black40 = .071
wholesale.black50 = .076
wholesale.black60 = .079
wholesale.black70 = .080
wholesale.black80 = .102
wholesale.black90 = .125
wholesale.green60 = .106
wholesale.white22 = .076
wholesale.white40 = .097
wholesale.white50 = .132
wholesale.violet = .131
wholesale.blue = .175
wholesale.tan = .175
wholesale.smokeblue75 = .175
wholesale.red80 = .175

wholesale.tape = .18
wholesale.grommets = .13
wholesale.seam = .18

// Standard widths of the cloth. Used to compute seams.
standard = new Object
standard.alum30 = new Array(6.5, 13 ,28)
standard.alum40 = new Array(7, 14, 28)
standard.alum50 = new Array(7, 14, 28)
standard.alum60 = new Array(7, 14, 28)
standard.alum70 = new Array(7, 14, 28)
standard.black30 = new Array(6, 12, 20, 26, 34)
standard.black40 = new Array(6, 8, 12, 20, 26, 34)
standard.black50 = new Array(6, 8, 12, 20, 26, 34)
standard.black60 = new Array(6, 8, 12, 20, 26, 34)
standard.black70 = new Array(6, 12, 20, 26, 34)
standard.black80 = new Array(6, 12, 20, 26, 34)
standard.black90 = new Array(6, 12, 26, 34)
standard.green60 = new Array(6, 12)
standard.white22 = new Array(6, 12)
standard.white40 = new Array(6, 8, 12, 20, 26, 34)
standard.white50 = new Array(6, 8, 12, 20, 26, 34)
standard.violet = new Array(6, 12)
standard.blue = new Array(6, 12)
standard.tan = new Array(6, 12)
standard.smokeblue75 = new Array(6, 12)
standard.red80 = new Array(6, 12)

markup = .4

// This function is called onclick from the form
function calculate(x)
{
	// Determine standard widths to use
	avail = standard[x.type.value]
	buildWidth = 0
	useWidths = new Array
	breakOut = false
	while(x.width.value > buildWidth)
	{
		for(w in avail)
		{
			if(avail[w] >= x.width.value)
			{
				useWidths[useWidths.length] = avail[w]
				buildWidth = avail[w]
				breakOut = true
				break
			}
		}
		if(breakOut)
		{
			break
		}
		for(w in avail)
		{
			if(buildWidth == 0)
			{
				buildWidth = avail[avail.length - 1]
				useWidths[useWidths.length] = avail[avail.length - 1]
			}
			if(buildWidth + avail[w] >= x.width.value)
			{
				buildWidth += avail[w]
				useWidths[useWidths.length] = avail[w]
				break
			}
			else if(avail[w] == avail.slice(-1))
			{
				buildWidth += avail[w]
				useWidths[useWidths.length] = avail[w]
			}
		}
	}

	// Determine materials
	squareFt = x.width.value * x.length.value
	linealFt = (2 * x.width.value) + (2 * x.length.value)
	numGrommets = (linealFt * .5) - 2
	numSeams = useWidths.length - 1

	// Figure costs for each material and get a pre-markup value
	materialCost = squareFt * wholesale[x.type.value]
	tapeCost = (x.tape.checked) ? linealFt * wholesale.tape : 0
	grommetCost = (x.grommets.checked) ? numGrommets * wholesale.grommets : 0
	seamCost = (numSeams * x.length.value) * wholesale.seam
	preCost = materialCost + tapeCost + grommetCost + seamCost

	// Add markup, determine weight for S/H
	costEach = preCost / (1 - markup)
	costAll = costEach * x.quantity.value
	weightEach = squareFt * .015
	weightAll = weightEach * x.quantity.value
	preShipping = weightAll * .35

	// Figure total shipping and then total cost
	shipping = ((preShipping < 19.5) ? 19.5 : preShipping) * x.quantity.value
	total = costAll + shipping
	
	// Display result
	document.getElementById("result").innerHTML = "Cost of shade cloth: $" + total.toFixed(2)
}

// Use this as a parent function to determine if the calculation should be run.
function displayResult(x)
{
	if(x.width.value !== '' && x.length.value !== '')
		calculate(x)
	else
		document.getElementById("result").innerHTML = '&nbsp;'
}

// The specification says that if grommets are selected, tape must also.
function checkTape(x)
{
	if(x.grommets.checked)
	{
		x.tape.checked = true
		x.tape.disabled = true
	}
	else
		x.tape.disabled = false
}

// ---------- BEGIN AJAX AREA FOR SAVING SHADE CLOTH TO A SESSION ---------- //

var xmlHttp

function savePiece(x, clear)
{ 
	xmlHttp=GetXmlHttpObject()
	if(xmlHttp==null)
	{
		alert("Browser does not support HTTP Request")
		return
	}
	// Change this URL, possibly add as a var for function so you can do multiple
	var url="shadeClothSave.php"
	// Setup the query string: type, width, length, tape, grommets, cost
	if(clear == 'clear')
	{
		var str = 'clearSession'
	}
	else if(clear == null)
	{
		var str = x.type.value + ',' + x.width.value + ',' + x.length.value + ',' + x.tape.checked + ',' + x.grommets.checked + ',' + total.toFixed(2)
	}
	url=url+"?q="+str
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function stateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		// Change stuff here (mostly)
		// At least return a message, even if you don't need to.
		document.getElementById("savedPieces").innerHTML=xmlHttp.responseText
		if(xmlHttp.responseText == '')
		{
			document.getElementById('savedPieces').style.cssText = "display: none; background-color: transparent; border: 0;"
		}
		else
		{
			document.getElementById("savedPieces").style.cssText = "display: block;"
		}
	}
}

function GetXmlHttpObject()
{
	try
	{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")
	}
	catch(e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch(e) {}
	}

	if (xmlHttp==null)
	{
		xmlHttp=new XMLHttpRequest()
	}

	return xmlHttp
}