
// Distance
function cmtometer(d)
{
  meter=d/100;
  return meter;
}
function cmtokm(d)
{
  km=d/100000;
  return km;
}
function cmtocm(d)
{
  return d;
}
function cmtomm(d)
{
  mm=d*10;
  return mm;
}
function mmtocm(d)
{
  cm=d/10;
  return cm;
}
function cmtofeet(d)
{
  feet=d/30.48;
  return feet;
}
function cmtoinch(d)
{
  inch=d/2.54;
  return inch;
}
function cmtoyards(d)
{
  yards=d/91.44;
  return yards;
}
function cmtomiles(d)
{
  miles=d/160934.4;
  return miles;
}

function cmtoTarget(cm)
{
  if ( document.DistanceForm.Target.selectedIndex == 0 )
  {
    mm=cmtomm(cm);
    alert(mm + " mm");
  }
  else if ( document.DistanceForm.Target.selectedIndex == 1 )
  {
    cm=cmtocm(cm);
    alert(cm + " cm");
  }
  else if ( document.DistanceForm.Target.selectedIndex == 2 )
  {
    meter=cmtometer(cm);
    alert(meter + " meter(s)");
  }

  else if ( document.DistanceForm.Target.selectedIndex == 3 )
  {
    km=cmtokm(cm);
    alert(km + " km");
  }
  else if ( document.DistanceForm.Target.selectedIndex == 4 )
  {
    inch=cmtoinch(cm);
    alert(inch + " in");
  }
  else if ( document.DistanceForm.Target.selectedIndex == 5 )
  {
    feet=cmtofeet(cm);
    alert(feet + " feet");
  }
    else if ( document.DistanceForm.Target.selectedIndex == 6 )
  {
    yards=cmtoyards(cm);
    alert(yards + " yards");
  }
    else if ( document.DistanceForm.Target.selectedIndex == 7 )
  {
    miles=cmtomiles(cm);
    alert(miles + " miles");
  }
 }

function pressConvert()
{
  value = document.DistanceForm.Value.value;
  d = parseFloat(value);
  if ( isNaN(d))
  {
    alert("Please enter a number");
  }
  else
  {
    if ( document.DistanceForm.Source.selectedIndex == 0 )
    {
      cm=mmtocm(d);
      cmtoTarget(cm);
    }
    else if ( document.DistanceForm.Source.selectedIndex == 1 )
      cmtoTarget(d);
    else if ( document.DistanceForm.Source.selectedIndex == 2 )
    {
      cm=d*100;
      cmtoTarget(cm);
    }
    else if ( document.DistanceForm.Source.selectedIndex == 3 )
    {
      cm=d*100000;
      cmtoTarget(cm);
    }
    else if (document.DistanceForm.Source.selectedIndex== 4 )
    {
      cm=d*2.54;
      cmtoTarget(cm);
    }
    else if ( document.DistanceForm.Source.selectedIndex== 5 )
    {
      cm=d*30.48;
      cmtoTarget(cm);
    }
    else if ( document.DistanceForm.Source.selectedIndex== 6 )
    {
      cm=d*30.48*3;
      cmtoTarget(cm);
    }
    else if ( document.DistanceForm.Source.selectedIndex== 7 )
    {
      cm=d*30.48*5280;
      cmtoTarget(cm);
    }
  }
}

// Weight
function gramtokg(d)
{
  kg=d/1000;
  return kg;
}
function gramtometricton(d)
{
  lb=gramtolbavdp(d);
  metricton=lb/2204.600;
  return metricton;
}
function gramtogram(d)
{
  return d;
}
function gramtoDalton(d)
{
  Dalton=d/1.65E-24;
  return Dalton;
}
function gramtomg(d)
{
  mg=d*1000;
  return mg;
}
function gramtolbavdp(d)
{
  lb=d/453.5924;
  return lb;
}
function gramtoozavdp(d)
{
  lb=gramtolbavdp(d);
  oz=lb*16;
  return oz;
}
function gramtoshortton(d)
{
  lb=gramtolbavdp(d);
  shortton=lb/2000;
  return shortton;
}
function gramtolongton(d)
{
  lb=gramtolbavdp(d);
  longton=lb/2240;
  return longton;
}
function gramtotroyoz(d)
{
  troyoz=d/31.103481;
  return troyoz;
}
function gramtotroylb(d)
{
  troyoz=gramtotroyoz(d);
  troylb=troyoz/12;
  return troylb;
}


function gramtoTarget(gram)
{
  if ( document.WeightForm.Target.selectedIndex == 0 )
  {
    Dalton=gramtoDalton(gram);
    alert(Dalton + " Dalton");
  }
  else if ( document.WeightForm.Target.selectedIndex == 1 )
  {
    mg=gramtomg(gram);
    alert(mg + " mg");
  }
  else if ( document.WeightForm.Target.selectedIndex == 2 )
  {
    gram=gramtogram(gram);
    alert(gram + " g");
  }
  else if ( document.WeightForm.Target.selectedIndex == 3 )
  {
    kg=gramtokg(gram);
    alert(kg + " kg");
  }
  else if ( document.WeightForm.Target.selectedIndex == 4 )
  {
    metricton=gramtometricton(gram);
    alert(metricton + " (metric) tons");
  }
  else if ( document.WeightForm.Target.selectedIndex == 5 )
  {
    oz=gramtoozavdp(gram);
    alert(oz  + " oz");
  }
  else if ( document.WeightForm.Target.selectedIndex == 6 )
  {
    lb=gramtolbavdp(gram);
    alert(lb + " lb");
  }
  else if ( document.WeightForm.Target.selectedIndex == 7 )
  {
    shortton=gramtoshortton(gram);
    alert(shortton + " short tons");
  }
  else if ( document.WeightForm.Target.selectedIndex == 8 )
  {
    longton=gramtolongton(gram);
    alert(longton + " long tons");
  }
  else if ( document.WeightForm.Target.selectedIndex == 9 )
  {
    troyoz=gramtotroyoz(gram);
    alert(troyoz + " troy oz");
  }
  else if ( document.WeightForm.Target.selectedIndex == 10 )
  {
    troylb=gramtotroylb(gram);
    alert(troylb + " troy lb");
  }
}

function pressConvertW()
{
  var gram;

  value = document.WeightForm.Value.value;
  d = parseFloat(value);
  if ( isNaN(d))
  {
    alert("Please enter a number");
  }
  else
  {
    if ( document.WeightForm.Source.selectedIndex == 0 )
    {
      gram=d*1.650E-24;
      gramtoTarget(gram);
    }
    else if ( document.WeightForm.Source.selectedIndex == 1 )
    {
      gram=d/1000;
      gramtoTarget(gram);
    }
    else if ( document.WeightForm.Source.selectedIndex == 2 )
      gramtoTarget(d);
    else if ( document.WeightForm.Source.selectedIndex == 3 )
    {
      gram=d*1000;
      gramtoTarget(gram);
    }
    else if ( document.WeightForm.Source.selectedIndex == 4 )
    {
      lb=d*2204.600;
      gram=lb*453.5924;
      gramtoTarget(gram);
    }
    else if (document.WeightForm.Source.selectedIndex== 5 )
    {
      lb=d/16;
      gram=lb*453.5924;
      gramtoTarget(gram);
    }
    else if ( document.WeightForm.Source.selectedIndex== 6 )
    {
      gram=d*453.5924;
      gramtoTarget(gram);
    }
    else if ( document.WeightForm.Source.selectedIndex== 7 )
    {
      lb=d*2000;
      gram=lb*453.5924;
      gramtoTarget(gram);
    }
    else if ( document.WeightForm.Source.selectedIndex== 8 )
    {
      lb=d*2240;
      gram=lb*453.5924;
      gramtoTarget(gram);
    }
    else if ( document.WeightForm.Source.selectedIndex== 9 )
    {
      gram=d*31.103481;
      gramtoTarget(gram);
    }
    else if ( document.WeightForm.Source.selectedIndex== 10 )
    {
      gram=d*31.103481*12;
      gramtoTarget(gram);
    }
  }
}

// Volume
var mloz = 29.5729;

function oztoml(d)
{
  oz=d*mloz;
  return oz;
}
function mltooz(d)
{
  ml=d/mloz;
  return ml;
}
function mltoml(d)
{
  return d;
}
function tsptoml(d)
{
  ml=d*(mloz/6);
  return ml;
}
function mltotsp(d)
{
  tps=d/(mloz/6);
  return tps;
}
function tbsptoml(d)
{
  ml=d*(mloz/2);
  return ml;
}
function mltotbsp(d)
{
  tbsp=d/(mloz/2);
  return tbsp;
}
function cuptoml(d)
{
  ml=d*(mloz*8);
  return ml;
}
function mltocup(d)
{
  cup=d/(mloz*8);
  return cup;
}
function pinttoml(d)
{
  ml=d*(mloz*16);
  return ml;
}
function mltopint(d)
{
  pint=d/(mloz*16);
  return pint;
}
function quarttoml(d)
{
  ml=d*(mloz*32);
  return ml;
}
function mltoquart(d)
{
  quart=d/(mloz*32);
  return quart;
}
function litertoml(d)
{
  ml=d*1000;
  return ml;
}
function mltoliter(d)
{
  liter=d/1000;
  return liter;
}
function gltoml(d)
{
  ml=d*(mloz*128);
  return ml;
}
function mltogl(d)
{
  gl=d/(mloz*128);
  return gl;
}
function cctoml(d)
{
  ml=d/1.000027051794;
  return ml;
}
function mltocc(d)
{
  cc=d*1.000027051794;
  return cc;
}
function SourceToHub(index,d)
{
  if (index==1)
    hub=mltoml(d);
  else if (index==2)
    hub=litertoml(d);
  else if (index==3)
    hub=oztoml(d);
  else if (index==4)
    hub=tsptoml(d);
  else if (index==5)
    hub=tbsptoml(d);
  else if (index==6)
    hub=cuptoml(d);
  else if (index==7)
    hub=pinttoml(d);
  else if (index==8)
    hub=quarttoml(d);
  else if (index==9)
    hub=gltoml(d);
  else if (index==0)
    hub=cctoml(d);
  return hub;
}
function HubToTarget(index,ml)
{
  if (index==1)
    dest=mltoml(ml)+" ml";
  else if (index==2)
    dest=mltoliter(ml)+" liters";
  else if (index==3)
    dest=mltooz(ml)+" oz";
  else if (index==4)
    dest=mltotsp(ml)+" tsp";
  else if (index==5)
    dest=mltotbsp(ml)+" tbsp";
  else if (index==6)
    dest=mltocup(ml)+" cup";
  else if (index==7)
    dest=mltopint(ml)+" pint";
  else if (index==8)
    dest=mltoquart(ml)+" quart";
  else if (index==9)
    dest=mltogl(ml)+" gl";
  else if (index==0)
    dest=mltocc(ml)+" cc";
  return dest;
}

function pressConvertV()
{

  text=document.VolumeForm.Value.value;
  d=parseFloat(text);
  if (isNaN(d))
  {
    alert("Please enter a number");
  }
  else
  {
    index=document.VolumeForm.Source.selectedIndex;
    ml=SourceToHub(index,d);
    index=document.VolumeForm.Target.selectedIndex;
    ans=HubToTarget(index,ml);
    alert(ans);

  }
}
