function isNum(args)
{
	args = args.toString();

	if (args.length == 0)
	return false;

	for (var i = 0;  i<args.length;  i++)
	{
		if ((args.substring(i,i+1) < "0" || args.substring(i, i+1) > "9") && args.substring(i, i+1) != ".")
			{
			return false;
			}
	}

	return true;
}

function clearForm()
{
	document.weight.kg.value="";
	document.weight.ton.value="";
	document.weight.carat.value="";
	document.weight.g.value="";
	document.weight.mg.value="";
	document.weight.ug.value="";
	document.weight.ng.value="";
	document.weight.pg.value="";
	document.weight.longton.value="";
	document.weight.longhw.value="";
	document.weight.stone.value="";
	document.weight.ounce.value="";
	document.weight.grain.value="";
	document.weight.caratuk.value="";
	document.weight.shortton.value="";
	document.weight.shorthw.value="";
	document.weight.pound.value="";
	document.weight.dram.value="";
}

function convertweight(ii,opt)
{
	var   kg =document.weight.kg.value;
	var   ton=document.weight.ton.value;
	var   carat=document.weight.carat.value;
	var   g=document.weight.g.value;
	var   mg=document.weight.mg.value;
	var   ug=document.weight.ug.value;
	var   ng=document.weight.ng.value;
	var   pg=document.weight.pg.value;
	var   longton=document.weight.longton.value;
	var   longhw=document.weight.longhw.value;
	var   stone=document.weight.stone.value;
	var   ounce=document.weight.ounce.value;
	var   grain=document.weight.grain.value;
	var   caratuk=document.weight.caratuk.value;
	var   shortton=document.weight.shortton.value;
	var   shorthw=document.weight.shorthw.value;
	var   pound=document.weight.pound.value;
	var   dram=document.weight.dram.value;

	
	if (opt=="ton")  
	{
	    if (isNum(ton))
		{
			  kg = ton*1000;
			  carat = kg * 5000;
			  g = ton*1000000;
			  mg = ton*1000000000;
			  ug = ton*1000000000000;
			  ng = ton*1000000000000000;
			  pg = ton*1000000000000000000;
			  
			  longton = ton * 0.9842065276110606;
			  longhw = longton*20;
			  stone = longton*160;
			  ounce = longton*35840;
			  grain = longton*15680000;
			  caratuk = ton*1000000*3.8580895882;
			  
			  shortton = ton * 1.1023113109243878;
			  shorthw = shortton * 20;
			  pound = shortton * 2000;
			  dram = shortton * 512000;
		}
		else
		{
			  ton = ton.substring(0,ton.length-1);			  
		}
	}	
	else if (opt=="kg")
	{
	    if (isNum(kg))
		{
			  ton = kg/1000;
			  carat = kg * 5000;
			  g = kg*1000;
			  mg = kg*1000000;
			  ug = kg*1000000000;
			  ng = kg*1000000000000;
			  pg = kg*1000000000000000;
			  
			  stone = 160*kg*0.9842065276110606/1000;
		      longhw = stone/8;	
			  longton = stone/160;
			  ounce = stone*224;
			  grain = longton*15680000;
			  
			  caratuk = ton*1000000*3.8580895882;
			  
			  pound = kg * 2*1.1023113109243878;
			  shortton = pound/2000;
			  shorthw = pound/100;

			  dram = pound *256;
		  }
		  else
		  {
			  kg = kg.substring(0,kg.length-1);			  	  
		  }
	}   
	else if (opt=="g")
	{
	    if (isNum(g))
		{
			  ton = g/1000000;
			  kg = g/1000;
			  mg = g * 1000;
			  ug = g * 1000000;
			  ng = g * 1000000000;
			  pg = g * 1000000000000;
			  carat = g * 5;
			  
			  ounce = 160*224*g*0.9842065276110606/1000000;
		      
			  grain = ounce*437.5;
			  stone = ounce/224;
			  longhw = ounce/1792;
			  longton = ounce/(1792*20);
			  caratuk = g*3.8580895882;

		      dram = g*512*1.1023113109243878/1000;	  
			  pound = dram/256;
			  shorthw = dram/25600;
			  shortton = dram/512000;
		  }
		  else
		  {
			g = g.substring(0,g.length-1);			  	  
		  }

	}
	else if (opt=="mg")
	{
	    if (isNum(mg))
		{
			  ton = mg/1000000000;
			  kg = mg/1000000;
			  g = mg/1000;
			  ug = mg * 1000;
			  ng = mg * 1000000;
			  pg = mg * 1000000000;
			  carat = mg*5/1000;
		      
			  longton = mg*0.9842065276110606/1000000000;
			  longhw = longton*20;
			  stone = longton*160;
			  ounce = longton*35840;
			  grain = longton*15680000;
			  caratuk = ton*1000000*3.8580895882;

			  shortton = mg * 1.1023113109243878/1000000000;
			  shorthw = shortton * 20;
			  pound = shortton * 2000;
			  dram = shortton * 512000;	 
		  }
		  else
		  {
			mg= mg.substring(0,mg.length-1);			  	  
		  }
		  
	}
	else if (opt=="ug")
	{
	    if (isNum(ug))
		{
		  ton = ug/1000000000000;
		  kg = ug/1000000000;
		  g = ug/1000000;
		  mg = ug/1000;
		  ng = ug * 1000;
		  pg = ug * 1000000;
		  carat = ug*5/1000000;
	      
		  longton = ug*0.9842065276110606/1000000000000;
		  longhw = longton*20;
		  stone = longton*160;
		  ounce = longton*35840;
		  grain = longton*15680000;
		  caratuk = ton*1000000*3.8580895882;

		  shortton = ug * 1.1023113109243878/1000000000000;
		  shorthw = shortton * 20;
		  pound = shortton * 2000;
		  dram = shortton * 512000;	  
		  }
		  else
		  {
			ug= ug.substring(0,ug.length-1);			  	  
		  }
	}
	else if (opt=="pg")
	{
	    if (isNum(pg))
		{
			  ton = pg/1000000000000000000;
			  kg = pg/1000000000000000;
			  g = pg/1000000000000;
			  mg = pg/1000000000;
			  ug = pg/1000000;
			  ng = pg/1000;
			  carat = pg*5/1000000000000;
		      
		      grain = pg * 15680000* 0.9842065276110606/1000000000000000000;
			  ounce = grain*35840/15680000;
			  stone = grain * 160/15680000;
			  longhw = grain *20/15680000;
			  longton = grain/15680000;
			  caratuk = ton*1000000*3.8580895882;

			  dram = pg * 1.1023113109243878*512000/1000000000000000000;
		      shortton = dram/512000;
		      shorthw = dram*20/512000;
		      pound = dram *2000/512000;
		  }
		  else
		  {
			  pg = pg.substring(0,pg.length-1);			  	  
		  }
	}
	else if (opt=="ng")
	{
	    if (isNum(ng))
		{
		  ton = ng/1000000000000000;
		  kg = ng/1000000000000;
		  g = ng/1000000000;
		  mg = ng/1000000;
		  ug = ng/1000;
		  pg = ng * 1000;
		  carat = ng*5/1000000000;
	 
	      grain = ng * 1568000* 0.9842065276110606/100000000000000;
		  ounce = grain*35840/15680000;
		  stone = grain * 160/15680000;
		  longhw = grain *20/15680000;
		  longton = grain/15680000;
		  caratuk = ton*1000000*3.8580895882;

		  dram = ng * 1.1023113109243878*512000/1000000000000000;
	      shortton = dram/512000;
	      shorthw = dram*20/512000;
	      pound = dram *2000/512000;
		  }
		  else
		  {
			ng = ng.substring(0,ng.length-1);			  	  
		  }
	}
	else if (opt=="carat")
	{
	    if (isNum(carat))
		{
		  g = carat/5;
		  mg = g * 1000;
		  ug = g * 1000000;
		  
		  ng = g * 1000000000;
		  ng = g * 1000000000;
		  pg = g * 1000000000000;
		  
		  kg = g/1000;
		  ton = g/1000000;
	 
	      grain = carat * 1568* 0.9842065276110606/500;
		  ounce = grain*35840/15680000;
		  stone = grain * 160/15680000;
		  longhw = grain *20/15680000;
		  longton = grain/15680000;
		  caratuk = ton*1000000*3.8580895882;

		  dram = carat * 1.1023113109243878*512000/5000000;
	      shortton = dram/512000;
	      shorthw = dram*20/512000;
	      pound = dram *2000/512000;
		  }
		  else
		  {
			carat = carat.substring(0,carat.length-1);			  	  
		  }
	}
	else if (opt=="longton")
	{
	      if (isNum(longton))
		  {
			  longhw = longton*20;
			  stone = longton*160;
			  ounce = longton*35840;
			  grain = longton*15680000;
			  caratuk = longton*3920000;
			  
			  ton = longton/0.9842065276110606;
			  shortton = longton*1.12;

			  kg = ton*1000;
			  carat = kg * 5000;
			  g = ton*1000000;
			  mg = ton*1000000000;
			  ug = ton*1000000000000;
			  ng = ton*1000000000000000;
			  pg = ton*1000000000000000000;
			  
			  shorthw = shortton * 20;
			  pound = shortton * 2000;
			  dram = shortton * 512000;
		  }
		  else
		  {
			longton = longton.substring(0,longton.length-1);			  	  
		  }
	}
	else if (opt=="longhw")
	{
	    if (isNum(longhw))
		{
		  longton = longhw/20;
		  stone = longton*160;
		  ounce = longton*35840;
		  grain = longton*15680000;
		  caratuk = longton*3920000;
		  
		  ton = longton/0.9842065276110606;
		  shortton = longton*1.12;
		  
		  kg = ton*1000;
		  carat = kg * 5000;
		  g = ton*1000000;
		  mg = ton*1000000000;
		  ug = ton*1000000000000;
		  ng = ton*1000000000000000;
		  pg = ton*1000000000000000000;
		  
		  shorthw = shortton * 20;
		  pound = shortton * 2000;
		  dram = shortton * 512000;
		  }
		  else
		  {
			longhw = longhw.substring(0,longhw.length-1);			  	  
		  }
	}
	else if (opt=="stone")
	{
	    if (isNum(stone))
		{
	      longhw = stone/8;	
		  longton = stone/160;
		  ounce = stone*224;
		  grain = stone*98000;
		  caratuk = longton*3920000;
		  
		  ton = stone/(160*0.9842065276110606);
		  kg = stone*100/(16 * 0.9842065276110606);
		  
		  shortton = longton*1.12;

		  carat = kg * 5000;
		  g = kg*1000;
		  mg = kg*1000000;
		  ug = kg*1000000000;
		  ng = kg*1000000000000;
		  pg = kg*1000000000000000;
		  
		  shorthw = stone*0.14;
		  pound = stone * 14;
		  dram = stone * 3584;
		  }
		  else
		  {
			stone = stone.substring(0,stone.length-1);			  	  
		  }

		  }
	else if (opt=="ounce")
	{
	    if (isNum(ounce))
		{
		  stone = ounce/224;
		  longhw = ounce/(224*8);	
		  longton = ounce/(160*224);
		  grain = ounce*437.5;
		  caratuk = ounce*109.375;	  
		  
		  g = ounce*1000000/(160*224*0.9842065276110606);
		  dram = ounce/16;
		  
		  ton = g/1000000;
		  kg = g/1000;
		  mg = g * 1000;
		  ug = g * 1000000;
		  ng = g * 1000000000;
		  pg = g * 1000000000000;
		  carat = g * 5;
	      
	      dram = g*512*1.1023113109243878/1000;	  
		  pound = dram/256;
		  shorthw = dram/25600;
		  shortton = dram/512000;	  
		  }
		  else
		  {
			ounce = ounce.substring(0,ounce.length-1);			  	  
		  }
	}
		else if (opt=="grain")
	{
	    if (isNum(grain))
		{
		  ounce = grain*35840/15680000;
		  stone = grain * 160/15680000;
		  longhw = grain *20/15680000;
		  longton = grain/15680000;
		  caratuk = ounce*109.375;	  

	      dram = grain/27.34375;	  
	      shortton = dram/512000;
	      shorthw = dram*20/512000;
	      pound = dram *2000/512000;
		  
		  g = grain * 100/(1568 * 0.9842065276110606);
		  ton = g/1000000;
		  kg = g/1000;
		  mg = g * 1000;
		  ug = g * 1000000;
		  ng = g * 1000000000;
		  pg = g * 1000000000000;
		  carat = g * 5;
		  }
		  else
		  {
			grain = grain.substring(0,grain.length-1);			  	  
		  }
	}
else if (opt=="shortton")
	{
	    if (isNum(shortton))
		{
		 shorthw = shortton*20;
		 pound = shortton *2000;
		 grain = shortton * 14000000;
		 dram = shortton * 512000;
		 ounce = shortton * 32000;
		 
		 ton = shortton/1.1023113109243878;
		 
		 longton = shortton/1.12;
		  kg = ton*1000;
		  carat = kg * 5000;
		  g = ton*1000000;
		  mg = ton*1000000000;
		  ug = ton*1000000000000;
		  ng = ton*1000000000000000;
		  pg = ton*1000000000000000000;
		  
		  longton = ton * 0.9842065276110606;
		  longhw = longton*20;
		  stone = longton*160;
		  ounce = longton*35840;
		  grain = longton*15680000;
		  caratuk = ounce*109.375;	  
		  }
		  else
		  {
			shortton = shortton.substring(0,shortton.length-1);			  	  
		  }
	}
	else if (opt=="shorthw")
	{
	    if (isNum(shorthw))
		{
			  shortton = shorthw/20;
			  ton = shortton/1.1023113109243878;	 
			  longton = shortton/1.12;	 
			  kg = ton*1000;
			  carat = kg * 5000;
			  g = ton*1000000;
			  mg = ton*1000000000;
			  ug = ton*1000000000000;
			  ng = ton*1000000000000000;
			  pg = ton*1000000000000000000;	  
			  longhw = longton*20;
			  stone = longton*160;
			  ounce = longton*35840;
			  grain = shorthw*700000;
			  caratuk = ounce*109.375;	  	  
			  pound = shortton * 2000;
			  dram = shortton * 512000;	 
		  }
		  else
		  {
			shorthw = shorthw.substring(0,shorthw.length-1);			  	  
		  }
	}
	else if (opt=="pound")
	{
	     if (isNum(pound))
		{
		  kg = pound*1000/(2000*1.1023113109243878);
		  ton = kg/1000;
		  carat = kg * 5000;
		  g = kg*1000;
		  mg = kg*1000000;
		  ug = kg*1000000000;
		  ng = kg*1000000000000;
		  pg = kg*1000000000000000;
		    
		  shortton = pound/1000;
		  shorthw = pound/100;
		  pound = kg * 2*1.1023113109243878;
		  dram = pound *256;
		 
		  ounce = pound*16;
		  stone = ounce/224;
		  longhw = ounce/(224*8);	
		  longton = ounce/(160*224);
		  grain = pound*7000;
		  caratuk = ounce*109.375;	  	 
		  }
		  else
		  {
			pound = pound.substring(0,pound.length-1);			  	  
		  }
	}
	else if (opt=="dram")
	{
	     if (isNum(dram))
		{
		 ounce = dram/16;
		 
		 longton = dram/573440;
		 
		  g = dram*1000000/(573440*0.9842065276110606);
		  ton = g/1000000;
		  kg = g/1000;
		  mg = g * 1000;
		  ug = g * 1000000;
		  ng = g * 1000000000;
		  pg = g * 1000000000000;
		  carat = g * 5;
	      
		  grain = ounce*437.5;
		  stone = ounce/224;
		  longhw = ounce/1792;
		  longton = ounce/(1792*20);
		  caratuk = g*3.8580895882;

		  pound = dram/256;
		  shorthw = dram/25600;
		  shortton = dram/512000;
		  }
		  else
		  {
			 dram = dram.substring(0,dram.length-1);			  	  
		  }
	}
	else if (opt=="caratuk")
	{
	    if (isNum(caratuk))
		{
			  ounce = caratuk/109.375;
			  g = caratuk /3.8580895882;		  
			  ton = g/1000000;
			  kg = g/1000;
			  mg = g * 1000;
			  ug = g * 1000000;
			  ng = g * 1000000000;
			  pg = g * 1000000000000;
			  carat = g * 5;
		      
			  grain = ounce*437.5;
			  stone = ounce/224;
			  longhw = ounce/1792;
			  longton = ounce/(1792*20);

		      dram = g*512*1.1023113109243878/1000;	  
			  pound = dram/256;
			  shorthw = dram/25600;
			  shortton = dram/512000;	 
		  }
		  else
		  {
			caratuk = caratuk.substring(0,caratuk.length-1);			  	  
		  }
	}

	document.weight.kg.value=kg;
	document.weight.ton.value=ton;
	document.weight.carat.value=carat;
	document.weight.g.value=g;
	document.weight.mg.value=mg;
	document.weight.ug.value=ug;
	document.weight.ng.value=ng;
	document.weight.pg.value=pg;
	document.weight.longton.value=longton;
	document.weight.longhw.value=longhw;
	document.weight.stone.value=stone;
	document.weight.ounce.value=ounce;
	document.weight.grain.value=grain;
	document.weight.caratuk.value=caratuk;
	document.weight.shortton.value=shortton;
	document.weight.shorthw.value=shorthw;
	document.weight.pound.value=pound;
	document.weight.dram.value=dram;
}

