/*
Gain and Watts Calculator Copyright (C) 2005 Giacomo Cau

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
*/

function step(f, t, n) {
	return Math.pow(t/f, 1/(n-1))
}

function prc(a, b) {
	return ( b/a - 1 ) * 100;
}

function GetScriptEngineInfo(){
	var s;
	s = ''; // Build string with necessary info.
	s += ScriptEngine() + ' Version ';
	s += ScriptEngineMajorVersion() + '.';
	s += ScriptEngineMinorVersion() + '.';
	s += ScriptEngineBuildVersion();
	return(s);
}

String.prototype.rep = function (n) {
	var s = ""
	while (n--) s+=this
	return s
}


String.prototype.replaceAll = function (f,r) {
   var s = this
   while(s.indexOf(f) != -1)
      s = s.replace(f,r)
   return s
}

Number.prototype.frmt = function (d,f) {
	var m = Math.pow(10,d);	
	var n = (Math.floor(this * m + 0.5)/m).toString();
	if (f && d > 0) {
		m = n.indexOf('.')
		if (m==-1) 
			n += '.' + '0'.rep(d)
		else 
			n=(n+'0'.rep(d)).substring(0,m+d+1)
	}
	return n
}

Number.prototype.log10 = function ()  {
	return Math.log(10) * Math.log(this)
} 

Number.prototype.toPrc = function () {
	return ( (this) - 1 ) * 100;
}

Object.prototype.toString = function (o) {
	var s = ''
	if (!o) o = this
	for (var p in o ) {
		s += p + ':' + o[p] + ' '
	}
	return s
}

function Argv() {
	argv = {}
	var s = document.location.search.substr(1).replaceAll('+', ' ')
	if (s) {
		var a = s.split('&')
		for (var i=0; i<a.length; i++ ) {
			var p = a[i].indexOf('=')
			argv[a[i].substring(0,p)] = a[i].substr(p+1)
		}
	}
	return argv
}

function cpyForm( t, f ) {
   for ( var e in f )  {
      if ( t[e] ) {
         if (t[e].type == 'text'
			||  t[e].type == 'hidden') 
            t[e].value = f[e]
         else if (t[e].type == 'checkbox') 
            t[e].checked = true
         else if (t[e].type == 'select-one') {
            var s=t[e]
            for (var i=0; i<s.length; i++) {	
               if (s.options[i].value==f[e])
                  s.options[i].selected=true
            } 
         }
			else if (t[e].length) { // radio ovvero controlli con lo stesso nome!!
            var c=t[e]
            for (var i=0; i<c.length; i++) {	
               if (c[i].value==f[e])
                  c[i].checked=true
            } 
			}
      }
   }
}


Array.prototype.max = function() {
	var m = Number.MIN_VALUE
	for (var i=0; i<this.length; i++) {
		m = Math.max(m, this[i]) 
	}
	return m
}

Array.prototype.min = function() {
	var m = Number.MAX_VALUE
	for (var i=0; i<this.length; i++) {
		m = Math.min(m, this[i]) 
	}
	return m
}

Array.prototype.mean = function (n) {
	var m=0
	var e=this.length + (n?n:0)
	for (var i=0; i<e; i++) {
		m += this[i]
	} 
	return m / e
}

Array.prototype.mktoString = function (f) {
	this.toString = function () {
		var s=''
		for (var i=0; i<this.length; i++) {
			s += (i?' ' : '' ) + (f ? f(this[i]) : this[i])
		}
		return s
	}
}

