Page 1 of 1

Creating a Total Amount Value.

Posted: Tue Mar 11, 2003 11:07 pm
by evilcoder
Hey all, i have this code which a friend gave to me.. Now i've got it working but i want to add another value, which will give me the setupFee + theCost to create a total value.

http://www.dnxnet.com/?dnx=designquote

there you can have alook at what i'm talking about, and here is the javascript file:

Code: Select all

function init()
{	

	thePricing = new hostOption();
	initialSetup = 150;
	setupFee = initialSetup;
	bTaxFree = false; 
	
	if (document.pricing.setup.value == 0)
	{
		document.pricing.setup.value = round(setupFee);
		document.pricing.setupDisplay.value = format ( (setupFee * 1), 2 );
		theCost = eval(document.pricing.spaceїdocument.pricing.space.selectedIndex].value);
		document.pricing.cost.value = round(theCost );
		document.pricing.costDisplay.value = format ( (theCost * 1 ), 2 );
	}

	document.pricing.setup.defaultValue = ( initialSetup );
	document.pricing.setupDisplay.defaultValue = format ( ( initialSetup * 1 ), 2 );
	document.pricing.cost.defaultValue = eval( ( document.pricing.spaceї0].value ) );
	document.pricing.costDisplay.defaultValue = eval( format ( ( document.pricing.spaceї0].value ) * 1, 2 ) );
	document.pricing.GST.defaultValue = calculateGST();
	calculateGST();
	
	for(j=0; j<document.pricing.length; j++)
	&#123;
		if(document.pricing.elements&#1111;j].name.indexOf("Units")== 0)
		&#123;
			if(document.pricing.elements&#1111;j-1].checked == false)
			&#123;
				document.pricing.elements&#1111;j].disabled = true;
			&#125;
		&#125;
	&#125;

	theChoice = eval(document.pricing.space&#1111;document.pricing.space.selectedIndex].value);
	document.pricing.GST.value = round(theGST);
&#125;

function hostOption()
&#123; &#125;

// Make sure select boxes are enabled if the check box is checked
function enableSelect ( checkbox, selectbox ) &#123;
	alert  (checkbox.checked);
	if ( checkbox.checked=="1" && selectbox.disabled == true )
		&#123; selectbox.disabled = false &#125;
&#125;

function addCheck(checkBox, isSetup) &#123; //v2.0
	document.pricing.cost.value= round(eval(document.pricing.costDisplay.value / 1));
	document.pricing.setup.value= round(eval(document.pricing.setupDisplay.value / 1));

	if(isSetup)
	&#123;
		if (checkBox.checked=="1")
  		&#123;
    		document.pricing.setup.value = round(eval(document.pricing.setup.value) + eval(checkBox.value));
		&#125;
		else
		&#123;
			document.pricing.setup.value = round(eval(document.pricing.setup.value) - eval(checkBox.value));
		&#125;
		setupFee = document.pricing.setup.value;
		document.pricing.setupDisplay.value = format ( setupFee * 1, 2 );
	&#125;
	else
	&#123;
		if (checkBox.checked=="1")
  		&#123;
    		document.pricing.cost.value = round(eval(document.pricing.cost.value) + eval(checkBox.value));
		&#125;
		else
		&#123;
			document.pricing.cost.value = round(eval(document.pricing.cost.value) - eval(checkBox.value));
		&#125;
		theCost = document.pricing.cost.value;
		document.pricing.costDisplay.value = format ( theCost * 1, 2 );
	&#125;
	calculateGST();
&#125;
function format ( expr, decPlaces ) &#123;
	var str= "" + Math.round ( eval ( expr ) * Math.pow ( 10, decPlaces ) )
	while ( str.length <= decPlaces ) &#123;
		str = "0" + str &#125;
	var decpoint = str.length - decPlaces
	var value=str.substring ( 0, decpoint ) + "." + str.substring ( decpoint, str.length )
	return value.toString();
&#125;
function calcdomain(fieldname)&#123;
//  var pricearray = new Array ('0','53.90','105.60','151.80','193.60','242.00','313.50','365.20','418.00','469.70','522.50');
  var pricearray = new Array ('0','0','69.00','103.50','138.00','172.50','207.00','241.50','276.00','310.50','345.00');
  var newprice;
  var years;
  eval("years=document.domaincalc."+fieldname+"Years.options&#1111;document.domaincalc."+fieldname+"Years.selectedIndex].text" );
  newprice=pricearray&#1111;years];
  eval("document.domaincalc."+fieldname+"Price.value = '"+newprice+"'"); 
&#125;

function addChoice(choiceValue)&#123;
  document.pricing.cost.value= round(eval(document.pricing.costDisplay.value / 1));
  document.pricing.setup.value= round(eval(document.pricing.setupDisplay.value / 1));

  document.pricing.cost.value = round((eval(document.pricing.cost.value) + eval(choiceValue&#1111;choiceValue.selectedIndex].value) - eval(theChoice)));
  theChoice = eval(choiceValue&#1111;choiceValue.selectedIndex].value);
  theCost = document.pricing.cost.value;
  setupFee = document.pricing.setup.value;
  document.pricing.costDisplay.value = format ( ( theCost * 1 ), 2 )
  document.pricing.setupDisplay.value = format ( ( setupFee * 1 ), 2 )
  calculateGST();
&#125;

function changeChoice(checkBox, isSetup, theID, units) &#123;

	document.pricing.cost.value= round(eval(document.pricing.costDisplay.value / 1));
	document.pricing.setup.value= round(eval(document.pricing.setupDisplay.value / 1));

	if (checkBox.checked=="1")
	&#123;
		if(isSetup)
		&#123;		
    		document.pricing.setup.value = round(eval(document.pricing.setup.value) - thePricing&#1111;theID] + (eval(checkBox.value) * units));
			setupFee = document.pricing.setup.value;
			document.pricing.setupDisplay.value = format ( setupFee * 1, 2 )
		&#125;
		else
		&#123;
    		document.pricing.cost.value = round(eval(document.pricing.cost.value) - thePricing&#1111;theID] + (eval(checkBox.value) * units));
			theCost = document.pricing.cost.value;
			document.pricing.costDisplay.value = format ( theCost * 1, 2 )
		&#125;	
	&#125;
	calculateGST();
	thePricing&#1111;theID] = checkBox.value * units;
&#125;


function mrChoice(checkBox, isSetup, theID, units, unable) &#123; //v2.0
	document.pricing.cost.value= round(eval(document.pricing.costDisplay.value / 1));
	document.pricing.setup.value= round(eval(document.pricing.setupDisplay.value / 1));

	if(thePricing&#1111;theID] == null)
	&#123;
		thePricing&#1111;theID] = "0";
	&#125;

	if(isSetup)
	&#123;
		if (checkBox.checked=="1")
  		&#123;
			unable.disabled = false;
    		document.pricing.setup.value = round(eval(document.pricing.setup.value) - thePricing&#1111;theID] + (eval(checkBox.value) * units));
			thePricing&#1111;theID] = checkBox.value * units;
		&#125;
		else
		&#123;
			unable.disabled = true;
			document.pricing.setup.value = round(eval(document.pricing.setup.value) - thePricing&#1111;theID]);
			thePricing&#1111;theID] = 0;
		&#125;
		setupFee = document.pricing.setup.value;
		document.pricing.setupDisplay.value = format ( setupFee * 1, 2 )
	&#125;
	else
	&#123;
		if (checkBox.checked=="1")
  		&#123;
			unable.disabled = false;
    		document.pricing.cost.value = round(eval(document.pricing.cost.value) - thePricing&#1111;theID] + (eval(checkBox.value) * units));
			thePricing&#1111;theID] = checkBox.value * units;
		&#125;
		else
		&#123;
			unable.disabled = true;
			document.pricing.cost.value = round(eval(document.pricing.cost.value) - thePricing&#1111;theID]);
			thePricing&#1111;theID] = 0;
		&#125;
		theCost = document.pricing.cost.value;
		document.pricing.costDisplay.value = format ( theCost * 1, 2 )
	&#125;
	calculateGST();	
&#125;

function doFocus(checkBox, theID, units, zap)
&#123;
	thePricing&#1111;theID] = checkBox.value * units;
&#125;

function calculateGST()
&#123;
	theGST = round((eval(document.pricing.cost.value) + eval(document.pricing.setup.value)) / 1);
	if (bTaxFree) 
		document.pricing.GST.value = "0";
	else 
		document.pricing.GST.value = round(theGST);
&#125;

function round(price)
&#123;
	newPrice = Math.round(price*Math.pow(10,2))/Math.pow(10,2);
	string = "" + newPrice;
	number = string.length - string.indexOf('.');

	if (number == 2)
      return newPrice + '0';
	else
	  return newPrice;
&#125;

function taxFree(strGst) &#123;
	if (strGst == "Yes") &#123;
		bTaxFree = false;
		document.pricing.GST.value = round(theGST);
	&#125;
	else &#123;
		bTaxFree = true;
		document.pricing.GST.value = "0";
	&#125;
&#125;

function newWin(url) &#123;
	window.open(url);
	window.close();
&#125;
I can understand if none of you even attempt this, but i'm a strong PHP coder, not javascript :) . Thanks peoples.

Posted: Wed Mar 12, 2003 3:30 am
by volka
Lucky you, there already is a function that is always called after a change has been made (as far as I can see) ;)

Code: Select all

function calculateGST()
&#123;
	theGST = round((eval(document.pricing.cost.value) + eval(document.pricing.setup.value)) / 1);
	if (bTaxFree) 
		document.pricing.GST.value = "0";
	else 
		document.pricing.GST.value = round(theGST);
		
	document.pricing.totalDisplay.value = eval(document.pricing.cost.value+'+'+document.pricing.setup.value);
&#125;