function SetupCalc() {
  for(var i = 0, ii = paperSizes.length; i < ii; i++) {
    k = document.options.paper_size.length;
    document.options.paper_size.length++;
    document.options.paper_size.options[k].value = i;
    document.options.paper_size.options[k].text = paperSizes[i];
  }
  document.options.paper_size.options[0].selected = true;
  
  for(var i = 0, ii = paperTypes.length; i < ii; i++) {
    k = document.options.paper_type.length;
    document.options.paper_type.length++;
    document.options.paper_type.options[k].value = i;
    document.options.paper_type.options[k].text = paperTypes[i];
  }
  document.options.paper_type.options[0].selected = true;
  
  for(var i = 0, ii = foldTypes.length; i < ii; i++) {
    k = document.options.fold_type.length;
    document.options.fold_type.length++;
    document.options.fold_type.options[k].value = foldTypes[i];
    document.options.fold_type.options[k].text = foldTypes[i];
  }
  document.options.fold_type.options[0].selected = true;
}

function NotEditable() {
  alert('This field is calculated automatically.\nPlease adjust the values on the left to change the pricing.');
}

function CheckFoldType() {
  if (document.options.fold_type.options[document.options.fold_type.selectedIndex].value == '') {
    document.options.option_fold.checked = false;
  } else {
    document.options.option_fold.checked = true;
  }
}

function SetCollating() {
  if( isNaN( document.options.pages_per_document.value ) )
		document.options.pages_per_document.value = 1;
  if ( (document.options.pages_per_document.value > 1) && (document.options.option_staple.checked == true) )
    document.options.option_collate.checked = true;
  else
    document.options.option_collate.checked = false;
}

function FormatTotal(value) {
	value = Math.round(value*100);
	var string;
	if (value < 10)
		string = "00" + value;
	else if (value < 100)
		string = "0" + value;
	else
	string = "" + value;
	string = string.substring (0, string.length - 2) +
		"." + string.substring (string.length - 2, string.length);
	return '$' + string;
}

function CalculatePrice() {
	var priceDocument, pricePaper, priceFinishing, priceTotal;
  var totalFinishedPieces, sides, ps, pt;
	
	// Made sure we have nmbers and they are positive values
	if( isNaN( document.options.pages_per_document.value ) )
		document.options.pages_per_document.value = 1;
  if( isNaN( document.options.copies_per_document.value ) )
		document.options.copies_per_document.value = 1;
	document.options.pages_per_document.value = Math.abs( document.options.pages_per_document.value );
  document.options.copies_per_document.value = Math.abs( document.options.copies_per_document.value );
	if( document.options.pages_per_document.value < 1 )
		document.options.pages_per_document.value = 1;
  if( document.options.copies_per_document.value < 1 )
		document.options.copies_per_document.value = 1;
	totalFinishedPieces = document.options.pages_per_document.value * document.options.copies_per_document.value;
  
	// Check for single or double sided
  sides = 1;
  for (var i = 0 ; i < document.options.document_sides.length; i++) {
    if (document.options.document_sides[i].checked) {
      sides = document.options.document_sides[i].value;
      break;
    }
  }

  // Check for size of the paper
  ps = document.options.paper_size.options[document.options.paper_size.selectedIndex].value;
  document.options.paper_size_text.value = document.options.paper_size.options[document.options.paper_size.selectedIndex].text;
  
  // Do the basic calculation
  if (sides == 1) {
    priceDocument = costSimgleSided[ps][0];
    if (totalFinishedPieces > costSimgleSided[ps][1]) {
      priceDocument += (totalFinishedPieces - costSimgleSided[ps][1]) * costAdditonal[ps];
    }
  } else {
    priceDocument = costDoubleSided[ps][0];
    if (totalFinishedPieces > costDoubleSided[ps][1]) {
      priceDocument += (totalFinishedPieces - costDoubleSided[ps][1]) * (costAdditonal[ps] * sides);
    }
  }
  
  // Set the paper and finishing cost to zero
  pricePaper = 0;
  priceFinishing = 0;
  
  // Check for the type of paper requested
  pt = document.options.paper_type.options[document.options.paper_type.selectedIndex].value;
  document.options.paper_type_text.value = document.options.paper_type.options[document.options.paper_type.selectedIndex].text;

  if (pt == "X") {
    pt = 0;
    document.options.paper_type.options[1].selected = true;
  }
  pricePaper += priceDocument * costTypes[pt];
  
  // Check for binding options
	if (document.options.option_cut.checked) {
    priceFinishing += costCut;
  }
  if (document.options.option_staple.checked) {
    priceFinishing += costStaple;
  }
  if (document.options.option_collate.checked) {
    priceFinishing += costCollate;
  }
  if (document.options.option_fold.checked) {
    priceFinishing += costFold;
  }
  
  
  // Now dispaly the finial costs
  priceTotal = priceDocument + pricePaper + priceFinishing;
  document.options.document_cost.value = FormatTotal( priceDocument );
  document.options.paper_cost.value = FormatTotal( pricePaper );
	document.options.finish_cost.value = FormatTotal( priceFinishing );
  document.options.total_cost.value = FormatTotal( priceTotal );
	
}