am = new Array(13.62, 676.50, 71.02, 437.5, 156.99)
bm = new Array(3.2258, 2.4390, 1.1765, 1.9417, 4.0000)

totpts=new Array(0,0)

function cntres(fnum,num,pts) 
{
	a=am;
	b=bm;

	pts=new Number(pts)
	
	
	if (num==0||num==4)
	{
		res = Math.pow((1010/(pts+10)),(1/b[num]))*a[num];
	}
	else
	{
		res = a[num]/Math.pow((1010/(pts+10)),(1/b[num]));
	}
	if (isNaN(res)) {res=0}
	
	nolla=0
	totpts[fnum]=new Number(nolla)

	for (i=0;i<5;i++) 
	{
		nextval=new Number(document.forms[fnum].elements[4*i+3].value);
		totpts[fnum]+=nextval;
	}
	
	document.forms[fnum].totpts.value=totpts[fnum]

	if (num==4)
	{
		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
	{
		res=replaceSubstring(""+res, ".", ",");
	}
	
	return res;
}


function cntpts(fnum,num,input) 
{
	a=am;
	b=bm;

	
	if (input==0) {input=b[num]}
	
	input=replaceSubstring(input, ",", ".");

	if (num==4)
	{
		input=eval(input.substring(0,1))*60+eval(input.substring(2,4))+eval(input.substring(5,7))/100;
	}

	if (num==2 && input < 10)
	{
		input=input*100;
	}

	if (num==1 && input < 10)
	{
		input=input*100;
	}


	if (num==0||num==4)
	{
		pts = Math.floor(1010/Math.pow((input/a[num]),b[num])-10);
	}
	else
	{
		pts = Math.floor(1010/Math.pow((a[num]/input),b[num])-10);
	}

	if (isNaN(pts)) {pts=0}
	
	document.forms[fnum].elements[4*num+3].value=pts;

	nolla=0
	totpts[fnum]=new Number(nolla)

	for (i=0;i<5;i++) 
	{
		nextval=new Number(document.forms[fnum].elements[4*i+3].value);
		totpts[fnum]+=nextval;
	}
	document.forms[fnum].totpts.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
