function DiceRoll(droll)
{
	this.diceroll = droll;
	this.singleDiceResult = [];
	this.result = 0;
	this.Roll = ComputeDiceroll;
}
function guidGenerator() {
    var S4 = function() {
       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    };
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
function Result(high,res)
{
	this.res = res;
	this.high = high;
}
function ComputeDiceroll()
{
	var diceroll = this.diceroll;
		
	// now to work out the dice roll
	// first seperate into strings
	var components = [];
	// get number of + and -
	var singleComponent = "";
	
	for(i = 0; i < diceroll.length; i++)
	{
		if(diceroll[i] != '+' && diceroll[i] != '-' && diceroll[i] != '*' && diceroll[i] != '/' && diceroll[i] != ' ')
		{
			singleComponent = singleComponent.concat(diceroll[i]);
		}
		else if(diceroll[i] == ' ')
		{
		}
		else
		{
			components.push(singleComponent);
			components.push(diceroll[i]);
			singleComponent = "";
		}
	}
	components.push(singleComponent);

	var operator = '+';
	
	// go through all components and work out the final number for each
	var finalMath = this.result;
	for(x = 0; x < components.length; x++)
	{
		var currentComponent = components[x];
		//check to see if it's a dice
		if(currentComponent.search(/d/) >= 0)
		{
			// it's got a d so it's a dice
			// work out how many to multply by
			var multiple = 1;
			if(currentComponent.search(/d/) != 0)
			{
				// d is not the first character, so there's a number before it
				multiple = currentComponent.substring(0,currentComponent.search(/d/));
			}
			// high num of the roll
			var highnum = currentComponent.substring(currentComponent.search(/d/)+1);
			
			// get random number between 0 and high multiple times
			for(y = 0; y < multiple; y++)
			{
				// add each dice to the single diceroll
				switch(operator)
				{
					case '+':
						var r = 0 
						$.ajax({
							url: "/forum/dmtools/dmtools-code.php",
							async: false,
							data: {function : "roll", highnum : highnum},
							success: function (data, textStatus, jqXHR) {
								r = Number(data);	
							}
						});
						var dd = new Result(highnum, r);
						this.singleDiceResult.push(dd);
						finalMath += r;
						break;
					case '-':
						var r = 0;
						$.ajax({
							url: "/forum/dmtools/dmtools-code.php",
							async: false,
							data: {function : "roll", highnum : highnum},
							success: function (data, textStatus, jqXHR) {
								r = Number(data);	
							}
						});
						var dd = new Result(highnum, r);
						this.singleDiceResult.push(dd);
						finalMath -= r;
						break;
					case '*':
						var r = 0;
						$.ajax({
							url: "/forum/dmtools/dmtools-code.php",
							async: false,
							data: {function : "roll", highnum : highnum},
							success: function (data, textStatus, jqXHR) {
								r = Number(data);	
							}
						});
						var dd = new Result(highnum, r);
						this.singleDiceResult.push(dd);
						finalMath = finalMath * r;
						break;
					case '/':
						var r = 0;
						$.ajax({
							url: "/forum/dmtools/dmtools-code.php",
							async: false,
							data: {function : "roll", highnum : highnum},
							success: function (data, textStatus, jqXHR) {
								r = Number(data);	
							}
						});
						var dd = new Result(highnum, r);
						this.singleDiceResult.push(dd);
						finalMath = finalMath / r;
						break;
								
				}
			}
		}
		// check to see if it's normal number
		else if(currentComponent.search(/[+-]|\*|\//) < 0)
		{
			// not a plus or minus
			switch(operator)
				{
					case '+':
						finalMath = Number(finalMath) + Number(currentComponent);
						break;
					case '-':
						finalMath = Number(finalMath) - Number(currentComponent);
						break;
					case '*':
						finalMath = Number(finalMath) * Number(currentComponent);
						break;
					case '/':
						finalMath = Number(finalMath) / Number(currentComponent);
						break;
				}
		}
		// check for operator and change it
		else
		{
			operator = currentComponent;	
		}
	}
	
	this.result = finalMath;
	return finalMath;
}

function rand(from, to) {
	return Math.floor(Math.random() * (to - from + 1) + from);
}// JavaScript Document
