am = new Array(25.4348,90.5674,51.39,585.64,1.53775,5.74354,12.91,140.182,10.14,0.03768)
bm = new Array(18.0, 2.2, 1.5, 0.75, 82.0, 28.5, 4.0, 1.0, 7.0, 480.0)
cm = new Array(1.81, 1.4, 1.05, 1.42, 1.81, 1.92, 1.1, 1.35, 1.08, 1.85)

totpts=new Array(0,0)

function cntres(fnum,num,form) 
{
	a=am;
	b=bm;
	c=cm;

	pts=new Number(form["pts"+fnum+num].value)
	
	
	d=1
	res=0

	if (num==0||num==4||num==5||num==9) 
	{
		d*=-1;
		res=-0.01
	}

	d*=Math.pow((pts/a[num]),1/c[num])
	res += Math.ceil(100 * (b[num] + d)) / 100
	if (isNaN(res)) {res=0}
	pts-=1
	
	nolla=0
	totpts[fnum]=new Number(nolla)

	for (i=0;i<10;i++) 
	{
		nextval=new Number(form["pts"+fnum+i].value);
		totpts[fnum]+=nextval;
	}
	
	form["totpts"+fnum].value=totpts[fnum];

	if (num==9)
	{
		res=res.toString();
		oo=res.substring(4,7);
		res=res.substring(0,3);
		mm=eval(res)/60;
		mm=mm.toString();
		mm=mm.substring(0,1);
		ss=res-(60*mm);
		if (ss<10)
		{
			ss="0"+ss;
		}
		
		res=mm+"."+ss+","+oo;
	}
	else
	{
		form["res"+fnum+num].value=replaceSubstring(""+res, ".", ",");
		return replaceSubstring(""+res, ".", ",")	
	}
	form["res"+fnum+num].value=res;
	return res
}


function cntpts(fnum,num,form) 
{
	a=am;
	b=bm;
	c=cm;

	input=form["res"+fnum+num].value;
	input=replaceSubstring(input, ",", ".");

	if (input==0) {input=b[num]}
	
	
	if (num==9)
	{
		input=eval(input.substring(0,1))*60+eval(input.substring(2,4))+eval(input.substring(5,7))/100;
	}

	d=input-b[num]

	if (num==0||num==4||num==5||num==9) 
	{
		d*=-1
	}

	pts = Math.floor(a[num]*Math.pow(d,c[num]));
	if (isNaN(pts)) {pts=0}
	
	form["pts"+fnum+num].value=pts;

	nolla=0
	totpts[fnum]=new Number(nolla)

	for (i=0;i<10;i++) 
	{
		nextval=new Number(form["pts"+fnum+i].value);
		totpts[fnum]+=nextval;
	}

	form["totpts"+fnum].value=totpts[fnum];

	return pts
}

function replaceSubstring(inputString, fromString, toString) 
{
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function
