am = new Array(58.015, 90.5674, 51.39, 585.64, 20.5173, 140.182, 0.08713)
bm = new Array(11.5, 2.2, 1.5, 0.75, 15.5, 1.0, 305.5)
cm = new Array(1.81, 1.4, 1.05, 1.42, 1.92, 1.35, 1.85)

totpts=new Array(0,0)

function cntres(fnum,num,pts) 
{
	a=am;
	b=bm;
	c=cm;

	pts=new Number(pts)
	
	
	d=1
	res=0

	if (num==0||num==4||num==6) 
	{
		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<7;i++) 
	{
		nextval=new Number(document.forms[fnum].elements[4*i+3].value);
		totpts[fnum]+=nextval;
	}
	
	document.forms[fnum].totpts.value=totpts[fnum]

	if (num==6)
	{
		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;
	c=cm;

	
	if (input==0) {input=b[num]}
	
	input=replaceSubstring(input, ",", ".");

	if (num==6)
	{
		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==6) 
	{
		d*=-1
	}

	pts = Math.floor(a[num]*Math.pow(d,c[num]));
	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<7;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
