// JavaScript Document
// Stair Diameter Form Validation 
function isDiameterSelected(input)
{      
  var selected = -1;
  var counter = input.length;
  if(counter == undefined)
  {
     if(input.checked ==false)
        return false;
     else
        return true;
  }
  for(var i=0; i<counter; i++)
     if(input[i].checked )
     {
        selected = i;
        break;
     }
  if(selected > -1)
     return true;
  else
     return false;
}
// End of Stair Diameter Form Validation

// Handrail Options Form Validation
function isHandrailSelected(input)
{      
  var selected = -1;
  var counter = input.length;
  if(counter == undefined)
  {
     if(input.checked ==false)
        return false;
     else
        return true;
  }
  for(var i=0; i<counter; i++)
     if(input[i].checked )
     {
        selected = i;
        break;
     }
  if(selected > -1)
     return true;
  else
     return false;
}
// End of Handrail Options Form Validation


// Step Options Form Validation
function isStepSelected(input)
{      
  var selected = -1;
  var counter = input.length;
  if(counter == undefined)
  {
     if(input.checked ==false)
        return false;
     else
        return true;
  }
  for(var i=0; i<counter; i++)
     if(input[i].checked )
     {
        selected = i;
        break;
     }
  if(selected > -1)
     return true;
  else
     return false;
}
// End of Step Options Form Validation

// Powder Coating Form Validation 
function isPaintingSelected(input)
{      
  var selected = -1;
  var counter = input.length;
  if(counter == undefined)
  {
     if(input.checked ==false)
        return false;
     else
        return true;
  }
  for(var i=0; i<counter; i++)
     if(input[i].checked )
     {
        selected = i;
        break;
     }
  if(selected > -1)
     return true;
  else
     return false;
}
// End of Power Coating Form Validation

// Delivery Form Validation
function isDelivery(input)
{      
  var selected = -1;
  var counter = input.length;
  if(counter == undefined)
  {
     if(input.checked ==false)
        return false;
     else
        return true;
  }
  for(var i=0; i<counter; i++)
     if(input[i].checked )
     {
        selected = i;
        break;
     }
  if(selected > -1)
     return true;
  else
     return false;
}

function activateDeliveryField(val)
{
 document.deliveryOption.distance.disabled = val
}

// End of Delivery Form Validation

function numberRisers()
{
 elevation = document.floorToFloor.elev.value;
  risers = (elevation / 9.5);     // calculates number of risers
   risers = Math.ceil (risers);    // Rounds to the next whole number (actual number of risers)
    return risers;  
}

function revolution()
{
 var diameter = document.selectDiameter.diameter;
 var revol = 0;
  if(diameter[0].checked)
   revol = Math.round((numberRisers()-2)*29.48+26.6);     // Calculates revolution of the 5 ft stair ( @ 7-11/16" Step Run)
    else
     revol = Math.round((numberRisers()-2)*29.70+26.33);    // Calculates revolution of the 6 ft stair ( @ 7-11/16" Step Run)
      document.getElementById("revolution").value = revol;
}

function total()
{
 var diameter = document.selectDiameter.diameter;
  if(isDiameterSelected(diameter) == false)
   return alert('Please Select Stair Diameter');
   
 var handrail = document.selectHandrail.handrail;
  if(isHandrailSelected(handrail) == false)
   return alert('Please Select Stair Handrail');

 var steps = document.selectStep.steps;
  if(isStepSelected(steps) == false)
   return alert('Please Select Style of Treads');

 var costPerRiser = 0;
  if(diameter[0].checked)
   basicRiserCost = 250.00; // Basic Price per Rise of 5 ft Stair, Smooth Steps, Vertical Balusters 
    else
     basicRiserCost = 275.00; // Basic Price per Rise of 6 ft Stair, Smooth Steps, Vertical Balusters

 var handrailOption = 0;
  if(handrail[0].checked)
   handrailOption = 0;
    if(handrail[1].checked)
     handrailOption = 280.00/8;        //Additional charge for the Handrail Cap ($280.00-cost of rolled caprail for the 8 risers stair)
      if(handrail[2].checked)
       handrailOption = 200.00/8*8;       //Additional charge for Multi-Helix ($200.00-cost of each extra helix (*8 pcs) for the 8 risers stair)


 var stepOption = 0;
  if(steps[0].checked)
   stepOption = 0;
    if(steps[1].checked)
     stepOption = 5.00;        //Additional charge per Step with Diamond Plate
      if(steps[2].checked)
       stepOption = 70.00;       //Additional charge per Step with Bar Grating

 var powderCoating = document.selectPowderCoating.powderCoating;
  if(isPaintingSelected(powderCoating) == false)
   return alert('Please Select Powder Coating Options');
   
 var painting = 0;
  if(powderCoating[0].checked)
   painting = 0.00;
    else
    {
    if(diameter[0].checked)
     painting = 95.00; // Powder Coating per Rise of 5 ft Stair
      if(diameter[1].checked)
        painting = 110.00; // Powder Coating per Rise of 6 ft Stair
    }

 var delivery = document.deliveryOption.delivery;
  if(isDelivery(delivery) == false)
   return alert('Please Select Delivery Options');
 
 var distance = document.deliveryOption.distance.value;
   if(delivery[0].checked)
    miles = 0;
	 if(delivery[1].checked)
      {      
       if(distance.length == 0)
        return alert('Please Enter Delivery Distance');
         else
          {
      	   miles = document.deliveryOption.distance.value;
		  }
      }      
 price = numberRisers() * (basicRiserCost + handrailOption + painting) + stepOption * (numberRisers() - 1) + miles * 2.00;   // Main formula for total price. Enter delivery charge per mile (2.00)

 // Round to 2 decimal places--curency format-- 
 price = price * 1000;
 price = Math.round(price/10) + "";
 while (price.length < 3) {price = "0" + price};
 len = price.length;
 price = price.substring(0,len-2) + "." + price.substring(len-2,len);
 // end of curency format
 document.getElementById("result").value = price;
}
