
var indx = 1;

//function for addition of a row

function addSysRes(id)
{
  var x = document.getElementById(id).insertRow(indx+2);
  var cel1 = x.insertCell(0);
  var cel2 = x.insertCell(1);
  var cel3 = x.insertCell(2);
  var cel4 = x.insertCell(3);
  var cel5 = x.insertCell(4);
  var cel6 = x.insertCell(5);
  
  cel1.innerHTML = "<br><INPUT class=RightAlign maxLength=10 size=12 value='Server 1'><BR><select style='font-size:smaller'><option>CPU<option>Memory <option>Disk<option>Network &nbsp;&nbsp;&nbsp;&nbsp;</select>"
  
  cel2.innerHTML = "<INPUT type='text' class=RightAlign maxLength=7 size=7 name='baseMrk'>"
  
  cel3.innerHTML = "<INPUT type='text' class=RightAlign maxLength=7 size=7 name='tgtMrk'>"
  
  cel4.innerHTML = "<INPUT type='text' class=RightAlign maxLength=7 size=7 name='baseThres' value='100'>%"
  
  cel5.innerHTML = "<INPUT type='text'  style='text-align:center' disabled='true' class=RightAlign maxLength=7 size=7 name='capFtr'>"
  
  cel6.innerHTML = "<INPUT type='hidden'  style='text-align:center' disabled='true' class=RightAlign size=1 name='testWrkLd'>"
  
  indx = indx+1;
  
}


//function for deletion of a row

function deleteSysRes(id)
{
	if(indx >= 2)
	{
		document.getElementById(id).deleteRow((indx-1)+2);
		indx = indx-1;
	}
	else
	{
		alert("No rows to be deleted");
	}
}

//validation of input fields in screen1

function getValues(value)
{
	var fnlValue = "";
	for (k=0;k<(value.length-1);k++)
	{
		//removing % from input
		firstCh = value.charAt(k);
		if(isNaN(firstCh) && (firstCh!="."))
		{
			alert("Enter a positive value");
			return false;
		}
		else
		{
			fnlValue+=firstCh;
		}
	}
	return fnlValue;
}


function testFormValidate()
{
	
	tgtIncrLoad = document.ptest.tgtIncrLoad.value;
	lastCh = tgtIncrLoad.charAt(tgtIncrLoad.length-1);
	if (tgtIncrLoad < 0 || isNaN(tgtIncrLoad) || (tgtIncrLoad==""))
	{
		if(lastCh!="%")
		{
			alert("Enter the increase expected in consolidated workload");
			document.ptest.elements[0].value = "";
			document.ptest.elements[0].focus();
			return false;
		}
	}
	
	var firstCh;
	var lastCh;
	for(i=0;i<=indx;i++)
	{
		//Validation of Base Threshold
		baseThres = document.ptest.baseThres[i].value;
		firstCh = baseThres.charAt(0);
		lastCh = baseThres.charAt(baseThres.length-1);
	
		if (baseThres < 0 || baseThres > 100 || isNaN(baseThres) || (baseThres=="") || (baseThres==null) || (firstCh==0))
		{
			if(lastCh != "%")
			{
				alert("Enter a positive value");
				document.ptest.baseThres[i].value = "";
				document.ptest.baseThres[i].focus();
				return false;
			}
		}
		
		
		//Validation of Base Benchmark
		baseMrk = document.ptest.baseMrk[i].value;
		firstCh = baseMrk.charAt(0);
		
		if( firstCh == "-" || isNaN(baseMrk) || (baseMrk==null) || (baseMrk=="") || firstCh == 0)
		{
			alert("Enter a positive value for Base Benchmark");
			document.ptest.baseMrk[i].value = "";
			document.ptest.baseMrk[i].focus();
			return false;
		}
		
		//Validation of Target Benchmark
		tgtMrk = document.ptest.tgtMrk[i].value;
		firstCh = tgtMrk.charAt(0);
		
		if( firstCh == "-" || isNaN(tgtMrk) || (tgtMrk==null) || (tgtMrk=="") || firstCh == 0)
		{
			alert("Enter a positive value for Target Benchmark");
			document.ptest.tgtMrk[i].value = "";
			document.ptest.tgtMrk[i].focus();
			return false;
		}
		
	}
		
	pTestCalc()
}


//calculation of capacity factor

function pTestCalc()
{
	var tgtIncrLoad = "";
	value = document.ptest.tgtIncrLoad.value;
	lastCh = value.charAt(value.length-1);
	if(lastCh=="%")
	{
		for (k=0;k<(value.length-1);k++)
		{
			//removing % from input
			firstCh = value.charAt(k);
			if(isNaN(firstCh) && (firstCh!="."))
			{
				alert("Enter the increase expected in consolidated workload");
				document.ptest.elements[0].value = "";
				document.ptest.elements[0].focus();
				return false;
			}
			else
			{
				tgtIncrLoad+= firstCh;
			}
		}
	}
	else
	{
		tgtIncrLoad = document.ptest.tgtIncrLoad.value;
	}
			
	var value;
	var wrkLoad=tgtIncrLoad;
	for(i=0;i<=indx;i++)
	{
		value = document.ptest.baseThres[i].value;
		lastCh = value.charAt(value.length-1);
		if(lastCh == "%")
		{
			fnlValue = getValues(value);
			baseThres = fnlValue;
		}
		else
		{
			baseThres = document.ptest.baseThres[i].value;
		}
		
		baseMrk = document.ptest.baseMrk[i].value;
		tgtMrk = document.ptest.tgtMrk[i].value;
		document.ptest.capFtr[i].value = roundOff(parseFloat(tgtMrk) / parseFloat(baseMrk));
		
		document.ptest.testWrkLd[i].value = roundOff((tgtIncrLoad / document.ptest.capFtr[i].value) * (parseFloat(baseThres) / 100));
		
		if(parseFloat(document.ptest.testWrkLd[i].value) < wrkLoad)
		{
			wrkLoad = document.ptest.testWrkLd[i].value;
			document.ptest.wrkLoad.value = document.ptest.testWrkLd[i].value;
		}
		
	}
	
}

