// --------------------------------------------------------------------------
// Calculate tGLIDE pricing
// Arrick Robotics
// Roger Arrick
// 26 Feb 2009
// Edit 21
// --------------------------------------------------------------------------

// Update from elements based on settings
function updateform()
{
  // Config
  if (document.getElementById("xconfig").checked  == true ) {
    // X
    document.getElementById("tglideicon").src="tglidexicon.jpg";
    document.getElementById("ystuff").style.visibility = "hidden";
  } else {
    // XY
    document.getElementById("tglideicon").src="tglidexyicon.jpg";
    document.getElementById("ystuff").style.visibility = "visible";
  }

  // C4/MD2
  if (document.getElementById("MD2bcNo").checked  == true ) {
    document.getElementById("c4md2icon").style.visibility = "hidden";
  } else {
    document.getElementById("c4md2icon").style.visibility = "visible";
    //document.getElementById("c4md2icon").style.top = document.getElementById("motorcontrolplace").style.top;
  }
 
  // Units
  if (document.getElementById("unitsinch").checked  == true ) {
    // inches
    document.getElementById("xunit").innerHTML="Inches (1-168)";
  } else {
    // mm
    document.getElementById("xunit").innerHTML="mm (25-4268)";
  }
  document.getElementById("yunit").innerHTML=document.getElementById("xunit").innerHTML;

  // Clear results
  document.getElementById("msg").innerHTML="&nbsp;";
  document.getElementById("price").innerHTML="";
  document.getElementById("leadtime").innerHTML="";

}  // End of updateform


// Calculate results based on form settings
function calcit()
{
  // ------------------------------------------------------------------------------------------
  // Set these variables to calculate pricing.

  // X positioner pricing.
  var pricebasex = 775;			// Base price of an X positioner.
  var priceinchx = 6;			// Per inch price of an X positioner.
  var maxtravel = 168;			// Maximum travel for X or Y.

  // XY positioner pricing.
  var pricebasexy  = 2100;		// Base price of an XY positioner.
  var priceinchxyx = 9.5;		// Per inch price of the X axis on XY positioner.
  var priceinchxyy = 8.5;		// Per inch price of the Y axis on XY positioner.

  // MD2/C4 pricing.
  var pricemd2bc110 = 1100;		// MD2bc (includes C4) 110V.
  var pricemd2bc220 = 1200;		// MD2bc (includes C4) 220V.

  // End of - Set these variables to calculate pricing.
  // ------------------------------------------------------------------------------------------

  // Working variables.
  var pricetotal = 0;
  var leadtime = 0;
  var xtdi = 0;
  var ytdi = 0;

  // Clear results.
  document.getElementById("price").innerHTML="";
  document.getElementById("leadtime").innerHTML="";
  document.getElementById("msg").innerHTML="Travel rounded up to nearest inch (25.4mm)";

  // X
  if ( document.getElementById("xconfig").checked  == true ) { 

    // Grab travel distance
    xtdi = document.getElementById("xtravel").value;

    // Convert mm to inches, deal in inches
    if ( document.getElementById("unitsmm").checked == true ) {
      xtdi = xtdi / 25.4;
    }  

    // Check for valid travel distances.
    if ( xtdi < 1 || xtdi > maxtravel || isNaN(xtdi) ) {
      document.getElementById("msg").innerHTML="Invalid Travel";
      return;
    }

    // Round up to nearest inch
    xtdi = Math.round(xtdi);

    // Calc X
    pricetotal = pricebasex + (xtdi * priceinchx);

    // Calculate leadtime
    leadtime = 5;

  }  // End of X

  // XY
  if (document.getElementById("xyconfig").checked  == true ) { 

    // Grab travel distances
    xtdi = document.getElementById("xtravel").value;
    ytdi = document.getElementById("ytravel").value;

    // Convert mm to inches, deal in inches
    if (document.getElementById("unitsmm").checked == true ) {
      xtdi = xtdi / 25.4;
      ytdi = ytdi / 25.4;
    }  

    // Check for valid travel distances.
    if ( xtdi < 1 || xtdi > maxtravel || ytdi < 1 || ytdi > maxtravel || isNaN(xtdi) || isNaN(ytdi) ) {
      document.getElementById("msg").innerHTML="Invalid Travel";
      return;
    }

    // Round up to nearest inch
    xtdi = Math.round(xtdi);
    ytdi = Math.round(ytdi);

    // Calc XY
    pricetotal = pricebasexy + (xtdi * priceinchxyx) + (ytdi * priceinchxyy);

    // Calculate leadtime
    leadtime = 10;

  }  // End of XY

  // debug - display final travel in inches
  //document.getElementById("msg").innerHTML = xtdi + "  " + ytdi;

  // Make price into dollar format and round.
  // INT((D23+10)/10) * 10  Round up to nearest $10

  // Add MD2bc?
  if ( document.getElementById("MD2bc110").checked == true ) { pricetotal = pricetotal + pricemd2bc110; }
  if ( document.getElementById("MD2bc220").checked == true ) { pricetotal = pricetotal + pricemd2bc220; }

  // Display price
  document.getElementById("price").innerHTML = Currency(pricetotal) + " + Shipping";

  // Display lead time
  document.getElementById("leadtime").innerHTML = leadtime + " days";

}  // end of calcit

// Make amount formated as currency with commas, and $
function Currency(amount)
{
  return "$" + CommaFormatted(CurrencyFormatted(amount));
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if (isNaN(i)) { i = 0.00; }
	var minus = '';
	if (i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if (s.indexOf('.') < 0) { s += '.00'; }
	if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
} // end of function CurrencyFormatted()

function CommaFormatted(amount)
{
	var delimiter = ",";
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if ( isNaN(i) ) { return ''; }
	var minus = '';
	if (i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while (n.length > 3) {
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if (n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if (d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
} // end of function CommaFormatted()

