// JavaScript Document

/* This function is called from lottoNumGen and is used to scan the number string  */
/* for duplicates.                                                                 */
/*      strNum - The complete string to scan                                       */
/*      strComp - The integer value to scan for                                    */
/*      j - The number of integers to loop through                                 */
/***********************************************************************************/

function scanString(strNum,strComp,j) {
k = 1
while (k < j) {
		strLen  = strNum.length
		pos1    = strNum.indexOf(",")
		strSub1 = strNum.substring(0,pos1)
		pos1 ++
		strSub2 = strNum.substring(pos1,strLen)
		if (strSub1 == strComp) {
		return false
		}
		strNum = strSub2
		k ++
}
return true
}

/***********************************************************************************/
/* This function generates a series of random numbers given a range of numbers and */
/* the amount of numbers to create. After passing a few edits, the input is run    */
/* through a series of loops that ensures there are no duplicates and the numbers  */
/* fall within the specified range.                                                */
/***********************************************************************************/
function lottoNumGen(lottoGen) {
if (lottoGen.numRange1.value == "" || lottoGen.numRange2.value == "") {
alert("You must have a complete number pool to continue.")
return false
	} else if (lottoGen.numRange2.value == "0") {
		alert("The end value of your number range must be => 1.")
		return false
	} else if (lottoGen.numRange2.value <= lottoGen.numRange1.value) {
		alert("The end value of your number range must be > the start value.")
		return false		
	} else if (lottoGen.numCombo.value == "" || lottoGen.numCombo.value < "3" || lottoGen.numCombo.value > "9") {
		alert("The amount of numbers in your drawing must be between 3 and 9.")
		return false
	} else {
		// Set the range of numbers to work from
		num1 	  = lottoGen.numRange1.value
		num2 	  = eval(lottoGen.numRange2.value) + eval(1)
		numString = ""
		lastValue = ""
		numCount  = lottoGen.numCombo.value
		var myResults = new Array()
		myResults[0] = ""
		myResults[1] = ""
		myResults[2] = ""
		myResults[3] = ""
		myResults[4] = ""
		for (m=0;m<10;m++) {

// Set the loop for the amount of numbers to create
i = 0		
while (i < numCount) {

// Get a random number
numgen1 = Math.random()

// Multiply by 100 to get a base 10 and Modulo by the upper bound
numgen2 = Math.floor(numgen1 * 100) % num2

// Check for range and previous number
if (lastValue != numgen2 && numgen2 >= num1) {

// 1st time through, no dups
if (lastValue == "" && numString == "") {
numString = numgen2
i ++

// Save the last number added to the string
lastValue = numgen2						

// 2nd time through, no dups, not 1st number
} else if (i == 1) {
numString = numString + "," + numgen2
i ++					

// Save the last number added to the string
lastValue = numgen2							


// 3rd time through, scan entire string for duplicates

// if none, add to string, else get new number
} else if (scanString(numString,numgen2,i)) {
numString = numString + "," + numgen2
i ++					

// Save the last number added to the string
lastValue = numgen2							
} else {
numString = numString
}
}
}

// Add the number combination to an array and clear the strings
myResults[m] = numString
numString = ""
lastValue = ""
}
}

// Write the finished results to the page
lottoGen.Result1.value = myResults[0]
lottoGen.Result2.value = myResults[1]
lottoGen.Result3.value = myResults[2]
lottoGen.Result4.value = myResults[3]
lottoGen.Result5.value = myResults[4]
lottoGen.Result6.value = myResults[5]
lottoGen.Result7.value = myResults[6]
lottoGen.Result8.value = myResults[7]
lottoGen.Result9.value = myResults[8]
lottoGen.Result10.value = myResults[9]
}
//-->
