// Begin rounding function
function roundIt(number,X) {
// rounds number to X decimal places, defaults to 2
X = (!X ? 2 : X);
return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

// convert r to z
function r2z(r) {
     with (Math) {
          var z = .5 * (log(((1+r)/(1-r)))) ; 
          return z ;
     }
}

// convert r to d
function r2d(r) {
     with (Math) {
          var rsq = r*r;
		  var d =(r*2)/(sqrt((1-rsq)))
          return d ;
     }
}
/* Check for the direction of the 
   effect size and write the 
   conversion results to the 
   appropriate form fields
*/
function WriteValues2Fields(r,d,z,Ntot){
     var e = prompt("Enter the direction of the Effect Size; \nEnter a + if it is in the predicted direction. A - if it is not."," ");
     if (e =="-") {
          with (Math) {
               var r = (-1*(abs(r)));
               var d = (-1*(abs(d)));
               var z = (-1*(abs(z)));
          }
     }
     document.ConvertStudyStats.r.value = roundIt(r,4) ;
     document.ConvertStudyStats.d.value = roundIt(d,4) ;
     document.ConvertStudyStats.z.value = roundIt(z,4) ;
     document.ConvertStudyStats.Ntot.value = Ntot;
}

// Enter Reliability Estimates For X and Y
function EnterReliability (){
     if (confirm("Do you want to enter reliability estiimates?")) {
          document.ConvertStudyStats.RelX.value = prompt("Enter Reliability Estimate for X.\nLeave blank to skip.","");
          document.ConvertStudyStats.RelY.value = prompt("Enter Reliability Estimate for Y.\nLeave blank to skip.","");
     }
}

// convert t to effect size
function t2Fx() { 
    var t = parseFloat(prompt("\nEnter the t-value",""));
    var Ne = parseInt(prompt("\nEnter the number of subjects in the Experimental Group.","")) ;
    var Nc = parseInt(prompt("\nEnter the number of subjects in the Comparison Group.","")) ;
    var Ntot = Ne + Nc ;
    var df = Ntot - 1 ; 
    with (Math) {
     var d = (t*Ne) / ((sqrt(df)) * (sqrt((Ne*Nc))));
     var r = sqrt(((d*d)/((d*d)+4)));
    }
     var z = r2z(r);
     WriteValues2Fields(r,d,z,Ntot);
     EnterReliability();
     
}
// convert One Way ANOVA to effect size
function OnewayF2Fx(){
     var f = parseFloat(prompt("Enter the One Way F value.",""));
     var dfg = parseInt(prompt("Enter the Between Groups df.",""));
     var dfe = parseInt(prompt("Enter the Within Groups (error) df.",""));
     var Ntot = parseInt(prompt("Enter the Total number of subjects in this study.",""));
     with (Math) {
          var r = sqrt((f * dfg) /((f * dfg) + dfe));
          var d = (2 * r) / (sqrt((1-(r*r))));
     }
     
     var z = r2z(r);
     WriteValues2Fields(r,d,z,Ntot);
     EnterReliability();
}
// convert an F from a two way anova (A x B) to effect size
function TWF2Fx() {
     var fa = parseFloat(prompt("Enter the F value for Effect A.",""));
     var dfa = parseInt(prompt("Enter the df for Effect A.",""));
     var fb = parseFloat(prompt("Enter the F value for Effect B.",""));
     var dfb = parseInt(prompt("Enter the df for Effect B.",""));
     var fc = parseFloat(prompt("Enter the F value for the Interaction Effect.",""));
     var dfc = parseInt(prompt("Enter the df for the Interaction Effect.",""));
     var dfw = parseInt(prompt("Enter the Within Groups (error) df.",""));
     var Ntot = parseInt(prompt("Enter the Total number of subjects in this study.",""));
     var r = 0;
	 with (Math) {
          var r = sqrt((fa*dfa)/((fa*dfa)+(fb*dfb)+(fc*dfc)+ dfw));
     }
     var d = r2d(r);
     var z = r2z(r);
     WriteValues2Fields(r,d,z,Ntot);
     EnterReliability();
}

// convert a correlation to effect size
function R2FX(){
     var r = parseFloat(prompt("Enter the correlation.",""));
     var Ntot = parseInt(prompt("Enter the Total number of subjects in this study.",""));
     var d = r2d(r);
     var z = r2z(r);
     WriteValues2Fields(r,d,z,Ntot);
     EnterReliability();
}

// convert a one df chi square to effect size
function Chi2FX(){
     var chi = parseFloat(prompt("Enter the one df Chi Square Value.",""));
     var Ntot = parseInt(prompt("Enter the Total number of subjects in this study.",""));
     with (Math) {
     var r = sqrt(chi/Ntot) ;
     }
     var d = r2d(r);
     var z = r2z(r);
     WriteValues2Fields(r,d,z,Ntot);
     EnterReliability();
}

// convert a one df chi square to effect size
function p2Fx(){
     var p = parseFloat(prompt("Enter the one-tailed probability value.",""));
     var Ntot = parseInt(prompt("Enter the Total number of subjects in this study.",""));
     with (Math) {
          var df = Ntot-2;
          if (p > .5) {
               var p = 1 - p;
          }
          var q = sqrt(log(1/(p*p)));
          var z1 = (2.515517 + (.802852*q) +(.013028*(q*q*q)));
          var z2 = (1 + ((1.432788*q) + (.189269 * (q*q)) + (.001308*(q*q*q))));
          var z = q-(z1/z2);
          var r = sqrt(((z*z) / Ntot));
     }
     var d = r2d(r);
     var z2 = r2z(r);
     WriteValues2Fields(r,d,z2,Ntot);
     EnterReliability();
}

// convert means and standard deviations to effect size
function means2Fx() {
     var Me = parseFloat(prompt("Enter the mean for the Experimental Group.",""));
     var StdE = parseFloat(prompt("Enter the standard deviation for the Experimental Group.",""));
     var Ne = parseInt(prompt("Enter the number of subjects in the Experimental Group.",""));
     var Mc = parseFloat(prompt("Enter the mean for the Comparison Group.",""));
     var StdC = parseFloat(prompt("Enter the standard deviation for the Comparison Group.",""));
     var Nc = parseInt(prompt("Enter the number of subjects in the Comparison Group.",""));
     with (Math) {
          var varE = StdE*StdE;
          var varC = StdC*StdC;
          var Ntot = Ne + Nc;
          var df = Ntot-2;
          var pooledStd = sqrt(((((Ne-1) * varE) + ((Nc-1) * varC))/df));
          var d = (Me - Mc)/pooledStd;
          if (Me < Mc) {
               var NoteConv = alert("Warning. Experimental Group mean is less than Comparison Group mean.\nResults will be adjusted for effect size direction.");
               var d = abs(d) ;          
          }
          var r = ( sqrt(( (d*d) / ((d*d) + 4) ) ) );
     }
     var z = r2z(r);
     WriteValues2Fields(r,d,z,Ntot);
     EnterReliability();
}

// convert Cohen's d to effect size 
function d2Fx() {
     var d = parseFloat(prompt("Enter the standardized difference between Mean scores (Cohen's d).",""));
     var Ntot = parseInt(prompt("Enter the total number of subjects in theis study.",""));
     with (Math) {
          var r = ( sqrt( ( (d*d) / ((d*d) + 4) ) ) );
     }
     var z = r2z(r);
     WriteValues2Fields(r,d,z,Ntot);
     EnterReliability();
}
// limit number of entries into the citation field
function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
       
        field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else {
    countfield.value = maxlimit - field.value.length;
    }
}
// convert a z score to effect size
function z2Fx(){
     var z = parseFloat(prompt("Enter the z score value.",""));
     var Ntot = parseInt(prompt("Enter the Total number of subjects in this study.",""));
     with (Math) {
          var r = sqrt(((z^2) / Ntot));
     }
     var d = r2d(r);
     var z2 = r2z(r);
     WriteValues2Fields(r,d,z2,Ntot);
     EnterReliability();
}