function OnLoad()
{
	var iM, iMT=0;
	if ((iM=window.location.search.substring(6,window.location.search.length)) && iM>=0 && iM<=a_a_strMat.length)
		iMT=a_a_strMat[iM][0];
	else
		iM=0;
	LoadMaterials(a_a_strMat, "lstMaterial", "imgTexture", iMT, iM);
	document.getElementsByName("radMatType")[iMT].checked=true;
	LoadMaterials(a_a_strMat, "lstTileMaterial", "imgTileTexture", iMT, iM);
	document.getElementsByName("radTilesMatType")[iMT].checked=true;
}
function SetTexture(a_aA, strI, iM)
{
	document.getElementById(strI).src="images/" + a_a_strMatType[a_aA[iM][0]][0] 
		+ "/" + a_aA[iM][2] + "large.jpg";
}
function LoadMaterials(a_aA, strS, strI, iMT, iM)
{
	var iO, oS = document.getElementById(strS);
	oS.length = 0;
	for (var i=0;i<a_aA.length;i++)
		if (a_aA[i][0]==iMT)
		{
			if (iM==null)
				iM=i;
			oS.options[iO=oS.options.length]=new Option(a_aA[i][1],i);
			if (iM==i)
				oS.options[iO].selected=true;
		}
	SetTexture(a_aA, strI, iM);
}
function NextNearest(iV, iN) 
{
  var iR = iV % iN;
  if (iR > 0)
    iV = iV - iR + iN;
  return iV;
}
function Calculate()
{
	var iC = 0;
	
	var oM = document.getElementById("lstMaterial");
	var iM = oM.selectedIndex;
	var strM = oM.options[iM].text;

	var oTM = document.getElementById("lstTileMaterial");
	var iTM = oTM.selectedIndex;
	var strTM =  oTM.options[iTM].text;
	
	
	var iL60 = document.getElementById("txt60Length").value;
	var iL90 = document.getElementById("txt90Length").value;


	var iL = 0;
	iL += parseInt(iL60 > 0 ? iL60:0);
	iL += parseInt(iL90 > 0 ? iL90:0);

	if (iL > 0)
	{
	
		// Calculate Dimension Cost
		var iC_DxW60 = a_a_strMat[iM][3] / 100 * 60;
		var iC_DxW90 = a_a_strMat[iM][3] / 100 * 90;
		
		iL60 = NextNearest(iL60 / 100, iML);
		iL90 = NextNearest(iL90 / 100, iML);
		iL = iL60 + iL90;
		iC += iC_DxW60 * iL60;
		iC += iC_DxW90 * iL90;
		// Calculated Costs
		iC += iSP_IL_M * iL;
		var iV = document.getElementById("txtUpstands").value;
		if (iV > 0 && iV < 9999999)
		{
			iV = NextNearest(iV / 100, iML);
			iC += iUPM * iV;
		}
		var iV = document.getElementById("txtSplash").value;
		if (iV > 0 && iV < 9999999)
		{
			iV = NextNearest(iV / 100, iML);
			iC += iSPM * iV;
		}
		var iV = document.getElementById("txtWindow").value;
		if (iV > 0 && iV < 9999999)
		{
			iV = NextNearest(iV / 100, iML);
			iC += iWPM * iV;
		}

		
		// Check Costs 
		if (document.getElementById("chkCurvedCut").checked)
			iC += iCPM;
		if (document.getElementById("chkHobCO").checked)
			iC += iHC;
		if (document.getElementById("chkSinkCO").checked)
			iC += iSC;
		if (document.getElementById("chkUSP").checked)
			iC += iUSP;
		if (document.getElementById("chkSingBel").checked)
			iC += iSBS;
		if (document.getElementById("chkDoubBel").checked)
			iC += iDBS;
		if (document.getElementById("chkDrainGrooves").checked)
			iC += iDG;
		if (document.getElementById("chkUMB").checked)
			iC += iUMB;
		if (document.getElementById("chkRDEWT").checked)
			iC += iRDEWT;
		if (document.getElementById("chkPW").checked)
			iC += iPW;

		// Radio Costs
		if (document.frmDetails.radTapHoles[1].checked)
			iC += iTH;
		else if (document.frmDetails.radTapHoles[2].checked)
			iC += iTH*2;
		if (document.frmDetails.radTF[0].checked)
			iC += iFD;
		else if (document.frmDetails.radTF[1].checked)
			iC += iSD;
		else if (document.frmDetails.radTF[2].checked)
		{
			iC += iFD;
			iC *= iPFD;
		}
		
		// Floor Tiles
		var iGTC = isNaN(parseInt(document.getElementById("txtTilesSqM").value))?0:parseInt(document.getElementById("txtTilesSqM").value);
		iGTC *= document.frmDetails.radT_TF[1].checked?iFT_S:iFT_F;
		iC += iGTC;

		iC = Math.round(iC * 100)/100;
		document.getElementById("txtQuote").value = iC;
		var iQV = Math.round(iC * iVAT * 100)/100;
		document.getElementById("txtVAT").value = iQV;
		document.getElementById("txtQuoteIV").value = Math.round((iC + iQV) * 100)/100;
		document.getElementById("txtWS_Mat").value = strM;
		document.getElementById("txtT_Mat").value = strTM;
		return true;
	}
}
function Validate()
{
	var strError = "";
	if(!(document.getElementById("txt60Length").value > 0) && !(document.getElementById("txt90Length").value > 0))
		strError += "--> Worksurface Dimensions - Length missing\n";
	if (strError == "")
	{
		Calculate();
		return true;
	}
	else
	{
		alert(strError);
		return false;
	}
}
