
 function jsinit( )
  {

    /* renew session time for user counter*/
    counterHTML = loadDoc( 'http://www.molcon.f2s.com/discworld/includes/touch_session.php');
    divToSwitch = document.getElementById( 'usersonline' );
    divToSwitch.innerHTML = counterHTML;
    
    /* reinitialise thickbox */
    TB_init();
    
    AJForm.init();

    /* scroll to top */
    window.scrollTo(0,0);
  }

  function finger( player )
  {
    divToSwitch = document.getElementById( 'center' );
    
    newHTML = "<p>Loading...</p>";
    divToSwitch.innerHTML = newHTML;    
    
    newHTML = loadDoc( 'http://www.molcon.f2s.com/discworld/finger/' + player);
    if( newHTML && newHTML.length>5 )
      divToSwitch.innerHTML = newHTML;
  }

/***************************/

/* players online */

function OnlineSubmitPage( obj )
{
	guild=obj.options[obj.selectedIndex].value;
	divToSwitch = document.getElementById( 'bottom' );

	newHTML = "<p>Loading...</p>";
	divToSwitch.innerHTML = newHTML;

	newHTML = loadDoc( 'http://www.molcon.f2s.com/discworld/pages/online.php?bottom=1&guild=' + guild);
	divToSwitch.innerHTML = newHTML;
}

function OnlineFinger(name)
{
	divToSwitch = document.getElementById( 'bottom' );

	newHTML = "<p>Loading...</p>";
	divToSwitch.innerHTML = newHTML;

	url = "http://www.molcon.f2s.com/discworld/finger/" + name.substring(0,1).toUpperCase() + name.substring(1);
	newHTML = loadDoc( url );
	divToSwitch.innerHTML = newHTML;
}

/* end players online */

/***************************/

/* wiki search */
		var FormElementID;
		var FormElementValue;

		function SumbitSearch(obj)
		{
			FormElementID = obj.id;
			FormElementValue = obj.value;
			if(FormElementValue) {
				sendRequest(FormElementValue);
				makeHistory('wiki/Special:Search/' + FormElementValue);
			}
		}

		function createRequestObject() {
			var req;
			if(window.XMLHttpRequest){
				// Firefox, Safari, Opera...
				req = new XMLHttpRequest();
			} else if(window.ActiveXObject) {
				// Internet Explorer 5+
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				// old browser is being used.
				alert('Problem creating the XMLHttpRequest object');
			}
			return req;
		}

		// Make the XMLHttpRequest object
		var http = createRequestObject();

		function sendRequest(SaveToDB) {
			// Open server script for requests
			http.open('get', 'http://www.molcon.f2s.com/discworld/wiki/search.php?search='+SaveToDB);
			http.onreadystatechange = handleResponse;
			http.send(null);
		}

		function handleResponse() {
			if(http.readyState == 4 && http.status == 200){

				// Text returned FROM the server script
				var response = http.responseText;

				if(response) {
					// UPDATE ajaxTest content
					document.getElementById("center").innerHTML = response;
				}
			}
		}

		function catchEnter(e,obj){
			var characterCode
			if(e && e.which){
				e = e
				characterCode = e.which
			} else {
				e = event
				characterCode = e.keyCode
			}
			if(characterCode == 13){
				SumbitSearch(obj);
				return false
			} else{
				return true
			}
		}

/* end wiki search */

/***************************/

/* Teaching Cost Calculator */

function getcost() {
	var fromlevel=1*(document.expform.fromlevel.value);
	var tolevel=1*(document.expform.tolevel.value);
	var totalcost= new Number(0);

	for (j=fromlevel; j<tolevel; j++){
		totalcost= totalcost + 500 + Math.floor(375*(j*(Math.exp(j/500))));
	}

	document.expform.cost.value=totalcost;
}

/* end Teaching Cost Calculator */

/***************************/

/* Bonus Calculator */

function calc2() {
        numweights=0;
        for (i1=0;i1<54;i1++) {
                numweights+=document.skills[i1].value*1;
        }
        if (numweights <1) {
                alert("Please give a weighting to at least 1 skill.");
                return;
        }

        totalmins=0;
        for (i1=54;i1<59;i1++) {
                if (isNaN(document.skills[i1].value)) {
                        document.skills[i1].value = "8";
                }
                if (document.skills[i1].value < 8) {
                        document.skills[i1].value = "8";
                }
                if (document.skills[i1].value > 23) {
                        document.skills[i1].value = "23";
                }
                totalmins = totalmins + (document.skills[i1].value*1);
        }
        if (totalmins > 65) {
                alert("The minimums you have entered exceed at total of 65. ("+totalmins+")");
                return;
        }
        totalmaxs=0;
        for (i1=59;i1<64;i1++) {
                if (isNaN(document.skills[i1].value)) {
                        document.skills[i1].value = "23";
                }
                if (document.skills[i1].value < 8) {
                        document.skills[i1].value = "8";
                }
                if (document.skills[i1].value > 23) {
                        document.skills[i1].value = "23";
                }
                if ((document.skills[i1].value*1) < (document.skills[i1-5].value*1)) {
                        alert("Maximums must be greater or equal to the corresponding minimum.");
                        return;
                }
                totalmaxs = totalmaxs + (document.skills[i1].value*1);
        }
        if (totalmaxs < 65) {
                alert("The maximumss you have entered do not meet or exceed 65. ("+totalmaxs+")");
                return;
        }

        mincon = document.skills[54].value*1;
        mindex = document.skills[55].value*1;
        minint = document.skills[56].value*1;
        minstr = document.skills[57].value*1;
        minwis = document.skills[58].value*1;
        maxcon = document.skills[59].value*1;
        maxdex = document.skills[60].value*1;
        maxint = document.skills[61].value*1;
        maxstr = document.skills[62].value*1;
        maxwis = document.skills[63].value*1;

        var level = new Number(300);
        var maxbonustotal=new Number(0);
        var bestcon=new Number(8);
        var bestdex=new Number(8);
        var bestint=new Number(8);
        var beststr=new Number(8);
        var bestwis=new Number(8);
        var weighting = new Array();

        for (concount=mincon;concount<=maxcon;concount++) {
                for (dexcount=mindex;dexcount<=maxdex;dexcount++) {
                        for (intcount=minint;intcount<=maxint;intcount++){
                                for (strcount=minstr;strcount<=maxstr;strcount++) {
                                        for (wiscount=minwis;wiscount<=maxwis;wiscount++) {
                                                if(concount*1+dexcount*1+intcount*1+strcount*1+wiscount*1 == 65) {
                                                        bonustotal=0;
                                                        stats = [concount,dexcount,intcount,strcount,wiscount];         
                                                        for (i1=0;i1<54;i1++) {
                                                                scale=document.skills[i1].value;
                                                                if (scale > 0) {
                                                                        w = document.dependencies2[i1].value;
                                                                        for (i2=0;i2<5;i2++) {
                                                                                weighting[i2] = w.charAt(i2*2);
                                                                        }
                                                                        statfactor=0;
                                                                        for (i2=0;i2<5;i2++) {
                                                                                statfactor = statfactor + (weighting[i2]*((10.2*(Math.log(stats[i2])))-12));
                                                                        }
                                                                        statfactor = statfactor/5;
                                                                        correction = 0;
                                                                        bonus = Math.floor(((statfactor+7)*(7+(0.025*level)))+correction);
                                                                        bonustotal += bonus * scale;
                                                                }
                                                        }
                                                        if(bonustotal > maxbonustotal) {
                                                                maxbonustotal=bonustotal;
                                                                bestcon=concount;
                                                                bestdex=dexcount;
                                                                bestint=intcount;
                                                                beststr=strcount;
                                                                bestwis=wiscount;
                                                        }
                                                        
                                                }
                                        }
                                }
                        }
                }
        }
        document.results[0].value=bestcon;     
        document.results[1].value=bestdex;
        document.results[2].value=bestint;
        document.results[3].value=beststr;
        document.results[4].value=bestwis;
}
function resetform() {
        for (i5=0;i5<54;i5++) {
                document.skills[i5].value="";
        }
        for (i6=0;i6<5;i6++) {
                document.results[i6].value="";
        }
}
function calcBonuses() {

/*      for (i7=0;i7<5;i7++) {
                if((isNaN(document.results[i7].value)) || (document.results[i7].value < 8) || (document.results[i7].value > 23)) {
                        alert("The stats are not valid");
                        return;
                }
        }*/

        var stats = [document.results[0].value,document.results[1].value,document.results[2].value,document.results[3].value,document.results[4].value];

        var weighting = new Array();
        var weights = new String();
        var level = new Number(document.results[5].value);
        var ref = new Number(0);
        var best= new String();
        var beststat= new Array();

        rwin=open("","Bonuses","height=600,width=560,status=no,toolbar=no,directories=no,menubar=no,location=no,resizable=yes,scrollbars=yes,left=0,top=0");
        rwd = rwin.document;
        rwd.write("<HTML><HEAD><TITLE>Calculated Bonuses</TITLE></HEAD><BODY><PRE>");
        rwd.writeln("con "+stats[0]+", dex "+stats[1]+", int "+stats[2]+", str "+stats[3]+", wis "+stats[4]+".");
        rwd.writeln("");
        rwd.writeln("Skill name                           Lvl  Rate   Bns  Best   Diff");
        for (skillcount=0;skillcount<54;skillcount++) {
                w = document.dependencies[skillcount].value;
                for (i8=0;i8<5;i8++) {
                        weighting[i8] = w.charAt(i8*2);
                }

                statfactor=0;
                for (i9=0;i9<5;i9++) {
                        statfactor = statfactor + (weighting[i9]*((10.2*(Math.log(stats[i9])))-12));
                }
                statfactor = statfactor/5;
                correction = 0;
                bonus = Math.floor(((statfactor+7)*(7+(0.025*level)))+correction);

                rate_range_start = Math.floor(((statfactor+7)*(7+(0.025*100)))+correction)
                rate_range_end   = Math.floor(((statfactor+7)*(7+(0.025*600)))+correction)

                rate = (((rate_range_end - rate_range_start) / 500) * 1000);

                best = document.beststats2[skillcount].value;
                statnum=0;
                beststat[0]="";
                for (i10=0;i10<best.length;i10++) {
                        chr=best.charAt(i10);
                        if (chr != " ") {
                                beststat[statnum]=beststat[statnum]+chr;
                        } else {
                                statnum++;
                                beststat[statnum]="";
                        }
                }
                beststatfactor=0;
                for (i11=0;i11<5;i11++) {
                        beststatfactor = beststatfactor + (weighting[i11]*((10.2*(Math.log(beststat[i11])))-12));
                }
                beststatfactor = beststatfactor/5;
                bestbonus = Math.floor(((beststatfactor+7)*(7+(0.025*level)))+correction);

                rwd.writeln(document.names[skillcount].value + level + "  0." + rate + "  " + bonus + "  (" + bestbonus +")  " + (bonus-bestbonus));
        }
        rwd.write("</PRE></BODY></HTML>");
        rwin.focus();


}

/* end Bonus Calculator */
/***************************/

/* Currency Converter */

	function getReturnData( data , statusCode , statusMessage) {
		//AJFORM failed. Submit form normally.
		if( statusCode != AJForm.STATUS['SUCCESS'] ) {
		 alert( "Status: " + statusMessage );
		 return true;

		}
		//AJFORM succeeded.
		else {
		 document.getElementById("results").innerHTML = data;
		}
	}

	function preProcess( thisForm ) {
		if( thisForm.elements['myName'].value == "" ) {
		 alert( "You did not enter a name. The data will not be sent to the server." );
		 return false;
		}
		else {
		 alert( "You successfully completed the form. The data will now be sent to the server." );
		 return true;
		}
	}

	function goForm(whichForm) {
	 thisForm = document.getElementById( whichForm );
	 // thisForm.submit() will not work using AJFORM. Instead, you need to use the following:
	 thisForm.ajform_submit();
	}


/* end Currency Converter */

/***************************/

/* Request-a-Blorple */

  function switchPrice( obj, url )
  {
	txt=obj.options[obj.selectedIndex].value;
	url2 = url + txt;
    divToSwitch = document.getElementById( "selectedbox" );
    newHTML = loadDoc( url2 );
    if( newHTML && newHTML.length>5 )
      divToSwitch.innerHTML = newHTML;
  }
  
/* end Request-a-Blorple */  

/***************************/

/* Drop Down Box */

  function switchBox( obj, url )
  {
	txt=obj.options[obj.selectedIndex].value;
	url2 = url + txt;
    divToSwitch = document.getElementById( "selectedbox" );
    newHTML = loadDoc( url2 );
    if( newHTML && newHTML.length>5 )
      divToSwitch.innerHTML = newHTML;
  }
  
/* end Drop Down Box */  

/***************************/

/* results div */

  function switchResults( obj, url )
  {
    divToSwitch = document.getElementById( "results" );
    newHTML = loadDoc( url );
    if( newHTML && newHTML.length>5 )
      divToSwitch.innerHTML = newHTML;
  }
  
/* end results div */  

/***************************/
/* Shop prices */

      function balsa_wand () {
	// calculates the price of a balsa wand
	if (document.getElementById( 'balsa_wand_charges' ).value > 0) {
	    price = 275 + (100 * document.getElementById( 'balsa_wand_charges' ).value);
	} else {
	    price = 0;
	}
	document.getElementById( 'balsa_wand_buy' ).value = "$" + Math.round(price * 100)/100;
      }

      function blue_ring () {
	   // calculates the price of a blue crystal ring
	   if (document.getElementById( 'blue_ring_charges' ).value > 0) {
	       price = 75.0 + (50.0 * document.getElementById( 'blue_ring_charges' ).value);
	   } else {
	       price = 0;
	   }
	   document.getElementById( 'blue_ring_buy' ).value = "$" + Math.round(price * 100)/100;
      }

      function crystals ()
           // calculates the price of imbued crystals
           {
           if (document.getElementById( 'crystal_bonus' ).value > 199)
               {
               // crystalcolour: amethyst = 1, aquamarine = 2, emerald = 3, jet = 4, ruby = 5
               // crystalsize: small = 1, large = 2
               baseprice = 3.0;                        // base crystal price
               imbmod = 20.0;                          // imbuing value divisor
               imboff = 100;                           // imbuing bonus for 0 value
               switch (document.getElementById( 'crystal_colour' ).value)       // spell value modifiers to imbuing value
                  {
                  case "1" :                           // amethyst
                     sval = 1.0;                       // value = 1.0
                     break;
                  case "2" :                           // aquamarine
                     sval = 1.0;                       // value = 1.0
                     break;
                  case "3" :                           // emerald
                     sval = 1.5;                       // value = 1.5
                     break;
                  case "4" :                           // jet
                     sval = 2.0;                       // value = 2.0
                     break;
                  case "5" :                           // ruby
                     sval = 1.5;                       // value = 1.5
                     break;
                  }
               switch (document.getElementById( 'crystal_size' ).value)      // crystal size based modifiers
                    {
                    case "1" :                         // small crsytals
                        szmod = 1.0;                   // size modifier for base crystal price
                        spmod = 1.0;                   // size modifier for imbuing bonus
                        break;
                    case "2" :                         // large crystals
                        szmod = 2.0;                   // size modifier for base crystal price
                        spmod = 1.6;                   // size modifier for imbuing bonus
                        break;
                    }
               imbuing = (document.getElementById( 'crystal_bonus' ).value - imboff) / imbmod;
               price = (baseprice * szmod) + (imbuing * sval * spmod);
               }
           else
               {
               price = 0;
               }
           document.getElementById( 'crystal_buy' ).value = "$" + Math.round(price * 100)/100;
           }

      function red_staff () {
           // calculates the price of a red staff
           if (document.getElementById( 'red_staff_charges' ).value > 0) {
               price = 15.0 + (5.0 * document.getElementById( 'red_staff_charges' ).value);
           } else {
               price = 0;
           }
           document.getElementById( 'red_staff_buy' ).value = "$" + Math.round(price * 100)/100;
      }

      function hp_tea () {
           // calculates the price per bottle of healing tea
           if (document.getElementById( 'hp_bonus' ).value > 299) {
           	price = (document.getElementById( 'hp_bonus' ).value - 100.0) / 10.0;
           } else {
           	price = 0;
           }
           document.getElementById( 'hp_buy' ).value = "$" + Math.round(price * 100)/100;
           }
	
      function gp_tea () {
           // calculates the price per bottle of gp tea
           if (document.getElementById( 'gp_bonus' ).value > 199) {
           	price = 2 * (document.getElementById( 'gp_bonus' ).value - 100.0) / 10.0;
           } else {
           	price = 0;
           }
           document.getElementById( 'gp_buy' ).value = "$" + Math.round(price * 100)/100;
           }
	   
      function curing_tea () {
           // calculates the price per bottle of gp tea
           if (document.getElementById( 'curing_bonus' ).value > 199) {
           	price = 0.2 * (document.getElementById( 'curing_bonus' ).value - 100.0) / 10.0;
           } else {
           	price = 0;
           }
           document.getElementById( 'curing_buy' ).value = "$" + Math.round(price * 100)/100;
           }
           
/* end Shop Prices */

/***************************/
