
var timeIntr;
var load;
var chaos;
var val;
var ite;
var i;
var j;
var k;
var currItrn = new Array();
var prevItrn = new Array();
var freqVal = new Array();

		
function roundOff(val)
{
	//Round-off to 2 decimal places
	var len;
	var ans;
	ans = val * 1000;
	ans = Math.round(ans /10) + "";
	while (ans.length < 3)
	{
		ans = "0" + ans;
	}
	len = ans.length;
	val = ans.substring(0,len-2) + "." + ans.substring(len-2,len);
	return val;
}

function frqDist(power)
{
	//Frequency distribution
	var loop;
	loop = 0;
	ite = ite+1;
	i = 0;
	j = 0;
	
	while (loop <= (ite-2))
	{
		if (loop != (ite-2))
		{
			prevItrn[j+1] = currItrn[i+1];
			currItrn[++i] = prevItrn[j] + prevItrn[++j];
			if((ite-1) == power)
			{
				freqVal[k++] = currItrn[i];
			}
		}
		else
		{
			currItrn[++i] = 1;
			if((ite-1) == power)
			{
				freqVal[k++] = currItrn[i];
			}
		}
		
		loop = loop + 1;
	}
}


function formValidate()
{
		var durtnHr;
		var durtnMn;
		var durtnSe;
		var firstCh = 0;
		timeIntr=0;
		var n;
		
		//Validation of duration
		
		n = document.selfsimilar.durationhr.selectedIndex;    // Which menu item is selected
		durtnHr = document.selfsimilar.durationhr[n].text;    // Return string value of menu item
		
		n = document.selfsimilar.durationmn.selectedIndex;   
		durtnMn = document.selfsimilar.durationmn[n].text;      
		
		n = document.selfsimilar.durationse.selectedIndex;
		durtnSe = document.selfsimilar.durationse[n].text;      
		
		timeIntr=(parseFloat(durtnHr)*60*60) + (parseFloat(durtnMn)*60) + parseFloat(durtnSe);
		
		if (timeIntr==0)
		{
				alert("Select the duration");
				return false;
		}
		
		//Validation of load
		load = document.selfsimilar.load.value;
		firstCh = document.forms[0].elements[3].value.charAt(0);
		
		if( firstCh == "-" || isNaN(load) || (load==null) || (load=="") || firstCh == 0)
		{
			alert("Enter a positive integer value for load");
			document.forms[0].elements[3].value = "";
			document.forms[0].elements[3].focus();
			return false;
		}
		
		//Validation of chaos
		chaos = document.selfsimilar.chaos.value;
		
		if (chaos < 0.5 || chaos > 1)
		{
			alert("Enter a value between 0.5 and 1 for Chaos factor");
			document.forms[0].elements[4].value = "";
			document.forms[0].elements[4].focus();
			return false;
		}
		calc(timeIntr,load,chaos)
}

function calc(timeIntr,load,chaos)
{
		var prevTimeIntr;
		var power;
		var currPwr;
		var prevPwr;
		var totfreq;
		var val;
		var tempHitVal = new Array();
		var hitVal = new Array();
		var freq = new Array();
		var prob = new Array();
		var cdf = new Array();
		var content;
		
		//Open a new window to display the results
		var newWindow = window.open("","","status,scrollbars=yes,height=300,width=410");
		newWindow.document.write("<html><head><title>Self-Similar Traffic Generator</title><link rel='stylesheet' href='cssnw.css' type='text/css'> </head> </html>");
				
		newWindow.document.write("<TABLE width=380 align=center bgcolor='#FFFFFF'><TBODY><TR><TD class=style2 width=500><H1><font color='#6698FF'> Self-Similar Traffic Generator Results</font></H1><br>")	;
		
		//Display the input in result window
		newWindow.document.write("<font size=1> Duration: " + timeIntr + "</font><br>")	;
		newWindow.document.write("Load: " + load + "<br>");	
		newWindow.document.write("Chaos Factor: " + chaos + "<br><br>");
		
		prevTimeIntr = parseInt(timeIntr);
		power = 0;
		
		//Calculate power
		while (timeIntr > Math.pow(2,power))
		{
			power = power + 1;
		}
		
		//Check whether duration is power of 2....if not,make it to nearest power of 2
		currPwr = Math.abs(Math.pow(2,power) - parseInt(timeIntr));
		prevPwr = Math.abs(Math.pow(2,(power-1)) - parseInt(timeIntr));
		if(currPwr < prevPwr)
		{
			timeIntr = parseInt(Math.pow(2,power));
		} 
		else
		{
			timeIntr = parseInt(Math.pow(2,(power-1)));
			power = power-1;
		}
		
		//Increase hit value proportionally
		if (timeIntr != prevTimeIntr)
		{
			load = parseFloat(load) * (timeIntr/prevTimeIntr);
		}
		
		newWindow.document.write("<br/>");
		
		//Generate freq. distribution
		ite = 1;
		i = 0;
		j = 0;
		k = 0;
		currItrn[1] = 1;
		prevItrn[0] = 1;
		prevItrn[1] = 1;
		freqVal[k++] = 1;
		
		while (timeIntr > 1)
		{
			frqDist(power);
			timeIntr = timeIntr / 2;
		}
		
		//Calculate hit value and round-off hit value to 2 decimal places
		len = 0;
		ans = 0;
		i = 0;
		while (i <= power)
		{		
			val = load * (Math.pow(chaos,(power-i))) * (Math.pow((1-chaos),i));
			tempHitVal[i] = roundOff(val);
			i = i+1;
		}
		
		//If 2 consecutive hit values are same then add their freq.s
		i = power;
		k = 0;
		hitVal[0] = tempHitVal[i];
		freq[0] = freqVal[i];
		while (i <= power && i > 0)
		{
			if (tempHitVal[i] == tempHitVal[i-1]) 
			{
				freqVal[i-1] = freqVal[i] + freqVal[i-1];
				freq[k] = freqVal[i-1];
				hitVal[k] = tempHitVal[i-1];
			}
			else			//Else donot add freq.s
			{
				k = k+1;
				freq[k] = freqVal[i-1];
				hitVal[k] = tempHitVal[i-1];
			}
			i = i-1;
		}
		
		//Calculate total frequency
		totfreq = 0;
		i = 0;
		while (i <= k)	
		{
			totfreq = totfreq+freq[i];
			i = i+1;
		}
		
		//Calculate probability and percentile..expressed in terms of % & rounded-off to 2 decimal places
		i = 0;
		cdf[i-1] = 0;
		
		while (i <= k)
		{
			val = (freq[i]/totfreq) * 100;
			prob[i] = roundOff(val);
			val = parseFloat(prob[i]) + parseFloat(cdf[i-1]);
			cdf[i] = roundOff(val);
			if(i == k)
			{
				 cdf[i] = Math.round(cdf[i]);
			}
			i = i+1;
		}	
		
		//Create a table to display the values
		content = "<CENTER><TABLE BORDER>";
		
		content += "<TR><TH align='center' bgcolor='#4863A0'><font size='2' face='Arial' color='#FFFFFF'> Hit Value </font></TH><TH align='center' bgcolor='#4863A0'><font size='2'face='Arial' color='#FFFFFF'> Frequency </TH><TH align='center' bgcolor='#4863A0'><font size='2'face='Arial' color='#FFFFFF'>  Probability (%) &nbsp </TH><TH align='center' bgcolor='#4863A0'><font size='2'face='Arial' color='#FFFFFF'> Percentile (%) &nbsp&nbsp </TH></TR>";
		
		
		//Enter table data
		i = 0;
		while (i <= k)
		{
			content += "<TD bgcolor='#F5F5F5'>" + hitVal[i] + "</TD>";
			content += "<TD bgcolor='#F5F5F5'>" + freq[i]+ "</TD>" ;
			content += "<TD bgcolor='#F5F5F5'>" + prob[i] + "</TD>";
			content += "<TD bgcolor='#F5F5F5'>" + cdf[i] + "</TD>";
			content += "</TR>";
			i = i+1;
		}
		content += "</TABLE></CENTER><br><br></td></tr></tbody></table>";
		newWindow.document.write(content);
		
}

