// JavaScript Document

function isNumeric(){
   //  check for valid numeric strings	
   
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
	var x=document.form1.litres.value;
	var y=document.form1.price.value;
	
   if (x.length == 0 || y.length==0) blnResult=false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < x.length && blnResult == true; i++)
      {
      strChar = x.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }

   for (i = 0; i < y.length && blnResult == true; i++)
      {
      strChar = y.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
	  
	if(blnResult==false) alert("Non numeric value entered!");	
	
   return blnResult;
}

function addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function calculate_losses(){
	
	blnNumeric=isNumeric();
	
	if(blnNumeric==true){
		var litres=document.form1.litres.value;
		var price=document.form1.price.value;
		
		var per_week=litres * price;
		var per_year=per_week * 52;
		
		per_week=per_week.toFixed(2);	
		per_year=per_year.toFixed(2);
	
		var yr=addCommas(per_year);
	
		document.form1.per_week.value=addCommas(per_week);
		document.form1.per_year.value=addCommas(per_year);
	}
	
}

